From 12d58e3239302e84febddadc90971cb5a6d521c6 Mon Sep 17 00:00:00 2001 From: Richard Artoul Date: Wed, 5 Feb 2020 20:39:17 -0500 Subject: [PATCH 1/2] Add Reset method to buffer and cleanup comments --- go/arrow/memory/buffer.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/go/arrow/memory/buffer.go b/go/arrow/memory/buffer.go index 234f5d4337f..006e45577b1 100644 --- a/go/arrow/memory/buffer.go +++ b/go/arrow/memory/buffer.go @@ -22,6 +22,7 @@ import ( "github.com/apache/arrow/go/arrow/internal/debug" ) +// Buffer is a wrapper type for a buffer of bytes. type Buffer struct { refCount int64 buf []byte @@ -35,7 +36,7 @@ func NewBufferBytes(data []byte) *Buffer { return &Buffer{refCount: 0, buf: data, length: len(data)} } -// NewBuffer creates a mutable, resizable buffer with an Allocator for managing memory. +// NewResizableBuffer creates a mutable, resizable buffer with an Allocator for managing memory. func NewResizableBuffer(mem Allocator) *Buffer { return &Buffer{refCount: 1, mutable: true, mem: mem} } @@ -60,15 +61,29 @@ func (b *Buffer) Release() { } } +// Reset resets the buffer for reuse. +func (b *Buffer) Reset(buf []byte) { + b.refCount = 0 + b.buf = buf + b.length = len(buf) +} + // Buf returns the slice of memory allocated by the Buffer, which is adjusted by calling Reserve. func (b *Buffer) Buf() []byte { return b.buf } // Bytes returns a slice of size Len, which is adjusted by calling Resize. func (b *Buffer) Bytes() []byte { return b.buf[:b.length] } + +// Mutable returns a bool indicating whether the buffer is mutable or not. func (b *Buffer) Mutable() bool { return b.mutable } -func (b *Buffer) Len() int { return b.length } -func (b *Buffer) Cap() int { return len(b.buf) } +// Len returns the length of the buffer. +func (b *Buffer) Len() int { return b.length } + +// Cap returns the capacity of the buffer. +func (b *Buffer) Cap() int { return len(b.buf) } + +// Reserve reserves the provided amount of capacity for the buffer. func (b *Buffer) Reserve(capacity int) { if capacity > len(b.buf) { newCap := roundUpToMultipleOf64(capacity) @@ -80,10 +95,13 @@ func (b *Buffer) Reserve(capacity int) { } } +// Resize resizes the buffer to the target size. func (b *Buffer) Resize(newSize int) { b.resize(newSize, true) } +// ResizeNoShrink resizes the buffer to the target size, but will not +// shrink it. func (b *Buffer) ResizeNoShrink(newSize int) { b.resize(newSize, false) } From fb575241b4130c068c1153613cacd8c9c508077a Mon Sep 17 00:00:00 2001 From: Richard Artoul Date: Wed, 5 Feb 2020 20:43:35 -0500 Subject: [PATCH 2/2] Dont reset refcount --- go/arrow/memory/buffer.go | 1 - 1 file changed, 1 deletion(-) diff --git a/go/arrow/memory/buffer.go b/go/arrow/memory/buffer.go index 006e45577b1..57c0db487d1 100644 --- a/go/arrow/memory/buffer.go +++ b/go/arrow/memory/buffer.go @@ -63,7 +63,6 @@ func (b *Buffer) Release() { // Reset resets the buffer for reuse. func (b *Buffer) Reset(buf []byte) { - b.refCount = 0 b.buf = buf b.length = len(buf) }