//! A global allocator that counts allocations. #![allow(unsafe_code, reason = "implementing `GlobalAlloc` is unsafe; every method forwards to `System`")] use std::alloc::{GlobalAlloc, Layout, System}; use std::sync::atomic::{AtomicU64, Ordering}; static COUNT: AtomicU64 = AtomicU64::new(0); static BYTES: AtomicU64 = AtomicU64::new(0); /// Forwards to the system allocator and counts every allocation and reallocation. A binary /// installs it with `#[global_allocator]`. pub struct CountingAlloc; // SAFETY: every method forwards its arguments unchanged to `System`, which upholds the contract. unsafe impl GlobalAlloc for CountingAlloc { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { COUNT.fetch_add(1, Ordering::Relaxed); BYTES.fetch_add(layout.size() as u64, Ordering::Relaxed); unsafe { System.alloc(layout) } } unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { COUNT.fetch_add(1, Ordering::Relaxed); BYTES.fetch_add(layout.size() as u64, Ordering::Relaxed); 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, new_size: usize) -> *mut u8 { COUNT.fetch_add(1, Ordering::Relaxed); BYTES.fetch_add(new_size as u64, Ordering::Relaxed); unsafe { System.realloc(ptr, layout, new_size) } } } /// The allocations and the bytes requested so far. pub fn allocations() -> (u64, u64) { (COUNT.load(Ordering::Relaxed), BYTES.load(Ordering::Relaxed)) }