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
#![feature(external_doc)]
#![doc(include = "../README.md")]
use std::alloc::{System,GlobalAlloc,Layout};
use std::cell::Cell;
#[cfg(all(feature = "disable_release", feature = "warn_release"))]
compile_error!("disable_release cannot be active at the same time with warn_release");
#[cfg(not(all(feature = "disable_release", not(debug_assertions))))]
thread_local! {
static ALLOC_FORBID_COUNT: Cell<u32> = Cell::new(0);
#[cfg(any( all(feature="warn_debug", debug_assertions), all(feature="warn_release", not(debug_assertions)) ))]
static ALLOC_VIOLATION_COUNT: Cell<u32> = Cell::new(0);
}
#[cfg(all(feature = "disable_release", not(debug_assertions)))]
pub fn assert_no_alloc<T, F: FnOnce() -> T> (func: F) -> T {
func()
}
#[cfg(not(all(feature = "disable_release", not(debug_assertions))))]
pub fn assert_no_alloc<T, F: FnOnce() -> T> (func: F) -> T {
struct Guard;
impl Guard {
fn new() -> Guard {
ALLOC_FORBID_COUNT.with(|c| c.set(c.get()+1));
Guard
}
}
impl Drop for Guard {
fn drop(&mut self) {
ALLOC_FORBID_COUNT.with(|c| c.set(c.get()-1));
}
}
#[cfg(any( all(feature="warn_debug", debug_assertions), all(feature="warn_release", not(debug_assertions)) ))]
let old_violation_count = violation_count();
let guard = Guard::new();
let ret = func();
std::mem::drop(guard);
#[cfg(any( all(feature="warn_debug", debug_assertions), all(feature="warn_release", not(debug_assertions)) ))]
if violation_count() > old_violation_count {
eprintln!("Tried to (de)allocate memory in a thread that forbids allocator calls!");
}
return ret;
}
#[cfg(any( all(feature="warn_debug", debug_assertions), all(feature="warn_release", not(debug_assertions)) ))]
pub fn violation_count() -> u32 {
ALLOC_VIOLATION_COUNT.with(|c| c.get())
}
#[cfg(any( all(feature="warn_debug", debug_assertions), all(feature="warn_release", not(debug_assertions)) ))]
pub fn reset_violation_count() {
ALLOC_VIOLATION_COUNT.with(|c| c.set(0));
}
#[cfg(not(all(feature = "disable_release", not(debug_assertions))))]
pub struct AllocDisabler;
#[cfg(not(all(feature = "disable_release", not(debug_assertions))))]
impl AllocDisabler {
fn check(&self, _layout: Layout) {
let forbid_count = ALLOC_FORBID_COUNT.with(|f| f.get());
if forbid_count > 0 {
#[cfg(any( all(feature="warn_debug", debug_assertions), all(feature="warn_release", not(debug_assertions)) ))]
ALLOC_VIOLATION_COUNT.with(|c| c.set(c.get()+1));
#[cfg(any( all(not(feature="warn_debug"), debug_assertions), all(not(feature="warn_release"), not(debug_assertions)) ))]
std::alloc::handle_alloc_error(_layout);
}
}
}
#[cfg(not(all(feature = "disable_release", not(debug_assertions))))]
unsafe impl GlobalAlloc for AllocDisabler {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
self.check(layout);
System.alloc(layout)
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
self.check(layout);
System.dealloc(ptr, layout)
}
}