#arena-allocator

index_arena

A simple, id-based, heterogeneous arena allocator

4 releases

0.1.3 Sep 2, 2024
0.1.2 Aug 31, 2024
0.1.1 Aug 29, 2024
0.1.0 Aug 28, 2024

#219 in Memory management

Download history 481/week @ 2024-08-27 61/week @ 2024-09-03 8/week @ 2024-09-10 5/week @ 2024-09-17 5/week @ 2024-09-24 12/week @ 2024-10-01

167 downloads per month

MIT/Apache

27KB
420 lines

A simple, id-based, heterogeneous arena allocator.

Id-based

Uses small unique identifiers instead of references to represent allocations. This leverages the type system to statically assign every identifier to the arena it belongs to, ensuring safety without incurring runtime overhead.

Accessing individual elements is done through the various arena methods, conceptually similar to indexing a Vec.

Heterogeneous

Supports allocating values of all statically sized non-ZST types, which is especially useful in scenarios where you have tree-like data structures with different node types.

Statically guaranteed safety

The implementation leverages the power of the Rust's type system, achieving safety with almost no runtime checks.

No Drop

This design, however, has one downside: the arena does not know about individual objects it contains, which makes it impossible to run their destructors on drop.

Examples

use index_arena::{Id, new_arena};

struct Even<A> {
     next: Option<Id<Odd<A>, A>>,
}

struct Odd<A> {
     next: Option<Id<Even<A>, A>>,
}

let mut arena = new_arena!();

let three = arena.alloc(Odd { next: None });
let two = arena.alloc(Even { next: Some(three) });
let one = arena.alloc(Odd { next: Some(two) });

assert_eq!(&arena[one].next, &Some(two));

Dependencies

~0.3–0.8MB
~20K SLoC