@@ -33,7 +33,6 @@ impl TrackingAllocator {
3333 self . total_allocated . store ( 0 , Ordering :: Relaxed ) ;
3434 }
3535
36-
3736 pub fn print_stats ( & self ) {
3837 let allocations = self . allocations . load ( Ordering :: Relaxed ) ;
3938 let deallocations = self . deallocations . load ( Ordering :: Relaxed ) ;
@@ -46,9 +45,21 @@ impl TrackingAllocator {
4645 println ! ( "Total allocations: {}" , allocations) ;
4746 println ! ( "Total deallocations: {}" , deallocations) ;
4847 println ! ( "Total reallocations: {}" , reallocations) ;
49- println ! ( "Current allocated: {} bytes ({:.2} MB)" , current_allocated, current_allocated as f64 / 1024.0 / 1024.0 ) ;
50- println ! ( "Peak allocated: {} bytes ({:.2} MB)" , peak_allocated, peak_allocated as f64 / 1024.0 / 1024.0 ) ;
51- println ! ( "Total allocated: {} bytes ({:.2} MB)" , total_allocated, total_allocated as f64 / 1024.0 / 1024.0 ) ;
48+ println ! (
49+ "Current allocated: {} bytes ({:.2} MB)" ,
50+ current_allocated,
51+ current_allocated as f64 / 1024.0 / 1024.0
52+ ) ;
53+ println ! (
54+ "Peak allocated: {} bytes ({:.2} MB)" ,
55+ peak_allocated,
56+ peak_allocated as f64 / 1024.0 / 1024.0
57+ ) ;
58+ println ! (
59+ "Total allocated: {} bytes ({:.2} MB)" ,
60+ total_allocated,
61+ total_allocated as f64 / 1024.0 / 1024.0
62+ ) ;
5263 println ! ( "===============================" ) ;
5364 }
5465
@@ -57,7 +68,7 @@ impl TrackingAllocator {
5768 self . allocations . fetch_add ( 1 , Ordering :: Relaxed ) ;
5869 self . total_allocated . fetch_add ( size, Ordering :: Relaxed ) ;
5970 let current = self . current_allocated . fetch_add ( size, Ordering :: Relaxed ) + size;
60-
71+
6172 // Update peak if current exceeds it
6273 let mut peak = self . peak_allocated . load ( Ordering :: Relaxed ) ;
6374 while current > peak {
@@ -96,12 +107,16 @@ unsafe impl GlobalAlloc for TrackingAllocator {
96107 let new_ptr = self . inner . realloc ( ptr, layout, new_size) ;
97108 if !new_ptr. is_null ( ) {
98109 self . reallocations . fetch_add ( 1 , Ordering :: Relaxed ) ;
99-
110+
100111 // Update counters: subtract old size, add new size
101- self . current_allocated . fetch_sub ( layout. size ( ) , Ordering :: Relaxed ) ;
112+ self . current_allocated
113+ . fetch_sub ( layout. size ( ) , Ordering :: Relaxed ) ;
102114 self . total_allocated . fetch_add ( new_size, Ordering :: Relaxed ) ;
103- let current = self . current_allocated . fetch_add ( new_size, Ordering :: Relaxed ) + new_size;
104-
115+ let current = self
116+ . current_allocated
117+ . fetch_add ( new_size, Ordering :: Relaxed )
118+ + new_size;
119+
105120 // Update peak if current exceeds it
106121 let mut peak = self . peak_allocated . load ( Ordering :: Relaxed ) ;
107122 while current > peak {
@@ -118,4 +133,4 @@ unsafe impl GlobalAlloc for TrackingAllocator {
118133 }
119134 new_ptr
120135 }
121- }
136+ }
0 commit comments