Documentation ¶
Overview ¶
Package arena implements a memory arena.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Arena ¶
Arena is a native memory allocator that owns each of the allocations made by Allocate() and Reallocate(). If there are any outstanding allocations when the Arena is disposed then these allocations are automatically freed. Because the memory is allocated outside of the Go environment it is important to explicity free unused memory - either by calling Free() or calling Dispose() on the Arena. Failing to Dispose() the arena will leak memory.
func New ¶
func New() Arena
New constructs a new arena. You must call Dispose to free the arena object and any arena-owned allocations.
func (Arena) Allocate ¶
Allocate returns a pointer to a new arena-owned, contiguous block of memory of the specified size and alignment.
func (Arena) Dispose ¶
func (a Arena) Dispose()
Dispose destructs and frees the arena and all arena-owned allocations.
func (Arena) Free ¶
Free releases the memory at ptr, which must have been previously allocated by this arena.
func (Arena) NewWriter ¶
NewWriter returns a new Writer to a new arena allocated buffer of the initial size. The native buffer may grow if the writer exceeds the size of the buffer. The buffer will always be of the specified alignment in memory. The once the native buffer is no longer needed, the pointer returned by Pointer() should be passed to Arena.Free().
func (Arena) Reallocate ¶
Reallocate reallocates the memory at ptr to the new size and alignment. ptr must have been allocated from this arena or be nil.
type Offsetable ¶
type Offsetable struct{ Offset int }
Offsetable is used as an anonymous field of types that require a current offset value.
func (*Offsetable) AlignUp ¶
func (o *Offsetable) AlignUp(n int)
AlignUp rounds-up the current offset so that is is a multiple of n.
type Reader ¶
type Reader struct { Offsetable // The current read-offset in bytes. // contains filtered or unexported fields }
Reader provides the Read method to read native buffer data. Use NewReader() to construct.
type Writer ¶
type Writer struct { Offsetable // The current write-offset in bytes. // contains filtered or unexported fields }
Writer provides methods to help allocate and populate a native buffer with data. Use Arena.Writer() to construct.
func (*Writer) Pointer ¶
Pointer returns the base address of the native buffer for the writer. Calling Pointer() freezes the writer - once called no more writes to the buffer can be made, unless Reset() is called. Freezing attempts to reduce the chance of the stale pointer being used after a buffer reallocation.
func (*Writer) Reset ¶
func (w *Writer) Reset()
Reset sets the write offset back to the start of the buffer and unfreezes the writer. This allows for efficient reuse of the writer's native buffer.