14
14
#include "utils_concurrency.h"
15
15
16
16
#ifndef NDEBUG
17
- #define DEBUG_RUN_CHECKS (pool ) ba_debug_checks(pool)
18
- #define DEBUG_SET_VAR (var , value ) DO_WHILE_EXPRS((var = value))
19
- #define DEBUG_INC_VAR (var ) DO_WHILE_EXPRS((var++))
20
- #define DEBUG_DEC_VAR (var ) DO_WHILE_EXPRS((var--))
17
+ #define _DEBUG_EXECUTE (expression ) DO_WHILE_EXPRS(expression)
21
18
#else
22
- #define DEBUG_RUN_CHECKS (pool ) DO_WHILE_EMPTY
23
- #define DEBUG_SET_VAR (var , value ) DO_WHILE_EMPTY
24
- #define DEBUG_INC_VAR (var ) DO_WHILE_EMPTY
25
- #define DEBUG_DEC_VAR (var ) DO_WHILE_EMPTY
19
+ #define _DEBUG_EXECUTE (expression ) DO_WHILE_EMPTY
26
20
#endif /* NDEBUG */
27
21
28
22
// minimum size of a single pool of the linear base allocator
@@ -110,8 +104,8 @@ umf_ba_linear_pool_t *umf_ba_linear_create(size_t pool_size) {
110
104
pool -> metadata .size_left = size_left ;
111
105
pool -> next_pool = NULL ; // this is the only pool now
112
106
pool -> metadata .pool_n_allocs = 0 ;
113
- DEBUG_SET_VAR (pool -> metadata .n_pools , 1 );
114
- DEBUG_SET_VAR (pool -> metadata .global_n_allocs , 0 );
107
+ _DEBUG_EXECUTE (pool -> metadata .n_pools = 1 );
108
+ _DEBUG_EXECUTE (pool -> metadata .global_n_allocs = 0 );
115
109
116
110
// init lock
117
111
os_mutex_t * lock = util_mutex_init (& pool -> metadata .lock );
@@ -159,7 +153,7 @@ void *umf_ba_linear_alloc(umf_ba_linear_pool_t *pool, size_t size) {
159
153
// add the new pool to the list of pools
160
154
new_pool -> next_pool = pool -> next_pool ;
161
155
pool -> next_pool = new_pool ;
162
- DEBUG_INC_VAR (pool -> metadata .n_pools );
156
+ _DEBUG_EXECUTE (pool -> metadata .n_pools ++ );
163
157
}
164
158
165
159
assert (pool -> metadata .size_left >= aligned_size );
@@ -171,8 +165,8 @@ void *umf_ba_linear_alloc(umf_ba_linear_pool_t *pool, size_t size) {
171
165
} else {
172
166
pool -> metadata .pool_n_allocs ++ ;
173
167
}
174
- DEBUG_INC_VAR (pool -> metadata .global_n_allocs );
175
- DEBUG_RUN_CHECKS ( pool );
168
+ _DEBUG_EXECUTE (pool -> metadata .global_n_allocs ++ );
169
+ _DEBUG_EXECUTE ( ba_debug_checks ( pool ) );
176
170
util_mutex_unlock (& pool -> metadata .lock );
177
171
178
172
return ptr ;
@@ -191,10 +185,10 @@ static inline int pool_contains_ptr(void *pool, size_t pool_size,
191
185
// -1 - ptr doesn't belong to the pool and wasn't freed
192
186
int umf_ba_linear_free (umf_ba_linear_pool_t * pool , void * ptr ) {
193
187
util_mutex_lock (& pool -> metadata .lock );
194
- DEBUG_RUN_CHECKS ( pool );
188
+ _DEBUG_EXECUTE ( ba_debug_checks ( pool ) );
195
189
if (pool_contains_ptr (pool , pool -> metadata .pool_size , pool -> data , ptr )) {
196
190
pool -> metadata .pool_n_allocs -- ;
197
- DEBUG_DEC_VAR (pool -> metadata .global_n_allocs );
191
+ _DEBUG_EXECUTE (pool -> metadata .global_n_allocs -- );
198
192
size_t page_size = ba_os_get_page_size ();
199
193
if ((pool -> metadata .pool_n_allocs == 0 ) && pool -> next_pool &&
200
194
(pool -> metadata .pool_size > page_size )) {
@@ -203,7 +197,7 @@ int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
203
197
size_t size = pool -> metadata .pool_size - page_size ;
204
198
ba_os_free (ptr , size );
205
199
}
206
- DEBUG_RUN_CHECKS ( pool );
200
+ _DEBUG_EXECUTE ( ba_debug_checks ( pool ) );
207
201
util_mutex_unlock (& pool -> metadata .lock );
208
202
return 0 ;
209
203
}
@@ -213,20 +207,20 @@ int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
213
207
while (next_pool ) {
214
208
if (pool_contains_ptr (next_pool , next_pool -> pool_size , next_pool -> data ,
215
209
ptr )) {
216
- DEBUG_DEC_VAR (pool -> metadata .global_n_allocs );
210
+ _DEBUG_EXECUTE (pool -> metadata .global_n_allocs -- );
217
211
next_pool -> pool_n_allocs -- ;
218
212
// pool->next_pool is the active pool - we cannot free it
219
213
if ((next_pool -> pool_n_allocs == 0 ) &&
220
214
next_pool != pool -> next_pool ) {
221
215
assert (prev_pool ); // it cannot be the active pool
222
216
assert (prev_pool -> next_pool == next_pool );
223
217
prev_pool -> next_pool = next_pool -> next_pool ;
224
- DEBUG_DEC_VAR (pool -> metadata .n_pools );
218
+ _DEBUG_EXECUTE (pool -> metadata .n_pools -- );
225
219
void * ptr = next_pool ;
226
220
size_t size = next_pool -> pool_size ;
227
221
ba_os_free (ptr , size );
228
222
}
229
- DEBUG_RUN_CHECKS ( pool );
223
+ _DEBUG_EXECUTE ( ba_debug_checks ( pool ) );
230
224
util_mutex_unlock (& pool -> metadata .lock );
231
225
return 0 ;
232
226
}
@@ -248,7 +242,7 @@ void umf_ba_linear_destroy(umf_ba_linear_pool_t *pool) {
248
242
}
249
243
250
244
#ifndef NDEBUG
251
- DEBUG_RUN_CHECKS ( pool );
245
+ _DEBUG_EXECUTE ( ba_debug_checks ( pool ) );
252
246
if (pool -> metadata .global_n_allocs ) {
253
247
fprintf (stderr , "umf_ba_linear_destroy(): global_n_allocs = %zu\n" ,
254
248
pool -> metadata .global_n_allocs );
0 commit comments