#arena

no-std typed-arena

The arena, a fast but limited type of allocator

12 stable releases

2.0.0 Dec 3, 2019
2.0.0-rc1 Nov 26, 2019
1.7.0 Oct 31, 2019
1.4.1 Jun 30, 2018
1.0.1 Apr 10, 2015

#4 in Memory management

Download history 2878/week @ 2019-08-20 2787/week @ 2019-08-27 3150/week @ 2019-09-03 3424/week @ 2019-09-10 4855/week @ 2019-09-17 5016/week @ 2019-09-24 5295/week @ 2019-10-01 6029/week @ 2019-10-08 6780/week @ 2019-10-15 5578/week @ 2019-10-22 5402/week @ 2019-10-29 6227/week @ 2019-11-05 5324/week @ 2019-11-12 5443/week @ 2019-11-19 5540/week @ 2019-11-26

22,579 downloads per month
Used in 130 crates (31 directly)

MIT license

36KB
570 lines

typed-arena

Travis CI Build Status

A fast (but limited) allocation arena for values of a single type.

Allocated objects are destroyed all at once, when the arena itself is destroyed. There is no deallocation of individual objects while the arena itself is still alive. The flipside is that allocation is fast: typically just a vector push.

There is also a method into_vec() to recover ownership of allocated objects when the arena is no longer required, instead of destroying everything.

Example

use typed_arena::Arena;

struct Monster {
    level: u32,
}

let monsters = Arena::new();

let vegeta = monsters.alloc(Monster { level: 9001 });
assert!(vegeta.level > 9000);

Safe Cycles

All allocated objects get the same lifetime, so you can safely create cycles between them. This can be useful for certain data structures, such as graphs and trees with parent pointers.

use std::cell::Cell;
use typed_arena::Arena;

struct CycleParticipant<'a> {
    other: Cell<Option<&'a CycleParticipant<'a>>>,
}

let arena = Arena::new();

let a = arena.alloc(CycleParticipant { other: Cell::new(None) });
let b = arena.alloc(CycleParticipant { other: Cell::new(None) });

a.other.set(Some(b));
b.other.set(Some(a));

Alternatives

Need to allocate many different types of values?

Use multiple arenas if you have only a couple different types or try bumpalo, which is a bump-allocation arena can allocate heterogenous types of values.

Want allocation to return identifiers instead of references and dealing with references and lifetimes everywhere?

Check out id-arena or generational-arena.

Need to deallocate individual objects at a time?

Check out generational-arena for an arena-style crate or look for a more traditional allocator.

No runtime deps