Crate stable_arena

Crate stable_arena 

Source
Expand description

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 pointer bump.

This crate implements two kinds of arena.

§For types that need to be dropped: TypedArena

TypedArena is used like this:

use stable_arena::TypedArena;

let arena: TypedArena<Box<i32>> = TypedArena::default();
let x = arena.alloc(Box::new(42));
assert_eq!(**x, 42);

(Of course, storing a Box in an arena defeats the purpose of the arena, but you get the idea.)

A TypedArena can only hold objects of one type. It will call drop on all objects when the arena itself is dropped.

§For types that don’t need to be dropped: DroplessArena

The advantage of a DroplessArena is that it can hold objects of any type. The disadvantage is that it will not call drop on any of them when it is dropped.

It can be used like this:

use stable_arena::DroplessArena;

let arena = DroplessArena::default();
let x = arena.alloc(42);
assert_eq!(*x, 42);
let y = arena.alloc_str("hello");
assert_eq!(y, "hello");

You can also create reference cycles within a DroplessArena and it’s still perfectly safe; the memory will be freed when the arena is dropped.

use std::cell::Cell;
use stable_arena::DroplessArena;

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

let arena = DroplessArena::default();

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

§Features

  • The from-iter feature enables the alloc_from_iter method on both arenas. This feature is enabled by default.

Macros§

declare_arena
Declare an Arena containing one dropless arena and many typed arenas (the types of the typed arenas are specified by the arguments).

Structs§

DroplessArena
An arena that can hold objects of multiple different types that impl Copy and/or satisfy !mem::needs_drop.
IsCopy
IsNotCopy
TypedArena
An arena that can hold objects of only one type.