Debian Codegraph @

tools/aapt2/util/BigBuffer.h

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