blob: d23c41d4d6f0f4ef8a43af8b7d6bfb0c44d0a5b3 [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 Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <cstring>
21#include <memory>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070022#include <type_traits>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include <vector>
24
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025#include "android-base/logging.h"
26#include "android-base/macros.h"
27
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028namespace aapt {
29
30/**
31 * Inspired by protobuf's ZeroCopyOutputStream, offers blocks of memory
32 * in which to write without knowing the full size of the entire payload.
33 * This is essentially a list of memory blocks. As one fills up, another
34 * block is allocated and appended to the end of the list.
35 */
36class BigBuffer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 public:
38 /**
39 * A contiguous block of allocated memory.
40 */
41 struct Block {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042 /**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 * Pointer to the memory.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 std::unique_ptr<uint8_t[]> buffer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046
47 /**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 * Size of memory that is currently occupied. The actual
49 * allocation may be larger.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 size_t size;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 private:
54 friend class BigBuffer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055
56 /**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 * The size of the memory block allocation.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 size_t block_size_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 };
61
62 typedef std::vector<Block>::const_iterator const_iterator;
63
64 /**
65 * Create a BigBuffer with block allocation sizes
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 * of block_size.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 explicit BigBuffer(size_t block_size);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069
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>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 T* NextBlock(size_t count = 1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083
84 /**
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 * Returns the next block available and puts the size in out_count.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 * This is useful for grabbing blocks where the size doesn't matter.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 * Use BackUp() to give back any bytes that were not used.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 void* NextBlock(size_t* out_count);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090
91 /**
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 * 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()
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 * call.
95 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 void BackUp(size_t count);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097
98 /**
99 * Moves the specified BigBuffer into this one. When this method
100 * returns, buffer is empty.
101 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 void AppendBuffer(BigBuffer&& buffer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103
104 /**
105 * Pads the block with 'bytes' bytes of zero values.
106 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 void Pad(size_t bytes);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108
109 /**
110 * Pads the block so that it aligns on a 4 byte boundary.
111 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 void Align4();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 size_t block_size() const;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115
116 const_iterator begin() const;
117 const_iterator end() const;
118
119 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 DISALLOW_COPY_AND_ASSIGN(BigBuffer);
121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 /**
123 * Returns a pointer to a buffer of the requested size.
124 * The buffer is zero-initialized.
125 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 void* NextBlockImpl(size_t size);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 size_t block_size_;
129 size_t size_;
130 std::vector<Block> blocks_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800131};
132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133inline BigBuffer::BigBuffer(size_t block_size)
134 : block_size_(block_size), size_(0) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136inline BigBuffer::BigBuffer(BigBuffer&& rhs)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 : block_size_(rhs.block_size_),
138 size_(rhs.size_),
139 blocks_(std::move(rhs.blocks_)) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141inline size_t BigBuffer::size() const { return size_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143inline size_t BigBuffer::block_size() const { return block_size_; }
Adam Lesinski21efb682016-09-14 17:35:43 -0700144
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800145template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146inline T* BigBuffer::NextBlock(size_t count) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 static_assert(std::is_standard_layout<T>::value,
148 "T must be standard_layout type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 CHECK(count != 0);
150 return reinterpret_cast<T*>(NextBlockImpl(sizeof(T) * count));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800151}
152
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153inline void BigBuffer::BackUp(size_t count) {
154 Block& block = blocks_.back();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 block.size -= count;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 size_ -= count;
Adam Lesinski21efb682016-09-14 17:35:43 -0700157}
158
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159inline void BigBuffer::AppendBuffer(BigBuffer&& buffer) {
160 std::move(buffer.blocks_.begin(), buffer.blocks_.end(),
161 std::back_inserter(blocks_));
162 size_ += buffer.size_;
163 buffer.blocks_.clear();
164 buffer.size_ = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800165}
166
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167inline void BigBuffer::Pad(size_t bytes) { NextBlock<char>(bytes); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800168
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169inline void BigBuffer::Align4() {
170 const size_t unaligned = size_ % 4;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 if (unaligned != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 Pad(4 - unaligned);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800174}
175
176inline BigBuffer::const_iterator BigBuffer::begin() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 return blocks_.begin();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800178}
179
180inline BigBuffer::const_iterator BigBuffer::end() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 return blocks_.end();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182}
183
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800185
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186#endif // AAPT_BIG_BUFFER_H