Skip to main content

singleton

Macro singleton 

Source
macro_rules! singleton {
    ($t:ty = $init:expr) => { ... };
}
Expand description

Hand out a unique &'static mut to a static, at most once per call site.

The initialiser must be a const expression: the value is a real static, living in .bss/.data like any other, so a multi-kilobyte arena costs no stack to create (passing such a buffer by value through an initialiser, as cell-based abstractions do, can overflow a small MCU stack before the move is elided). A second take of the same call site panics rather than aliasing the &mut.

§Panics

Panics on a second take of the same call site, and in any concurrent race all but one taker panics.

§Target requirements

The guard uses an atomic swap, available on Cortex-M3 and above (thumbv7/thumbv8); it is not available on thumbv6m (Cortex-M0/M0+).

let heap: &'static mut [u8; 1024] = iree_embedded::singleton!([u8; 1024] = [0; 1024]);
heap[0] = 1;