|
3 | 3 | //! # Example
|
4 | 4 | //!
|
5 | 5 | //! ```
|
| 6 | +//! #![feature(alloc)] |
| 7 | +//! #![feature(global_allocator)] |
| 8 | +//! #![feature(lang_items)] |
| 9 | +//! |
6 | 10 | //! // Plug in the allocator crate
|
7 |
| -//! extern crate alloc_cortex_m; |
8 | 11 | //! extern crate alloc;
|
| 12 | +//! extern crate alloc_cortex_m; |
| 13 | +//! #[macro_use] |
| 14 | +//! extern crate cortex_m_rt as rt; // v0.5.x |
9 | 15 | //!
|
10 | 16 | //! use alloc::Vec;
|
11 | 17 | //! use alloc_cortex_m::CortexMHeap;
|
12 | 18 | //!
|
13 | 19 | //! #[global_allocator]
|
14 | 20 | //! static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
|
15 | 21 | //!
|
16 |
| -//! // These symbols come from a linker script |
17 |
| -//! extern "C" { |
18 |
| -//! static mut _sheap: u32; |
19 |
| -//! static mut _eheap: u32; |
20 |
| -//! } |
| 22 | +//! entry!(main); |
21 | 23 | //!
|
22 |
| -//! #[no_mangle] |
23 |
| -//! pub fn main() -> ! { |
24 |
| -//! // Initialize the heap BEFORE you use the allocator |
25 |
| -//! let start = unsafe { &mut _sheap as *mut u32 as usize }; |
26 |
| -//! let end = unsafe { &mut _sheap as *mut u32 as usize }; |
27 |
| -//! unsafe { ALLOCATOR.init(start, end - start) } |
| 24 | +//! fn main() -> ! { |
| 25 | +//! // Initialize the allocator BEFORE you use it |
| 26 | +//! let start = rt::heap_start() as usize; |
| 27 | +//! let size = 1024; // in bytes |
| 28 | +//! unsafe { ALLOCATOR.init(start, size) } |
28 | 29 | //!
|
29 | 30 | //! let mut xs = Vec::new();
|
30 | 31 | //! xs.push(1);
|
31 |
| -//! // ... |
| 32 | +//! |
| 33 | +//! loop { /* .. */ } |
32 | 34 | //! }
|
33 |
| -//! ``` |
34 | 35 | //!
|
35 |
| -//! And in your linker script, you might have something like: |
| 36 | +//! // required: define how Out Of Memory (OOM) conditions should be handled |
| 37 | +//! // *if* no other crate has already defined `oom` |
| 38 | +//! #[lang = "oom"] |
| 39 | +//! #[no_mangle] |
| 40 | +//! pub fn rust_oom() -> ! { |
| 41 | +//! // .. |
| 42 | +//! } |
36 | 43 | //!
|
37 |
| -//! ``` text |
38 |
| -//! /* space reserved for the stack */ |
39 |
| -//! _stack_size = 0x1000; |
40 | 44 | //!
|
41 |
| -//! /* `.` is right after the .bss and .data sections */ |
42 |
| -//! _heap_start = .; |
43 |
| -//! _heap_end = ORIGIN(SRAM) + LENGTH(SRAM) - _stack_size; |
| 45 | +//! // omitted: exception handlers |
44 | 46 | //! ```
|
45 | 47 |
|
46 | 48 | #![feature(alloc)]
|
|
0 commit comments