1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use core::marker::PhantomData;
use parity_util_mem::{malloc_size, MallocSizeOf};
pub trait MemTracker<T> {
fn on_remove(&mut self, _value: &T) {}
fn on_insert(&mut self, _value: &T) {}
fn on_clear(&mut self) {}
fn get_size(&self) -> usize { 0 }
}
#[derive(Eq, PartialEq)]
pub struct MemCounter<T> {
malloc_size_of_values: usize,
_phantom: PhantomData<T>,
}
impl<T> MemCounter<T> {
pub fn new() -> Self {
Self {
malloc_size_of_values: 0,
_phantom: PhantomData,
}
}
}
impl<T> Default for MemCounter<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> Clone for MemCounter<T> {
fn clone(&self) -> Self {
Self {
malloc_size_of_values: self.malloc_size_of_values,
_phantom: PhantomData,
}
}
}
impl<T> Copy for MemCounter<T> {}
impl<T: MallocSizeOf> MemTracker<T> for MemCounter<T> {
fn on_remove(&mut self, value: &T) {
self.malloc_size_of_values -= malloc_size(value);
}
fn on_insert(&mut self, value: &T) {
self.malloc_size_of_values += malloc_size(value);
}
fn on_clear(&mut self) {
self.malloc_size_of_values = 0;
}
fn get_size(&self) -> usize {
self.malloc_size_of_values
}
}
#[derive(PartialEq, Eq)]
pub struct NoopTracker<T>(PhantomData<T>);
impl<T> Default for NoopTracker<T> {
fn default() -> Self {
Self(PhantomData)
}
}
impl<T> Clone for NoopTracker<T> {
fn clone(&self) -> Self {
Self::default()
}
}
impl<T> Copy for NoopTracker<T> {}
impl<T> MemTracker<T> for NoopTracker<T> {}