Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 NewMutexArena ¶
func NewMutexArena() Arena
NewMutexArena returns a new thread-safe arena which uses a mutex to synchronize access.
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.