Trait allocators::Allocator
[−]
[src]
pub unsafe trait Allocator { unsafe fn allocate_raw(&self, size: usize, align: usize) -> Result<Block, Error>; unsafe fn reallocate_raw<'a>(&'a self, block: Block<'a>, new_size: usize) -> Result<Block<'a>, (Error, Block<'a>)>; unsafe fn deallocate_raw(&self, block: Block); fn allocate<T>(&self, val: T) -> Result<AllocBox<T, Self>, (Error, T)> where Self: Sized { ... } fn make_place<T>(&self) -> Result<Place<T, Self>, Error> where Self: Sized { ... } }
A custom memory allocator.
Required Methods
unsafe fn allocate_raw(&self, size: usize, align: usize) -> Result<Block, Error>
Attempt to allocate a block of memory.
Returns either a block of memory allocated
or an Error. If size
is equal to 0, the block returned must
be created by Block::empty()
Safety
Never use the block's 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 reallocate_raw<'a>(&'a self, block: Block<'a>, new_size: usize) -> Result<Block<'a>, (Error, Block<'a>)>
Reallocate a block of memory.
This either returns a new, possibly moved block with the requested size, or the old block back. The new block will have the same alignment as the old.
Safety
If given an empty block, it must return it back instead of allocating the new size, since the alignment is unknown.
If the requested size is 0, it must deallocate the old block and return an empty one.
unsafe fn deallocate_raw(&self, block: Block)
Deallocate the memory referred to by this block.
Safety
This block must have been allocated by this allocator.
Provided Methods
fn allocate<T>(&self, val: T) -> Result<AllocBox<T, Self>, (Error, T)> where Self: Sized
Attempts to allocate the value supplied to it.
Examples
use allocators::{Allocator, AllocBox}; fn alloc_array<A: Allocator>(allocator: &A) -> AllocBox<[u8; 1000], A> { allocator.allocate([0; 1000]).ok().unwrap() }
fn make_place<T>(&self) -> Result<Place<T, Self>, Error> 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, AllocBox}; fn alloc_array<A: Allocator>(allocator: &A) -> AllocBox<[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, L: ProxyLogger> Allocator for Proxy<A, L>
impl<'a, A: 'a + Allocator> Allocator for FreeList<'a, A>
impl<'a, A: Allocator> Allocator for Scoped<'a, A>
impl Allocator for HeapAllocator
impl<'a, A: ?Sized + Allocator + 'a> Allocator for Box<A>
impl<'a, 'b, A: ?Sized + Allocator + 'b> Allocator for &'a A
impl<'a, 'b, A: ?Sized + Allocator + 'b> Allocator for &'a mut A