|
5 | 5 | //! ```
|
6 | 6 | //! // Plug in the allocator crate
|
7 | 7 | //! extern crate alloc_cortex_m;
|
8 |
| -//! extern crate collections; |
| 8 | +//! extern crate alloc; |
9 | 9 | //!
|
10 |
| -//! use collections::Vec; |
| 10 | +//! use alloc::Vec; |
11 | 11 | //! use alloc_cortex_m::CortexMHeap;
|
12 | 12 | //!
|
13 | 13 | //! #[global_allocator]
|
|
47 | 47 | #![no_std]
|
48 | 48 | #![feature(alloc, allocator_api)]
|
49 | 49 |
|
| 50 | +extern crate alloc; |
50 | 51 | extern crate cortex_m;
|
51 | 52 | extern crate linked_list_allocator;
|
52 |
| -extern crate alloc; |
53 | 53 |
|
54 |
| -use alloc::allocator::{Alloc, Layout, AllocErr}; |
| 54 | +use core::alloc::{GlobalAlloc, Layout, Opaque}; |
| 55 | +use core::ptr::NonNull; |
55 | 56 |
|
56 |
| -use linked_list_allocator::Heap; |
57 | 57 | use cortex_m::interrupt::Mutex;
|
| 58 | +use linked_list_allocator::Heap; |
58 | 59 |
|
59 | 60 | pub struct CortexMHeap {
|
60 | 61 | heap: Mutex<Heap>,
|
61 | 62 | }
|
62 | 63 |
|
63 | 64 | impl CortexMHeap {
|
64 |
| - |
65 | 65 | /// Crate a new UNINITIALIZED heap allocator
|
66 | 66 | ///
|
67 | 67 | /// You must initialize this heap using the
|
@@ -95,19 +95,21 @@ impl CortexMHeap {
|
95 | 95 | ///
|
96 | 96 | /// - This function must be called exactly ONCE.
|
97 | 97 | /// - `size > 0`
|
98 |
| - pub unsafe fn init(&self, start_addr: usize, size: usize){ |
| 98 | + pub unsafe fn init(&self, start_addr: usize, size: usize) { |
99 | 99 | self.heap.lock(|heap| heap.init(start_addr, size));
|
100 | 100 | }
|
101 | 101 | }
|
102 | 102 |
|
103 |
| -unsafe impl<'a> Alloc for &'a CortexMHeap { |
104 |
| - unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> { |
105 |
| - self.heap.lock(|heap| { |
106 |
| - heap.allocate_first_fit(layout) |
107 |
| - }) |
| 103 | +unsafe impl GlobalAlloc for CortexMHeap { |
| 104 | + unsafe fn alloc(&self, layout: Layout) -> *mut Opaque { |
| 105 | + self.heap |
| 106 | + .lock(|heap| heap.allocate_first_fit(layout)) |
| 107 | + .ok() |
| 108 | + .map_or(0 as *mut Opaque, |allocation| allocation.as_ptr()) |
108 | 109 | }
|
109 | 110 |
|
110 |
| - unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) { |
111 |
| - self.heap.lock(|heap| heap.deallocate(ptr, layout)); |
| 111 | + unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) { |
| 112 | + self.heap |
| 113 | + .lock(|heap| heap.deallocate(NonNull::new_unchecked(ptr), layout)); |
112 | 114 | }
|
113 | 115 | }
|
0 commit comments