Move file and mmap information form Dictionary to BinaryDictionaryInfo.

Bug: 6669677
Change-Id: I05d435e0884430f8abbbed6ea2df775005ee8622
main
Keisuke Kuroynagi 2013-06-25 16:03:30 +09:00
parent 1fc6b80009
commit 46159d5422
4 changed files with 37 additions and 27 deletions

View File

@ -53,22 +53,22 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring s
sourceDirChars[sourceDirUtf8Length] = '\0'; sourceDirChars[sourceDirUtf8Length] = '\0';
int fd = 0; int fd = 0;
void *dictBuf = 0; void *dictBuf = 0;
int adjust = 0; int offset = 0;
fd = open(sourceDirChars, O_RDONLY); fd = open(sourceDirChars, O_RDONLY);
if (fd < 0) { if (fd < 0) {
AKLOGE("DICT: Can't open sourceDir. sourceDirChars=%s errno=%d", sourceDirChars, errno); AKLOGE("DICT: Can't open sourceDir. sourceDirChars=%s errno=%d", sourceDirChars, errno);
return 0; return 0;
} }
int pagesize = getpagesize(); int pagesize = getpagesize();
adjust = static_cast<int>(dictOffset) % pagesize; offset = static_cast<int>(dictOffset) % pagesize;
int adjDictOffset = static_cast<int>(dictOffset) - adjust; int adjDictOffset = static_cast<int>(dictOffset) - offset;
int adjDictSize = static_cast<int>(dictSize) + adjust; int adjDictSize = static_cast<int>(dictSize) + offset;
dictBuf = mmap(0, adjDictSize, PROT_READ, MAP_PRIVATE, fd, adjDictOffset); dictBuf = mmap(0, adjDictSize, PROT_READ, MAP_PRIVATE, fd, adjDictOffset);
if (dictBuf == MAP_FAILED) { if (dictBuf == MAP_FAILED) {
AKLOGE("DICT: Can't mmap dictionary. errno=%d", errno); AKLOGE("DICT: Can't mmap dictionary. errno=%d", errno);
return 0; return 0;
} }
dictBuf = static_cast<char *>(dictBuf) + adjust; dictBuf = static_cast<char *>(dictBuf) + offset;
if (!dictBuf) { if (!dictBuf) {
AKLOGE("DICT: dictBuf is null"); AKLOGE("DICT: dictBuf is null");
return 0; return 0;
@ -78,9 +78,9 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring s
== BinaryDictionaryFormat::detectFormatVersion(static_cast<uint8_t *>(dictBuf), == BinaryDictionaryFormat::detectFormatVersion(static_cast<uint8_t *>(dictBuf),
static_cast<int>(dictSize))) { static_cast<int>(dictSize))) {
AKLOGE("DICT: dictionary format is unknown, bad magic number"); AKLOGE("DICT: dictionary format is unknown, bad magic number");
releaseDictBuf(static_cast<const char *>(dictBuf) - adjust, adjDictSize, fd); releaseDictBuf(static_cast<const char *>(dictBuf) - offset, adjDictSize, fd);
} else { } else {
dictionary = new Dictionary(dictBuf, static_cast<int>(dictSize), fd, adjust); dictionary = new Dictionary(dictBuf, static_cast<int>(dictSize), fd, offset);
} }
PROF_END(66); PROF_END(66);
PROF_CLOSE; PROF_CLOSE;
@ -221,10 +221,13 @@ static jint latinime_BinaryDictionary_editDistance(JNIEnv *env, jclass clazz, ji
static void latinime_BinaryDictionary_close(JNIEnv *env, jclass clazz, jlong dict) { static void latinime_BinaryDictionary_close(JNIEnv *env, jclass clazz, jlong dict) {
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict); Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
if (!dictionary) return; if (!dictionary) return;
const void *dictBuf = dictionary->getBinaryDictionaryInfo()->getDictBuf(); const BinaryDictionaryInfo *const binaryDictionaryInfo = dictionary->getBinaryDictionaryInfo();
const int dictBufOffset = binaryDictionaryInfo->getDictBufOffset();
const void *dictBuf = binaryDictionaryInfo->getDictBuf();
if (!dictBuf) return; if (!dictBuf) return;
releaseDictBuf(static_cast<const char *>(dictBuf) - dictionary->getDictBufAdjust(), releaseDictBuf(static_cast<const char *>(dictBuf) - dictBufOffset,
dictionary->getDictSize() + dictionary->getDictBufAdjust(), dictionary->getMmapFd()); binaryDictionaryInfo->getDictSize() + dictBufOffset,
binaryDictionaryInfo->getMmapFd());
delete dictionary; delete dictionary;
} }

