Struct maskerad_memory_allocators::DoubleBufferedAllocator
[−]
[src]
pub struct DoubleBufferedAllocator { /* fields omitted */ }
A double-buffered allocator for data implementing the Drop trait.
This allocator is a wrapper around two StackAllocator. It works like a StackAllocator, and allows you to swap the buffers.
Example
use maskerad_memory_allocators::DoubleBufferedAllocator; struct Monster { hp :u32, level: u32, } impl Default for Monster { fn default() -> Self { Monster { hp: 1, level: 1, } } } impl Drop for Monster { fn drop(&mut self) { println!("I'm dying !"); } } let mut allocator = DoubleBufferedAllocator::with_capacity(100); //100 bytes. let mut closed = false; while !closed { //swap the active and inactive buffers of the allocator. allocator.swap_buffers(); //clear the newly active buffer. allocator.reset(); //allocate with the current buffer, leaving the data in the inactive buffer intact. //You can use this data during this frame, or the next frame. //After the next frame, the monster will be dropped and print "i'm dying!". let my_monster = allocator.alloc(|| { Monster::default() }).unwrap(); closed = true; }
Methods
impl DoubleBufferedAllocator
[src]
fn with_capacity(capacity: usize) -> Self
[src]
Create a DoubleBufferedAllocator with the given capacity (in bytes).
Example
#![feature(alloc)] use maskerad_memory_allocators::DoubleBufferedAllocator; let allocator = DoubleBufferedAllocator::with_capacity(100); assert_eq!(allocator.active_buffer().storage().borrow().capacity(), 100); assert_eq!(allocator.inactive_buffer().storage().borrow().capacity(), 100);
fn alloc<T, F>(&self, op: F) -> AllocationResult<&mut T> where
F: FnOnce() -> T,
[src]
F: FnOnce() -> T,
Allocates data in the active buffer.
Panic
This function will panic if the allocation exceeds the maximum storage capacity of the active allocator.
fn reset(&self)
[src]
Resets completely the active buffer, dropping all its content.
fn active_buffer(&self) -> &StackAllocator
[src]
fn inactive_buffer(&self) -> &StackAllocator
[src]
fn reset_to_marker(&self, marker: usize)
[src]
Resets partially the active buffer, dropping all the content lying between the marker and the first unused memory address.
fn marker(&self) -> usize
[src]
Returns the index of the first unused memory address in the active buffer.
fn swap_buffers(&mut self)
[src]
Swap the buffers. The inactive one becomes the active.