Crate stalloc

Source
Expand description

Stalloc (Stack + alloc) is a fast first-fit memory allocator. From my benchmarking, it can be over 3x as fast as the default OS allocator! This is because all memory is allocated from the stack, which allows it to avoid all OS overhead. Since it doesn’t rely on the OS (aside from SyncStalloc), this library is no_std compatible.

use stalloc::SyncStalloc;

// Create a global allocator with 1000 blocks, each 4 bytes in length.
#[global_allocator]
static GLOBAL: SyncStalloc<1000, 4> = SyncStalloc::new();

fn main() {
    // All of these allocations are being handled by the global `SyncStalloc` instance.
    let s1 = String::from("Hello");
    let s2 = String::from("world");
    let msg = format!("{s1}, {s2}!");

    assert!(!GLOBAL.is_oom());
    println!("Allocator state: {GLOBAL:?}");
}

Structs§

Align
A ZST with a given alignment. Align and Alignment are used to ensure that Block, and hence Stalloc, are aligned to a particular value. The definition of Block is:
Stalloc
A fast first-fit memory allocator.
SyncStalloc
A wrapper around UnsafeStalloc that is safe to create because it prevents data races using a Mutex. In comparison to UnsafeStalloc, the Mutex may cause a slight overhead.
UnsafeStalloc
A wrapper around Stalloc that implements Sync and GlobalAlloc.