Skip to content

Commit a0731d8

Browse files
authored
Merge pull request #12 from phil-opp/new_global_alloc_api
Use new global allocator API
2 parents 0f43808 + 7d5d728 commit a0731d8

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

Diff for: Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ version = "0.3.2"
1616

1717
[dependencies]
1818
cortex-m = "0.1.5"
19-
linked_list_allocator = "0.5.0"
19+
linked_list_allocator = "0.6.0"

Diff for: src/lib.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
//! ```
66
//! // Plug in the allocator crate
77
//! extern crate alloc_cortex_m;
8-
//! extern crate collections;
8+
//! extern crate alloc;
99
//!
10-
//! use collections::Vec;
10+
//! use alloc::Vec;
1111
//! use alloc_cortex_m::CortexMHeap;
1212
//!
1313
//! #[global_allocator]
@@ -47,21 +47,21 @@
4747
#![no_std]
4848
#![feature(alloc, allocator_api)]
4949

50+
extern crate alloc;
5051
extern crate cortex_m;
5152
extern crate linked_list_allocator;
52-
extern crate alloc;
5353

54-
use alloc::allocator::{Alloc, Layout, AllocErr};
54+
use core::alloc::{GlobalAlloc, Layout, Opaque};
55+
use core::ptr::NonNull;
5556

56-
use linked_list_allocator::Heap;
5757
use cortex_m::interrupt::Mutex;
58+
use linked_list_allocator::Heap;
5859

5960
pub struct CortexMHeap {
6061
heap: Mutex<Heap>,
6162
}
6263

6364
impl CortexMHeap {
64-
6565
/// Crate a new UNINITIALIZED heap allocator
6666
///
6767
/// You must initialize this heap using the
@@ -95,19 +95,21 @@ impl CortexMHeap {
9595
///
9696
/// - This function must be called exactly ONCE.
9797
/// - `size > 0`
98-
pub unsafe fn init(&self, start_addr: usize, size: usize){
98+
pub unsafe fn init(&self, start_addr: usize, size: usize) {
9999
self.heap.lock(|heap| heap.init(start_addr, size));
100100
}
101101
}
102102

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())
108109
}
109110

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));
112114
}
113115
}

0 commit comments

Comments
 (0)