Skip to content

Commit 531453f

Browse files
authored
Merge pull request #218 from igchor/add_more_debug_checks
Add extra debug check for base_alloc
2 parents 8931442 + 2c94f60 commit 531453f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/base_alloc/base_alloc.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,29 @@ void *umf_ba_alloc(umf_ba_pool_t *pool) {
196196
return chunk;
197197
}
198198

199+
#ifndef NDEBUG
200+
// Checks if given pointer belongs to the pool. Should be called
201+
// under the lock
202+
static int pool_contains_pointer(umf_ba_pool_t *pool, void *ptr) {
203+
char *cptr = (char *)ptr;
204+
if (cptr >= pool->data &&
205+
cptr < ((char *)(pool)) + pool->metadata.pool_size) {
206+
return 1;
207+
}
208+
209+
umf_ba_next_pool_t *next_pool = pool->next_pool;
210+
while (next_pool) {
211+
if (cptr >= next_pool->data &&
212+
cptr < ((char *)(next_pool)) + pool->metadata.pool_size) {
213+
return 1;
214+
}
215+
next_pool = next_pool->next_pool;
216+
}
217+
218+
return 0;
219+
}
220+
#endif
221+
199222
void umf_ba_free(umf_ba_pool_t *pool, void *ptr) {
200223
if (ptr == NULL) {
201224
return;
@@ -204,6 +227,7 @@ void umf_ba_free(umf_ba_pool_t *pool, void *ptr) {
204227
umf_ba_chunk_t *chunk = (umf_ba_chunk_t *)ptr;
205228

206229
util_mutex_lock(&pool->metadata.free_lock);
230+
assert(pool_contains_pointer(pool, ptr));
207231
chunk->next = pool->metadata.free_list;
208232
pool->metadata.free_list = chunk;
209233
#ifndef NDEBUG

0 commit comments

Comments
 (0)