#![cfg(not(test))]
extern crate alloc;
use core::alloc::{GlobalAlloc, Layout};
use core::ffi::c_void;
#[global_allocator]
#[cfg(feature = "allocator")]
pub static GLOBAL: PlaydateAllocator = PlaydateAllocator;
#[alloc_error_handler]
#[cfg(feature = "allocator")]
fn alloc_error(layout: Layout) -> ! { panic!("Out of Memory, requested {}.", layout.size()) }
pub struct PlaydateAllocator;
unsafe impl GlobalAlloc for PlaydateAllocator {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { realloc(core::ptr::null_mut(), layout.size()) as *mut u8 }
#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { realloc(ptr as *mut c_void, 0); }
#[inline]
unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 {
realloc(ptr as *mut c_void, new_size) as *mut u8
}
}
#[track_caller]
unsafe fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void {
static mut REALLOC: Option<unsafe extern "C" fn(ptr: *mut c_void, size: usize) -> *mut c_void> = None;
let f = REALLOC.get_or_insert_with(|| {
if let Some(realloc) = crate::sys::API.as_ref().and_then(|api| (*api.system).realloc) {
realloc
} else {
panic!("Missed API.realloc")
}
});
f(ptr, size)
}