use std::alloc::System;
use std::sync::atomic::{AtomicUsize, Ordering};
pub struct TrackedAllocator {
allocated: AtomicUsize,
deallocated: AtomicUsize,
#[allow(dead_code)]
inner: System,
}
impl Default for TrackedAllocator {
fn default() -> Self {
Self::new()
}
}
impl TrackedAllocator {
pub const fn new() -> Self {
Self {
allocated: AtomicUsize::new(0),
deallocated: AtomicUsize::new(0),
inner: System,
}
}
pub fn allocated_bytes(&self) -> usize {
self.allocated.load(Ordering::SeqCst)
}
pub fn deallocated_bytes(&self) -> usize {
self.deallocated.load(Ordering::SeqCst)
}
pub fn current_usage(&self) -> usize {
self.allocated_bytes()
.saturating_sub(self.deallocated_bytes())
}
}
pub fn get_memory_usage() -> usize {
0
}
pub fn get_total_allocated() -> usize {
0
}
pub fn get_total_deallocated() -> usize {
0
}
#[cfg(test)]
mod tests {
use super::*;
use std::mem;
#[test]
fn test_memory_tracking() {
let start_allocated = get_total_allocated();
let start_deallocated = get_total_deallocated();
let data = vec![0u8; 1024];
let allocated_size = mem::size_of_val(data.as_slice());
assert!(get_total_allocated() > start_allocated);
assert!(get_total_allocated() - start_allocated >= allocated_size);
drop(data);
assert!(get_total_deallocated() > start_deallocated);
assert!(get_total_deallocated() - start_deallocated >= allocated_size);
}
}