@@ -12,3 +12,48 @@ mod tlsf;
1212pub use llff:: Heap as LlffHeap ;
1313#[ cfg( feature = "tlsf" ) ]
1414pub use tlsf:: Heap as TlsfHeap ;
15+
16+ /// Initialize the global heap.
17+ ///
18+ /// This macro creates a static, uninitialized memory buffer of the specified size and
19+ /// initializes the heap instance with that buffer.
20+ ///
21+ /// # Parameters
22+ ///
23+ /// - `$heap:ident`: The identifier of the global heap instance to initialize.
24+ /// - `$size:expr`: An expression evaluating to a `usize` that specifies the size of the
25+ /// static memory buffer in bytes. It must be **greater than zero**.
26+ ///
27+ /// # Safety
28+ ///
29+ /// This macro must be called first, before any operations on the heap, and **only once**.
30+ /// It internally calls `Heap::init(...)` on the heap,
31+ /// so `Heap::init(...)` should not be called directly if this macro is used.
32+ ///
33+ /// # Example
34+ ///
35+ /// ```rust
36+ /// use cortex_m_rt::entry;
37+ /// use embedded_alloc::LlffHeap as Heap;
38+ ///
39+ /// #[global_allocator]
40+ /// static HEAP: Heap = Heap::empty();
41+ ///
42+ /// #[entry]
43+ /// fn main() -> ! {
44+ /// // Initialize the allocator BEFORE you use it
45+ /// unsafe {
46+ /// embedded_alloc::init!(HEAP, 1024);
47+ /// }
48+ /// let mut xs = Vec::new();
49+ /// // ...
50+ /// }
51+ /// ```
52+ #[ macro_export]
53+ macro_rules! init {
54+ ( $heap: ident, $size: expr) => {
55+ static mut HEAP_MEM : [ :: core:: mem:: MaybeUninit <u8 >; $size] =
56+ [ :: core:: mem:: MaybeUninit :: uninit( ) ; $size] ;
57+ $heap. init( & raw mut HEAP_MEM as usize , $size)
58+ } ;
59+ }
0 commit comments