Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Macros for more convenient usage (#16)
Browse files Browse the repository at this point in the history
* Macros for more convenient usage

* Remove unnecessary< fn, hide statics
  • Loading branch information
bjoernQ authored Nov 27, 2023
1 parent 0b2a8d3 commit 0de0725
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#![no_std]

pub mod macros;

use core::{
alloc::{GlobalAlloc, Layout},
cell::RefCell,
Expand Down
40 changes: 40 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//! 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();
static mut HEAP: core::mem::MaybeUninit<[u8; $size]> = core::mem::MaybeUninit::uninit();

unsafe {
ALLOCATOR.init(HEAP.as_mut_ptr() as *mut u8, $size);
}
}};
}

/// 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();

use $psram_module as _psram;
_psram::init_psram($peripheral);
unsafe {
ALLOCATOR.init(_psram::psram_vaddr_start() as *mut u8, _psram::PSRAM_BYTES);
}
}};
}

0 comments on commit 0de0725

Please sign in to comment.