#arena #typed #allocator #type

no-std typed-arena

The arena, a fast but limited type of allocator

7 stable releases

1.4.1 Jun 30, 2018
1.4.0 Jun 21, 2018
1.3.0 May 4, 2017
1.2.0 May 20, 2016
1.0.1 Apr 10, 2015

#4 in Memory management

Download history 1298/week @ 2018-09-16 1666/week @ 2018-09-23 1580/week @ 2018-09-30 1501/week @ 2018-10-07 1498/week @ 2018-10-14 1906/week @ 2018-10-21 1868/week @ 2018-10-28 1860/week @ 2018-11-04 1783/week @ 2018-11-11 1713/week @ 2018-11-18 1609/week @ 2018-11-25 1669/week @ 2018-12-02 1519/week @ 2018-12-09

5,475 downloads per month
Used in 78 crates (25 directly)

MIT license

18KB
268 lines

rust-typed-arena

Docs Status

The arena, a fast but limited type of allocator.

Arenas are a type of allocator that destroy the objects within, all at once, once the arena itself is destroyed. They do not support deallocation of individual objects while the arena itself is still alive. The benefit of an arena is very fast allocation; just a vector push.

This is an equivalent to arena::TypedArena distributed with rustc, but is available of Rust beta/stable.

It is probably slightly less efficient, but is simpler internally and uses much less unsafe code. It is based on a Vec<Vec<T>> instead of raw pointers and manual drops.

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


lib.rs:

The arena, a fast but limited type of allocator.

Arenas are a type of allocator that destroy the objects within, all at once, once the arena itself is destroyed. They do not support deallocation of individual objects while the arena itself is still alive. The benefit of an arena is very fast allocation; just a vector push.

This is an equivalent of the old arena::TypedArena type that was once distributed with nightly rustc but has since been removed.

It is slightly less efficient, but simpler internally and uses much less unsafe code. It is based on a Vec<Vec<T>> instead of raw pointers and manual drops.

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));

No runtime deps