From 8c56e563826cf599bfb75a0dfc67c3796037c6e6 Mon Sep 17 00:00:00 2001 From: George Rhoten Date: Sat, 16 Feb 2008 11:26:53 +0000 Subject: [PATCH] ICU-6076 Make it a little easier to detect initialization problems, and improve initialization time. X-SVN-Rev: 23442 --- icu4c/source/common/unicode/uniset.h | 2 ++ icu4c/source/common/uniset.cpp | 10 ++++++++-- icu4c/source/common/uniset_props.cpp | 19 ++++++++++++++----- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/icu4c/source/common/unicode/uniset.h b/icu4c/source/common/unicode/uniset.h index 09e25a3ce39..40e644be4fc 100644 --- a/icu4c/source/common/unicode/uniset.h +++ b/icu4c/source/common/unicode/uniset.h @@ -1503,6 +1503,8 @@ private: UnicodeString& rebuiltPat, UErrorCode& ec); + static const UnicodeSet* getInclusions(int32_t src, UErrorCode &status); + /** * A filter that returns TRUE if the given code point should be * included in the UnicodeSet being constructed. diff --git a/icu4c/source/common/uniset.cpp b/icu4c/source/common/uniset.cpp index 9720d1c3c0d..215bdb34193 100644 --- a/icu4c/source/common/uniset.cpp +++ b/icu4c/source/common/uniset.cpp @@ -1446,8 +1446,14 @@ UnicodeSet& UnicodeSet::compact() { if (len < capacity) { // Make the capacity equal to len or 1. // We don't want to realloc of 0 size. - capacity = len + (len == 0); - list = (UChar32*) uprv_realloc(list, sizeof(UChar32) * capacity); + int32_t newCapacity = len + (len == 0); + UChar32* temp = (UChar32*) uprv_realloc(list, sizeof(UChar32) * newCapacity); + if (temp) { + list = temp; + capacity = newCapacity; + } + // else what the heck happened?! We allocated less memory! + // Oh well. We'll keep our original array. } return *this; } diff --git a/icu4c/source/common/uniset_props.cpp b/icu4c/source/common/uniset_props.cpp index e1a40c51366..cce3a28da8a 100644 --- a/icu4c/source/common/uniset_props.cpp +++ b/icu4c/source/common/uniset_props.cpp @@ -136,7 +136,14 @@ U_CDECL_END U_NAMESPACE_BEGIN -static const UnicodeSet* getInclusions(int32_t src, UErrorCode &status) { +/* +Reduce excessive reallocation, and make it easier to detect initialization +problems. +Usually you don't see smaller sets than this for Unicode 5.0. +*/ +#define DEFAULT_INCLUSION_CAPACITY 3072 + +const UnicodeSet* UnicodeSet::getInclusions(int32_t src, UErrorCode &status) { UBool needInit; UMTX_CHECK(NULL, (INCLUSIONS[src] == NULL), needInit); if (needInit) { @@ -146,9 +153,10 @@ static const UnicodeSet* getInclusions(int32_t src, UErrorCode &status) { _set_add, _set_addRange, _set_addString, - NULL // don't need remove() + NULL, // don't need remove() + NULL // don't need removeRange() }; - + incl->ensureCapacity(DEFAULT_INCLUSION_CAPACITY, status); if (incl != NULL) { switch(src) { case UPROPS_SRC_CHAR: @@ -894,7 +902,7 @@ void UnicodeSet::applyFilter(UnicodeSet::Filter filter, clear(); UChar32 startHasProperty = -1; - int limitRange = inclusions->getRangeCount(); + int32_t limitRange = inclusions->getRangeCount(); for (int j=0; j