summaryrefslogtreecommitdiff
path: root/pkmem.h
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 /pkmem.h
parentac9a82de4c0cfa67c0c32e814556aa83434a46c7 (diff)
pkmem: return null when out of space
Diffstat (limited to 'pkmem.h')
-rw-r--r--pkmem.h16
1 files changed, 12 insertions, 4 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);
}