Crate static_alloc

source ·
Expand description

General purpose global allocator(s) with static, inline storage.

Provides an allocator for extremely resource constrained environments where the only memory guaranteed is your program’s image in memory as provided by the loader. Possible use cases are OS-less development, embedded, bootloaders (even stage0/1).

The allocator does not support meaningful deallocation but the whole allocator itself can be reset by mutable reference. This is useful for a local, single thread allocator. It’s recommended to use the global instance to split resources once at startup and then utilize multiple local allocators for actual working memory. See doc for some use case studies with examples.

§Usage – Global allocator

use static_alloc::Bump;

#[global_allocator]
static A: Bump<[u8; 1 << 16]> = Bump::uninit();

fn main() {
    let v = vec![0xdeadbeef_u32; 128];
    println!("{:x?}", v);

    let buffer: &'static mut [u32; 128] = A.leak([0; 128])
        .unwrap_or_else(|_| panic!("Runtime allocated before main"));
}

You can find more detailed examples in the doc module:

§Why the name?

This crates makes it safe to define a static object and to then use its memory to allocate dynamic values without accidentally exposing or using uninitialized memory. This allows obtaining &'static mut T instances which is handy if a struct requires a mutable reference but it is also required that this struct has 'static lifetime bounds itself.

Re-exports§

Modules§

  • The bump allocator.
  • A module containing extended documentation and examples.
  • This module contains an owning wrapper of a leaked struct.
  • An unsynchronized allocator.