5 releases (2 stable)
Uses new Rust 2024
| 1.0.1 | Jan 19, 2026 |
|---|---|
| 0.1.2 | Jan 19, 2026 |
| 0.1.1 | Jan 19, 2026 |
| 0.1.0 | Jan 19, 2026 |
#223 in Memory management
12KB
203 lines
Heap1
The simplest possible heap. It's similar to heap1 in FreeRTOS.
Because it's the simplest implementation, it does NOT free memory. Any memory you drop cannot be reused (it's leaked), so avoid dropping anything whenever possible.
It is recommended that you use embedded-alloc. This crate is only intended for replacing heap-less modules.
Usage
cargo add heap1
Global Allocator
Using static global allocator:
use heap1::{Heap, Inline};
#[global_allocator]
static HEAP: Heap<Inline<4096>> = Heap::new();
You can also initialize the global allocator in two steps to meet specific requirements:
use core::mem::MaybeUninit;
use heap1::{Heap, Pointer};
#[global_allocator]
static HEAP: Heap<Pointer> = Heap::empty();
fn main() {
// Initialize the allocator BEFORE you use it
const HEAP_SIZE: usize = 4096;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init_with_ptr(&raw mut HEAP_MEM as usize, HEAP_SIZE) }
}
Local Allocator
Create a local allocator on stack.
use heap1::{Heap, Inline};
fn foo() {
let heap = Heap::<Inline::<64>>::new();
}
Create a local allocator from global heap.
use heap1::Heap;
fn foo() {
let heap = Heap::new_boxed(64);
}
Cargo Features
stdfor unit test onlyallocator-apifor unstable allocator-api
Dependencies
~1MB
~20K SLoC