Skip to content

Commit fc221c8

Browse files
authored
Merge pull request #79 from torfmaster/feature/allocator-api
Looks good to me.
2 parents 00197a6 + bb88445 commit fc221c8

File tree

2 files changed

+3
-42
lines changed

2 files changed

+3
-42
lines changed

src/entry_point.rs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,7 @@
1-
extern crate linked_list_allocator;
2-
3-
use self::linked_list_allocator::Heap;
41
use crate::syscalls;
5-
use core::alloc::Alloc;
6-
use core::alloc::GlobalAlloc;
7-
use core::alloc::Layout;
2+
use crate::ALLOCATOR;
83
use core::intrinsics;
94
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-
}
435

446
// _start and rust_start are the first two procedures executed when a Tock
457
// application starts. _start is invoked directly by the Tock kernel; it
@@ -176,7 +138,7 @@ pub unsafe extern "C" fn rust_start(
176138

177139
// Initialize the heap and tell the kernel where everything is. The heap is
178140
// 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);
180142
syscalls::memop(10, stack_top);
181143
syscalls::memop(11, bss_end);
182144

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![feature(
22
asm,
33
alloc,
4-
allocator_api,
54
alloc_error_handler,
65
core_intrinsics,
76
lang_items,
@@ -41,4 +40,4 @@ mod syscalls;
4140

4241
#[cfg(target_arch = "arm")]
4342
#[global_allocator]
44-
static ALLOCATOR: entry_point::TockAllocator = entry_point::TockAllocator;
43+
static ALLOCATOR: linked_list_allocator::LockedHeap = linked_list_allocator::LockedHeap::empty();

0 commit comments

Comments
 (0)