Crate esp_alloc

source ·
Expand description

A simple no_std heap allocator for RISC-V and Xtensa processors from Espressif. Supports all currently available ESP32 devices.

NOTE: using this as your global allocator requires using Rust 1.68 or greater, or the nightly release channel.

§Using this as your Global Allocator

To use EspHeap as your global allocator, you need at least Rust 1.68 or nightly.

#[global_allocator]
static ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty();

fn init_heap() {
    const HEAP_SIZE: usize = 32 * 1024;
    static mut HEAP: MaybeUninit<[u8; HEAP_SIZE]> = MaybeUninit::uninit();

    unsafe {
        ALLOCATOR.init(HEAP.as_mut_ptr() as *mut u8, HEAP_SIZE);
    }
}

§Using this with the nightly allocator_api-feature

Sometimes you want to have single allocations in PSRAM, instead of an esp’s DRAM. For that, it’s convenient to use the nightly allocator_api-feature, which allows you to specify an allocator for single allocations.

NOTE: To use this, you have to enable the create’s nightly feature flag.

Create and initialize an allocator to use in single allocations:

static PSRAM_ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty();

fn init_psram_heap() {
    unsafe {
        PSRAM_ALLOCATOR.init(psram::psram_vaddr_start() as *mut u8, psram::PSRAM_BYTES);
    }
}

And then use it in an allocation:

let large_buffer: Vec<u8, _> = Vec::with_capacity_in(1048576, &PSRAM_ALLOCATOR);

Modules§

  • Macros provided for convenience

Macros§

Structs§