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
use std::path::Path;
use crate::malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
pub fn shallow_size_of_fxhashmap<K, V>(
val: &rustc_hash::FxHashMap<K, V>,
ops: &mut MallocSizeOfOps,
) -> usize {
if ops.has_malloc_enclosing_size_of() {
val.values()
.next()
.map_or(0, |v| unsafe { ops.malloc_enclosing_size_of(v) })
} else {
val.capacity()
* (std::mem::size_of::<V>() + std::mem::size_of::<K>() + std::mem::size_of::<usize>())
}
}
pub fn size_of_btreemap<K, V>(
val: &std::collections::BTreeMap<K, V>,
ops: &mut MallocSizeOfOps,
) -> usize
where
K: MallocSizeOf,
V: MallocSizeOf,
{
let mut size = 0;
for (key, value) in val.iter() {
size += std::mem::size_of::<(K, V)>() + key.size_of(ops) + value.size_of(ops);
}
size
}
pub fn size_of_pathbuf(val: &Path, ops: &mut MallocSizeOfOps) -> usize {
val.as_os_str().size_of(ops)
}
pub fn size_of_option_tempdir(val: &Option<tempfile::TempDir>, ops: &mut MallocSizeOfOps) -> usize {
let mut result = std::mem::size_of::<Option<tempfile::TempDir>>();
if let Some(dir) = val {
result += dir.path().as_os_str().size_of(ops)
}
result
}
#[cfg(not(windows))]
pub mod platform {
use std::os::raw::c_void;
extern "C" {
#[cfg_attr(any(target_os = "macos", target_os = "ios"), link_name = "malloc_size")]
fn malloc_usable_size(ptr: *const c_void) -> usize;
}
pub unsafe extern "C" fn usable_size(ptr: *const c_void) -> usize {
if ptr.is_null() {
0
} else {
malloc_usable_size(ptr)
}
}
}
#[cfg(windows)]
pub mod platform {
extern crate winapi;
use self::winapi::um::heapapi::{GetProcessHeap, HeapSize, HeapValidate};
use std::os::raw::c_void;
pub unsafe extern "C" fn usable_size(mut ptr: *const c_void) -> usize {
let heap = GetProcessHeap();
if HeapValidate(heap, 0, ptr) == 0 {
ptr = *(ptr as *const *const c_void).offset(-1);
}
HeapSize(heap, 0, ptr) as usize
}
}