|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#ifndef ORC_BLOCK_BUFFER_HH |
| 20 | +#define ORC_BLOCK_BUFFER_HH |
| 21 | + |
| 22 | +#include "orc/MemoryPool.hh" |
| 23 | + |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +namespace orc { |
| 27 | + |
| 28 | + /** |
| 29 | + * BlockBuffer implements a memory allocation policy based on |
| 30 | + * equal-length blocks. BlockBuffer will reserve multiple blocks |
| 31 | + * for allocation. |
| 32 | + */ |
| 33 | + class BlockBuffer { |
| 34 | + private: |
| 35 | + MemoryPool& memoryPool; |
| 36 | + // current buffer size |
| 37 | + uint64_t currentSize; |
| 38 | + // maximal capacity (actual allocated memory) |
| 39 | + uint64_t currentCapacity; |
| 40 | + // unit for buffer expansion |
| 41 | + const uint64_t blockSize; |
| 42 | + // pointers to the start of each block |
| 43 | + std::vector<char*> blocks; |
| 44 | + |
| 45 | + // non-copy-constructible |
| 46 | + BlockBuffer(BlockBuffer& buffer) = delete; |
| 47 | + BlockBuffer& operator=(BlockBuffer& buffer) = delete; |
| 48 | + BlockBuffer(BlockBuffer&& buffer) = delete; |
| 49 | + BlockBuffer& operator=(BlockBuffer&& buffer) = delete; |
| 50 | + |
| 51 | + public: |
| 52 | + BlockBuffer(MemoryPool& pool, uint64_t blockSize); |
| 53 | + |
| 54 | + ~BlockBuffer(); |
| 55 | + |
| 56 | + /** |
| 57 | + * Block points to a section of memory allocated by BlockBuffer, |
| 58 | + * containing the corresponding physical memory address and available size. |
| 59 | + */ |
| 60 | + struct Block { |
| 61 | + // the start of block |
| 62 | + char* data; |
| 63 | + // number of bytes available at data |
| 64 | + uint64_t size; |
| 65 | + |
| 66 | + Block() : data(nullptr), size(0) {} |
| 67 | + Block(char* _data, uint64_t _size) : data(_data), size(_size) {} |
| 68 | + Block(const Block& block) = default; |
| 69 | + ~Block() = default; |
| 70 | + }; |
| 71 | + |
| 72 | + /** |
| 73 | + * Get the allocated block object. |
| 74 | + * The last allocated block size may be less than blockSize, |
| 75 | + * and the rest of the blocks are all of size blockSize. |
| 76 | + * @param blockIndex the index of blocks |
| 77 | + * @return the allocated block object |
| 78 | + */ |
| 79 | + Block getBlock(uint64_t blockIndex) const; |
| 80 | + |
| 81 | + /** |
| 82 | + * Get a empty block or allocate a new block to write. |
| 83 | + * If the last allocated block size is less than blockSize, |
| 84 | + * the size of empty block is equal to blockSize minus the size of |
| 85 | + * the last allocated block size. Otherwise, the size of |
| 86 | + * the empty block is equal to blockSize. |
| 87 | + * @return a empty block object |
| 88 | + */ |
| 89 | + Block getNextBlock(); |
| 90 | + |
| 91 | + /** |
| 92 | + * Get the number of blocks that are fully or partially occupied |
| 93 | + */ |
| 94 | + uint64_t getBlockNumber() const { |
| 95 | + return (currentSize + blockSize - 1) / blockSize; |
| 96 | + } |
| 97 | + |
| 98 | + uint64_t size() const { |
| 99 | + return currentSize; |
| 100 | + } |
| 101 | + |
| 102 | + uint64_t capacity() const { |
| 103 | + return currentCapacity; |
| 104 | + } |
| 105 | + |
| 106 | + void resize(uint64_t size); |
| 107 | + /** |
| 108 | + * Requests the BlockBuffer to contain at least newCapacity bytes. |
| 109 | + * Reallocation happens if there is need of more space. |
| 110 | + * @param newCapacity new capacity of BlockBuffer |
| 111 | + */ |
| 112 | + void reserve(uint64_t newCapacity); |
| 113 | + }; |
| 114 | +} // namespace orc |
| 115 | + |
| 116 | +#endif |
0 commit comments