25
25
#include < string>
26
26
#include < type_traits>
27
27
28
+ #include " arrow/memory_pool.h"
28
29
#include " arrow/status.h"
29
30
#include " arrow/util/bit-util.h"
30
31
#include " arrow/util/macros.h"
31
32
#include " arrow/util/visibility.h"
32
33
33
34
namespace arrow {
34
35
35
- class MemoryPool ;
36
-
37
36
// ----------------------------------------------------------------------
38
37
// Buffer classes
39
38
40
- // / Immutable API for a chunk of bytes which may or may not be owned by the
41
- // / class instance.
39
+ // / \class Buffer
40
+ // / \brief Object containing a pointer to a piece of contiguous memory with a
41
+ // / particular size. Base class does not own its memory
42
42
// /
43
43
// / Buffers have two related notions of length: size and capacity. Size is
44
44
// / the number of bytes that might have valid data. Capacity is the number
@@ -133,7 +133,8 @@ ARROW_EXPORT
133
133
std::shared_ptr<Buffer> SliceMutableBuffer (const std::shared_ptr<Buffer>& buffer,
134
134
const int64_t offset, const int64_t length);
135
135
136
- // / A Buffer whose contents can be mutated. May or may not own its data.
136
+ // / \class MutableBuffer
137
+ // / \brief A Buffer whose contents can be mutated. May or may not own its data.
137
138
class ARROW_EXPORT MutableBuffer : public Buffer {
138
139
public:
139
140
MutableBuffer (uint8_t * data, const int64_t size) : Buffer(data, size) {
@@ -148,6 +149,8 @@ class ARROW_EXPORT MutableBuffer : public Buffer {
148
149
MutableBuffer () : Buffer(NULLPTR, 0 ) {}
149
150
};
150
151
152
+ // / \class ResizableBuffer
153
+ // / \brief A mutable buffer that can be resized
151
154
class ARROW_EXPORT ResizableBuffer : public MutableBuffer {
152
155
public:
153
156
// / Change buffer reported size to indicated size, allocating memory if
@@ -190,13 +193,22 @@ class ARROW_EXPORT PoolBuffer : public ResizableBuffer {
190
193
MemoryPool* pool_;
191
194
};
192
195
196
+ // / \class BufferBuilder
197
+ // / \brief A class for incrementally building a contiguous chunk of in-memory data
193
198
class ARROW_EXPORT BufferBuilder {
194
199
public:
195
- explicit BufferBuilder (MemoryPool* pool)
200
+ explicit BufferBuilder (MemoryPool* pool ARROW_MEMORY_POOL_DEFAULT )
196
201
: pool_(pool), data_(NULLPTR), capacity_(0 ), size_(0 ) {}
197
202
198
- // / Resizes the buffer to the nearest multiple of 64 bytes per Layout.md
199
- Status Resize (const int64_t elements) {
203
+ // / \brief Resizes the buffer to the nearest multiple of 64 bytes
204
+ // /
205
+ // / \param elements the new capacity of the of the builder. Will be rounded
206
+ // / up to a multiple of 64 bytes for padding
207
+ // / \param shrink_to_fit if new capacity smaller than existing size,
208
+ // / reallocate internal buffer. Set to false to avoid reallocations when
209
+ // / shrinking the builder
210
+ // / \return Status
211
+ Status Resize (const int64_t elements, bool shrink_to_fit = true ) {
200
212
// Resize(0) is a no-op
201
213
if (elements == 0 ) {
202
214
return Status::OK ();
@@ -205,7 +217,7 @@ class ARROW_EXPORT BufferBuilder {
205
217
buffer_ = std::make_shared<PoolBuffer>(pool_);
206
218
}
207
219
int64_t old_capacity = capacity_;
208
- RETURN_NOT_OK (buffer_->Resize (elements));
220
+ RETURN_NOT_OK (buffer_->Resize (elements, shrink_to_fit ));
209
221
capacity_ = buffer_->capacity ();
210
222
data_ = buffer_->mutable_data ();
211
223
if (capacity_ > old_capacity) {
@@ -214,7 +226,14 @@ class ARROW_EXPORT BufferBuilder {
214
226
return Status::OK ();
215
227
}
216
228
217
- Status Append (const uint8_t * data, int64_t length) {
229
+ // / \brief Ensure that builder can accommodate the additional number of bytes
230
+ // / without the need to perform allocations
231
+ // /
232
+ // / \param size number of additional bytes to make space for
233
+ // / \return Status
234
+ Status Reserve (const int64_t size) { return Resize (size_ + size, false ); }
235
+
236
+ Status Append (const void * data, int64_t length) {
218
237
if (capacity_ < length + size_) {
219
238
int64_t new_capacity = BitUtil::NextPower2 (length + size_);
220
239
RETURN_NOT_OK (Resize (new_capacity));
@@ -248,7 +267,7 @@ class ARROW_EXPORT BufferBuilder {
248
267
}
249
268
250
269
// Unsafe methods don't check existing size
251
- void UnsafeAppend (const uint8_t * data, int64_t length) {
270
+ void UnsafeAppend (const void * data, int64_t length) {
252
271
memcpy (data_ + size_, data, static_cast <size_t >(length));
253
272
size_ += length;
254
273
}
0 commit comments