Skip to content

Commit 1210730

Browse files
author
Jorge Aparicio
committed
tweak docs, hide HEAP
1 parent 465d65e commit 1210730

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

src/lib.rs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,39 @@
33
//! # Example
44
//!
55
//! ```
6-
//! // Plug in the allocator
6+
//! // Plug in the allocator crate
77
//! extern crate alloc_cortex_m;
88
//! extern crate collections;
99
//!
10-
//! use alloc_cortex_m;
1110
//! use collections::Vec;
1211
//!
13-
//! pub unsafe extern "C" fn reset_vector() {
14-
//! let heap_start: *mut usize = &mut _heap_start;
15-
//! let heap_end: *mut usize = &mut _heap_end;
12+
//! // These symbols come from a linker script
13+
//! extern "C" {
14+
//! static mut _heap_start: usize;
15+
//! static mut _heap_end: usize;
1616
//! }
1717
//!
1818
//! #[no_mangle]
1919
//! pub fn main() -> ! {
2020
//! // Initialize the heap BEFORE you use the allocator
21-
//! unsafe { alloc_cortex_m::init(heap_start, heap_end) }
21+
//! unsafe { alloc_cortex_m::init(&mut _heap_start, &mut _heap_end) }
2222
//!
2323
//! let mut xs = Vec::new();
2424
//! xs.push(1);
2525
//! // ...
2626
//! }
2727
//! ```
28+
//!
29+
//! And in your linker script, you might have something like:
30+
//!
31+
//! ``` text
32+
//! /* space reserved for the stack */
33+
//! _stack_size = 0x1000;
34+
//!
35+
//! /* `.` is right after the .bss and .data sections */
36+
//! _heap_start = .;
37+
//! _heap_end = ORIGIN(SRAM) + LENGTH(SRAM) - _stack_size;
38+
//! ```
2839
2940
#![allocator]
3041
#![feature(allocator)]
@@ -34,7 +45,7 @@
3445
extern crate cortex_m;
3546
extern crate linked_list_allocator;
3647

37-
use core::{ptr, cmp};
48+
use core::{cmp, ptr};
3849

3950
use linked_list_allocator::Heap;
4051
use cortex_m::interrupt::Mutex;
@@ -43,26 +54,32 @@ use cortex_m::interrupt::Mutex;
4354
///
4455
/// You must initialize this heap using the
4556
/// [`init`](struct.Heap.html#method.init) method before using the allocator.
46-
pub static HEAP: Mutex<Heap> = Mutex::new(Heap::empty());
57+
static HEAP: Mutex<Heap> = Mutex::new(Heap::empty());
4758

4859
/// Initializes the heap
4960
///
50-
/// This method must be called before you run any code that makes use of the
61+
/// This function must be called BEFORE you run any code that makes use of the
5162
/// allocator.
5263
///
53-
/// This method must be called exactly ONCE.
64+
/// `start_addr` is the address where the heap will be located.
65+
///
66+
/// `end_addr` points to the end of the heap.
67+
///
68+
/// Note that:
69+
///
70+
/// - The heap grows "upwards", towards larger addresses. Thus `end_addr` must
71+
/// be larger than `start_addr`
5472
///
55-
/// `start_addr` is the address where the heap will be located. Note that
56-
/// heap grows "upwards", towards larger addresses.
73+
/// - The size of the heap will actually be
74+
/// `(end_addr as usize) - (start_addr as usize) + 1` because the allocator
75+
/// won't use the byte at `end_addr`.
5776
///
58-
/// `end_addr` is the address just part the end of the heap.
77+
/// # Unsafety
5978
///
60-
/// In your linker script, you might have something like:
79+
/// Obey these or Bad Stuff will happen.
6180
///
62-
/// ```
63-
/// _heap_start = .;
64-
/// _heap_end = ORIGIN(SRAM) + LENGTH(SRAM) - _stack_size;
65-
/// ```
81+
/// - This function must be called exactly ONCE.
82+
/// - `end_addr` > `start_addr`
6683
pub unsafe fn init(start_addr: *mut usize, end_addr: *mut usize) {
6784
let start = start_addr as usize;
6885
let end = end_addr as usize;
@@ -76,7 +93,9 @@ pub unsafe fn init(start_addr: *mut usize, end_addr: *mut usize) {
7693
#[no_mangle]
7794
/// Rust allocation function (c.f. malloc)
7895
pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
79-
HEAP.lock(|heap| heap.allocate_first_fit(size, align).expect("out of memory"))
96+
HEAP.lock(|heap| {
97+
heap.allocate_first_fit(size, align).expect("out of memory")
98+
})
8099
}
81100

82101
/// Rust de-allocation function (c.f. free)

0 commit comments

Comments
 (0)