1#![no_std]
2use core::alloc::GlobalAlloc;
3pub struct MosAllocator;
4
5extern "C" {
6 fn malloc(n: usize) -> *mut u8;
7 fn realloc(ptr: *mut u8, n: usize) -> *mut u8;
8 fn free(ptr: *mut u8);
9 fn __heap_bytes_free() -> usize;
10 fn __heap_bytes_used() -> usize;
11 fn __set_heap_limit(limit: usize);
12 fn __heap_limit() -> usize;
13}
14
15pub fn bytes_free() -> usize {
16 unsafe { __heap_bytes_free() }
17}
18pub fn bytes_used() -> usize {
19 unsafe { __heap_bytes_used() }
20}
21
22pub fn set_limit(limit: usize) {
23 unsafe { __set_heap_limit(limit) }
24}
25pub fn get_limit() -> usize {
26 unsafe { __heap_limit() }
27}
28
29unsafe impl GlobalAlloc for MosAllocator {
30 unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
31 malloc(layout.size())
32 }
33
34 unsafe fn dealloc(&self, ptr: *mut u8, _layout: core::alloc::Layout) {
35 free(ptr);
36 }
37
38 unsafe fn realloc(
39 &self,
40 ptr: *mut u8,
41 _layout: core::alloc::Layout,
42 new_size: usize,
43 ) -> *mut u8 {
44 realloc(ptr, new_size)
45 }
46}
47
48#[global_allocator]
49static ALLOCATOR: MosAllocator = MosAllocator;