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
- Safe Rust wrapper with optional
GlobalAllocimplementation
Installation
Add to your Cargo.toml:
[]
= "0.0.1"
For use as a global allocator:
[]
= { = "0.0.1", = ["global_alloc"] }
Usage
Basic Usage
use O1Heap;
use MaybeUninit;
// Create an aligned memory arena
;
static mut ARENA: Arena = Arena;
As Global Allocator
extern crate alloc;
use O1HeapGlobalAlloc;
use MaybeUninit;
;
static mut ARENA: Arena = Arena;
static HEAP: O1HeapGlobalAlloc = new;
Alignment
o1heap guarantees that all allocated memory is aligned to O1HEAP_ALIGNMENT:
- 32-bit systems: 16 bytes
- 64-bit systems: 32 bytes
This alignment is available as o1heap::ALIGNMENT.
Minimum Arena Size
The minimum arena size can be queried at runtime with o1heap::min_arena_size(). This is typically around 192 bytes on 32-bit systems.
Diagnostics
let diag = heap.diagnostics;
println!;
println!;
println!;
println!;
// Runtime invariant check
assert!;
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
This crate is licensed under the MIT license. The underlying o1heap C library is Copyright (c) Pavel Kirienko and also MIT licensed.
Credits
- o1heap by Pavel Kirienko