ICU-458 breakiterator & co should now close opened udata instances

X-SVN-Rev: 1801
This commit is contained in:
Vladimir Weinstein 2000-07-12 05:01:53 +00:00
parent 569eceea65
commit b13c43bb2c
9 changed files with 65 additions and 60 deletions

View File

@ -56,22 +56,17 @@ BreakIterator::createWordInstance(const Locale& key, UErrorCode& status)
UDataMemory* file = udata_open(NULL, "brk", filename, &status); UDataMemory* file = udata_open(NULL, "brk", filename, &status);
if (!U_FAILURE(status)) { if (!U_FAILURE(status)) {
const void* image = udata_getMemory(file);
if (image != NULL) {
if(!uprv_strcmp(filename, "word_th")) { if(!uprv_strcmp(filename, "word_th")) {
filename = "thaidict.brk"; filename = "thaidict.brk";
result = new DictionaryBasedBreakIterator(image, (char *)filename, status); result = new DictionaryBasedBreakIterator(file, (char *)filename, status);
} }
else { 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; return result;
} }
@ -97,21 +92,17 @@ BreakIterator::createLineInstance(const Locale& key, UErrorCode& status)
UDataMemory* file = udata_open(NULL, "brk", filename, &status); UDataMemory* file = udata_open(NULL, "brk", filename, &status);
if (!U_FAILURE(status)) { if (!U_FAILURE(status)) {
const void* image = udata_getMemory(file);
if (image != NULL) {
if (!uprv_strcmp(key.getLanguage(), "th")) { if (!uprv_strcmp(key.getLanguage(), "th")) {
const char* dataDir = u_getDataDirectory(); const char* dataDir = u_getDataDirectory();
filename = "thaidict.brk"; filename = "thaidict.brk";
result = new DictionaryBasedBreakIterator(image, (char *)filename, status); result = new DictionaryBasedBreakIterator(file, (char *)filename, status);
} }
else { 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; return result;
} }
@ -132,14 +123,9 @@ BreakIterator::createCharacterInstance(const Locale& key, UErrorCode& status)
UDataMemory* file = udata_open(NULL, "brk", filename, &status); UDataMemory* file = udata_open(NULL, "brk", filename, &status);
if (!U_FAILURE(status)) { if (!U_FAILURE(status)) {
const void* image = udata_getMemory(file); result = new RuleBasedBreakIterator(file);
if (image != NULL) {
result = new RuleBasedBreakIterator(image);
}
} }
//udata_close(file); // This prevents a leak, but it should be checked whether it is harmful
return result; return result;
} }
@ -160,15 +146,9 @@ BreakIterator::createSentenceInstance(const Locale& key, UErrorCode& status)
UDataMemory* file = udata_open(NULL, "brk", filename, &status); UDataMemory* file = udata_open(NULL, "brk", filename, &status);
if (!U_FAILURE(status)) { if (!U_FAILURE(status)) {
const void* image = udata_getMemory(file); result = new RuleBasedBreakIterator(file);
if (image != NULL) {
result = new RuleBasedBreakIterator(image);
}
} }
//udata_close(file); // This prevents a leak, but it should be checked whether it is harmful
return result; return result;
} }

View File

@ -19,10 +19,10 @@ char DictionaryBasedBreakIterator::fgClassID = 0;
// constructors // constructors
//======================================================================= //=======================================================================
DictionaryBasedBreakIterator::DictionaryBasedBreakIterator(const void* tablesImage, DictionaryBasedBreakIterator::DictionaryBasedBreakIterator(UDataMemory* tablesImage,
char* dictionaryFilename, char* dictionaryFilename,
UErrorCode& status) UErrorCode& status)
: RuleBasedBreakIterator((const void*)NULL), : RuleBasedBreakIterator((UDataMemory*)NULL),
dictionaryCharCount(0), dictionaryCharCount(0),
cachedBreakPositions(NULL), cachedBreakPositions(NULL),
numCachedBreakPositions(0), numCachedBreakPositions(0),

View File

@ -17,22 +17,27 @@
//======================================================================= //=======================================================================
DictionaryBasedBreakIteratorTables::DictionaryBasedBreakIteratorTables( DictionaryBasedBreakIteratorTables::DictionaryBasedBreakIteratorTables(
const void* tablesImage, UDataMemory* tablesMemory,
char* dictionaryFilename, char* dictionaryFilename,
UErrorCode &status) UErrorCode &status)
: RuleBasedBreakIteratorTables(tablesImage), : RuleBasedBreakIteratorTables(tablesMemory),
dictionary(dictionaryFilename, status) dictionary(dictionaryFilename, status)
{ {
if (U_FAILURE(status)) return; if(tablesMemory != 0) {
const int32_t* tablesIdx = (int32_t*) tablesImage; const void* tablesImage = udata_getMemory(tablesMemory);
const int8_t* dbbiImage = ((const int8_t*)tablesImage + tablesIdx[8]); if(tablesImage != 0) {
// we know the offset into the memory image where the DBBI stuff if (U_FAILURE(status)) return;
// starts is stored in element 8 of the array. There should be const int32_t* tablesIdx = (int32_t*) tablesImage;
// a way for the RBBI constructor to give us this, but there's const int8_t* dbbiImage = ((const int8_t*)tablesImage + tablesIdx[8]);
// isn't a good one. // we know the offset into the memory image where the DBBI stuff
const int32_t* dbbiIdx = (const int32_t*)dbbiImage; // 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]);
}
}
} }
//======================================================================= //=======================================================================

View File

