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);
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;
}

View File

@ -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),

View File

@ -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]);
}
}
}
//=======================================================================

View File

@ -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);

View File

@ -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)
{

View File

@ -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);
}
}
}

View File

@ -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:
/**

View File

@ -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

View File

@ -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:
/**