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

alloc.scope(|inner| {
    let mut bombs = Vec::new();
    for i in 0..100 { bombs.push(inner.allocate(Bomb(i)).ok().unwrap())}
    // watch the bombs go off!
});
 
let my_int = alloc.allocate(23).ok().unwrap();
println!("My int: {}", *my_int);

Structs

Allocated

An item allocated by a custom allocator.

ScopedAllocator

A scoped linear allocator