Crate allocators [−] [src]
Custom memory allocators and utilities for using them.
Examples
#![feature(placement_in_syntax)] use std::io; use allocators::{Allocator, ScopedAllocator, OwningAllocator, ProxyAllocator}; #[derive(Debug)] struct Bomb(u8); impl Drop for Bomb { fn drop(&mut self) { println!("Boom! {}", self.0); } } // new scoped allocator with 4 kilobytes of memory. let alloc = ScopedAllocator::new(4 * 1024).unwrap(); alloc.scope(|inner| { let mut bombs = Vec::new(); // allocate makes the value on the stack first. for i in 0..100 { bombs.push(inner.allocate(Bomb(i)).unwrap())} // there's also in-place allocation! let bomb_101 = in inner.make_place().unwrap() { Bomb(101) }; // watch the bombs go off! }); // You can make allocators backed by other allocators. { let secondary_alloc = ScopedAllocator::new_from(&alloc, 1024).unwrap(); let mut val = secondary_alloc.allocate(0i32).unwrap(); *val = 1; } // Let's wrap our allocator in a proxy to log what it's doing. let proxied = ProxyAllocator::new(alloc, io::stdout()); let logged_allocation = proxied.allocate([0u8; 32]).unwrap();
Reexports
pub use composable::*; |
pub use scoped::ScopedAllocator; |
Modules
| composable | |
| scoped |
Structs
| Allocated |
An item allocated by a custom allocator. |
| Block |
A block of memory created by an allocator. |
| HeapAllocator |
Allocator stub that just forwards to heap allocation.
It is recommended to use the |
| Place |
A place for allocating into. This is only used for in-place allocation, e.g. let val = in (alloc.allocate().unwrap()) |
Enums
| AllocatorError |
Errors that can occur while creating an allocator or allocating from it. |
Constants
| HEAP |
Traits
| Allocator |
A custom memory allocator. |
| OwningAllocator |
An allocator that knows which blocks have been issued by it. |