1#![no_std]
4#![deny(missing_docs)]
6
7extern crate alloc;
8
9use alloc::alloc::handle_alloc_error;
10use core::{
11 alloc::{GlobalAlloc, Layout},
12 ptr::NonNull,
13};
14use customizable_buddy::{BuddyAllocator, LinkedListBuddy, UsizeBuddy};
15
16#[inline]
24pub fn init(base_address: usize) {
25 unsafe {
28 HEAP.init(
29 core::mem::size_of::<usize>().trailing_zeros() as _,
30 NonNull::new(base_address as *mut u8).unwrap(),
31 )
32 };
33}
34
35#[inline]
45pub unsafe fn transfer(region: &'static mut [u8]) {
46 let ptr = NonNull::new(region.as_mut_ptr()).unwrap();
47 HEAP.transfer(ptr, region.len());
49}
50
51static mut HEAP: BuddyAllocator<21, UsizeBuddy, LinkedListBuddy> = BuddyAllocator::new();
61
62struct Global;
63
64#[global_allocator]
65static GLOBAL: Global = Global;
66
67unsafe impl GlobalAlloc for Global {
70 #[inline]
71 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
72 if let Ok((ptr, _)) = HEAP.allocate_layout::<u8>(layout) {
75 ptr.as_ptr()
76 } else {
77 handle_alloc_error(layout)
78 }
79 }
80
81 #[inline]
82 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
83 HEAP.deallocate_layout(NonNull::new(ptr).unwrap(), layout)
86 }
87}