Skip to content

Update to Cortex-M dependency #34

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

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- Bumped the dependency of the `linked_list_allocator` crate to v0.8.1.
- Bumped the dependency of the `cortex-m` crate to v0.6.2.
- Bumped the dependency of the `linked_list_allocator` crate to v0.8.4.
- Removed `#![feature(alloc)]` to supress compiler warning about stability for alloc

## [v0.3.5] - 2018-06-19
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ 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
version = "0.8.1"
version = "0.8.4"

[dev-dependencies]
cortex-m-rt = "0.6.12"
30 changes: 20 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
#![no_std]

use core::alloc::{GlobalAlloc, Layout};
use core::ptr::NonNull;
use core::{cell::RefCell, 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 +25,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 +53,30 @@ 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))
.ok()
.map_or(0 as *mut u8, |allocation| allocation.as_ptr())
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())
})
}

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);
});
}
}