Skip to content

Commit 2444b77

Browse files
committed
Use critical-section for heap locking.
1 parent aa48d77 commit 2444b77

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ name = "alloc-cortex-m"
2222
version = "0.4.2"
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.8.11"
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

Lines changed: 6 additions & 6 deletions
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,25 +54,25 @@ 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.borrow(cs).borrow_mut().init(start_addr, size);
5959
});
6060
}
6161

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

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

7373
unsafe impl GlobalAlloc for CortexMHeap {
7474
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
75-
cortex_m::interrupt::free(|cs| {
75+
critical_section::with(|cs| {
7676
self.heap
7777
.borrow(cs)
7878
.borrow_mut()
@@ -83,7 +83,7 @@ unsafe impl GlobalAlloc for CortexMHeap {
8383
}
8484

8585
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
86-
cortex_m::interrupt::free(|cs| {
86+
critical_section::with(|cs| {
8787
self.heap
8888
.borrow(cs)
8989
.borrow_mut()

0 commit comments

Comments
 (0)