From 95cd5c5318285e448c3daa4ec738386c665e3f57 Mon Sep 17 00:00:00 2001 From: Sebastien Bechet Date: Thu, 4 Jun 2020 23:00:23 +0200 Subject: [PATCH] update to cortex-m 0.6.2 --- Cargo.toml | 3 ++- src/lib.rs | 26 ++++++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1ac0637..162dc1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ authors = [ "The Cortex-M Team ", "Jonathan Pallant ", "Jorge Aparicio ", + "Sébastien Béchet ", ] description = "A heap allocator for Cortex-M processors" @@ -21,7 +22,7 @@ name = "alloc-cortex-m" version = "0.3.5" [dependencies] -cortex-m = "0.1.5" +cortex-m = "0.6.2" [dependencies.linked_list_allocator] default-features = false diff --git a/src/lib.rs b/src/lib.rs index 3eff61b..2eb99ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ #![no_std] +use core::cell::RefCell; use core::alloc::{GlobalAlloc, Layout}; use core::ptr::NonNull; @@ -15,7 +16,7 @@ use cortex_m::interrupt::Mutex; use linked_list_allocator::Heap; pub struct CortexMHeap { - heap: Mutex, + heap: Mutex>, } impl CortexMHeap { @@ -25,7 +26,7 @@ impl CortexMHeap { /// [`init`](struct.CortexMHeap.html#method.init) method before using the allocator. pub const fn empty() -> CortexMHeap { CortexMHeap { - heap: Mutex::new(Heap::empty()), + heap: Mutex::new(RefCell::new(Heap::empty())), } } @@ -53,20 +54,29 @@ impl CortexMHeap { /// - This function must be called exactly ONCE. /// - `size > 0` pub unsafe fn init(&self, start_addr: usize, size: usize) { - self.heap.lock(|heap| heap.init(start_addr, size)); + cortex_m::interrupt::free(|cs| { + self.heap + .borrow(cs) + .borrow_mut() + .init(start_addr, size); + }); } } unsafe impl GlobalAlloc for CortexMHeap { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - self.heap - .lock(|heap| heap.allocate_first_fit(layout)) + cortex_m::interrupt::free(|cs| self.heap + .borrow(cs) + .borrow_mut() + .allocate_first_fit(layout) .ok() - .map_or(0 as *mut u8, |allocation| allocation.as_ptr()) + .map_or(0 as *mut u8, |allocation| allocation.as_ptr())) } unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { - self.heap - .lock(|heap| heap.deallocate(NonNull::new_unchecked(ptr), layout)); + cortex_m::interrupt::free(|cs| self.heap + .borrow(cs) + .borrow_mut() + .deallocate(NonNull::new_unchecked(ptr), layout)); } }