summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-06-04 16:45:11 -0400
committerJonathan Bradley <jcb@pikum.xyz>2025-06-04 17:39:05 -0400
commit15518175c10b9bf18b21dac0a5b3d6472b525968 (patch)
treeda17046bb9b5d1ebb56b34586f8be63c9c781a01
parent61cd9162df6424d3a4f427daa1d69476b913aca6 (diff)
pkmem.h: debug on bucket + change signatures
-rw-r--r--pk.h.in13
-rw-r--r--pkmem.h155
-rw-r--r--test/pkarr.c5
-rw-r--r--test/pkarr.cpp4
-rw-r--r--test/pkbktarr.c8
-rw-r--r--test/pkbktarr.cpp8
-rw-r--r--test/pkmem.c10
-rw-r--r--test/pkmem.cpp170
8 files changed, 105 insertions, 268 deletions
diff --git a/pk.h.in b/pk.h.in
index 0503415..7e95dd7 100644
--- a/pk.h.in
+++ b/pk.h.in
@@ -75,20 +75,19 @@
********************************************************************************
* pkmem.h: def PK_IMPL_MEM before including pk.h to enable ad-hoc.
*
-* A bucketed memory manager. Allows for the creation and management of up to a
-* well-defined number of buckets.
+* A bucketed memory manager. Allows for the creation of ad-hoc buckets.
*
-* Thread safety: Bucket creation and destruction is *not* thread-safe. On the
-* other hand, the "pk_new" and "pk_delete" methods *are* thread-safe, but
+* Note: Each created pk_membucket MUST call pk_bucket_destroy(bkt). Memory
+* buckets are client managed.
+*
+* Thread safety: "pk_new" and "pk_delete" methods *are* thread-safe, but
* thread-safety is implemented per-bucket via a single mutex with long-running
* lock times. PRs for a more performant thread-safe strategy are welcome,
* complexity and benchmark depending.
*
* The following definitions (shown with defaults) can be overridden:
-* PK_DEFAULT_BUCKET_SIZE 256MB (used when bkt is NULL on first call)
+* PK_MEM_DEFAULT_BUCKET_SIZE 256MB (client-convenience only)
* PK_MINIMUM_ALIGNMENT 1
-* PK_MAXIMUM_ALIGNMENT 64
-* PK_MAX_BUCKET_COUNT 8
*
* For debugging purposes, define the following:
* PK_MEMORY_DEBUGGER : enables a tracking system for all allocs and frees to
diff --git a/pkmem.h b/pkmem.h
index adfaa36..50e251d 100644
--- a/pkmem.h
+++ b/pkmem.h
@@ -6,25 +6,17 @@
#include <stdint.h>
#include <stdlib.h>
-#ifndef PK_DEFAULT_BUCKET_SIZE
-# define PK_DEFAULT_BUCKET_SIZE (1ULL * 1024ULL * 1024ULL * 256ULL)
+#ifndef PK_MEM_DEFAULT_BUCKET_SIZE
+# define PK_MEM_DEFAULT_BUCKET_SIZE (1ULL * 1024ULL * 1024ULL * 256ULL)
#endif
-#ifndef PK_MINIMUM_ALIGNMENT
-# define PK_MINIMUM_ALIGNMENT 1
-#endif
-#ifndef PK_MAXIMUM_ALIGNMENT
-# define PK_MAXIMUM_ALIGNMENT 64
-#endif
-
-size_t pk_mem_calculate_bkt_size(size_t sz, size_t reserved_block_count);
-struct pk_membucket* pk_bucket_create(const char* description, int64_t sz, enum PK_MEMBUCKET_FLAGS flags);
-void pk_bucket_destroy(struct pk_membucket* bkt);
-void pk_bucket_reset(struct pk_membucket* bkt);
-void pk_bucket_set_client_bucket(struct pk_membucket *bkt);
-void pk_memory_debug_print(struct pk_membucket *bkt);
-void pk_memory_teardown_all();
-bool pk_memory_is_in_bucket(const void* ptr, const struct pk_membucket* bkt);
+size_t pk_mem_bucket_calculate_size(size_t sz, size_t reserved_block_count);
+struct pk_membucket* pk_mem_bucket_create(const char* description, int64_t sz, enum PK_MEMBUCKET_FLAGS flags);
+void pk_mem_bucket_debug_print(struct pk_membucket *bkt);
+void pk_mem_bucket_destroy(struct pk_membucket* bkt);
+void pk_mem_bucket_reset(struct pk_membucket* bkt);
+void pk_mem_bucket_set_client_mem_bucket(struct pk_membucket *bkt);
+bool pk_mem_bucket_ptr_is_in_mem_bucket(const void* ptr, const struct pk_membucket* bkt);
void* pk_new_base(size_t sz, size_t alignment);
void* pk_new_bkt(size_t sz, size_t alignment, struct pk_membucket* bkt);
@@ -121,6 +113,13 @@ static inline void pkmem_stupid_header_warnings() { (void)stdout; }
#if defined(PK_MEMORY_DEBUGGER)
#endif
+#ifndef PK_MINIMUM_ALIGNMENT
+# define PK_MINIMUM_ALIGNMENT 1
+#endif
+#ifndef PK_MEMORY_DEBUGGER_MAX_BUCKET_COUNT
+ #define PK_MEMORY_DEBUGGER_MAX_BUCKET_COUNT 16
+#endif
+
#define EXPECTED_PK_MEMBLOCK_SIZE 128
#define pk_memblock_blocks_idx(bkt, idx) ((bkt->block_capacity-1)-(idx))
@@ -165,17 +164,17 @@ struct pk_membucket {
const char *description;
// 96
#ifdef PK_MEMORY_DEBUGGER
- uint32_t debug_bkt_index;
- // 100
- uint32_t debug_head_l;
+ struct pk_memblock *debug_blocks;
// 104
- uint32_t debug_head_r;
+ uint32_t debug_head_l;
// 108
- char padding[4+(8*1)];
- // 120
- size_t debug_block_capacity;
+ uint32_t debug_head_r;
+ // 112
+ uint32_t debug_block_capacity;
+ // 116
+ char padding[(8*1)+4];
#else
- char padding[8*4];
+ char padding[(8*4)];
#endif
// 128
// starting point for alloc'd data
@@ -185,15 +184,8 @@ struct pk_membucket {
static struct pk_membucket *client_bucket = NULL;
-#ifdef PK_MEMORY_DEBUGGER
-#include <stdatomic.h>
-static struct pk_memblock *debug_allocs[16];
-static size_t debug_alloc_head = 0;
-static mtx_t debug_mtx;
-#endif
-
size_t
-pk_mem_calculate_bkt_size(size_t sz, size_t reserved_block_count)
+pk_mem_bucket_calculate_size(size_t sz, size_t reserved_block_count)
{
size_t base_size = EXPECTED_PK_MEMBLOCK_SIZE + sz + (sizeof(struct pk_memblock) * reserved_block_count);
// This trick ensures that our array of pk_memblocks at the end is mem-aligned.
@@ -203,50 +195,32 @@ pk_mem_calculate_bkt_size(size_t sz, size_t reserved_block_count)
}
bool
-pk_memory_is_in_bucket(const void* ptr, const struct pk_membucket* bkt)
+pk_mem_bucket_ptr_is_in_mem_bucket(const void* ptr, const struct pk_membucket* bkt)
{
return (ptr >= (void*)bkt && ptr < (void*)pk_bkt_head(bkt));
}
void
-pk_memory_debug_print(struct pk_membucket *bkt)
+pk_mem_bucket_debug_print(struct pk_membucket *bkt)
{
PK_LOG_INF("pk_membucket details:\n");
PK_LOGV_INF("\tbkt: %p\n", (void *)bkt);
PK_LOGV_INF("\tdescription: %s\n", bkt->description);
PK_LOGV_INF("\tsize: %lu\n", bkt->size);
PK_LOGV_INF("\thead: %lu\n", bkt->head);
- PK_LOGV_INF("\tallocs: %u\n", bkt->alloc_count);
- PK_LOGV_INF("\tblock head_l: %u\n", bkt->block_head_l);
- PK_LOGV_INF("\tblock head_r: %u\n", bkt->block_head_r);
- PK_LOGV_INF("\tflags: %lu\n", bkt->flags);
+ PK_LOGV_INF("\tallocs: %u\n", bkt->alloc_count);
+ PK_LOGV_INF("\tblock head_l: %u\n", bkt->block_head_l);
+ PK_LOGV_INF("\tblock head_r: %u\n", bkt->block_head_r);
+ PK_LOGV_INF("\tflags: %lu\n", bkt->flags);
#ifdef PK_MEMORY_DEBUGGER
- PK_LOGV_INF("\tdebug index: %u\n", bkt->debug_bkt_index);
- PK_LOGV_INF("\tdebug alloc head_l: %u\n", bkt->debug_head_l);
- PK_LOGV_INF("\tdebug alloc head_r: %u\n", bkt->debug_head_r);
- PK_LOGV_INF("\tdebug cappacity: %lu\n", bkt->debug_block_capacity);
-#endif
-}
-
-void
-pk_memory_teardown_all()
-{
- client_bucket = NULL;
-#ifdef PK_MEMORY_DEBUGGER
- mtx_lock(&debug_mtx);
- for (size_t i = 0; i < debug_alloc_head; ++i) {
- if (debug_allocs[i] != NULL) {
- free(debug_allocs[i]);
- }
- debug_allocs[i] = NULL;
- }
- debug_alloc_head = 0;
- mtx_unlock(&debug_mtx);
+ PK_LOGV_INF("\tdebug alloc head_l: %u\n", bkt->debug_head_l);
+ PK_LOGV_INF("\tdebug alloc head_r: %u\n", bkt->debug_head_r);
+ PK_LOGV_INF("\tdebug cappacity: %u\n", bkt->debug_block_capacity);
#endif
}
struct pk_membucket*
-pk_bucket_create(const char* description, int64_t sz, enum PK_MEMBUCKET_FLAGS flags)
+pk_mem_bucket_create(const char* description, int64_t sz, enum PK_MEMBUCKET_FLAGS flags)
{
// 512 example:
// [000-127] pk_membucket
@@ -273,40 +247,27 @@ pk_bucket_create(const char* description, int64_t sz, enum PK_MEMBUCKET_FLAGS fl
bkt->blocks[pk_memblock_blocks_idx(bkt,0)].ptr = pk_bkt_data(bkt);
#ifdef PK_MEMORY_DEBUGGER
- mtx_lock(&debug_mtx);
- bkt->debug_bkt_index = debug_alloc_head++;
- mtx_unlock(&debug_mtx);
bkt->debug_head_l = 0;
bkt->debug_head_r = 0;
bkt->debug_block_capacity = 128;
- debug_allocs[bkt->debug_bkt_index] = (struct pk_memblock*)aligned_alloc(alignof(struct pk_memblock), sizeof(struct pk_memblock) * 128);
+ bkt->debug_blocks = (struct pk_memblock*)aligned_alloc(alignof(struct pk_memblock), sizeof(struct pk_memblock) * 128);
#endif
return bkt;
}
void
-pk_bucket_destroy(struct pk_membucket* bkt)
+pk_mem_bucket_destroy(struct pk_membucket* bkt)
{
assert(bkt != NULL);
#ifdef PK_MEMORY_DEBUGGER
- mtx_lock(&debug_mtx);
- assert(bkt->debug_bkt_index <= debug_alloc_head);
- if (debug_allocs[bkt->debug_bkt_index] != NULL) free(debug_allocs[bkt->debug_bkt_index]);
- debug_allocs[bkt->debug_bkt_index] = NULL;
- while (debug_alloc_head > 0) {
- if (debug_allocs[debug_alloc_head-1] != NULL) {
- break;
- }
- debug_alloc_head--;
- }
- mtx_unlock(&debug_mtx);
+ if (bkt->debug_blocks != NULL) free(bkt->debug_blocks);
#endif
free(bkt);
}
void
-pk_bucket_reset(struct pk_membucket* bkt)
+pk_mem_bucket_reset(struct pk_membucket* bkt)
{
if (PK_HAS_FLAG(bkt->flags, PK_MEMBUCKET_FLAG_TRANSIENT) == true) {
PK_LOG_ERR("WARNING: pk_bucket_reset called on non-transient pk_membucket\n");
@@ -330,7 +291,7 @@ pk_bucket_reset(struct pk_membucket* bkt)
#endif
}
-void pk_bucket_set_client_bucket(struct pk_membucket *bkt) {
+void pk_mem_bucket_set_client_mem_bucket(struct pk_membucket *bkt) {
client_bucket = bkt;
}
@@ -342,8 +303,8 @@ pk_bucket_insert_block(struct pk_membucket* bkt, const struct pk_memblock* block
// This means that the block will never go at the END of the list - that would be an append.
// It can, however, be placed at the beginning, in which case the entire array shifts.
- struct pk_memblock* new_block;
- struct pk_memblock* old_block;
+ struct pk_memblock* new_block = NULL;
+ struct pk_memblock* old_block = NULL;
size_t i, k;
// 1. resize if needed
@@ -369,7 +330,7 @@ pk_bucket_insert_block(struct pk_membucket* bkt, const struct pk_memblock* block
break;
}
}
- if (i == 0) {
+ if (i == 0 && old_block != NULL) {
*old_block = *block;
} else {
*new_block = *block;
@@ -444,23 +405,33 @@ pk_new_bkt(size_t sz, size_t alignment, struct pk_membucket* bkt)
size_t ii;
if (PK_HAS_FLAG(bkt->flags, PK_MEMBUCKET_FLAG_TRANSIENT) == false) {
for (i = 0; i < bkt->debug_head_r; ++i) {
- assert((debug_allocs[bkt->debug_bkt_index][i].size == 0 || (void*)(debug_allocs[bkt->debug_bkt_index][i].data) != data) && "mem address alloc'd twice!");
+ assert((bkt->debug_blocks[i].size == 0 || (void*)(bkt->debug_blocks[i].data) != data) && "mem address alloc'd twice!");
}
i = bkt->debug_head_l;
if (bkt->debug_head_l == bkt->debug_head_r) {
bkt->debug_head_l++;
bkt->debug_head_r++;
+
+ if (bkt->debug_head_r == bkt->debug_block_capacity) {
+ struct pk_memblock *debug_blocks;
+ debug_blocks = (struct pk_memblock*)aligned_alloc(alignof(struct pk_memblock), sizeof(struct pk_memblock) * bkt->debug_block_capacity + 128);
+ assert(debug_blocks != NULL);
+ memcpy(debug_blocks, bkt->debug_blocks, sizeof(struct pk_memblock) * bkt->debug_block_capacity);
+ free(bkt->debug_blocks);
+ bkt->debug_blocks = debug_blocks;
+ }
+
} else {
for (ii = bkt->debug_head_l+1; ii <= bkt->debug_head_r; ++ii) {
- if (debug_allocs[bkt->debug_bkt_index][ii].size == 0) {
+ if (bkt->debug_blocks[ii].size == 0) {
bkt->debug_head_l = ii;
break;
}
}
}
assert(bkt->debug_head_l <= bkt->debug_head_r);
- debug_allocs[bkt->debug_bkt_index][i].data = (char*)data;
- debug_allocs[bkt->debug_bkt_index][i].size = sz;
+ bkt->debug_blocks[i].data = (char*)data;
+ bkt->debug_blocks[i].size = sz;
}
#endif
if (block->data == pk_bkt_head(bkt)) {
@@ -486,7 +457,7 @@ pk_new_bkt(size_t sz, size_t alignment, struct pk_membucket* bkt)
int64_t debug_tracked_alloc_size = 0;
int64_t debug_bucket_alloc_size = pk_bkt_data_sz(bkt);
for (i = 0; i < bkt->debug_head_r; ++i) {
- debug_tracked_alloc_size += debug_allocs[bkt->debug_bkt_index][i].size;
+ debug_tracked_alloc_size += bkt->debug_blocks[i].size;
}
for (i = 0; i <= bkt->block_head_r; ++i) {
k = pk_memblock_blocks_idx(bkt, i);
@@ -527,11 +498,11 @@ pk_delete_bkt(const void* ptr, size_t sz, struct pk_membucket* bkt)
size_t i, k;
mtx_lock(&bkt->mtx);
assert(bkt->alloc_count > 0);
- assert(pk_memory_is_in_bucket(ptr, bkt) && "pointer not in memory bucket range");
+ assert(pk_mem_bucket_ptr_is_in_mem_bucket(ptr, bkt) && "pointer not in memory bucket range");
assert(sz > 0 && "attempted to free pointer of size 0");
#ifdef PK_MEMORY_DEBUGGER
bool found = PK_HAS_FLAG(bkt->flags, PK_MEMBUCKET_FLAG_TRANSIENT);
- struct pk_memblock *debug_memblocks = debug_allocs[bkt->debug_bkt_index];
+ struct pk_memblock *debug_memblocks = bkt->debug_blocks;
struct pk_memblock *mb;
if (found == false) {
for (i = bkt->debug_head_r+1; i > 0; --i) {
@@ -565,8 +536,8 @@ pk_delete_bkt(const void* ptr, size_t sz, struct pk_membucket* bkt)
#ifdef PK_MEMORY_DEBUGGER
bkt->debug_head_l = 0;
bkt->debug_head_r = 0;
- debug_allocs[bkt->debug_bkt_index][0].data = NULL;
- debug_allocs[bkt->debug_bkt_index][0].size = 0;
+ bkt->debug_blocks[0].data = NULL;
+ bkt->debug_blocks[0].size = 0;
#endif
mtx_unlock(&bkt->mtx);
return;
@@ -625,7 +596,7 @@ pk_delete_bkt(const void* ptr, size_t sz, struct pk_membucket* bkt)
int64_t debug_tracked_alloc_size = 0;
int64_t debug_bucket_alloc_size = pk_bkt_data_sz(bkt);
for (i = 0; i < bkt->debug_head_r; ++i) {
- debug_tracked_alloc_size += debug_allocs[bkt->debug_bkt_index][i].size;
+ debug_tracked_alloc_size += bkt->debug_blocks[i].size;
}
for (i = 0; i <= bkt->block_head_r; ++i) {
k = pk_memblock_blocks_idx(bkt, i);
diff --git a/test/pkarr.c b/test/pkarr.c
index 58a0c7f..74ff27d 100644
--- a/test/pkarr.c
+++ b/test/pkarr.c
@@ -31,17 +31,16 @@ void
test_spinup(struct pk_arr *arr, struct pk_membucket **bkt)
{
memset(arr, 0, sizeof(struct pk_arr));
- *bkt = pk_bucket_create("test", 1024 * 1024, false);
+ *bkt = pk_mem_bucket_create("test", 1024 * 1024, false);
arr->bkt = *bkt;
}
void
test_teardown(struct pk_arr *arr, struct pk_membucket **bkt)
{
- pk_bucket_destroy(*bkt);
+ pk_mem_bucket_destroy(*bkt);
*bkt = NULL;
arr->data = NULL;
- pk_memory_teardown_all();
}
bool
diff --git a/test/pkarr.cpp b/test/pkarr.cpp
index 600b38f..4b03922 100644
--- a/test/pkarr.cpp
+++ b/test/pkarr.cpp
@@ -14,13 +14,13 @@
void
test_spinup(struct pk_membucket **bkt)
{
- *bkt = pk_bucket_create("test", 1024 * 1024, PK_MEMBUCKET_FLAG_NONE);
+ *bkt = pk_mem_bucket_create("test", 1024 * 1024, PK_MEMBUCKET_FLAG_NONE);
}
void
test_teardown(struct pk_membucket **bkt)
{
- pk_bucket_destroy(*bkt);
+ pk_mem_bucket_destroy(*bkt);
*bkt = NULL;
}
diff --git a/test/pkbktarr.c b/test/pkbktarr.c
index ef8a78f..6767cbc 100644
--- a/test/pkbktarr.c
+++ b/test/pkbktarr.c
@@ -39,15 +39,15 @@ void iter(void *user_data, void *arr_item) {
void test_spinup(struct pk_membucket **bkt_buckets, struct pk_membucket **bkt_data)
{
- *bkt_buckets = pk_bucket_create("buckets", 1 << 16, false);
- *bkt_data = pk_bucket_create("data", 1 << 16, false);
+ *bkt_buckets = pk_mem_bucket_create("buckets", 1 << 16, false);
+ *bkt_data = pk_mem_bucket_create("data", 1 << 16, false);
global_counter = 0;
}
void test_teardown(struct pk_membucket **bkt_buckets, struct pk_membucket **bkt_data)
{
- pk_bucket_destroy(*bkt_buckets);
- pk_bucket_destroy(*bkt_data);
+ pk_mem_bucket_destroy(*bkt_buckets);
+ pk_mem_bucket_destroy(*bkt_data);
*bkt_buckets = nullptr;
*bkt_data = nullptr;
}
diff --git a/test/pkbktarr.cpp b/test/pkbktarr.cpp
index 45d2ba2..c4ce85c 100644
--- a/test/pkbktarr.cpp
+++ b/test/pkbktarr.cpp
@@ -7,15 +7,15 @@
void test_spinup(struct pk_membucket **bkt_buckets, struct pk_membucket **bkt_data)
{
- *bkt_buckets = pk_bucket_create("buckets", 1 << 16, PK_MEMBUCKET_FLAG_NONE);
- *bkt_data = pk_bucket_create("data", 1 << 16, PK_MEMBUCKET_FLAG_NONE);
+ *bkt_buckets = pk_mem_bucket_create("buckets", 1 << 16, PK_MEMBUCKET_FLAG_NONE);
+ *bkt_data = pk_mem_bucket_create("data", 1 << 16, PK_MEMBUCKET_FLAG_NONE);
}
void test_teardown(struct pk_membucket **bkt_buckets, struct pk_membucket **bkt_data)
{
// reverse order
- pk_bucket_destroy(*bkt_data);
- pk_bucket_destroy(*bkt_buckets);
+ pk_mem_bucket_destroy(*bkt_data);
+ pk_mem_bucket_destroy(*bkt_buckets);
*bkt_buckets = nullptr;
*bkt_data = nullptr;
}
diff --git a/test/pkmem.c b/test/pkmem.c
index 7614379..5ec2643 100644
--- a/test/pkmem.c
+++ b/test/pkmem.c
@@ -15,8 +15,8 @@ struct mem_test {
} mt;
void spinup() {
- mt.bkt1 = pk_bucket_create("bkt1", 1024, false);
- mt.bkt2 = pk_bucket_create("bkt2", 1024, false);
+ mt.bkt1 = pk_mem_bucket_create("bkt1", 1024, false);
+ mt.bkt2 = pk_mem_bucket_create("bkt2", 1024, false);
}
void spinup_w_instr() {
@@ -25,8 +25,8 @@ void spinup_w_instr() {
}
void teardown() {
- pk_bucket_destroy(mt.bkt1);
- pk_bucket_destroy(mt.bkt2);
+ pk_mem_bucket_destroy(mt.bkt1);
+ pk_mem_bucket_destroy(mt.bkt2);
pk_funcinstr_teardown();
}
@@ -38,7 +38,7 @@ int main(int argc, char *argv[])
// pk_new_base
{
spinup_w_instr();
- pk_bucket_set_client_bucket(mt.bkt1);
+ pk_mem_bucket_set_client_mem_bucket(mt.bkt1);
char *some_dang_string = (char*)pk_new_base(64, alignof(char*));
fprintf(stdout, "some_dang_string: %s: %p\n", some_dang_string, (void *)some_dang_string);
pk_delete_base(some_dang_string, 64);
diff --git a/test/pkmem.cpp b/test/pkmem.cpp
index 6a52f25..3a2b425 100644
--- a/test/pkmem.cpp
+++ b/test/pkmem.cpp
@@ -21,11 +21,11 @@ class FreeTest {
void *ptrs[4];
FreeTest() :
- bkt(pk_bucket_create("freetest", 1024 * 1024, PK_MEMBUCKET_FLAG_NONE)) {
- pk_bucket_set_client_bucket(this->bkt);
+ bkt(pk_mem_bucket_create("freetest", 1024 * 1024, PK_MEMBUCKET_FLAG_NONE)) {
+ pk_mem_bucket_set_client_mem_bucket(this->bkt);
}
virtual ~FreeTest() {
- pk_bucket_destroy(this->bkt);
+ pk_mem_bucket_destroy(this->bkt);
}
void setup() {
@@ -128,7 +128,6 @@ void pk_test_pkmem_spinup(bool init_funcinstr = false) {
}
void pk_test_pkmem_teardown() {
pk_funcinstr_teardown();
- pk_memory_teardown_all();
}
/*
@@ -170,16 +169,18 @@ int main(int argc, char *argv[])
pk_test_pkmem_spinup(true);
fprintf(stdout, "[pkmem.cpp] start: %s\n", test_basic_alloc);
{
- pk_membucket *bkt = pk_bucket_create("test-some-dang-string", 1024, PK_MEMBUCKET_FLAG_NONE);
- pk_bucket_set_client_bucket(bkt);
+ pk_membucket *bkt = pk_mem_bucket_create("test-some-dang-string", 1024, PK_MEMBUCKET_FLAG_NONE);
+ pk_mem_bucket_set_client_mem_bucket(bkt);
char *some_dang_string = pk_new<char>(64);
fprintf(stdout, "some_dang_string: %s: %p\n", some_dang_string, (void *)some_dang_string);
- fprintf(stdout, "debug block : size: %zu, data: %p\n", debug_allocs[0][0].size, debug_allocs[0][0].ptr);
- pk_mem_assert(64 == debug_allocs[0][0].size);
- pk_mem_assert((char*)bkt + EXPECTED_PK_MEMBLOCK_SIZE == debug_allocs[0][0].data);
+#ifdef PK_MEMORY_DEBUGGER
+ fprintf(stdout, "debug block : size: %zu, data: %p\n", bkt->debug_blocks[0].size, bkt->debug_blocks[0].ptr);
+ pk_mem_assert(64 == bkt->debug_blocks[0].size);
+ pk_mem_assert((char*)bkt + EXPECTED_PK_MEMBLOCK_SIZE == bkt->debug_blocks[0].data);
+#endif
fprintf(stdout, "bkt->head : %lu\n", bkt->head);
pk_mem_assert(64 == bkt->head);
@@ -193,7 +194,7 @@ int main(int argc, char *argv[])
pk_delete<char>(some_dang_string, 64);
- pk_bucket_destroy(bkt);
+ pk_mem_bucket_destroy(bkt);
}
pk_test_pkmem_teardown();
fprintf(stdout, "[pkmem.cpp] end: %s\n\n", test_basic_alloc);
@@ -202,8 +203,8 @@ int main(int argc, char *argv[])
pk_test_pkmem_spinup(true);
fprintf(stdout, "[pkmem.cpp] start: %s\n", test_misalignment01);
{
- pk_membucket *bkt = pk_bucket_create("misalignment", 1024 * 1024, PK_MEMBUCKET_FLAG_NONE);
- pk_bucket_set_client_bucket(bkt);
+ pk_membucket *bkt = pk_mem_bucket_create("misalignment", 1024 * 1024, PK_MEMBUCKET_FLAG_NONE);
+ pk_mem_bucket_set_client_mem_bucket(bkt);
pk_new(1, 1, bkt);
pk_new(1, 64, bkt);
pk_new(9, 1, bkt);
@@ -220,7 +221,7 @@ int main(int argc, char *argv[])
fprintf(stdout, "blocks[15].size : %li\n", bkt->blocks[15].size);
pk_mem_assert(54 == bkt->blocks[15].size);
- pk_bucket_destroy(bkt);
+ pk_mem_bucket_destroy(bkt);
}
fprintf(stdout, "[pkmem.cpp] end: %s\n\n", test_misalignment01);
pk_test_pkmem_teardown();
@@ -229,8 +230,8 @@ int main(int argc, char *argv[])
pk_test_pkmem_spinup();
fprintf(stdout, "[pkmem.cpp] start: %s\n", test_misalignment02);
{
- pk_membucket *bkt = pk_bucket_create("misalignment", 1024 * 1024, PK_MEMBUCKET_FLAG_NONE);
- pk_bucket_set_client_bucket(bkt);
+ pk_membucket *bkt = pk_mem_bucket_create("misalignment", 1024 * 1024, PK_MEMBUCKET_FLAG_NONE);
+ pk_mem_bucket_set_client_mem_bucket(bkt);
pk_new(1, 1, bkt);
pk_new(1, 64, bkt);
pk_new(9, 16, bkt);
@@ -253,7 +254,7 @@ int main(int argc, char *argv[])
fprintf(stdout, "blocks[14].size : %li\n", bkt->blocks[14].size);
pk_mem_assert(39 == bkt->blocks[14].size);
- pk_bucket_destroy(bkt);
+ pk_mem_bucket_destroy(bkt);
}
fprintf(stdout, "[pkmem.cpp] end: %s\n\n", test_misalignment02);
pk_test_pkmem_teardown();
@@ -589,7 +590,7 @@ int main(int argc, char *argv[])
fprintf(stdout, "[pkmem.cpp] start: %s\n", test_alloc_strings);
{
size_t diff;
- pk_membucket *bkt = pk_bucket_create(test_free_between, PK_DEFAULT_BUCKET_SIZE, PK_MEMBUCKET_FLAG_NONE);
+ pk_membucket *bkt = pk_mem_bucket_create(test_free_between, PK_MEM_DEFAULT_BUCKET_SIZE, PK_MEMBUCKET_FLAG_NONE);
char *str1 = pk_new<char>(16, bkt);
char *str2 = pk_new<char>(16, bkt);
@@ -622,143 +623,10 @@ int main(int argc, char *argv[])
fprintf(stdout, "handle2 - handle1 : %zu\n", diff);
pk_mem_assert(diff == 8);
- pk_bucket_destroy(bkt);
+ pk_mem_bucket_destroy(bkt);
}
fprintf(stdout, "[pkmem.cpp] end: %s\n\n", test_alloc_strings);
pk_test_pkmem_teardown();
- // assert bucket count
- /*
- pk_test_pkmem_spinup(false);
- fprintf(stdout, "[pkmem.cpp] start: %s\n", test_bucket_count01);
- do
- {
- for (i = 0; i < PK_MAX_BUCKET_COUNT; ++i) {
- pk_membucket *bkt = pk_bucket_create("lol", 1024, PK_MEMBUCKET_FLAG_NONE);
- pk_mem_assert(bkt != nullptr);
- (void)bkt;
- }
- pk_membucket *bkt = pk_bucket_create("lol", 1024, PK_MEMBUCKET_FLAG_NONE);
- pk_mem_assert(bkt == nullptr);
- }
- while(false);
- fprintf(stdout, "[pkmem.cpp] end: %s\n\n", test_bucket_count01);
- pk_test_pkmem_teardown();
- */
-
- // assert no buckets available on pk_new with full memory
- /*
- pk_test_pkmem_spinup(false);
- fprintf(stdout, "[pkmem.cpp] start: %s\n", test_bucket_count02);
- do
- {
- for (i = 0; i < PK_MAX_BUCKET_COUNT; ++i) {
- pk_membucket *bkt = pk_bucket_create("lol2", 1024, PK_MEMBUCKET_FLAG_NONE);
- char *asdf = pk_new<char>(768, bkt);
- pk_mem_assert(asdf != nullptr);
- }
- char *asdf = pk_new<char>(768);
- pk_mem_assert(asdf == nullptr);
- }
- while(false);
- fprintf(stdout, "[pkmem.cpp] end: %s\n\n", test_bucket_count02);
- pk_test_pkmem_teardown();
- */
-
- // assert no buckets available on pk_new with full memory
- /*
- pk_test_pkmem_spinup(false);
- fprintf(stdout, "[pkmem.cpp] start: %s\n", test_bucket_count03);
- do
- {
- pk_membucket *bkt = nullptr;
- for (i = 0; i < PK_MAX_BUCKET_COUNT; ++i) {
- bkt = pk_bucket_create("lol3", 1024, PK_MEMBUCKET_FLAG_NONE);
- char *asdf = pk_new<char>(768, bkt);
- pk_mem_assert(asdf != nullptr);
- }
- char *asdf = pk_new<char>(768, bkt);
- pk_mem_assert(asdf == nullptr);
- }
- while(false);
- fprintf(stdout, "[pkmem.cpp] end: %s\n\n", test_bucket_count03);
- pk_test_pkmem_teardown();
- */
-
- pk_test_pkmem_spinup(false);
- fprintf(stdout, "[pkmem.cpp] start: %s\n", test_bucket_count04);
- {
- pk_membucket *bkt1 = pk_bucket_create("bucket1", 1024, PK_MEMBUCKET_FLAG_NONE);
- pk_membucket *bkt2 = pk_bucket_create("bucket2", 1024, PK_MEMBUCKET_FLAG_NONE);
- pk_membucket *bkt3 = pk_bucket_create("bucket3", 1024, PK_MEMBUCKET_FLAG_NONE);
- pk_membucket *bkt4 = pk_bucket_create("bucket4", 1024, PK_MEMBUCKET_FLAG_NONE);
-
- fprintf(stdout, "debug_alloc_head: %li\n", debug_alloc_head);
- pk_mem_assert(4 == debug_alloc_head);
-
- pk_bucket_destroy(bkt4);
-
- fprintf(stdout, "debug_alloc_head: %li\n", debug_alloc_head);
- pk_mem_assert(3 == debug_alloc_head);
-
- pk_bucket_destroy(bkt3);
-
- fprintf(stdout, "debug_alloc_head: %li\n", debug_alloc_head);
- pk_mem_assert(2 == debug_alloc_head);
-
- pk_bucket_destroy(bkt2);
-
- fprintf(stdout, "debug_alloc_head: %li\n", debug_alloc_head);
- pk_mem_assert(1 == debug_alloc_head);
-
- pk_bucket_destroy(bkt1);
-
- fprintf(stdout, "debug_alloc_head: %li\n", debug_alloc_head);
- pk_mem_assert(0 == debug_alloc_head);
- }
- fprintf(stdout, "[pkmem.cpp] end: %s\n\n", test_bucket_count04);
- pk_test_pkmem_teardown();
-
- /* 2025-01-06 JCB
- * buckets are stored in an array.
- * debug_alloc_head is only an *indicator* of how many buckets have been
- * created total, and the index of the next available.
- */
- pk_test_pkmem_spinup(false);
- fprintf(stdout, "[pkmem.cpp] start: %s\n", test_bucket_count05);
- {
- pk_membucket *bkt1 = pk_bucket_create("bucket1", 1024, PK_MEMBUCKET_FLAG_NONE);
- pk_membucket *bkt2 = pk_bucket_create("bucket2", 1024, PK_MEMBUCKET_FLAG_NONE);
- pk_membucket *bkt3 = pk_bucket_create("bucket3", 1024, PK_MEMBUCKET_FLAG_NONE);
- pk_membucket *bkt4 = pk_bucket_create("bucket4", 1024, PK_MEMBUCKET_FLAG_NONE);
-
- fprintf(stdout, "debug_alloc_head: %li\n", debug_alloc_head);
- pk_mem_assert(4 == debug_alloc_head);
-
- pk_bucket_destroy(bkt1);
- fprintf(stdout, "debug_alloc_head: %li\n", debug_alloc_head);
- pk_mem_assert(4 == debug_alloc_head);
-
- pk_bucket_destroy(bkt2);
- fprintf(stdout, "debug_alloc_head: %li\n", debug_alloc_head);
- pk_mem_assert(4 == debug_alloc_head);
-
- pk_bucket_destroy(bkt3);
- fprintf(stdout, "debug_alloc_head: %li\n", debug_alloc_head);
- pk_mem_assert(4 == debug_alloc_head);
-
- pk_bucket_destroy(bkt4);
- fprintf(stdout, "debug_alloc_head: %li\n", debug_alloc_head);
- pk_mem_assert(0 == debug_alloc_head);
-
- /*
- pk_memory_flush();
- fprintf(stdout, "debug_alloc_head: %li\n", debug_alloc_head);
- pk_mem_assert(0 == debug_alloc_head);
- */
- }
- fprintf(stdout, "[pkmem.cpp] end: %s\n\n", test_bucket_count05);
- pk_test_pkmem_teardown();
-
return 0;
}