Skip to content

Commit 08fb0f9

Browse files
committed
Removed Heap wrapper
1 parent 8273c14 commit 08fb0f9

File tree

2 files changed

+23
-34
lines changed

2 files changed

+23
-34
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
authors = ["Jorge Aparicio <[email protected]>"]
2+
authors = ["Jorge Aparicio <[email protected]>", "Jonathan Pallant <[email protected]>"]
33
description = "A heap allocator for Cortex-M processors"
44
documentation = "https://docs.rs/alloc-cortex-m"
55
keywords = ["arm", "cortex-m", "allocator"]

src/lib.rs

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extern crate linked_list_allocator;
3131

3232
use core::{ptr, cmp};
3333

34+
use linked_list_allocator::Heap;
3435
use cortex_m::interrupt::Mutex;
3536

3637
/// A global UNINITIALIZED heap allocator
@@ -39,35 +40,25 @@ use cortex_m::interrupt::Mutex;
3940
/// [`init`](struct.Heap.html#method.init) method before using the allocator.
4041
pub static HEAP: Mutex<Heap> = Mutex::new(Heap::empty());
4142

42-
/// A heap allocator
43-
// NOTE newtype to hide all the other Heap methods
44-
pub struct Heap {
45-
inner: linked_list_allocator::Heap,
46-
}
47-
48-
impl Heap {
49-
const fn empty() -> Self {
50-
Heap { inner: linked_list_allocator::Heap::empty() }
51-
}
52-
53-
/// Initializes the heap
54-
///
55-
/// This method must be called before you run any code that makes use of the
56-
/// allocator.
57-
///
58-
/// This method must be called exactly ONCE.
59-
///
60-
/// `heap_bottom` is the address where the heap will be located. Note that
61-
/// heap grows "upwards", towards larger addresses.
62-
///
63-
/// `heap_size` is the size of the heap in bytes
64-
pub unsafe fn init(&mut self, heap_bottom: usize, heap_size: usize) {
65-
self.inner.init(heap_bottom, heap_size);
66-
}
67-
}
68-
69-
/// Init function
70-
pub unsafe fn alloc_init(start_addr: *mut usize, end_addr: *mut usize) {
43+
/// Initializes the heap
44+
///
45+
/// This method must be called before you run any code that makes use of the
46+
/// allocator.
47+
///
48+
/// This method must be called exactly ONCE.
49+
///
50+
/// `start_addr` is the address where the heap will be located. Note that
51+
/// heap grows "upwards", towards larger addresses.
52+
///
53+
/// `end_addr` is the address just part the end of the heap.
54+
///
55+
/// In your linker script, you might have something like:
56+
///
57+
/// ```
58+
/// _heap_start = .;
59+
/// _heap_end = ORIGIN(SRAM) + LENGTH(SRAM) - _stack_size;
60+
/// ```
61+
pub unsafe fn init(start_addr: *mut usize, end_addr: *mut usize) {
7162
let start = start_addr as usize;
7263
let end = end_addr as usize;
7364
let size = (end - start) - 1;
@@ -80,16 +71,14 @@ pub unsafe fn alloc_init(start_addr: *mut usize, end_addr: *mut usize) {
8071
#[no_mangle]
8172
/// Rust allocation function (c.f. malloc)
8273
pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
83-
HEAP.lock(|heap| {
84-
heap.inner.allocate_first_fit(size, align).expect("out of memory")
85-
})
74+
HEAP.lock(|heap| heap.allocate_first_fit(size, align).expect("out of memory"))
8675
}
8776

8877
/// Rust de-allocation function (c.f. free)
8978
#[doc(hidden)]
9079
#[no_mangle]
9180
pub extern "C" fn __rust_deallocate(ptr: *mut u8, size: usize, align: usize) {
92-
HEAP.lock(|heap| unsafe { heap.inner.deallocate(ptr, size, align) });
81+
HEAP.lock(|heap| unsafe { heap.deallocate(ptr, size, align) });
9382
}
9483

9584
/// Rust re-allocation function (c.f. realloc)

0 commit comments

Comments
 (0)