aboutsummaryrefslogtreecommitdiffstats
path: root/native
diff options
context:
space:
mode:
Diffstat (limited to 'native')
-rw-r--r--native/jni/Android.mk2
-rw-r--r--native/jni/NativeFileList.mk1
-rw-r--r--native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.cpp59
-rw-r--r--native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h4
-rw-r--r--native/jni/tests/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer_test.cpp94
5 files changed, 136 insertions, 24 deletions
diff --git a/native/jni/Android.mk b/native/jni/Android.mk
index 8be4d78b6..3a2073f03 100644
--- a/native/jni/Android.mk
+++ b/native/jni/Android.mk
@@ -90,7 +90,7 @@ LOCAL_MODULE_TAGS := optional
LOCAL_CLANG := true
LOCAL_SDK_VERSION := 14
LOCAL_NDK_STL_VARIANT := c++_static
-LOCAL_LDFLAGS += -ldl -fuse-ld=mcld
+LOCAL_LDFLAGS += -ldl
include $(BUILD_SHARED_LIBRARY)
#################### Clean up the tmp vars
diff --git a/native/jni/NativeFileList.mk b/native/jni/NativeFileList.mk
index fe2106140..1cb61c45f 100644
--- a/native/jni/NativeFileList.mk
+++ b/native/jni/NativeFileList.mk
@@ -124,4 +124,5 @@ LATIN_IME_CORE_TEST_FILES := \
defines_test.cpp \
suggest/core/layout/normal_distribution_2d_test.cpp \
suggest/core/dictionary/bloom_filter_test.cpp \
+ suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer_test.cpp \
utils/autocorrection_threshold_utils_test.cpp
diff --git a/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.cpp b/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.cpp
index 259dae4c6..825b72c6a 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.cpp
@@ -49,6 +49,10 @@ void BufferWithExtendableBuffer::readCodePointsAndAdvancePosition(const int maxC
}
}
+bool BufferWithExtendableBuffer::extend(const int size) {
+ return checkAndPrepareWriting(getTailPosition(), size);
+}
+
bool BufferWithExtendableBuffer::writeUint(const uint32_t data, const int size, const int pos) {
int writingPos = pos;
return writeUintAndAdvancePosition(data, size, &writingPos);
@@ -96,38 +100,49 @@ bool BufferWithExtendableBuffer::writeCodePointsAndAdvancePosition(const int *co
return true;
}
-bool BufferWithExtendableBuffer::extendBuffer() {
+bool BufferWithExtendableBuffer::extendBuffer(const size_t size) {
+ const size_t extendSize = std::max(EXTEND_ADDITIONAL_BUFFER_SIZE_STEP, size);
const size_t sizeAfterExtending =
- mAdditionalBuffer.size() + EXTEND_ADDITIONAL_BUFFER_SIZE_STEP;
- if (sizeAfterExtending > mMaxAdditionalBufferSize) {
+ std::min(mAdditionalBuffer.size() + extendSize, mMaxAdditionalBufferSize);
+ if (sizeAfterExtending < mAdditionalBuffer.size() + size) {
return false;
}
- mAdditionalBuffer.resize(mAdditionalBuffer.size() + EXTEND_ADDITIONAL_BUFFER_SIZE_STEP);
+ mAdditionalBuffer.resize(sizeAfterExtending);
return true;
}
bool BufferWithExtendableBuffer::checkAndPrepareWriting(const int pos, const int size) {
- if (isInAdditionalBuffer(pos)) {
- const int tailPosition = getTailPosition();
- if (pos == tailPosition) {
- // Append data to the tail.
- if (pos + size > static_cast<int>(mAdditionalBuffer.size()) + mOriginalBufferSize) {
- // Need to extend buffer.
- if (!extendBuffer()) {
- return false;
- }
- }
- mUsedAdditionalBufferSize += size;
- } else if (pos + size > tailPosition) {
- // The access will beyond the tail of used region.
- return false;
- }
- } else {
- if (pos < 0 || mOriginalBufferSize < pos + size) {
- // Invalid position or violate the boundary.
+ if (pos < 0 || size < 0) {
+ // Invalid position or size.
+ return false;
+ }
+ const size_t totalRequiredSize = static_cast<size_t>(pos + size);
+ if (!isInAdditionalBuffer(pos)) {
+ // Here don't need to care about the additional buffer.
+ if (static_cast<size_t>(mOriginalBufferSize) < totalRequiredSize) {
+ // Violate the boundary.
return false;
}
+ // The buffer has sufficient capacity.
+ return true;
+ }
+ // Hereafter, pos is in the additional buffer.
+ const size_t tailPosition = static_cast<size_t>(getTailPosition());
+ if (totalRequiredSize <= tailPosition) {
+ // The buffer has sufficient capacity.
+ return true;
+ }
+ if (static_cast<size_t>(pos) != tailPosition) {
+ // The additional buffer must be extended from the tail position.
+ return false;
+ }
+ const size_t extendSize = totalRequiredSize -
+ std::min(mAdditionalBuffer.size() + mOriginalBufferSize, totalRequiredSize);
+ if (extendSize > 0 && !extendBuffer(extendSize)) {
+ // Failed to extend the buffer.
+ return false;
}
+ mUsedAdditionalBufferSize += size;
return true;
}
diff --git a/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h b/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h
index a2e88a46c..5e1362eee 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h
+++ b/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h
@@ -87,6 +87,8 @@ class BufferWithExtendableBuffer {
* NEAR_BUFFER_LIMIT_THRESHOLD_PERCENTILE) / 100);
}
+ bool extend(const int size);
+
/**
* For writing.
*
@@ -115,7 +117,7 @@ class BufferWithExtendableBuffer {
const size_t mMaxAdditionalBufferSize;
// Return if the buffer is successfully extended or not.
- bool extendBuffer();
+ bool extendBuffer(const size_t size);
// Returns if it is possible to write size-bytes from pos. When pos is at the tail position of
// the additional buffer, try extending the buffer.
diff --git a/native/jni/tests/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer_test.cpp b/native/jni/tests/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer_test.cpp
new file mode 100644
index 000000000..fa6c6d71e
--- /dev/null
+++ b/native/jni/tests/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer_test.cpp
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
+
+#include <gtest/gtest.h>
+
+namespace latinime {
+namespace {
+
+const int DEFAULT_MAX_BUFFER_SIZE = 1024;
+
+TEST(BufferWithExtendablebufferTest, TestWriteAndRead) {
+ BufferWithExtendableBuffer buffer(DEFAULT_MAX_BUFFER_SIZE);
+ int pos = 0;
+ // 1 byte
+ const uint32_t data_1 = 0xFF;
+ EXPECT_TRUE(buffer.writeUint(data_1, 1 /* size */, pos));
+ EXPECT_EQ(data_1, buffer.readUint(1, pos));
+ pos += 1;
+ // 2 byte
+ const uint32_t data_2 = 0xFFFF;
+ EXPECT_TRUE(buffer.writeUint(data_2, 2 /* size */, pos));
+ EXPECT_EQ(data_2, buffer.readUint(2, pos));
+ pos += 2;
+ // 3 byte
+ const uint32_t data_3 = 0xFFFFFF;
+ EXPECT_TRUE(buffer.writeUint(data_3, 3 /* size */, pos));
+ EXPECT_EQ(data_3, buffer.readUint(3, pos));
+ pos += 3;
+ // 4 byte
+ const uint32_t data_4 = 0xFFFFFFFF;
+ EXPECT_TRUE(buffer.writeUint(data_4, 4 /* size */, pos));
+ EXPECT_EQ(data_4, buffer.readUint(4, pos));
+}
+
+TEST(BufferWithExtendablebufferTest, TestExtend) {
+ BufferWithExtendableBuffer buffer(DEFAULT_MAX_BUFFER_SIZE);
+ EXPECT_EQ(0, buffer.getTailPosition());
+ EXPECT_TRUE(buffer.writeUint(0xFF /* data */, 4 /* size */, 0 /* pos */));
+ EXPECT_EQ(4, buffer.getTailPosition());
+ EXPECT_TRUE(buffer.extend(8 /* size */));
+ EXPECT_EQ(12, buffer.getTailPosition());
+ EXPECT_TRUE(buffer.writeUint(0xFFFF /* data */, 4 /* size */, 8 /* pos */));
+ EXPECT_TRUE(buffer.writeUint(0xFF /* data */, 4 /* size */, 0 /* pos */));
+}
+
+TEST(BufferWithExtendablebufferTest, TestCopy) {
+ BufferWithExtendableBuffer buffer(DEFAULT_MAX_BUFFER_SIZE);
+ EXPECT_TRUE(buffer.writeUint(0xFF /* data */, 4 /* size */, 0 /* pos */));
+ EXPECT_TRUE(buffer.writeUint(0xFFFF /* data */, 4 /* size */, 4 /* pos */));
+ BufferWithExtendableBuffer targetBuffer(DEFAULT_MAX_BUFFER_SIZE);
+ EXPECT_TRUE(targetBuffer.copy(&buffer));
+ EXPECT_EQ(0xFFu, targetBuffer.readUint(4 /* size */, 0 /* pos */));
+ EXPECT_EQ(0xFFFFu, targetBuffer.readUint(4 /* size */, 4 /* pos */));
+}
+
+TEST(BufferWithExtendablebufferTest, TestSizeLimit) {
+ BufferWithExtendableBuffer emptyBuffer(0 /* maxAdditionalBufferSize */);
+ EXPECT_FALSE(emptyBuffer.writeUint(0 /* data */, 1 /* size */, 0 /* pos */));
+ EXPECT_FALSE(emptyBuffer.extend(1 /* size */));
+
+ BufferWithExtendableBuffer smallBuffer(4 /* maxAdditionalBufferSize */);
+ EXPECT_TRUE(smallBuffer.writeUint(0 /* data */, 4 /* size */, 0 /* pos */));
+ EXPECT_FALSE(smallBuffer.writeUint(0 /* data */, 1 /* size */, 4 /* pos */));
+
+ EXPECT_TRUE(smallBuffer.copy(&emptyBuffer));
+ EXPECT_FALSE(emptyBuffer.copy(&smallBuffer));
+
+ BufferWithExtendableBuffer buffer(DEFAULT_MAX_BUFFER_SIZE);
+ EXPECT_FALSE(buffer.isNearSizeLimit());
+ int pos = 0;
+ while (!buffer.isNearSizeLimit()) {
+ EXPECT_TRUE(buffer.writeUintAndAdvancePosition(0 /* data */, 4 /* size */, &pos));
+ }
+ EXPECT_GT(pos, 0);
+ EXPECT_LE(pos, DEFAULT_MAX_BUFFER_SIZE);
+}
+
+} // namespace
+} // namespace latinime