use std::{
alloc::{GlobalAlloc, Layout, System},
cell::Cell,
};
thread_local! { static ENABLED: Cell<bool> = const { Cell::new(false) }; static COUNT: Cell<usize> = const { Cell::new(0) }; }
pub struct Counter;
fn count() {
if ENABLED.try_with(Cell::get).unwrap_or(false) {
let _ = COUNT.try_with(|n| n.set(n.get() + 1));
}
}
unsafe impl GlobalAlloc for Counter {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
count();
unsafe { System.alloc(layout) }
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
count();
unsafe { System.alloc_zeroed(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe { System.dealloc(ptr, layout) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, size: usize) -> *mut u8 {
count();
unsafe { System.realloc(ptr, layout, size) }
}
}
pub fn measure(f: impl FnOnce()) -> usize {
struct Guard;
impl Drop for Guard {
fn drop(&mut self) {
ENABLED.with(|v| v.set(false));
}
}
COUNT.with(|n| n.set(0));
ENABLED.with(|v| v.set(true));
let guard = Guard;
f();
drop(guard);
COUNT.with(Cell::get)
}