blob: 57aca7def33526bf279d475471c83b3752a61dfc (
plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
#ifndef COUNT_NEW_HPP
#define COUNT_NEW_HPP
# include <cstdlib>
# include <cassert>
# include <new>
#ifndef __has_feature
# define __has_feature(x) 0
#endif
#if __has_feature(address_sanitizer) \
|| __has_feature(memory_sanitizer) \
|| __has_feature(thread_sanitizer)
#define DISABLE_NEW_COUNT
#endif
class MemCounter
{
public:
// Make MemCounter super hard to accidentally construct or copy.
class MemCounterCtorArg_ {};
explicit MemCounter(MemCounterCtorArg_) { reset(); }
private:
MemCounter(MemCounter const &);
MemCounter & operator=(MemCounter const &);
public:
// All checks return true when disable_checking is enabled.
static const bool disable_checking;
// Disallow any allocations from occurring. Useful for testing that
// code doesn't perform any allocations.
bool disable_allocations;
int outstanding_new;
int new_called;
int delete_called;
int last_new_size;
int outstanding_array_new;
int new_array_called;
int delete_array_called;
int last_new_array_size;
public:
void newCalled(std::size_t s)
{
assert(disable_allocations == false);
assert(s);
++new_called;
++outstanding_new;
last_new_size = s;
}
void deleteCalled(void * p)
{
assert(p);
--outstanding_new;
++delete_called;
}
void newArrayCalled(std::size_t s)
{
assert(disable_allocations == false);
assert(s);
++outstanding_array_new;
++new_array_called;
last_new_array_size = s;
}
void deleteArrayCalled(void * p)
{
assert(p);
--outstanding_array_new;
++delete_array_called;
}
void disableAllocations()
{
disable_allocations = true;
}
void enableAllocations()
{
disable_allocations = false;
}
void reset()
{
disable_allocations = false;
outstanding_new = 0;
new_called = 0;
delete_called = 0;
last_new_size = 0;
outstanding_array_new = 0;
new_array_called = 0;
delete_array_called = 0;
last_new_array_size = 0;
}
public:
bool checkOutstandingNewEq(int n) const
{
return disable_checking || n == outstanding_new;
}
bool checkOutstandingNewNotEq(int n) const
{
return disable_checking || n != outstanding_new;
}
bool checkNewCalledEq(int n) const
{
return disable_checking || n == new_called;
}
bool checkNewCalledNotEq(int n) const
{
return disable_checking || n != new_called;
}
bool checkDeleteCalledEq(int n) const
{
return disable_checking || n == delete_called;
}
bool checkDeleteCalledNotEq(int n) const
{
return disable_checking || n != delete_called;
}
bool checkLastNewSizeEq(int n) const
{
return disable_checking || n == last_new_size;
}
bool checkLastNewSizeNotEq(int n) const
{
return disable_checking || n != last_new_size;
}
bool checkOutstandingArrayNewEq(int n) const
{
return disable_checking || n == outstanding_array_new;
}
bool checkOutstandingArrayNewNotEq(int n) const
{
return disable_checking || n != outstanding_array_new;
}
bool checkNewArrayCalledEq(int n) const
{
return disable_checking || n == new_array_called;
}
bool checkNewArrayCalledNotEq(int n) const
{
return disable_checking || n != new_array_called;
}
bool checkDeleteArrayCalledEq(int n) const
{
return disable_checking || n == delete_array_called;
}
bool checkDeleteArrayCalledNotEq(int n) const
{
return disable_checking || n != delete_array_called;
}
bool checkLastNewArraySizeEq(int n) const
{
return disable_checking || n == last_new_array_size;
}
bool checkLastNewArraySizeNotEq(int n) const
{
return disable_checking || n != last_new_array_size;
}
};
#ifdef DISABLE_NEW_COUNT
const bool MemCounter::disable_checking = true;
#else
const bool MemCounter::disable_checking = false;
#endif
MemCounter globalMemCounter((MemCounter::MemCounterCtorArg_()));
#ifndef DISABLE_NEW_COUNT
void* operator new(std::size_t s) throw(std::bad_alloc)
{
globalMemCounter.newCalled(s);
return std::malloc(s);
}
void operator delete(void* p) throw()
{
globalMemCounter.deleteCalled(p);
std::free(p);
}
void* operator new[](std::size_t s) throw(std::bad_alloc)
{
globalMemCounter.newArrayCalled(s);
return operator new(s);
}
void operator delete[](void* p) throw()
{
globalMemCounter.deleteArrayCalled(p);
operator delete(p);
}
#endif // DISABLE_NEW_COUNT
struct DisableAllocationGuard {
explicit DisableAllocationGuard(bool disable = true) : m_disabled(disable)
{
// Don't re-disable if already disabled.
if (globalMemCounter.disable_allocations == true) m_disabled = false;
if (m_disabled) globalMemCounter.disableAllocations();
}
void release() {
if (m_disabled) globalMemCounter.enableAllocations();
m_disabled = false;
}
~DisableAllocationGuard() {
release();
}
private:
bool m_disabled;
DisableAllocationGuard(DisableAllocationGuard const&);
DisableAllocationGuard& operator=(DisableAllocationGuard const&);
};
#endif /* COUNT_NEW_HPP */
|