Byte Arena

medium · memory, allocation, buffers

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

  • NewArena allocates a single backing buffer of size bytes.
  • Alloc(n):
    • returns a slice of length and capacity n carved from the arena
    • returns nil if there is not enough space
    • must not allocate
  • AllocAligned(n, align):
    • aligns the allocation start to a power‑of‑two boundary (align > 0)
    • returns nil if align is not a power of two or there is not enough space
    • must not allocate
  • AllocString(s):
    • allocates len(s) bytes and copies s into the arena
    • returns nil if there is not enough space
    • must not allocate
  • 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