Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ version = "0.3.2"

[dependencies]
cortex-m = "0.1.5"
linked_list_allocator = "0.5.0"
linked_list_allocator = "0.6.0"
30 changes: 16 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
//! ```
//! // Plug in the allocator crate
//! extern crate alloc_cortex_m;
//! extern crate collections;
//! extern crate alloc;
//!
//! use collections::Vec;
//! use alloc::Vec;
//! use alloc_cortex_m::CortexMHeap;
//!
//! #[global_allocator]
Expand Down Expand Up @@ -47,21 +47,21 @@
#![no_std]
#![feature(alloc, allocator_api)]

extern crate alloc;
extern crate cortex_m;
extern crate linked_list_allocator;
extern crate alloc;

use alloc::allocator::{Alloc, Layout, AllocErr};
use core::alloc::{GlobalAlloc, Layout, Opaque};
use core::ptr::NonNull;

use linked_list_allocator::Heap;
use cortex_m::interrupt::Mutex;
use linked_list_allocator::Heap;

pub struct CortexMHeap {
heap: Mutex<Heap>,
}

impl CortexMHeap {

/// Crate a new UNINITIALIZED heap allocator
///
/// You must initialize this heap using the
Expand Down Expand Up @@ -95,19 +95,21 @@ impl CortexMHeap {
///
/// - This function must be called exactly ONCE.
/// - `size > 0`
pub unsafe fn init(&self, start_addr: usize, size: usize){
pub unsafe fn init(&self, start_addr: usize, size: usize) {
self.heap.lock(|heap| heap.init(start_addr, size));
}
}

unsafe impl<'a> Alloc for &'a CortexMHeap {
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
self.heap.lock(|heap| {
heap.allocate_first_fit(layout)
})
unsafe impl GlobalAlloc for CortexMHeap {
unsafe fn alloc(&self, layout: Layout) -> *mut Opaque {
self.heap
.lock(|heap| heap.allocate_first_fit(layout))
.ok()
.map_or(0 as *mut Opaque, |allocation| allocation.as_ptr())
}

unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
self.heap.lock(|heap| heap.deallocate(ptr, layout));
unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) {
self.heap
.lock(|heap| heap.deallocate(NonNull::new_unchecked(ptr), layout));
}
}