#[cfg(any(doc, feature = "alloc"))]
use alloc::{boxed::Box, rc::Rc, sync::Arc};
use core::{
alloc::{AllocErr, AllocInit, Layout, MemoryBlock, ReallocPlacement},
ptr::NonNull,
};
pub unsafe trait CallbackRef {
fn alloc(&self, layout: Layout, init: AllocInit, result: Result<MemoryBlock, AllocErr>);
fn dealloc(&self, ptr: NonNull<u8>, layout: Layout);
fn grow(
&self,
ptr: NonNull<u8>,
layout: Layout,
new_size: usize,
placement: ReallocPlacement,
init: AllocInit,
result: Result<MemoryBlock, AllocErr>,
);
fn shrink(
&self,
ptr: NonNull<u8>,
layout: Layout,
new_size: usize,
placement: ReallocPlacement,
result: Result<MemoryBlock, AllocErr>,
);
fn owns(&self, success: bool);
#[inline]
fn by_ref(&self) -> &Self {
self
}
}
unsafe impl<C: CallbackRef> CallbackRef for &C {
#[inline]
fn alloc(&self, layout: Layout, init: AllocInit, result: Result<MemoryBlock, AllocErr>) {
(**self).alloc(layout, init, result)
}
#[inline]
fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
(**self).dealloc(ptr, layout)
}
#[inline]
fn grow(
&self,
ptr: NonNull<u8>,
layout: Layout,
new_size: usize,
placement: ReallocPlacement,
init: AllocInit,
result: Result<MemoryBlock, AllocErr>,
) {
(**self).grow(ptr, layout, new_size, placement, init, result)
}
#[inline]
fn shrink(
&self,
ptr: NonNull<u8>,
layout: Layout,
new_size: usize,
placement: ReallocPlacement,
result: Result<MemoryBlock, AllocErr>,
) {
(**self).shrink(ptr, layout, new_size, placement, result)
}
#[inline]
fn owns(&self, success: bool) {
(**self).owns(success)
}
}
macro_rules! impl_alloc_stats {
($tt:tt) => {
#[cfg(any(doc, feature = "alloc"))]
#[cfg_attr(doc, doc(cfg(feature = "alloc")))]
unsafe impl<C: CallbackRef> CallbackRef for $tt<C> {
#[inline]
fn alloc(
&self,
layout: Layout,
init: AllocInit,
result: Result<MemoryBlock, AllocErr>,
) {
(**self).alloc(layout, init, result)
}
#[inline]
fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
(**self).dealloc(ptr, layout)
}
#[inline]
fn grow(
&self,
ptr: NonNull<u8>,
layout: Layout,
new_size: usize,
placement: ReallocPlacement,
init: AllocInit,
result: Result<MemoryBlock, AllocErr>,
) {
(**self).grow(ptr, layout, new_size, placement, init, result)
}
#[inline]
fn shrink(
&self,
ptr: NonNull<u8>,
layout: Layout,
new_size: usize,
placement: ReallocPlacement,
result: Result<MemoryBlock, AllocErr>,
) {
(**self).shrink(ptr, layout, new_size, placement, result)
}
#[inline]
fn owns(&self, success: bool) {
(**self).owns(success)
}
}
};
}
impl_alloc_stats!(Box);
impl_alloc_stats!(Rc);
impl_alloc_stats!(Arc);