From 2adbfa2736235910fd0d4f6174a52233a834ee34 Mon Sep 17 00:00:00 2001 From: robojumper Date: Thu, 30 May 2024 08:43:50 +0200 Subject: [PATCH] m_allocator --- config/SOUE01/splits.txt | 4 ++ config/SOUE01/symbols.txt | 28 +++++----- configure.py | 1 + include/m/m_allocator.h | 8 +-- src/egg/core/eggHeap.cpp | 3 +- src/m/m_allocator.cpp | 104 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 129 insertions(+), 19 deletions(-) create mode 100644 src/m/m_allocator.cpp diff --git a/config/SOUE01/splits.txt b/config/SOUE01/splits.txt index 34ce56fe..3378965e 100644 --- a/config/SOUE01/splits.txt +++ b/config/SOUE01/splits.txt @@ -222,6 +222,10 @@ f/f_manager.cpp: .sbss start:0x80575BB8 end:0x80575BC0 .bss start:0x805B84D8 end:0x805B8588 +m/m_allocator.cpp: + .text start:0x802EE0E0 end:0x802EE5EC + .data start:0x80542820 end:0x80542848 + m/m_angle.cpp: .text start:0x802EE5F0 end:0x802EE6B8 .ctors start:0x804DB8CC end:0x804DB8D0 diff --git a/config/SOUE01/symbols.txt b/config/SOUE01/symbols.txt index 8f6bb548..55b35a52 100644 --- a/config/SOUE01/symbols.txt +++ b/config/SOUE01/symbols.txt @@ -17695,21 +17695,21 @@ fn_802EE0A0 = .text:0x802EE0A0; // type:function size:0x4 fn_802EE0B0 = .text:0x802EE0B0; // type:function size:0x4 fn_802EE0C0 = .text:0x802EE0C0; // type:function size:0x4 fn_802EE0D0 = .text:0x802EE0D0; // type:function size:0x8 -fn_802EE0E0 = .text:0x802EE0E0; // type:function size:0x44 -fn_802EE130 = .text:0x802EE130; // type:function size:0x58 -fn_802EE190 = .text:0x802EE190; // type:function size:0x50 -fn_802EE1E0 = .text:0x802EE1E0; // type:function size:0x4 -fn_802EE1F0 = .text:0x802EE1F0; // type:function size:0x4 +__ct__12mAllocator_cFv = .text:0x802EE0E0; // type:function size:0x44 +__dt__12mAllocator_cFv = .text:0x802EE130; // type:function size:0x58 +attach__12mAllocator_cFPQ23EGG4Heapi = .text:0x802EE190; // type:function size:0x50 +alloc__12mAllocator_cFUl = .text:0x802EE1E0; // type:function size:0x4 +free__12mAllocator_cFPv = .text:0x802EE1F0; // type:function size:0x4 __ct__16mHeapAllocator_cFv = .text:0x802EE200; // type:function size:0x3C __dt__16mHeapAllocator_cFv = .text:0x802EE240; // type:function size:0x6C -fn_802EE2B0 = .text:0x802EE2B0; // type:function size:0x84 -fn_802EE340 = .text:0x802EE340; // type:function size:0x84 -fn_802EE3D0 = .text:0x802EE3D0; // type:function size:0x54 -fn_802EE430 = .text:0x802EE430; // type:function size:0x70 -fn_802EE4A0 = .text:0x802EE4A0; // type:function size:0x70 -fn_802EE510 = .text:0x802EE510; // type:function size:0x4C +replaceWithNewFrmHeap__16mHeapAllocator_cFlPQ23EGG4HeapPclUl = .text:0x802EE2B0; // type:function size:0x84 +replaceWithNewExpHeap__16mHeapAllocator_cFlPQ23EGG4HeapPclUl = .text:0x802EE340; // type:function size:0x84 +destroyHeap__16mHeapAllocator_cFv = .text:0x802EE3D0; // type:function size:0x54 +adjustFrmHeap__16mHeapAllocator_cFv = .text:0x802EE430; // type:function size:0x70 +adjustExpHeap__16mHeapAllocator_cFv = .text:0x802EE4A0; // type:function size:0x70 +createNewTempFrmHeap__16mHeapAllocator_cFlPQ23EGG4HeapPclUl = .text:0x802EE510; // type:function size:0x4C adjustFrmHeapRestoreCurrent__16mHeapAllocator_cFv = .text:0x802EE560; // type:function size:0x64 -fn_802EE5D0 = .text:0x802EE5D0; // type:function size:0x1C +allocOnHeap__16mHeapAllocator_cFUlP16mHeapAllocator_c = .text:0x802EE5D0; // type:function size:0x1C step__4mAngFslss = .text:0x802EE5F0; // type:function size:0xA4 __sinit_\m_angle_cpp = .text:0x802EE6A0; // type:function size:0x18 scope:local fn_802EE6C0 = .text:0x802EE6C0; // type:function size:0x114 @@ -35714,8 +35714,8 @@ lbl_805427A8 = .data:0x805427A8; // type:object size:0x2C lbl_805427D4 = .data:0x805427D4; // type:object size:0xC lbl_805427E0 = .data:0x805427E0; // type:object size:0x10 lbl_805427F0 = .data:0x805427F0; // type:object size:0x30 -lbl_80542820 = .data:0x80542820; // type:object size:0x14 -lbl_80542834 = .data:0x80542834; // type:object size:0x14 +__vt__16mHeapAllocator_c = .data:0x80542820; // type:object size:0x14 +__vt__12mAllocator_c = .data:0x80542834; // type:object size:0x14 lbl_80542848 = .data:0x80542848; // type:object size:0x28 __vt__Q24mDvd10MyThread_c = .data:0x80542870; // type:object size:0x18 __vt__23mDvd_toMainRam_normal_c = .data:0x80542888; // type:object size:0x14 diff --git a/configure.py b/configure.py index 62187b35..9d73dcd8 100644 --- a/configure.py +++ b/configure.py @@ -303,6 +303,7 @@ config.libs = [ Object(NonMatching, "f/f_base.cpp"), Object(Matching, "f/f_list.cpp"), Object(Matching, "f/f_manager.cpp"), + Object(Matching, "m/m_allocator.cpp"), Object(Matching, "m/m_angle.cpp"), Object(Matching, "m/m_dvd.cpp"), Object(Matching, "m/m_heap.cpp"), diff --git a/include/m/m_allocator.h b/include/m/m_allocator.h index d2d69cb3..1930e9ca 100644 --- a/include/m/m_allocator.h +++ b/include/m/m_allocator.h @@ -10,7 +10,7 @@ public: /* 0x08 */ virtual ~mAllocator_c(); /* 0x0C */ virtual void *alloc(u32 size); /* 0x10 */ virtual void free(void *block); - bool attach(EGG::Heap *heap, s32 align); + bool attach(EGG::Heap *heap, int align); }; class mHeapAllocator_c : public mAllocator_c { public: @@ -19,13 +19,13 @@ public: /* 0x08 */ virtual ~mHeapAllocator_c(); /* 0x0C */ // virtual void* alloc(u32 size); // see mAlloctor::alloc /* 0x10 */ // virtual void free(void* block); // see mAlloctor::free - int replaceWithNewFrmHeap(s32 size, EGG::Heap *newHeap, char *heapName, s32 align, u32 unk); - int replaceWithNewExpHeap(s32 size, EGG::Heap *newHeap, char *heapName, s32 align, u32 unk); + bool replaceWithNewFrmHeap(s32 size, EGG::Heap *newHeap, char *heapName, s32 align, u32 unk); + bool replaceWithNewExpHeap(s32 size, EGG::Heap *newHeap, char *heapName, s32 align, u32 unk); void destroyHeap(); s32 adjustFrmHeap(); s32 adjustExpHeap(); s32 createNewTempFrmHeap(s32 size, EGG::Heap *newHeap, char *heapName, s32 align, u32 unk); void adjustFrmHeapRestoreCurrent(); - static void *allocOnHeap(s32 size, mHeapAllocator_c *allocator); + static void *allocOnHeap(u32 size, mHeapAllocator_c *allocator); }; #endif diff --git a/src/egg/core/eggHeap.cpp b/src/egg/core/eggHeap.cpp index 49234e0d..a7aa7817 100644 --- a/src/egg/core/eggHeap.cpp +++ b/src/egg/core/eggHeap.cpp @@ -148,7 +148,8 @@ extern "C" MEMiHeapHead *fn_803CC670(const void *memBlock); u32 mem[2] = {0, 0}; OSLockMutex(&sRootMutex); - for (Heap *heap = nullptr; heap != nullptr; heap = (Heap *)nw4r::ut::List_GetNext(&sHeapList, &heap)) { + Heap *heap = nullptr; + while ((heap = (Heap *)nw4r::ut::List_GetNext(&sHeapList, &heap)) != nullptr) { Heap *childHeap = nullptr; Heap *parentHeap = heap->findParentHeap(); if ((u32)heap < 0x90000000) { diff --git a/src/m/m_allocator.cpp b/src/m/m_allocator.cpp new file mode 100644 index 00000000..a4f2c05b --- /dev/null +++ b/src/m/m_allocator.cpp @@ -0,0 +1,104 @@ +#include +#include +#include + +// TODO this include is weird +#include +#include + +mAllocator_c::mAllocator_c() : EGG::Allocator(mHeap::g_assertHeap, 0x04) {} + +mAllocator_c::~mAllocator_c() {} + +bool mAllocator_c::attach(EGG::Heap *heap, int align) { + if (heap == nullptr) { + heap = EGG::Heap::sCurrentHeap; + } + this->align = align; + this->mHeap = heap; + heap->initAllocator(this, align); + return true; +} + +void *mAllocator_c::alloc(u32 size) { + return EGG::Allocator::alloc(size); +} + +void mAllocator_c::free(void *block) { + EGG::Allocator::free(block); +} + +mHeapAllocator_c::mHeapAllocator_c() {} +mHeapAllocator_c::~mHeapAllocator_c() { + destroyHeap(); +} + +bool mHeapAllocator_c::replaceWithNewFrmHeap(s32 size, EGG::Heap *newHeap, char *heapName, s32 align, u32 attrs) { + destroyHeap(); + EGG::Heap *heap = mHeap::createFrmHeap(size, newHeap, heapName, align, attrs); + if (heap == nullptr) { + return false; + } + attach(heap, align); + return true; +} + +bool mHeapAllocator_c::replaceWithNewExpHeap(s32 size, EGG::Heap *newHeap, char *heapName, s32 align, u32 attrs) { + destroyHeap(); + EGG::Heap *heap = mHeap::createExpHeap(size, newHeap, heapName, align, attrs); + if (heap == nullptr) { + return false; + } + attach(heap, align); + return true; +} + +void mHeapAllocator_c::destroyHeap() { + EGG::Heap *assertHeap = mHeap::g_assertHeap; + if (mHeap != assertHeap) { + mHeap->destroy(); + mHeap = assertHeap; + } +} + +inline EGG::Heap *getHeapOfKind(EGG::Heap *heap, EGG::Heap::eHeapKind kind) { + if (heap != nullptr && heap->getHeapKind() == kind) { + return heap; + } + return nullptr; +} + +s32 mHeapAllocator_c::adjustFrmHeap() { + EGG::Heap *heap = mHeap; + if (heap == mHeap::g_assertHeap) { + return 0; + } + return mHeap::adjustFrmHeap(static_cast(getHeapOfKind(heap, EGG::Heap::HEAP_KIND_FRAME))); +} + +s32 mHeapAllocator_c::adjustExpHeap() { + EGG::Heap *heap = mHeap; + if (heap == mHeap::g_assertHeap) { + return 0; + } + return mHeap::adjustExpHeap(static_cast(getHeapOfKind(heap, EGG::Heap::HEAP_KIND_EXPANDED))); +} + +s32 mHeapAllocator_c::createNewTempFrmHeap(s32 size, EGG::Heap *newHeap, char *heapName, s32 align, u32 attrs) { + if (!replaceWithNewFrmHeap(size, newHeap, heapName, align, attrs)) { + return false; + } + mHeap::saveCurrentHeap(); + mHeap::setCurrentHeap(mHeap); + return true; +} + +void mHeapAllocator_c::adjustFrmHeapRestoreCurrent() { + mHeap::restoreCurrentHeap(); + EGG::Heap *heap = mHeap; + mHeap::adjustFrmHeap(static_cast(getHeapOfKind(heap, EGG::Heap::HEAP_KIND_FRAME))); +} + +void *mHeapAllocator_c::allocOnHeap(u32 size, mHeapAllocator_c *allocator) { + return allocator->alloc(size); +}