1#![no_std]
12
13use core::alloc::{GlobalAlloc, Layout};
14
15pub struct EspIdfAllocator;
16
17const MALLOC_CAP_8BIT: u32 = 4;
18
19extern "C" {
20 fn heap_caps_malloc(size: usize, caps: u32) -> *mut core::ffi::c_void;
21 fn heap_caps_free(ptr: *mut core::ffi::c_void);
22 fn heap_caps_realloc(
23 ptr: *mut core::ffi::c_void,
24 size: usize,
25 caps: u32,
26 ) -> *mut core::ffi::c_void;
27}
28
29unsafe impl GlobalAlloc for EspIdfAllocator {
30 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
31 heap_caps_malloc(layout.size(), MALLOC_CAP_8BIT) as *mut u8
32 }
33
34 unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
35 heap_caps_free(ptr as *mut core::ffi::c_void)
36 }
37
38 unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 {
39 heap_caps_realloc(ptr as *mut core::ffi::c_void, new_size, MALLOC_CAP_8BIT) as *mut u8
40 }
41}