Struct maskerad_stack_allocator::StackAllocator [] [src]

pub struct StackAllocator { /* fields omitted */ }

A stack-based allocator.

It uses a RawVec to allocate bytes in a vector-like fashion and a pointer to its current top of the stack.

When instantiated, the top pointer is at the bottom of the stack. When an object is allocated in memory, a pointer to the current top of the stack is returned and the pointer to the current top of the stack is moved according to an offset.

This offset is calculated by the size of the object, its memory-alignment and an offset to align the object in memory.

This allocator is dropless: memory is never freed. It is assumed that is allocator is used in a game loop the following way : - the allocator is cleared - objects are allocated with the allocator - objects are consumed

Every object allocated at frame N is used at frame N, not N + 1. This way, even though memory is overridden, it is guaranteed that this overridden memory was not used.

Example

use maskerad_stack_allocator::StackAllocator;

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

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

let single_frame_allocator = StackAllocator::with_capacity(100); //100 bytes
let mut closed = false;

while !closed {
    //allocator cleared every frame.
    single_frame_allocator.reset();

    //...

    //allocate from the single frame allocator.
    //Be sure to use the data during this frame only!
    let my_monster = single_frame_allocator.alloc(Monster::default());

    assert_eq!(my_monster.level, 1);
    closed = true;
}

Methods

impl StackAllocator
[src]

[src]

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

Example

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

let allocator = StackAllocator::with_capacity(100);
assert_eq!(allocator.stack().cap(), 100);

[src]

Return an immutable reference to the stack used by the allocator.

[src]

Return an immutable reference to the current top of the stack.

Example

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

let allocator = StackAllocator::with_capacity(100);

// allocator.stack().ptr() return a pointer to the start of the allocation (the bottom of the stack).
assert_eq!(allocator.stack().ptr(), allocator.current_offset().get());

[src]

Move the pointer from the current top of the stack to the bottom of the stack.

Example

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

let allocator = StackAllocator::with_capacity(100);
let an_i32 = allocator.alloc(25);
assert_ne!(allocator.stack().ptr(), allocator.current_offset().get());

allocator.reset();
assert_eq!(allocator.stack().ptr(), allocator.current_offset().get());

[src]

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

Panics

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

Example

use maskerad_stack_allocator::StackAllocator;

let allocator = StackAllocator::with_capacity(100);

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