qudag_protocol/
allocator.rs

1use std::alloc::System;
2use std::sync::atomic::{AtomicUsize, Ordering};
3
4/// Custom allocator that tracks memory allocations
5pub struct TrackedAllocator {
6    allocated: AtomicUsize,
7    deallocated: AtomicUsize,
8    #[allow(dead_code)]
9    inner: System,
10}
11
12impl Default for TrackedAllocator {
13    fn default() -> Self {
14        Self::new()
15    }
16}
17
18impl TrackedAllocator {
19    pub const fn new() -> Self {
20        Self {
21            allocated: AtomicUsize::new(0),
22            deallocated: AtomicUsize::new(0),
23            inner: System,
24        }
25    }
26
27    pub fn allocated_bytes(&self) -> usize {
28        self.allocated.load(Ordering::SeqCst)
29    }
30
31    pub fn deallocated_bytes(&self) -> usize {
32        self.deallocated.load(Ordering::SeqCst)
33    }
34
35    pub fn current_usage(&self) -> usize {
36        self.allocated_bytes()
37            .saturating_sub(self.deallocated_bytes())
38    }
39}
40
41// TODO: Unsafe allocator implementation disabled due to #![deny(unsafe_code)]
42// This would require enabling unsafe code in specific modules
43/*
44unsafe impl GlobalAlloc for TrackedAllocator {
45    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
46        let ptr = self.inner.alloc(layout);
47        if !ptr.is_null() {
48            self.allocated.fetch_add(layout.size(), Ordering::SeqCst);
49        }
50        ptr
51    }
52
53    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
54        self.deallocated.fetch_add(layout.size(), Ordering::SeqCst);
55        self.inner.dealloc(ptr, layout)
56    }
57}
58*/
59
60// #[global_allocator]
61// static ALLOCATOR: TrackedAllocator = TrackedAllocator::new();
62
63/// Get the current memory usage in bytes
64pub fn get_memory_usage() -> usize {
65    // TODO: Implement without global allocator
66    0
67}
68
69/// Get the total number of bytes allocated
70pub fn get_total_allocated() -> usize {
71    // TODO: Implement without global allocator
72    0
73}
74
75/// Get the total number of bytes deallocated
76pub fn get_total_deallocated() -> usize {
77    // TODO: Implement without global allocator
78    0
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84    use std::mem;
85
86    #[test]
87    fn test_memory_tracking() {
88        // Record initial values
89        let start_allocated = get_total_allocated();
90        let start_deallocated = get_total_deallocated();
91
92        // Allocate some memory
93        let data = vec![0u8; 1024];
94        let allocated_size = mem::size_of_val(data.as_slice());
95
96        // Check allocation was tracked
97        assert!(get_total_allocated() > start_allocated);
98        assert!(get_total_allocated() - start_allocated >= allocated_size);
99
100        // Drop the allocation
101        drop(data);
102
103        // Check deallocation was tracked
104        assert!(get_total_deallocated() > start_deallocated);
105        assert!(get_total_deallocated() - start_deallocated >= allocated_size);
106    }
107}