#![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") }
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
}
}
fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void {
unsafe {
if let Some(api) = crate::sys::API.as_ref() {
if let Some(f) = (*api.system).realloc {
return f(ptr, size);
}
}
}
panic!("realloc")
}