Skip to content

Commit f751519

Browse files
committed
Use critical-section for heap locking.
1 parent e805a4a commit f751519

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ name = "alloc-cortex-m"
2222
version = "0.4.3"
2323

2424
[dependencies]
25-
cortex-m = "0.7.2"
25+
critical-section = "1.0"
2626

2727
[dependencies.linked_list_allocator]
2828
default-features = false
2929
version = "0.10.4"
3030
features = ["const_mut_refs"]
3131

3232
[dev-dependencies]
33-
cortex-m-rt = "0.6.12"
33+
cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
34+
cortex-m-rt = "0.7"

src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use core::alloc::{GlobalAlloc, Layout};
1212
use core::cell::RefCell;
1313
use core::ptr::{self, NonNull};
1414

15-
use cortex_m::interrupt::Mutex;
15+
use critical_section::Mutex;
1616
use linked_list_allocator::Heap;
1717

1818
pub struct CortexMHeap {
@@ -54,7 +54,7 @@ impl CortexMHeap {
5454
/// - This function must be called exactly ONCE.
5555
/// - `size > 0`
5656
pub unsafe fn init(&self, start_addr: usize, size: usize) {
57-
cortex_m::interrupt::free(|cs| {
57+
critical_section::with(|cs| {
5858
self.heap
5959
.borrow(cs)
6060
.borrow_mut()
@@ -64,18 +64,18 @@ impl CortexMHeap {
6464

6565
/// Returns an estimate of the amount of bytes in use.
6666
pub fn used(&self) -> usize {
67-
cortex_m::interrupt::free(|cs| self.heap.borrow(cs).borrow_mut().used())
67+
critical_section::with(|cs| self.heap.borrow(cs).borrow_mut().used())
6868
}
6969

7070
/// Returns an estimate of the amount of bytes available.
7171
pub fn free(&self) -> usize {
72-
cortex_m::interrupt::free(|cs| self.heap.borrow(cs).borrow_mut().free())
72+
critical_section::with(|cs| self.heap.borrow(cs).borrow_mut().free())
7373
}
7474
}
7575

7676
unsafe impl GlobalAlloc for CortexMHeap {
7777
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
78-
cortex_m::interrupt::free(|cs| {
78+
critical_section::with(|cs| {
7979
self.heap
8080
.borrow(cs)
8181
.borrow_mut()
@@ -86,7 +86,7 @@ unsafe impl GlobalAlloc for CortexMHeap {
8686
}
8787

8888
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
89-
cortex_m::interrupt::free(|cs| {
89+
critical_section::with(|cs| {
9090
self.heap
9191
.borrow(cs)
9292
.borrow_mut()

0 commit comments

Comments
 (0)