Struct block_allocator::Allocator
[−]
[src]
pub struct Allocator<'a> { /* fields omitted */ }
Allocator
Provides fixed-sized buffers from a pre-allocated arena specified at creation
Current limitations: Max number of buffers it can produce is u32::MAX
- 1
Multiple allocators may be in use at any time, but their buffers may not be
used interchangibly :)
Note : This allocator will only produce blocks in sizes of powers of two. Any size requested that isn't a power of two will result in an error
Implementation This keeps track of the next available buffer in the slab using a Double CAS Treiber Stack Since Rust atomics don't actually support a double CAS yet, I am simulating it by CAS'ing on a single 64 bit value that is actually a [u32; 2], where the lower bits are the counter, and the higher order bits is the next offset
Example
use block_allocator::Allocator; //reserve 100 usable blocks of size 512 (-4) bytes let myalloc = Allocator::new(512, 100).unwrap(); let buf = myalloc.alloc().unwrap(); //do stuff myalloc.free(buf);
Methods
impl<'a> Allocator<'a>
[src]
fn new(block_size: u32, num_blocks: u32) -> Result<Allocator<'a>, AllocError>
Constructs a new Block Allocator
fn alloc(&self) -> Result<&'a mut [u8], AllocError>
Acquire the next free buffer from the allocator's slab
fn free(&self, buf: &'a mut [u8]) -> Result<(), AllocError>
Free the buffer back into the allocator's slab
unsafe fn alloc_raw(&self) -> Result<*mut u8, AllocError>
Acquire the next buffer as a raw std::u8
pointer from the allocator's slab
unsafe fn free_raw(&self, item: *mut u8) -> Result<(), AllocError>
Free a raw (previously alloc'd pointer) back into the allocator's slab