1#![cfg_attr(not(feature = "std"), no_std)]
14
15#[cfg(not(feature = "std"))]
16extern crate alloc;
17
18cfg_if::cfg_if! {
19 if #[cfg(all(
20 feature = "jemalloc-global",
21 not(target_os = "windows"),
22 not(target_arch = "wasm32")
23 ))] {
24 #[global_allocator]
26 pub static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
27
28 mod memory_stats_jemalloc;
29 use memory_stats_jemalloc as memory_stats;
30 } else if #[cfg(feature = "dlmalloc-global")] {
31 #[global_allocator]
33 pub static ALLOC: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc;
34
35 mod memory_stats_noop;
36 use memory_stats_noop as memory_stats;
37 } else if #[cfg(all(
38 feature = "mimalloc-global",
39 not(target_arch = "wasm32")
40 ))] {
41 #[global_allocator]
43 pub static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
44
45 mod memory_stats_noop;
46 use memory_stats_noop as memory_stats;
47 } else {
48 mod memory_stats_noop;
50 use memory_stats_noop as memory_stats;
51 }
52}
53
54pub mod allocators;
55
56#[cfg(any(
57 all(any(target_os = "macos", target_os = "ios"), not(feature = "jemalloc-global"),),
58 feature = "estimate-heapsize"
59))]
60pub mod sizeof;
61
62#[macro_use]
68mod malloc_size;
69
70#[cfg(feature = "ethereum-impls")]
71pub mod ethereum_impls;
72
73#[cfg(feature = "primitive-types")]
74pub mod primitives_impls;
75
76pub use allocators::MallocSizeOfExt;
77pub use malloc_size::{MallocShallowSizeOf, MallocSizeOf, MallocSizeOfOps};
78
79pub use parity_util_mem_derive::*;
80
81pub fn malloc_size<T: MallocSizeOf + ?Sized>(t: &T) -> usize {
85 MallocSizeOf::size_of(t, &mut allocators::new_malloc_size_ops())
86}
87
88#[derive(Clone, Debug)]
90pub struct MemoryStatsError(memory_stats::Error);
91
92#[cfg(feature = "std")]
93impl std::fmt::Display for MemoryStatsError {
94 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
95 self.0.fmt(fmt)
96 }
97}
98
99#[cfg(feature = "std")]
100impl std::error::Error for MemoryStatsError {}
101
102#[non_exhaustive]
104#[derive(Debug, Clone)]
105pub struct MemoryAllocationSnapshot {
106 pub resident: u64,
108 pub allocated: u64,
110}
111
112#[derive(Clone)]
114pub struct MemoryAllocationTracker(self::memory_stats::MemoryAllocationTracker);
115
116impl MemoryAllocationTracker {
117 pub fn new() -> Result<Self, MemoryStatsError> {
119 self::memory_stats::MemoryAllocationTracker::new()
120 .map(MemoryAllocationTracker)
121 .map_err(MemoryStatsError)
122 }
123
124 pub fn snapshot(&self) -> Result<MemoryAllocationSnapshot, MemoryStatsError> {
126 self.0.snapshot().map_err(MemoryStatsError)
127 }
128}
129
130#[cfg(feature = "std")]
131#[cfg(test)]
132mod test {
133 use super::{malloc_size, MallocSizeOf, MallocSizeOfExt};
134 use std::sync::Arc;
135
136 #[test]
137 fn test_arc() {
138 let val = Arc::new("test".to_string());
139 let s = val.malloc_size_of();
140 assert!(s > 0);
141 }
142
143 #[test]
144 fn test_dyn() {
145 trait Augmented: MallocSizeOf {}
146 impl Augmented for Vec<u8> {}
147 let val: Arc<dyn Augmented> = Arc::new(vec![0u8; 1024]);
148 assert!(malloc_size(&*val) > 1000);
149 }
150}