blob: d4b3abce68a789d5723a0b5e3cb24ac84d33a034 [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 Lesinskic8f71aa2017-02-08 07:03:50 -080022#include <string>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include <type_traits>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include <vector>
25
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "android-base/logging.h"
27#include "android-base/macros.h"
28
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080029namespace aapt {
30
31/**
32 * Inspired by protobuf's ZeroCopyOutputStream, offers blocks of memory
33 * in which to write without knowing the full size of the entire payload.
34 * This is essentially a list of memory blocks. As one fills up, another
35 * block is allocated and appended to the end of the list.
36 */
37class BigBuffer {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 public:
39 /**
40 * A contiguous block of allocated memory.
41 */
42 struct Block {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043 /**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 * Pointer to the memory.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080045 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 std::unique_ptr<uint8_t[]> buffer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047
48 /**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 * Size of memory that is currently occupied. The actual
50 * allocation may be larger.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080051 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 size_t size;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 private:
55 friend class BigBuffer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056
57 /**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 * The size of the memory block allocation.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 size_t block_size_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 };
62
63 typedef std::vector<Block>::const_iterator const_iterator;
64
65 /**
66 * Create a BigBuffer with block allocation sizes
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 * of block_size.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 explicit BigBuffer(size_t block_size);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070
Chih-Hung Hsiehfc816262018-09-25 12:01:21 -070071 BigBuffer(BigBuffer&& rhs) noexcept;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072
73 /**
74 * Number of occupied bytes in all the allocated blocks.
75 */
76 size_t size() const;
77
78 /**
79 * Returns a pointer to an array of T, where T is
80 * a POD type. The elements are zero-initialized.
81 */
82 template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 T* NextBlock(size_t count = 1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084
85 /**
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 * Returns the next block available and puts the size in out_count.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 * This is useful for grabbing blocks where the size doesn't matter.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 * Use BackUp() to give back any bytes that were not used.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 void* NextBlock(size_t* out_count);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091
92 /**
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 * Backs up count bytes. This must only be called after NextBlock()
94 * and can not be larger than sizeof(T) * count of the last NextBlock()
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 * call.
96 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 void BackUp(size_t count);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098
99 /**
100 * Moves the specified BigBuffer into this one. When this method
101 * returns, buffer is empty.
102 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 void AppendBuffer(BigBuffer&& buffer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104
105 /**
106 * Pads the block with 'bytes' bytes of zero values.
107 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 void Pad(size_t bytes);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109
110 /**
111 * Pads the block so that it aligns on a 4 byte boundary.
112 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 void Align4();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 size_t block_size() const;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116
117 const_iterator begin() const;
118 const_iterator end() const;
119
Adam Lesinskic8f71aa2017-02-08 07:03:50 -0800120 std::string to_string() const;
121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 DISALLOW_COPY_AND_ASSIGN(BigBuffer);
124
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 /**
126 * Returns a pointer to a buffer of the requested size.
127 * The buffer is zero-initialized.
128 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 void* NextBlockImpl(size_t size);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 size_t block_size_;
132 size_t size_;
133 std::vector<Block> blocks_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134};
135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136inline BigBuffer::BigBuffer(size_t block_size)
137 : block_size_(block_size), size_(0) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800138
Chih-Hung Hsiehfc816262018-09-25 12:01:21 -0700139inline BigBuffer::BigBuffer(BigBuffer&& rhs) noexcept
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 : block_size_(rhs.block_size_),
141 size_(rhs.size_),
142 blocks_(std::move(rhs.blocks_)) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800143
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144inline size_t BigBuffer::size() const { return size_; }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800145
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146inline size_t BigBuffer::block_size() const { return block_size_; }
Adam Lesinski21efb682016-09-14 17:35:43 -0700147
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800148template <typename T>
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149inline T* BigBuffer::NextBlock(size_t count) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 static_assert(std::is_standard_layout<T>::value,
151 "T must be standard_layout type");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 CHECK(count != 0);
153 return reinterpret_cast<T*>(NextBlockImpl(sizeof(T) * count));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800154}
155
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156inline void BigBuffer::BackUp(size_t count) {
157 Block& block = blocks_.back();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 block.size -= count;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 size_ -= count;
Adam Lesinski21efb682016-09-14 17:35:43 -0700160}
161
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162inline void BigBuffer::AppendBuffer(BigBuffer&& buffer) {
163 std::move(buffer.blocks_.begin(), buffer.blocks_.end(),
164 std::back_inserter(blocks_));
165 size_ += buffer.size_;
166 buffer.blocks_.clear();
167 buffer.size_ = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800168}
169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170inline void BigBuffer::Pad(size_t bytes) { NextBlock<char>(bytes); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800171
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172inline void BigBuffer::Align4() {
173 const size_t unaligned = size_ % 4;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 if (unaligned != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 Pad(4 - unaligned);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800177}
178
179inline BigBuffer::const_iterator BigBuffer::begin() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 return blocks_.begin();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800181}
182
183inline BigBuffer::const_iterator BigBuffer::end() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 return blocks_.end();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800185}
186
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800188
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189#endif // AAPT_BIG_BUFFER_H