memory

package
v18.9.0-rc0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: Apache-2.0, BSD-3-Clause Imports: 10 Imported by: 251 modules, 314 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package memory provides support for allocating and manipulating memory at a low level.

The build tag 'mallocator' will switch the default allocator to one backed by libc malloc. This also requires CGO.

Alignment

GoAllocator (the DefaultAllocator unless the 'mallocator' build tag is set) returns buffers whose start is rounded up to a multiple of 64 bytes, and the mallocator package's github.com/apache/arrow-go/v18/arrow/memory/mallocator.NewMallocator does the same for libc memory. Neither promises more than 64. A consumer that needs a larger alignment (a page-aligned buffer for a GPU runtime, mmap or DMA, for example) should allocate with github.com/apache/arrow-go/v18/arrow/memory/mallocator.NewMallocatorWithAlignment, which takes any power of two.

Sharing buffers with C

Memory from GoAllocator lives on the Go heap. Under the cgo pointer rules (https://pkg.go.dev/cmd/cgo#hdr-Passing_pointers) C code may use such a pointer only for the duration of the cgo call it was passed to; it must not keep it, because the garbage collector may move or free the memory. Buffers that C will hold on to, such as arrays exported over the C Data Interface with the cdata package and retained by the consumer, should therefore be allocated with an allocator that returns C memory: github.com/apache/arrow-go/v18/arrow/memory/mallocator.Mallocator (libc malloc, no C++ dependency, requires cgo) or, when the Arrow C++ library is linked, the CgoArrowAllocator behind the 'ccalloc' build tag.

Example (MemoryPerf)
package main

import (
	"fmt"

	"github.com/apache/arrow-go/v18/arrow/array"
	"github.com/apache/arrow-go/v18/arrow/memory"
)

func main() {
	// Create a memory allocator
	pool := memory.NewGoAllocator()

	// Create a large Arrow array
	fmt.Println("Creating a large Arrow Int64 array...")
	const N = 10_000_000
	builder := array.NewInt64Builder(pool)
	defer builder.Release()

	for i := 0; i < N; i++ {
		builder.Append(int64(i))
	}
	arr := builder.NewInt64Array()
	defer arr.Release()

	fmt.Printf("Built array of %d elements\n", N)

	// Batch processing
	fmt.Println("\nBatch processing large data in chunks...")
	batchSize := 1_000_000
	for batch := 0; batch < N; batch += batchSize {
		end := batch + batchSize
		if end > N {
			end = N
		}
		b := array.NewInt64Builder(pool)
		for i := batch; i < end; i++ {
			b.Append(int64(i))
		}
		chunk := b.NewInt64Array()
		// Simulate processing
		_ = chunk.Value(0)
		chunk.Release()
		b.Release()
	}
	fmt.Printf("Processed %d elements in batches of %d\n", N, batchSize)

}
Output:
Creating a large Arrow Int64 array...
Built array of 10000000 elements

Batch processing large data in chunks...
Processed 10000000 elements in batches of 1000000

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReleaseBuffers

func ReleaseBuffers(buffers []*Buffer)

func Set

func Set(buf []byte, c byte)

Set assigns the value c to every element of the slice buf.

Types

type Allocator

type Allocator interface {
	Allocate(size int) []byte
	Reallocate(size int, b []byte) []byte
	Free(b []byte)
}
var DefaultAllocator Allocator = NewGoAllocator()

DefaultAllocator is a default implementation of Allocator and can be used anywhere an Allocator is required.

DefaultAllocator is safe to use from multiple goroutines.

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer is a wrapper type for a buffer of bytes.

func NewBufferBytes

func NewBufferBytes(data []byte) *Buffer

NewBufferBytes creates a fixed-size buffer from the specified data.

func NewBufferWithAllocator

func NewBufferWithAllocator(data []byte, mem Allocator) *Buffer

NewBufferWithAllocator returns a buffer with the mutable flag set as false. The intention here is to allow wrapping a byte slice along with an allocator as a buffer to track the lifetime via refcounts in order to call Free when the refcount goes to zero.

The primary example this is used for, is currently importing data through the c data interface and tracking the lifetime of the imported buffers.

func NewResizableBuffer

func NewResizableBuffer(mem Allocator) *Buffer

NewResizableBuffer creates a mutable, resizable buffer with an Allocator for managing memory.

func SliceBuffer

func SliceBuffer(buf *Buffer, offset, length int) *Buffer

func (*Buffer) Buf

func (b *Buffer) Buf() []byte

Buf returns the slice of memory allocated by the Buffer, which is adjusted by calling Reserve.

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes returns a slice of size Len, which is adjusted by calling Resize.

func (*Buffer) Cap

func (b *Buffer) Cap() int

Cap returns the capacity of the buffer.

func (*Buffer) Len

func (b *Buffer) Len() int

Len returns the length of the buffer.

func (*Buffer) Mutable

func (b *Buffer) Mutable() bool

Mutable returns a bool indicating whether the buffer is mutable or not.

func (*Buffer) Parent

func (b *Buffer) Parent() *Buffer

Parent returns either nil or a pointer to the parent buffer if this buffer was sliced from another.

func (*Buffer) Release

func (b *Buffer) Release()

Release decreases the reference count by 1. When the reference count goes to zero, the memory is freed.

func (*Buffer) Reserve

func (b *Buffer) Reserve(capacity int)

Reserve reserves the provided amount of capacity for the buffer.

func (*Buffer) Reset

func (b *Buffer) Reset(buf []byte)

Reset resets the buffer for reuse. If the buffer owns memory through an allocator, the supplied data is copied into allocator-owned storage.

func (*Buffer) Resize

func (b *Buffer) Resize(newSize int)

Resize resizes the buffer to the target size.

func (*Buffer) ResizeNoShrink

func (b *Buffer) ResizeNoShrink(newSize int)

ResizeNoShrink resizes the buffer to the target size, but will not shrink it.

func (*Buffer) Retain

func (b *Buffer) Retain()

Retain increases the reference count by 1.

type CheckedAllocator

type CheckedAllocator struct {
	// contains filtered or unexported fields
}

func NewCheckedAllocator

func NewCheckedAllocator(mem Allocator) *CheckedAllocator

func (*CheckedAllocator) Allocate

func (a *CheckedAllocator) Allocate(size int) []byte

func (*CheckedAllocator) AssertSize

func (a *CheckedAllocator) AssertSize(t TestingT, sz int)

func (*CheckedAllocator) CurrentAlloc

func (a *CheckedAllocator) CurrentAlloc() int

func (*CheckedAllocator) Free

func (a *CheckedAllocator) Free(b []byte)

func (*CheckedAllocator) Reallocate

func (a *CheckedAllocator) Reallocate(size int, b []byte) []byte

type CheckedAllocatorScope

type CheckedAllocatorScope struct {
	// contains filtered or unexported fields
}

func NewCheckedAllocatorScope

func NewCheckedAllocatorScope(alloc *CheckedAllocator) *CheckedAllocatorScope

func (*CheckedAllocatorScope) CheckSize

func (c *CheckedAllocatorScope) CheckSize(t TestingT)

type GoAllocator

type GoAllocator struct{}

GoAllocator is an Allocator backed by the Go heap. Buffers it returns start at a 64-byte boundary; Free is a no-op because the garbage collector reclaims the memory. Its buffers must not be retained by C code beyond a single cgo call; see the package documentation for allocators that return C memory.

func NewGoAllocator

func NewGoAllocator() *GoAllocator

NewGoAllocator returns a GoAllocator.

func (*GoAllocator) Allocate

func (a *GoAllocator) Allocate(size int) []byte

func (*GoAllocator) Free

func (a *GoAllocator) Free(b []byte)

func (*GoAllocator) Reallocate

func (a *GoAllocator) Reallocate(size int, b []byte) []byte

type TestingT

type TestingT interface {
	Errorf(format string, args ...interface{})
	Helper()
}

Directories

Path Synopsis
Package mallocator defines an allocator implementation for memory.Allocator which defers to libc malloc.
Package mallocator defines an allocator implementation for memory.Allocator which defers to libc malloc.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL