diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2024-11-22 14:32:51 -0500 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2024-11-22 16:35:43 -0500 |
| commit | 31275f4cee6f0593fc39fecef04477d4a26e8df4 (patch) | |
| tree | 5ae3dfc297fb3729ecf74b0e8a52bb7cf3ddc574 /pkmem.h | |
| parent | f23b4ce2dd648174a5df0e259faf7209c5d4c653 (diff) | |
pkmem.h: test for expected assert failures
Diffstat (limited to 'pkmem.h')
| -rw-r--r-- | pkmem.h | 14 |
1 files changed, 10 insertions, 4 deletions
@@ -221,9 +221,9 @@ pk_memory_flush() void pk_memory_teardown_all() { - for (int64_t i = pk_bucket_head - 1; i > 0; --i) { - if (pk_buckets[i].ptr == nullptr) continue; - pk_bucket_destroy(&pk_buckets[i]); + for (int64_t i = pk_bucket_head; i > 0; --i) { + if (pk_buckets[i - 1].ptr == nullptr) continue; + pk_bucket_destroy(&pk_buckets[i - 1]); } pk_bucket_head = 0; } @@ -231,6 +231,7 @@ pk_memory_teardown_all() static int64_t pk_bucket_create_inner(int64_t sz, bool transient, const char* description) { + assert(pk_bucket_head < PK_MAX_BUCKET_COUNT && "pkmem.h: reserved bucket count exceeded"); #ifdef PK_MEMORY_DEBUGGER if (has_init_debug == false) { has_init_debug = true; @@ -246,6 +247,7 @@ pk_bucket_create_inner(int64_t sz, bool transient, const char* description) bkt->lastEmptyBlockIndex = 0; bkt->maxBlockCount = blockCount < 10 ? 10 : blockCount; bkt->blocks = (struct pk_memblock*)malloc(sz); + mtx_init(&bkt->mtx, mtx_plain); assert(bkt->blocks != nullptr && "failed to allocate memory"); #if 1 memset(bkt->blocks, 0, sz); @@ -292,6 +294,7 @@ pk_bucket_destroy(struct pk_membucket* bkt) bkt->blocks = CAFE_BABE(struct pk_memblock); bkt->ptr = CAFE_BABE(char); bkt->transient = false; + mtx_destroy(&bkt->mtx); #ifdef PK_MEMORY_DEBUGGER for (i = debug_alloc_head; i > -1; --i) { if (debug_all_allocs[i].bkt == bkt) { @@ -394,7 +397,10 @@ pk_new_bkt(size_t sz, size_t alignment, struct pk_membucket* bkt) break; } } - assert(block != nullptr && "memory corruption: failed to find bucket with enough space"); + if (block == nullptr) { + mtx_unlock(&bkt->mtx); + assert(block != nullptr && "memory corruption: not enough space in chosen bkt"); + } data = block->data + misalignment; #ifdef PK_MEMORY_DEBUGGER bool handled = bkt->transient; |
