diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-01-10 10:08:05 -0500 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-01-10 10:08:05 -0500 |
| commit | 79e040d203e63ec79bb124215dcd1e940f7b676c (patch) | |
| tree | 23c11c6911b99161d0063e63122d9dd15485e1ed /pkmem.h | |
| parent | 74fb835d28f2a4e604a32fd292bb3060a832a1db (diff) | |
pk.h: handle gcc compiler warnings; breaking changes
Diffstat (limited to 'pkmem.h')
| -rw-r--r-- | pkmem.h | 83 |
1 files changed, 46 insertions, 37 deletions
@@ -133,19 +133,19 @@ struct pk_memblock { struct pk_membucket { // the total size of the bucket, `blocks+ptr` - int64_t size; + size_t size; // the current head of the bucket: byte offset from `ptr`. // All currently alloc'd data is before this offset - int64_t head; + size_t head; // amount of lost bytes in this membucket, hopefully zero - int64_t lostBytes; + size_t lostBytes; // the number of active allocations from this bucket - int64_t allocs; + size_t allocs; // the index of the last empty block. // Should always point to `pk_memblock{ .data = ptr+head, .size=size-head }` - int64_t lastEmptyBlockIndex; + size_t lastEmptyBlockIndex; // number of pk_memblocks in the `*blocks` array - int64_t maxBlockCount; + size_t maxBlockCount; // ptr to an array of pk_memblock to track ALL free space between ptr and ptr+sz struct pk_memblock* blocks; // starting point for alloc'd data @@ -159,7 +159,7 @@ struct pk_membucket { }; static struct pk_membucket pk_buckets[PK_MAX_BUCKET_COUNT]; -static int64_t pk_bucket_head = 0; +static size_t pk_bucket_head = 0; #ifdef PK_MEMORY_DEBUGGER struct pk_dbg_memblock { @@ -167,7 +167,7 @@ struct pk_dbg_memblock { struct pk_membucket *bkt; }; static struct pk_dbg_memblock debug_all_allocs[1024 * 1024]; -static int64_t debug_alloc_head = 0; +static size_t debug_alloc_head = 0; static bool has_init_debug = false; #endif @@ -182,7 +182,7 @@ void pk_memory_debug_print() { PK_LOGV_INF("Memory Manager printout:\nBucket count: %li\n", pk_bucket_head); - for (long i = 0; i < pk_bucket_head; ++i) { + for (size_t i = 0; i < pk_bucket_head; ++i) { PK_LOGV_INF("- bucket #%li\n", i); PK_LOGV_INF("\tdescription: %s\n", pk_buckets[i].description); PK_LOGV_INF("\tsize: %li\n", pk_buckets[i].size); @@ -191,12 +191,12 @@ pk_memory_debug_print() PK_LOGV_INF("\tallocs: %li\n", pk_buckets[i].allocs); PK_LOGV_INF("\tlastEmptyBlockIndex: %li\n", pk_buckets[i].lastEmptyBlockIndex); PK_LOGV_INF("\tmaxBlockCount: %li\n", pk_buckets[i].maxBlockCount); - PK_LOGV_INF("\tblocks: %p\n", pk_buckets[i].blocks); - PK_LOGV_INF("\tptr: %p\n", pk_buckets[i].ptr); + PK_LOGV_INF("\tblocks: %p\n", (void *)pk_buckets[i].blocks); + PK_LOGV_INF("\tptr: %p\n", (void *)pk_buckets[i].ptr); PK_LOGV_INF("\ttransient: %i\n", pk_buckets[i].transient); #ifdef PK_MEMORY_DEBUGGER uint64_t count = 0; - for (int64_t d = 0; d < debug_alloc_head; ++d) { + for (size_t d = 0; d < debug_alloc_head; ++d) { if (debug_all_allocs[d].bkt == &pk_buckets[d] && debug_all_allocs[d].blk.size > 0) { count += 1; } @@ -255,7 +255,7 @@ pk_bucket_create_inner(int64_t sz, bool transient, const char* description) memset(bkt->blocks, 0, sz); #endif bkt->ptr = ((char*)(bkt->blocks)) + (sizeof(struct pk_memblock) * bkt->maxBlockCount); - size_t misalignment = (uint64_t)(bkt->ptr) % PK_MAXIMUM_ALIGNMENT; + size_t misalignment = (size_t)(bkt->ptr) % PK_MAXIMUM_ALIGNMENT; if (misalignment != 0) { size_t moreBlocks = misalignment / sizeof(struct pk_memblock); bkt->maxBlockCount += moreBlocks; @@ -278,7 +278,7 @@ pk_bucket_create(const char* description, int64_t sz, bool transient) void pk_bucket_destroy(struct pk_membucket* bkt) { - int64_t i; + size_t i; for (i = 0; i < pk_bucket_head; ++i) { if (&pk_buckets[i] == bkt) { if (pk_bucket_head == i + 1) @@ -291,14 +291,16 @@ pk_bucket_destroy(struct pk_membucket* bkt) bkt->head = 0; bkt->lostBytes = 0; bkt->allocs = 0; - bkt->lastEmptyBlockIndex = -1; + bkt->lastEmptyBlockIndex = 0; bkt->maxBlockCount = 0; 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) { + size_t ii; + for (ii = debug_alloc_head+1; ii > 0; --ii) { + i = ii-1; if (debug_all_allocs[i].bkt == bkt) { debug_all_allocs[i].blk.data = NULL; debug_all_allocs[i].blk.size = 0u; @@ -354,7 +356,9 @@ pk_bucket_insert_block(struct pk_membucket* bkt, const struct pk_memblock* block void pk_bucket_collapse_empty_blocks(struct pk_membucket* bkt) { - for (int64_t i = bkt->lastEmptyBlockIndex; i > -1; --i) { + size_t i, ii; + for (ii = bkt->lastEmptyBlockIndex+1; ii > 0; --ii) { + i = ii-1; struct pk_memblock* block = &bkt->blocks[i]; if (block->size == 0 && i == bkt->lastEmptyBlockIndex) { block->data = nullptr; @@ -364,7 +368,7 @@ pk_bucket_collapse_empty_blocks(struct pk_membucket* bkt) { if (block->size > 0) { continue; } - for (int64_t k = i; k < bkt->lastEmptyBlockIndex; ++k) { + for (size_t k = i; k < bkt->lastEmptyBlockIndex; ++k) { bkt->blocks[k].data = bkt->blocks[k + 1].data; bkt->blocks[k].size = bkt->blocks[k + 1].size; } @@ -379,6 +383,7 @@ pk_new_bkt(size_t sz, size_t alignment, struct pk_membucket* bkt) return malloc(sz); #endif if (sz == 0) return nullptr; + size_t i; size_t calculatedAlignment = alignment < PK_MINIMUM_ALIGNMENT ? PK_MINIMUM_ALIGNMENT : alignment; size_t misalignment = 0; struct pk_memblock* prevBlock = nullptr; @@ -386,7 +391,7 @@ pk_new_bkt(size_t sz, size_t alignment, struct pk_membucket* bkt) struct pk_memblock* nextBlock = nullptr; void* data = nullptr; mtx_lock(&bkt->mtx); - for (int64_t i = 0; i <= bkt->lastEmptyBlockIndex; ++i) { + for (i = 0; i <= bkt->lastEmptyBlockIndex; ++i) { struct pk_memblock* blk = &bkt->blocks[i]; misalignment = (size_t)(blk->data) % calculatedAlignment; misalignment = (calculatedAlignment - misalignment) % calculatedAlignment; @@ -409,7 +414,7 @@ pk_new_bkt(size_t sz, size_t alignment, struct pk_membucket* bkt) #ifdef PK_MEMORY_DEBUGGER bool handled = bkt->transient; if (handled == false) { - for (int64_t i = 0; i < debug_alloc_head; ++i) { + for (i = 0; i < debug_alloc_head; ++i) { struct pk_dbg_memblock* mb = &debug_all_allocs[i]; if (mb->bkt != NULL) continue; assert((mb->blk.size == 0 || (void*)(mb->blk.data) != data) && "mem address alloc'd twice!"); @@ -423,13 +428,10 @@ pk_new_bkt(size_t sz, size_t alignment, struct pk_membucket* bkt) } } if (handled == false) { - debug_all_allocs[debug_alloc_head++] = (struct pk_dbg_memblock){ - .blk = (struct pk_memblock) { - .data = (char*)(data), - .size = sz, - }, - .bkt = bkt, - }; + i = debug_alloc_head++; + debug_all_allocs[i].blk.data = (char*)data; + debug_all_allocs[i].blk.size = sz; + debug_all_allocs[i].bkt = bkt; } #endif int64_t afterSize = block->size - (misalignment + sz); @@ -462,11 +464,11 @@ pk_new_bkt(size_t sz, size_t alignment, struct pk_membucket* bkt) if (!bkt->transient) { int64_t debug_tracked_alloc_size = 0; int64_t debug_bucket_alloc_size = bkt->size - (sizeof(struct pk_memblock) * bkt->maxBlockCount); - for (int64_t i = 0; i < debug_alloc_head; ++i) { + for (i = 0; i < debug_alloc_head; ++i) { if (debug_all_allocs[i].bkt != bkt) continue; debug_tracked_alloc_size += debug_all_allocs[i].blk.size; } - for (int64_t i = 0; i <= bkt->lastEmptyBlockIndex; ++i) { + for (i = 0; i <= bkt->lastEmptyBlockIndex; ++i) { debug_bucket_alloc_size -= bkt->blocks[i].size; } assert(debug_tracked_alloc_size == debug_bucket_alloc_size && "allocation size mismatch!"); @@ -480,7 +482,7 @@ void* pk_new_base(size_t sz, size_t alignment) { struct pk_membucket* bkt = nullptr; - for (long i = 0; i < pk_bucket_head; ++i) { + for (size_t i = 0; i < pk_bucket_head; ++i) { if (pk_buckets[i].transient == false && pk_buckets[i].size - pk_buckets[i].head > sz + PK_MAXIMUM_ALIGNMENT) { bkt = &pk_buckets[i]; break; @@ -505,13 +507,16 @@ pk_delete_bkt(const void* ptr, size_t sz, struct pk_membucket* bkt) #ifdef PK_MEMORY_FORCE_MALLOC return std::free(const_cast<void*>(ptr)); #endif + size_t i; mtx_lock(&bkt->mtx); assert(ptr >= bkt->raw && (char*)ptr < bkt->ptr + bkt->size && "pointer not in memory bucket range"); assert(sz > 0 && "attempted to free pointer of size 0"); #ifdef PK_MEMORY_DEBUGGER + size_t ii; bool found = bkt->transient; if (found == false) { - for (int64_t i = debug_alloc_head - 1; i > -1; --i) { + for (ii = debug_alloc_head; ii > 0; --ii) { + i = ii-1; struct pk_dbg_memblock* mb = &debug_all_allocs[i]; if (mb->bkt != bkt) continue; if (mb->blk.size == 0) continue; @@ -540,7 +545,7 @@ pk_delete_bkt(const void* ptr, size_t sz, struct pk_membucket* bkt) char* afterPtr = ((char*)(ptr))+sz; struct pk_memblock* beforeBlk = nullptr; struct pk_memblock* afterBlk = nullptr; - for (int64_t i = bkt->lastEmptyBlockIndex; i > 0; --i) { + for (i = bkt->lastEmptyBlockIndex; i > 0; --i) { if (bkt->blocks[i-1].data + bkt->blocks[i-1].size == ptr) { beforeBlk = &bkt->blocks[i-1]; } @@ -581,11 +586,11 @@ pk_delete_bkt(const void* ptr, size_t sz, struct pk_membucket* bkt) if (!bkt->transient) { int64_t debug_tracked_alloc_size = 0; int64_t debug_bucket_alloc_size = bkt->size - (sizeof(struct pk_memblock) * bkt->maxBlockCount); - for (int64_t i = 0; i < debug_alloc_head; ++i) { + for (i = 0; i < debug_alloc_head; ++i) { if (debug_all_allocs[i].bkt != bkt) continue; debug_tracked_alloc_size += debug_all_allocs[i].blk.size; } - for (int64_t i = 0; i <= bkt->lastEmptyBlockIndex; ++i) { + for (i = 0; i <= bkt->lastEmptyBlockIndex; ++i) { debug_bucket_alloc_size -= bkt->blocks[i].size; } assert(debug_tracked_alloc_size == debug_bucket_alloc_size && "allocation size mismatch!"); @@ -598,7 +603,7 @@ void pk_delete_base(const void* ptr, size_t sz) { struct pk_membucket* bkt = nullptr; - for (long i = 0; i < pk_bucket_head; ++i) { + for (size_t i = 0; i < pk_bucket_head; ++i) { bkt = &pk_buckets[i]; if (ptr >= bkt->raw && (char*)ptr < bkt->ptr + bkt->size) break; } @@ -609,8 +614,12 @@ pk_delete_base(const void* ptr, size_t sz) void pk_delete(const void* ptr, size_t sz, struct pk_membucket* bkt) { - if (bkt != NULL) return pk_delete_bkt(ptr, sz, bkt); - return pk_delete_base(ptr, sz); + if (bkt != NULL) { + pk_delete_bkt(ptr, sz, bkt); + return; + } + pk_delete_base(ptr, sz); + return; } #endif /* PK_IMPL_MEM */ |
