Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linked_list_allocator update for AllocRef and spinning_top #26

Merged
merged 1 commit into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
update to cortex-m 0.6.2
  • Loading branch information
sbechet committed Jun 4, 2020
commit 95cd5c5318285e448c3daa4ec738386c665e3f57
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));
}
}