Skip to content

Commit 813370f

Browse files
bors[bot]japaric
andcommitted
Merge #15
15: show how to define the `oom` lang item r=japaric a=japaric Co-authored-by: Jorge Aparicio <[email protected]>
2 parents 0e9e718 + 1578d31 commit 813370f

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/lib.rs

+23-21
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,46 @@
33
//! # Example
44
//!
55
//! ```
6+
//! #![feature(alloc)]
7+
//! #![feature(global_allocator)]
8+
//! #![feature(lang_items)]
9+
//!
610
//! // Plug in the allocator crate
7-
//! extern crate alloc_cortex_m;
811
//! extern crate alloc;
12+
//! extern crate alloc_cortex_m;
13+
//! #[macro_use]
14+
//! extern crate cortex_m_rt as rt; // v0.5.x
915
//!
1016
//! use alloc::Vec;
1117
//! use alloc_cortex_m::CortexMHeap;
1218
//!
1319
//! #[global_allocator]
1420
//! static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
1521
//!
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);
2123
//!
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) }
2829
//!
2930
//! let mut xs = Vec::new();
3031
//! xs.push(1);
31-
//! // ...
32+
//!
33+
//! loop { /* .. */ }
3234
//! }
33-
//! ```
3435
//!
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+
//! }
3643
//!
37-
//! ``` text
38-
//! /* space reserved for the stack */
39-
//! _stack_size = 0x1000;
4044
//!
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
4446
//! ```
4547
4648
#![feature(alloc)]

0 commit comments

Comments
 (0)