Skip to content

mcu-rust/heap1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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<100>> = 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 = 100;
    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::<100>>::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

About

The simplest possible heap.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages