Skip to content

Commit a61b645

Browse files
committed
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.
1 parent caa821e commit a61b645

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

examples/global_alloc.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
1616
#[entry]
1717
fn main() -> ! {
1818
// Initialize the allocator BEFORE you use it
19-
let start = cortex_m_rt::heap_start() as usize;
20-
let size = 1024; // in bytes
21-
unsafe { ALLOCATOR.init(start, size) }
19+
{
20+
use core::mem::MaybeUninit;
21+
const HEAP_SIZE: usize = 1024;
22+
static mut HEAP: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
23+
unsafe { ALLOCATOR.init((&mut HEAP).as_ptr() as usize, HEAP_SIZE) }
24+
}
2225

2326
let mut xs = Vec::new();
2427
xs.push(1);

0 commit comments

Comments
 (0)