Skip to content

Commit a1be51e

Browse files
bors[bot]nviennot
andauthored
Merge #53
53: Change usage example for statically allocated heap r=adamgreig a=nviennot The linker needs to know how big the heap is, otherwise, it could place some of the global variables in the heap region. This commit changes the example code to do that. Co-authored-by: Nicolas Viennot <nicolas@viennot.biz>
2 parents caa821e + a61b645 commit a1be51e

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)