blob: b273733f6489f62ed4f470a11d2fa668a0cdfcb8 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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 Lesinskica2fc352015-04-03 12:08:26 -070020#include <cassert>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080021#include <cstring>
22#include <memory>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include <type_traits>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include <vector>
25
26namespace 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 */
34class BigBuffer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 public:
36 /**
37 * A contiguous block of allocated memory.
38 */
39 struct Block {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040 /**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 * Pointer to the memory.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 std::unique_ptr<uint8_t[]> buffer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044
45 /**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 * Size of memory that is currently occupied. The actual
47 * allocation may be larger.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 size_t size;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 private:
52 friend class BigBuffer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053
54 /**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 * The size of the memory block allocation.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056 */
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057 size_t mBlockSize;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 };
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 Lesinski6f6ceb72014-11-14 14:48:12 -0800129};
130
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131inline BigBuffer::BigBuffer(size_t blockSize)
132 : mBlockSize(blockSize), mSize(0) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800133
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134inline BigBuffer::BigBuffer(BigBuffer&& rhs)
135 : mBlockSize(rhs.mBlockSize),
136 mSize(rhs.mSize),
137 mBlocks(std::move(rhs.mBlocks)) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800138
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139inline size_t BigBuffer::size() const { return mSize; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141inline size_t BigBuffer::getBlockSize() const { return mBlockSize; }
Adam Lesinski21efb682016-09-14 17:35:43 -0700142
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800143template <typename T>
144inline T* BigBuffer::nextBlock(size_t count) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 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 Lesinski6f6ceb72014-11-14 14:48:12 -0800149}
150
Adam Lesinski21efb682016-09-14 17:35:43 -0700151inline void BigBuffer::backUp(size_t count) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 Block& block = mBlocks.back();
153 block.size -= count;
154 mSize -= count;
Adam Lesinski21efb682016-09-14 17:35:43 -0700155}
156
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800157inline void BigBuffer::appendBuffer(BigBuffer&& buffer) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 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 Lesinski6f6ceb72014-11-14 14:48:12 -0800163}
164
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165inline void BigBuffer::pad(size_t bytes) { nextBlock<char>(bytes); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800166
167inline void BigBuffer::align4() {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 const size_t unaligned = mSize % 4;
169 if (unaligned != 0) {
170 pad(4 - unaligned);
171 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800172}
173
174inline BigBuffer::const_iterator BigBuffer::begin() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 return mBlocks.begin();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800176}
177
178inline BigBuffer::const_iterator BigBuffer::end() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 return mBlocks.end();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800180}
181
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800183
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184#endif // AAPT_BIG_BUFFER_H