From 960698146d81fa458c0c006e43e83e765f083eaa Mon Sep 17 00:00:00 2001 From: bjoernQ Date: Fri, 17 Nov 2023 12:17:26 +0100 Subject: [PATCH 1/2] Macros for more convenient usage --- src/lib.rs | 2 ++ src/macros.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/macros.rs diff --git a/src/lib.rs b/src/lib.rs index 69adbfd..4ff3667 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,8 @@ #![no_std] +pub mod macros; + use core::{ alloc::{GlobalAlloc, Layout}, cell::RefCell, diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..be76c00 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,51 @@ +//! Macros provided for convenience + +/// Create a heap allocator providing a heap of the given size in bytes +/// +/// You can only have ONE allocator at most +#[macro_export] +macro_rules! heap_allocator { + ($size:expr) => { + #[global_allocator] + static ALLOCATOR: $crate::EspHeap = $crate::EspHeap::empty(); + + fn init_heap() { + static mut HEAP: core::mem::MaybeUninit<[u8; $size]> = core::mem::MaybeUninit::uninit(); + + unsafe { + ALLOCATOR.init(HEAP.as_mut_ptr() as *mut u8, $size); + } + } + + init_heap(); + }; +} + +/// Create a heap allocator backed by PSRAM +/// +/// You can only have ONE allocator at most. You need a SoC which supports PSRAM and activate the feature to enable it. +/// You need to pass the PSRAM peripheral and the psram module path. +/// +/// # Usage +/// ```no_run +/// esp_alloc::psram_allocator!(peripherals.PSRAM, hal::psram); +/// ``` +#[macro_export] +macro_rules! psram_allocator { + ($peripheral:expr,$psram_module:path) => { + #[global_allocator] + static ALLOCATOR: $crate::EspHeap = $crate::EspHeap::empty(); + + fn init_psram_heap() { + use $psram_module as _psram; + unsafe { + ALLOCATOR.init(_psram::psram_vaddr_start() as *mut u8, _psram::PSRAM_BYTES); + } + } + { + use $psram_module as _psram; + _psram::init_psram($peripheral); + } + init_psram_heap(); + }; +} From ac03a0bc411482718fff633be62b93f4dc9086c1 Mon Sep 17 00:00:00 2001 From: bjoernQ Date: Mon, 27 Nov 2023 15:21:58 +0100 Subject: [PATCH 2/2] Remove unnecessary< fn, hide statics --- src/macros.rs | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index be76c00..04268bc 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -5,20 +5,15 @@ /// You can only have ONE allocator at most #[macro_export] macro_rules! heap_allocator { - ($size:expr) => { + ($size:expr) => {{ #[global_allocator] static ALLOCATOR: $crate::EspHeap = $crate::EspHeap::empty(); + static mut HEAP: core::mem::MaybeUninit<[u8; $size]> = core::mem::MaybeUninit::uninit(); - fn init_heap() { - static mut HEAP: core::mem::MaybeUninit<[u8; $size]> = core::mem::MaybeUninit::uninit(); - - unsafe { - ALLOCATOR.init(HEAP.as_mut_ptr() as *mut u8, $size); - } + unsafe { + ALLOCATOR.init(HEAP.as_mut_ptr() as *mut u8, $size); } - - init_heap(); - }; + }}; } /// Create a heap allocator backed by PSRAM @@ -32,20 +27,14 @@ macro_rules! heap_allocator { /// ``` #[macro_export] macro_rules! psram_allocator { - ($peripheral:expr,$psram_module:path) => { + ($peripheral:expr,$psram_module:path) => {{ #[global_allocator] static ALLOCATOR: $crate::EspHeap = $crate::EspHeap::empty(); - fn init_psram_heap() { - use $psram_module as _psram; - unsafe { - ALLOCATOR.init(_psram::psram_vaddr_start() as *mut u8, _psram::PSRAM_BYTES); - } - } - { - use $psram_module as _psram; - _psram::init_psram($peripheral); + use $psram_module as _psram; + _psram::init_psram($peripheral); + unsafe { + ALLOCATOR.init(_psram::psram_vaddr_start() as *mut u8, _psram::PSRAM_BYTES); } - init_psram_heap(); - }; + }}; }