| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013, The Android Open Source Project |
| 3 * |
| 4 * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 * you may not use this file except in compliance with the License. |
| 6 * You may obtain a copy of the License at |
| 7 * |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 * |
| 10 * Unless required by applicable law or agreed to in writing, software |
| 11 * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 * See the License for the specific language governing permissions and |
| 14 * limitations under the License. |
| 15 */ |
| 16 |
| 17 #ifndef LATINIME_BUFFER_WITH_EXTENDABLE_BUFFER_H |
| 18 #define LATINIME_BUFFER_WITH_EXTENDABLE_BUFFER_H |
| 19 |
| 20 #include <cstddef> |
| 21 #include <cstdint> |
| 22 #include <vector> |
| 23 |
| 24 #include "third_party/prediction/defines.h" |
| 25 #include "third_party/prediction/suggest/policyimpl/dictionary/utils/byte_array_
utils.h" |
| 26 #include "third_party/prediction/utils/byte_array_view.h" |
| 27 |
| 28 namespace latinime { |
| 29 |
| 30 // This is used as a buffer that can be extended for updatable dictionaries. |
| 31 // To optimize performance, raw pointer is directly used for reading buffer. The |
| 32 // position has to be |
| 33 // adjusted to access additional buffer. On the other hand, this class does not |
| 34 // provide writable |
| 35 // raw pointer but provides several methods that handle boundary checking for |
| 36 // writing data. |
| 37 class BufferWithExtendableBuffer { |
| 38 public: |
| 39 static const size_t DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE; |
| 40 |
| 41 BufferWithExtendableBuffer(const ReadWriteByteArrayView originalBuffer, |
| 42 const int maxAdditionalBufferSize) |
| 43 : mOriginalBuffer(originalBuffer), |
| 44 mAdditionalBuffer(), |
| 45 mUsedAdditionalBufferSize(0), |
| 46 mMaxAdditionalBufferSize(maxAdditionalBufferSize) {} |
| 47 |
| 48 // Without original buffer. |
| 49 BufferWithExtendableBuffer(const int maxAdditionalBufferSize) |
| 50 : mOriginalBuffer(), |
| 51 mAdditionalBuffer(), |
| 52 mUsedAdditionalBufferSize(0), |
| 53 mMaxAdditionalBufferSize(maxAdditionalBufferSize) {} |
| 54 |
| 55 AK_FORCE_INLINE int getTailPosition() const { |
| 56 return mOriginalBuffer.size() + mUsedAdditionalBufferSize; |
| 57 } |
| 58 |
| 59 AK_FORCE_INLINE int getUsedAdditionalBufferSize() const { |
| 60 return mUsedAdditionalBufferSize; |
| 61 } |
| 62 |
| 63 /** |
| 64 * For reading. |
| 65 */ |
| 66 AK_FORCE_INLINE bool isInAdditionalBuffer(const int position) const { |
| 67 return position >= static_cast<int>(mOriginalBuffer.size()); |
| 68 } |
| 69 |
| 70 // TODO: Resolve the issue that the address can be changed when the vector is |
| 71 // resized. |
| 72 // CAVEAT!: Be careful about array out of bound access with buffers |
| 73 AK_FORCE_INLINE const uint8_t* getBuffer( |
| 74 const bool usesAdditionalBuffer) const { |
| 75 if (usesAdditionalBuffer) { |
| 76 return mAdditionalBuffer.data(); |
| 77 } else { |
| 78 return mOriginalBuffer.data(); |
| 79 } |
| 80 } |
| 81 |
| 82 uint32_t readUint(const int size, const int pos) const; |
| 83 |
| 84 uint32_t readUintAndAdvancePosition(const int size, int* const pos) const; |
| 85 |
| 86 void readCodePointsAndAdvancePosition(const int maxCodePointCount, |
| 87 int* const outCodePoints, |
| 88 int* outCodePointCount, |
| 89 int* const pos) const; |
| 90 |
| 91 AK_FORCE_INLINE int getOriginalBufferSize() const { |
| 92 return mOriginalBuffer.size(); |
| 93 } |
| 94 |
| 95 AK_FORCE_INLINE bool isNearSizeLimit() const { |
| 96 return mAdditionalBuffer.size() >= |
| 97 ((mMaxAdditionalBufferSize * |
| 98 NEAR_BUFFER_LIMIT_THRESHOLD_PERCENTILE) / |
| 99 100); |
| 100 } |
| 101 |
| 102 bool extend(const int size); |
| 103 |
| 104 /** |
| 105 * For writing. |
| 106 * |
| 107 * Writing is allowed for original buffer, already written region of |
| 108 *additional buffer and the |
| 109 * tail of additional buffer. |
| 110 */ |
| 111 bool writeUint(const uint32_t data, const int size, const int pos); |
| 112 |
| 113 bool writeUintAndAdvancePosition(const uint32_t data, |
| 114 const int size, |
| 115 int* const pos); |
| 116 |
| 117 bool writeCodePointsAndAdvancePosition(const int* const codePoints, |
| 118 const int codePointCount, |
| 119 const bool writesTerminator, |
| 120 int* const pos); |
| 121 |
| 122 bool copy(const BufferWithExtendableBuffer* const sourceBuffer); |
| 123 |
| 124 private: |
| 125 DISALLOW_COPY_AND_ASSIGN(BufferWithExtendableBuffer); |
| 126 |
| 127 static const int NEAR_BUFFER_LIMIT_THRESHOLD_PERCENTILE; |
| 128 static const size_t EXTEND_ADDITIONAL_BUFFER_SIZE_STEP; |
| 129 |
| 130 const ReadWriteByteArrayView mOriginalBuffer; |
| 131 std::vector<uint8_t> mAdditionalBuffer; |
| 132 int mUsedAdditionalBufferSize; |
| 133 const size_t mMaxAdditionalBufferSize; |
| 134 |
| 135 // Return if the buffer is successfully extended or not. |
| 136 bool extendBuffer(const size_t size); |
| 137 |
| 138 // Returns if it is possible to write size-bytes from pos. When pos is at the |
| 139 // tail position of |
| 140 // the additional buffer, try extending the buffer. |
| 141 bool checkAndPrepareWriting(const int pos, const int size); |
| 142 }; |
| 143 } |
| 144 #endif /* LATINIME_BUFFER_WITH_EXTENDABLE_BUFFER_H */ |
| OLD | NEW |