From 3f40dcb6b457b3560ba6e5c190e52b18b5531bc2 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Fri, 25 Oct 2013 22:53:25 +0200 Subject: [PATCH] PackageWriterImpl::_AttributeRemoved(): Update string cache When removing a string attribute, decrement the referenced string's usage count in the string cache. This fixes the potentially incorrect usage counts in update mode. Not a serious problem, but it could lead to only singly (or no longer) used strings to be written to the string subsection instead of encoding them inline and thus to slightly greater file sizes. --- headers/private/package/hpkg/Strings.h | 10 +++++----- src/kits/package/hpkg/PackageWriterImpl.cpp | 3 ++- src/kits/package/hpkg/Strings.cpp | 14 +++++++++++++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/headers/private/package/hpkg/Strings.h b/headers/private/package/hpkg/Strings.h index afdbd826e6..dcd4da9e4d 100644 --- a/headers/private/package/hpkg/Strings.h +++ b/headers/private/package/hpkg/Strings.h @@ -1,5 +1,5 @@ /* - * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. + * Copyright 2009-2013, Ingo Weinhold, ingo_weinhold@gmx.de. * Distributed under the terms of the MIT License. */ #ifndef _PACKAGE__HPKG__PRIVATE__STRINGS_H_ @@ -89,11 +89,11 @@ struct CachedStringUsageGreater { struct StringCache : public CachedStringTable { - StringCache(); - ~StringCache(); - - CachedString* Get(const char* value); + StringCache(); + ~StringCache(); + CachedString* Get(const char* value); + void Put(CachedString* string); }; diff --git a/src/kits/package/hpkg/PackageWriterImpl.cpp b/src/kits/package/hpkg/PackageWriterImpl.cpp index cf2dd504ec..7d52ed9190 100644 --- a/src/kits/package/hpkg/PackageWriterImpl.cpp +++ b/src/kits/package/hpkg/PackageWriterImpl.cpp @@ -1052,7 +1052,8 @@ PackageWriterImpl::_AttributeRemoved(Attribute* attribute) && value.encoding == B_HPKG_ATTRIBUTE_ENCODING_RAW_HEAP) { if (!fHeapRangesToRemove->AddRange(value.data.offset, value.data.size)) throw std::bad_alloc(); - } + } else if (value.type == B_HPKG_ATTRIBUTE_TYPE_STRING) + fStringCache.Put(value.string); for (DoublyLinkedList::Iterator it = attribute->children.GetIterator(); diff --git a/src/kits/package/hpkg/Strings.cpp b/src/kits/package/hpkg/Strings.cpp index 4f87d8e20c..32210fd2f4 100644 --- a/src/kits/package/hpkg/Strings.cpp +++ b/src/kits/package/hpkg/Strings.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. + * Copyright 2009-2013, Ingo Weinhold, ingo_weinhold@gmx.de. * Distributed under the terms of the MIT License. */ @@ -70,6 +70,18 @@ StringCache::Get(const char* value) } +void +StringCache::Put(CachedString* string) +{ + if (string != NULL) { + if (--string->usageCount == 0) { + Remove(string); + delete string; + } + } +} + + } // namespace BPrivate } // namespace BHPKG