Skip to content

Commit b14f714

Browse files
committed
Update to new global allocator API
1 parent 0f43808 commit b14f714

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ version = "0.3.2"
1616

1717
[dependencies]
1818
cortex-m = "0.1.5"
19-
linked_list_allocator = "0.5.0"
19+
linked_list_allocator = "0.6.0"

src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ extern crate cortex_m;
5151
extern crate linked_list_allocator;
5252
extern crate alloc;
5353

54-
use alloc::allocator::{Alloc, Layout, AllocErr};
54+
use core::alloc::{GlobalAlloc, Layout, Opaque};
55+
use core::ptr::NonNull;
5556

5657
use linked_list_allocator::Heap;
5758
use cortex_m::interrupt::Mutex;
@@ -100,14 +101,14 @@ impl CortexMHeap {
100101
}
101102
}
102103

103-
unsafe impl<'a> Alloc for &'a CortexMHeap {
104-
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
104+
unsafe impl GlobalAlloc for CortexMHeap {
105+
unsafe fn alloc(&self, layout: Layout) -> *mut Opaque {
105106
self.heap.lock(|heap| {
106107
heap.allocate_first_fit(layout)
107-
})
108+
}).ok().map_or(0 as *mut Opaque, |allocation| allocation.as_ptr())
108109
}
109110

110-
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
111-
self.heap.lock(|heap| heap.deallocate(ptr, layout));
111+
unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) {
112+
self.heap.lock(|heap| heap.deallocate(NonNull::new_unchecked(ptr), layout));
112113
}
113114
}

0 commit comments

Comments
 (0)