Crate scoped_allocator [] [src]

A scoped linear allocator. This is useful for reusing a block of memory for temporary allocations within a tight inner loop. Multiple nested scopes can be used if desired.

Examples

use scoped_allocator::{Allocator, ScopedAllocator};
struct Bomb(u8);
impl Drop for Bomb {
    fn drop(&mut self) {
        println!("Boom! {}", self.0);
    }
}
// new allocator with a kilobyte of memory.
let alloc = ScopedAllocator::new(1024).unwrap();

alloc.scope(|inner| {
    let mut bombs = Vec::new();
    // allocate_val makes the value on the stack first.
    for i in 0..100 { bombs.push(inner.allocate_val(Bomb(i)).ok().unwrap())}
    // watch the bombs go off!
});

let my_int = alloc.allocate_val(23).ok().unwrap();
println!("My int: {}", *my_int);

Structs

Allocated

An item allocated by a custom allocator.

HeapAllocator

Allocator stub that just forwards to heap allocation.

Place

A place for allocating into. This is only used for in-place allocation, e.g. let val = in (alloc.allocate().unwrap())

ScopedAllocator

A scoped linear allocator

Enums

AllocatorError

Errors that can occur while creating an allocator or allocating from it.

Traits

Allocator

A custom memory allocator.