use std::cell::Cell;
use crate::{Allocator, arena::Arena};
#[derive(Default, Debug)]
pub struct AllocationStats {
num_alloc: Cell<usize>,
num_realloc: Cell<usize>,
}
impl AllocationStats {
pub(crate) fn record_allocation(&self) {
self.num_alloc.set(self.num_alloc.get().saturating_add(1));
}
pub(crate) fn record_reallocation(&self) {
self.num_realloc.set(self.num_realloc.get().saturating_add(1));
}
pub(crate) fn record_reallocation_after_allocation(&self) {
let num_alloc = self.num_alloc.get();
if num_alloc != usize::MAX {
self.num_alloc.set(num_alloc - 1);
}
self.num_realloc.set(self.num_realloc.get().saturating_add(1));
}
pub(crate) fn reset(&self) {
self.num_alloc.set(0);
self.num_realloc.set(0);
}
}
impl Arena {
fn get_allocation_stats(&self) -> (usize, usize) {
let num_alloc = self.stats.num_alloc.get();
let num_realloc = self.stats.num_realloc.get();
(num_alloc, num_realloc)
}
}
impl Allocator {
#[doc(hidden)]
pub fn get_allocation_stats(&self) -> (usize, usize) {
self.arena().get_allocation_stats()
}
}