arena

package
v0.0.0-...-2aa3dc7 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alloc

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

Alloc allocates a new object and returns a pointer to it.

Usage:

var foo *float64
var bar *MyStruct
foo = Alloc[float64](arena)
bar = Alloc[MyStruct](arena)

Types

type Arena

type Arena interface {
	// Cap returns the arena capacity.
	Cap() int64

	// Len calculates and returns the number of used bytes.
	Len() int64

	// Alloc allocates a memory block and returns a pointer to it.
	Alloc(size int) unsafe.Pointer

	// Bytes allocates a byte slice.
	Bytes(size int) []byte

	// Buffer allocates a buffer in the arena, the buffer cannot be freed.
	Buffer() buffer.Buffer

	// Pin pins an external object to the arena.
	// The method is used to prevent the object from being collected by the garbage collector.
	Pin(obj any)

	// Reset resets the arena.
	Reset()

	// Free frees the arena and releases its memory.
	// The method is not thread-safe and must be called only once.
	Free()
}

Arena is an arena allocator, which internally allocates memory in blocks.

Arena is not thread-safe. If you need to use it in multiple goroutines, use NewMutexArena.

func AcquireArena

func AcquireArena() Arena

AcquireArena returns a pooled arena, which is released to the pool on Free.

The arena must not be used or even referenced after Free. Use these method only when arenas do not escape an isolated scope.

Typical usage:

arena := alloc.AcquireArena()
defer arena.Free() // free immediately

func New

func New() Arena

New returns an empty non-thread-safe arena.

func NewMutexArena

func NewMutexArena() Arena

NewMutexArena returns a new thread-safe arena which uses a mutex to synchronize access.

func Test

func Test() Arena

func TestMutex

func TestMutex() Arena

type BufferPool

type BufferPool interface {
	// Get returns an empty pool.
	Get() buffer.Buffer

	// Put reset and puts a buffer back into the pool.
	// The buffer must be allocated in this pool.
	Put(buf buffer.Buffer)
}

BufferPool is a pool of buffers allocated in the arena. It is thread-safe but only if backed by MutexArena. The pool itself is allocated in the arena.

func NewBufferPool

func NewBufferPool(arena Arena) BufferPool

NewBufferPool returns a new buffer pool which allocates buffers in the given arena.

type MutexArena

type MutexArena = Arena

MutexArena is a thread-safe arena which uses a mutex to synchronize access.

type Pool

type Pool[T any] interface {
	// Get acquires an object and returns true, or allocates a new one and returns false.
	Get() (*T, bool)

	// Put puts an object back into the pool, does not zero it.
	// The object must be allocated in this pool.
	Put(obj *T)
}

Pool is a pool of objects allocated in the arena. It is thread-safe but only if backed by MutexArena. The pool itself is allocated in the arena.

func NewPool

func NewPool[T any](a Arena) Pool[T]

NewPool returns a new pool which allocates objects in the given arena.

Jump to

Keyboard shortcuts

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