use std::alloc::{GlobalAlloc, Layout, System};
pub struct CountingAllocator<A = System>(A);
impl CountingAllocator<System> {
pub const fn new() -> Self {
Self(System)
}
}
impl<A> CountingAllocator<A> {
pub const fn with(inner: A) -> Self {
Self(inner)
}
}
impl<A: Default> Default for CountingAllocator<A> {
fn default() -> Self {
Self(A::default())
}
}
unsafe impl<A> GlobalAlloc for CountingAllocator<A>
where
A: GlobalAlloc,
{
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
#[cfg(feature = "hotpath-alloc")]
crate::lib_on::functions::alloc::core::track_alloc(layout.size());
unsafe { self.0.alloc(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
#[cfg(feature = "hotpath-alloc")]
crate::lib_on::functions::alloc::core::track_dealloc(layout.size());
unsafe {
self.0.dealloc(ptr, layout);
}
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
#[cfg(feature = "hotpath-alloc")]
crate::lib_on::functions::alloc::core::track_alloc(layout.size());
unsafe { self.0.alloc_zeroed(layout) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
let new_ptr = unsafe { self.0.realloc(ptr, layout, new_size) };
#[cfg(feature = "hotpath-alloc")]
if !new_ptr.is_null() {
crate::lib_on::functions::alloc::core::track_dealloc(layout.size());
crate::lib_on::functions::alloc::core::track_alloc(new_size);
}
new_ptr
}
}