1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
22 "github.com/apache/arrow/go/v6/arrow/bitutil"
23 "github.com/apache/arrow/go/v6/arrow/internal/debug"
24 "github.com/apache/arrow/go/v6/arrow/memory"
27 // A bufferBuilder provides common functionality for populating memory with a sequence of type-specific values.
28 // Specialized implementations provide type-safe APIs for appending and accessing the memory.
29 type bufferBuilder struct {
39 // Retain increases the reference count by 1.
40 // Retain may be called simultaneously from multiple goroutines.
41 func (b *bufferBuilder) Retain() {
42 atomic.AddInt64(&b.refCount, 1)
45 // Release decreases the reference count by 1.
46 // When the reference count goes to zero, the memory is freed.
47 // Release may be called simultaneously from multiple goroutines.
48 func (b *bufferBuilder) Release() {
49 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases")
51 if atomic.AddInt64(&b.refCount, -1) == 0 {
54 b.buffer, b.bytes = nil, nil
59 // Len returns the length of the memory buffer in bytes.
60 func (b *bufferBuilder) Len() int { return b.length }
62 // Cap returns the total number of bytes that can be stored without allocating additional memory.
63 func (b *bufferBuilder) Cap() int { return b.capacity }
65 // Bytes returns a slice of length b.Len().
66 // The slice is only valid for use until the next buffer modification. That is, until the next call
67 // to Advance, Reset, Finish or any Append function. The slice aliases the buffer content at least until the next
68 // buffer modification.
69 func (b *bufferBuilder) Bytes() []byte { return b.bytes[:b.length] }
71 func (b *bufferBuilder) resize(elements int) {
73 b.buffer = memory.NewResizableBuffer(b.mem)
76 b.buffer.Resize(elements)
77 oldCapacity := b.capacity
78 b.capacity = b.buffer.Cap()
79 b.bytes = b.buffer.Buf()
81 if b.capacity > oldCapacity {
82 memory.Set(b.bytes[oldCapacity:], 0)
86 // Advance increases the buffer by length and initializes the skipped bytes to zero.
87 func (b *bufferBuilder) Advance(length int) {
88 if b.capacity < b.length+length {
89 newCapacity := bitutil.NextPowerOf2(b.length + length)
95 // Append appends the contents of v to the buffer, resizing it if necessary.
96 func (b *bufferBuilder) Append(v []byte) {
97 if b.capacity < b.length+len(v) {
98 newCapacity := bitutil.NextPowerOf2(b.length + len(v))
104 // Reset returns the buffer to an empty state. Reset releases the memory and sets the length and capacity to zero.
105 func (b *bufferBuilder) Reset() {
109 b.buffer, b.bytes = nil, nil
110 b.capacity, b.length = 0, 0
114 func (b *bufferBuilder) Finish() (buffer *memory.Buffer) {
116 b.buffer.ResizeNoShrink(b.length)
124 func (b *bufferBuilder) unsafeAppend(data []byte) {
125 copy(b.bytes[b.length:], data)
126 b.length += len(data)