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 | |
| parent | 74fb835d28f2a4e604a32fd292bb3060a832a1db (diff) | |
pk.h: handle gcc compiler warnings; breaking changes
| -rw-r--r-- | Makefile | 106 | ||||
| -rw-r--r-- | config.mk | 4 | ||||
| -rw-r--r-- | pkev.h | 3 | ||||
| -rw-r--r-- | pkmem-types.h | 8 | ||||
| -rw-r--r-- | pkmem.h | 83 | ||||
| -rw-r--r-- | pkstr.h | 40 | ||||
| -rw-r--r-- | test/pkarr.cpp | 2 | ||||
| -rw-r--r-- | test/pkev.cpp | 18 | ||||
| -rw-r--r-- | test/pkmacros.cpp | 32 | ||||
| -rw-r--r-- | test/pkmem-types.cpp | 16 |
10 files changed, 163 insertions, 149 deletions
@@ -3,7 +3,7 @@ include config.mk -.PHONY: pkmacros pkmem-types pkmem pkstr pkarr +.PHONY: pkmacros pkmem-types pkmem pkstr pkev pkarr pkstn pktmr test SRC = \ pkmacros.h \ @@ -44,19 +44,19 @@ all: options .WAIT clean .WAIT \ pkarr \ pkstn \ pktmr \ - test-pkmem-types test-pkmem-types-cpp \ - test-pkmem test-pkmem-cpp \ - test-pkmacros test-pkmacros-cpp \ - test-pkstr test-pkstr-cpp \ - test-pkev test-pkev-cpp \ - test-pkarr test-pkarr-cpp \ - test-pkstn test-pkstn-cpp \ - test-pktmr test-pktmr-cpp \ + test/test-pkmem-types test/test-pkmem-types-cpp \ + test/test-pkmem test/test-pkmem-cpp \ + test/test-pkmacros test/test-pkmacros-cpp \ + test/test-pkstr test/test-pkstr-cpp \ + test/test-pkev test/test-pkev-cpp \ + test/test-pkarr test/test-pkarr-cpp \ + test/test-pkstn test/test-pkstn-cpp \ + test/test-pktmr test/test-pktmr-cpp \ options: @echo at-suite build options: @echo "CFLAGS = $(CFLAGS)" - @echo "CPPFLAGS = $(CPPFLAGS)" + @echo "CXXFLAGS = $(CXXFLAGS)" @echo "LDFLAGS = $(LDFLAGS)" @echo "CC = $(CC)" @echo "CXX = $(CXX)" @@ -64,11 +64,11 @@ options: %.gch: %.h $(CC) -std=c2x $(CFLAGS) -c $< -o $@ %.gchpp: %.h - $(CXX) -std=c++23 $(CPPFLAGS) -x c++-header -c $< -o $@ + $(CXX) -std=c++23 $(CXXFLAGS) -x c++-header -c $< -o $@ %.o: %.c - $(CC) -std=c2x $(CFLAGS) -g -O0 -c $< -o $@ + $(CC) -std=c2x $(CFLAGS) -g -O3 -c $< -o $@ %.so: %.cpp - $(CXX) -std=c++23 $(CPPFLAGS) -g -O0 -c $< -o $@ + $(CXX) -std=c++23 $(CXXFLAGS) -g -O3 -c $< -o $@ $(OBJ): config.mk $(PPOBJ): config.mk @@ -119,63 +119,63 @@ build: pktmr pk.h -test-pkmacros: test/pkmacros.o - $(CC) -g -O0 -std=c2x $(CFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pkmacros: test/pkmacros.o + $(CC) -g -O3 -std=c2x $(CFLAGS) -o $@ $^ $(LDFLAGS) -test-pkmacros-cpp: test/pkmacros.so - $(CXX) -g -O0 -std=c++23 $(CPPFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pkmacros-cpp: test/pkmacros.so + $(CXX) -g -O3 -std=c++23 $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -test-pkmem-types: test/pkmem-types.o - $(CC) -g -O0 -std=c2x $(CFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pkmem-types: test/pkmem-types.o + $(CC) -g -O3 -std=c2x $(CFLAGS) -o $@ $^ $(LDFLAGS) -test-pkmem-types-cpp: test/pkmem-types.so - $(CXX) -g -O0 -std=c++23 $(CPPFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pkmem-types-cpp: test/pkmem-types.so + $(CXX) -g -O3 -std=c++23 $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -test-pkmem: test/pkmem.o - $(CC) -g -O0 -std=c2x $(CFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pkmem: test/pkmem.o + $(CC) -g -O3 -std=c2x $(CFLAGS) -o $@ $^ $(LDFLAGS) -test-pkmem-cpp: test/pkmem.so - $(CXX) -g -O0 -std=c++23 $(CPPFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pkmem-cpp: test/pkmem.so + $(CXX) -g -O3 -std=c++23 $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -test-pkstr: test/pkstr.o - $(CC) -g -O0 -std=c2x $(CFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pkstr: test/pkstr.o + $(CC) -g -O3 -std=c2x $(CFLAGS) -o $@ $^ $(LDFLAGS) -test-pkstr-cpp: test/pkstr.so - $(CXX) -g -O0 -std=c++23 $(CPPFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pkstr-cpp: test/pkstr.so + $(CXX) -g -O3 -std=c++23 $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -test-pkev: test/pkev.o - $(CC) -g -O0 -std=c2x $(CFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pkev: test/pkev.o + $(CC) -g -O3 -std=c2x $(CFLAGS) -o $@ $^ $(LDFLAGS) -test-pkev-cpp: test/pkev.so - $(CXX) -g -O0 -std=c++23 $(CPPFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pkev-cpp: test/pkev.so + $(CXX) -g -O3 -std=c++23 $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -test-pkarr: test/pkarr.o - $(CC) -g -O0 -std=c2x $(CFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pkarr: test/pkarr.o + $(CC) -g -O3 -std=c2x $(CFLAGS) -o $@ $^ $(LDFLAGS) -test-pkarr-cpp: test/pkarr.so - $(CXX) -g -O0 -std=c++23 $(CPPFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pkarr-cpp: test/pkarr.so + $(CXX) -g -O3 -std=c++23 $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -test-pkstn: test/pkstn.o - $(CC) -g -O0 -std=c2x $(CFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pkstn: test/pkstn.o + $(CC) -g -O3 -std=c2x $(CFLAGS) -o $@ $^ $(LDFLAGS) -test-pkstn-cpp: test/pkstn.so - $(CXX) -g -O0 -std=c++23 $(CPPFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pkstn-cpp: test/pkstn.so + $(CXX) -g -O3 -std=c++23 $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -test-pktmr: test/pktmr.o - $(CC) -g -O0 -std=c2x $(CFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pktmr: test/pktmr.o + $(CC) -g -O3 -std=c2x $(CFLAGS) -o $@ $^ $(LDFLAGS) -test-pktmr-cpp: test/pktmr.so - $(CXX) -g -O0 -std=c++23 $(CPPFLAGS) -o test/$@ $^ $(LDFLAGS) +test/test-pktmr-cpp: test/pktmr.so + $(CXX) -g -O3 -std=c++23 $(CXXFLAGS) -o $@ $^ $(LDFLAGS) test: pkmacros pkmem-types pkmem pkstr pkev pkarr -test: test-pkmacros test-pkmacros-cpp -test: test-pkmem-types test-pkmem-types-cpp -test: test-pkmem test-pkmem-cpp -test: test-pkstr test-pkstr-cpp -test: test-pkev test-pkev-cpp -test: test-pkarr test-pkarr-cpp -test: test-pkstn test-pkstn-cpp -test: test-pktmr test-pktmr-cpp +test: test/test-pkmacros test/test-pkmacros-cpp +test: test/test-pkmem-types test/test-pkmem-types-cpp +test: test/test-pkmem test/test-pkmem-cpp +test: test/test-pkstr test/test-pkstr-cpp +test: test/test-pkev test/test-pkev-cpp +test: test/test-pkarr test/test-pkarr-cpp +test: test/test-pkstn test/test-pkstn-cpp +test: test/test-pktmr test/test-pktmr-cpp test: @echo "" ./test/test-pkmacros ; echo Result: $$? "\n" @@ -23,8 +23,8 @@ SHARED_FLAGS = -D_DEFAULT_SOURCE -D_POSIX_C_SOURCE=200809L \ -DPK_IMPL_ARR \ -DPK_IMPL_STN \ -CPPFLAGS += -Wall $(INCS) $(SHARED_FLAGS) -CFLAGS += -Wall $(INCS) $(SHARED_FLAGS) +CFLAGS += -Wall -pedantic $(INCS) $(SHARED_FLAGS) +CXXFLAGS += -Wall -pedantic $(INCS) $(SHARED_FLAGS) LDFLAGS = $(LIBS) # compiler and linker @@ -95,7 +95,7 @@ pk_ev_init() void pk_ev_teardown() { - int i; + long unsigned int i; for (i = 0; i < pk_ev_mstr.rn_mgrs; ++i) { if ((atomic_load(&pk_ev_mstr.rn_mgrs) & (1lu << i)) == 0) continue; mtx_lock(&pk_ev_mstr.mtxs[i]); @@ -124,7 +124,6 @@ pk_ev_inner_ev_mgr_create(uint64_t ev_count, uint64_t cb_count) struct pk_ev_mgr *mgr = (struct pk_ev_mgr*)malloc(sz); if (mgr == NULL) goto early_exit; - memset(mgr, 0, sz); mgr->ev = (struct pk_ev*)(((char *)mgr) + sizeof(struct pk_ev_mgr)); atomic_init(&mgr->rn_ev, ev_count); atomic_init(&mgr->rn_cb, cb_count); diff --git a/pkmem-types.h b/pkmem-types.h index cf779d9..7f7a2ac 100644 --- a/pkmem-types.h +++ b/pkmem-types.h @@ -18,13 +18,17 @@ struct pk_handle { pk_handle_item_index_T itemIndex; }; -#define PK_HANDLE_MAX ((struct pk_handle){ .bucketIndex = 0xFFFFFFFF, .itemIndex = 0xFFFFFFFF }) +#if ! defined(__cplusplus) + #define PK_HANDLE_MAX ((struct pk_handle){ .bucketIndex = 0xFFFFFFFF, .itemIndex = 0xFFFFFFFF }) +#else + #define PK_HANDLE_MAX (pk_handle{ 0xFFFFFFFF, 0xFFFFFFFF }) +#endif enum PK_HANDLE_VALIDATION pk_handle_validate(const struct pk_handle handle, const struct pk_handle bucketHandle, const uint64_t maxItems); #if defined(__cplusplus) -constexpr struct pk_handle pk_handle_MAX_constexpr = (struct pk_handle){ .bucketIndex = 0xFFFFFFFF, .itemIndex = 0xFFFFFFFF }; +constexpr struct pk_handle pk_handle_MAX_constexpr = { 0xFFFFFFFF, 0xFFFFFFFF }; inline constexpr bool operator==(const pk_handle& lhs, const pk_handle& rhs) @@ -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 */ @@ -31,41 +31,41 @@ int pk_compare_cstr(const struct pk_cstr *lhs, const struct pk_cstr *rhs); struct pk_str cstring_to_pk_str(char *s) { - return (struct pk_str) { - .val = s, - .length = (uint32_t)(strlen(s)), - .reserved = 0, - }; + struct pk_str ret; + ret.val = s; + ret.length = (uint32_t)(strlen(s)); + ret.reserved = 0; + return ret; } struct pk_cstr cstring_to_pk_cstr(const char *s) { - return (struct pk_cstr) { - .val = s, - .length = (uint32_t)(strlen(s)), - .reserved = 0, - }; + struct pk_cstr ret; + ret.val = s; + ret.length = (uint32_t)(strlen(s)); + ret.reserved = 0; + return ret; } struct pk_str pk_cstr_to_pk_str(const struct pk_cstr *s) { - return (struct pk_str) { - .val = (char *)(s->val), - .length = s->length, - .reserved = s->reserved, - }; + struct pk_str ret; + ret.val = (char *)s->val; + ret.length = s->length; + ret.reserved = s->reserved; + return ret; } struct pk_cstr pk_str_to_pk_cstr(const struct pk_str *s) { - return (struct pk_cstr) { - .val = (char *)(s->val), - .length = s->length, - .reserved = s->reserved, - }; + struct pk_cstr ret; + ret.val = (char *)s->val; + ret.length = s->length; + ret.reserved = s->reserved; + return ret; } int diff --git a/test/pkarr.cpp b/test/pkarr.cpp index d60d90c..306dd51 100644 --- a/test/pkarr.cpp +++ b/test/pkarr.cpp @@ -1,4 +1,6 @@ +#include <cstring> + #include "../pkarr.h" void diff --git a/test/pkev.cpp b/test/pkev.cpp index 652f020..dd227ce 100644 --- a/test/pkev.cpp +++ b/test/pkev.cpp @@ -50,13 +50,13 @@ test_setup() return evmgr; } -struct cb_data { +typedef struct cb_data { int i; struct ev *ev; -}; +} cb_data; void stress_cb(void *, void *, void *user_data) { - struct cb_data *data = reinterpret_cast<struct cb_data*>(user_data); + cb_data *data = reinterpret_cast<cb_data*>(user_data); data->ev[data->i].handled = true; data->ev[data->i].count += 1; } @@ -70,7 +70,7 @@ int main(int argc, char *argv[]) (void)argc; (void)argv; (void)stdout; - int i, ii; + size_t i, ii; // register, emit, catch @@ -84,7 +84,7 @@ int main(int argc, char *argv[]) pk_ev_register_cb(evmgr, ev_two.evid, &invoke_packged_task, NULL); pk_ev_emit(evmgr, ev_one.evid, &handle_ev_one); pk_ev_emit(evmgr, ev_two.evid, &handle_ev_two); - PK_LOGV_INF("%s: ev_one: %b, ev_two: %b\n", __FILE__, ev_one.handled, ev_two.handled); + PK_LOGV_INF("%s: ev_one: %s, ev_two: %s\n", __FILE__, ev_one.handled ? "true" : "false", ev_two.handled ? "true" : "false"); pk_ev_teardown(); fflush(stdout); fflush(stderr); @@ -104,7 +104,7 @@ int main(int argc, char *argv[]) auto t2 = std::thread([&handle_ev_two]() { pk_ev_emit(ev_two.evmgr, ev_two.evid, &handle_ev_two); }); t1.join(); t2.join(); - PK_LOGV_INF("%s: ev_one: %b, ev_two: %b\n", __FILE__, ev_one.handled, ev_two.handled); + PK_LOGV_INF("%s: ev_one: %s, ev_two: %s\n", __FILE__, ev_one.handled ? "true" : "false", ev_two.handled ? "true" : "false"); pk_ev_teardown(); fflush(stdout); fflush(stderr); @@ -126,7 +126,7 @@ int main(int argc, char *argv[]) } for (i = 0; i < ev_count; ++i) { - struct cb_data *d = new struct cb_data{}; + cb_data *d = new cb_data{}; d->ev = evs; d->i = i; pk_ev_emit(evs[i].evmgr, evs[i].evid, d); @@ -134,7 +134,7 @@ int main(int argc, char *argv[]) bool any_false = false; for (i = 0; i < ev_count; ++i) { - PK_LOGV_INF("%s: ev# %.2i: %b, called count: %i\n", __FILE__, i, evs[i].handled, evs[i].count); + PK_LOGV_INF("%s: ev# %.2zu: %s, called count: %i\n", __FILE__, i, evs[i].handled ? "true" : "false", evs[i].count); any_false = any_false || !evs[i].handled; } pk_ev_teardown(); @@ -158,7 +158,7 @@ int main(int argc, char *argv[]) } for (i = 0; i < ev_count; ++i) { - struct cb_data *d = new struct cb_data{}; + cb_data *d = new cb_data{}; d->ev = evs; d->i = i; pk_ev_emit(evs[i].evmgr, evs[i].evid, d); diff --git a/test/pkmacros.cpp b/test/pkmacros.cpp index 5e5377f..8edcbfd 100644 --- a/test/pkmacros.cpp +++ b/test/pkmacros.cpp @@ -136,8 +136,8 @@ int main(int argc, char *argv[]) // MISC { - PK_LOGV_INF("PK_HAS_FLAG 000: %b\n", PK_HAS_FLAG(0xFF, 5)); - PK_LOGV_INF("PK_HAS_FLAG 001: %b\n", PK_HAS_FLAG(0x0F, 5)); + PK_LOGV_INF("PK_HAS_FLAG 000: %s\n", PK_HAS_FLAG(0xFF, 5) ? "true" : "false"); + PK_LOGV_INF("PK_HAS_FLAG 001: %s\n", PK_HAS_FLAG(0x0F, 5) ? "true" : "false"); PK_LOGV_INF("PK_CLAMP 000: %i\n", PK_CLAMP(0, -10, 10)); PK_LOGV_INF("PK_CLAMP 001: %i\n", PK_CLAMP(-20, -10, 10)); PK_LOGV_INF("PK_CLAMP 002: %i\n", PK_CLAMP(20, -10, 10)); @@ -154,9 +154,9 @@ int main(int argc, char *argv[]) { bool b = false; if IS_CONSTRUCTIBLE(sdc) b = true; - fprintf(stdout, "class sdc IS_CONSTRUCTIBLE: %b\n", b); + fprintf(stdout, "class sdc IS_CONSTRUCTIBLE: %s\n", b ? "true" : "false"); if IS_DESTRUCTIBLE(sdc) b = false; - fprintf(stdout, "class sdc IS_DESTRUCTIBLE: %b\n", b); + fprintf(stdout, "class sdc IS_DESTRUCTIBLE: %s\n", b ? "true" : "false"); } // TypeSafeInt @@ -169,22 +169,22 @@ int main(int argc, char *argv[]) PK_LOGV_INF(PK_TO_BIN_PAT_16"\n", PK_TO_BIN_16(static_cast<MyConstexprTSI_16_T>(MyConstexprTSI_16_MAX))); PK_LOGV_INF(PK_TO_BIN_PAT_32"\n", PK_TO_BIN_32(static_cast<MyConstexprTSI_32_T>(MyConstexprTSI_32_MAX))); PK_LOGV_INF(PK_TO_BIN_PAT_64"\n", PK_TO_BIN_64(static_cast<MyConstexprTSI_64_T>(MyConstexprTSI_64_MAX))); - PK_LOGV_INF("tsi_test_operators 08: %b\n", tsi_test_operators<MyTSI_8, MyTSI_8_T>()); - PK_LOGV_INF("tsi_test_operators 16: %b\n", tsi_test_operators<MyTSI_16, MyTSI_16_T>()); - PK_LOGV_INF("tsi_test_operators 32: %b\n", tsi_test_operators<MyTSI_32, MyTSI_32_T>()); - PK_LOGV_INF("tsi_test_operators 64: %b\n", tsi_test_operators<MyTSI_64, MyTSI_64_T>()); - PK_LOGV_INF("tsi_test_operators c08: %b\n", tsi_test_operators<MyConstexprTSI_8, MyConstexprTSI_8_T>()); - PK_LOGV_INF("tsi_test_operators c16: %b\n", tsi_test_operators<MyConstexprTSI_16, MyConstexprTSI_16_T>()); - PK_LOGV_INF("tsi_test_operators c32: %b\n", tsi_test_operators<MyConstexprTSI_32, MyConstexprTSI_32_T>()); - PK_LOGV_INF("tsi_test_operators c64: %b\n", tsi_test_operators<MyConstexprTSI_64, MyConstexprTSI_64_T>()); + PK_LOGV_INF("tsi_test_operators 08: %s\n", tsi_test_operators<MyTSI_8, MyTSI_8_T>() ? "true" : "false"); + PK_LOGV_INF("tsi_test_operators 16: %s\n", tsi_test_operators<MyTSI_16, MyTSI_16_T>() ? "true" : "false"); + PK_LOGV_INF("tsi_test_operators 32: %s\n", tsi_test_operators<MyTSI_32, MyTSI_32_T>() ? "true" : "false"); + PK_LOGV_INF("tsi_test_operators 64: %s\n", tsi_test_operators<MyTSI_64, MyTSI_64_T>() ? "true" : "false"); + PK_LOGV_INF("tsi_test_operators c08: %s\n", tsi_test_operators<MyConstexprTSI_8, MyConstexprTSI_8_T>() ? "true" : "false"); + PK_LOGV_INF("tsi_test_operators c16: %s\n", tsi_test_operators<MyConstexprTSI_16, MyConstexprTSI_16_T>() ? "true" : "false"); + PK_LOGV_INF("tsi_test_operators c32: %s\n", tsi_test_operators<MyConstexprTSI_32, MyConstexprTSI_32_T>() ? "true" : "false"); + PK_LOGV_INF("tsi_test_operators c64: %s\n", tsi_test_operators<MyConstexprTSI_64, MyConstexprTSI_64_T>() ? "true" : "false"); constexpr bool b1 = tsi_test_operators_constexpr<MyConstexprTSI_8, MyConstexprTSI_8_T>(); constexpr bool b2 = tsi_test_operators_constexpr<MyConstexprTSI_16, MyConstexprTSI_16_T>(); constexpr bool b3 = tsi_test_operators_constexpr<MyConstexprTSI_32, MyConstexprTSI_32_T>(); constexpr bool b4 = tsi_test_operators_constexpr<MyConstexprTSI_64, MyConstexprTSI_64_T>(); - PK_LOGV_INF("TypeSafeInt_constexpr_08: %b\n", b1); - PK_LOGV_INF("TypeSafeInt_constexpr_16: %b\n", b2); - PK_LOGV_INF("TypeSafeInt_constexpr_32: %b\n", b3); - PK_LOGV_INF("TypeSafeInt_constexpr_64: %b\n", b4); + PK_LOGV_INF("TypeSafeInt_constexpr_08: %s\n", b1 ? "true" : "false"); + PK_LOGV_INF("TypeSafeInt_constexpr_16: %s\n", b2 ? "true" : "false"); + PK_LOGV_INF("TypeSafeInt_constexpr_32: %s\n", b3 ? "true" : "false"); + PK_LOGV_INF("TypeSafeInt_constexpr_64: %s\n", b4 ? "true" : "false"); } return 0; diff --git a/test/pkmem-types.cpp b/test/pkmem-types.cpp index 0dc0f90..34ef102 100644 --- a/test/pkmem-types.cpp +++ b/test/pkmem-types.cpp @@ -18,26 +18,26 @@ int main(int argc, char *argv[]) constexpr struct pk_handle ch1 { .bucketIndex = 0xAAAAAAAA, .itemIndex = 0x55555555 }; constexpr struct pk_handle cbh1 { .bucketIndex = 0xAAAAAAAA, .itemIndex = 0x55555555 }; constexpr bool ret1 = (ch1 == cbh1); - PK_LOGV_INF("pk_handle constexpr operator== 000: %b\n", ret1); + PK_LOGV_INF("pk_handle constexpr operator== 000: %s\n", ret1 ? "true" : "false"); constexpr struct pk_handle ch2 { .bucketIndex = 0x00000000, .itemIndex = 0x00000000 }; constexpr struct pk_handle cbh2 { .bucketIndex = 0xFFFFFFFF, .itemIndex = 0xFFFFFFFF }; constexpr bool ret2 = (ch2 == cbh2); - PK_LOGV_INF("pk_handle constexpr operator== 001: %b\n", ret2); + PK_LOGV_INF("pk_handle constexpr operator== 001: %s\n", ret2 ? "true" : "false"); h.bucketIndex = 0xCAFE; h.itemIndex = 0xBABE; bh.bucketIndex = 0xCAFE; bh.itemIndex = 0xBABE; res = h == bh; - PK_LOGV_INF("pk_handle operator== 000: %b\n", res); + PK_LOGV_INF("pk_handle operator== 000: %s\n", res ? "true" : "false"); h.bucketIndex = 0x00000000; h.itemIndex = 0x00000000; bh.bucketIndex = 0xFFFFFFFF; bh.itemIndex = 0xFFFFFFFF; res = h == bh; - PK_LOGV_INF("pk_handle operator== 001: %b\n", res); + PK_LOGV_INF("pk_handle operator== 001: %s\n", res ? "true" : "false"); } // pk_handle_validate_constexpr @@ -70,26 +70,26 @@ int main(int argc, char *argv[]) struct custom_handle ch1; ch1.bucketIndex = 0xAAAAAAAA; ch1.itemIndex = 0x55555555; struct custom_handle cbh1; cbh1.bucketIndex = 0xAAAAAAAA; cbh1.itemIndex = 0x55555555; bool ret1 = (ch1 == cbh1); - PK_LOGV_INF("custom_handle constexpr operator== 000: %b\n", ret1); + PK_LOGV_INF("custom_handle constexpr operator== 000: %s\n", ret1 ? "true" : "false"); struct custom_handle ch2; ch2.bucketIndex = 0x00000000; ch2.itemIndex = 0x00000000; struct custom_handle cbh2; cbh2.bucketIndex = 0xFFFFFFFF; cbh2.itemIndex = 0xFFFFFFFF; bool ret2 = (ch2 == cbh2); - PK_LOGV_INF("custom_handle constexpr operator== 001: %b\n", ret2); + PK_LOGV_INF("custom_handle constexpr operator== 001: %s\n", ret2 ? "true" : "false"); h.bucketIndex = 0xCAFE; h.itemIndex = 0xBABE; bh.bucketIndex = 0xCAFE; bh.itemIndex = 0xBABE; res = h == bh; - PK_LOGV_INF("custom_handle operator== 000: %b\n", res); + PK_LOGV_INF("custom_handle operator== 000: %s\n", res ? "true" : "false"); h.bucketIndex = 0x00000000; h.itemIndex = 0x00000000; bh.bucketIndex = 0xFFFFFFFF; bh.itemIndex = 0xFFFFFFFF; res = h == bh; - PK_LOGV_INF("custom_handle operator== 001: %b\n", res); + PK_LOGV_INF("custom_handle operator== 001: %s\n", res ? "true" : "false"); } return 0; |
