diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp index 1225e7f7a..cbe7ce451 100644 --- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp +++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp @@ -53,22 +53,22 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring s sourceDirChars[sourceDirUtf8Length] = '\0'; int fd = 0; void *dictBuf = 0; - int adjust = 0; + int offset = 0; fd = open(sourceDirChars, O_RDONLY); if (fd < 0) { AKLOGE("DICT: Can't open sourceDir. sourceDirChars=%s errno=%d", sourceDirChars, errno); return 0; } int pagesize = getpagesize(); - adjust = static_cast(dictOffset) % pagesize; - int adjDictOffset = static_cast(dictOffset) - adjust; - int adjDictSize = static_cast(dictSize) + adjust; + offset = static_cast(dictOffset) % pagesize; + int adjDictOffset = static_cast(dictOffset) - offset; + int adjDictSize = static_cast(dictSize) + offset; dictBuf = mmap(0, adjDictSize, PROT_READ, MAP_PRIVATE, fd, adjDictOffset); if (dictBuf == MAP_FAILED) { AKLOGE("DICT: Can't mmap dictionary. errno=%d", errno); return 0; } - dictBuf = static_cast(dictBuf) + adjust; + dictBuf = static_cast(dictBuf) + offset; if (!dictBuf) { AKLOGE("DICT: dictBuf is null"); return 0; @@ -78,9 +78,9 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring s == BinaryDictionaryFormat::detectFormatVersion(static_cast(dictBuf), static_cast(dictSize))) { AKLOGE("DICT: dictionary format is unknown, bad magic number"); - releaseDictBuf(static_cast(dictBuf) - adjust, adjDictSize, fd); + releaseDictBuf(static_cast(dictBuf) - offset, adjDictSize, fd); } else { - dictionary = new Dictionary(dictBuf, static_cast(dictSize), fd, adjust); + dictionary = new Dictionary(dictBuf, static_cast(dictSize), fd, offset); } PROF_END(66); 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) { Dictionary *dictionary = reinterpret_cast(dict); 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; - releaseDictBuf(static_cast(dictBuf) - dictionary->getDictBufAdjust(), - dictionary->getDictSize() + dictionary->getDictBufAdjust(), dictionary->getMmapFd()); + releaseDictBuf(static_cast(dictBuf) - dictBufOffset, + binaryDictionaryInfo->getDictSize() + dictBufOffset, + binaryDictionaryInfo->getMmapFd()); delete dictionary; } diff --git a/native/jni/src/suggest/core/dictionary/binary_dictionary_info.h b/native/jni/src/suggest/core/dictionary/binary_dictionary_info.h index 0b77e5ee9..c16b5310d 100644 --- a/native/jni/src/suggest/core/dictionary/binary_dictionary_info.h +++ b/native/jni/src/suggest/core/dictionary/binary_dictionary_info.h @@ -29,15 +29,29 @@ class BinaryDictionaryHeader; class BinaryDictionaryInfo { public: - BinaryDictionaryInfo(const uint8_t *const dictBuf, const int dictSize) - : mDictBuf(dictBuf), - mDictionaryFormat(BinaryDictionaryFormat::detectFormatVersion(mDictBuf, dictSize)), + BinaryDictionaryInfo(const uint8_t *const dictBuf, const int dictSize, const int mmapFd, + const int dictBufOffset) + : mDictBuf(dictBuf), mDictSize(dictSize), mMmapFd(mmapFd), + mDictBufOffset(dictBufOffset), + mDictionaryFormat(BinaryDictionaryFormat::detectFormatVersion(mDictBuf, mDictSize)), mDictionaryHeader(this), mDictRoot(mDictBuf + mDictionaryHeader.getSize()) {} AK_FORCE_INLINE const uint8_t *getDictBuf() const { 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 { return mDictRoot; } @@ -58,6 +72,9 @@ class BinaryDictionaryInfo { DISALLOW_COPY_AND_ASSIGN(BinaryDictionaryInfo); const uint8_t *const mDictBuf; + const int mDictSize; + const int mMmapFd; + const int mDictBufOffset; const BinaryDictionaryFormat::FORMAT_VERSION mDictionaryFormat; const BinaryDictionaryHeader mDictionaryHeader; const uint8_t *const mDictRoot; diff --git a/native/jni/src/suggest/core/dictionary/dictionary.cpp b/native/jni/src/suggest/core/dictionary/dictionary.cpp index 27b052b7e..b92e5644c 100644 --- a/native/jni/src/suggest/core/dictionary/dictionary.cpp +++ b/native/jni/src/suggest/core/dictionary/dictionary.cpp @@ -32,10 +32,9 @@ namespace latinime { -Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust) - : mBinaryDictionaryInfo(static_cast(dict), dictSize), - mDictSize(dictSize), - mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust), +Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufOffset) + : mBinaryDictionaryInfo( + static_cast(dict), dictSize, mmapFd, dictBufOffset), mBigramDictionary(new BigramDictionary(&mBinaryDictionaryInfo)), mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())), mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) { diff --git a/native/jni/src/suggest/core/dictionary/dictionary.h b/native/jni/src/suggest/core/dictionary/dictionary.h index 151f26183..12884a430 100644 --- a/native/jni/src/suggest/core/dictionary/dictionary.h +++ b/native/jni/src/suggest/core/dictionary/dictionary.h @@ -52,7 +52,7 @@ class Dictionary { static const int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000; 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 *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints, @@ -68,21 +68,12 @@ class Dictionary { const BinaryDictionaryInfo *getBinaryDictionaryInfo() const { return &mBinaryDictionaryInfo; } - int getDictSize() const { return mDictSize; } - int getMmapFd() const { return mMmapFd; } - int getDictBufAdjust() const { return mDictBufAdjust; } virtual ~Dictionary(); private: DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary); 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; SuggestInterface *mGestureSuggest; SuggestInterface *mTypingSuggest;