|
8 | 8 |
|
9 | 9 | #![no_std]
|
10 | 10 |
|
11 |
| -use core::cell::RefCell; |
12 | 11 | use core::alloc::{GlobalAlloc, Layout};
|
| 12 | +use core::cell::RefCell; |
13 | 13 | use core::ptr::NonNull;
|
14 | 14 |
|
15 | 15 | use cortex_m::interrupt::Mutex;
|
@@ -55,48 +55,39 @@ impl CortexMHeap {
|
55 | 55 | /// - `size > 0`
|
56 | 56 | pub unsafe fn init(&self, start_addr: usize, size: usize) {
|
57 | 57 | cortex_m::interrupt::free(|cs| {
|
58 |
| - self.heap |
59 |
| - .borrow(cs) |
60 |
| - .borrow_mut() |
61 |
| - .init(start_addr, size); |
| 58 | + self.heap.borrow(cs).borrow_mut().init(start_addr, size); |
62 | 59 | });
|
63 | 60 | }
|
64 | 61 |
|
65 | 62 | /// Returns an estimate of the amount of bytes in use.
|
66 | 63 | pub fn used(&self) -> usize {
|
67 |
| - cortex_m::interrupt::free(|cs| { |
68 |
| - self.heap |
69 |
| - .borrow(cs) |
70 |
| - .borrow_mut() |
71 |
| - .used() |
72 |
| - }) |
| 64 | + cortex_m::interrupt::free(|cs| self.heap.borrow(cs).borrow_mut().used()) |
73 | 65 | }
|
74 | 66 |
|
75 | 67 | /// Returns an estimate of the amount of bytes available.
|
76 | 68 | pub fn free(&self) -> usize {
|
77 |
| - cortex_m::interrupt::free(|cs| { |
78 |
| - self.heap |
79 |
| - .borrow(cs) |
80 |
| - .borrow_mut() |
81 |
| - .free() |
82 |
| - }) |
| 69 | + cortex_m::interrupt::free(|cs| self.heap.borrow(cs).borrow_mut().free()) |
83 | 70 | }
|
84 | 71 | }
|
85 | 72 |
|
86 | 73 | unsafe impl GlobalAlloc for CortexMHeap {
|
87 | 74 | unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
88 |
| - cortex_m::interrupt::free(|cs| self.heap |
89 |
| - .borrow(cs) |
90 |
| - .borrow_mut() |
91 |
| - .allocate_first_fit(layout) |
92 |
| - .ok() |
93 |
| - .map_or(0 as *mut u8, |allocation| allocation.as_ptr())) |
| 75 | + cortex_m::interrupt::free(|cs| { |
| 76 | + self.heap |
| 77 | + .borrow(cs) |
| 78 | + .borrow_mut() |
| 79 | + .allocate_first_fit(layout) |
| 80 | + .ok() |
| 81 | + .map_or(0 as *mut u8, |allocation| allocation.as_ptr()) |
| 82 | + }) |
94 | 83 | }
|
95 | 84 |
|
96 | 85 | unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
97 |
| - cortex_m::interrupt::free(|cs| self.heap |
98 |
| - .borrow(cs) |
99 |
| - .borrow_mut() |
100 |
| - .deallocate(NonNull::new_unchecked(ptr), layout)); |
| 86 | + cortex_m::interrupt::free(|cs| { |
| 87 | + self.heap |
| 88 | + .borrow(cs) |
| 89 | + .borrow_mut() |
| 90 | + .deallocate(NonNull::new_unchecked(ptr), layout) |
| 91 | + }); |
101 | 92 | }
|
102 | 93 | }
|
0 commit comments