Use ReadWriteByteArrayView in BufferWithExtendableBuffer.
Bug: 16691311 Change-Id: Ibd68354a53cebf284a886d76bd8ce9848a51346f
This commit is contained in:
parent
df11f9b8e6
commit
0257e40345
2 changed files with 22 additions and 21 deletions
|
@ -25,7 +25,7 @@ const size_t BufferWithExtendableBuffer::EXTEND_ADDITIONAL_BUFFER_SIZE_STEP = 12
|
||||||
|
|
||||||
uint32_t BufferWithExtendableBuffer::readUint(const int size, const int pos) const {
|
uint32_t BufferWithExtendableBuffer::readUint(const int size, const int pos) const {
|
||||||
const bool readingPosIsInAdditionalBuffer = isInAdditionalBuffer(pos);
|
const bool readingPosIsInAdditionalBuffer = isInAdditionalBuffer(pos);
|
||||||
const int posInBuffer = readingPosIsInAdditionalBuffer ? pos - mOriginalBufferSize : pos;
|
const int posInBuffer = readingPosIsInAdditionalBuffer ? pos - mOriginalBuffer.size() : pos;
|
||||||
return ByteArrayUtils::readUint(getBuffer(readingPosIsInAdditionalBuffer), size, posInBuffer);
|
return ByteArrayUtils::readUint(getBuffer(readingPosIsInAdditionalBuffer), size, posInBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,12 +40,12 @@ void BufferWithExtendableBuffer::readCodePointsAndAdvancePosition(const int maxC
|
||||||
int *const outCodePoints, int *outCodePointCount, int *const pos) const {
|
int *const outCodePoints, int *outCodePointCount, int *const pos) const {
|
||||||
const bool readingPosIsInAdditionalBuffer = isInAdditionalBuffer(*pos);
|
const bool readingPosIsInAdditionalBuffer = isInAdditionalBuffer(*pos);
|
||||||
if (readingPosIsInAdditionalBuffer) {
|
if (readingPosIsInAdditionalBuffer) {
|
||||||
*pos -= mOriginalBufferSize;
|
*pos -= mOriginalBuffer.size();
|
||||||
}
|
}
|
||||||
*outCodePointCount = ByteArrayUtils::readStringAndAdvancePosition(
|
*outCodePointCount = ByteArrayUtils::readStringAndAdvancePosition(
|
||||||
getBuffer(readingPosIsInAdditionalBuffer), maxCodePointCount, outCodePoints, pos);
|
getBuffer(readingPosIsInAdditionalBuffer), maxCodePointCount, outCodePoints, pos);
|
||||||
if (readingPosIsInAdditionalBuffer) {
|
if (readingPosIsInAdditionalBuffer) {
|
||||||
*pos += mOriginalBufferSize;
|
*pos += mOriginalBuffer.size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,13 +69,14 @@ bool BufferWithExtendableBuffer::writeUintAndAdvancePosition(const uint32_t data
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const bool usesAdditionalBuffer = isInAdditionalBuffer(*pos);
|
const bool usesAdditionalBuffer = isInAdditionalBuffer(*pos);
|
||||||
uint8_t *const buffer = usesAdditionalBuffer ? &mAdditionalBuffer[0] : mOriginalBuffer;
|
uint8_t *const buffer =
|
||||||
|
usesAdditionalBuffer ? mAdditionalBuffer.data() : mOriginalBuffer.data();
|
||||||
if (usesAdditionalBuffer) {
|
if (usesAdditionalBuffer) {
|
||||||
*pos -= mOriginalBufferSize;
|
*pos -= mOriginalBuffer.size();
|
||||||
}
|
}
|
||||||
ByteArrayUtils::writeUintAndAdvancePosition(buffer, data, size, pos);
|
ByteArrayUtils::writeUintAndAdvancePosition(buffer, data, size, pos);
|
||||||
if (usesAdditionalBuffer) {
|
if (usesAdditionalBuffer) {
|
||||||
*pos += mOriginalBufferSize;
|
*pos += mOriginalBuffer.size();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -88,14 +89,15 @@ bool BufferWithExtendableBuffer::writeCodePointsAndAdvancePosition(const int *co
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const bool usesAdditionalBuffer = isInAdditionalBuffer(*pos);
|
const bool usesAdditionalBuffer = isInAdditionalBuffer(*pos);
|
||||||
uint8_t *const buffer = usesAdditionalBuffer ? &mAdditionalBuffer[0] : mOriginalBuffer;
|
uint8_t *const buffer =
|
||||||
|
usesAdditionalBuffer ? mAdditionalBuffer.data() : mOriginalBuffer.data();
|
||||||
if (usesAdditionalBuffer) {
|
if (usesAdditionalBuffer) {
|
||||||
*pos -= mOriginalBufferSize;
|
*pos -= mOriginalBuffer.size();
|
||||||
}
|
}
|
||||||
ByteArrayUtils::writeCodePointsAndAdvancePosition(buffer, codePoints, codePointCount,
|
ByteArrayUtils::writeCodePointsAndAdvancePosition(buffer, codePoints, codePointCount,
|
||||||
writesTerminator, pos);
|
writesTerminator, pos);
|
||||||
if (usesAdditionalBuffer) {
|
if (usesAdditionalBuffer) {
|
||||||
*pos += mOriginalBufferSize;
|
*pos += mOriginalBuffer.size();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -119,7 +121,7 @@ bool BufferWithExtendableBuffer::checkAndPrepareWriting(const int pos, const int
|
||||||
const size_t totalRequiredSize = static_cast<size_t>(pos + size);
|
const size_t totalRequiredSize = static_cast<size_t>(pos + size);
|
||||||
if (!isInAdditionalBuffer(pos)) {
|
if (!isInAdditionalBuffer(pos)) {
|
||||||
// Here don't need to care about the additional buffer.
|
// Here don't need to care about the additional buffer.
|
||||||
if (static_cast<size_t>(mOriginalBufferSize) < totalRequiredSize) {
|
if (mOriginalBuffer.size() < totalRequiredSize) {
|
||||||
// Violate the boundary.
|
// Violate the boundary.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -137,7 +139,7 @@ bool BufferWithExtendableBuffer::checkAndPrepareWriting(const int pos, const int
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const size_t extendSize = totalRequiredSize -
|
const size_t extendSize = totalRequiredSize -
|
||||||
std::min(mAdditionalBuffer.size() + mOriginalBufferSize, totalRequiredSize);
|
std::min(mAdditionalBuffer.size() + mOriginalBuffer.size(), totalRequiredSize);
|
||||||
if (extendSize > 0 && !extendBuffer(extendSize)) {
|
if (extendSize > 0 && !extendBuffer(extendSize)) {
|
||||||
// Failed to extend the buffer.
|
// Failed to extend the buffer.
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
|
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
#include "suggest/policyimpl/dictionary/utils/byte_array_utils.h"
|
#include "suggest/policyimpl/dictionary/utils/byte_array_utils.h"
|
||||||
|
#include "utils/byte_array_view.h"
|
||||||
|
|
||||||
namespace latinime {
|
namespace latinime {
|
||||||
|
|
||||||
|
@ -36,18 +37,17 @@ class BufferWithExtendableBuffer {
|
||||||
|
|
||||||
BufferWithExtendableBuffer(uint8_t *const originalBuffer, const int originalBufferSize,
|
BufferWithExtendableBuffer(uint8_t *const originalBuffer, const int originalBufferSize,
|
||||||
const int maxAdditionalBufferSize)
|
const int maxAdditionalBufferSize)
|
||||||
: mOriginalBuffer(originalBuffer), mOriginalBufferSize(originalBufferSize),
|
: mOriginalBuffer(originalBuffer, originalBufferSize),
|
||||||
mAdditionalBuffer(0), mUsedAdditionalBufferSize(0),
|
mAdditionalBuffer(0), mUsedAdditionalBufferSize(0),
|
||||||
mMaxAdditionalBufferSize(maxAdditionalBufferSize) {}
|
mMaxAdditionalBufferSize(maxAdditionalBufferSize) {}
|
||||||
|
|
||||||
// Without original buffer.
|
// Without original buffer.
|
||||||
BufferWithExtendableBuffer(const int maxAdditionalBufferSize)
|
BufferWithExtendableBuffer(const int maxAdditionalBufferSize)
|
||||||
: mOriginalBuffer(0), mOriginalBufferSize(0),
|
: mOriginalBuffer(), mAdditionalBuffer(0), mUsedAdditionalBufferSize(0),
|
||||||
mAdditionalBuffer(0), mUsedAdditionalBufferSize(0),
|
|
||||||
mMaxAdditionalBufferSize(maxAdditionalBufferSize) {}
|
mMaxAdditionalBufferSize(maxAdditionalBufferSize) {}
|
||||||
|
|
||||||
AK_FORCE_INLINE int getTailPosition() const {
|
AK_FORCE_INLINE int getTailPosition() const {
|
||||||
return mOriginalBufferSize + mUsedAdditionalBufferSize;
|
return mOriginalBuffer.size() + mUsedAdditionalBufferSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
AK_FORCE_INLINE int getUsedAdditionalBufferSize() const {
|
AK_FORCE_INLINE int getUsedAdditionalBufferSize() const {
|
||||||
|
@ -58,16 +58,16 @@ class BufferWithExtendableBuffer {
|
||||||
* For reading.
|
* For reading.
|
||||||
*/
|
*/
|
||||||
AK_FORCE_INLINE bool isInAdditionalBuffer(const int position) const {
|
AK_FORCE_INLINE bool isInAdditionalBuffer(const int position) const {
|
||||||
return position >= mOriginalBufferSize;
|
return position >= static_cast<int>(mOriginalBuffer.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Resolve the issue that the address can be changed when the vector is resized.
|
// TODO: Resolve the issue that the address can be changed when the vector is resized.
|
||||||
// CAVEAT!: Be careful about array out of bound access with buffers
|
// CAVEAT!: Be careful about array out of bound access with buffers
|
||||||
AK_FORCE_INLINE const uint8_t *getBuffer(const bool usesAdditionalBuffer) const {
|
AK_FORCE_INLINE const uint8_t *getBuffer(const bool usesAdditionalBuffer) const {
|
||||||
if (usesAdditionalBuffer) {
|
if (usesAdditionalBuffer) {
|
||||||
return &mAdditionalBuffer[0];
|
return mAdditionalBuffer.data();
|
||||||
} else {
|
} else {
|
||||||
return mOriginalBuffer;
|
return mOriginalBuffer.data();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ class BufferWithExtendableBuffer {
|
||||||
int *const outCodePoints, int *outCodePointCount, int *const pos) const;
|
int *const outCodePoints, int *outCodePointCount, int *const pos) const;
|
||||||
|
|
||||||
AK_FORCE_INLINE int getOriginalBufferSize() const {
|
AK_FORCE_INLINE int getOriginalBufferSize() const {
|
||||||
return mOriginalBufferSize;
|
return mOriginalBuffer.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
AK_FORCE_INLINE bool isNearSizeLimit() const {
|
AK_FORCE_INLINE bool isNearSizeLimit() const {
|
||||||
|
@ -110,8 +110,7 @@ class BufferWithExtendableBuffer {
|
||||||
static const int NEAR_BUFFER_LIMIT_THRESHOLD_PERCENTILE;
|
static const int NEAR_BUFFER_LIMIT_THRESHOLD_PERCENTILE;
|
||||||
static const size_t EXTEND_ADDITIONAL_BUFFER_SIZE_STEP;
|
static const size_t EXTEND_ADDITIONAL_BUFFER_SIZE_STEP;
|
||||||
|
|
||||||
uint8_t *const mOriginalBuffer;
|
const ReadWriteByteArrayView mOriginalBuffer;
|
||||||
const int mOriginalBufferSize;
|
|
||||||
std::vector<uint8_t> mAdditionalBuffer;
|
std::vector<uint8_t> mAdditionalBuffer;
|
||||||
int mUsedAdditionalBufferSize;
|
int mUsedAdditionalBufferSize;
|
||||||
const size_t mMaxAdditionalBufferSize;
|
const size_t mMaxAdditionalBufferSize;
|
||||||
|
|
Loading…
Reference in a new issue