Skip to content

Commit

Permalink
Feature "big-heap", fix calloc (esp-rs#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernQ committed May 23, 2024
1 parent bb177ca commit cb35f12
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ ble = [ "esp32-hal?/bluetooth" ]
phy-enable-usb = []
ps-min-modem = []
esp-now = [ "wifi", "embedded-svc" ]
big-heap = []
1 change: 1 addition & 0 deletions esp-wifi/src/common_adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
14 changes: 11 additions & 3 deletions esp-wifi/src/compat/malloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
}
17 changes: 14 additions & 3 deletions esp-wifi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];

pub(crate) static HEAP: Mutex<RefCell<Heap>> = Mutex::new(RefCell::new(Heap::empty()));

pub fn init_heap() {
fn init_heap() {
critical_section::with(|cs| {
HEAP.borrow(cs)
.borrow_mut()
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit cb35f12

Please sign in to comment.