From cb35f1252942fa35b85998f0a82bf5a223354926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Quentin?= Date: Thu, 6 Apr 2023 17:01:29 +0200 Subject: [PATCH] Feature "big-heap", fix `calloc` (#155) --- esp-wifi/Cargo.toml | 1 + esp-wifi/src/common_adapter/mod.rs | 1 + esp-wifi/src/compat/malloc.rs | 14 +++++++++++--- esp-wifi/src/lib.rs | 17 ++++++++++++++--- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/esp-wifi/Cargo.toml b/esp-wifi/Cargo.toml index bcac61f76e8..a1b94a9edfd 100644 --- a/esp-wifi/Cargo.toml +++ b/esp-wifi/Cargo.toml @@ -66,3 +66,4 @@ ble = [ "esp32-hal?/bluetooth" ] phy-enable-usb = [] ps-min-modem = [] esp-now = [ "wifi", "embedded-svc" ] +big-heap = [] diff --git a/esp-wifi/src/common_adapter/mod.rs b/esp-wifi/src/common_adapter/mod.rs index cb73658a985..f49ed772e38 100644 --- a/esp-wifi/src/common_adapter/mod.rs +++ b/esp-wifi/src/common_adapter/mod.rs @@ -141,6 +141,7 @@ pub unsafe extern "C" fn semphr_give(semphr: *mut crate::binary::c_types::c_void ****************************************************************************/ #[allow(unused)] #[ram] +#[no_mangle] pub unsafe extern "C" fn random() -> crate::binary::c_types::c_ulong { trace!("random"); diff --git a/esp-wifi/src/compat/malloc.rs b/esp-wifi/src/compat/malloc.rs index 67794bad5a4..8c97db6e15e 100644 --- a/esp-wifi/src/compat/malloc.rs +++ b/esp-wifi/src/compat/malloc.rs @@ -17,7 +17,7 @@ pub unsafe extern "C" fn malloc(size: u32) -> *const u8 { }); if ptr.is_null() { - log::debug!("out of memory"); + log::warn!("Unable to allocate {} bytes", size); return ptr; } @@ -47,6 +47,14 @@ pub unsafe extern "C" fn free(ptr: *const u8) { pub unsafe extern "C" fn calloc(number: u32, size: u32) -> *const u8 { log::trace!("calloc {} {}", number, size); - let total_size = number * size + 4; - malloc(total_size) + let total_size = number * size; + let ptr = malloc(total_size) as *mut u8; + + if !ptr.is_null() { + for i in 0..total_size as isize { + ptr.offset(i).write_volatile(0); + } + } + + ptr } diff --git a/esp-wifi/src/lib.rs b/esp-wifi/src/lib.rs index 3570eb215a9..8f6e06e0ef0 100644 --- a/esp-wifi/src/lib.rs +++ b/esp-wifi/src/lib.rs @@ -86,17 +86,24 @@ pub fn current_millis() -> u64 { get_systimer_count() / (TICKS_PER_SECOND / 1000) } -#[cfg(not(coex))] +#[cfg(all(not(coex), not(feature = "big-heap")))] const HEAP_SIZE: usize = 64 * 1024; -#[cfg(coex)] +#[cfg(all(coex, not(feature = "big-heap")))] const HEAP_SIZE: usize = 64 * 1024; +#[cfg(all(not(coex), feature = "big-heap"))] +const HEAP_SIZE: usize = 100 * 1024; + +#[cfg(all(coex, feature = "big-heap"))] +const HEAP_SIZE: usize = 100 * 1024; + +#[cfg_attr(feature = "esp32", link_section = ".dram2_uninit")] static mut HEAP_DATA: [MaybeUninit; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; pub(crate) static HEAP: Mutex> = Mutex::new(RefCell::new(Heap::empty())); -pub fn init_heap() { +fn init_heap() { critical_section::with(|cs| { HEAP.borrow(cs) .borrow_mut() @@ -113,6 +120,8 @@ pub fn initialize( radio_clocks: hal::system::RadioClockControl, clocks: &Clocks, ) -> Result<(), InitializationError> { + init_heap(); + #[cfg(feature = "esp32c6")] if clocks.cpu_clock != MegahertzU32::MHz(160) { return Err(InitializationError::WrongClockConfig); @@ -188,6 +197,8 @@ pub fn initialize( radio_clocks: hal::system::RadioClockControl, clocks: &Clocks, ) -> Result<(), InitializationError> { + init_heap(); + if clocks.cpu_clock != MegahertzU32::MHz(240) { return Err(InitializationError::WrongClockConfig); }