Compare commits

..

3 commits

6 changed files with 88 additions and 8 deletions

View file

@ -62,6 +62,21 @@ uint32_t ObjectAllocUsage(uint32_t heapId) {
return result; return result;
} }
void* ObjectPtr(uint32_t heapId, uint32_t memHandle) {
auto globals = GetObjAllocGlobals();
STORM_ASSERT(heapId < globals->objects.Count());
auto result = globals->objects[heapId].Ptr(memHandle);
ReleaseObjAllocGlobals();
return result;
}
void ObjectFree(uint32_t heapId, uint32_t memHandle) {
auto globals = GetObjAllocGlobals();
STORM_ASSERT(heapId < globals->objects.Count());
globals->objects[heapId].Delete(memHandle);
ReleaseObjAllocGlobals();
}
void ObjectAllocDestroy() { void ObjectAllocDestroy() {
// NOTICE: It seems like sub_4D2F90 does nothing useful // NOTICE: It seems like sub_4D2F90 does nothing useful
auto globals = GetObjAllocGlobals(); auto globals = GetObjAllocGlobals();

View file

@ -9,6 +9,10 @@ uint32_t ObjectAllocAddHeap(uint32_t objectSize, uint32_t objsPerBlock, const ch
uint32_t ObjectAllocUsage(uint32_t heapId); uint32_t ObjectAllocUsage(uint32_t heapId);
void* ObjectPtr(uint32_t heapId, uint32_t memHandle);
void ObjectFree(uint32_t heapId, uint32_t memHandle);
void ObjectAllocDestroy(); void ObjectAllocDestroy();
#endif #endif

View file

@ -4,10 +4,20 @@
#include <bc/Memory.hpp> #include <bc/Memory.hpp>
#include <storm/Error.hpp> #include <storm/Error.hpp>
CObjectHeap::~CObjectHeap() { CObjectHeap::CObjectHeap(const CObjectHeap& heap) {
if (this->m_obj) { if (this == &heap) {
SMemFree(this->m_obj, __FILE__, __LINE__, 0); return;
} }
this->m_obj = heap.m_obj;
this->m_indexStack = heap.m_indexStack;
this->m_allocated = heap.m_allocated;
heap.m_obj = nullptr;
heap.m_indexStack = nullptr;
heap.m_allocated = 0;
}
CObjectHeap::~CObjectHeap() {
this->Free();
} }
int32_t CObjectHeap::Allocate(uint32_t objSize, uint32_t heapObjects, const char* heapName) { int32_t CObjectHeap::Allocate(uint32_t objSize, uint32_t heapObjects, const char* heapName) {
@ -127,3 +137,12 @@ void CObjectHeap::Delete(uint32_t index, uint32_t objSize, uint32_t heapObjects)
this->m_indexStack[--this->m_allocated] = index; this->m_indexStack[--this->m_allocated] = index;
} }
void CObjectHeap::Free() {
if (this->m_obj) {
SMemFree(this->m_obj, __FILE__, __LINE__, 0);
}
this->m_obj = nullptr;
this->m_indexStack = nullptr;
this->m_allocated = 0;
}

View file

@ -6,18 +6,21 @@
class CObjectHeap { class CObjectHeap {
public: public:
// Member variables // Member variables
void* m_obj = nullptr; mutable void* m_obj = nullptr;
uint32_t* m_indexStack = nullptr; mutable uint32_t* m_indexStack = nullptr;
uint32_t m_allocated = 0; mutable uint32_t m_allocated = 0;
// Member functions // Member functions
CObjectHeap() = default; CObjectHeap() = default;
CObjectHeap(const CObjectHeap& heap);
CObjectHeap& operator=(const CObjectHeap& heap) = delete;
~CObjectHeap(); ~CObjectHeap();
int32_t Allocate(uint32_t objSize, uint32_t heapObjects, const char* heapName); int32_t Allocate(uint32_t objSize, uint32_t heapObjects, const char* heapName);
int32_t New(uint32_t objSize, uint32_t heapObjects, uint32_t* index, const char* heapName, void** obj, bool zero); int32_t New(uint32_t objSize, uint32_t heapObjects, uint32_t* index, const char* heapName, void** obj, bool zero);
void* Ptr(uint32_t index, uint32_t objSize, uint32_t heapObjects); void* Ptr(uint32_t index, uint32_t objSize, uint32_t heapObjects);
void Delete(uint32_t index, uint32_t objSize, uint32_t heapObjects); void Delete(uint32_t index, uint32_t objSize, uint32_t heapObjects);
void Free();
}; };
#endif #endif

View file

@ -1,6 +1,26 @@
#include "common/objectalloc/CObjectHeapList.hpp" #include "common/objectalloc/CObjectHeapList.hpp"
#include <storm/Error.hpp> #include <storm/Error.hpp>
CObjectHeapList::CObjectHeapList(const CObjectHeapList& list) {
if (this == &list) {
return;
}
this->m_heaps.Set(list.m_heaps.Count(), list.m_heaps.Ptr());
this->m_objSize = list.m_objSize;
this->m_objsPerBlock = list.m_objsPerBlock;
this->m_numFullHeaps = list.m_numFullHeaps;
this->m_hasEmptyHeaps = list.m_hasEmptyHeaps;
this->uint20 = list.uint20;
this->m_fullestHeap = list.m_fullestHeap;
memcpy(this->m_heapName, list.m_heapName, sizeof(this->m_heapName));
this->uint78 = list.uint78;
this->uint7C = list.uint7C;
this->uint80 = list.uint80;
this->m_freeEmptyHeaps = list.m_freeEmptyHeaps;
}
int32_t CObjectHeapList::New(uint32_t* index, void** obj, bool zero) { int32_t CObjectHeapList::New(uint32_t* index, void** obj, bool zero) {
STORM_ASSERT(index); STORM_ASSERT(index);
@ -50,10 +70,19 @@ int32_t CObjectHeapList::New(uint32_t* index, void** obj, bool zero) {
return 1; return 1;
} }
void* CObjectHeapList::Ptr(uint32_t index) {
uint32_t heapId = index / this->m_objsPerBlock;
uint32_t stackId = index % this->m_objsPerBlock;
STORM_ASSERT(heapId < this->m_heaps.Count());
return this->m_heaps[heapId].Ptr(stackId, this->m_objSize, this->m_objsPerBlock);
}
void CObjectHeapList::Delete(uint32_t index) { void CObjectHeapList::Delete(uint32_t index) {
uint32_t heapId = index / this->m_objsPerBlock; uint32_t heapId = index / this->m_objsPerBlock;
uint32_t stackId = index % this->m_objsPerBlock; uint32_t stackId = index % this->m_objsPerBlock;
STORM_ASSERT(heapId < this->m_heaps.Count());
auto& heap = this->m_heaps[heapId]; auto& heap = this->m_heaps[heapId];
if (heap.m_allocated == this->m_objsPerBlock) { if (heap.m_allocated == this->m_objsPerBlock) {
this->m_numFullHeaps--; this->m_numFullHeaps--;
@ -108,7 +137,13 @@ void CObjectHeapList::FreeEmptyHeaps() {
uint32_t count = this->m_heaps.Count(); uint32_t count = this->m_heaps.Count();
while (count > 0 && totalAllocated < totalObjsPerBlock && !totalEmptyHeaps) { while (count > 0 && totalAllocated < totalObjsPerBlock && !totalEmptyHeaps) {
// TODO: Remove last element from this->m_heaps auto& heap = this->m_heaps[count - 1];
if (heap.m_obj && !heap.m_allocated) {
heap.Free();
totalObjsPerBlock -= this->m_objsPerBlock;
totalEmptyHeaps--;
this->uint20 = 0;
}
count--; count--;
} }

View file

@ -14,14 +14,18 @@ class CObjectHeapList {
uint32_t m_hasEmptyHeaps = 0; uint32_t m_hasEmptyHeaps = 0;
uint32_t uint20 = 0; uint32_t uint20 = 0;
uint32_t m_fullestHeap = 0; uint32_t m_fullestHeap = 0;
char m_heapName[80]; char m_heapName[80] = {};
uint32_t uint78 = 0; uint32_t uint78 = 0;
uint32_t uint7C = 0; uint32_t uint7C = 0;
uint32_t uint80 = 0; uint32_t uint80 = 0;
uint8_t m_freeEmptyHeaps = 1; uint8_t m_freeEmptyHeaps = 1;
// Member functions // Member functions
CObjectHeapList() = default;
CObjectHeapList(const CObjectHeapList& list);
CObjectHeapList& operator=(const CObjectHeapList& list) = delete;
int32_t New(uint32_t* index, void** obj, bool zero); int32_t New(uint32_t* index, void** obj, bool zero);
void* Ptr(uint32_t index);
void Delete(uint32_t index); void Delete(uint32_t index);
void FreeEmptyHeaps(); void FreeEmptyHeaps();
uint32_t BlocksAllocated(); uint32_t BlocksAllocated();