use crate::alloc::Layout;
use core::sync::atomic::{AtomicPtr, Ordering};
use core::{mem, ptr};
static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
pub fn set_alloc_error_hook(hook: fn(Layout)) {
HOOK.store(hook as *mut (), Ordering::SeqCst);
}
pub fn take_alloc_error_hook() -> fn(Layout) {
let hook = HOOK.swap(ptr::null_mut(), Ordering::SeqCst);
if hook.is_null() {
default_alloc_error_hook
} else {
unsafe { mem::transmute(hook) }
}
}
fn default_alloc_error_hook(_layout: Layout) {
}
pub fn rust_oom(layout: Layout) -> ! {
let hook = HOOK.load(Ordering::SeqCst);
let hook: fn(Layout) = if hook.is_null() {
default_alloc_error_hook
} else {
unsafe { mem::transmute(hook) }
};
hook(layout);
loop {}
}