Crate heap1

Crate heap1 

Source
Expand description

§Heap1

CI Crates.io Docs.rs

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

  • std for unit test only
  • allocator-api for unstable allocator-api

Structs§

BoxedSlice
Heap
The simplest possible heap.
Inline
Pointer

Traits§

ConstStorage
Storage
Trait for providing access to the storage