mem

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package mem provides memory allocation facilities.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoAlloc = errors.New("mem: allocation not allowed")
View Source
var ErrOutOfMemory = errors.New("out of memory")

ErrOutOfMemory is returned when a memory allocation fails due to insufficient memory.

Functions

func Alloc

func Alloc[T any](a Allocator) *T

Alloc allocates a single value of type T using allocator a. Returns a pointer to the allocated memory or panics on failure. Whether new memory is zeroed depends on the allocator. If the allocator is nil, uses the system allocator.

func AllocSlice

func AllocSlice[T any](a Allocator, len int, cap int) []T

AllocSlice allocates a slice of type T with given length and capacity using allocator a. Returns a slice of the allocated memory or panics on failure. Whether new memory is zeroed depends on the allocator. If the allocator is nil, uses the system allocator.

func Clear

func Clear(ptr any, size int)

Clear zeroes size bytes starting at ptr.

func Compare

func Compare(a any, b any, size int) int

Compare compares size bytes at a and b. Returns an integer comparing the bytes at a and b. The result will be 0 if the bytes are equal, -1 if a < b, and +1 if a > b. Panics if either a or b is nil.

func Copy

func Copy(dst any, src any, n int) any

Copy copies n bytes from src to dst. Returns dst. The memory areas must not overlap. Panics if either dst or src is nil.

func Free

func Free[T any](a Allocator, ptr *T)

Free frees a value previously allocated with Alloc or TryAlloc. If the allocator is nil, uses the system allocator.

func FreeSlice

func FreeSlice[T any](a Allocator, slice []T)

FreeSlice frees a slice previously allocated with AllocSlice or TryAllocSlice. If the allocator is nil, uses the system allocator. Calling FreeSlice on an empty or nil slice is a no-op.

func FreeString

func FreeString(a Allocator, s string)

FreeString frees a heap-allocated string. If the allocator is nil, uses the system allocator.

func Move

func Move(dst any, src any, n int) any

Move copies n bytes from src to dst. Returns dst. The memory areas may overlap. Panics if either dst or src is nil.

func ReallocSlice

func ReallocSlice[T any](a Allocator, slice []T, newLen int, newCap int) []T

ReallocSlice reallocates a slice of type T with new length and capacity using allocator a. Preserves contents up to the old capacity. Returns the reallocated slice or panics on failure. Whether new memory is zeroed depends on the allocator. If the allocator is nil, uses the system allocator.

func Swap

func Swap[T any](a *T, b *T)

Swap swaps the values pointed to by a and b. Panics if either a or b is nil.

func SwapByte

func SwapByte(a any, b any, n int)

SwapByte swaps n bytes between a and b. Panics if either a or b is nil.

SwapByte temporarily allocates a buffer of size n on the stack, so it's not suitable for large n.

func TryAlloc

func TryAlloc[T any](a Allocator) (*T, error)

TryAlloc is like Alloc but returns an error instead of panicking on failure.

func TryAllocSlice

func TryAllocSlice[T any](a Allocator, len int, cap int) ([]T, error)

TryAllocSlice is like AllocSlice but returns an error instead of panicking on allocation failure.

func TryReallocSlice

func TryReallocSlice[T any](a Allocator, slice []T, newLen int, newCap int) ([]T, error)

TryReallocSlice is like ReallocSlice but returns an error instead of panicking on allocation failure.

Types

type Allocator

type Allocator interface {
	// Alloc allocates a block of memory of the given size and alignment.
	Alloc(size int, align int) (any, error)
	// Realloc resizes a previously allocated block of memory.
	Realloc(ptr any, oldSize int, newSize int, align int) (any, error)
	// Free frees a previously allocated block of memory.
	Free(ptr any, size int, align int)
}

Allocator defines the interface for memory allocators. Whether allocated or reallocated memory is zeroed is allocator-specific.

var NoAlloc Allocator = &NoAllocator{}

NoAlloc is an instance of an Allocator that returns an error on any allocation. Free is a no-op. It's meant for cases when allocations are strictly forbidden.

var System Allocator = &SystemAllocator{}

System is an instance of a memory allocator that uses the system's malloc, realloc, and free functions.

type Arena

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

Arena is a memory allocator that bump-allocates linearly within a fixed buffer. Free reclaims the last allocation if the pointer matches; otherwise it is a no-op. Use Reset to reclaim all memory at once.

func NewArena

func NewArena(buf []byte) Arena

NewArena creates an arena allocator backed by the given buffer.

func (*Arena) Alloc

func (a *Arena) Alloc(size int, align int) (any, error)

func (*Arena) Free

func (a *Arena) Free(ptr any, size int, align int)

func (*Arena) Realloc

func (a *Arena) Realloc(ptr any, oldSize int, newSize int, align int) (any, error)

func (*Arena) Reset

func (a *Arena) Reset()

Reset reclaims all allocated memory, allowing the arena to be reused.

type NoAllocator

type NoAllocator struct{}

NoAllocator is an Allocator that returns an error on any allocation. Free is a no-op. It's meant for cases when allocations are strictly forbidden.

func (*NoAllocator) Alloc

func (*NoAllocator) Alloc(size int, align int) (any, error)

func (*NoAllocator) Free

func (*NoAllocator) Free(ptr any, size int, align int)

func (*NoAllocator) Realloc

func (*NoAllocator) Realloc(ptr any, oldSize int, newSize int, align int) (any, error)

type Stats

type Stats struct {
	// Alloc is bytes of allocated heap objects.
	Alloc uint64

	// TotalAlloc is cumulative bytes allocated for heap objects.
	//
	// TotalAlloc increases as heap objects are allocated, but
	// unlike Alloc, it does not decrease when
	// objects are freed.
	TotalAlloc uint64

	// Mallocs is the cumulative count of heap objects allocated.
	// The number of live objects is Mallocs - Frees.
	Mallocs uint64

	// Frees is the cumulative count of heap objects freed.
	Frees uint64
}

A Stats records statistics about the memory allocator.

type SystemAllocator

type SystemAllocator struct{}

SystemAllocator uses the system's malloc, realloc, and free functions. It zeros out new memory on allocation and reallocation.

func (*SystemAllocator) Alloc

func (*SystemAllocator) Alloc(size int, align int) (any, error)

func (*SystemAllocator) Free

func (*SystemAllocator) Free(ptr any, size int, align int)

func (*SystemAllocator) Realloc

func (*SystemAllocator) Realloc(ptr any, oldSize int, newSize int, align int) (any, error)

type Tracker

type Tracker struct {
	Allocator Allocator
	Stats     Stats
}

A Tracker wraps an Allocator and tracks all allocations and deallocations made through it.

func (*Tracker) Alloc

func (t *Tracker) Alloc(size int, align int) (any, error)

func (*Tracker) Free

func (t *Tracker) Free(ptr any, size int, align int)

func (*Tracker) Realloc

func (t *Tracker) Realloc(ptr any, oldSize int, newSize int, align int) (any, error)

Jump to

Keyboard shortcuts

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