Struct maskerad_stack_allocator::DoubleBufferedAllocator [] [src]

pub struct DoubleBufferedAllocator { /* fields omitted */ }

A double-buffered allocator.

This allocator is a wrapper around two StackAllocator. It works like a StackAllocator, and allows you to swap the buffers.

Useful if you want to use data created at frame N during frame N + 1.

Example

use maskerad_stack_allocator::DoubleBufferedAllocator;

struct Monster {
    hp :u32,
    level: u32,
}

impl Default for Monster {
    fn default() -> Self {
        Monster {
        hp: 1,
        level: 1,
        }
    }
}

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_current();

    //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.
    let my_monster: &mut Monster = allocator.alloc(Monster::default());

    closed = true;
}

Methods

impl DoubleBufferedAllocator
[src]

[src]

Create a DoubleBufferedAllocator with the given capacity (in bytes).

Example

#![feature(alloc)]
use maskerad_stack_allocator::DoubleBufferedAllocator;

let allocator = DoubleBufferedAllocator::with_capacity(100);

assert_eq!(allocator.active_buffer().stack().cap(), 100);
assert_eq!(allocator.inactive_buffer().stack().cap(), 100);

[src]

Return an immutable reference to the active StackAllocator.

[src]

Return an immutable reference to the inactive StackAllocator.

[src]

Reset the pointer of the active StackAllocator, from the current top of its stack to the bottom of its stack.

Example

#![feature(alloc)]
use maskerad_stack_allocator::DoubleBufferedAllocator;

let allocator = DoubleBufferedAllocator::with_capacity(100);

let my_i32 = allocator.alloc(26);
assert_eq!(allocator.inactive_buffer().stack().ptr(), allocator.inactive_buffer().current_offset().get());
assert_ne!(allocator.active_buffer().stack().ptr(), allocator.active_buffer().current_offset().get());

allocator.reset_current();
assert_eq!(allocator.inactive_buffer().stack().ptr(), allocator.inactive_buffer().current_offset().get());
assert_eq!(allocator.active_buffer().stack().ptr(), allocator.active_buffer().current_offset().get());

[src]

Swap the buffers. The inactive one becomes the active.

[src]

Allocate data in the active allocator's memory, from the current top of its stack.

Panics

This function will panic if the current length of the active allocator + the size of the allocated object exceed the allocator's capacity.

Example

use maskerad_stack_allocator::DoubleBufferedAllocator;

let allocator = DoubleBufferedAllocator::with_capacity(100);

let my_i32 = allocator.alloc(26);
assert_eq!(my_i32, &mut 26);