aboutsummaryrefslogtreecommitdiffstats
path: root/native/jni/src
diff options
context:
space:
mode:
Diffstat (limited to 'native/jni/src')
-rw-r--r--native/jni/src/suggest/core/dictionary/binary_dictionary_info.h67
-rw-r--r--native/jni/src/suggest/core/dictionary/dictionary.cpp39
-rw-r--r--native/jni/src/suggest/core/dictionary/dictionary.h17
-rw-r--r--native/jni/src/suggest/core/policy/dictionary_structure_with_buffer_policy.h12
-rw-r--r--native/jni/src/suggest/policyimpl/dictionary/dictionary_structure_with_buffer_policy_factory.cpp21
-rw-r--r--native/jni/src/suggest/policyimpl/dictionary/dictionary_structure_with_buffer_policy_factory.h3
-rw-r--r--native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.cpp30
-rw-r--r--native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.h19
-rw-r--r--native/jni/src/suggest/policyimpl/dictionary/header/header_reading_utils.cpp2
-rw-r--r--native/jni/src/suggest/policyimpl/dictionary/patricia_trie_policy.h31
-rw-r--r--native/jni/src/suggest/policyimpl/dictionary/utils/mmaped_buffer.h99
11 files changed, 219 insertions, 121 deletions
diff --git a/native/jni/src/suggest/core/dictionary/binary_dictionary_info.h b/native/jni/src/suggest/core/dictionary/binary_dictionary_info.h
deleted file mode 100644
index e50baae0b..000000000
--- a/native/jni/src/suggest/core/dictionary/binary_dictionary_info.h
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Copyright (C) 2013, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef LATINIME_BINARY_DICTIONARY_INFO_H
-#define LATINIME_BINARY_DICTIONARY_INFO_H
-
-#include <stdint.h>
-
-#include "defines.h"
-
-namespace latinime {
-
-class BinaryDictionaryInfo {
- public:
- AK_FORCE_INLINE BinaryDictionaryInfo(const uint8_t *const dictBuf,
- const int dictSize, const int mmapFd, const int dictBufOffset, const bool isUpdatable)
- : mDictBuf(dictBuf), mDictSize(dictSize), mMmapFd(mmapFd),
- mDictBufOffset(dictBufOffset), mIsUpdatable(isUpdatable) {}
-
- ~BinaryDictionaryInfo() {}
-
- 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 bool isDynamicallyUpdatable() const {
- // TODO: Support dynamic dictionary formats.
- const bool isUpdatableDictionaryFormat = false;
- return mIsUpdatable && isUpdatableDictionaryFormat;
- }
-
- private:
- DISALLOW_COPY_AND_ASSIGN(BinaryDictionaryInfo);
-
- const uint8_t *const mDictBuf;
- const int mDictSize;
- const int mMmapFd;
- const int mDictBufOffset;
- const bool mIsUpdatable;
-};
-}
-#endif /* LATINIME_BINARY_DICTIONARY_INFO_H */
diff --git a/native/jni/src/suggest/core/dictionary/dictionary.cpp b/native/jni/src/suggest/core/dictionary/dictionary.cpp
index af00e9927..8418a608a 100644
--- a/native/jni/src/suggest/core/dictionary/dictionary.cpp
+++ b/native/jni/src/suggest/core/dictionary/dictionary.cpp
@@ -23,23 +23,19 @@
#include "defines.h"
#include "suggest/core/dictionary/bigram_dictionary.h"
#include "suggest/core/policy/dictionary_header_structure_policy.h"
+#include "suggest/core/policy/dictionary_structure_with_buffer_policy.h"
#include "suggest/core/session/dic_traverse_session.h"
#include "suggest/core/suggest.h"
#include "suggest/core/suggest_options.h"
-#include "suggest/policyimpl/dictionary/dictionary_structure_with_buffer_policy_factory.h"
#include "suggest/policyimpl/gesture/gesture_suggest_policy_factory.h"
#include "suggest/policyimpl/typing/typing_suggest_policy_factory.h"
#include "utils/log_utils.h"
namespace latinime {
-Dictionary::Dictionary(JNIEnv *env, void *dict, int dictSize, int mmapFd,
- int dictBufOffset, bool isUpdatable)
- : mBinaryDictionaryInfo(static_cast<const uint8_t *>(dict), dictSize, mmapFd,
- dictBufOffset, isUpdatable),
- mDictionaryStructureWithBufferPolicy(DictionaryStructureWithBufferPolicyFactory
- ::newDictionaryStructureWithBufferPolicy(
- static_cast<const uint8_t *>(dict), dictSize)),
+Dictionary::Dictionary(JNIEnv *env,
+ DictionaryStructureWithBufferPolicy *const dictionaryStructureWithBufferPolicy)
+ : mDictionaryStructureWithBufferPolicy(dictionaryStructureWithBufferPolicy),
mBigramDictionary(new BigramDictionary(mDictionaryStructureWithBufferPolicy)),
mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())),
mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) {
@@ -102,32 +98,18 @@ bool Dictionary::isValidBigram(const int *word0, int length0, const int *word1,
}
void Dictionary::addUnigramWord(const int *const word, const int length, const int probability) {
- if (!mBinaryDictionaryInfo.isDynamicallyUpdatable()) {
- // This method should not be called for non-updatable dictionary.
- AKLOGI("Warning: Dictionary::addUnigramWord() is called for non-updatable dictionary.");
- return;
- }
- // TODO: Support dynamic update
+ mDictionaryStructureWithBufferPolicy->addUnigramWord(word, length, probability);
}
void Dictionary::addBigramWords(const int *const word0, const int length0, const int *const word1,
const int length1, const int probability) {
- if (!mBinaryDictionaryInfo.isDynamicallyUpdatable()) {
- // This method should not be called for non-updatable dictionary.
- AKLOGI("Warning: Dictionary::addBigramWords() is called for non-updatable dictionary.");
- return;
- }
- // TODO: Support dynamic update
+ mDictionaryStructureWithBufferPolicy->addBigramWords(word0, length0, word1, length1,
+ probability);
}
void Dictionary::removeBigramWords(const int *const word0, const int length0,
const int *const word1, const int length1) {
- if (!mBinaryDictionaryInfo.isDynamicallyUpdatable()) {
- // This method should not be called for non-updatable dictionary.
- AKLOGI("Warning: Dictionary::removeBigramWords() is called for non-updatable dictionary.");
- return;
- }
- // TODO: Support dynamic update
+ mDictionaryStructureWithBufferPolicy->removeBigramWords(word0, length0, word1, length1);
}
void Dictionary::logDictionaryInfo(JNIEnv *const env) const {
@@ -154,9 +136,8 @@ void Dictionary::logDictionaryInfo(JNIEnv *const env) const {
dateStringCharBuffer, BUFFER_SIZE);
LogUtils::logToJava(env,
- "Dictionary info: dictionary = %s ; version = %s ; date = %s ; filesize = %i",
- dictionaryIdCharBuffer, versionStringCharBuffer, dateStringCharBuffer,
- mBinaryDictionaryInfo.getDictSize());
+ "Dictionary info: dictionary = %s ; version = %s ; date = %s",
+ dictionaryIdCharBuffer, versionStringCharBuffer, dateStringCharBuffer);
}
} // namespace latinime
diff --git a/native/jni/src/suggest/core/dictionary/dictionary.h b/native/jni/src/suggest/core/dictionary/dictionary.h
index 17ce47974..0afe5a54b 100644
--- a/native/jni/src/suggest/core/dictionary/dictionary.h
+++ b/native/jni/src/suggest/core/dictionary/dictionary.h
@@ -21,7 +21,6 @@
#include "defines.h"
#include "jni.h"
-#include "suggest/core/dictionary/binary_dictionary_info.h"
namespace latinime {
@@ -54,8 +53,8 @@ class Dictionary {
static const int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000;
static const int KIND_FLAG_EXACT_MATCH = 0x40000000;
- Dictionary(JNIEnv *env, void *dict, int dictSize, int mmapFd, int dictBufOffset,
- bool isUpdatable);
+ Dictionary(JNIEnv *env,
+ DictionaryStructureWithBufferPolicy *const dictionaryStructureWithBufferPoilcy);
int getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession *traverseSession,
int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints,
@@ -78,11 +77,6 @@ class Dictionary {
void removeBigramWords(const int *const word0, const int length0, const int *const word1,
const int length1);
- // TODO: Remove.
- const BinaryDictionaryInfo *getBinaryDictionaryInfo() const {
- return &mBinaryDictionaryInfo;
- }
-
const DictionaryStructureWithBufferPolicy *getDictionaryStructurePolicy() const {
return mDictionaryStructureWithBufferPolicy;
}
@@ -92,11 +86,10 @@ class Dictionary {
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary);
- const BinaryDictionaryInfo mBinaryDictionaryInfo;
DictionaryStructureWithBufferPolicy *const mDictionaryStructureWithBufferPolicy;
- const BigramDictionary *mBigramDictionary;
- SuggestInterface *mGestureSuggest;
- SuggestInterface *mTypingSuggest;
+ const BigramDictionary *const mBigramDictionary;
+ const SuggestInterface *const mGestureSuggest;
+ const SuggestInterface *const mTypingSuggest;
void logDictionaryInfo(JNIEnv *const env) const;
};
diff --git a/native/jni/src/suggest/core/policy/dictionary_structure_with_buffer_policy.h b/native/jni/src/suggest/core/policy/dictionary_structure_with_buffer_policy.h
index 37daef98a..532411509 100644
--- a/native/jni/src/suggest/core/policy/dictionary_structure_with_buffer_policy.h
+++ b/native/jni/src/suggest/core/policy/dictionary_structure_with_buffer_policy.h
@@ -59,6 +59,18 @@ class DictionaryStructureWithBufferPolicy {
virtual const DictionaryShortcutsStructurePolicy *getShortcutsStructurePolicy() const = 0;
+ // Returns whether the update was success or not.
+ virtual bool addUnigramWord(const int *const word, const int length,
+ const int probability) = 0;
+
+ // Returns whether the update was success or not.
+ virtual bool addBigramWords(const int *const word0, const int length0, const int *const word1,
+ const int length1, const int probability) = 0;
+
+ // Returns whether the update was success or not.
+ virtual bool removeBigramWords(const int *const word0, const int length0,
+ const int *const word1, const int length1) = 0;
+
protected:
DictionaryStructureWithBufferPolicy() {}
diff --git a/native/jni/src/suggest/policyimpl/dictionary/dictionary_structure_with_buffer_policy_factory.cpp b/native/jni/src/suggest/policyimpl/dictionary/dictionary_structure_with_buffer_policy_factory.cpp
index 34f092f49..cc5252c43 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/dictionary_structure_with_buffer_policy_factory.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/dictionary_structure_with_buffer_policy_factory.cpp
@@ -22,18 +22,29 @@
#include "suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.h"
#include "suggest/policyimpl/dictionary/patricia_trie_policy.h"
#include "suggest/policyimpl/dictionary/utils/format_utils.h"
+#include "suggest/policyimpl/dictionary/utils/mmaped_buffer.h"
namespace latinime {
/* static */ DictionaryStructureWithBufferPolicy *DictionaryStructureWithBufferPolicyFactory
- ::newDictionaryStructureWithBufferPolicy(const uint8_t *const dictBuf,
- const int dictSize) {
- switch (FormatUtils::detectFormatVersion(dictBuf, dictSize)) {
+ ::newDictionaryStructureWithBufferPolicy(const char *const path, const int pathLength,
+ const int bufOffset, const int size, const bool isUpdatable) {
+ // Allocated buffer in MmapedBuffer::openBuffer() will be freed in the destructor of
+ // impl classes of DictionaryStructureWithBufferPolicy.
+ const MmapedBuffer *const mmapedBuffer = MmapedBuffer::openBuffer(path, pathLength, bufOffset,
+ size, isUpdatable);
+ if (!mmapedBuffer) {
+ return 0;
+ }
+ switch (FormatUtils::detectFormatVersion(mmapedBuffer->getBuffer(),
+ mmapedBuffer->getBufferSize())) {
case FormatUtils::VERSION_2:
- return new PatriciaTriePolicy(dictBuf);
+ return new PatriciaTriePolicy(mmapedBuffer);
case FormatUtils::VERSION_3:
- return new DynamicPatriciaTriePolicy(dictBuf);
+ return new DynamicPatriciaTriePolicy(mmapedBuffer);
default:
+ AKLOGE("DICT: dictionary format is unknown, bad magic number");
+ delete mmapedBuffer;
ASSERT(false);
return 0;
}
diff --git a/native/jni/src/suggest/policyimpl/dictionary/dictionary_structure_with_buffer_policy_factory.h b/native/jni/src/suggest/policyimpl/dictionary/dictionary_structure_with_buffer_policy_factory.h
index 53eb8f927..1cb7a89c4 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/dictionary_structure_with_buffer_policy_factory.h
+++ b/native/jni/src/suggest/policyimpl/dictionary/dictionary_structure_with_buffer_policy_factory.h
@@ -27,7 +27,8 @@ namespace latinime {
class DictionaryStructureWithBufferPolicyFactory {
public:
static DictionaryStructureWithBufferPolicy *newDictionaryStructureWithBufferPolicy(
- const uint8_t *const dictBuf, const int dictSize);
+ const char *const path, const int pathLength, const int bufOffset, const int size,
+ const bool isUpdatable);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(DictionaryStructureWithBufferPolicyFactory);
diff --git a/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.cpp b/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.cpp
index cca801098..3000860a3 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.cpp
@@ -231,4 +231,34 @@ int DynamicPatriciaTriePolicy::getBigramsPositionOfNode(const int nodePos) const
return nodeReader.getBigramsPos();
}
+bool DynamicPatriciaTriePolicy::addUnigramWord(const int *const word, const int length,
+ const int probability) {
+ if (!mBuffer->isUpdatable()) {
+ AKLOGI("Warning: addUnigramWord() is called for non-updatable dictionary.");
+ return false;
+ }
+ // TODO: Implement.
+ return false;
+}
+
+bool DynamicPatriciaTriePolicy::addBigramWords(const int *const word0, const int length0,
+ const int *const word1, const int length1, const int probability) {
+ if (!mBuffer->isUpdatable()) {
+ AKLOGI("Warning: addBigramWords() is called for non-updatable dictionary.");
+ return false;
+ }
+ // TODO: Implement.
+ return false;
+}
+
+bool DynamicPatriciaTriePolicy::removeBigramWords(const int *const word0, const int length0,
+ const int *const word1, const int length1) {
+ if (!mBuffer->isUpdatable()) {
+ AKLOGI("Warning: removeBigramWords() is called for non-updatable dictionary.");
+ return false;
+ }
+ // TODO: Implement.
+ return false;
+}
+
} // namespace latinime
diff --git a/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.h b/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.h
index ad8911c80..708967d7b 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.h
+++ b/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.h
@@ -24,6 +24,7 @@
#include "suggest/policyimpl/dictionary/bigram/bigram_list_policy.h"
#include "suggest/policyimpl/dictionary/header/header_policy.h"
#include "suggest/policyimpl/dictionary/shortcut/shortcut_list_policy.h"
+#include "suggest/policyimpl/dictionary/utils/mmaped_buffer.h"
namespace latinime {
@@ -32,11 +33,14 @@ class DicNodeVector;
class DynamicPatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
public:
- DynamicPatriciaTriePolicy(const uint8_t *const dictBuf)
- : mHeaderPolicy(dictBuf), mDictRoot(dictBuf + mHeaderPolicy.getSize()),
+ DynamicPatriciaTriePolicy(const MmapedBuffer *const buffer)
+ : mBuffer(buffer), mHeaderPolicy(mBuffer->getBuffer()),
+ mDictRoot(mBuffer->getBuffer() + mHeaderPolicy.getSize()),
mBigramListPolicy(mDictRoot), mShortcutListPolicy(mDictRoot) {}
- ~DynamicPatriciaTriePolicy() {}
+ ~DynamicPatriciaTriePolicy() {
+ delete mBuffer;
+ }
AK_FORCE_INLINE int getRootPosition() const {
return 0;
@@ -70,10 +74,19 @@ class DynamicPatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
return &mShortcutListPolicy;
}
+ bool addUnigramWord(const int *const word, const int length, const int probability);
+
+ bool addBigramWords(const int *const word0, const int length0, const int *const word1,
+ const int length1, const int probability);
+
+ bool removeBigramWords(const int *const word0, const int length0, const int *const word1,
+ const int length1);
+
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicPatriciaTriePolicy);
static const int MAX_CHILD_COUNT_TO_AVOID_INFINITE_LOOP;
+ const MmapedBuffer *const mBuffer;
const HeaderPolicy mHeaderPolicy;
// TODO: Consolidate mDictRoot.
const uint8_t *const mDictRoot;
diff --git a/native/jni/src/suggest/policyimpl/dictionary/header/header_reading_utils.cpp b/native/jni/src/suggest/policyimpl/dictionary/header/header_reading_utils.cpp
index 23b88ecdf..f323876c4 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/header/header_reading_utils.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/header/header_reading_utils.cpp
@@ -34,7 +34,7 @@ const int HeaderReadingUtils::HEADER_SIZE_FIELD_SIZE = 4;
const HeaderReadingUtils::DictionaryFlags
HeaderReadingUtils::NO_FLAGS = 0;
// Flags for special processing
-// Those *must* match the flags in makedict (BinaryDictInputOutput#*_PROCESSING_FLAG) or
+// Those *must* match the flags in makedict (FormatSpec#*_PROCESSING_FLAG) or
// something very bad (like, the apocalypse) will happen. Please update both at the same time.
const HeaderReadingUtils::DictionaryFlags
HeaderReadingUtils::GERMAN_UMLAUT_PROCESSING_FLAG = 0x1;
diff --git a/native/jni/src/suggest/policyimpl/dictionary/patricia_trie_policy.h b/native/jni/src/suggest/policyimpl/dictionary/patricia_trie_policy.h
index 2e34480aa..0d85050f3 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/patricia_trie_policy.h
+++ b/native/jni/src/suggest/policyimpl/dictionary/patricia_trie_policy.h
@@ -24,6 +24,7 @@
#include "suggest/policyimpl/dictionary/bigram/bigram_list_policy.h"
#include "suggest/policyimpl/dictionary/header/header_policy.h"
#include "suggest/policyimpl/dictionary/shortcut/shortcut_list_policy.h"
+#include "suggest/policyimpl/dictionary/utils/mmaped_buffer.h"
namespace latinime {
@@ -32,11 +33,14 @@ class DicNodeVector;
class PatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
public:
- PatriciaTriePolicy(const uint8_t *const dictBuf)
- : mHeaderPolicy(dictBuf), mDictRoot(dictBuf + mHeaderPolicy.getSize()),
+ PatriciaTriePolicy(const MmapedBuffer *const buffer)
+ : mBuffer(buffer), mHeaderPolicy(mBuffer->getBuffer()),
+ mDictRoot(mBuffer->getBuffer() + mHeaderPolicy.getSize()),
mBigramListPolicy(mDictRoot), mShortcutListPolicy(mDictRoot) {}
- ~PatriciaTriePolicy() {}
+ ~PatriciaTriePolicy() {
+ delete mBuffer;
+ }
AK_FORCE_INLINE int getRootPosition() const {
return 0;
@@ -70,9 +74,30 @@ class PatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
return &mShortcutListPolicy;
}
+ bool addUnigramWord(const int *const word, const int length, const int probability) {
+ // This method should not be called for non-updatable dictionary.
+ AKLOGI("Warning: addUnigramWord() is called for non-updatable dictionary.");
+ return false;
+ }
+
+ bool addBigramWords(const int *const word0, const int length0, const int *const word1,
+ const int length1, const int probability) {
+ // This method should not be called for non-updatable dictionary.
+ AKLOGI("Warning: addBigramWords() is called for non-updatable dictionary.");
+ return false;
+ }
+
+ bool removeBigramWords(const int *const word0, const int length0, const int *const word1,
+ const int length1) {
+ // This method should not be called for non-updatable dictionary.
+ AKLOGI("Warning: removeBigramWords() is called for non-updatable dictionary.");
+ return false;
+ }
+
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(PatriciaTriePolicy);
+ const MmapedBuffer *const mBuffer;
const HeaderPolicy mHeaderPolicy;
const uint8_t *const mDictRoot;
const BigramListPolicy mBigramListPolicy;
diff --git a/native/jni/src/suggest/policyimpl/dictionary/utils/mmaped_buffer.h b/native/jni/src/suggest/policyimpl/dictionary/utils/mmaped_buffer.h
new file mode 100644
index 000000000..e3aabb084
--- /dev/null
+++ b/native/jni/src/suggest/policyimpl/dictionary/utils/mmaped_buffer.h
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2013, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef LATINIME_MMAPED_BUFFER_H
+#define LATINIME_MMAPED_BUFFER_H
+
+#include <cerrno>
+#include <fcntl.h>
+#include <stdint.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include "defines.h"
+
+namespace latinime {
+
+class MmapedBuffer {
+ public:
+ static MmapedBuffer* openBuffer(const char *const path, const int pathLength,
+ const int bufOffset, const int size, const bool isUpdatable) {
+ const int openMode = isUpdatable ? O_RDWR : O_RDONLY;
+ const int fd = open(path, openMode);
+ if (fd < 0) {
+ AKLOGE("DICT: Can't open the source. path=%s errno=%d", path, errno);
+ return 0;
+ }
+ const int pagesize = getpagesize();
+ const int offset = bufOffset % pagesize;
+ int adjOffset = bufOffset - offset;
+ int adjSize = size + offset;
+ const int protMode = isUpdatable ? PROT_READ | PROT_WRITE : PROT_READ;
+ void *const mmapedBuffer = mmap(0, adjSize, protMode, MAP_PRIVATE, fd, adjOffset);
+ if (mmapedBuffer == MAP_FAILED) {
+ AKLOGE("DICT: Can't mmap dictionary. errno=%d", errno);
+ close(fd);
+ return 0;
+ }
+ uint8_t *const buffer = static_cast<uint8_t *>(mmapedBuffer) + bufOffset;
+ if (!buffer) {
+ AKLOGE("DICT: buffer is null");
+ close(fd);
+ return 0;
+ }
+ return new MmapedBuffer(buffer, adjSize, fd, adjOffset, isUpdatable);
+ }
+
+ ~MmapedBuffer() {
+ int ret = munmap(static_cast<void *>(mBuffer - mBufferOffset),
+ mBufferSize + mBufferOffset);
+ if (ret != 0) {
+ AKLOGE("DICT: Failure in munmap. ret=%d errno=%d", ret, errno);
+ }
+ ret = close(mMmapFd);
+ if (ret != 0) {
+ AKLOGE("DICT: Failure in close. ret=%d errno=%d", ret, errno);
+ }
+ }
+
+ AK_FORCE_INLINE uint8_t *getBuffer() const {
+ return mBuffer;
+ }
+
+ AK_FORCE_INLINE int getBufferSize() const {
+ return mBufferSize;
+ }
+
+ AK_FORCE_INLINE bool isUpdatable() const {
+ return mIsUpdatable;
+ }
+
+ private:
+ AK_FORCE_INLINE MmapedBuffer(uint8_t *const buffer, const int bufferSize, const int mmapFd,
+ const int bufferOffset, const bool isUpdatable)
+ : mBuffer(buffer), mBufferSize(bufferSize), mMmapFd(mmapFd),
+ mBufferOffset(bufferOffset), mIsUpdatable(isUpdatable) {}
+
+ DISALLOW_IMPLICIT_CONSTRUCTORS(MmapedBuffer);
+
+ uint8_t *const mBuffer;
+ const int mBufferSize;
+ const int mMmapFd;
+ const int mBufferOffset;
+ const bool mIsUpdatable;
+};
+}
+#endif /* LATINIME_MMAPED_BUFFER_H */