use alloc::alloc::GlobalAlloc; use core::{ alloc::Layout, ops::{Deref, DerefMut}, }; use crate::stats_allocator::StatsAllocator; #[derive(Debug, Default)] pub struct Allocator(StatsAllocator); impl Allocator { pub const fn new(inner: A) -> Self { Allocator(StatsAllocator::new(inner)) } } unsafe impl GlobalAlloc for Allocator { unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 { let ptr = self.0.alloc(layout); log::trace!( "Alloc {:p}, layout = {:?}, stats = {:?}", ptr, layout, self.0.stats() ); ptr } unsafe fn dealloc(&self, ptr: *mut u8, layout: core::alloc::Layout) { log::trace!( "Free {:p}, layout = {:?}, stats = {:?}", ptr, layout, self.0.stats() ); self.0.dealloc(ptr, layout); } unsafe fn realloc( &self, ptr: *mut u8, layout: Layout, new_size: usize, ) -> *mut u8 { let new_ptr = self.0.realloc(ptr, layout, new_size); log::trace!( "Realloc {:p} to {} bytes at {:p}, layout = {:?}, stats = {:?}", ptr, new_size, new_ptr, layout, self.0.stats(), ); new_ptr } } impl Deref for Allocator { type Target = StatsAllocator; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for Allocator { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } }