Skip to content

Commit

Permalink
update to cortex-m 0.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
sbechet committed Jun 4, 2020
1 parent dcae426 commit 95cd5c5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ authors = [
"The Cortex-M Team <cortex-m@teams.rust-embedded.org>",
"Jonathan Pallant <github@thejpster.org.uk>",
"Jorge Aparicio <jorge@japaric.io>",
"Sébastien Béchet <sebastien.bechet@osinix.com>",
]

description = "A heap allocator for Cortex-M processors"
Expand All @@ -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
Expand Down
26 changes: 18 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
#![no_std]

use core::cell::RefCell;
use core::alloc::{GlobalAlloc, Layout};
use core::ptr::NonNull;

use cortex_m::interrupt::Mutex;
use linked_list_allocator::Heap;

pub struct CortexMHeap {
heap: Mutex<Heap>,
heap: Mutex<RefCell<Heap>>,
}

impl CortexMHeap {
Expand All @@ -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())),
}
}

Expand Down Expand Up @@ -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));
}
}

0 comments on commit 95cd5c5

Please sign in to comment.