| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
use std::alloc::{GlobalAlloc, Layout, System}; |
| 7 |
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::Relaxed}; |
| 8 |
|
| 9 |
static LIVE: AtomicUsize = AtomicUsize::new(0); |
| 10 |
static PEAK: AtomicUsize = AtomicUsize::new(0); |
| 11 |
static TOTAL: AtomicUsize = AtomicUsize::new(0); |
| 12 |
static COUNT: AtomicUsize = AtomicUsize::new(0); |
| 13 |
static ACTIVE: AtomicBool = AtomicBool::new(false); |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
pub struct Counting; |
| 18 |
|
| 19 |
impl Counting { |
| 20 |
fn grew(n: usize) { |
| 21 |
ACTIVE.store(true, Relaxed); |
| 22 |
let live = LIVE.fetch_add(n, Relaxed) + n; |
| 23 |
TOTAL.fetch_add(n, Relaxed); |
| 24 |
COUNT.fetch_add(1, Relaxed); |
| 25 |
PEAK.fetch_max(live, Relaxed); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
unsafe impl GlobalAlloc for Counting { |
| 30 |
unsafe fn alloc(&self, l: Layout) -> *mut u8 { |
| 31 |
let p = System.alloc(l); |
| 32 |
if !p.is_null() { |
| 33 |
Self::grew(l.size()); |
| 34 |
} |
| 35 |
p |
| 36 |
} |
| 37 |
unsafe fn alloc_zeroed(&self, l: Layout) -> *mut u8 { |
| 38 |
let p = System.alloc_zeroed(l); |
| 39 |
if !p.is_null() { |
| 40 |
Self::grew(l.size()); |
| 41 |
} |
| 42 |
p |
| 43 |
} |
| 44 |
unsafe fn dealloc(&self, p: *mut u8, l: Layout) { |
| 45 |
System.dealloc(p, l); |
| 46 |
LIVE.fetch_sub(l.size(), Relaxed); |
| 47 |
} |
| 48 |
unsafe fn realloc(&self, p: *mut u8, l: Layout, new: usize) -> *mut u8 { |
| 49 |
let q = System.realloc(p, l, new); |
| 50 |
if !q.is_null() { |
| 51 |
LIVE.fetch_sub(l.size(), Relaxed); |
| 52 |
Self::grew(new); |
| 53 |
} |
| 54 |
q |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 60 |
pub struct Stats { |
| 61 |
|
| 62 |
pub live: usize, |
| 63 |
|
| 64 |
pub peak: usize, |
| 65 |
|
| 66 |
pub total: usize, |
| 67 |
|
| 68 |
pub count: usize, |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
pub fn stats() -> Option<Stats> { |
| 73 |
ACTIVE.load(Relaxed).then(|| Stats { |
| 74 |
live: LIVE.load(Relaxed), |
| 75 |
peak: PEAK.load(Relaxed), |
| 76 |
total: TOTAL.load(Relaxed), |
| 77 |
count: COUNT.load(Relaxed), |
| 78 |
}) |
| 79 |
} |