|
1 |
| -extern crate linked_list_allocator; |
2 |
| - |
3 |
| -use self::linked_list_allocator::Heap; |
4 | 1 | use crate::syscalls;
|
5 |
| -use core::alloc::Alloc; |
6 |
| -use core::alloc::GlobalAlloc; |
7 |
| -use core::alloc::Layout; |
| 2 | +use crate::ALLOCATOR; |
8 | 3 | use core::intrinsics;
|
9 | 4 | use core::ptr;
|
10 |
| -use core::ptr::NonNull; |
11 |
| - |
12 |
| -/// Memory allocation implementation for Tock applications. This must be |
13 |
| -/// instantiated in the crate root. |
14 |
| -pub(crate) struct TockAllocator; |
15 |
| - |
16 |
| -impl TockAllocator { |
17 |
| - // Retrieve access to the global linked_list_allocator::Heap instance. |
18 |
| - unsafe fn heap(&self) -> &mut Heap { |
19 |
| - static mut HEAP: Heap = Heap::empty(); |
20 |
| - &mut HEAP |
21 |
| - } |
22 |
| - |
23 |
| - /// Initializes an empty heap |
24 |
| - /// |
25 |
| - /// # Unsafety |
26 |
| - /// |
27 |
| - /// This function must be called at most once. The memory between [`heap_location`] and [`heap_top`] must not overlap with any other memory section. |
28 |
| - #[inline(never)] |
29 |
| - unsafe fn init(&mut self, heap_bottom: usize, heap_top: usize) { |
30 |
| - self.heap().init(heap_bottom, heap_top - heap_bottom); |
31 |
| - } |
32 |
| -} |
33 |
| - |
34 |
| -unsafe impl GlobalAlloc for TockAllocator { |
35 |
| - unsafe fn alloc(&self, layout: Layout) -> *mut u8 { |
36 |
| - self.heap().alloc(layout).unwrap().as_ptr() |
37 |
| - } |
38 |
| - |
39 |
| - unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { |
40 |
| - self.heap().dealloc(NonNull::new_unchecked(ptr), layout) |
41 |
| - } |
42 |
| -} |
43 | 5 |
|
44 | 6 | // _start and rust_start are the first two procedures executed when a Tock
|
45 | 7 | // application starts. _start is invoked directly by the Tock kernel; it
|
@@ -176,7 +138,7 @@ pub unsafe extern "C" fn rust_start(
|
176 | 138 |
|
177 | 139 | // Initialize the heap and tell the kernel where everything is. The heap is
|
178 | 140 | // placed between .bss and the end of application memory.
|
179 |
| - TockAllocator.init(bss_end, app_heap_break); |
| 141 | + ALLOCATOR.lock().init(bss_end, app_heap_break); |
180 | 142 | syscalls::memop(10, stack_top);
|
181 | 143 | syscalls::memop(11, bss_end);
|
182 | 144 |
|
|
0 commit comments