alloc

package
v0.0.0-...-32c7374 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: MIT Imports: 10 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)

func Append

func Append[S ~[]T, T any](a Arena, s S, item T) S

Append appends a new item to a slice, grows the slice if required, and returns the modified slice.

func AppendN

func AppendN[S ~[]T, T any](a Arena, s S, items ...T) S

AppendN appends a new slice to a slice, grows the slice if required, and returns the modified slice.

func Bytes

func Bytes(a Arena, len int) []byte

Bytes allocates a new byte slice.

func Copy

func Copy[S ~[]T, T any](a Arena, src S) S

Copy allocates a new slice and copies items from src into it. The slice capacity is len(src).

func CopyBytes

func CopyBytes(a Arena, src []byte) []byte

CopyBytes allocates a new byte slice and copies items from src into it. The slice capacity is len(src).

func CopyBytesTo

func CopyBytesTo(a Arena, dst []byte, src []byte) []byte

CopyBytesTo copies bytes from src into dst, growing dst if needed.

func Grow

func Grow[S ~[]T, T any](a Arena, s S, capacity int) S

Grow grows the slice to at least the given capacity.

func NewArenaRef

func NewArenaRef() ref.R[Arena]

NewArenaRef returns a reference to a new non-thread-safe arena.

func Slice

func Slice[S ~[]T, T any](a Arena, len int, cap int) S

Slice allocates a new slice of a generic type.

Usage:

s := Slice[[]MyStruct](arena, 0, 16)

func Slice1

func Slice1[S ~[]T, T any](a Arena, item T) S

Slice1 allocates a new slice with a single item.

Usage:

elem := 123
s := Slice[[]int](arena, elem)

func String

func String(a Arena, src string) string

String allocates a new string and copies data from src into it.

func StringBytes

func StringBytes(a Arena, src []byte) string

StringBytes allocates a new string and copies data from src into it.

func StringFormat

func StringFormat(a Arena, format string, args ...any) string

StringFormat formats a string using fmt.Appendf and returns a new string allocated in the arena.

func StringJoin

func StringJoin(a Arena, src []string, sep string) string

StringJoin joins strings using separator.

func StringJoin2

func StringJoin2(a Arena, s1, s2, sep string) string

StringJoin2 joins two strings using separator.

func StringRunes

func StringRunes(a Arena, src []rune) string

StringRunes allocates a new string and copies data from src into it.

Types

type Arena

type Arena = arena.Arena

Arena is an arena memory allocator. The arena must be freed after usage.

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 NewArena

func NewArena() Arena

NewArena returns a new non-thread-safe arena.

func NewMutexArena

func NewMutexArena() Arena

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

type Buffer

type Buffer = buffer.Buffer

Buffer is a buffer allocated by an allocator. The buffer must be freed after usage.

func AcquireBuffer

func AcquireBuffer() Buffer

AcquireBuffer returns a new buffer from the pool.

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

Typical usage:

buf := alloc.AcquireBuffer()
defer buf.Free() // free immediately

func NewBuffer

func NewBuffer() Buffer

NewBuffer allocates a buffer.

func NewBufferSize

func NewBufferSize(size int) Buffer

NewBuffer allocates a buffer of a preallocated capacity.

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(a Arena) BufferPool

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

type BufferedWriter

type BufferedWriter = bufwriter.Writer

Writer buffers small writes and flushes them to an underlying writer.

func NewBufferedWriter

func NewBufferedWriter(dst io.Writer) BufferedWriter

NewBufferedWriter returns a new buffered writer with the default buffer size.

func NewBufferedWriterSize

func NewBufferedWriterSize(dst io.Writer, size int) BufferedWriter

NewBufferedWriterSize returns a new buffered writer with the specified buffer size.

type MutexArena

type MutexArena = arena.MutexArena

Arena is an arena memory allocator. The arena must be freed after usage.

type Pinned

type Pinned[T any] struct {
	Set bool
	Obj T
}

Pinned is a wrapper for an object pinned to an arena.

func Pin

func Pin[T any](arena Arena, obj T) Pinned[T]

Pin pins an object to an arena.

func (*Pinned[T]) Reset

func (p *Pinned[T]) Reset()

Reset clears the pinned object and the set flag.

func (Pinned[T]) Unwrap

func (p Pinned[T]) Unwrap() T

Unwrap returns the pinned object and panics if the object is not pinned.

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.
	// 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.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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