11
11
#if (TD_DARWIN || TD_LINUX) && defined(USE_MEMPROF)
12
12
#include < algorithm>
13
13
#include < atomic>
14
- #include < cassert>
15
14
#include < cstddef>
16
15
#include < cstdint>
17
16
#include < cstdlib>
@@ -34,6 +33,11 @@ double get_fast_backtrace_success_rate() {
34
33
}
35
34
#else
36
35
36
+ #define my_assert (f ) \
37
+ if (!(f)) { \
38
+ std::abort (); \
39
+ }
40
+
37
41
#if TD_LINUX
38
42
extern void *__libc_stack_end;
39
43
#endif
@@ -156,7 +160,7 @@ std::int32_t get_ht_pos(const Backtrace &bt, bool force = false) {
156
160
if (pos_hash == 0 ) {
157
161
if (ht_size > HT_MAX_SIZE / 2 ) {
158
162
if (force) {
159
- assert (ht_size * 10 < HT_MAX_SIZE * 7 );
163
+ my_assert (ht_size * 10 < HT_MAX_SIZE * 7 );
160
164
} else {
161
165
Backtrace unknown_bt{{nullptr }};
162
166
unknown_bt[0 ] = reinterpret_cast <void *>(1 );
@@ -188,18 +192,21 @@ std::int32_t get_ht_pos(const Backtrace &bt, bool force = false) {
188
192
189
193
void dump_alloc (const std::function<void (const AllocInfo &)> &func) {
190
194
for (auto &node : ht) {
191
- if (node.size == 0 ) {
195
+ auto size = node.size .load (std::memory_order_relaxed);
196
+ if (size == 0 ) {
192
197
continue ;
193
198
}
194
- func (AllocInfo{node.backtrace , node. size . load () });
199
+ func (AllocInfo{node.backtrace , size});
195
200
}
196
201
}
197
202
198
203
void register_xalloc (malloc_info *info, std::int32_t diff) {
204
+ my_assert (info->size >= 0 );
199
205
if (diff > 0 ) {
200
- ht[info->ht_pos ].size += info->size ;
206
+ ht[info->ht_pos ].size . fetch_add ( info->size , std::memory_order_relaxed) ;
201
207
} else {
202
- ht[info->ht_pos ].size -= info->size ;
208
+ auto old_value = ht[info->ht_pos ].size .fetch_sub (info->size , std::memory_order_relaxed);
209
+ my_assert (old_value >= static_cast <std::size_t >(info->size ));
203
210
}
204
211
}
205
212
@@ -234,7 +241,7 @@ static malloc_info *get_info(void *data_void) {
234
241
auto *buf = data - RESERVED_SIZE;
235
242
236
243
auto *info = reinterpret_cast <malloc_info *>(buf);
237
- assert (info->magic == MALLOC_INFO_MAGIC);
244
+ my_assert (info->magic == MALLOC_INFO_MAGIC);
238
245
return info;
239
246
}
240
247
@@ -276,7 +283,7 @@ void *realloc(void *ptr, std::size_t size) {
276
283
return new_ptr;
277
284
}
278
285
void *memalign (std::size_t aligment, std::size_t size) {
279
- assert (false && " Memalign is unsupported" );
286
+ my_assert (false && " Memalign is unsupported" );
280
287
return nullptr ;
281
288
}
282
289
}
0 commit comments