blob: 43b56c32fb790a567c7f8f4079cfc2b7fb36eb8a [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
Jeremy Meyer56f36e82022-05-20 20:35:42 +000017#include <androidfw/BigBuffer.h>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018
19#include <algorithm>
Yurii Zubrytskyi707263e2024-08-09 10:47:08 -070020#include <iterator>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080021#include <memory>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022
Adam Lesinskice5e56e22016-10-21 17:56:45 -070023#include "android-base/logging.h"
24
Jeremy Meyer56f36e82022-05-20 20:35:42 +000025namespace android {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026
Adam Lesinskice5e56e22016-10-21 17:56:45 -070027void* BigBuffer::NextBlockImpl(size_t size) {
28 if (!blocks_.empty()) {
29 Block& block = blocks_.back();
30 if (block.block_size_ - block.size >= size) {
31 void* out_buffer = block.buffer.get() + block.size;
32 block.size += size;
33 size_ += size;
34 return out_buffer;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -070036 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037
Adam Lesinskice5e56e22016-10-21 17:56:45 -070038 const size_t actual_size = std::max(block_size_, size);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039
Adam Lesinskice5e56e22016-10-21 17:56:45 -070040 Block block = {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041
Adam Lesinskice5e56e22016-10-21 17:56:45 -070042 // Zero-allocate the block's buffer.
43 block.buffer = std::unique_ptr<uint8_t[]>(new uint8_t[actual_size]());
44 CHECK(block.buffer);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080045
Adam Lesinskice5e56e22016-10-21 17:56:45 -070046 block.size = size;
47 block.block_size_ = actual_size;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048
Adam Lesinskice5e56e22016-10-21 17:56:45 -070049 blocks_.push_back(std::move(block));
50 size_ += size;
51 return blocks_.back().buffer.get();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052}
53
Adam Lesinskice5e56e22016-10-21 17:56:45 -070054void* BigBuffer::NextBlock(size_t* out_size) {
55 if (!blocks_.empty()) {
56 Block& block = blocks_.back();
57 if (block.size != block.block_size_) {
58 void* out_buffer = block.buffer.get() + block.size;
59 size_t size = block.block_size_ - block.size;
60 block.size = block.block_size_;
61 size_ += size;
62 *out_size = size;
63 return out_buffer;
Adam Lesinski21efb682016-09-14 17:35:43 -070064 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -070065 }
Adam Lesinski21efb682016-09-14 17:35:43 -070066
Adam Lesinskice5e56e22016-10-21 17:56:45 -070067 // Zero-allocate the block's buffer.
68 Block block = {};
69 block.buffer = std::unique_ptr<uint8_t[]>(new uint8_t[block_size_]());
70 CHECK(block.buffer);
71 block.size = block_size_;
72 block.block_size_ = block_size_;
73 blocks_.push_back(std::move(block));
74 size_ += block_size_;
75 *out_size = block_size_;
76 return blocks_.back().buffer.get();
Adam Lesinski21efb682016-09-14 17:35:43 -070077}
78
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080079std::string BigBuffer::to_string() const {
80 std::string result;
Yurii Zubrytskyi707263e2024-08-09 10:47:08 -070081 result.reserve(size_);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080082 for (const Block& block : blocks_) {
83 result.append(block.buffer.get(), block.buffer.get() + block.size);
84 }
85 return result;
86}
87
Yurii Zubrytskyi707263e2024-08-09 10:47:08 -070088void BigBuffer::AppendBuffer(BigBuffer&& buffer) {
89 std::move(buffer.blocks_.begin(), buffer.blocks_.end(), std::back_inserter(blocks_));
90 size_ += buffer.size_;
91 buffer.blocks_.clear();
92 buffer.size_ = 0;
93}
94
95void BigBuffer::BackUp(size_t count) {
96 Block& block = blocks_.back();
97 block.size -= count;
98 size_ -= count;
99 // BigBuffer is supposed to always give zeroed memory, but backing up usually means
100 // something has been already written into the block. Erase it.
101 std::fill_n(block.buffer.get() + block.size, count, 0);
102}
103
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000104} // namespace android