summaryrefslogtreecommitdiff
path: root/src/pk.h
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-02-19 17:20:09 -0500
committerJonathan Bradley <jcb@pikum.xyz>2025-02-19 17:20:09 -0500
commitf7ff00ed46d93c9ec3e8981c7f9b596b68adce82 (patch)
tree29dc7dd8bcb0b07da6769260d7ad3822cc591072 /src/pk.h
parentfdbfcf764424bf05349cd35caadba04576846458 (diff)
pke: update pk.h to 0.2.2
Diffstat (limited to 'src/pk.h')
-rw-r--r--src/pk.h52
1 files changed, 46 insertions, 6 deletions
diff --git a/src/pk.h b/src/pk.h
index 917a125..d407283 100644
--- a/src/pk.h
+++ b/src/pk.h
@@ -1,7 +1,7 @@
#ifndef PK_SINGLE_HEADER_FILE_H
#define PK_SINGLE_HEADER_FILE_H
/*******************************************************************************
-* PK Single-Header-Library V0.2.0
+* PK Single-Header-Library V0.2.2
*
* Author: Jonathan Bradley
* Copyright: © 2024-2025 Jonathan Bradley
@@ -178,7 +178,7 @@
*
*******************************************************************************/
-#define PK_VERSION "0.2.0"
+#define PK_VERSION "0.2.2"
#ifdef PK_IMPL_ALL
# ifndef PK_IMPL_MEM_TYPES
@@ -264,6 +264,8 @@
constexpr TypeName TypeName_MAX = TypeName{TypeName_T_MAX}; \
TypeName operator+(const TypeName& a, const TypeName& b); \
TypeName operator-(const TypeName& a, const TypeName& b); \
+ TypeName operator*(const TypeName& a, const TypeName& b); \
+ TypeName operator/(const TypeName& a, const TypeName& b); \
TypeName operator&(const TypeName& a, const TypeName& b); \
TypeName operator|(const TypeName& a, const TypeName& b); \
TypeName operator^(const TypeName& a, const TypeName& b); \
@@ -275,6 +277,8 @@
TypeName operator>>(const TypeName& a, const TypeName& b); \
TypeName operator+=(TypeName& a, const TypeName& b); \
TypeName operator-=(TypeName& a, const TypeName& b); \
+ TypeName operator*=(TypeName& a, const TypeName& b); \
+ TypeName operator/=(TypeName& a, const TypeName& b); \
TypeName operator&=(TypeName& a, const TypeName& b); \
TypeName operator|=(TypeName& a, const TypeName& b); \
TypeName operator^=(TypeName& a, const TypeName& b); \
@@ -286,6 +290,12 @@
inline TypeName operator-(const TypeName& a, const TypeName& b) { \
return TypeName(static_cast<TypeName_T>(a) - static_cast<TypeName_T>(b)); \
} \
+ inline TypeName operator*(const TypeName& a, const TypeName& b) { \
+ return TypeName(static_cast<TypeName_T>(a) * static_cast<TypeName_T>(b)); \
+ } \
+ inline TypeName operator/(const TypeName& a, const TypeName& b) { \
+ return TypeName(static_cast<TypeName_T>(a) / static_cast<TypeName_T>(b)); \
+ } \
inline TypeName operator&(const TypeName& a, const TypeName& b) { \
return TypeName(static_cast<TypeName_T>(a) & static_cast<TypeName_T>(b)); \
} \
@@ -325,6 +335,14 @@
a = TypeName{a - b}; \
return a; \
}; \
+ inline TypeName operator*=(TypeName& a, const TypeName& b) { \
+ a = TypeName{a * b}; \
+ return a; \
+ }; \
+ inline TypeName operator/=(TypeName& a, const TypeName& b) { \
+ a = TypeName{a / b}; \
+ return a; \
+ }; \
inline TypeName operator&=(TypeName& a, const TypeName& b) { \
a = TypeName{a & b}; \
return a; \
@@ -357,6 +375,12 @@
constexpr TypeName operator-(const TypeName& a, const TypeName& b) { \
return TypeName(static_cast<TypeName_T>(a) - static_cast<TypeName_T>(b)); \
} \
+ constexpr TypeName operator*(const TypeName& a, const TypeName& b) { \
+ return TypeName(static_cast<TypeName_T>(a) * static_cast<TypeName_T>(b)); \
+ } \
+ constexpr TypeName operator/(const TypeName& a, const TypeName& b) { \
+ return TypeName(static_cast<TypeName_T>(a) / static_cast<TypeName_T>(b)); \
+ } \
constexpr TypeName operator&(const TypeName& a, const TypeName& b) { \
return TypeName(static_cast<TypeName_T>(a) & static_cast<TypeName_T>(b)); \
} \
@@ -396,6 +420,14 @@
a = TypeName{a - b}; \
return a; \
}; \
+ constexpr TypeName operator*=(TypeName& a, const TypeName& b) { \
+ a = TypeName{a * b}; \
+ return a; \
+ }; \
+ constexpr TypeName operator/=(TypeName& a, const TypeName& b) { \
+ a = TypeName{a / b}; \
+ return a; \
+ }; \
constexpr TypeName operator&=(TypeName& a, const TypeName& b) { \
a = TypeName{a & b}; \
return a; \
@@ -845,7 +877,8 @@ pk_bucket_insert_block(struct pk_membucket* bkt, const struct pk_memblock* block
}
void
-pk_bucket_collapse_empty_blocks(struct pk_membucket* bkt) {
+pk_bucket_collapse_empty_blocks(struct pk_membucket* bkt)
+{
size_t i, ii;
for (ii = bkt->lastEmptyBlockIndex+1; ii > 0; --ii) {
i = ii-1;
@@ -872,6 +905,7 @@ pk_new_bkt(size_t sz, size_t alignment, struct pk_membucket* bkt)
#ifdef PK_MEMORY_FORCE_MALLOC
return malloc(sz);
#endif
+ assert((bkt->size - bkt->head) > (sz + alignment -1) && "Not enough space in bucket");
if (sz == 0) return nullptr;
size_t i;
size_t calculatedAlignment = alignment < PK_MINIMUM_ALIGNMENT ? PK_MINIMUM_ALIGNMENT : alignment;
@@ -973,10 +1007,14 @@ pk_new_base(size_t sz, size_t alignment)
{
struct pk_membucket* bkt = nullptr;
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;
+ if (pk_buckets[i].transient == true) {
+ continue;
}
+ if (pk_buckets[i].size - pk_buckets[i].head < sz + (alignment - 1)) {
+ continue;
+ }
+ bkt = &pk_buckets[i];
+ break;
}
if (bkt == nullptr) {
bkt = &pk_buckets[pk_bucket_create_inner(PK_DEFAULT_BUCKET_SIZE, false, "pk_bucket internally created")];
@@ -999,6 +1037,7 @@ pk_delete_bkt(const void* ptr, size_t sz, struct pk_membucket* bkt)
#endif
size_t i;
mtx_lock(&bkt->mtx);
+ assert(bkt->allocs > 0);
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
@@ -1030,6 +1069,7 @@ pk_delete_bkt(const void* ptr, size_t sz, struct pk_membucket* bkt)
bkt->lastEmptyBlockIndex = 0;
bkt->blocks[0].data = bkt->ptr;
bkt->blocks[0].size = bkt->size - (sizeof(struct pk_memblock) * bkt->maxBlockCount);
+ mtx_unlock(&bkt->mtx);
return;
}
char* afterPtr = ((char*)(ptr))+sz;