summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-05-07 13:24:13 -0400
committerJonathan Bradley <jcb@pikum.xyz>2025-05-07 13:24:13 -0400
commit86b9b350c6344e78f0d22bcd054e568130a40b08 (patch)
tree7ae2e0938caa4221ba92b7985d8781d4b49f3966
parentac9a82de4c0cfa67c0c32e814556aa83434a46c7 (diff)
pkmem: return null when out of space
-rw-r--r--pkmem.h16
-rw-r--r--test/pkmem.cpp126
2 files changed, 66 insertions, 76 deletions
diff --git a/pkmem.h b/pkmem.h
index 95e29fa..e8e5714 100644
--- a/pkmem.h
+++ b/pkmem.h
@@ -64,6 +64,7 @@ pk_new(long count, pk_membucket* bucket = nullptr)
} else {
ptr = static_cast<char*>(pk_new_base(sizeof(T) * count, alignof(T)));
}
+ if (ptr == nullptr) return nullptr;
if IS_CONSTRUCTIBLE(T) {
for (long i = 0; i < count; ++i) {
new (ptr + (i * sizeof(T))) T{};
@@ -233,7 +234,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");
+ if (pk_bucket_head >= PK_MAX_BUCKET_COUNT) return -1;
#ifdef PK_MEMORY_DEBUGGER
if (has_init_debug == false) {
has_init_debug = true;
@@ -272,7 +273,10 @@ pk_bucket_create_inner(int64_t sz, bool transient, const char* description)
struct pk_membucket*
pk_bucket_create(const char* description, int64_t sz, bool transient)
{
- return &pk_buckets[pk_bucket_create_inner(sz, transient, description)];
+ int64_t bkt_index = pk_bucket_create_inner(sz, transient, description);
+ // TODO some of of error handling
+ if (bkt_index < 0) { return nullptr; }
+ return &pk_buckets[bkt_index];
}
void
@@ -384,7 +388,9 @@ pk_new_bkt(size_t sz, size_t alignment, struct pk_membucket* bkt)
return malloc(sz);
#endif
if (sz == 0) return nullptr;
- assert((bkt->size - bkt->head) > (sz + alignment -1) && "Not enough space in bucket");
+ if (bkt == nullptr) return nullptr;
+ // TODO some type of error handling
+ if ((bkt->size - bkt->head) < (sz + alignment - 1)) return nullptr;
size_t i;
size_t calculatedAlignment = alignment < PK_MINIMUM_ALIGNMENT ? PK_MINIMUM_ALIGNMENT : alignment;
size_t misalignment = 0;
@@ -495,7 +501,9 @@ pk_new_base(size_t sz, size_t alignment)
break;
}
if (bkt == nullptr) {
- bkt = &pk_buckets[pk_bucket_create_inner(PK_DEFAULT_BUCKET_SIZE, false, "pk_bucket internally created")];
+ int64_t bkt_index = pk_bucket_create_inner(PK_DEFAULT_BUCKET_SIZE, false, "pk_bucket internally created");
+ // TODO some of of error handling
+ if (bkt_index >= 0) bkt = &pk_buckets[bkt_index];
}
return pk_new_bkt(sz, alignment, bkt);
}
diff --git a/test/pkmem.cpp b/test/pkmem.cpp
index 2e3b6f2..ffc24b3 100644
--- a/test/pkmem.cpp
+++ b/test/pkmem.cpp
@@ -1,9 +1,7 @@
+#include <new>
#include "../pkmem.h"
-#include <csetjmp>
-#include <signal.h>
-
class FreeTest {
public:
pk_membucket *bkt;
@@ -105,12 +103,14 @@ const char *test_free_0001 = "test_free_0001";
const char *test_free_after = "test_free_after";
const char *test_free_before = "test_free_before";
const char *test_free_between = "test_free_between";
+const char *test_alloc_strings = "test_alloc_strings";
const char *test_bucket_count01 = "test_bucket_count01";
const char *test_bucket_count02 = "test_bucket_count02";
const char *test_bucket_count03 = "test_bucket_count03";
const char *test_bucket_count04 = "test_bucket_count04";
const char *test_bucket_count05 = "test_bucket_count05";
+/*
static bool expected_exit = false;
static bool caught = false;
static jmp_buf jmp_env;
@@ -136,10 +136,11 @@ handle_assert_abort(int sig)
_exit(1);
}
}
+*/
int main(int argc, char *argv[])
{
- signal(SIGABRT, handle_assert_abort);
+ // signal(SIGABRT, handle_assert_abort);
(void)argc;
(void)argv;
int i;
@@ -523,32 +524,58 @@ int main(int argc, char *argv[])
fprintf(stdout, "[pkmem.cpp] end: %s\n\n", test_free_between);
pk_memory_teardown_all();
+ 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, false);
+
+ char *str1 = pk_new<char>(16, bkt);
+ char *str2 = pk_new<char>(16, bkt);
+ pk_handle *handle1 = pk_new<pk_handle>(bkt);
+ pk_handle *handle2 = pk_new<pk_handle>(bkt);
+
+ fprintf(stdout, "ptr : %p\n", (void *)bkt->ptr);
+ fprintf(stdout, "str1 : %p\n", (void *)str1);
+ fprintf(stdout, "str2 : %p\n", (void *)str2);
+ fprintf(stdout, "handle1 : %p\n", (void *)handle1);
+ fprintf(stdout, "handle2 : %p\n", (void *)handle2);
+
+ diff = str1 - bkt->ptr;
+ fprintf(stdout, "str1 - ptr : %zu\n", diff);
+ assert(diff == 0);
+
+ diff = str2 - str1;
+ fprintf(stdout, "str2 - str1 : %zu\n", diff);
+ assert(diff == 16);
+
+ diff = str2 - bkt->ptr;
+ fprintf(stdout, "str2 - ptr : %zu\n", diff);
+ assert(diff == 16);
+
+ diff = (char *)handle1 - str1;
+ fprintf(stdout, "handle1 - str1 : %zu\n", diff);
+ assert(diff == 32);
+
+ diff = (char *)handle2 - (char *)handle1;
+ fprintf(stdout, "handle2 - handle1 : %zu\n", diff);
+ assert(diff == 8);
+
+ pk_bucket_destroy(bkt);
+ }
+ fprintf(stdout, "[pkmem.cpp] end: %s\n\n", test_alloc_strings);
+ pk_memory_teardown_all();
+
// assert bucket count
fprintf(stdout, "[pkmem.cpp] start: %s\n", test_bucket_count01);
do
{
- int r;
- r = setjmp(jmp_env);
- if (r == 1) {
- if (expected_exit == true && caught == true) {
- expected_exit = false;
- caught = false;
- PK_LOGV_INF("%s: successfully caught err.\n", __FILE__);
- fflush(stdout);
- fflush(stderr);
- break;
- } else {
- goto uncaught_err;
- }
- }
- for (i = 0; i < PK_MAX_BUCKET_COUNT + 1; ++i) {
- caught = false;
- expected_exit = true;
+ for (i = 0; i < PK_MAX_BUCKET_COUNT; ++i) {
pk_membucket *bkt = pk_bucket_create("lol", 1024, false);
- expected_exit = false;
+ assert(bkt != nullptr);
(void)bkt;
}
- goto uncaught_err;
+ pk_membucket *bkt = pk_bucket_create("lol", 1024, false);
+ assert(bkt == nullptr);
}
while(false);
fprintf(stdout, "[pkmem.cpp] end: %s\n\n", test_bucket_count01);
@@ -558,33 +585,13 @@ int main(int argc, char *argv[])
fprintf(stdout, "[pkmem.cpp] start: %s\n", test_bucket_count02);
do
{
- int r;
- caught = false;
- expected_exit = false;
for (i = 0; i < PK_MAX_BUCKET_COUNT; ++i) {
pk_membucket *bkt = pk_bucket_create("lol2", 1024, false);
char *asdf = pk_new<char>(768, bkt);
- (void)asdf;
+ assert(asdf != nullptr);
}
- r = setjmp(jmp_env);
- if (r == 1) {
- if (expected_exit == true && caught == true) {
- expected_exit = false;
- caught = false;
- PK_LOGV_INF("%s: successfully caught err.\n", __FILE__);
- fflush(stdout);
- fflush(stderr);
- break;
- } else {
- goto uncaught_err;
- }
- }
- caught = false;
- expected_exit = true;
char *asdf = pk_new<char>(768);
- (void)asdf;
- expected_exit = false;
- goto uncaught_err;
+ assert(asdf == nullptr);
}
while(false);
fprintf(stdout, "[pkmem.cpp] end: %s\n\n", test_bucket_count02);
@@ -594,34 +601,14 @@ int main(int argc, char *argv[])
fprintf(stdout, "[pkmem.cpp] start: %s\n", test_bucket_count03);
do
{
- int r;
- caught = false;
- expected_exit = false;
pk_membucket *bkt = nullptr;
for (i = 0; i < PK_MAX_BUCKET_COUNT; ++i) {
bkt = pk_bucket_create("lol3", 1024, false);
char *asdf = pk_new<char>(768, bkt);
- (void)asdf;
- }
- r = setjmp(jmp_env);
- if (r == 1) {
- if (expected_exit == true && caught == true) {
- expected_exit = false;
- caught = false;
- PK_LOGV_INF("%s: successfully caught err.\n", __FILE__);
- fflush(stdout);
- fflush(stderr);
- break;
- } else {
- goto uncaught_err;
- }
+ assert(asdf != nullptr);
}
- caught = false;
- expected_exit = true;
char *asdf = pk_new<char>(768, bkt);
- (void)asdf;
- expected_exit = false;
- goto uncaught_err;
+ assert(asdf == nullptr);
}
while(false);
fprintf(stdout, "[pkmem.cpp] end: %s\n\n", test_bucket_count03);
@@ -699,9 +686,4 @@ int main(int argc, char *argv[])
pk_memory_teardown_all();
return 0;
-uncaught_err:
- PK_LOGV_ERR("%s: failed to catch err.\n", __FILE__);
- fflush(stdout);
- fflush(stderr);
- return 1;
}