feat(objectalloc): complete implementation of ObjectAlloc methods

This commit is contained in:
VDm 2025-08-23 12:11:12 +04:00
parent 94dcf8ca77
commit 199a2c46cb
6 changed files with 47 additions and 4 deletions

View file

@ -62,6 +62,21 @@ uint32_t ObjectAllocUsage(uint32_t heapId) {
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() {
// NOTICE: It seems like sub_4D2F90 does nothing useful
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);
void* ObjectPtr(uint32_t heapId, uint32_t memHandle);
void ObjectFree(uint32_t heapId, uint32_t memHandle);
void ObjectAllocDestroy();
#endif

View file

@ -5,9 +5,7 @@
#include <storm/Error.hpp>
CObjectHeap::~CObjectHeap() {
if (this->m_obj) {
SMemFree(this->m_obj, __FILE__, __LINE__, 0);
}
this->Free();
}
int32_t CObjectHeap::Allocate(uint32_t objSize, uint32_t heapObjects, const char* heapName) {
@ -127,3 +125,12 @@ void CObjectHeap::Delete(uint32_t index, uint32_t objSize, uint32_t heapObjects)
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

@ -18,6 +18,7 @@ class CObjectHeap {
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 Delete(uint32_t index, uint32_t objSize, uint32_t heapObjects);
void Free();
};
#endif

View file

@ -50,10 +50,19 @@ int32_t CObjectHeapList::New(uint32_t* index, void** obj, bool zero) {
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) {
uint32_t heapId = index / this->m_objsPerBlock;
uint32_t stackId = index % this->m_objsPerBlock;
STORM_ASSERT(heapId < this->m_heaps.Count());
auto& heap = this->m_heaps[heapId];
if (heap.m_allocated == this->m_objsPerBlock) {
this->m_numFullHeaps--;
@ -108,7 +117,13 @@ void CObjectHeapList::FreeEmptyHeaps() {
uint32_t count = this->m_heaps.Count();
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--;
}

View file

@ -22,6 +22,7 @@ class CObjectHeapList {
// Member functions
int32_t New(uint32_t* index, void** obj, bool zero);
void* Ptr(uint32_t index);
void Delete(uint32_t index);
void FreeEmptyHeaps();
uint32_t BlocksAllocated();