@@ -31,6 +31,7 @@ extern crate linked_list_allocator;
31
31
32
32
use core:: { ptr, cmp} ;
33
33
34
+ use linked_list_allocator:: Heap ;
34
35
use cortex_m:: interrupt:: Mutex ;
35
36
36
37
/// A global UNINITIALIZED heap allocator
@@ -39,35 +40,25 @@ use cortex_m::interrupt::Mutex;
39
40
/// [`init`](struct.Heap.html#method.init) method before using the allocator.
40
41
pub static HEAP : Mutex < Heap > = Mutex :: new ( Heap :: empty ( ) ) ;
41
42
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 ) {
71
62
let start = start_addr as usize ;
72
63
let end = end_addr as usize ;
73
64
let size = ( end - start) - 1 ;
@@ -80,16 +71,14 @@ pub unsafe fn alloc_init(start_addr: *mut usize, end_addr: *mut usize) {
80
71
#[ no_mangle]
81
72
/// Rust allocation function (c.f. malloc)
82
73
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" ) )
86
75
}
87
76
88
77
/// Rust de-allocation function (c.f. free)
89
78
#[ doc( hidden) ]
90
79
#[ no_mangle]
91
80
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) } ) ;
93
82
}
94
83
95
84
/// Rust re-allocation function (c.f. realloc)
0 commit comments