memory heap starting to work

This commit is contained in:
aap
2020-11-25 22:49:50 +01:00
parent 88baa9ce5f
commit 4ddc356341
8 changed files with 74 additions and 16 deletions

View File

@ -9,8 +9,11 @@
#ifdef USE_CUSTOM_ALLOCATOR
#define MEMORYHEAP_ASSERT(cond) { if (!(cond)) { printf("ASSERT File:%s Line:%d\n", __FILE__, __LINE__); exit(1); } }
#define MEMORYHEAP_ASSERT_MESSAGE(cond, message) { if (!(cond)) { printf("ASSERT File:%s Line:%d:\n\t%s\n", __FILE__, __LINE__, message); exit(1); } }
//#define MEMORYHEAP_ASSERT(cond) { if (!(cond)) { printf("ASSERT File:%s Line:%d\n", __FILE__, __LINE__); exit(1); } }
//#define MEMORYHEAP_ASSERT_MESSAGE(cond, message) { if (!(cond)) { printf("ASSERT File:%s Line:%d:\n\t%s\n", __FILE__, __LINE__, message); exit(1); } }
#define MEMORYHEAP_ASSERT(cond) assert(cond)
#define MEMORYHEAP_ASSERT_MESSAGE(cond, message) assert(cond)
// registered pointers that we keep track of
void **gPtrList[4000];
@ -272,6 +275,7 @@ CMemoryHeap::Free(void *ptr)
MEMORYHEAP_ASSERT(m_unkMemId == -1 || m_unkMemId == block->m_memId);
RegisterFree(block);
block->m_memId = MEMID_FREE;
CombineFreeBlocks(block);
FreeBlock(block);
if(block->m_ptrListIndex != -1){
@ -313,7 +317,7 @@ uint32
CMemoryHeap::CombineFreeBlocks(HeapBlockDesc *block)
{
HeapBlockDesc *next = block->GetNextConsecutive();
if(next->m_memId == MEMID_FREE)
if(next->m_memId != MEMID_FREE)
return block->m_size;
// get rid of free blocks after this one and adjust size
for(; next->m_memId == MEMID_FREE; next = next->GetNextConsecutive())
@ -535,6 +539,10 @@ MemoryMgrCalloc(uint32 num, uint32 size)
void
MemoryMgrFree(void *ptr)
{
#ifdef FIX_BUGS
// i don't suppose this is handled by RW?
if(ptr == nil) return;
#endif
gMainHeap.Free(ptr);
}

View File

@ -1,6 +1,12 @@
#pragma once
// some windows shit
#ifdef MoveMemory
#undef MoveMemory
#endif
extern RwMemoryFunctions memFuncs;
void InitMemoryMgr(void);
template<typename T, uint32 N>
class CStack
@ -56,7 +62,10 @@ struct HeapBlockDesc
}
};
#ifdef USE_CUSTOM_ALLOCATOR
// TODO: figure something out for 64 bit pointers
static_assert(sizeof(HeapBlockDesc) == 0x10, "HeapBlockDesc must have 0x10 size otherwise most of assumptions don't make sense");
#endif
struct HeapBlockList
{
@ -181,8 +190,7 @@ public:
return;
}
}
HeapBlockDesc *it;
for(it = m_freeList.m_first.m_next; it->m_size < block->m_size; it = it->m_next);
block->InsertHeapFreeBlock(it->m_prev);
HeapBlockDesc *b = m_freeList.m_first.FindSmallestFreeBlock(block->m_size);
block->InsertHeapFreeBlock(b->m_prev);
}
};