4 releases (2 breaking)

0.6.0 Sep 2, 2024
0.5.1 Nov 5, 2023
0.5.0 Dec 8, 2022
0.0.0 Dec 6, 2022

#148 in Embedded development

Download history 4001/week @ 2024-07-30 4493/week @ 2024-08-06 3313/week @ 2024-08-13 4676/week @ 2024-08-20 5241/week @ 2024-08-27 5802/week @ 2024-09-03 3306/week @ 2024-09-10 4302/week @ 2024-09-17 5078/week @ 2024-09-24 4537/week @ 2024-10-01 3386/week @ 2024-10-08 4923/week @ 2024-10-15 4125/week @ 2024-10-22 4987/week @ 2024-10-29 4811/week @ 2024-11-05 4327/week @ 2024-11-12

19,433 downloads per month
Used in 15 crates (13 directly)

MIT/Apache

17KB
159 lines

crates.io crates.io Minimum Supported Rust Version - Documentation - Change log

embedded-alloc

A heap allocator for embedded systems.

Note that using this as your global allocator requires Rust 1.68 or later. (With earlier versions, you need the unstable feature #![feature(default_alloc_error_handler)])

This project is developed and maintained by the libs team.

Example

Starting with Rust 1.68, this crate can be used as a global allocator on stable Rust:

#![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
    {
        use core::mem::MaybeUninit;
        const HEAP_SIZE: usize = 1024;
        static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
        unsafe { HEAP.init(HEAP_MEM.as_ptr() 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.

Dependencies

~33–570KB
~11K SLoC