Arena allocator for zero-copy bytes::Bytes.
Write into a Buffer, call freeze(), and hand off Send + 'static Bytes backed by arena memory. Each slot or block returns to the arena when its final clone or slice drops.
arena-alligator = "0.8"
usage
use core::num::NonZeroUsize; use arena_alligator::FixedArena; use bytes::BufMut; let arena = FixedArena::with_slot_capacity( NonZeroUsize::new(1024).unwrap(), NonZeroUsize::new(4096).unwrap(), ).build()?; let mut buf = arena.allocate()?; buf.put_slice(b"request payload"); let bytes = buf.freeze(); // slot frees when the last clone or slice drops
use core::num::NonZeroUsize; use arena_alligator::{BuddyArena, BuddyGeometry}; use bytes::BufMut; let arena = BuddyArena::builder( BuddyGeometry::exact( NonZeroUsize::new(64 * 1024 * 1024).unwrap(), NonZeroUsize::new(256).unwrap(), )?, ).build()?; let mut buf = arena.allocate(NonZeroUsize::new(8192).unwrap())?; buf.put_slice(&[0u8; 8192]); let bytes = buf.freeze();
use core::num::NonZeroUsize; use std::sync::Arc; use arena_alligator::FixedArena; let arena = Arc::new( FixedArena::with_slot_capacity( NonZeroUsize::new(64).unwrap(), NonZeroUsize::new(4096).unwrap(), ) .build_async()?, ); let mut buf = arena.allocate_async().await;
use core::num::NonZeroUsize; use arena_alligator::{FixedArena, SlotSpec}; static mut BLOCK: [u8; 4096] = [0; 4096]; // from_raw() accepts any pointer, length, and Dealloc strategy without prefaulting. let arena = FixedArena::from_static( unsafe { &mut BLOCK }, SlotSpec::Count(NonZeroUsize::new(4).unwrap()), ).build()?;
use core::num::NonZeroUsize; use arena_alligator::FixedArena; let arena = FixedArena::with_slot_capacity( NonZeroUsize::new(16).unwrap(), NonZeroUsize::new(256).unwrap(), ) .hazmat_raw_access() .build()?; let mut raw = arena.raw_alloc()?; unsafe { core::ptr::copy_nonoverlapping(b"hello".as_ptr(), raw.as_mut_ptr().add(4), 5) }; let payload = unsafe { raw.freeze(4..9) }?;
use core::num::NonZeroUsize; use arena_alligator::{FixedArena, InitPolicy}; let arena = FixedArena::with_slot_capacity( NonZeroUsize::new(1024).unwrap(), NonZeroUsize::new(4096).unwrap(), ) .init_policy(InitPolicy::Zero) // scrubs on return + cold first alloc via zeroize .build()?;
why arena-alligator
-
Off the global heap
Pre-allocated contiguous memory with lock-free bitmap claims (
FixedArena) or power-of-two split/coalesce (BuddyArena) keeps steady-state allocation predictable. -
No arena lifetimes
freeze()returns ordinarySend + 'staticBytes; pass clones and slices across async tasks or FFI and reclaim each slot as soon as its last ref drops. -
Hard memory bounds
Cap memory up front instead of OOMing on slow consumers: fail fast (
ArenaFull), park onallocate_async(), or opt intoauto_spill(). -
Bring your own memory & scrub on reuse
Hand
mmapor static buffers tofrom_raw()/from_static()without prefaulting, opt intohazmat-raw-access, or scrub slots viazeroizewithInitPolicy::Zero.
ownership lifecycle
flowchart TD
A["arena.allocate()"] -->|"claim slot / split block"| B["Buffer (BufMut)"]
B -->|"freeze()"| C["bytes::Bytes (Send + 'static, cloneable / sliceable)"]
B -->|"drop / abandon()"| R["Release slot or block"]
B -->|"auto_spill() overflow"| S["Heap BytesMut (slot freed immediately)"]
C -->|"last clone & slice drop"| R
S --> R
R -->|"zeroize if InitPolicy::Zero"| F["Bitmap free / buddy coalesce + wake async waiter"]
classDef accent fill:#166439,stroke:#38a169,color:#fffffb,stroke-width:1.5px;
classDef box fill:#141514,stroke:#454a46,color:#e8e8e2,stroke-width:1px;
class A,B,S,R box;
class C,F accent;
benchmarks
Apple M4 Max, Criterion 4-run median, rustc 1.100.0 (allocate → write → freeze → drop). Full contention tables and k8s runs in docs/benchmarks.md.
| benchmark | payload | median | range | throughput |
|---|---|---|---|---|
fixed_roundtrip | 256 B | 58.1 ns | 57.8 .. 58.9 ns | 4.10 GiB/s |
fixed_roundtrip | 1024 B | 78.4 ns | 76.9 .. 79.2 ns | 12.17 GiB/s |
fixed_roundtrip | 4096 B | 144.3 ns | 138.9 .. 147.2 ns | 26.44 GiB/s |
buddy_roundtrip | 1024 B | 1.188 μs | 1.172 .. 1.215 μs | 822.4 MiB/s |
buddy_roundtrip | 4096 B | 379.5 ns | 363.8 .. 394.2 ns | 10.05 GiB/s |
buddy_roundtrip | 16384 B | 334.5 ns | 327.4 .. 338.1 ns | 45.62 GiB/s |
features
| flag | default | description |
|---|---|---|
std | yes | Standard-library integrations; disable default features for #![no_std] + alloc. |
libc | yes | Page size detection via sysconf on Unix for PageSize::Auto. |
async-alloc | no | allocate_async() and raw_alloc_async() via tokio (implies std). |
hazmat-raw-access | no | Direct pointer access via RawRegion and unsafe freeze(range). |