diff options
Diffstat (limited to 'thirdparty/icu4c/common/serv.cpp')
-rw-r--r-- | thirdparty/icu4c/common/serv.cpp | 101 |
1 files changed, 45 insertions, 56 deletions
diff --git a/thirdparty/icu4c/common/serv.cpp b/thirdparty/icu4c/common/serv.cpp index ce545b9dbd..0c54a4dce9 100644 --- a/thirdparty/icu4c/common/serv.cpp +++ b/thirdparty/icu4c/common/serv.cpp @@ -8,6 +8,7 @@ */ #include "unicode/utypes.h" +#include "unicode/localpointer.h" #if !UCONFIG_NO_SERVICE @@ -237,7 +238,7 @@ public: * you're removing pointer to this somewhere. Management of that * pointer will have to know how to deal with refcounts. Once * the refcount drops to zero, the resource is released. Return - * false if the resouce has been released. + * false if the resource has been released. */ CacheEntry* unref() { if ((--refcount) == 0) { @@ -256,20 +257,13 @@ public: } }; -// UObjectDeleter for serviceCache +// Deleter for serviceCache U_CDECL_BEGIN static void U_CALLCONV cacheDeleter(void* obj) { U_NAMESPACE_USE ((CacheEntry*)obj)->unref(); } -/** -* Deleter for UObjects -*/ -static void U_CALLCONV -deleteUObject(void *obj) { - U_NAMESPACE_USE delete (UObject*) obj; -} U_CDECL_END /* @@ -418,12 +412,6 @@ private: UBool fActive; }; -struct UVectorDeleter { - UVector* _obj; - UVectorDeleter() : _obj(NULL) {} - ~UVectorDeleter() { delete _obj; } -}; - // called only by factories, treat as private UObject* ICUService::getKey(ICUServiceKey& key, UnicodeString* actualReturn, const ICUServiceFactory* factory, UErrorCode& status) const @@ -454,6 +442,7 @@ ICUService::getKey(ICUServiceKey& key, UnicodeString* actualReturn, const ICUSer if (serviceCache == NULL) { ncthis->serviceCache = new Hashtable(status); if (ncthis->serviceCache == NULL) { + status = U_MEMORY_ALLOCATION_ERROR; return NULL; } if (U_FAILURE(status)) { @@ -464,7 +453,7 @@ ICUService::getKey(ICUServiceKey& key, UnicodeString* actualReturn, const ICUSer } UnicodeString currentDescriptor; - UVectorDeleter cacheDescriptorList; + LocalPointer<UVector> cacheDescriptorList; UBool putInCache = FALSE; int32_t startIndex = 0; @@ -502,18 +491,17 @@ ICUService::getKey(ICUServiceKey& key, UnicodeString* actualReturn, const ICUSer int32_t index = startIndex; while (index < limit) { ICUServiceFactory* f = (ICUServiceFactory*)factories->elementAt(index++); - UObject* service = f->create(key, this, status); + LocalPointer<UObject> service(f->create(key, this, status)); if (U_FAILURE(status)) { - delete service; return NULL; } - if (service != NULL) { - result = new CacheEntry(currentDescriptor, service); + if (service.isValid()) { + result = new CacheEntry(currentDescriptor, service.getAlias()); if (result == NULL) { - delete service; status = U_MEMORY_ALLOCATION_ERROR; return NULL; } + service.orphan(); // result now owns service. goto outerEnd; } @@ -524,19 +512,22 @@ ICUService::getKey(ICUServiceKey& key, UnicodeString* actualReturn, const ICUSer // don't want to keep querying on an id that's going to // fallback to the one that succeeded, we want to hit the // cache the first time next goaround. - if (cacheDescriptorList._obj == NULL) { - cacheDescriptorList._obj = new UVector(uprv_deleteUObject, NULL, 5, status); + if (cacheDescriptorList.isNull()) { + cacheDescriptorList.adoptInsteadAndCheckErrorCode(new UVector(uprv_deleteUObject, NULL, 5, status), status); if (U_FAILURE(status)) { return NULL; } } - UnicodeString* idToCache = new UnicodeString(currentDescriptor); - if (idToCache == NULL || idToCache->isBogus()) { + + LocalPointer<UnicodeString> idToCache(new UnicodeString(currentDescriptor), status); + if (U_FAILURE(status)) { + return NULL; + } + if (idToCache->isBogus()) { status = U_MEMORY_ALLOCATION_ERROR; return NULL; } - - cacheDescriptorList._obj->addElement(idToCache, status); + cacheDescriptorList->adoptElement(idToCache.orphan(), status); if (U_FAILURE(status)) { return NULL; } @@ -550,9 +541,9 @@ outerEnd: return NULL; } - if (cacheDescriptorList._obj != NULL) { - for (int32_t i = cacheDescriptorList._obj->size(); --i >= 0;) { - UnicodeString* desc = (UnicodeString*)cacheDescriptorList._obj->elementAt(i); + if (cacheDescriptorList.isValid()) { + for (int32_t i = cacheDescriptorList->size(); --i >= 0;) { + UnicodeString* desc = (UnicodeString*)cacheDescriptorList->elementAt(i); serviceCache->put(*desc, result, status); if (U_FAILURE(status)) { @@ -560,7 +551,7 @@ outerEnd: } result->ref(); - cacheDescriptorList._obj->removeElementAt(i); + cacheDescriptorList->removeElementAt(i); } } } @@ -613,6 +604,7 @@ ICUService::getVisibleIDs(UVector& result, const UnicodeString* matchID, UErrorC if (U_FAILURE(status)) { return result; } + UObjectDeleter *savedDeleter = result.setDeleter(uprv_deleteUObject); { Mutex mutex(&lock); @@ -620,7 +612,7 @@ ICUService::getVisibleIDs(UVector& result, const UnicodeString* matchID, UErrorC if (map != NULL) { ICUServiceKey* fallbackKey = createKey(matchID, status); - for (int32_t pos = UHASH_FIRST;;) { + for (int32_t pos = UHASH_FIRST; U_SUCCESS(status); ) { const UHashElement* e = map->nextElement(pos); if (e == NULL) { break; @@ -633,17 +625,11 @@ ICUService::getVisibleIDs(UVector& result, const UnicodeString* matchID, UErrorC } } - UnicodeString* idClone = new UnicodeString(*id); - if (idClone == NULL || idClone->isBogus()) { - delete idClone; + LocalPointer<UnicodeString> idClone(new UnicodeString(*id), status); + if (U_SUCCESS(status) && idClone->isBogus()) { status = U_MEMORY_ALLOCATION_ERROR; - break; - } - result.addElement(idClone, status); - if (U_FAILURE(status)) { - delete idClone; - break; } + result.adoptElement(idClone.orphan(), status); } delete fallbackKey; } @@ -651,6 +637,7 @@ ICUService::getVisibleIDs(UVector& result, const UnicodeString* matchID, UErrorC if (U_FAILURE(status)) { result.removeAllElements(); } + result.setDeleter(savedDeleter); return result; } @@ -798,7 +785,7 @@ ICUService::getDisplayNames(UVector& result, } const UnicodeString* dn = (const UnicodeString*)entry->key.pointer; StringPair* sp = StringPair::create(*id, *dn, status); - result.addElement(sp, status); + result.adoptElement(sp, status); if (U_FAILURE(status)) { result.removeAllElements(); break; @@ -846,32 +833,34 @@ ICUService::createSimpleFactory(UObject* objToAdopt, const UnicodeString& id, UB } URegistryKey -ICUService::registerFactory(ICUServiceFactory* factoryToAdopt, UErrorCode& status) +ICUService::registerFactory(ICUServiceFactory* factoryToAdopt, UErrorCode& status) { - if (U_SUCCESS(status) && factoryToAdopt != NULL) { + LocalPointer<ICUServiceFactory>lpFactoryToAdopt(factoryToAdopt); + if (U_FAILURE(status) || factoryToAdopt == nullptr) { + return nullptr; + } + { Mutex mutex(&lock); - if (factories == NULL) { - factories = new UVector(deleteUObject, NULL, status); + if (factories == nullptr) { + LocalPointer<UVector> lpFactories(new UVector(uprv_deleteUObject, nullptr, status), status); if (U_FAILURE(status)) { - delete factories; - return NULL; + return nullptr; } + factories = lpFactories.orphan(); } - factories->insertElementAt(factoryToAdopt, 0, status); + factories->insertElementAt(lpFactoryToAdopt.orphan(), 0, status); if (U_SUCCESS(status)) { clearCaches(); - } else { - delete factoryToAdopt; - factoryToAdopt = NULL; } - } + } // Close of mutex lock block. - if (factoryToAdopt != NULL) { + if (U_SUCCESS(status)) { notifyChanged(); + return (URegistryKey)factoryToAdopt; + } else { + return nullptr; } - - return (URegistryKey)factoryToAdopt; } UBool |