Skip to content

Commit 758d125

Browse files
committed
Add extra debug check for base_alloc
that verifies that ptr belongs to a specific pool.
1 parent 4c95ea8 commit 758d125

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/base_alloc/base_alloc.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,28 @@ 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 && cptr < ((char *)(pool)) + pool->metadata.pool_size) {
205+
return 1;
206+
}
207+
208+
umf_ba_next_pool_t *next_pool = pool->next_pool;
209+
while (next_pool) {
210+
if (cptr >= next_pool->data &&
211+
cptr < ((char *)(next_pool)) + pool->metadata.pool_size) {
212+
return 1;
213+
}
214+
next_pool = next_pool->next_pool;
215+
}
216+
217+
return 0;
218+
}
219+
#endif
220+
199221
void umf_ba_free(umf_ba_pool_t *pool, void *ptr) {
200222
if (ptr == NULL) {
201223
return;
@@ -204,6 +226,7 @@ void umf_ba_free(umf_ba_pool_t *pool, void *ptr) {
204226
umf_ba_chunk_t *chunk = (umf_ba_chunk_t *)ptr;
205227

206228
util_mutex_lock(&pool->metadata.free_lock);
229+
assert(pool_contains_pointer(pool, ptr));
207230
chunk->next = pool->metadata.free_list;
208231
pool->metadata.free_list = chunk;
209232
#ifndef NDEBUG

0 commit comments

Comments
 (0)