Documentation
¶
Overview ¶
Package mem provides memory allocation facilities.
Index ¶
- Variables
- func Alloc[T any](a Allocator) *T
- func AllocSlice[T any](a Allocator, len int, cap int) []T
- func Clear(ptr any, size int)
- func Compare(a any, b any, size int) int
- func Copy(dst any, src any, n int) any
- func Free[T any](a Allocator, ptr *T)
- func FreeSlice[T any](a Allocator, slice []T)
- func FreeString(a Allocator, s string)
- func Move(dst any, src any, n int) any
- func ReallocSlice[T any](a Allocator, slice []T, newLen int, newCap int) []T
- func Swap[T any](a *T, b *T)
- func SwapByte(a any, b any, n int)
- func TryAlloc[T any](a Allocator) (*T, error)
- func TryAllocSlice[T any](a Allocator, len int, cap int) ([]T, error)
- func TryReallocSlice[T any](a Allocator, slice []T, newLen int, newCap int) ([]T, error)
- type Allocator
- type Arena
- type NoAllocator
- type Stats
- type SystemAllocator
- type Tracker
Constants ¶
This section is empty.
Variables ¶
var ErrNoAlloc = errors.New("mem: allocation not allowed")
var ErrOutOfMemory = errors.New("out of memory")
ErrOutOfMemory is returned when a memory allocation fails due to insufficient memory.
Functions ¶
func Alloc ¶
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 ¶
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 Compare ¶
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 ¶
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 ¶
Free frees a value previously allocated with Alloc or TryAlloc. If the allocator is nil, uses the system allocator.
func FreeSlice ¶
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 ¶
FreeString frees a heap-allocated string. If the allocator is nil, uses the system allocator.
func Move ¶
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 ¶
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 ¶
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 TryAllocSlice ¶
TryAllocSlice is like AllocSlice but returns an error instead of panicking on allocation failure.
func TryReallocSlice ¶
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.
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.
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.