Skip to main content

Crate taktora_bounded_alloc

Crate taktora_bounded_alloc 

Source
Expand description

Static pre-allocated bounded #[global_allocator] with hard caps on per-allocation size and total live block count.

See FEAT_0040 and REQ_0300..REQ_0304 for the design contract.

§Quick start

use taktora_bounded_alloc::declare_global_allocator;

// Declares a `#[global_allocator]` static named `ALLOC` with a
// 512 × 1024-byte arena. Maximum per-allocation size is 1024
// bytes; maximum live blocks is 512.
declare_global_allocator!(ALLOC, 512, 1024);

fn main() {
    let s = String::from("hello");
    assert_eq!(ALLOC.alloc_count(), 1);
    drop(s);
    assert_eq!(ALLOC.dealloc_count(), 1);
}

§Lock-after-init

Call ALLOC.lock() once the program reaches its steady-state point (e.g. after Executor::build). Any subsequent allocation call panics — which under panic = "abort" aborts the process, catching stray heap activity that escaped review.

ALLOC.lock();
let _ = String::from("this aborts the process");

The consuming binary’s Cargo.toml must set [profile.release] panic = "abort" (and [profile.dev] if dev builds also need the guarantee) — otherwise the panic itself will allocate the unwinder’s payload string.

Macros§

bounded_allocator
Construct a const-evaluable BoundedAllocator expression with the bitmap word count computed automatically. Useful in regular (non-global-allocator) static declarations and tests.
declare_global_allocator
Declare a static BoundedAllocator registered as #[global_allocator], computing the bitmap word count from MAX_BLOCKS automatically.

Structs§

BoundedAllocator
Static pre-allocated bounded global allocator.
CountingAllocator
#[global_allocator]-compatible wrapper around the system allocator that counts every successful allocation and deallocation while a thread-shared TRACKING flag is set.