dorkhub

embedded-alloc

A heap allocator for embedded systems

rust-embedded
Rust46764 forksApache-2.0updated 4 months ago
git clone https://github.com/rust-embedded/embedded-alloc.gitrust-embedded/embedded-alloc

crates.io crates.io - Documentation - Change log

embedded-alloc

A heap allocator for embedded systems.

This project is developed and maintained by the libs team.

Example

#![no_std]
#![no_main]

extern crate alloc;

use cortex_m_rt::entry;
use embedded_alloc::LlffHeap as Heap;

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

#[entry]
fn main() -> ! {
    // Initialize the allocator BEFORE you use it
    unsafe {
        embedded_alloc::init!(HEAP, 1024);
    }
    // Alternatively, you can write the code directly to meet specific requirements.
    {
        use core::mem::MaybeUninit;
        const HEAP_SIZE: usize = 1024;
        static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
        unsafe { HEAP.init(&raw mut HEAP_MEM as usize, HEAP_SIZE) }
    }

    // now the allocator is ready types like Box, Vec can be used.

    loop { /* .. */ }
}

For a full usage example, see examples/global_alloc.rs.

For this to work, an implementation of critical-section must be provided.

For simple use cases with Cortex-M CPUs you may enable the critical-section-single-core feature in the cortex-m crate. Please refer to the documentation of critical-section for further guidance.

Features

There are two heaps available to use:

  • llff: Provides LlffHeap, a Linked List First Fit heap.
  • tlsf: Provides TlsfHeap, a Two-Level Segregated Fit heap.

The best heap to use will depend on your application, see #78 for more discussion.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Code of Conduct

Contribution to this crate is organized under the terms of the Rust Code of Conduct, the maintainer of this crate, the libs team, promises to intervene to uphold that code of conduct.

more like this

anne-key

Firmware for Anne Pro Keyboard written in Rust

Rust572

TachiSnap

TachiSnap — Pixel Snapper for animation pixel artists. Rust + WebAssembly client-side tool for cleaning up AI-generated…

HTML50

bit_gossip

Pathfinding library for calculating all node pairs' shortest paths in an unweighted undirected graph.

Rust50

search

search projects, people, and tags