use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
pub struct CountingAllocator;
static ALLOCS: AtomicUsize = AtomicUsize::new(0);
static BYTES: AtomicUsize = AtomicUsize::new(0);
unsafe impl GlobalAlloc for CountingAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ALLOCS.fetch_add(1, Relaxed);
BYTES.fetch_add(layout.size(), Relaxed);
unsafe { System.alloc(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe { System.dealloc(ptr, layout) }
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
ALLOCS.fetch_add(1, Relaxed);
BYTES.fetch_add(layout.size(), Relaxed);
unsafe { System.alloc_zeroed(layout) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
if new_size > layout.size() {
ALLOCS.fetch_add(1, Relaxed);
BYTES.fetch_add(new_size - layout.size(), Relaxed);
}
unsafe { System.realloc(ptr, layout, new_size) }
}
}
impl CountingAllocator {
pub fn reset() {
ALLOCS.store(0, Relaxed);
BYTES.store(0, Relaxed);
}
pub fn allocs() -> usize {
ALLOCS.load(Relaxed)
}
pub fn bytes() -> usize {
BYTES.load(Relaxed)
}
pub fn allocs_per(n: usize) -> f64 {
Self::allocs() as f64 / n as f64
}
}