#![feature(const_fn)]
#![no_std]
#![feature(alloc, allocator_api)]
extern crate cortex_m;
extern crate linked_list_allocator;
extern crate alloc;
use alloc::allocator::{Alloc, Layout, AllocErr};
use linked_list_allocator::Heap;
use cortex_m::interrupt::Mutex;
pub struct CortexMHeap {
heap: Mutex<Heap>,
}
impl CortexMHeap {
pub const fn empty() -> CortexMHeap {
CortexMHeap {
heap: Mutex::new(Heap::empty()),
}
}
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 fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
self.heap.lock(|heap| heap.deallocate(ptr, layout));
}
}