Skip to content

Commit 5b9672a

Browse files
authored
Merge pull request #254 from ldorau/Replace_four_debug_macros_with_only_one
Replace four debug macros with only one
2 parents 75f78c9 + 677f7b1 commit 5b9672a

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

src/base_alloc/base_alloc_linear.c

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,9 @@
1414
#include "utils_concurrency.h"
1515

1616
#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)
2118
#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
2620
#endif /* NDEBUG */
2721

2822
// 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) {
110104
pool->metadata.size_left = size_left;
111105
pool->next_pool = NULL; // this is the only pool now
112106
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);
115109

116110
// init lock
117111
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) {
159153
// add the new pool to the list of pools
160154
new_pool->next_pool = pool->next_pool;
161155
pool->next_pool = new_pool;
162-
DEBUG_INC_VAR(pool->metadata.n_pools);
156+
_DEBUG_EXECUTE(pool->metadata.n_pools++);
163157
}
164158

165159
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) {
171165
} else {
172166
pool->metadata.pool_n_allocs++;
173167
}
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));
176170
util_mutex_unlock(&pool->metadata.lock);
177171

178172
return ptr;
@@ -191,10 +185,10 @@ static inline int pool_contains_ptr(void *pool, size_t pool_size,
191185
// -1 - ptr doesn't belong to the pool and wasn't freed
192186
int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
193187
util_mutex_lock(&pool->metadata.lock);
194-
DEBUG_RUN_CHECKS(pool);
188+
_DEBUG_EXECUTE(ba_debug_checks(pool));
195189
if (pool_contains_ptr(pool, pool->metadata.pool_size, pool->data, ptr)) {
196190
pool->metadata.pool_n_allocs--;
197-
DEBUG_DEC_VAR(pool->metadata.global_n_allocs);
191+
_DEBUG_EXECUTE(pool->metadata.global_n_allocs--);
198192
size_t page_size = ba_os_get_page_size();
199193
if ((pool->metadata.pool_n_allocs == 0) && pool->next_pool &&
200194
(pool->metadata.pool_size > page_size)) {
@@ -203,7 +197,7 @@ int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
203197
size_t size = pool->metadata.pool_size - page_size;
204198
ba_os_free(ptr, size);
205199
}
206-
DEBUG_RUN_CHECKS(pool);
200+
_DEBUG_EXECUTE(ba_debug_checks(pool));
207201
util_mutex_unlock(&pool->metadata.lock);
208202
return 0;
209203
}
@@ -213,20 +207,20 @@ int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
213207
while (next_pool) {
214208
if (pool_contains_ptr(next_pool, next_pool->pool_size, next_pool->data,
215209
ptr)) {
216-
DEBUG_DEC_VAR(pool->metadata.global_n_allocs);
210+
_DEBUG_EXECUTE(pool->metadata.global_n_allocs--);
217211
next_pool->pool_n_allocs--;
218212
// pool->next_pool is the active pool - we cannot free it
219213
if ((next_pool->pool_n_allocs == 0) &&
220214
next_pool != pool->next_pool) {
221215
assert(prev_pool); // it cannot be the active pool
222216
assert(prev_pool->next_pool == next_pool);
223217
prev_pool->next_pool = next_pool->next_pool;
224-
DEBUG_DEC_VAR(pool->metadata.n_pools);
218+
_DEBUG_EXECUTE(pool->metadata.n_pools--);
225219
void *ptr = next_pool;
226220
size_t size = next_pool->pool_size;
227221
ba_os_free(ptr, size);
228222
}
229-
DEBUG_RUN_CHECKS(pool);
223+
_DEBUG_EXECUTE(ba_debug_checks(pool));
230224
util_mutex_unlock(&pool->metadata.lock);
231225
return 0;
232226
}
@@ -248,7 +242,7 @@ void umf_ba_linear_destroy(umf_ba_linear_pool_t *pool) {
248242
}
249243

250244
#ifndef NDEBUG
251-
DEBUG_RUN_CHECKS(pool);
245+
_DEBUG_EXECUTE(ba_debug_checks(pool));
252246
if (pool->metadata.global_n_allocs) {
253247
fprintf(stderr, "umf_ba_linear_destroy(): global_n_allocs = %zu\n",
254248
pool->metadata.global_n_allocs);

0 commit comments

Comments
 (0)