You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]externcrate alloc;use cortex_m_rt::entry;use embedded_alloc::LlffHeapasHeap;#[global_allocator]staticHEAP:Heap = Heap::empty();#[entry]fnmain() -> ! {// Initialize the allocator BEFORE you use it{use core::mem::MaybeUninit;constHEAP_SIZE:usize = 1024;staticmutHEAP_MEM:[MaybeUninit<u8>;HEAP_SIZE] = [MaybeUninit::uninit();HEAP_SIZE];unsafe{HEAP.init(&rawmutHEAP_MEMasusize,HEAP_SIZE)}}// now the allocator is ready types like Box, Vec can be used.loop{/* .. */}}
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.
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.