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