Byte Arena
Byte Arena
Implement a simple bump allocator (arena) for reusable byte slices.
Types
type Arena struct {
// internal fields
}
Functions
func NewArena(size int) *Arena
func (a *Arena) Alloc(n int) []byte
func (a *Arena) AllocAligned(n int, align int) []byte
func (a *Arena) AllocString(s string) []byte
func (a *Arena) Reset()
func (a *Arena) Remaining() int
Requirements
NewArenaallocates a single backing buffer ofsizebytes.Alloc(n):- returns a slice of length and capacity
ncarved from the arena - returns
nilif there is not enough space - must not allocate
- returns a slice of length and capacity
AllocAligned(n, align):- aligns the allocation start to a power‑of‑two boundary (
align> 0) - returns
nilifalignis not a power of two or there is not enough space - must not allocate
- aligns the allocation start to a power‑of‑two boundary (
AllocString(s):- allocates
len(s)bytes and copiessinto the arena - returns
nilif there is not enough space - must not allocate
- allocates
Reset():- resets the arena so all bytes are available again
- must not allocate
Remaining()returns remaining bytes.
Notes
- Returned slices may contain stale data (no zeroing required).
- Tests enforce zero allocations in hot methods.
Run tests to see results
No issues detected