Skip to main content

nexus_core/
alloc_counter.rs

1use std::alloc::{GlobalAlloc, Layout, System};
2use std::sync::atomic::{AtomicU64, Ordering};
3
4pub struct CountingAllocator;
5
6static ALLOCATION_EVENTS: AtomicU64 = AtomicU64::new(0);
7
8#[global_allocator]
9static GLOBAL_ALLOCATOR: CountingAllocator = CountingAllocator;
10
11unsafe impl GlobalAlloc for CountingAllocator {
12    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
13        ALLOCATION_EVENTS.fetch_add(1, Ordering::Relaxed);
14        unsafe { System.alloc(layout) }
15    }
16
17    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
18        ALLOCATION_EVENTS.fetch_add(1, Ordering::Relaxed);
19        unsafe { System.alloc_zeroed(layout) }
20    }
21
22    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
23        ALLOCATION_EVENTS.fetch_add(1, Ordering::Relaxed);
24        unsafe { System.realloc(ptr, layout, new_size) }
25    }
26
27    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
28        unsafe { System.dealloc(ptr, layout) }
29    }
30}
31
32#[derive(Debug, Clone, Copy)]
33pub struct AllocSnapshot {
34    alloc_events: u64,
35}
36
37pub fn snapshot() -> AllocSnapshot {
38    AllocSnapshot {
39        alloc_events: ALLOCATION_EVENTS.load(Ordering::Relaxed),
40    }
41}
42
43pub fn allocations_since(snapshot: AllocSnapshot) -> u64 {
44    ALLOCATION_EVENTS
45        .load(Ordering::Relaxed)
46        .saturating_sub(snapshot.alloc_events)
47}