memory

package
v3.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package memory provides support for allocating and reusing contiguous [Region]s of memory.

An Allocator supports reclaiming memory regions for reuse, invaliding existing regions. Using a memory region after it has been reclaimed produces undefined behavior, so caution must be taken to ensure that the lifetime of Memory does not exceed the lifetime of the owning Allocator.

Utility packages are provided to make it easier to work with memory regions:

Memory is EXPERIMENTAL and is currently only intended for use by github.com/grafana/loki/v3/pkg/dataobj.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Allocator

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

Allocator is an arena-style memory allocator that manages a set of memory regions.

An Allocator must not be copied after first use.

The Allocator can be reset to reclaim memory regions, marking them as free for future calls to Allocator.Allocate.

Allocator is unsafe and provides no use-after-free checks for Region. Callers must take care to ensure that the lifetime of a returned Memory does not exceed the lifetime of an allocator reuse cycle.

Allocators are not goroutine safe. If an Allocator methods are called concurrently, the method will panic.

func NewAllocator

func NewAllocator(parent *Allocator) *Allocator

NewAllocator creates a new Allocator with the given parent. If parent is nil, the returned allocator is a root allocator.

Child allocators will obtain memory from their parent and can manage its own Trim and Reclaim lifecycle. All memory allocated from a child is invalidated when any of its parents (up to the root) reclaims memory.

func (*Allocator) Allocate

func (alloc *Allocator) Allocate(size int) *Region

Allocate retrieves the next free Memory region that can hold at least size bytes. If there is no such free Memory region, a new memory region will be created.

func (*Allocator) AllocatedBytes

func (alloc *Allocator) AllocatedBytes() int

AllocatedBytes returns the total amount of bytes owned by the Allocator.

func (*Allocator) Free

func (alloc *Allocator) Free()

Free returns all memory regions back to the parent allocator, if there is one. Otherwise, released memory regions are returned to the Go runtime for garbage collection.

It is a convenience wrapper for calling Allocator.Reclaim and Allocator.Trim (in that order).

func (*Allocator) FreeBytes

func (alloc *Allocator) FreeBytes() int

FreeBytes returns the total amount of bytes available for Memory to use without requiring additional allocations.

func (*Allocator) Reclaim

func (alloc *Allocator) Reclaim()

Reclaim all memory regions back to the Allocator for reuse. After calling Reclaim, any Region returned by the Allocator or any child allocators must no longer be used.

func (*Allocator) Reset

func (alloc *Allocator) Reset()

Reset resets the Allocator for reuse. It is a convenience wrapper for calling Allocator.Trim and Allocator.Reclaim (in that order).

Advanced use cases may wish to selectively call Trim depending on the values of Allocator.AllocatedBytes and Allocator.FreeBytes.

func (*Allocator) Trim

func (alloc *Allocator) Trim()

Trim releases unused memory regions. If the allocator has a parent, released memory regions are returned to the parent allocator. Otherwise, released memory regions may be returned to the Go runtime for garbage collection.

If Trim is called after Reclaim, all memory regions will be released.

type Bitmap

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

Bitmap is a bit-packed representation of a sequence of boolean values. Bits are ordered in LSB (Least Significant Bit) for compatibility with Apache Arrow.

The zero value is ready for use, unassociated with a memory allocator. Use NewBitmap to create an allocator-associated bitmap.

func NewBitmap

func NewBitmap(alloc *Allocator, n int) Bitmap

NewBitmap creates a Bitmap managed by the provided allocator. The returned Bitmap will have an initial length of zero and a capacity of at least n (which may be 0).

If alloc is nil, memory is created using Go's built-in memory allocation. Otherwise, the lifetime of the returned Bitmap must not exceed the lifetime of alloc.

func (*Bitmap) Append

func (bmap *Bitmap) Append(value bool)

Append appends value to bmap.

func (*Bitmap) AppendBitmap

func (bmap *Bitmap) AppendBitmap(from Bitmap)

AppendBitmap appends the contents of another bitmap into bmap.

func (*Bitmap) AppendCount

func (bmap *Bitmap) AppendCount(value bool, count int)

AppendCount appends value count times to bmap.

func (*Bitmap) AppendCountUnsafe

func (bmap *Bitmap) AppendCountUnsafe(value bool, count int)

AppendCountUnsafe appends value count times to bmap without checking for capacity.

func (*Bitmap) AppendUnsafe

func (bmap *Bitmap) AppendUnsafe(value bool)

AppendUnsafe appends value to bmap without checking for capacity.

func (*Bitmap) AppendValues

func (bmap *Bitmap) AppendValues(values ...bool)

AppendValues adds a sequence of values to bmap.

func (*Bitmap) Bytes

func (bmap *Bitmap) Bytes() (data []byte, offset int)

Bytes returns the raw representation of bmap, with bits stored in Least Significant Bit (LSB) order.

The offset return parameter denotes the offset of the first bit in data. This can be set to non-zero if bmap is sliced partially into a word.

The first offset bits in data are undefined.

func (*Bitmap) Cap

func (bmap *Bitmap) Cap() int

Cap returns how many values bmap can hold without needing a new allocation.

func (*Bitmap) ClearCount

func (bmap *Bitmap) ClearCount() int

ClearCount returns the number of bits unset in the bitmap.

func (*Bitmap) Clone

func (bmap *Bitmap) Clone(alloc *Allocator) *Bitmap

Clone returns a copy of bmap using the provided allocator. The returned bitmap will store the provided allocator for future allocations.

