use std::{
alloc::{GlobalAlloc, Layout, System},
cell::Cell,
};
thread_local! {
static ENABLED: Cell<bool> = const { Cell::new(false) };
static ALLOCATIONS: Cell<usize> = const { Cell::new(0) };
}
struct CountingAllocator;
#[global_allocator]
static ALLOCATOR: CountingAllocator = CountingAllocator;
fn record_allocation() {
let _ = ENABLED.try_with(|enabled| {
if enabled.get() {
let _ = ALLOCATIONS.try_with(|allocations| allocations.set(allocations.get() + 1));
}
});
}
unsafe impl GlobalAlloc for CountingAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
record_allocation();
unsafe { System.alloc(layout) }
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
record_allocation();
unsafe { System.alloc_zeroed(layout) }
}
unsafe fn realloc(&self, pointer: *mut u8, layout: Layout, size: usize) -> *mut u8 {
record_allocation();
unsafe { System.realloc(pointer, layout, size) }
}
unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) {
unsafe { System.dealloc(pointer, layout) }
}
}
pub(crate) fn count<T>(work: impl FnOnce() -> T) -> (T, usize) {
struct Reset;
impl Drop for Reset {
fn drop(&mut self) {
ENABLED.set(false);
}
}
assert!(!ENABLED.get(), "allocation measurement cannot be nested");
ALLOCATIONS.set(0);
ENABLED.set(true);
let reset = Reset;
let result = work();
drop(reset);
(result, ALLOCATIONS.get())
}