@@ -49,6 +49,35 @@ impl ThreadLocalCounter {
49
49
}
50
50
}
51
51
52
+ fn update ( & mut self , old_size : usize , new_size : usize ) {
53
+ self . allocation_counters . deallocations += old_size;
54
+ self . allocation_counters . deallocation_count += 1 ;
55
+ self . allocation_counters . allocations += new_size;
56
+ self . allocation_counters . allocation_count += 1 ;
57
+ match old_size. cmp ( & new_size) {
58
+ std:: cmp:: Ordering :: Equal => { }
59
+ std:: cmp:: Ordering :: Less => {
60
+ let size = new_size - old_size;
61
+ if self . buffer >= size {
62
+ self . buffer -= size;
63
+ } else {
64
+ let offset = size - self . buffer + TARGET_BUFFER ;
65
+ self . buffer = TARGET_BUFFER ;
66
+ ALLOCATED . fetch_add ( offset, Ordering :: Relaxed ) ;
67
+ }
68
+ }
69
+ std:: cmp:: Ordering :: Greater => {
70
+ let size = old_size - new_size;
71
+ self . buffer += size;
72
+ if self . buffer > MAX_BUFFER {
73
+ let offset = self . buffer - TARGET_BUFFER ;
74
+ self . buffer = TARGET_BUFFER ;
75
+ ALLOCATED . fetch_sub ( offset, Ordering :: Relaxed ) ;
76
+ }
77
+ }
78
+ }
79
+ }
80
+
52
81
fn unload ( & mut self ) {
53
82
if self . buffer > 0 {
54
83
ALLOCATED . fetch_sub ( self . buffer , Ordering :: Relaxed ) ;
@@ -93,6 +122,11 @@ pub fn remove(size: usize) {
93
122
with_local_counter ( |local| local. remove ( size) ) ;
94
123
}
95
124
125
+ /// Adds some `size` to the global counter in a thread-local buffered way.
126
+ pub fn update ( old_size : usize , new_size : usize ) {
127
+ with_local_counter ( |local| local. update ( old_size, new_size) ) ;
128
+ }
129
+
96
130
/// Flushes the thread-local buffer to the global counter. This should be called
97
131
/// e. g. when a thread is stopped or goes to sleep for a long time.
98
132
pub fn flush ( ) {
0 commit comments