Skip to content

Commit 95cd5c5

Browse files
committed
update to cortex-m 0.6.2
1 parent dcae426 commit 95cd5c5

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ authors = [
33
"The Cortex-M Team <cortex-m@teams.rust-embedded.org>",
44
"Jonathan Pallant <github@thejpster.org.uk>",
55
"Jorge Aparicio <jorge@japaric.io>",
6+
"Sébastien Béchet <sebastien.bechet@osinix.com>",
67
]
78

89
description = "A heap allocator for Cortex-M processors"
@@ -21,7 +22,7 @@ name = "alloc-cortex-m"
2122
version = "0.3.5"
2223

2324
[dependencies]
24-
cortex-m = "0.1.5"
25+
cortex-m = "0.6.2"
2526

2627
[dependencies.linked_list_allocator]
2728
default-features = false

src/lib.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
99
#![no_std]
1010

11+
use core::cell::RefCell;
1112
use core::alloc::{GlobalAlloc, Layout};
1213
use core::ptr::NonNull;
1314

1415
use cortex_m::interrupt::Mutex;
1516
use linked_list_allocator::Heap;
1617

1718
pub struct CortexMHeap {
18-
heap: Mutex<Heap>,
19+
heap: Mutex<RefCell<Heap>>,
1920
}
2021

2122
impl CortexMHeap {
@@ -25,7 +26,7 @@ impl CortexMHeap {
2526
/// [`init`](struct.CortexMHeap.html#method.init) method before using the allocator.
2627
pub const fn empty() -> CortexMHeap {
2728
CortexMHeap {
28-
heap: Mutex::new(Heap::empty()),
29+
heap: Mutex::new(RefCell::new(Heap::empty())),
2930
}
3031
}
3132

@@ -53,20 +54,29 @@ impl CortexMHeap {
5354
/// - This function must be called exactly ONCE.
5455
/// - `size > 0`
5556
pub unsafe fn init(&self, start_addr: usize, size: usize) {
56-
self.heap.lock(|heap| heap.init(start_addr, size));
57+
cortex_m::interrupt::free(|cs| {
58+
self.heap
59+
.borrow(cs)
60+
.borrow_mut()
61+
.init(start_addr, size);
62+
});
5763
}
5864
}
5965

6066
unsafe impl GlobalAlloc for CortexMHeap {
6167
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
62-
self.heap
63-
.lock(|heap| heap.allocate_first_fit(layout))
68+
cortex_m::interrupt::free(|cs| self.heap
69+
.borrow(cs)
70+
.borrow_mut()
71+
.allocate_first_fit(layout)
6472
.ok()
65-
.map_or(0 as *mut u8, |allocation| allocation.as_ptr())
73+
.map_or(0 as *mut u8, |allocation| allocation.as_ptr()))
6674
}
6775

6876
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
69-
self.heap
70-
.lock(|heap| heap.deallocate(NonNull::new_unchecked(ptr), layout));
77+
cortex_m::interrupt::free(|cs| self.heap
78+
.borrow(cs)
79+
.borrow_mut()
80+
.deallocate(NonNull::new_unchecked(ptr), layout));
7181
}
7282
}

0 commit comments

Comments
 (0)