If bmap is an unaligned slice, the cloned bitmap will be normalized to remove offsets. See Bitmap.Bytes for more information on aligned slices.

func (*Bitmap) Get

func (bmap *Bitmap) Get(i int) bool

Get returns the value at index i. Get panics if i is out of range.

func (*Bitmap) Grow

func (bmap *Bitmap) Grow(n int)

Grow increases bmap's capacity, if necessary, to guarantee space for another n values. After Grow(n), at least n values can be appended to bmap without another allocation. If n is negative or too large to allocate the memory, Grow panics.

func (*Bitmap) IterValues

func (bmap *Bitmap) IterValues(value bool) iter.Seq[int]

IterValues returns an iterator over bits, returning the index of each bit matching value.

func (*Bitmap) Len

func (bmap *Bitmap) Len() int

Len returns the length of bmap.

func (*Bitmap) Resize

func (bmap *Bitmap) Resize(n int)

Resize changes the length of bmap to n, allowing to set any index of bmap up to n. Resize will allocate additional memory if necessary.

func (*Bitmap) Set

func (bmap *Bitmap) Set(i int, value bool)

Set sets the bit at index i to the given value. Set panics if i is out of range of the length.

func (*Bitmap) SetCount

func (bmap *Bitmap) SetCount() int

SetCount returns the number of bits set in the bitmap.

func (*Bitmap) SetRange

func (bmap *Bitmap) SetRange(from, to int, value bool)

SetRange sets all the bits in the range [from, to). SetRange panics if from > to or if to > bmap.Len().

func (*Bitmap) Slice

func (bmap *Bitmap) Slice(i, j int) *Bitmap

Slice returns a slice of bmap from index i to j. he returned slice has both a length and capacity of j-i, shares memory with bmap, and uses the same allocator for new allocations (when needed).

Slice panics if the following invariant is not met: 0 <= i <= j <= bmap.Len()

type Buffer

type Buffer[T any] struct {
	// contains filtered or unexported fields
}

Buffer is a low-level memory buffer for storing a set of elements contiguously in memory.

Buffers must be created using NewBuffer.

func NewBuffer

func NewBuffer[T any](alloc *Allocator, n int) Buffer[T]

NewBuffer creates a Buffer managed by the provided allocator. The returned Buffer will have an initial length of zero and a capacity of at least n (which may be 0).

The lifetime of the returned Buffer must not exceed the lifetime of alloc.

func (*Buffer[T]) Append

func (buf *Buffer[T]) Append(values ...T)

Append appends a set of values to buf, adding len(values) to buf's length. Append panics if there is not enough capacity for at least len(values) to be pushed.

func (*Buffer[T]) AppendCount

func (buf *Buffer[T]) AppendCount(value T, n int)

AppendCount appends the value to buf n times, adding n to buf's length. AppendCount panics if there is not enough capacity for the value to be appended n times.

func (*Buffer[T]) Cap

func (buf *Buffer[T]) Cap() int

Cap returns the capacity of buf.

func (*Buffer[T]) Clear

func (buf *Buffer[T]) Clear()

Clear zeroes out all memory in buf.

func (*Buffer[T]) Data

func (buf *Buffer[T]) Data() []T

Data returns the current data slice of buf so that elements can be read or modified directly.

func (*Buffer[T]) Get

func (buf *Buffer[T]) Get(i int) T

Get returns the value at index i. Get panics if i is out of bounds.

func (*Buffer[T]) Grow

func (buf *Buffer[T]) Grow(n int)

Grow increases the capacity of buf, if necessary, to have space for at least another n elements. After Grow(n), at least n elements can be pushed to the slice without another allocation. If n is negative or too large to allocate the memory, Grow panics.

func (*Buffer[T]) Len

func (buf *Buffer[T]) Len() int

Len returns the length of buf.

func (*Buffer[T]) Push

func (buf *Buffer[T]) Push(value T)

Push appends a single value to buf, adding 1 to buf's length. Push panics if there is not enough capacity for the value to be pushed.

func (*Buffer[T]) Resize

func (buf *Buffer[T]) Resize(n int)

Resize changes the length of buf to n, allowing to call Buffer.Set on any index up to n. Resize panics if n is bigger than the capacity of the buffer.

func (*Buffer[T]) Serialize

func (buf *Buffer[T]) Serialize() []byte

Serialize returns the serializable form of the underlying byte array representing buf, padded to 64-bytes. Padded bytes will be set to zero.

The returned memory is shared with buf, not a copy.

func (*Buffer[T]) Set

func (buf *Buffer[T]) Set(i int, value T)

Set sets the value at index i to value. Set panics if i is out of bounds.

func (*Buffer[T]) Slice

func (buf *Buffer[T]) Slice(i, j int) *Buffer[T]

Slice returns a slice of buf from index i to j. The returned slice has both a length and capacity of j-i, shares memory with buf, and uses the same allocator for new allocations (when needed).

Slice panics if the following invariant is not met: 0 <= i <= j <= buf.Len()

type Region

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

Region is a contiguous region of memory owned by an Allocator.

func (*Region) Data

func (m *Region) Data() []byte

Data returns the raw data of the memory region.

Directories

Path Synopsis
internal
memalign
Package memalign provides utilities for aligning memory.
Package memalign provides utilities for aligning memory.
unsafecast
Package unsafecast provides utilties for performing unsafe type casts.
Package unsafecast provides utilties for performing unsafe type casts.

Jump to

Keyboard shortcuts

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