Merge "Use unique_ptr."
This commit is contained in:
commit
c36c97b4f7
18 changed files with 125 additions and 209 deletions
|
@ -46,15 +46,16 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring s
|
|||
char sourceDirChars[sourceDirUtf8Length + 1];
|
||||
env->GetStringUTFRegion(sourceDir, 0, env->GetStringLength(sourceDir), sourceDirChars);
|
||||
sourceDirChars[sourceDirUtf8Length] = '\0';
|
||||
DictionaryStructureWithBufferPolicy::StructurePolicyPtr dictionaryStructureWithBufferPolicy =
|
||||
DictionaryStructureWithBufferPolicy::StructurePolicyPtr dictionaryStructureWithBufferPolicy(
|
||||
DictionaryStructureWithBufferPolicyFactory::newDictionaryStructureWithBufferPolicy(
|
||||
sourceDirChars, static_cast<int>(dictOffset), static_cast<int>(dictSize),
|
||||
isUpdatable == JNI_TRUE);
|
||||
if (!dictionaryStructureWithBufferPolicy.get()) {
|
||||
isUpdatable == JNI_TRUE));
|
||||
if (!dictionaryStructureWithBufferPolicy) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
Dictionary *const dictionary = new Dictionary(env, dictionaryStructureWithBufferPolicy);
|
||||
Dictionary *const dictionary =
|
||||
new Dictionary(env, std::move(dictionaryStructureWithBufferPolicy));
|
||||
PROF_END(66);
|
||||
PROF_CLOSE;
|
||||
return reinterpret_cast<jlong>(dictionary);
|
||||
|
|
|
@ -34,9 +34,9 @@ namespace latinime {
|
|||
|
||||
const int Dictionary::HEADER_ATTRIBUTE_BUFFER_SIZE = 32;
|
||||
|
||||
Dictionary::Dictionary(JNIEnv *env, const DictionaryStructureWithBufferPolicy::StructurePolicyPtr
|
||||
&dictionaryStructureWithBufferPolicy)
|
||||
: mDictionaryStructureWithBufferPolicy(dictionaryStructureWithBufferPolicy),
|
||||
Dictionary::Dictionary(JNIEnv *env, DictionaryStructureWithBufferPolicy::StructurePolicyPtr
|
||||
dictionaryStructureWithBufferPolicy)
|
||||
: mDictionaryStructureWithBufferPolicy(std::move(dictionaryStructureWithBufferPolicy)),
|
||||
mBigramDictionary(new BigramDictionary(mDictionaryStructureWithBufferPolicy.get())),
|
||||
mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())),
|
||||
mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) {
|
||||
|
@ -53,7 +53,7 @@ int Dictionary::getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession
|
|||
if (suggestOptions->isGesture()) {
|
||||
DicTraverseSession::initSessionInstance(
|
||||
traverseSession, this, prevWordCodePoints, prevWordLength, suggestOptions);
|
||||
result = mGestureSuggest.get()->getSuggestions(proximityInfo, traverseSession, xcoordinates,
|
||||
result = mGestureSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
|
||||
ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint, outWords,
|
||||
outputScores, spaceIndices, outputTypes, outputAutoCommitFirstWordConfidence);
|
||||
if (DEBUG_DICT) {
|
||||
|
@ -63,7 +63,7 @@ int Dictionary::getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession
|
|||
} else {
|
||||
DicTraverseSession::initSessionInstance(
|
||||
traverseSession, this, prevWordCodePoints, prevWordLength, suggestOptions);
|
||||
result = mTypingSuggest.get()->getSuggestions(proximityInfo, traverseSession, xcoordinates,
|
||||
result = mTypingSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
|
||||
ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint,
|
||||
outWords, outputScores, spaceIndices, outputTypes,
|
||||
outputAutoCommitFirstWordConfidence);
|
||||
|
@ -78,7 +78,7 @@ int Dictionary::getBigrams(const int *word, int length, int *outWords, int *outp
|
|||
int *outputTypes) const {
|
||||
TimeKeeper::setCurrentTime();
|
||||
if (length <= 0) return 0;
|
||||
return mBigramDictionary.get()->getPredictions(word, length, outWords, outputScores,
|
||||
return mBigramDictionary->getPredictions(word, length, outWords, outputScores,
|
||||
outputTypes);
|
||||
}
|
||||
|
||||
|
@ -95,7 +95,7 @@ int Dictionary::getProbability(const int *word, int length) const {
|
|||
int Dictionary::getBigramProbability(const int *word0, int length0, const int *word1,
|
||||
int length1) const {
|
||||
TimeKeeper::setCurrentTime();
|
||||
return mBigramDictionary.get()->getBigramProbability(word0, length0, word1, length1);
|
||||
return mBigramDictionary->getBigramProbability(word0, length0, word1, length1);
|
||||
}
|
||||
|
||||
void Dictionary::addUnigramWord(const int *const word, const int length, const int probability,
|
||||
|
@ -103,7 +103,7 @@ void Dictionary::addUnigramWord(const int *const word, const int length, const i
|
|||
const int shortcutProbability, const bool isNotAWord, const bool isBlacklisted,
|
||||
const int timestamp) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
mDictionaryStructureWithBufferPolicy.get()->addUnigramWord(word, length, probability,
|
||||
mDictionaryStructureWithBufferPolicy->addUnigramWord(word, length, probability,
|
||||
shortcutTargetCodePoints, shortcutLength, shortcutProbability, isNotAWord,
|
||||
isBlacklisted, timestamp);
|
||||
}
|
||||
|
@ -111,48 +111,48 @@ void Dictionary::addUnigramWord(const int *const word, const int length, const i
|
|||
void Dictionary::addBigramWords(const int *const word0, const int length0, const int *const word1,
|
||||
const int length1, const int probability, const int timestamp) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
mDictionaryStructureWithBufferPolicy.get()->addBigramWords(word0, length0, word1, length1,
|
||||
mDictionaryStructureWithBufferPolicy->addBigramWords(word0, length0, word1, length1,
|
||||
probability, timestamp);
|
||||
}
|
||||
|
||||
void Dictionary::removeBigramWords(const int *const word0, const int length0,
|
||||
const int *const word1, const int length1) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
mDictionaryStructureWithBufferPolicy.get()->removeBigramWords(word0, length0, word1, length1);
|
||||
mDictionaryStructureWithBufferPolicy->removeBigramWords(word0, length0, word1, length1);
|
||||
}
|
||||
|
||||
void Dictionary::flush(const char *const filePath) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
mDictionaryStructureWithBufferPolicy.get()->flush(filePath);
|
||||
mDictionaryStructureWithBufferPolicy->flush(filePath);
|
||||
}
|
||||
|
||||
void Dictionary::flushWithGC(const char *const filePath) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
mDictionaryStructureWithBufferPolicy.get()->flushWithGC(filePath);
|
||||
mDictionaryStructureWithBufferPolicy->flushWithGC(filePath);
|
||||
}
|
||||
|
||||
bool Dictionary::needsToRunGC(const bool mindsBlockByGC) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
return mDictionaryStructureWithBufferPolicy.get()->needsToRunGC(mindsBlockByGC);
|
||||
return mDictionaryStructureWithBufferPolicy->needsToRunGC(mindsBlockByGC);
|
||||
}
|
||||
|
||||
void Dictionary::getProperty(const char *const query, const int queryLength, char *const outResult,
|
||||
const int maxResultLength) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
return mDictionaryStructureWithBufferPolicy.get()->getProperty(query, queryLength, outResult,
|
||||
return mDictionaryStructureWithBufferPolicy->getProperty(query, queryLength, outResult,
|
||||
maxResultLength);
|
||||
}
|
||||
|
||||
const WordProperty Dictionary::getWordProperty(const int *const codePoints,
|
||||
const int codePointCount) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
return mDictionaryStructureWithBufferPolicy.get()->getWordProperty(
|
||||
return mDictionaryStructureWithBufferPolicy->getWordProperty(
|
||||
codePoints, codePointCount);
|
||||
}
|
||||
|
||||
int Dictionary::getNextWordAndNextToken(const int token, int *const outCodePoints) {
|
||||
TimeKeeper::setCurrentTime();
|
||||
return mDictionaryStructureWithBufferPolicy.get()->getNextWordAndNextToken(
|
||||
return mDictionaryStructureWithBufferPolicy->getNextWordAndNextToken(
|
||||
token, outCodePoints);
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
#define LATINIME_DICTIONARY_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <memory>
|
||||
|
||||
#include "defines.h"
|
||||
#include "jni.h"
|
||||
|
@ -26,7 +27,6 @@
|
|||
#include "suggest/core/policy/dictionary_header_structure_policy.h"
|
||||
#include "suggest/core/policy/dictionary_structure_with_buffer_policy.h"
|
||||
#include "suggest/core/suggest_interface.h"
|
||||
#include "utils/exclusive_ownership_pointer.h"
|
||||
|
||||
namespace latinime {
|
||||
|
||||
|
@ -58,8 +58,8 @@ class Dictionary {
|
|||
static const int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000;
|
||||
static const int KIND_FLAG_EXACT_MATCH = 0x40000000;
|
||||
|
||||
Dictionary(JNIEnv *env, const DictionaryStructureWithBufferPolicy::StructurePolicyPtr
|
||||
&dictionaryStructureWithBufferPolicy);
|
||||
Dictionary(JNIEnv *env, DictionaryStructureWithBufferPolicy::StructurePolicyPtr
|
||||
dictionaryStructureWithBufferPolicy);
|
||||
|
||||
int getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession *traverseSession,
|
||||
int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints,
|
||||
|
@ -108,8 +108,8 @@ class Dictionary {
|
|||
private:
|
||||
DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary);
|
||||
|
||||
typedef ExclusiveOwnershipPointer<BigramDictionary> BigramDictionaryPtr;
|
||||
typedef ExclusiveOwnershipPointer<SuggestInterface> SuggestInterfacePtr;
|
||||
typedef std::unique_ptr<BigramDictionary> BigramDictionaryPtr;
|
||||
typedef std::unique_ptr<SuggestInterface> SuggestInterfacePtr;
|
||||
|
||||
static const int HEADER_ATTRIBUTE_BUFFER_SIZE;
|
||||
|
||||
|
|
|
@ -17,9 +17,10 @@
|
|||
#ifndef LATINIME_DICTIONARY_STRUCTURE_POLICY_H
|
||||
#define LATINIME_DICTIONARY_STRUCTURE_POLICY_H
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "defines.h"
|
||||
#include "suggest/core/dictionary/word_property.h"
|
||||
#include "utils/exclusive_ownership_pointer.h"
|
||||
|
||||
namespace latinime {
|
||||
|
||||
|
@ -35,7 +36,7 @@ class DictionaryShortcutsStructurePolicy;
|
|||
*/
|
||||
class DictionaryStructureWithBufferPolicy {
|
||||
public:
|
||||
typedef ExclusiveOwnershipPointer<DictionaryStructureWithBufferPolicy> StructurePolicyPtr;
|
||||
typedef std::unique_ptr<DictionaryStructureWithBufferPolicy> StructurePolicyPtr;
|
||||
|
||||
virtual ~DictionaryStructureWithBufferPolicy() {}
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ namespace latinime {
|
|||
if (isUpdatable) {
|
||||
AKLOGE("One file dictionaries don't support updating. path: %s", path);
|
||||
ASSERT(false);
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(nullptr);
|
||||
}
|
||||
return newPolicyforFileDict(path, bufOffset, size);
|
||||
}
|
||||
|
@ -55,13 +55,13 @@ namespace latinime {
|
|||
getHeaderFilePathInDictDir(path, headerFilePathBufSize, headerFilePath);
|
||||
// Allocated buffer in MmapedBuffer::openBuffer() will be freed in the destructor of
|
||||
// MmappedBufferPtr if the instance has the responsibility.
|
||||
MmappedBuffer::MmappedBufferPtr mmappedBuffer = MmappedBuffer::openBuffer(headerFilePath,
|
||||
isUpdatable);
|
||||
if (!mmappedBuffer.get()) {
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
|
||||
MmappedBuffer::MmappedBufferPtr mmappedBuffer(
|
||||
MmappedBuffer::openBuffer(headerFilePath, isUpdatable));
|
||||
if (!mmappedBuffer) {
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(nullptr);
|
||||
}
|
||||
switch (FormatUtils::detectFormatVersion(mmappedBuffer.get()->getBuffer(),
|
||||
mmappedBuffer.get()->getBufferSize())) {
|
||||
switch (FormatUtils::detectFormatVersion(mmappedBuffer->getBuffer(),
|
||||
mmappedBuffer->getBufferSize())) {
|
||||
case FormatUtils::VERSION_2:
|
||||
AKLOGE("Given path is a directory but the format is version 2. path: %s", path);
|
||||
break;
|
||||
|
@ -72,25 +72,25 @@ namespace latinime {
|
|||
Ver4DictConstants::HEADER_FILE_EXTENSION, dictDirPathBufSize, dictPath)) {
|
||||
AKLOGE("Dictionary file name is not valid as a ver4 dictionary. path: %s", path);
|
||||
ASSERT(false);
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(nullptr);
|
||||
}
|
||||
const Ver4DictBuffers::Ver4DictBuffersPtr dictBuffers =
|
||||
Ver4DictBuffers::openVer4DictBuffers(dictPath, mmappedBuffer);
|
||||
if (!dictBuffers.get() || !dictBuffers.get()->isValid()) {
|
||||
Ver4DictBuffers::Ver4DictBuffersPtr dictBuffers(
|
||||
Ver4DictBuffers::openVer4DictBuffers(dictPath, std::move(mmappedBuffer)));
|
||||
if (!dictBuffers || !dictBuffers->isValid()) {
|
||||
AKLOGE("DICT: The dictionary doesn't satisfy ver4 format requirements. path: %s",
|
||||
path);
|
||||
ASSERT(false);
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(nullptr);
|
||||
}
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(
|
||||
new Ver4PatriciaTriePolicy(dictBuffers));
|
||||
new Ver4PatriciaTriePolicy(std::move(dictBuffers)));
|
||||
}
|
||||
default:
|
||||
AKLOGE("DICT: dictionary format is unknown, bad magic number. path: %s", path);
|
||||
break;
|
||||
}
|
||||
ASSERT(false);
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(nullptr);
|
||||
}
|
||||
|
||||
/* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr
|
||||
|
@ -98,16 +98,16 @@ namespace latinime {
|
|||
const char *const path, const int bufOffset, const int size) {
|
||||
// Allocated buffer in MmapedBuffer::openBuffer() will be freed in the destructor of
|
||||
// MmappedBufferPtr if the instance has the responsibility.
|
||||
MmappedBuffer::MmappedBufferPtr mmappedBuffer = MmappedBuffer::openBuffer(path, bufOffset,
|
||||
size, false /* isUpdatable */);
|
||||
if (!mmappedBuffer.get()) {
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
|
||||
MmappedBuffer::MmappedBufferPtr mmappedBuffer(
|
||||
MmappedBuffer::openBuffer(path, bufOffset, size, false /* isUpdatable */));
|
||||
if (!mmappedBuffer) {
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(nullptr);
|
||||
}
|
||||
switch (FormatUtils::detectFormatVersion(mmappedBuffer.get()->getBuffer(),
|
||||
mmappedBuffer.get()->getBufferSize())) {
|
||||
switch (FormatUtils::detectFormatVersion(mmappedBuffer->getBuffer(),
|
||||
mmappedBuffer->getBufferSize())) {
|
||||
case FormatUtils::VERSION_2:
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(
|
||||
new PatriciaTriePolicy(mmappedBuffer));
|
||||
new PatriciaTriePolicy(std::move(mmappedBuffer)));
|
||||
case FormatUtils::VERSION_4:
|
||||
AKLOGE("Given path is a file but the format is version 4. path: %s", path);
|
||||
break;
|
||||
|
@ -116,7 +116,7 @@ namespace latinime {
|
|||
break;
|
||||
}
|
||||
ASSERT(false);
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
|
||||
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(nullptr);
|
||||
}
|
||||
|
||||
/* static */ void DictionaryStructureWithBufferPolicyFactory::getHeaderFilePathInDictDir(
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
|
||||
#include "defines.h"
|
||||
#include "suggest/core/policy/dictionary_structure_with_buffer_policy.h"
|
||||
#include "utils/exclusive_ownership_pointer.h"
|
||||
|
||||
namespace latinime {
|
||||
|
||||
|
|
|
@ -37,12 +37,11 @@ class DicNodeVector;
|
|||
|
||||
class PatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
|
||||
public:
|
||||
PatriciaTriePolicy(const MmappedBuffer::MmappedBufferPtr &mmappedBuffer)
|
||||
: mMmappedBuffer(mmappedBuffer),
|
||||
mHeaderPolicy(mMmappedBuffer.get()->getBuffer(), FormatUtils::VERSION_2),
|
||||
mDictRoot(mMmappedBuffer.get()->getBuffer() + mHeaderPolicy.getSize()),
|
||||
mDictBufferSize(mMmappedBuffer.get()->getBufferSize()
|
||||
- mHeaderPolicy.getSize()),
|
||||
PatriciaTriePolicy(MmappedBuffer::MmappedBufferPtr mmappedBuffer)
|
||||
: mMmappedBuffer(std::move(mmappedBuffer)),
|
||||
mHeaderPolicy(mMmappedBuffer->getBuffer(), FormatUtils::VERSION_2),
|
||||
mDictRoot(mMmappedBuffer->getBuffer() + mHeaderPolicy.getSize()),
|
||||
mDictBufferSize(mMmappedBuffer->getBufferSize() - mHeaderPolicy.getSize()),
|
||||
mBigramListPolicy(mDictRoot), mShortcutListPolicy(mDictRoot),
|
||||
mPtNodeReader(mDictRoot, mDictBufferSize, &mBigramListPolicy, &mShortcutListPolicy),
|
||||
mPtNodeArrayReader(mDictRoot, mDictBufferSize),
|
||||
|
|
|
@ -31,15 +31,14 @@ class SingleDictContent : public DictContent {
|
|||
SingleDictContent(const char *const dictPath, const char *const contentFileName,
|
||||
const bool isUpdatable)
|
||||
: mMmappedBuffer(MmappedBuffer::openBuffer(dictPath, contentFileName, isUpdatable)),
|
||||
mExpandableContentBuffer(mMmappedBuffer.get() ?
|
||||
mMmappedBuffer.get()->getBuffer() : nullptr,
|
||||
mMmappedBuffer.get() ? mMmappedBuffer.get()->getBufferSize() : 0,
|
||||
mExpandableContentBuffer(mMmappedBuffer ? mMmappedBuffer->getBuffer() : nullptr,
|
||||
mMmappedBuffer ? mMmappedBuffer->getBufferSize() : 0,
|
||||
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
|
||||
mIsValid(mMmappedBuffer.get()) {}
|
||||
mIsValid(mMmappedBuffer) {}
|
||||
|
||||
SingleDictContent()
|
||||
: mMmappedBuffer(0), mExpandableContentBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE),
|
||||
mIsValid(true) {}
|
||||
: mMmappedBuffer(nullptr),
|
||||
mExpandableContentBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE), mIsValid(true) {}
|
||||
|
||||
virtual ~SingleDictContent() {}
|
||||
|
||||
|
|
|
@ -38,26 +38,25 @@ class SparseTableDictContent : public DictContent {
|
|||
MmappedBuffer::openBuffer(dictPath, lookupTableFileName, isUpdatable)),
|
||||
mAddressTableBuffer(
|
||||
MmappedBuffer::openBuffer(dictPath, addressTableFileName, isUpdatable)),
|
||||
mContentBuffer(MmappedBuffer::openBuffer(dictPath, contentFileName, isUpdatable)),
|
||||
mContentBuffer(
|
||||
MmappedBuffer::openBuffer(dictPath, contentFileName, isUpdatable)),
|
||||
mExpandableLookupTableBuffer(
|
||||
mLookupTableBuffer.get() ? mLookupTableBuffer.get()->getBuffer() : nullptr,
|
||||
mLookupTableBuffer.get() ? mLookupTableBuffer.get()->getBufferSize() : 0,
|
||||
mLookupTableBuffer ? mLookupTableBuffer->getBuffer() : nullptr,
|
||||
mLookupTableBuffer ? mLookupTableBuffer->getBufferSize() : 0,
|
||||
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
|
||||
mExpandableAddressTableBuffer(
|
||||
mAddressTableBuffer.get() ? mAddressTableBuffer.get()->getBuffer() : nullptr,
|
||||
mAddressTableBuffer.get() ? mAddressTableBuffer.get()->getBufferSize() : 0,
|
||||
mAddressTableBuffer ? mAddressTableBuffer->getBuffer() : nullptr,
|
||||
mAddressTableBuffer ? mAddressTableBuffer->getBufferSize() : 0,
|
||||
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
|
||||
mExpandableContentBuffer(mContentBuffer.get() ?
|
||||
mContentBuffer.get()->getBuffer() : nullptr,
|
||||
mContentBuffer.get() ? mContentBuffer.get()->getBufferSize() : 0,
|
||||
mExpandableContentBuffer(mContentBuffer ? mContentBuffer->getBuffer() : nullptr,
|
||||
mContentBuffer ? mContentBuffer->getBufferSize() : 0,
|
||||
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
|
||||
mAddressLookupTable(&mExpandableLookupTableBuffer, &mExpandableAddressTableBuffer,
|
||||
sparseTableBlockSize, sparseTableDataSize),
|
||||
mIsValid(mLookupTableBuffer.get() && mAddressTableBuffer.get()
|
||||
&& mContentBuffer.get()) {}
|
||||
mIsValid(mLookupTableBuffer && mAddressTableBuffer && mContentBuffer) {}
|
||||
|
||||
SparseTableDictContent(const int sparseTableBlockSize, const int sparseTableDataSize)
|
||||
: mLookupTableBuffer(0), mAddressTableBuffer(0), mContentBuffer(0),
|
||||
: mLookupTableBuffer(), mAddressTableBuffer(), mContentBuffer(),
|
||||
mExpandableLookupTableBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE),
|
||||
mExpandableAddressTableBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE),
|
||||
mExpandableContentBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE),
|
||||
|
|
|
@ -27,15 +27,15 @@
|
|||
namespace latinime {
|
||||
|
||||
/* static */ Ver4DictBuffers::Ver4DictBuffersPtr Ver4DictBuffers::openVer4DictBuffers(
|
||||
const char *const dictPath, const MmappedBuffer::MmappedBufferPtr &headerBuffer) {
|
||||
if (!headerBuffer.get()) {
|
||||
const char *const dictPath, MmappedBuffer::MmappedBufferPtr headerBuffer) {
|
||||
if (!headerBuffer) {
|
||||
ASSERT(false);
|
||||
AKLOGE("The header buffer must be valid to open ver4 dict buffers.");
|
||||
return Ver4DictBuffersPtr(nullptr);
|
||||
}
|
||||
// TODO: take only dictDirPath, and open both header and trie files in the constructor below
|
||||
return Ver4DictBuffersPtr(new Ver4DictBuffers(
|
||||
dictPath, headerBuffer, headerBuffer.get()->isUpdatable()));
|
||||
const bool isUpdatable = headerBuffer->isUpdatable();
|
||||
return Ver4DictBuffersPtr(new Ver4DictBuffers(dictPath, std::move(headerBuffer), isUpdatable));
|
||||
}
|
||||
|
||||
bool Ver4DictBuffers::flushHeaderAndDictBuffers(const char *const dictDirPath,
|
||||
|
@ -113,27 +113,25 @@ bool Ver4DictBuffers::flushHeaderAndDictBuffers(const char *const dictDirPath,
|
|||
}
|
||||
|
||||
Ver4DictBuffers::Ver4DictBuffers(const char *const dictPath,
|
||||
const MmappedBuffer::MmappedBufferPtr &headerBuffer, const bool isUpdatable)
|
||||
: mHeaderBuffer(headerBuffer),
|
||||
MmappedBuffer::MmappedBufferPtr headerBuffer, const bool isUpdatable)
|
||||
: mHeaderBuffer(std::move(headerBuffer)),
|
||||
mDictBuffer(MmappedBuffer::openBuffer(dictPath,
|
||||
Ver4DictConstants::TRIE_FILE_EXTENSION, isUpdatable)),
|
||||
mHeaderPolicy(headerBuffer.get()->getBuffer(), FormatUtils::VERSION_4),
|
||||
mExpandableHeaderBuffer(headerBuffer.get() ? headerBuffer.get()->getBuffer() : nullptr,
|
||||
mHeaderPolicy(mHeaderBuffer->getBuffer(), FormatUtils::VERSION_4),
|
||||
mExpandableHeaderBuffer(mHeaderBuffer ? mHeaderBuffer->getBuffer() : nullptr,
|
||||
mHeaderPolicy.getSize(),
|
||||
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
|
||||
mExpandableTrieBuffer(mDictBuffer.get() ? mDictBuffer.get()->getBuffer() : nullptr,
|
||||
mDictBuffer.get() ? mDictBuffer.get()->getBufferSize() : 0,
|
||||
mExpandableTrieBuffer(mDictBuffer ? mDictBuffer->getBuffer() : nullptr,
|
||||
mDictBuffer ? mDictBuffer->getBufferSize() : 0,
|
||||
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
|
||||
mTerminalPositionLookupTable(dictPath, isUpdatable),
|
||||
mProbabilityDictContent(dictPath, mHeaderPolicy.hasHistoricalInfoOfWords(),
|
||||
isUpdatable),
|
||||
mBigramDictContent(dictPath, mHeaderPolicy.hasHistoricalInfoOfWords(),
|
||||
isUpdatable),
|
||||
mProbabilityDictContent(dictPath, mHeaderPolicy.hasHistoricalInfoOfWords(), isUpdatable),
|
||||
mBigramDictContent(dictPath, mHeaderPolicy.hasHistoricalInfoOfWords(), isUpdatable),
|
||||
mShortcutDictContent(dictPath, isUpdatable),
|
||||
mIsUpdatable(isUpdatable) {}
|
||||
|
||||
Ver4DictBuffers::Ver4DictBuffers(const HeaderPolicy *const headerPolicy)
|
||||
: mHeaderBuffer(0), mDictBuffer(0), mHeaderPolicy(),
|
||||
: mHeaderBuffer(nullptr), mDictBuffer(nullptr), mHeaderPolicy(),
|
||||
mExpandableHeaderBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE),
|
||||
mExpandableTrieBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE),
|
||||
mTerminalPositionLookupTable(),
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
#ifndef LATINIME_VER4_DICT_BUFFER_H
|
||||
#define LATINIME_VER4_DICT_BUFFER_H
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "defines.h"
|
||||
#include "suggest/policyimpl/dictionary/header/header_policy.h"
|
||||
#include "suggest/policyimpl/dictionary/structure/v4/content/bigram_dict_content.h"
|
||||
|
@ -31,10 +33,10 @@ namespace latinime {
|
|||
|
||||
class Ver4DictBuffers {
|
||||
public:
|
||||
typedef ExclusiveOwnershipPointer<Ver4DictBuffers> Ver4DictBuffersPtr;
|
||||
typedef std::unique_ptr<Ver4DictBuffers> Ver4DictBuffersPtr;
|
||||
|
||||
static Ver4DictBuffersPtr openVer4DictBuffers(const char *const dictDirPath,
|
||||
const MmappedBuffer::MmappedBufferPtr &headerBuffer);
|
||||
MmappedBuffer::MmappedBufferPtr headerBuffer);
|
||||
|
||||
static AK_FORCE_INLINE Ver4DictBuffersPtr createVer4DictBuffers(
|
||||
const HeaderPolicy *const headerPolicy) {
|
||||
|
@ -42,7 +44,7 @@ class Ver4DictBuffers {
|
|||
}
|
||||
|
||||
AK_FORCE_INLINE bool isValid() const {
|
||||
return mHeaderBuffer.get() && mDictBuffer.get() && mHeaderPolicy.isValid()
|
||||
return mHeaderBuffer && mDictBuffer && mHeaderPolicy.isValid()
|
||||
&& mProbabilityDictContent.isValid() && mTerminalPositionLookupTable.isValid()
|
||||
&& mBigramDictContent.isValid() && mShortcutDictContent.isValid();
|
||||
}
|
||||
|
@ -118,7 +120,7 @@ class Ver4DictBuffers {
|
|||
DISALLOW_COPY_AND_ASSIGN(Ver4DictBuffers);
|
||||
|
||||
Ver4DictBuffers(const char *const dictDirPath,
|
||||
const MmappedBuffer::MmappedBufferPtr &headerBuffer, const bool isUpdatable);
|
||||
const MmappedBuffer::MmappedBufferPtr headerBuffer, const bool isUpdatable);
|
||||
|
||||
Ver4DictBuffers(const HeaderPolicy *const headerPolicy);
|
||||
|
||||
|
|
|
@ -135,7 +135,7 @@ int Ver4PatriciaTriePolicy::getShortcutPositionOfPtNode(const int ptNodePos) con
|
|||
if (ptNodeParams.isDeleted()) {
|
||||
return NOT_A_DICT_POS;
|
||||
}
|
||||
return mBuffers.get()->getShortcutDictContent()->getShortcutListHeadPos(
|
||||
return mBuffers->getShortcutDictContent()->getShortcutListHeadPos(
|
||||
ptNodeParams.getTerminalId());
|
||||
}
|
||||
|
||||
|
@ -147,7 +147,7 @@ int Ver4PatriciaTriePolicy::getBigramsPositionOfPtNode(const int ptNodePos) cons
|
|||
if (ptNodeParams.isDeleted()) {
|
||||
return NOT_A_DICT_POS;
|
||||
}
|
||||
return mBuffers.get()->getBigramDictContent()->getBigramListHeadPos(
|
||||
return mBuffers->getBigramDictContent()->getBigramListHeadPos(
|
||||
ptNodeParams.getTerminalId());
|
||||
}
|
||||
|
||||
|
@ -155,7 +155,7 @@ bool Ver4PatriciaTriePolicy::addUnigramWord(const int *const word, const int len
|
|||
const int probability, const int *const shortcutTargetCodePoints, const int shortcutLength,
|
||||
const int shortcutProbability, const bool isNotAWord, const bool isBlacklisted,
|
||||
const int timestamp) {
|
||||
if (!mBuffers.get()->isUpdatable()) {
|
||||
if (!mBuffers->isUpdatable()) {
|
||||
AKLOGI("Warning: addUnigramWord() is called for non-updatable dictionary.");
|
||||
return false;
|
||||
}
|
||||
|
@ -205,7 +205,7 @@ bool Ver4PatriciaTriePolicy::addUnigramWord(const int *const word, const int len
|
|||
bool Ver4PatriciaTriePolicy::addBigramWords(const int *const word0, const int length0,
|
||||
const int *const word1, const int length1, const int probability,
|
||||
const int timestamp) {
|
||||
if (!mBuffers.get()->isUpdatable()) {
|
||||
if (!mBuffers->isUpdatable()) {
|
||||
AKLOGI("Warning: addBigramWords() is called for non-updatable dictionary.");
|
||||
return false;
|
||||
}
|
||||
|
@ -243,7 +243,7 @@ bool Ver4PatriciaTriePolicy::addBigramWords(const int *const word0, const int le
|
|||
|
||||
bool Ver4PatriciaTriePolicy::removeBigramWords(const int *const word0, const int length0,
|
||||
const int *const word1, const int length1) {
|
||||
if (!mBuffers.get()->isUpdatable()) {
|
||||
if (!mBuffers->isUpdatable()) {
|
||||
AKLOGI("Warning: addBigramWords() is called for non-updatable dictionary.");
|
||||
return false;
|
||||
}
|
||||
|
@ -276,7 +276,7 @@ bool Ver4PatriciaTriePolicy::removeBigramWords(const int *const word0, const int
|
|||
}
|
||||
|
||||
void Ver4PatriciaTriePolicy::flush(const char *const filePath) {
|
||||
if (!mBuffers.get()->isUpdatable()) {
|
||||
if (!mBuffers->isUpdatable()) {
|
||||
AKLOGI("Warning: flush() is called for non-updatable dictionary. filePath: %s", filePath);
|
||||
return;
|
||||
}
|
||||
|
@ -287,7 +287,7 @@ void Ver4PatriciaTriePolicy::flush(const char *const filePath) {
|
|||
}
|
||||
|
||||
void Ver4PatriciaTriePolicy::flushWithGC(const char *const filePath) {
|
||||
if (!mBuffers.get()->isUpdatable()) {
|
||||
if (!mBuffers->isUpdatable()) {
|
||||
AKLOGI("Warning: flushWithGC() is called for non-updatable dictionary.");
|
||||
return;
|
||||
}
|
||||
|
@ -298,11 +298,11 @@ void Ver4PatriciaTriePolicy::flushWithGC(const char *const filePath) {
|
|||
}
|
||||
|
||||
bool Ver4PatriciaTriePolicy::needsToRunGC(const bool mindsBlockByGC) const {
|
||||
if (!mBuffers.get()->isUpdatable()) {
|
||||
if (!mBuffers->isUpdatable()) {
|
||||
AKLOGI("Warning: needsToRunGC() is called for non-updatable dictionary.");
|
||||
return false;
|
||||
}
|
||||
if (mBuffers.get()->isNearSizeLimit()) {
|
||||
if (mBuffers->isNearSizeLimit()) {
|
||||
// Additional buffer size is near the limit.
|
||||
return true;
|
||||
} else if (mHeaderPolicy->getExtendedRegionSize() + mDictBuffer->getUsedAdditionalBufferSize()
|
||||
|
@ -354,7 +354,7 @@ const WordProperty Ver4PatriciaTriePolicy::getWordProperty(const int *const code
|
|||
std::vector<int> codePointVector(ptNodeParams.getCodePoints(),
|
||||
ptNodeParams.getCodePoints() + ptNodeParams.getCodePointCount());
|
||||
const ProbabilityEntry probabilityEntry =
|
||||
mBuffers.get()->getProbabilityDictContent()->getProbabilityEntry(
|
||||
mBuffers->getProbabilityDictContent()->getProbabilityEntry(
|
||||
ptNodeParams.getTerminalId());
|
||||
const HistoricalInfo *const historicalInfo = probabilityEntry.getHistoricalInfo();
|
||||
// Fetch bigram information.
|
||||
|
@ -362,9 +362,9 @@ const WordProperty Ver4PatriciaTriePolicy::getWordProperty(const int *const code
|
|||
const int bigramListPos = getBigramsPositionOfPtNode(ptNodePos);
|
||||
if (bigramListPos != NOT_A_DICT_POS) {
|
||||
int bigramWord1CodePoints[MAX_WORD_LENGTH];
|
||||
const BigramDictContent *const bigramDictContent = mBuffers.get()->getBigramDictContent();
|
||||
const BigramDictContent *const bigramDictContent = mBuffers->getBigramDictContent();
|
||||
const TerminalPositionLookupTable *const terminalPositionLookupTable =
|
||||
mBuffers.get()->getTerminalPositionLookupTable();
|
||||
mBuffers->getTerminalPositionLookupTable();
|
||||
bool hasNext = true;
|
||||
int readingPos = bigramListPos;
|
||||
while (hasNext) {
|
||||
|
@ -400,7 +400,7 @@ const WordProperty Ver4PatriciaTriePolicy::getWordProperty(const int *const code
|
|||
if (shortcutPos != NOT_A_DICT_POS) {
|
||||
int shortcutTarget[MAX_WORD_LENGTH];
|
||||
const ShortcutDictContent *const shortcutDictContent =
|
||||
mBuffers.get()->getShortcutDictContent();
|
||||
mBuffers->getShortcutDictContent();
|
||||
bool hasNext = true;
|
||||
while (hasNext) {
|
||||
int shortcutTargetLength = 0;
|
||||
|
|
|
@ -37,17 +37,16 @@ namespace latinime {
|
|||
class DicNode;
|
||||
class DicNodeVector;
|
||||
|
||||
// TODO: Implement.
|
||||
class Ver4PatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
|
||||
public:
|
||||
Ver4PatriciaTriePolicy(const Ver4DictBuffers::Ver4DictBuffersPtr &buffers)
|
||||
: mBuffers(buffers), mHeaderPolicy(mBuffers.get()->getHeaderPolicy()),
|
||||
mDictBuffer(mBuffers.get()->getWritableTrieBuffer()),
|
||||
mBigramPolicy(mBuffers.get()->getMutableBigramDictContent(),
|
||||
mBuffers.get()->getTerminalPositionLookupTable(), mHeaderPolicy),
|
||||
mShortcutPolicy(mBuffers.get()->getMutableShortcutDictContent(),
|
||||
mBuffers.get()->getTerminalPositionLookupTable()),
|
||||
mNodeReader(mDictBuffer, mBuffers.get()->getProbabilityDictContent(), mHeaderPolicy),
|
||||
Ver4PatriciaTriePolicy(Ver4DictBuffers::Ver4DictBuffersPtr buffers)
|
||||
: mBuffers(std::move(buffers)), mHeaderPolicy(mBuffers->getHeaderPolicy()),
|
||||
mDictBuffer(mBuffers->getWritableTrieBuffer()),
|
||||
mBigramPolicy(mBuffers->getMutableBigramDictContent(),
|
||||
mBuffers->getTerminalPositionLookupTable(), mHeaderPolicy),
|
||||
mShortcutPolicy(mBuffers->getMutableShortcutDictContent(),
|
||||
mBuffers->getTerminalPositionLookupTable()),
|
||||
mNodeReader(mDictBuffer, mBuffers->getProbabilityDictContent(), mHeaderPolicy),
|
||||
mPtNodeArrayReader(mDictBuffer),
|
||||
mNodeWriter(mDictBuffer, mBuffers.get(), mHeaderPolicy, &mNodeReader,
|
||||
&mPtNodeArrayReader, &mBigramPolicy, &mShortcutPolicy),
|
||||
|
@ -132,7 +131,7 @@ class Ver4PatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
|
|||
static const int MARGIN_TO_REFUSE_DYNAMIC_OPERATIONS;
|
||||
static const int MIN_DICT_SIZE_TO_REFUSE_DYNAMIC_OPERATIONS;
|
||||
|
||||
Ver4DictBuffers::Ver4DictBuffersPtr mBuffers;
|
||||
const Ver4DictBuffers::Ver4DictBuffersPtr mBuffers;
|
||||
const HeaderPolicy *const mHeaderPolicy;
|
||||
BufferWithExtendableBuffer *const mDictBuffer;
|
||||
Ver4BigramListPolicy mBigramPolicy;
|
||||
|
|
|
@ -67,7 +67,7 @@ bool Ver4PatriciaTrieWritingHelper::writeToDictFileWithGC(const int rootPtNodeAr
|
|||
unigramCount, bigramCount, 0 /* extendedRegionSize */, &headerBuffer)) {
|
||||
return false;
|
||||
}
|
||||
return dictBuffers.get()->flushHeaderAndDictBuffers(dictDirPath, &headerBuffer);
|
||||
return dictBuffers->flushHeaderAndDictBuffers(dictDirPath, &headerBuffer);
|
||||
}
|
||||
|
||||
bool Ver4PatriciaTrieWritingHelper::runGC(const int rootPtNodeArrayPos,
|
||||
|
|
|
@ -48,17 +48,17 @@ const char *const DictFileWritingUtils::TEMP_FILE_SUFFIX_FOR_WRITING_DICT_FILE =
|
|||
const std::vector<int> localeAsCodePointVector,
|
||||
const DictionaryHeaderStructurePolicy::AttributeMap *const attributeMap) {
|
||||
HeaderPolicy headerPolicy(FormatUtils::VERSION_4, localeAsCodePointVector, attributeMap);
|
||||
Ver4DictBuffers::Ver4DictBuffersPtr dictBuffers =
|
||||
Ver4DictBuffers::createVer4DictBuffers(&headerPolicy);
|
||||
Ver4DictBuffers::Ver4DictBuffersPtr dictBuffers(
|
||||
Ver4DictBuffers::createVer4DictBuffers(&headerPolicy));
|
||||
headerPolicy.fillInAndWriteHeaderToBuffer(true /* updatesLastDecayedTime */,
|
||||
0 /* unigramCount */, 0 /* bigramCount */,
|
||||
0 /* extendedRegionSize */, dictBuffers.get()->getWritableHeaderBuffer());
|
||||
0 /* extendedRegionSize */, dictBuffers->getWritableHeaderBuffer());
|
||||
if (!DynamicPtWritingUtils::writeEmptyDictionary(
|
||||
dictBuffers.get()->getWritableTrieBuffer(), 0 /* rootPos */)) {
|
||||
dictBuffers->getWritableTrieBuffer(), 0 /* rootPos */)) {
|
||||
AKLOGE("Empty ver4 dictionary structure cannot be created on memory.");
|
||||
return false;
|
||||
}
|
||||
return dictBuffers.get()->flush(dirPath);
|
||||
return dictBuffers->flush(dirPath);
|
||||
}
|
||||
|
||||
/* static */ bool DictFileWritingUtils::flushAllHeaderAndBodyToFile(const char *const filePath,
|
||||
|
|
|
@ -33,7 +33,7 @@ namespace latinime {
|
|||
const int mmapFd = open(path, O_RDONLY);
|
||||
if (mmapFd < 0) {
|
||||
AKLOGE("DICT: Can't open the source. path=%s errno=%d", path, errno);
|
||||
return MmappedBufferPtr(0);
|
||||
return MmappedBufferPtr(nullptr);
|
||||
}
|
||||
const int pagesize = sysconf(_SC_PAGESIZE);
|
||||
const int offset = bufferOffset % pagesize;
|
||||
|
@ -45,13 +45,13 @@ namespace latinime {
|
|||
if (mmappedBuffer == MAP_FAILED) {
|
||||
AKLOGE("DICT: Can't mmap dictionary. errno=%d", errno);
|
||||
close(mmapFd);
|
||||
return MmappedBufferPtr(0);
|
||||
return MmappedBufferPtr(nullptr);
|
||||
}
|
||||
uint8_t *const buffer = static_cast<uint8_t *>(mmappedBuffer) + offset;
|
||||
if (!buffer) {
|
||||
AKLOGE("DICT: buffer is null");
|
||||
close(mmapFd);
|
||||
return MmappedBufferPtr(0);
|
||||
return MmappedBufferPtr(nullptr);
|
||||
}
|
||||
return MmappedBufferPtr(new MmappedBuffer(buffer, bufferSize, mmappedBuffer, alignedSize,
|
||||
mmapFd, isUpdatable));
|
||||
|
@ -61,7 +61,7 @@ namespace latinime {
|
|||
const char *const path, const bool isUpdatable) {
|
||||
const int fileSize = FileUtils::getFileSize(path);
|
||||
if (fileSize == -1) {
|
||||
return MmappedBufferPtr(0);
|
||||
return MmappedBufferPtr(nullptr);
|
||||
} else if (fileSize == 0) {
|
||||
return MmappedBufferPtr(new MmappedBuffer(isUpdatable));
|
||||
} else {
|
||||
|
@ -76,7 +76,7 @@ namespace latinime {
|
|||
const int filePathLength = snprintf(filePath, filePathBufferSize, "%s%s", dirPath,
|
||||
fileName);
|
||||
if (filePathLength >= filePathBufferSize) {
|
||||
return 0;
|
||||
return MmappedBufferPtr(nullptr);
|
||||
}
|
||||
return openBuffer(filePath, isUpdatable);
|
||||
}
|
||||
|
|
|
@ -17,16 +17,16 @@
|
|||
#ifndef LATINIME_MMAPPED_BUFFER_H
|
||||
#define LATINIME_MMAPPED_BUFFER_H
|
||||
|
||||
#include <memory>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "defines.h"
|
||||
#include "utils/exclusive_ownership_pointer.h"
|
||||
|
||||
namespace latinime {
|
||||
|
||||
class MmappedBuffer {
|
||||
public:
|
||||
typedef ExclusiveOwnershipPointer<MmappedBuffer> MmappedBufferPtr;
|
||||
typedef std::unique_ptr<const MmappedBuffer> MmappedBufferPtr;
|
||||
|
||||
static MmappedBufferPtr openBuffer(const char *const path,
|
||||
const int bufferOffset, const int bufferSize, const bool isUpdatable);
|
||||
|
|
|
@ -1,81 +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_EXCLUSIVE_OWNERSHIP_POINTER_H
|
||||
#define LATINIME_EXCLUSIVE_OWNERSHIP_POINTER_H
|
||||
|
||||
#include "defines.h"
|
||||
|
||||
namespace latinime {
|
||||
|
||||
template<class T>
|
||||
class ExclusiveOwnershipPointer {
|
||||
public:
|
||||
// This instance become an owner of the raw pointer.
|
||||
AK_FORCE_INLINE ExclusiveOwnershipPointer(T *const rawPointer)
|
||||
: mPointer(rawPointer),
|
||||
mSharedOwnerPtr(new (ExclusiveOwnershipPointer<T> *)(this)) {}
|
||||
|
||||
// Move the ownership.
|
||||
AK_FORCE_INLINE ExclusiveOwnershipPointer(const ExclusiveOwnershipPointer<T> &pointer)
|
||||
: mPointer(pointer.mPointer), mSharedOwnerPtr(pointer.mSharedOwnerPtr) {
|
||||
transferOwnership(&pointer);
|
||||
}
|
||||
|
||||
AK_FORCE_INLINE ~ExclusiveOwnershipPointer() {
|
||||
deletePointersIfHavingOwnership();
|
||||
}
|
||||
|
||||
AK_FORCE_INLINE T *get() const {
|
||||
return mPointer;
|
||||
}
|
||||
|
||||
private:
|
||||
// This class allows to copy and ensures only one instance has the ownership of the
|
||||
// managed pointer.
|
||||
DISALLOW_DEFAULT_CONSTRUCTOR(ExclusiveOwnershipPointer);
|
||||
DISALLOW_ASSIGNMENT_OPERATOR(ExclusiveOwnershipPointer);
|
||||
|
||||
void transferOwnership(const ExclusiveOwnershipPointer<T> *const src) {
|
||||
if (*mSharedOwnerPtr != src) {
|
||||
AKLOGE("Failed to transfer the ownership because src is not the current owner."
|
||||
"src: %p, owner: %p", src, *mSharedOwnerPtr);
|
||||
ASSERT(false);
|
||||
return;
|
||||
}
|
||||
// Transfer the ownership from src to this instance.
|
||||
*mSharedOwnerPtr = this;
|
||||
}
|
||||
|
||||
void deletePointersIfHavingOwnership() {
|
||||
if (mSharedOwnerPtr && *mSharedOwnerPtr == this) {
|
||||
if (mPointer) {
|
||||
if (DEBUG_DICT) {
|
||||
AKLOGI("Releasing pointer: %p", mPointer);
|
||||
}
|
||||
delete mPointer;
|
||||
}
|
||||
delete mSharedOwnerPtr;
|
||||
}
|
||||
}
|
||||
|
||||
T *mPointer;
|
||||
// mSharedOwnerPtr points a shared memory space where the instance which has the ownership is
|
||||
// stored.
|
||||
ExclusiveOwnershipPointer<T> **mSharedOwnerPtr;
|
||||
};
|
||||
} // namespace latinime
|
||||
#endif /* LATINIME_EXCLUSIVE_OWNERSHIP_POINTER_H */
|
Loading…
Reference in a new issue