#![allocator]
#![feature(allocator)]
#![feature(const_fn)]
#![no_std]
extern crate cortex_m;
extern crate linked_list_allocator;
use core::{ptr, cmp};
use cortex_m::interrupt::Mutex;
pub static HEAP: Mutex<Heap> = Mutex::new(Heap::empty());
pub struct Heap {
inner: linked_list_allocator::Heap,
}
impl Heap {
const fn empty() -> Self {
Heap { inner: linked_list_allocator::Heap::empty() }
}
pub unsafe fn init(&mut self, heap_bottom: usize, heap_size: usize) {
self.inner.init(heap_bottom, heap_size);
}
}
#[doc(hidden)]
#[no_mangle]
pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
HEAP.lock(|heap| {
heap.inner.allocate_first_fit(size, align).expect("out of memory")
})
}
#[doc(hidden)]
#[no_mangle]
pub extern "C" fn __rust_deallocate(ptr: *mut u8, size: usize, align: usize) {
HEAP.lock(|heap| unsafe { heap.inner.deallocate(ptr, size, align) });
}
#[doc(hidden)]
#[no_mangle]
pub extern "C" fn __rust_reallocate(ptr: *mut u8,
size: usize,
new_size: usize,
align: usize)
-> *mut u8 {
let new_ptr = __rust_allocate(new_size, align);
unsafe { ptr::copy(ptr, new_ptr, cmp::min(size, new_size)) };
__rust_deallocate(ptr, size, align);
new_ptr
}
#[doc(hidden)]
#[no_mangle]
pub extern "C" fn __rust_reallocate_inplace(_ptr: *mut u8,
size: usize,
_new_size: usize,
_align: usize)
-> usize {
size
}
#[doc(hidden)]
#[no_mangle]
pub extern "C" fn __rust_usable_size(size: usize, _align: usize) -> usize {
size
}