alloc.rs arena-alligator 0.8.0

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"
#![no_std] + alloc Rust 2024 MSRV 1.88 Miri + Loom MIT
Arena Alligator

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

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_roundtrip256 B58.1 ns57.8 .. 58.9 ns4.10 GiB/s
fixed_roundtrip1024 B78.4 ns76.9 .. 79.2 ns12.17 GiB/s
fixed_roundtrip4096 B144.3 ns138.9 .. 147.2 ns26.44 GiB/s
buddy_roundtrip1024 B1.188 μs1.172 .. 1.215 μs822.4 MiB/s
buddy_roundtrip4096 B379.5 ns363.8 .. 394.2 ns10.05 GiB/s
buddy_roundtrip16384 B334.5 ns327.4 .. 338.1 ns45.62 GiB/s

features

flag default description
stdyesStandard-library integrations; disable default features for #![no_std] + alloc.
libcyesPage size detection via sysconf on Unix for PageSize::Auto.
async-allocnoallocate_async() and raw_alloc_async() via tokio (implies std).
hazmat-raw-accessnoDirect pointer access via RawRegion and unsafe freeze(range).