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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#![deny(missing_docs)]
#![warn(clippy::all)]
#![warn(clippy::cargo)]
use std::{
error, fmt,
sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
Arc,
},
};
mod allocator;
mod token;
#[cfg(feature = "tracing-compat")]
mod tracing;
mod util;
pub use crate::allocator::Allocator;
pub use crate::token::{AllocationGroupToken, AllocationGuard};
#[cfg(feature = "tracing-compat")]
pub use crate::tracing::AllocationLayer;
static TRACKING_ENABLED: AtomicBool = AtomicBool::new(false);
static mut GLOBAL_TRACKER: Option<Tracker> = None;
static GLOBAL_INIT: AtomicUsize = AtomicUsize::new(UNINITIALIZED);
const UNINITIALIZED: usize = 0;
const INITIALIZING: usize = 1;
const INITIALIZED: usize = 2;
pub trait AllocationTracker {
fn allocated(
&self,
addr: usize,
size: usize,
group_id: usize,
tags: Option<&'static [(&'static str, &'static str)]>,
);
fn deallocated(&self, addr: usize);
}
struct Tracker {
tracker: Arc<dyn AllocationTracker + Send + Sync + 'static>,
}
impl Tracker {
fn from_allocation_tracker<T>(allocation_tracker: T) -> Self
where
T: AllocationTracker + Send + Sync + 'static,
{
Self {
tracker: Arc::new(allocation_tracker),
}
}
fn allocated(
&self,
addr: usize,
size: usize,
group_id: usize,
tags: Option<&'static [(&'static str, &'static str)]>,
) {
self.tracker.allocated(addr, size, group_id, tags)
}
fn deallocated(&self, addr: usize) {
self.tracker.deallocated(addr)
}
}
#[derive(Debug)]
pub struct SetTrackerError {
_sealed: (),
}
impl fmt::Display for SetTrackerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad("a global tracker has already been set")
}
}
impl error::Error for SetTrackerError {}
pub struct AllocationRegistry;
impl AllocationRegistry {
pub fn enable_tracking() {
TRACKING_ENABLED.store(true, Ordering::SeqCst);
}
pub fn disable_tracking() {
TRACKING_ENABLED.store(false, Ordering::SeqCst);
}
pub fn set_global_tracker<T>(tracker: T) -> Result<(), SetTrackerError>
where
T: AllocationTracker + Send + Sync + 'static,
{
if GLOBAL_INIT
.compare_exchange(
UNINITIALIZED,
INITIALIZING,
Ordering::SeqCst,
Ordering::SeqCst,
)
.is_ok()
{
unsafe {
GLOBAL_TRACKER = Some(Tracker::from_allocation_tracker(tracker));
}
GLOBAL_INIT.store(INITIALIZED, Ordering::SeqCst);
Ok(())
} else {
Err(SetTrackerError { _sealed: () })
}
}
#[doc(hidden)]
pub unsafe fn clear_global_tracker() {
GLOBAL_INIT.store(INITIALIZING, Ordering::SeqCst);
GLOBAL_TRACKER = None;
GLOBAL_INIT.store(UNINITIALIZED, Ordering::SeqCst);
}
}
#[inline(always)]
fn get_global_tracker() -> Option<&'static Tracker> {
if !TRACKING_ENABLED.load(Ordering::Relaxed) {
return None;
}
if GLOBAL_INIT.load(Ordering::SeqCst) != INITIALIZED {
return None;
}
unsafe {
let tracker = GLOBAL_TRACKER
.as_ref()
.expect("global tracked marked as initialized, but failed to unwrap");
Some(tracker)
}
}