]> granicus.if.org Git - icu/commitdiff
ICU-20202 Replace CharStringPool with MemoryPool.
authorFredrik Roubert <roubert@google.com>
Mon, 29 Oct 2018 21:12:00 +0000 (22:12 +0100)
committerFredrik Roubert <fredrik@roubert.name>
Mon, 29 Oct 2018 21:58:33 +0000 (22:58 +0100)
The shared templated helper class MemoryPool is a drop-in replacement
for the local helper class CharStringPool, with a simpler implementation
and an interface that allows parameters to be passed to the constructor.

icu4c/source/common/uloc_tag.cpp

index 8146ec3077a43797f41cd07d6c1fcfdd5a024b06..89702968cd7d65aea016fac92747a4723af2c207 100644 (file)
@@ -19,7 +19,6 @@
 #include "putilimp.h"
 #include "uinvchar.h"
 #include "ulocimp.h"
-#include "uvector.h"
 #include "uassert.h"
 
 
@@ -348,46 +347,6 @@ static const char*
 ultag_getGrandfathered(const ULanguageTag* langtag);
 #endif
 
-namespace {
-
-// Helper class to memory manage CharString objects.
-// Only ever stack-allocated, does not need to inherit UMemory.
-class CharStringPool {
-public:
-    CharStringPool() : status(U_ZERO_ERROR), pool(&deleter, nullptr, status) {}
-    ~CharStringPool() = default;
-
-    CharStringPool(const CharStringPool&) = delete;
-    CharStringPool& operator=(const CharStringPool&) = delete;
-
-    icu::CharString* create() {
-        if (U_FAILURE(status)) {
-            return nullptr;
-        }
-        icu::CharString* const obj = new icu::CharString;
-        if (obj == nullptr) {
-            status = U_MEMORY_ALLOCATION_ERROR;
-            return nullptr;
-        }
-        pool.addElement(obj, status);
-        if (U_FAILURE(status)) {
-            delete obj;
-            return nullptr;
-        }
-        return obj;
-    }
-
-private:
-    static void U_CALLCONV deleter(void* obj) {
-        delete static_cast<icu::CharString*>(obj);
-    }
-
-    UErrorCode status;
-    icu::UVector pool;
-};
-
-}  // namespace
-
 /*
 * -------------------------------------------------
 *
@@ -1105,7 +1064,7 @@ _appendKeywordsToLanguageTag(const char* localeID, icu::ByteSink& sink, UBool st
         AttributeListEntry *firstAttr = NULL;
         AttributeListEntry *attr;
         char *attrValue;
-        CharStringPool extBufPool;
+        icu::MemoryPool<icu::CharString> extBufPool;
         const char *bcpKey=nullptr, *bcpValue=nullptr;
         UErrorCode tmpStatus = U_ZERO_ERROR;
         int32_t keylen;
@@ -1285,12 +1244,12 @@ _appendKeywordsToLanguageTag(const char* localeID, icu::ByteSink& sink, UBool st
                     }
                 }
                 bcpKey = key;
-                icu::CharString* extBuf = extBufPool.create();
+                icu::CharString* extBuf =
+                    extBufPool.create(buf.data(), len, tmpStatus);
                 if (extBuf == nullptr) {
                     *status = U_MEMORY_ALLOCATION_ERROR;
                     break;
                 }
-                extBuf->append(buf.data(), len, tmpStatus);
                 if (U_FAILURE(tmpStatus)) {
                     *status = tmpStatus;
                     break;