From 5e67008bc6039c593af4930b3a39242c6fc48355 Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Fri, 13 Oct 2023 12:02:00 -0400 Subject: DynArray can specify MemBucket in constructor --- src/dynamic-array.cpp | 7 ++++--- src/dynamic-array.hpp | 20 ++++++++++++++------ src/ecs.cpp | 6 +++--- src/event.cpp | 2 +- src/memory.hpp | 34 ++++++++++++++++++++++++++-------- 5 files changed, 48 insertions(+), 21 deletions(-) diff --git a/src/dynamic-array.cpp b/src/dynamic-array.cpp index ed181f7..7dd15eb 100644 --- a/src/dynamic-array.cpp +++ b/src/dynamic-array.cpp @@ -4,10 +4,11 @@ void DynArrayReserve(DynArrayBase *arr, int64_t count) { assert(count != 0); if (arr->reservedCount >= count) return; - char *a = Pke_New(arr->elementSize * count); + char *a = nullptr; + a = Pke_New(arr->elementSize * count, arr->bkt); if (arr->ptr != nullptr) { std::memcpy(a, arr->ptr, arr->elementSize * arr->reservedCount); - Pke_Delete(arr->ptr, arr->elementSize * arr->reservedCount); + Pke_Delete(arr->ptr, arr->elementSize * arr->reservedCount, arr->bkt); } arr->reservedCount = count; arr->ptr = a; @@ -17,7 +18,7 @@ void DynArrayDestroy(DynArrayBase *arr) { if (arr->ptr == nullptr) return; if (arr->ptr == CAFE_BABE(char)) return; if (arr->reservedCount > 0) - Pke_Delete(arr->ptr, arr->elementSize * arr->reservedCount); + Pke_Delete(arr->ptr, arr->elementSize * arr->reservedCount, arr->bkt); arr->ptr = CAFE_BABE(char); } diff --git a/src/dynamic-array.hpp b/src/dynamic-array.hpp index 95ba967..3bf02ee 100644 --- a/src/dynamic-array.hpp +++ b/src/dynamic-array.hpp @@ -12,6 +12,7 @@ #define BAKE_DYN_ARRAY(T) template struct DynArray; struct DynArrayBase { + mutable MemBucket *bkt = nullptr; mutable char *ptr = nullptr; int64_t elementSize = 0; int64_t elementCount = 0; @@ -20,8 +21,8 @@ struct DynArrayBase { template struct DynArray: DynArrayBase { - DynArray(); - explicit DynArray(int64_t reserveCount); + explicit DynArray(MemBucket *bucket = nullptr); + explicit DynArray(int64_t reserveCount, MemBucket *bucket = nullptr); DynArray(const DynArray &other); DynArray(DynArray &&other); DynArray &operator=(const DynArray &other); @@ -38,6 +39,7 @@ struct DynArray: DynArrayBase { void Reserve(int64_t count); void Resize(int64_t count); protected: + using DynArrayBase::bkt; using DynArrayBase::ptr; using DynArrayBase::elementSize; using DynArrayBase::elementCount; @@ -47,7 +49,8 @@ struct DynArray: DynArrayBase { void DynArrayReserve(DynArrayBase *arr, int64_t count); void DynArrayDestroy(DynArrayBase *arr); -template inline DynArray::DynArray(int64_t count) { +template inline DynArray::DynArray(int64_t count, MemBucket *bucket) { + this->bkt = bucket; this->elementSize = sizeof(T); if (count > 0) DynArrayReserve(this, count); if IS_CONSTRUCTIBLE(T) { @@ -57,11 +60,13 @@ template inline DynArray::DynArray(int64_t count) { } } -template inline DynArray::DynArray() { +template inline DynArray::DynArray(MemBucket *bucket) { + this->bkt = bucket; this->elementSize = sizeof(T); } template inline DynArray::DynArray(const DynArray &rh) { + this->bkt = rh.bkt; this->ptr = rh.ptr; this->elementSize = rh.elementSize; this->elementCount = rh.elementCount; @@ -70,6 +75,7 @@ template inline DynArray::DynArray(const DynArray &rh) { } template inline DynArray::DynArray(DynArray &&rh) { + this->bkt = rh.bkt; this->ptr = rh.ptr; this->elementSize = rh.elementSize; this->elementCount = rh.elementCount; @@ -78,6 +84,7 @@ template inline DynArray::DynArray(DynArray &&rh) { } template inline DynArray &DynArray::operator=(const DynArray &rh) { + this->bkt = rh.bkt; this->ptr = rh.ptr; this->elementSize = rh.elementSize; this->elementCount = rh.elementCount; @@ -87,6 +94,7 @@ template inline DynArray &DynArray::operator=(const DynArray< } template inline DynArray &DynArray::operator=(DynArray &&rh) { + this->bkt = rh.bkt; this->ptr = rh.ptr; this->elementSize = rh.elementSize; this->elementCount = rh.elementCount; @@ -159,13 +167,13 @@ template inline void DynArray::Remove(std::size_t index) { assert(this->elementCount == 0 && "Invalid DynArray::Remove() - Contains no elements"); assert(index >= this->elementCount && "Invalid DynArray::Remove() - Out of bounds"); uint64_t moveCount = (this->elementCount - index - 1); - auto *tmp = Pke_New(sizeof(T) * moveCount); + T *tmp = Pke_New(moveCount, this->bkt); if IS_DESTRUCTIBLE(T) { reinterpret_cast(this->ptr + (index * sizeof(T))).~T(); } memcpy(tmp, this->ptr + (sizeof(T) * (index + 1)), sizeof(T) * moveCount); memcpy(this->ptr + (sizeof(T) * index), tmp, sizeof(T) * moveCount); - Pke_Delete(tmp, moveCount * sizeof(T)); + Pke_Delete(tmp, moveCount, this->bkt); this->elementCount -= 1; } diff --git a/src/ecs.cpp b/src/ecs.cpp index fe1dfb6..03f26db 100644 --- a/src/ecs.cpp +++ b/src/ecs.cpp @@ -18,7 +18,7 @@ struct InstanceBucket { }; DynArray EntitiesToBeRemoved{16}; // public -DynArray entitiesYetToBeRemoved{0}; +DynArray entitiesYetToBeRemoved{0, nullptr}; DynArray entitiesMarkedForRemoval{16}; BucketContainer Entities_BucketContainer{}; @@ -155,7 +155,7 @@ void ECS_Tick_Early(double delta) { struct InstanceBufferCopyChunk { uint64_t startingIndex; uint64_t endingIndex; - DynArray mats{0}; + DynArray mats{0, nullptr}; VkBufferCopy dstBufferCopy; }; struct InstanceBufferCopy { @@ -164,7 +164,7 @@ struct InstanceBufferCopy { DynArray chunks{4}; }; void ECS_Tick_Late(double delta) { - DynArray bufferUpdates{0}; + DynArray bufferUpdates{0, nullptr}; for (long b = 0; b <= Comp_Instance_BucketContainer.bucketCounter; ++b) { auto &bkt = Comp_Instance_BucketContainer.buckets[b]; long count = Comp_Instance_BucketContainer.bucketCounter == b ? Comp_Instance_BucketContainer.itemCounter >> 32 : maxBucketItemCount; diff --git a/src/event.cpp b/src/event.cpp index 0b1d48d..89756d4 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -5,7 +5,7 @@ struct EventBucket { char name[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; DynArray callbacks{8}; }; -DynArray eventBuckets{0}; +DynArray eventBuckets{0, nullptr}; void EventBucketFind(const char *name, char *safeName, EventBucket*& bkt) { assert(strlen(name) <= 16); diff --git a/src/memory.hpp b/src/memory.hpp index 50be56b..b93890a 100644 --- a/src/memory.hpp +++ b/src/memory.hpp @@ -35,16 +35,26 @@ void Pke_EndTransientBucket(MemBucket *bkt); void Pke_MemoryFlush(); template -inline T *Pke_New() { - void *ptr = Pke_New(sizeof(T)); +inline T *Pke_New(MemBucket *bucket = nullptr) { + void *ptr = nullptr; + if (bucket) { + ptr = Pke_New(sizeof(T), bucket); + } else { + ptr = Pke_New(sizeof(T)); + } if IS_CONSTRUCTIBLE(T) { return new (ptr) T{}; } return reinterpret_cast(ptr); } template -inline T *Pke_New(long count) { - char *ptr = static_cast(Pke_New(sizeof(T) * count)); +inline T *Pke_New(long count, MemBucket *bucket = nullptr) { + char *ptr = nullptr; + if (bucket) { + ptr = static_cast(Pke_New(sizeof(T) * count, bucket)); + } else { + ptr = static_cast(Pke_New(sizeof(T) * count)); + } if IS_CONSTRUCTIBLE(T) { for (long i = 0; i < count; ++i) { new (ptr + (i * sizeof(T))) T{}; @@ -53,20 +63,28 @@ inline T *Pke_New(long count) { return reinterpret_cast(ptr); } template -inline void Pke_Delete(const void *ptr) { +inline void Pke_Delete(const void *ptr, MemBucket *bucket = nullptr) { if IS_DESTRUCTIBLE(T) { reinterpret_cast(ptr)->~T(); } - return Pke_Delete(ptr, sizeof(T)); + if (bucket) { + return Pke_Delete(ptr, sizeof(T), bucket); + } else { + return Pke_Delete(ptr, sizeof(T)); + } } template -inline void Pke_Delete(const void *ptr, long count) { +inline void Pke_Delete(const void *ptr, long count, MemBucket *bucket = nullptr) { if IS_DESTRUCTIBLE(T) { for (long i = 0; i < count; ++i) { reinterpret_cast(reinterpret_cast(ptr) + (i * sizeof(T)))->~T(); } } - return Pke_Delete(ptr, sizeof(T) * count); + if (bucket) { + return Pke_Delete(ptr, sizeof(T) * count, bucket); + } else { + return Pke_Delete(ptr, sizeof(T) * count); + } } -- cgit v1.2.3