16f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski/*
26f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * Copyright (C) 2015 The Android Open Source Project
36f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski *
46f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
56f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * you may not use this file except in compliance with the License.
66f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * You may obtain a copy of the License at
76f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski *
86f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
96f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski *
106f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * Unless required by applicable law or agreed to in writing, software
116f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
126f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * See the License for the specific language governing permissions and
146f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * limitations under the License.
156f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski */
166f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
171ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "util/BigBuffer.h"
186f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
196f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#include <algorithm>
206f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#include <memory>
216f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#include <vector>
226f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
236f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskinamespace aapt {
246f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
256f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskivoid* BigBuffer::nextBlockImpl(size_t size) {
266f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    if (!mBlocks.empty()) {
276f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski        Block& block = mBlocks.back();
286f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski        if (block.mBlockSize - block.size >= size) {
296f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski            void* outBuffer = block.buffer.get() + block.size;
306f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski            block.size += size;
316f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski            mSize += size;
326f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski            return outBuffer;
336f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski        }
346f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    }
356f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
366f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    const size_t actualSize = std::max(mBlockSize, size);
376f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
386f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    Block block = {};
396f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
406f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    // Zero-allocate the block's buffer.
416f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    block.buffer = std::unique_ptr<uint8_t[]>(new uint8_t[actualSize]());
426f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    assert(block.buffer);
436f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
446f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    block.size = size;
456f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    block.mBlockSize = actualSize;
466f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
476f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    mBlocks.push_back(std::move(block));
486f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    mSize += size;
496f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    return mBlocks.back().buffer.get();
506f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
516f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
526f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski} // namespace aapt
53