From b14f714331f22c7b18942c997cf79783e38a0376 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Fri, 20 Apr 2018 11:52:10 +0200 Subject: [PATCH 1/3] Update to new global allocator API --- Cargo.toml | 2 +- src/lib.rs | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) 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..929a10a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,7 +51,8 @@ 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; @@ -100,14 +101,14 @@ impl CortexMHeap { } } -unsafe impl<'a> Alloc for &'a CortexMHeap { - unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> { +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)); } } From 4323d51874ffc5b440055ae6ae3fe93f4f79de2a Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Fri, 20 Apr 2018 11:55:15 +0200 Subject: [PATCH 2/3] Update documentation --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 929a10a..3bd9a1c 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] From 7d5d72823145c84991c3ada6a81ceeaff0a98321 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Fri, 20 Apr 2018 11:55:32 +0200 Subject: [PATCH 3/3] Run rustfmt --- src/lib.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3bd9a1c..fb1120f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,22 +47,21 @@ #![no_std] #![feature(alloc, allocator_api)] +extern crate alloc; extern crate cortex_m; extern crate linked_list_allocator; -extern crate alloc; 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 @@ -96,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 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()) + self.heap + .lock(|heap| heap.allocate_first_fit(layout)) + .ok() + .map_or(0 as *mut Opaque, |allocation| allocation.as_ptr()) } unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) { - self.heap.lock(|heap| heap.deallocate(NonNull::new_unchecked(ptr), layout)); + self.heap + .lock(|heap| heap.deallocate(NonNull::new_unchecked(ptr), layout)); } }