diff --git a/Cargo.toml b/Cargo.toml index 59a7b71..0d64a39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,4 @@ version = "0.3.2" [dependencies] cortex-m = "0.1.5" -linked_list_allocator = "0.5.0" +linked_list_allocator = "0.6.0" diff --git a/src/lib.rs b/src/lib.rs index 9ab9630..fb1120f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,9 +5,9 @@ //! ``` //! // Plug in the allocator crate //! extern crate alloc_cortex_m; -//! extern crate collections; +//! extern crate alloc; //! -//! use collections::Vec; +//! use alloc::Vec; //! use alloc_cortex_m::CortexMHeap; //! //! #[global_allocator] @@ -47,21 +47,21 @@ #![no_std] #![feature(alloc, allocator_api)] +extern crate alloc; extern crate cortex_m; extern crate linked_list_allocator; -extern crate alloc; -use alloc::allocator::{Alloc, Layout, AllocErr}; +use core::alloc::{GlobalAlloc, Layout, Opaque}; +use core::ptr::NonNull; -use linked_list_allocator::Heap; use cortex_m::interrupt::Mutex; +use linked_list_allocator::Heap; pub struct CortexMHeap { heap: Mutex, } impl CortexMHeap { - /// Crate a new UNINITIALIZED heap allocator /// /// You must initialize this heap using the @@ -95,19 +95,21 @@ impl CortexMHeap { /// /// - This function must be called exactly ONCE. /// - `size > 0` - pub unsafe fn init(&self, start_addr: usize, size: usize){ + pub unsafe fn init(&self, start_addr: usize, size: usize) { self.heap.lock(|heap| heap.init(start_addr, size)); } } -unsafe impl<'a> Alloc for &'a CortexMHeap { - unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> { - self.heap.lock(|heap| { - heap.allocate_first_fit(layout) - }) +unsafe impl GlobalAlloc for CortexMHeap { + unsafe fn alloc(&self, layout: Layout) -> *mut Opaque { + self.heap + .lock(|heap| heap.allocate_first_fit(layout)) + .ok() + .map_or(0 as *mut Opaque, |allocation| allocation.as_ptr()) } - unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) { - self.heap.lock(|heap| heap.deallocate(ptr, layout)); + unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) { + self.heap + .lock(|heap| heap.deallocate(NonNull::new_unchecked(ptr), layout)); } }