Skip to content

Commit ac4bfb2

Browse files
committed
Add debug macros to base_alloc_linear.c
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 1d79baa commit ac4bfb2

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/base_alloc/base_alloc_linear.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313
#include "utils_common.h"
1414
#include "utils_concurrency.h"
1515

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+
#else
21+
#define DEBUG_RUN_CHECKS(pool) DO_WHILE_EMPTY
22+
#define DEBUG_SET_VAR(var, value) DO_WHILE_EMPTY
23+
#define DEBUG_INC_VAR(var) DO_WHILE_EMPTY
24+
#endif /* NDEBUG */
25+
1626
// minimum size of a single pool of the linear base allocator
1727
#define MINIMUM_LINEAR_POOL_SIZE (ba_os_get_page_size())
1828

@@ -94,9 +104,7 @@ umf_ba_linear_pool_t *umf_ba_linear_create(size_t pool_size) {
94104
pool->metadata.data_ptr = data_ptr;
95105
pool->metadata.size_left = size_left;
96106
pool->next_pool = NULL; // this is the only pool now
97-
#ifndef NDEBUG
98-
pool->metadata.n_pools = 1;
99-
#endif /* NDEBUG */
107+
DEBUG_SET_VAR(pool->metadata.n_pools, 1);
100108

101109
// init lock
102110
os_mutex_t *lock = util_mutex_init(&pool->metadata.lock);
@@ -143,18 +151,14 @@ void *umf_ba_linear_alloc(umf_ba_linear_pool_t *pool, size_t size) {
143151
// add the new pool to the list of pools
144152
new_pool->next_pool = pool->next_pool;
145153
pool->next_pool = new_pool;
146-
#ifndef NDEBUG
147-
pool->metadata.n_pools++;
148-
#endif /* NDEBUG */
154+
DEBUG_INC_VAR(pool->metadata.n_pools);
149155
}
150156

151157
assert(pool->metadata.size_left >= aligned_size);
152158
void *ptr = pool->metadata.data_ptr;
153159
pool->metadata.data_ptr += aligned_size;
154160
pool->metadata.size_left -= aligned_size;
155-
#ifndef NDEBUG
156-
ba_debug_checks(pool);
157-
#endif /* NDEBUG */
161+
DEBUG_RUN_CHECKS(pool);
158162
util_mutex_unlock(&pool->metadata.lock);
159163

160164
return ptr;
@@ -168,9 +172,7 @@ void umf_ba_linear_destroy(umf_ba_linear_pool_t *pool) {
168172
return;
169173
}
170174

171-
#ifndef NDEBUG
172-
ba_debug_checks(pool);
173-
#endif /* NDEBUG */
175+
DEBUG_RUN_CHECKS(pool);
174176

175177
umf_ba_next_linear_pool_t *current_pool;
176178
umf_ba_next_linear_pool_t *next_pool = pool->next_pool;

src/utils/utils_common.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
extern "C" {
2020
#endif
2121

22+
#define DO_WHILE_EMPTY \
23+
do { \
24+
} while (0)
25+
#define DO_WHILE_EXPRS(expression) \
26+
do { \
27+
expression; \
28+
} while (0)
29+
2230
#ifdef _WIN32 /* Windows */
2331

2432
#define __TLS __declspec(thread)

0 commit comments

Comments
 (0)