diff --git a/common/datastore/WDataStore.cpp b/common/datastore/WDataStore.cpp new file mode 100644 index 0000000..57f4bd9 --- /dev/null +++ b/common/datastore/WDataStore.cpp @@ -0,0 +1,82 @@ +#include "common/datastore/WDataStore.hpp" +#include "common/ObjectAlloc.hpp" +#include + +static bool s_heapsInitialized = false; +static uint32_t* s_smallHeap = nullptr; +static uint32_t* s_largeHeap = nullptr; +static uint32_t* s_superHeap = nullptr; + +void WDataStore::StaticInitialize() { + if (s_heapsInitialized) { + return; + } + + s_smallHeap = NEW(uint32_t); + *s_smallHeap = ObjectAllocAddHeap(800, 256, "WDataStoreSmallBuffer", true); + + s_largeHeap = NEW(uint32_t); + *s_largeHeap = ObjectAllocAddHeap(16416, 16, "WDataStoreLargeBuffer", true); + + s_superHeap = NEW(uint32_t); + *s_superHeap = ObjectAllocAddHeap(262240, 4, "WDataStoreSuperBuffer", true); +} + +void WDataStore::StaticDestroy() { + DEL(s_smallHeap); + DEL(s_largeHeap); + DEL(s_superHeap); + + s_heapsInitialized = false; +} + +void* WDataStore::AllocBuffer(uint32_t size) { + uint32_t* heap = nullptr; + + if (size <= 768) { + heap = s_smallHeap; + } else if (size <= 16384) { + heap = s_largeHeap; + } else if (size <= 262208) { + heap = s_superHeap; + } + + if (heap) { + uint32_t handle = 0; + uint32_t* result = nullptr; + if (!ObjectAlloc(*heap, &handle, reinterpret_cast(&result), false)) { + return reinterpret_cast(8 * sizeof(uint32_t)); + } else { + *result = handle; + return result + 8; + } + } else { + return ALLOC(size); + } +} + +void WDataStore::FreeBuffer(void* buffer, uint32_t size) { + uint32_t* heap = nullptr; + + if (size <= 768) { + heap = s_smallHeap; + } + + if (size <= 16384) { + heap = s_largeHeap; + } + + if (size <= 262208) { + heap = s_superHeap; + } + + if (heap) { + ObjectFree(*heap, *((uint32_t*)buffer - 8)); + } else { + FREE(buffer); + } +} + +WDataStore::~WDataStore() { + +} diff --git a/common/datastore/WDataStore.hpp b/common/datastore/WDataStore.hpp new file mode 100644 index 0000000..5b3d296 --- /dev/null +++ b/common/datastore/WDataStore.hpp @@ -0,0 +1,25 @@ +#ifndef COMMON_DATASTORE_W_DATA_STORE_HPP +#define COMMON_DATASTORE_W_DATA_STORE_HPP + +#include "common/datastore/CDataStore.hpp" + +class WDataStore : public CDataStore { + public: + // Member variables + void* m_bufferObj = nullptr; + + // Static functions + static void StaticInitialize(); + static void StaticDestroy(); + static void* AllocBuffer(uint32_t size); + static void FreeBuffer(void* buffer, uint32_t size); + + // Virtual member functions + virtual ~WDataStore(); + + virtual void InternalInitialize(uint8_t*& data, uint32_t& base, uint32_t& alloc); + virtual void InternalDestroy(uint8_t*& data, uint32_t& base, uint32_t& alloc); + virtual int32_t InternalFetchWrite(uint32_t pos, uint32_t bytes, uint8_t*& data, uint32_t& base, uint32_t& alloc, const char* fileName, int32_t lineNumber); +}; + +#endif