From bbba275913692c026af87aed4ee6455b498b16db Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sat, 20 Sep 2008 15:53:17 +0000 Subject: Little readability tweak svn-id: r34615 --- common/str.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'common/str.cpp') diff --git a/common/str.cpp b/common/str.cpp index a415e376c9..d1fed68844 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -79,15 +79,17 @@ void String::initWithCStr(const char *str, uint32 len) { } String::String(const String &str) - : _size(str._size), _str(str.isStorageIntern() ? _storage : str._str) { + : _size(str._size) { if (str.isStorageIntern()) { // String in internal storage: just copy it - memcpy(_storage, str._storage, sizeof(_storage)); + memcpy(_storage, str._storage, _builtinCapacity); + _str = _storage; } else { // String in external storage: use refcount mechanism str.incRefCount(); _extern._refCount = str._extern._refCount; _extern._capacity = str._extern._capacity; + _str = str._str; } assert(_str != 0); } -- cgit v1.2.3 From f9650f0b6a07d4f6b835f1fe0b89ced58a504fe1 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 24 Sep 2008 20:29:29 +0000 Subject: Changed Common::String to use a MemoryPool for its refcounts svn-id: r34642 --- common/str.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'common/str.cpp') diff --git a/common/str.cpp b/common/str.cpp index d1fed68844..6c48738533 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -26,6 +26,13 @@ #include "common/hash-str.h" #include "common/util.h" +#include "common/memorypool.h" + +#if !defined(__SYMBIAN32__) +#include +#endif + + namespace Common { #if !(defined(PALMOS_ARM) || defined(PALMOS_DEBUG) || defined(__GP32__)) @@ -34,6 +41,9 @@ const String String::emptyString; const char *String::emptyString = ""; #endif + +MemoryPool *g_refCountPool = 0; // FIXME: This is never freed right now + static uint32 computeCapacity(uint32 len) { // By default, for the capacity we use the next multiple of 32 return ((len + 32 - 1) & ~0x1F); @@ -180,7 +190,11 @@ void String::ensureCapacity(uint32 new_size, bool keep_old) { void String::incRefCount() const { assert(!isStorageIntern()); if (_extern._refCount == 0) { - _extern._refCount = new int(2); + if (g_refCountPool == 0) + g_refCountPool = new MemoryPool(sizeof(int)); + + _extern._refCount = (int *)g_refCountPool->malloc(); + *_extern._refCount = 2; } else { ++(*_extern._refCount); } @@ -196,7 +210,10 @@ void String::decRefCount(int *oldRefCount) { if (!oldRefCount || *oldRefCount <= 0) { // The ref count reached zero, so we free the string storage // and the ref count storage. - delete oldRefCount; + if (oldRefCount) { + assert(g_refCountPool); + g_refCountPool->free(oldRefCount); + } free(_str); // Even though _str points to a freed memory block now, -- cgit v1.2.3