Crate maskerad_memory_allocators [] [src]

This library provides:

  • a stack-based allocator.

  • a double-buffered allocator.

Its primary purpose is to prevent memory fragmentation.

This is a nightly-only library (last rust nightly version tested: 1.25).

Example

A StackAllocator can be used as a "one-frame" buffer, for example.

In a loop, at the beginning, the allocator is reset and data is pushed into it. Before the end of the loop, this data is consumed.

Rinse and repeat.

use maskerad_memory_allocators::StackAllocator;
//100 bytes for data implementing Drop, 100 bytes for data implementing Copy.
let single_frame_allocator = StackAllocator::with_capacity(100, 100);
let mut closed = false;

while !closed {
    // The allocator is cleared every frame.
    // Everything is dropped.
    single_frame_allocator.reset();


    //allocate from the single frame allocator.
    //Be sure to use the data during this frame only!
    let my_vec: &Vec<u8> = single_frame_allocator.alloc(|| {
        Vec::with_capacity(10)
    })?;

    //Use this data before the loop ends.

    closed = true;
}

A DoubleBufferedAllocator can be used as a "two-frames" buffer.

use maskerad_memory_allocators::DoubleBufferedAllocator;
//100 bytes for data implementing the Drop trait, 100 bytes for data implementing the `Copy` trait.
let mut allocator = DoubleBufferedAllocator::with_capacity(100, 100);
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.
    let my_vec: &Vec<u8> = allocator.alloc(|| {
        Vec::with_capacity(10)
    })?;

    assert!(my_vec.is_empty());

    closed = true;
}

Modules

allocation_error

Structs

DoubleBufferedAllocator

A double-buffered allocator.

StackAllocator

A stack-based allocator.