o1heap

Crates.ioo1heap
lib.rso1heap
version0.0.2
created_at2025-11-25 20:12:48.623698+00
updated_at2025-11-27 00:02:40.499455+00
descriptionRust bindings for o1heap - a constant-time deterministic memory allocator
homepage
repositoryhttps://github.com/laktoosivaba/o1heap-rs
max_upload_size
id1950392
size1,029,026
Andrey Shinkarev (laktoosivaba)

documentation

https://docs.rs/o1heap

README

o1heap-rs

Rust bindings for o1heap - a constant-time deterministic memory allocator for hard real-time systems.

Features

  • Constant-time O(1) allocation and deallocation
  • Deterministic behavior suitable for real-time systems
  • No-std compatible - designed for embedded systems
  • Bounded fragmentation - predictable memory usage

Installation

[dependencies]
o1heap = "0.0.2"

Usage

As Global Allocator (STM32)

#![no_std]
#![no_main]

extern crate alloc;

use alloc::vec::Vec;
use core::mem::{size_of, MaybeUninit};
use core::ptr::addr_of_mut;
use cortex_m_rt::entry;
use o1heap::O1Heap;
use panic_halt as _;

#[repr(C, align(16))]
struct Arena([MaybeUninit<u8>; 8192]);
static mut ARENA: Arena = Arena([MaybeUninit::uninit(); 8192]);

#[global_allocator]
static HEAP: O1Heap = O1Heap::empty();

#[entry]
fn main() -> ! {
    unsafe {
        HEAP.init(addr_of_mut!(ARENA).cast(), size_of::<Arena>())
            .expect("heap init failed");
    }

    let mut xs = Vec::new();
    xs.push(1);

    loop { }
}

Alignment

o1heap guarantees that all allocated memory is aligned to:

  • 32-bit systems: 16 bytes
  • 64-bit systems: 32 bytes

This alignment is available as o1heap::ALIGNMENT.

Diagnostics

let diag = heap.diagnostics();
println!("Capacity: {}", diag.capacity);
println!("Allocated: {}", diag.allocated);
println!("Peak allocated: {}", diag.peak_allocated);
println!("OOM count: {}", diag.oom_count);

assert!(heap.invariants_hold());

Thread Safety

o1heap is not thread-safe. If you need to use it from multiple threads or interrupt contexts, you must provide external synchronization (e.g., critical sections, mutexes).

License

MIT license. The underlying o1heap C library is also MIT licensed.

Commit count: 0

cargo fmt