Skip to content

Commit 953c74c

Browse files
committed
update to cortex-m 0.6.2
1 parent 70b0765 commit 953c74c

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ keywords = [
1717
]
1818
license = "MIT OR Apache-2.0"
1919
name = "alloc-cortex-m"
20-
edition = '2018'
20+
edition = "2018"
2121
version = "0.3.5"
2222

23-
2423
[dependencies]
25-
cortex-m = "0.1.5"
24+
cortex-m = "0.6.2"
2625

2726
[dependencies.linked_list_allocator]
2827
default-features = false

src/lib.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! # Example
44
//!
55
//! ```
6-
//! #![feature(alloc)]
76
//! #![feature(global_allocator)]
87
//! #![feature(lang_items)]
98
//!
@@ -53,14 +52,15 @@ extern crate alloc;
5352
extern crate cortex_m;
5453
extern crate linked_list_allocator;
5554

55+
use core::cell::RefCell;
5656
use core::alloc::{GlobalAlloc, Layout};
5757
use core::ptr::NonNull;
5858

5959
use cortex_m::interrupt::Mutex;
6060
use linked_list_allocator::Heap;
6161

6262
pub struct CortexMHeap {
63-
heap: Mutex<Heap>,
63+
heap: Mutex<RefCell<Heap>>,
6464
}
6565

6666
impl CortexMHeap {
@@ -70,7 +70,7 @@ impl CortexMHeap {
7070
/// [`init`](struct.CortexMHeap.html#method.init) method before using the allocator.
7171
pub const fn empty() -> CortexMHeap {
7272
CortexMHeap {
73-
heap: Mutex::new(Heap::empty()),
73+
heap: Mutex::new(RefCell::new(Heap::empty())),
7474
}
7575
}
7676

@@ -98,20 +98,29 @@ impl CortexMHeap {
9898
/// - This function must be called exactly ONCE.
9999
/// - `size > 0`
100100
pub unsafe fn init(&self, start_addr: usize, size: usize) {
101-
self.heap.lock(|heap| heap.init(start_addr, size));
101+
cortex_m::interrupt::free(|cs| {
102+
self.heap
103+
.borrow(cs)
104+
.borrow_mut()
105+
.init(start_addr, size);
106+
});
102107
}
103108
}
104109

105110
unsafe impl GlobalAlloc for CortexMHeap {
106111
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
107-
self.heap
108-
.lock(|heap| heap.allocate_first_fit(layout))
109-
.ok()
110-
.map_or(0 as *mut u8, |allocation| allocation.as_ptr())
112+
cortex_m::interrupt::free(|cs| self.heap
113+
.borrow(cs)
114+
.borrow_mut()
115+
.allocate_first_fit(layout)
116+
.ok()
117+
.map_or(0 as *mut u8, |allocation| allocation.as_ptr()))
111118
}
112119

113120
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
114-
self.heap
115-
.lock(|heap| heap.deallocate(NonNull::new_unchecked(ptr), layout));
121+
cortex_m::interrupt::free(|cs| self.heap
122+
.borrow(cs)
123+
.borrow_mut()
124+
.deallocate(NonNull::new_unchecked(ptr), layout));
116125
}
117126
}

0 commit comments

Comments
 (0)