@ -13,6 +13,7 @@
#include "rbbi_tbl.h" #include "rbbi_tbl.h"
#include "brkdict.h" #include "brkdict.h"
#include "unicode/udata.h"
/* forward declaration */ /* forward declaration */
class DictionaryBasedBreakIterator; class DictionaryBasedBreakIterator;
@ -45,7 +46,7 @@ private:
// constructor // constructor
//======================================================================= //=======================================================================
DictionaryBasedBreakIteratorTables(const void* tablesImage, DictionaryBasedBreakIteratorTables(UDataMemory* tablesMemory,
char* dictionaryFilename, char* dictionaryFilename,
UErrorCode& status); UErrorCode& status);

View File

@ -54,7 +54,7 @@ RuleBasedBreakIterator::RuleBasedBreakIterator(RuleBasedBreakIteratorTables* tab
// This constructor uses the udata interface to create a BreakIterator whose // 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 // internal tables live in a memory-mapped file. "image" is a pointer to the
// beginning of that file. // beginning of that file.
RuleBasedBreakIterator::RuleBasedBreakIterator(const void* image) RuleBasedBreakIterator::RuleBasedBreakIterator(UDataMemory* image)
: tables(image != NULL ? new RuleBasedBreakIteratorTables(image) : NULL), : tables(image != NULL ? new RuleBasedBreakIteratorTables(image) : NULL),
text(NULL) text(NULL)
{ {

View File

@ -15,30 +15,43 @@
// constructor // constructor
//======================================================================= //=======================================================================
RuleBasedBreakIteratorTables::RuleBasedBreakIteratorTables(const void* image) RuleBasedBreakIteratorTables::RuleBasedBreakIteratorTables(UDataMemory* memory)
: refCount(0), : refCount(0),
ownTables(FALSE) ownTables(FALSE)
{ {
const int32_t* im = (const int32_t*)(image); if(memory != 0) {
const int8_t* base = (const int8_t*)(image); fMemory = memory;
const void* image = udata_getMemory(memory);
// the memory image begins with an index that gives the offsets into the if(image != 0) {
// image for each of the fields in the BreakIteratorTables object--
// use those to initialize the tables object (it will end up pointing const int32_t* im = (const int32_t*)(image);
// into the memory image for everything) const int8_t* base = (const int8_t*)(image);
numCategories = (int32_t)im[0];
description = UnicodeString(TRUE, (UChar*)((int32_t)im[1] + base), -1); // the memory image begins with an index that gives the offsets into the
charCategoryTable = ucmp8_openAdopt((uint16_t*)((int32_t)im[2] + base), // image for each of the fields in the BreakIteratorTables object--
(int8_t*)((int32_t)im[3] + base), 0); // use those to initialize the tables object (it will end up pointing
stateTable = (int16_t*)((int32_t)im[4] + base); // into the memory image for everything)
backwardsStateTable = (int16_t*)((int32_t)im[5] + base); numCategories = (int32_t)im[0];
endStates = (int8_t*)((int32_t)im[6] + base); description = UnicodeString(TRUE, (UChar*)((int32_t)im[1] + base), -1);
lookaheadStates = (int8_t*)((int32_t)im[7] + base); 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() RuleBasedBreakIteratorTables::RuleBasedBreakIteratorTables()
: refCount(0), : refCount(0),
ownTables(TRUE) ownTables(TRUE),
fMemory(0)
{ {
// everything else is null-initialized. This constructor depends on // everything else is null-initialized. This constructor depends on
// a RuleBasedBreakIteratorBuilder filling in all the members // a RuleBasedBreakIteratorBuilder filling in all the members
@ -61,6 +74,9 @@ RuleBasedBreakIteratorTables::~RuleBasedBreakIteratorTables() {
} }
else { else {
uprv_free(charCategoryTable); uprv_free(charCategoryTable);
if(fMemory != 0) {
udata_close(fMemory);
}
} }
} }

View File

@ -13,6 +13,7 @@
#include "unicode/utypes.h" #include "unicode/utypes.h"
#include "unicode/unistr.h" #include "unicode/unistr.h"
#include "unicode/brkiter.h" #include "unicode/brkiter.h"
#include "unicode/udata.h"
#include "filestrm.h" #include "filestrm.h"
U_CDECL_BEGIN U_CDECL_BEGIN
@ -99,7 +100,8 @@ private:
protected: protected:
RuleBasedBreakIteratorTables(); RuleBasedBreakIteratorTables();
RuleBasedBreakIteratorTables(const void* image); RuleBasedBreakIteratorTables(UDataMemory* memory);
UDataMemory *fMemory;
private: private:
/** /**

View File

@ -92,7 +92,7 @@ public:
* The caller owns the returned object and is responsible for deleting it. * The caller owns the returned object and is responsible for deleting it.
======================================================================= */ ======================================================================= */
private: private:
DictionaryBasedBreakIterator(const void* tablesImage, char* dictionaryFilename, UErrorCode& status); DictionaryBasedBreakIterator(UDataMemory* tablesImage, char* dictionaryFilename, UErrorCode& status);
public: public:
//======================================================================= //=======================================================================
// boilerplate // boilerplate

View File

@ -12,6 +12,7 @@
#include "unicode/utypes.h" #include "unicode/utypes.h"
#include "unicode/brkiter.h" #include "unicode/brkiter.h"
#include "unicode/udata.h"
class RuleBasedBreakIteratorTables; class RuleBasedBreakIteratorTables;
class BreakIterator; class BreakIterator;
@ -226,7 +227,7 @@ private:
// This constructor uses the udata interface to create a BreakIterator whose // 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 // internal tables live in a memory-mapped file. "image" is a pointer to the
// beginning of that file. // beginning of that file.
RuleBasedBreakIterator(const void* image); RuleBasedBreakIterator(UDataMemory* image);
public: public:
/** /**