Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 AAPT_BIG_BUFFER_H |
| 18 | #define AAPT_BIG_BUFFER_H |
| 19 | |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 20 | #include <cassert> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 21 | #include <cstring> |
| 22 | #include <memory> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 23 | #include <type_traits> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
| 26 | namespace aapt { |
| 27 | |
| 28 | /** |
| 29 | * Inspired by protobuf's ZeroCopyOutputStream, offers blocks of memory |
| 30 | * in which to write without knowing the full size of the entire payload. |
| 31 | * This is essentially a list of memory blocks. As one fills up, another |
| 32 | * block is allocated and appended to the end of the list. |
| 33 | */ |
| 34 | class BigBuffer { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 35 | public: |
| 36 | /** |
| 37 | * A contiguous block of allocated memory. |
| 38 | */ |
| 39 | struct Block { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 40 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 41 | * Pointer to the memory. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 42 | */ |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 43 | std::unique_ptr<uint8_t[]> buffer; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 46 | * Size of memory that is currently occupied. The actual |
| 47 | * allocation may be larger. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 48 | */ |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 49 | size_t size; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 50 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 51 | private: |
| 52 | friend class BigBuffer; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 55 | * The size of the memory block allocation. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 56 | */ |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 57 | size_t mBlockSize; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 58 | }; |
| 59 | |
| 60 | typedef std::vector<Block>::const_iterator const_iterator; |
| 61 | |
| 62 | /** |
| 63 | * Create a BigBuffer with block allocation sizes |
| 64 | * of blockSize. |
| 65 | */ |
| 66 | explicit BigBuffer(size_t blockSize); |
| 67 | |
| 68 | BigBuffer(const BigBuffer&) = delete; // No copying. |
| 69 | |
| 70 | BigBuffer(BigBuffer&& rhs); |
| 71 | |
| 72 | /** |
| 73 | * Number of occupied bytes in all the allocated blocks. |
| 74 | */ |
| 75 | size_t size() const; |
| 76 | |
| 77 | /** |
| 78 | * Returns a pointer to an array of T, where T is |
| 79 | * a POD type. The elements are zero-initialized. |
| 80 | */ |
| 81 | template <typename T> |
| 82 | T* nextBlock(size_t count = 1); |
| 83 | |
| 84 | /** |
| 85 | * Returns the next block available and puts the size in outCount. |
| 86 | * This is useful for grabbing blocks where the size doesn't matter. |
| 87 | * Use backUp() to give back any bytes that were not used. |
| 88 | */ |
| 89 | void* nextBlock(size_t* outCount); |
| 90 | |
| 91 | /** |
| 92 | * Backs up count bytes. This must only be called after nextBlock() |
| 93 | * and can not be larger than sizeof(T) * count of the last nextBlock() |
| 94 | * call. |
| 95 | */ |
| 96 | void backUp(size_t count); |
| 97 | |
| 98 | /** |
| 99 | * Moves the specified BigBuffer into this one. When this method |
| 100 | * returns, buffer is empty. |
| 101 | */ |
| 102 | void appendBuffer(BigBuffer&& buffer); |
| 103 | |
| 104 | /** |
| 105 | * Pads the block with 'bytes' bytes of zero values. |
| 106 | */ |
| 107 | void pad(size_t bytes); |
| 108 | |
| 109 | /** |
| 110 | * Pads the block so that it aligns on a 4 byte boundary. |
| 111 | */ |
| 112 | void align4(); |
| 113 | |
| 114 | size_t getBlockSize() const; |
| 115 | |
| 116 | const_iterator begin() const; |
| 117 | const_iterator end() const; |
| 118 | |
| 119 | private: |
| 120 | /** |
| 121 | * Returns a pointer to a buffer of the requested size. |
| 122 | * The buffer is zero-initialized. |
| 123 | */ |
| 124 | void* nextBlockImpl(size_t size); |
| 125 | |
| 126 | size_t mBlockSize; |
| 127 | size_t mSize; |
| 128 | std::vector<Block> mBlocks; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 129 | }; |
| 130 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 131 | inline BigBuffer::BigBuffer(size_t blockSize) |
| 132 | : mBlockSize(blockSize), mSize(0) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 133 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 134 | inline BigBuffer::BigBuffer(BigBuffer&& rhs) |
| 135 | : mBlockSize(rhs.mBlockSize), |
| 136 | mSize(rhs.mSize), |
| 137 | mBlocks(std::move(rhs.mBlocks)) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 138 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 139 | inline size_t BigBuffer::size() const { return mSize; } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 140 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 141 | inline size_t BigBuffer::getBlockSize() const { return mBlockSize; } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 142 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 143 | template <typename T> |
| 144 | inline T* BigBuffer::nextBlock(size_t count) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 145 | static_assert(std::is_standard_layout<T>::value, |
| 146 | "T must be standard_layout type"); |
| 147 | assert(count != 0); |
| 148 | return reinterpret_cast<T*>(nextBlockImpl(sizeof(T) * count)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 151 | inline void BigBuffer::backUp(size_t count) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 152 | Block& block = mBlocks.back(); |
| 153 | block.size -= count; |
| 154 | mSize -= count; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 157 | inline void BigBuffer::appendBuffer(BigBuffer&& buffer) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 158 | std::move(buffer.mBlocks.begin(), buffer.mBlocks.end(), |
| 159 | std::back_inserter(mBlocks)); |
| 160 | mSize += buffer.mSize; |
| 161 | buffer.mBlocks.clear(); |
| 162 | buffer.mSize = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 165 | inline void BigBuffer::pad(size_t bytes) { nextBlock<char>(bytes); } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 166 | |
| 167 | inline void BigBuffer::align4() { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 168 | const size_t unaligned = mSize % 4; |
| 169 | if (unaligned != 0) { |
| 170 | pad(4 - unaligned); |
| 171 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | inline BigBuffer::const_iterator BigBuffer::begin() const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 175 | return mBlocks.begin(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | inline BigBuffer::const_iterator BigBuffer::end() const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 179 | return mBlocks.end(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 182 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 183 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 184 | #endif // AAPT_BIG_BUFFER_H |