esp_idf_alloc/
lib.rs

1/*******************************************************************************
2 * Copyright (c) 2019 Jens Reimann
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License 2.0 which is available at
6 * http://www.eclipse.org/legal/epl-2.0
7 *
8 * SPDX-License-Identifier: EPL-2.0
9 *******************************************************************************/
10
11#![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}