X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/73c04bcfe1096173b00431f0cdc742894b15eef0..7393aa2fd2c40c89f12c2db881842a563afdb429:/icuSources/common/uvector.cpp diff --git a/icuSources/common/uvector.cpp b/icuSources/common/uvector.cpp index 028ef39f..2c9efbc2 100644 --- a/icuSources/common/uvector.cpp +++ b/icuSources/common/uvector.cpp @@ -93,12 +93,14 @@ UVector::~UVector() { */ void UVector::assign(const UVector& other, UTokenAssigner *assign, UErrorCode &ec) { if (ensureCapacity(other.count, ec)) { - setSize(other.count); - for (int32_t i=0; i= minimumCapacity) { - return TRUE; - } else { + if (capacity < minimumCapacity) { int32_t newCap = capacity * 2; if (newCap < minimumCapacity) { newCap = minimumCapacity; } - UHashTok* newElems = (UHashTok *)uprv_malloc(sizeof(UHashTok)*newCap); - if (newElems == 0) { + UHashTok* newElems = (UHashTok *)uprv_realloc(elements, sizeof(UHashTok)*newCap); + if (newElems == NULL) { + // We keep the original contents on the memory failure on realloc. status = U_MEMORY_ALLOCATION_ERROR; return FALSE; } - uprv_memcpy(newElems, elements, sizeof(elements[0]) * count); - uprv_free(elements); elements = newElems; capacity = newCap; - return TRUE; } + return TRUE; } /** @@ -349,14 +348,13 @@ UBool UVector::ensureCapacity(int32_t minimumCapacity, UErrorCode &status) { * newSize. If newSize is larger, grow the array, filling in new * slots with NULL. */ -void UVector::setSize(int32_t newSize) { +void UVector::setSize(int32_t newSize, UErrorCode &status) { int32_t i; if (newSize < 0) { return; } if (newSize > count) { - UErrorCode ec = U_ZERO_ERROR; - if (!ensureCapacity(newSize, ec)) { + if (!ensureCapacity(newSize, status)) { return; } UHashTok empty;