blob: 025142b5bfbadc7fe103c4681d2ebb441c28af3c [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
20#include <cstring>
21#include <memory>
22#include <vector>
23
24namespace aapt {
25
26/**
27 * Inspired by protobuf's ZeroCopyOutputStream, offers blocks of memory
28 * in which to write without knowing the full size of the entire payload.
29 * This is essentially a list of memory blocks. As one fills up, another
30 * block is allocated and appended to the end of the list.
31 */
32class BigBuffer {
33public:
34 /**
35 * A contiguous block of allocated memory.
36 */
37 struct Block {
38 /**
39 * Pointer to the memory.
40 */
41 std::unique_ptr<uint8_t[]> buffer;
42
43 /**
44 * Size of memory that is currently occupied. The actual
45 * allocation may be larger.
46 */
47 size_t size;
48
49 private:
50 friend class BigBuffer;
51
52 /**
53 * The size of the memory block allocation.
54 */
55 size_t mBlockSize;
56 };
57
58 typedef std::vector<Block>::const_iterator const_iterator;
59
60 /**
61 * Create a BigBuffer with block allocation sizes
62 * of blockSize.
63 */
64 BigBuffer(size_t blockSize);
65
66 BigBuffer(const BigBuffer&) = delete; // No copying.
67
68 BigBuffer(BigBuffer&& rhs);
69
70 /**
71 * Number of occupied bytes in all the allocated blocks.
72 */
73 size_t size() const;
74
75 /**
76 * Returns a pointer to an array of T, where T is
77 * a POD type. The elements are zero-initialized.
78 */
79 template <typename T>
80 T* nextBlock(size_t count = 1);
81
82 /**
83 * Moves the specified BigBuffer into this one. When this method
84 * returns, buffer is empty.
85 */
86 void appendBuffer(BigBuffer&& buffer);
87
88 /**
89 * Pads the block with 'bytes' bytes of zero values.
90 */
91 void pad(size_t bytes);
92
93 /**
94 * Pads the block so that it aligns on a 4 byte boundary.
95 */
96 void align4();
97
98 const_iterator begin() const;
99 const_iterator end() const;
100
101private:
102 /**
103 * Returns a pointer to a buffer of the requested size.
104 * The buffer is zero-initialized.
105 */
106 void* nextBlockImpl(size_t size);
107
108 size_t mBlockSize;
109 size_t mSize;
110 std::vector<Block> mBlocks;
111};
112
113inline BigBuffer::BigBuffer(size_t blockSize) : mBlockSize(blockSize), mSize(0) {
114}
115
116inline BigBuffer::BigBuffer(BigBuffer&& rhs) :
117 mBlockSize(rhs.mBlockSize), mSize(rhs.mSize), mBlocks(std::move(rhs.mBlocks)) {
118}
119
120inline size_t BigBuffer::size() const {
121 return mSize;
122}
123
124template <typename T>
125inline T* BigBuffer::nextBlock(size_t count) {
126 assert(count != 0);
127 return reinterpret_cast<T*>(nextBlockImpl(sizeof(T) * count));
128}
129
130inline void BigBuffer::appendBuffer(BigBuffer&& buffer) {
131 std::move(buffer.mBlocks.begin(), buffer.mBlocks.end(), std::back_inserter(mBlocks));
132 mSize += buffer.mSize;
133 buffer.mBlocks.clear();
134 buffer.mSize = 0;
135}
136
137inline void BigBuffer::pad(size_t bytes) {
138 nextBlock<char>(bytes);
139}
140
141inline void BigBuffer::align4() {
142 const size_t unaligned = mSize % 4;
143 if (unaligned != 0) {
144 pad(4 - unaligned);
145 }
146}
147
148inline BigBuffer::const_iterator BigBuffer::begin() const {
149 return mBlocks.begin();
150}
151
152inline BigBuffer::const_iterator BigBuffer::end() const {
153 return mBlocks.end();
154}
155
156} // namespace aapt
157
158#endif // AAPT_BIG_BUFFER_H