Lock-free arena allocator that produces bytes::Bytes via zero-copy freeze.
Write into a Buffer, freeze it into Bytes, and let the arena reclaim the backing memory when the last reference drops. The common path avoids a copy on freeze and avoids a fresh heap allocation per buffer.
use NonZeroUsize;
use FixedArena;
use BufMut;
let arena = builder.build?;
let mut buf = arena.allocate?;
buf.put_slice;
let _bytes = buf.freeze;
# Ok::
Allocator modes
FixedArena uses uniform slots. Allocation is a single bitmap claim. Use it when buffer sizes are predictable.
BuddyArena uses power-of-two blocks from one contiguous region. Requests are rounded up, larger blocks split on demand, and free blocks coalesce on release. Use it when sizes vary.
use NonZeroUsize;
use BuddyArena;
use BufMut;
let arena = builder.build?;
let mut buf = arena.allocate?;
buf.put_slice;
let _bytes = buf.freeze;
# Ok::
Auto-spill
By default, writing past buffer capacity panics. With auto_spill(), the buffer copies its current contents to heap-backed storage, frees the arena allocation immediately, and continues writing on the heap. freeze() still returns Bytes.
# use NonZeroUsize;
# use FixedArena;
# use BufMut;
let arena = builder.auto_spill.build?;
let mut buf = arena.allocate?;
buf.put_slice;
assert!;
let _bytes = buf.freeze;
# Ok::
Async allocation
With the async-alloc feature, both arena types support allocate_async(). The task waits until capacity is available and then retries against the live bitmap state.
[]
= { = "0.3", = ["async-alloc"] }
let arena = builder
.build_async?;
let buf = arena.allocate_async.await;
Metrics
Both arena types expose a metrics() snapshot. Fixed reports allocation, failure, spill, and live-capacity counters. Buddy adds split, coalesce, and largest-free-block data.
Status
Pre-1.0. The crate is usable now, but the API may still move before it settles.
License
MIT