@@ -40,13 +40,15 @@ namespace arrow {
4040
4141// / \class Buffer
4242// / \brief Object containing a pointer to a piece of contiguous memory with a
43- // / particular size. Base class does not own its memory
43+ // / particular size.
4444// /
4545// / Buffers have two related notions of length: size and capacity. Size is
4646// / the number of bytes that might have valid data. Capacity is the number
47- // / of bytes that where allocated for the buffer in total.
47+ // / of bytes that were allocated for the buffer in total.
4848// /
49- // / The following invariant is always true: Size < Capacity
49+ // / The Buffer base class does not own its memory, but subclasses often do.
50+ // /
51+ // / The following invariant is always true: Size <= Capacity
5052class ARROW_EXPORT Buffer {
5153 public:
5254 // / \brief Construct from buffer and size without copying memory
@@ -158,18 +160,25 @@ class ARROW_EXPORT Buffer {
158160 // / \note Can throw std::bad_alloc if buffer is large
159161 std::string ToString () const ;
160162
161- int64_t capacity () const { return capacity_; }
163+ // / \brief Return a pointer to the buffer's data
162164 const uint8_t * data () const { return data_; }
163-
165+ // / \brief Return a writable pointer to the buffer's data
166+ // /
167+ // / The buffer has to be mutable. Otherwise, an assertion may be thrown
168+ // / or a null pointer may be returned.
164169 uint8_t * mutable_data () {
165170#ifndef NDEBUG
166171 CheckMutable ();
167172#endif
168173 return mutable_data_;
169174 }
170175
176+ // / \brief Return the buffer's size in bytes
171177 int64_t size () const { return size_; }
172178
179+ // / \brief Return the buffer's capacity (number of allocated bytes)
180+ int64_t capacity () const { return capacity_; }
181+
173182 std::shared_ptr<Buffer> parent () const { return parent_; }
174183
175184 protected:
@@ -188,26 +197,38 @@ class ARROW_EXPORT Buffer {
188197 ARROW_DISALLOW_COPY_AND_ASSIGN (Buffer);
189198};
190199
191- // / Construct a view on passed buffer at the indicated offset and length. This
192- // / function cannot fail and does not error checking (except in debug builds)
200+ // / \defgroup buffer-slicing-functions Functions for slicing buffers
201+ // /
202+ // / @{
203+
204+ // / \brief Construct a view on a buffer at the given offset and length.
205+ // /
206+ // / This function cannot fail and does not check for errors (except in debug builds)
193207static inline std::shared_ptr<Buffer> SliceBuffer (const std::shared_ptr<Buffer>& buffer,
194208 const int64_t offset,
195209 const int64_t length) {
196210 return std::make_shared<Buffer>(buffer, offset, length);
197211}
198212
213+ // / \brief Construct a view on a buffer at the given offset, up to the buffer's end.
214+ // /
215+ // / This function cannot fail and does not check for errors (except in debug builds)
199216static inline std::shared_ptr<Buffer> SliceBuffer (const std::shared_ptr<Buffer>& buffer,
200217 const int64_t offset) {
201218 int64_t length = buffer->size () - offset;
202219 return SliceBuffer (buffer, offset, length);
203220}
204221
205- // / Construct a mutable buffer slice. If the parent buffer is not mutable, this
206- // / will abort in debug builds
222+ // / \brief Like SliceBuffer, but construct a mutable buffer slice.
223+ // /
224+ // / If the parent buffer is not mutable, behavior is undefined (it may abort
225+ // / in debug builds).
207226ARROW_EXPORT
208227std::shared_ptr<Buffer> SliceMutableBuffer (const std::shared_ptr<Buffer>& buffer,
209228 const int64_t offset, const int64_t length);
210229
230+ // / @}
231+
211232// / \class MutableBuffer
212233// / \brief A Buffer whose contents can be mutated. May or may not own its data.
213234class ARROW_EXPORT MutableBuffer : public Buffer {
@@ -266,6 +287,10 @@ class ARROW_EXPORT ResizableBuffer : public MutableBuffer {
266287 ResizableBuffer (uint8_t * data, int64_t size) : MutableBuffer(data, size) {}
267288};
268289
290+ // / \defgroup buffer-allocation-functions Functions for allocating buffers
291+ // /
292+ // / @{
293+
269294// / \brief Allocate a fixed size mutable buffer from a memory pool, zero its padding.
270295// /
271296// / \param[in] pool a memory pool
@@ -364,6 +389,8 @@ Status AllocateEmptyBitmap(MemoryPool* pool, int64_t length,
364389ARROW_EXPORT
365390Status AllocateEmptyBitmap (int64_t length, std::shared_ptr<Buffer>* out);
366391
392+ // / @}
393+
367394// ----------------------------------------------------------------------
368395// Buffer builder classes
369396
@@ -374,13 +401,13 @@ class ARROW_EXPORT BufferBuilder {
374401 explicit BufferBuilder (MemoryPool* pool ARROW_MEMORY_POOL_DEFAULT )
375402 : pool_(pool), data_(NULLPTR ), capacity_(0 ), size_(0 ) {}
376403
377- // / \brief Resizes the buffer to the nearest multiple of 64 bytes
404+ // / \brief Resize the buffer to the nearest multiple of 64 bytes
378405 // /
379406 // / \param elements the new capacity of the of the builder. Will be rounded
380407 // / up to a multiple of 64 bytes for padding
381- // / \param shrink_to_fit if new capacity smaller than existing size,
408+ // / \param shrink_to_fit if new capacity is smaller than the existing size,
382409 // / reallocate internal buffer. Set to false to avoid reallocations when
383- // / shrinking the builder
410+ // / shrinking the builder.
384411 // / \return Status
385412 Status Resize (const int64_t elements, bool shrink_to_fit = true ) {
386413 // Resize(0) is a no-op
@@ -409,6 +436,9 @@ class ARROW_EXPORT BufferBuilder {
409436 // / \return Status
410437 Status Reserve (const int64_t size) { return Resize (size_ + size, false ); }
411438
439+ // / \brief Append the given data to the buffer
440+ // /
441+ // / The buffer is automatically expanded if necessary.
412442 Status Append (const void * data, int64_t length) {
413443 if (capacity_ < length + size_) {
414444 int64_t new_capacity = BitUtil::NextPower2 (length + size_);
@@ -418,6 +448,9 @@ class ARROW_EXPORT BufferBuilder {
418448 return Status::OK ();
419449 }
420450
451+ // / \brief Append the given data to the buffer
452+ // /
453+ // / The buffer is automatically expanded if necessary.
421454 template <size_t NBYTES >
422455 Status Append (const std::array<uint8_t , NBYTES >& data) {
423456 constexpr auto nbytes = static_cast <int64_t >(NBYTES );
@@ -448,6 +481,15 @@ class ARROW_EXPORT BufferBuilder {
448481 size_ += length;
449482 }
450483
484+ // / \brief Return result of builder as a Buffer object.
485+ // /
486+ // / The builder is reset and can be reused afterwards.
487+ // /
488+ // / \param[out] out the finalized Buffer object
489+ // / \param shrink_to_fit if the buffer size is smaller than its capacity,
490+ // / reallocate to fit more tightly in memory. Set to false to avoid
491+ // / a reallocation, at the expense of potentially more memory consumption.
492+ // / \return Status
451493 Status Finish (std::shared_ptr<Buffer>* out, bool shrink_to_fit = true ) {
452494 ARROW_RETURN_NOT_OK (Resize (size_, shrink_to_fit));
453495 *out = buffer_;
@@ -472,6 +514,7 @@ class ARROW_EXPORT BufferBuilder {
472514 int64_t size_;
473515};
474516
517+ // / \brief A BufferBuilder subclass with convenience methods to append typed data
475518template <typename T>
476519class ARROW_EXPORT TypedBufferBuilder : public BufferBuilder {
477520 public:
0 commit comments