From b13c43bb2c393884298a054f64bc6eb792e22cd9 Mon Sep 17 00:00:00 2001 From: Vladimir Weinstein Date: Wed, 12 Jul 2000 05:01:53 +0000 Subject: [PATCH] ICU-458 breakiterator & co should now close opened udata instances X-SVN-Rev: 1801 --- icu4c/source/i18n/brkiter.cpp | 32 ++++----------------- icu4c/source/i18n/dbbi.cpp | 4 +-- icu4c/source/i18n/dbbi_tbl.cpp | 27 ++++++++++-------- icu4c/source/i18n/dbbi_tbl.h | 3 +- icu4c/source/i18n/rbbi.cpp | 2 +- icu4c/source/i18n/rbbi_tbl.cpp | 48 +++++++++++++++++++++----------- icu4c/source/i18n/rbbi_tbl.h | 4 ++- icu4c/source/i18n/unicode/dbbi.h | 2 +- icu4c/source/i18n/unicode/rbbi.h | 3 +- 9 files changed, 65 insertions(+), 60 deletions(-) diff --git a/icu4c/source/i18n/brkiter.cpp b/icu4c/source/i18n/brkiter.cpp index 320e6e3bcd0..1f9b3a0690f 100644 --- a/icu4c/source/i18n/brkiter.cpp +++ b/icu4c/source/i18n/brkiter.cpp @@ -56,22 +56,17 @@ BreakIterator::createWordInstance(const Locale& key, UErrorCode& status) UDataMemory* file = udata_open(NULL, "brk", filename, &status); if (!U_FAILURE(status)) { - const void* image = udata_getMemory(file); - if (image != NULL) { if(!uprv_strcmp(filename, "word_th")) { filename = "thaidict.brk"; - result = new DictionaryBasedBreakIterator(image, (char *)filename, status); + result = new DictionaryBasedBreakIterator(file, (char *)filename, status); } else { - result = new RuleBasedBreakIterator(image); + result = new RuleBasedBreakIterator(file); } - } } - //udata_close(file); // This prevents a leak, but it should be checked whether it is harmful - return result; } @@ -97,21 +92,17 @@ BreakIterator::createLineInstance(const Locale& key, UErrorCode& status) UDataMemory* file = udata_open(NULL, "brk", filename, &status); if (!U_FAILURE(status)) { - const void* image = udata_getMemory(file); - if (image != NULL) { if (!uprv_strcmp(key.getLanguage(), "th")) { const char* dataDir = u_getDataDirectory(); filename = "thaidict.brk"; - result = new DictionaryBasedBreakIterator(image, (char *)filename, status); + result = new DictionaryBasedBreakIterator(file, (char *)filename, status); } else { - result = new RuleBasedBreakIterator(image); + result = new RuleBasedBreakIterator(file); } - } } - //udata_close(file); // This prevents a leak, but it should be checked whether it is harmful return result; } @@ -132,14 +123,9 @@ BreakIterator::createCharacterInstance(const Locale& key, UErrorCode& status) UDataMemory* file = udata_open(NULL, "brk", filename, &status); if (!U_FAILURE(status)) { - const void* image = udata_getMemory(file); - - if (image != NULL) { - result = new RuleBasedBreakIterator(image); - } + result = new RuleBasedBreakIterator(file); } - //udata_close(file); // This prevents a leak, but it should be checked whether it is harmful return result; } @@ -160,15 +146,9 @@ BreakIterator::createSentenceInstance(const Locale& key, UErrorCode& status) UDataMemory* file = udata_open(NULL, "brk", filename, &status); if (!U_FAILURE(status)) { - const void* image = udata_getMemory(file); - - if (image != NULL) { - result = new RuleBasedBreakIterator(image); - } + result = new RuleBasedBreakIterator(file); } - //udata_close(file); // This prevents a leak, but it should be checked whether it is harmful - return result; } diff --git a/icu4c/source/i18n/dbbi.cpp b/icu4c/source/i18n/dbbi.cpp index 398f96f4411..3a104ed60af 100644 --- a/icu4c/source/i18n/dbbi.cpp +++ b/icu4c/source/i18n/dbbi.cpp @@ -19,10 +19,10 @@ char DictionaryBasedBreakIterator::fgClassID = 0; // constructors //======================================================================= -DictionaryBasedBreakIterator::DictionaryBasedBreakIterator(const void* tablesImage, +DictionaryBasedBreakIterator::DictionaryBasedBreakIterator(UDataMemory* tablesImage, char* dictionaryFilename, UErrorCode& status) -: RuleBasedBreakIterator((const void*)NULL), +: RuleBasedBreakIterator((UDataMemory*)NULL), dictionaryCharCount(0), cachedBreakPositions(NULL), numCachedBreakPositions(0), diff --git a/icu4c/source/i18n/dbbi_tbl.cpp b/icu4c/source/i18n/dbbi_tbl.cpp index 3fb812caef6..00344c241be 100644 --- a/icu4c/source/i18n/dbbi_tbl.cpp +++ b/icu4c/source/i18n/dbbi_tbl.cpp @@ -17,22 +17,27 @@ //======================================================================= DictionaryBasedBreakIteratorTables::DictionaryBasedBreakIteratorTables( - const void* tablesImage, + UDataMemory* tablesMemory, char* dictionaryFilename, UErrorCode &status) -: RuleBasedBreakIteratorTables(tablesImage), +: RuleBasedBreakIteratorTables(tablesMemory), dictionary(dictionaryFilename, status) { - if (U_FAILURE(status)) return; - const int32_t* tablesIdx = (int32_t*) tablesImage; - const int8_t* dbbiImage = ((const int8_t*)tablesImage + tablesIdx[8]); - // we know the offset into the memory image where the DBBI stuff - // starts is stored in element 8 of the array. There should be - // a way for the RBBI constructor to give us this, but there's - // isn't a good one. - const int32_t* dbbiIdx = (const int32_t*)dbbiImage; + if(tablesMemory != 0) { + const void* tablesImage = udata_getMemory(tablesMemory); + if(tablesImage != 0) { + if (U_FAILURE(status)) return; + const int32_t* tablesIdx = (int32_t*) tablesImage; + const int8_t* dbbiImage = ((const int8_t*)tablesImage + tablesIdx[8]); + // we know the offset into the memory image where the DBBI stuff + // starts is stored in element 8 of the array. There should be + // a way for the RBBI constructor to give us this, but there's + // isn't a good one. + const int32_t* dbbiIdx = (const int32_t*)dbbiImage; - categoryFlags = (int8_t*)((const int8_t*)dbbiImage + (int32_t)dbbiIdx[0]); + categoryFlags = (int8_t*)((const int8_t*)dbbiImage + (int32_t)dbbiIdx[0]); + } + } } //======================================================================= diff --git a/icu4c/source/i18n/dbbi_tbl.h b/icu4c/source/i18n/dbbi_tbl.h index 2a9231c04ee..8242b97b45c 100644 --- a/icu4c/source/i18n/dbbi_tbl.h +++ b/icu4c/source/i18n/dbbi_tbl.h @@ -13,6 +13,7 @@ #include "rbbi_tbl.h" #include "brkdict.h" +#include "unicode/udata.h" /* forward declaration */ class DictionaryBasedBreakIterator; @@ -45,7 +46,7 @@ private: // constructor //======================================================================= - DictionaryBasedBreakIteratorTables(const void* tablesImage, + DictionaryBasedBreakIteratorTables(UDataMemory* tablesMemory, char* dictionaryFilename, UErrorCode& status); diff --git a/icu4c/source/i18n/rbbi.cpp b/icu4c/source/i18n/rbbi.cpp index 1344317b467..57ea6557507 100644 --- a/icu4c/source/i18n/rbbi.cpp +++ b/icu4c/source/i18n/rbbi.cpp @@ -54,7 +54,7 @@ RuleBasedBreakIterator::RuleBasedBreakIterator(RuleBasedBreakIteratorTables* tab // This constructor uses the udata interface to create a BreakIterator whose // internal tables live in a memory-mapped file. "image" is a pointer to the // beginning of that file. -RuleBasedBreakIterator::RuleBasedBreakIterator(const void* image) +RuleBasedBreakIterator::RuleBasedBreakIterator(UDataMemory* image) : tables(image != NULL ? new RuleBasedBreakIteratorTables(image) : NULL), text(NULL) { diff --git a/icu4c/source/i18n/rbbi_tbl.cpp b/icu4c/source/i18n/rbbi_tbl.cpp index 06f802177cd..076181ad107 100644 --- a/icu4c/source/i18n/rbbi_tbl.cpp +++ b/icu4c/source/i18n/rbbi_tbl.cpp @@ -15,30 +15,43 @@ // constructor //======================================================================= -RuleBasedBreakIteratorTables::RuleBasedBreakIteratorTables(const void* image) +RuleBasedBreakIteratorTables::RuleBasedBreakIteratorTables(UDataMemory* memory) : refCount(0), ownTables(FALSE) { - const int32_t* im = (const int32_t*)(image); - const int8_t* base = (const int8_t*)(image); + if(memory != 0) { + fMemory = memory; + const void* image = udata_getMemory(memory); - // the memory image begins with an index that gives the offsets into the - // image for each of the fields in the BreakIteratorTables object-- - // use those to initialize the tables object (it will end up pointing - // into the memory image for everything) - numCategories = (int32_t)im[0]; - description = UnicodeString(TRUE, (UChar*)((int32_t)im[1] + base), -1); - charCategoryTable = ucmp8_openAdopt((uint16_t*)((int32_t)im[2] + base), - (int8_t*)((int32_t)im[3] + base), 0); - stateTable = (int16_t*)((int32_t)im[4] + base); - backwardsStateTable = (int16_t*)((int32_t)im[5] + base); - endStates = (int8_t*)((int32_t)im[6] + base); - lookaheadStates = (int8_t*)((int32_t)im[7] + base); + if(image != 0) { + + const int32_t* im = (const int32_t*)(image); + const int8_t* base = (const int8_t*)(image); + + // the memory image begins with an index that gives the offsets into the + // image for each of the fields in the BreakIteratorTables object-- + // use those to initialize the tables object (it will end up pointing + // into the memory image for everything) + numCategories = (int32_t)im[0]; + description = UnicodeString(TRUE, (UChar*)((int32_t)im[1] + base), -1); + charCategoryTable = ucmp8_openAlias((uint16_t*)((int32_t)im[2] + base), + (int8_t*)((int32_t)im[3] + base), 0); + stateTable = (int16_t*)((int32_t)im[4] + base); + backwardsStateTable = (int16_t*)((int32_t)im[5] + base); + endStates = (int8_t*)((int32_t)im[6] + base); + lookaheadStates = (int8_t*)((int32_t)im[7] + base); + } else { + udata_close(fMemory); + } + } else { + fMemory = 0; + } } RuleBasedBreakIteratorTables::RuleBasedBreakIteratorTables() : refCount(0), - ownTables(TRUE) + ownTables(TRUE), + fMemory(0) { // everything else is null-initialized. This constructor depends on // a RuleBasedBreakIteratorBuilder filling in all the members @@ -61,6 +74,9 @@ RuleBasedBreakIteratorTables::~RuleBasedBreakIteratorTables() { } else { uprv_free(charCategoryTable); + if(fMemory != 0) { + udata_close(fMemory); + } } } diff --git a/icu4c/source/i18n/rbbi_tbl.h b/icu4c/source/i18n/rbbi_tbl.h index d37eb6aaecc..fafbcb8b964 100644 --- a/icu4c/source/i18n/rbbi_tbl.h +++ b/icu4c/source/i18n/rbbi_tbl.h @@ -13,6 +13,7 @@ #include "unicode/utypes.h" #include "unicode/unistr.h" #include "unicode/brkiter.h" +#include "unicode/udata.h" #include "filestrm.h" U_CDECL_BEGIN @@ -99,7 +100,8 @@ private: protected: RuleBasedBreakIteratorTables(); - RuleBasedBreakIteratorTables(const void* image); + RuleBasedBreakIteratorTables(UDataMemory* memory); + UDataMemory *fMemory; private: /** diff --git a/icu4c/source/i18n/unicode/dbbi.h b/icu4c/source/i18n/unicode/dbbi.h index 1884b2a503b..160b1a178f1 100644 --- a/icu4c/source/i18n/unicode/dbbi.h +++ b/icu4c/source/i18n/unicode/dbbi.h @@ -92,7 +92,7 @@ public: * The caller owns the returned object and is responsible for deleting it. ======================================================================= */ private: - DictionaryBasedBreakIterator(const void* tablesImage, char* dictionaryFilename, UErrorCode& status); + DictionaryBasedBreakIterator(UDataMemory* tablesImage, char* dictionaryFilename, UErrorCode& status); public: //======================================================================= // boilerplate diff --git a/icu4c/source/i18n/unicode/rbbi.h b/icu4c/source/i18n/unicode/rbbi.h index db231403399..5e6712e20d5 100644 --- a/icu4c/source/i18n/unicode/rbbi.h +++ b/icu4c/source/i18n/unicode/rbbi.h @@ -12,6 +12,7 @@ #include "unicode/utypes.h" #include "unicode/brkiter.h" +#include "unicode/udata.h" class RuleBasedBreakIteratorTables; class BreakIterator; @@ -226,7 +227,7 @@ private: // This constructor uses the udata interface to create a BreakIterator whose // internal tables live in a memory-mapped file. "image" is a pointer to the // beginning of that file. -RuleBasedBreakIterator(const void* image); +RuleBasedBreakIterator(UDataMemory* image); public: /**