View File

@ -29,15 +29,29 @@ class BinaryDictionaryHeader;
class BinaryDictionaryInfo { class BinaryDictionaryInfo {
public: public:
BinaryDictionaryInfo(const uint8_t *const dictBuf, const int dictSize) BinaryDictionaryInfo(const uint8_t *const dictBuf, const int dictSize, const int mmapFd,
: mDictBuf(dictBuf), const int dictBufOffset)
mDictionaryFormat(BinaryDictionaryFormat::detectFormatVersion(mDictBuf, dictSize)), : mDictBuf(dictBuf), mDictSize(dictSize), mMmapFd(mmapFd),
mDictBufOffset(dictBufOffset),
mDictionaryFormat(BinaryDictionaryFormat::detectFormatVersion(mDictBuf, mDictSize)),
mDictionaryHeader(this), mDictRoot(mDictBuf + mDictionaryHeader.getSize()) {} mDictionaryHeader(this), mDictRoot(mDictBuf + mDictionaryHeader.getSize()) {}
AK_FORCE_INLINE const uint8_t *getDictBuf() const { AK_FORCE_INLINE const uint8_t *getDictBuf() const {
return mDictBuf; return mDictBuf;
} }
AK_FORCE_INLINE int getDictSize() const {
return mDictSize;
}
AK_FORCE_INLINE int getMmapFd() const {
return mMmapFd;
}
AK_FORCE_INLINE int getDictBufOffset() const {
return mDictBufOffset;
}
AK_FORCE_INLINE const uint8_t *getDictRoot() const { AK_FORCE_INLINE const uint8_t *getDictRoot() const {
return mDictRoot; return mDictRoot;
} }
@ -58,6 +72,9 @@ class BinaryDictionaryInfo {
DISALLOW_COPY_AND_ASSIGN(BinaryDictionaryInfo); DISALLOW_COPY_AND_ASSIGN(BinaryDictionaryInfo);
const uint8_t *const mDictBuf; const uint8_t *const mDictBuf;
const int mDictSize;
const int mMmapFd;
const int mDictBufOffset;
const BinaryDictionaryFormat::FORMAT_VERSION mDictionaryFormat; const BinaryDictionaryFormat::FORMAT_VERSION mDictionaryFormat;
const BinaryDictionaryHeader mDictionaryHeader; const BinaryDictionaryHeader mDictionaryHeader;
const uint8_t *const mDictRoot; const uint8_t *const mDictRoot;

View File

@ -32,10 +32,9 @@
namespace latinime { namespace latinime {
Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust) Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufOffset)
: mBinaryDictionaryInfo(static_cast<const uint8_t *>(dict), dictSize), : mBinaryDictionaryInfo(
mDictSize(dictSize), static_cast<const uint8_t *>(dict), dictSize, mmapFd, dictBufOffset),
mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust),
mBigramDictionary(new BigramDictionary(&mBinaryDictionaryInfo)), mBigramDictionary(new BigramDictionary(&mBinaryDictionaryInfo)),
mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())), mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())),
mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) { mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) {

View File

@ -52,7 +52,7 @@ class Dictionary {
static const int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000; static const int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000;
static const int KIND_FLAG_EXACT_MATCH = 0x40000000; static const int KIND_FLAG_EXACT_MATCH = 0x40000000;
Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust); Dictionary(void *dict, int dictSize, int mmapFd, int dictBufOffset);
int getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession *traverseSession, int getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession *traverseSession,
int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints, int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints,
@ -68,21 +68,12 @@ class Dictionary {
const BinaryDictionaryInfo *getBinaryDictionaryInfo() const { const BinaryDictionaryInfo *getBinaryDictionaryInfo() const {
return &mBinaryDictionaryInfo; return &mBinaryDictionaryInfo;
} }
int getDictSize() const { return mDictSize; }
int getMmapFd() const { return mMmapFd; }
int getDictBufAdjust() const { return mDictBufAdjust; }
virtual ~Dictionary(); virtual ~Dictionary();
private: private:
DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary); DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary);
const BinaryDictionaryInfo mBinaryDictionaryInfo; const BinaryDictionaryInfo mBinaryDictionaryInfo;
// Used only for the mmap version of dictionary loading, but we use these as dummy variables
// also for the malloc version.
const int mDictSize;
const int mMmapFd;
const int mDictBufAdjust;
const BigramDictionary *mBigramDictionary; const BigramDictionary *mBigramDictionary;
SuggestInterface *mGestureSuggest; SuggestInterface *mGestureSuggest;
SuggestInterface *mTypingSuggest; SuggestInterface *mTypingSuggest;