@@ -22,6 +22,7 @@ import (
22
22
"github.com/apache/arrow/go/arrow/internal/debug"
23
23
)
24
24
25
+ // Buffer is a wrapper type for a buffer of bytes.
25
26
type Buffer struct {
26
27
refCount int64
27
28
buf []byte
@@ -35,7 +36,7 @@ func NewBufferBytes(data []byte) *Buffer {
35
36
return & Buffer {refCount : 0 , buf : data , length : len (data )}
36
37
}
37
38
38
- // NewBuffer creates a mutable, resizable buffer with an Allocator for managing memory.
39
+ // NewResizableBuffer creates a mutable, resizable buffer with an Allocator for managing memory.
39
40
func NewResizableBuffer (mem Allocator ) * Buffer {
40
41
return & Buffer {refCount : 1 , mutable : true , mem : mem }
41
42
}
@@ -60,15 +61,28 @@ func (b *Buffer) Release() {
60
61
}
61
62
}
62
63
64
+ // Reset resets the buffer for reuse.
65
+ func (b * Buffer ) Reset (buf []byte ) {
66
+ b .buf = buf
67
+ b .length = len (buf )
68
+ }
69
+
63
70
// Buf returns the slice of memory allocated by the Buffer, which is adjusted by calling Reserve.
64
71
func (b * Buffer ) Buf () []byte { return b .buf }
65
72
66
73
// Bytes returns a slice of size Len, which is adjusted by calling Resize.
67
74
func (b * Buffer ) Bytes () []byte { return b .buf [:b .length ] }
75
+
76
+ // Mutable returns a bool indicating whether the buffer is mutable or not.
68
77
func (b * Buffer ) Mutable () bool { return b .mutable }
69
- func (b * Buffer ) Len () int { return b .length }
70
- func (b * Buffer ) Cap () int { return len (b .buf ) }
71
78
79
+ // Len returns the length of the buffer.
80
+ func (b * Buffer ) Len () int { return b .length }
81
+
82
+ // Cap returns the capacity of the buffer.
83
+ func (b * Buffer ) Cap () int { return len (b .buf ) }
84
+
85
+ // Reserve reserves the provided amount of capacity for the buffer.
72
86
func (b * Buffer ) Reserve (capacity int ) {
73
87
if capacity > len (b .buf ) {
74
88
newCap := roundUpToMultipleOf64 (capacity )
@@ -80,10 +94,13 @@ func (b *Buffer) Reserve(capacity int) {
80
94
}
81
95
}
82
96
97
+ // Resize resizes the buffer to the target size.
83
98
func (b * Buffer ) Resize (newSize int ) {
84
99
b .resize (newSize , true )
85
100
}
86
101
102
+ // ResizeNoShrink resizes the buffer to the target size, but will not
103
+ // shrink it.
87
104
func (b * Buffer ) ResizeNoShrink (newSize int ) {
88
105
b .resize (newSize , false )
89
106
}
0 commit comments