Trait allocators::Allocator
[−]
[src]
pub unsafe trait Allocator { unsafe fn allocate_raw(&self, size: usize, align: usize) -> Result<Block, AllocatorError>; unsafe fn deallocate_raw(&self, blk: Block); fn allocate<T>(&self, val: T) -> Result<Allocated<T, Self>, (AllocatorError, T)> where Self: Sized { ... } fn make_place<T>(&self) -> Result<Place<T, Self>, AllocatorError> where Self: Sized { ... } }
A custom memory allocator.
Required Methods
unsafe fn allocate_raw(&self, size: usize, align: usize) -> Result<Block, AllocatorError>
Attempt to allocate a block of memory.
Returns either a pointer to the block of memory allocated
or an Error. If size
is equal to 0, the pointer returned must
be equal to heap::EMPTY
Safety
Never use the pointer outside of the lifetime of the allocator. It must be deallocated with the same allocator as it was allocated with. It is undefined behavior to provide a non power-of-two align.
unsafe fn deallocate_raw(&self, blk: Block)
Deallocate the memory referred to by this pointer.
Safety
This block must have been allocated by this allocator.
Provided Methods
fn allocate<T>(&self, val: T) -> Result<Allocated<T, Self>, (AllocatorError, T)> where Self: Sized
Attempts to allocate the value supplied to it.
Examples
use allocators::{Allocator, Allocated}; fn alloc_array<A: Allocator>(allocator: &A) -> Allocated<[u8; 1000], A> { allocator.allocate([0; 1000]).ok().unwrap() }
fn make_place<T>(&self) -> Result<Place<T, Self>, AllocatorError> where Self: Sized
Attempts to create a place to allocate into.
For the general purpose, calling allocate
on the allocator is enough.
However, when you know the value you are allocating is too large
to be constructed on the stack, you should use in-place allocation.
Examples
#![feature(placement_in_syntax)] use allocators::{Allocator, Allocated}; fn alloc_array<A: Allocator>(allocator: &A) -> Allocated<[u8; 1000], A> { // if 1000 bytes were enough to smash the stack, this would still work. in allocator.make_place().unwrap() { [0; 1000] } }
Implementors
impl Allocator for NullAllocator
impl<M: BlockOwner, F: BlockOwner> Allocator for Fallback<M, F>
impl<A: Allocator, W: Write> Allocator for Proxy<A, W>
impl<'a, A: Allocator> Allocator for Scoped<'a, A>
impl Allocator for HeapAllocator