From a61b6454f265b1894b33a5a72ae7334281e7e9fd Mon Sep 17 00:00:00 2001 From: Nicolas Viennot Date: Sun, 6 Feb 2022 13:49:54 -0500 Subject: [PATCH] Change usage example for statically allocated heap The linker needs to know how big the heap is, otherwise, it could place some of the global variables in the heap region. --- examples/global_alloc.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/global_alloc.rs b/examples/global_alloc.rs index b00f9e0..dcf42c5 100644 --- a/examples/global_alloc.rs +++ b/examples/global_alloc.rs @@ -16,9 +16,12 @@ static ALLOCATOR: CortexMHeap = CortexMHeap::empty(); #[entry] fn main() -> ! { // Initialize the allocator BEFORE you use it - let start = cortex_m_rt::heap_start() as usize; - let size = 1024; // in bytes - unsafe { ALLOCATOR.init(start, size) } + { + use core::mem::MaybeUninit; + const HEAP_SIZE: usize = 1024; + static mut HEAP: [MaybeUninit; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; + unsafe { ALLOCATOR.init((&mut HEAP).as_ptr() as usize, HEAP_SIZE) } + } let mut xs = Vec::new(); xs.push(1);