mirror of
https://github.com/zeldaret/ss
synced 2026-07-31 16:07:06 -04:00
+40
-20
@@ -18,13 +18,15 @@ namespace EGG {
|
||||
/* 80576764 */ HeapCreateCallback Heap::sCreateCallback;
|
||||
/* 80576764 */ HeapDestroyCallback Heap::sDestroyCallback;
|
||||
|
||||
/* 804953f0 */ void Heap::initialize() {
|
||||
/* 804953f0 */
|
||||
void Heap::initialize() {
|
||||
nw4r::ut::List_Init(&sHeapList, 0x1c /* todo offsetof() */);
|
||||
OSInitMutex(&sRootMutex);
|
||||
sIsHeapListInitialized = true;
|
||||
}
|
||||
|
||||
/* 80495430 */ Heap::Heap(MEMiHeapHead *head) : mHeapHandle(head), mParentBlock(nullptr), mName("NoName"), mFlag() {
|
||||
/* 80495430 */
|
||||
Heap::Heap(MEMiHeapHead *head) : mHeapHandle(head), mParentBlock(nullptr), mName("NoName"), mFlag() {
|
||||
mFlag.value = 0;
|
||||
nw4r::ut::List_Init(&mChildren, 0x8 /* todo offsetof() */);
|
||||
OSLockMutex(&sRootMutex);
|
||||
@@ -32,13 +34,15 @@ namespace EGG {
|
||||
OSUnlockMutex(&sRootMutex);
|
||||
}
|
||||
|
||||
/* 804954c0 */ Heap::~Heap() {
|
||||
/* 804954c0 */
|
||||
Heap::~Heap() {
|
||||
OSLockMutex(&sRootMutex);
|
||||
nw4r::ut::List_Remove(&sHeapList, this);
|
||||
OSUnlockMutex(&sRootMutex);
|
||||
}
|
||||
|
||||
/* 80495560 */ void *Heap::alloc(size_t size, int align, Heap *heap) {
|
||||
/* 80495560 */
|
||||
void *Heap::alloc(size_t size, int align, Heap *heap) {
|
||||
Heap *currentHeap = sCurrentHeap;
|
||||
Thread *thread = Thread::findThread(OSGetCurrentThread());
|
||||
Heap *threadHeap = nullptr;
|
||||
@@ -79,7 +83,8 @@ namespace EGG {
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/* 80495690 */ Heap *Heap::findHeap(MEMiHeapHead *head) {
|
||||
/* 80495690 */
|
||||
Heap *Heap::findHeap(MEMiHeapHead *head) {
|
||||
Heap *heap = nullptr;
|
||||
OSLockMutex(&sRootMutex);
|
||||
if (sIsHeapListInitialized) {
|
||||
@@ -95,7 +100,8 @@ namespace EGG {
|
||||
return heap;
|
||||
}
|
||||
|
||||
/* 80495730 */ Heap *Heap::findParentHeap() {
|
||||
/* 80495730 */
|
||||
Heap *Heap::findParentHeap() {
|
||||
Heap *retHeap = nullptr;
|
||||
MEMiHeapHead *heap = MEMFindContainHeap(mHeapHandle);
|
||||
if (heap) {
|
||||
@@ -107,7 +113,8 @@ namespace EGG {
|
||||
|
||||
extern "C" MEMiHeapHead *fn_803CC670(const void *memBlock);
|
||||
|
||||
/* 80495780 */ Heap *Heap::findContainHeap(const void *memBlock) {
|
||||
/* 80495780 */
|
||||
Heap *Heap::findContainHeap(const void *memBlock) {
|
||||
Heap *retHeap = nullptr;
|
||||
MEMiHeapHead *heap = fn_803CC670(memBlock);
|
||||
if (heap) {
|
||||
@@ -117,7 +124,8 @@ extern "C" MEMiHeapHead *fn_803CC670(const void *memBlock);
|
||||
return retHeap;
|
||||
}
|
||||
|
||||
/* 804957c0 */ void Heap::free(void *ptr, Heap *heap) {
|
||||
/* 804957c0 */
|
||||
void Heap::free(void *ptr, Heap *heap) {
|
||||
if (heap == nullptr) {
|
||||
MEMiHeapHead *iheap = fn_803CC670(ptr);
|
||||
if (iheap == nullptr) {
|
||||
@@ -132,7 +140,8 @@ extern "C" MEMiHeapHead *fn_803CC670(const void *memBlock);
|
||||
heap->free(ptr);
|
||||
}
|
||||
|
||||
/* 80495830 */ void Heap::dispose() {
|
||||
/* 80495830 */
|
||||
void Heap::dispose() {
|
||||
mFlag.setBit(1);
|
||||
Heap *heap;
|
||||
while ((heap = (Heap *)nw4r::ut::List_GetFirst(&mChildren)) != nullptr) {
|
||||
@@ -141,14 +150,17 @@ extern "C" MEMiHeapHead *fn_803CC670(const void *memBlock);
|
||||
mFlag.resetBit(1);
|
||||
}
|
||||
|
||||
/* 804958a0 */ void Heap::dump() {}
|
||||
/* 804958a0 */
|
||||
void Heap::dump() {}
|
||||
|
||||
// TODO: This debugging function with stripped out error reports doesn't match yet
|
||||
/* 804958b0 */ void Heap::dumpAll() {
|
||||
/* 804958b0 */
|
||||
void Heap::dumpAll() {
|
||||
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) {
|
||||
@@ -166,7 +178,8 @@ extern "C" MEMiHeapHead *fn_803CC670(const void *memBlock);
|
||||
OSUnlockMutex(&sRootMutex);
|
||||
}
|
||||
|
||||
/* 804959a0 */ Heap *Heap::becomeCurrentHeap() {
|
||||
/* 804959a0 */
|
||||
Heap *Heap::becomeCurrentHeap() {
|
||||
OSLockMutex(&sRootMutex);
|
||||
Heap *h = sCurrentHeap;
|
||||
sCurrentHeap = this;
|
||||
@@ -174,7 +187,8 @@ extern "C" MEMiHeapHead *fn_803CC670(const void *memBlock);
|
||||
return h;
|
||||
}
|
||||
|
||||
/* 80495a00 */ Heap *Heap::_becomeCurrentHeapWithoutLock() {
|
||||
/* 80495a00 */
|
||||
Heap *Heap::_becomeCurrentHeapWithoutLock() {
|
||||
Heap *h = sCurrentHeap;
|
||||
sCurrentHeap = this;
|
||||
DCStoreRange(&sCurrentHeap, sizeof(sCurrentHeap));
|
||||
@@ -185,25 +199,31 @@ extern "C" MEMiHeapHead *fn_803CC670(const void *memBlock);
|
||||
extern "C" void MEMInitAllocatorForHeap(Allocator *alloc, s32 align, Heap *heap);
|
||||
|
||||
// TODO this could be an inline virtual function
|
||||
/* 80495a40 */ void Heap::initAllocator(Allocator *alloc, s32 align) {
|
||||
/* 80495a40 */
|
||||
void Heap::initAllocator(Allocator *alloc, s32 align) {
|
||||
MEMInitAllocatorForHeap(alloc, align, this);
|
||||
}
|
||||
|
||||
} // namespace EGG
|
||||
|
||||
/* 80495a60 */ void *operator new(size_t, void *p) {
|
||||
/* 80495a60 */
|
||||
void *operator new(size_t, void *p) {
|
||||
return p;
|
||||
}
|
||||
/* 80495a70 */ void *operator new(size_t size, EGG::Heap *heap, int align) {
|
||||
/* 80495a70 */
|
||||
void *operator new(size_t size, EGG::Heap *heap, int align) {
|
||||
return EGG::Heap::alloc(size, align, heap);
|
||||
}
|
||||
|
||||
/* 80495a80 */ void *operator new(size_t size, EGG::Allocator *alloc) {
|
||||
/* 80495a80 */
|
||||
void *operator new(size_t size, EGG::Allocator *alloc) {
|
||||
return MEMAllocFromAllocator(alloc->getHandle(), size);
|
||||
}
|
||||
/* 80495a90 */ void *operator new[](size_t size, int align) {
|
||||
/* 80495a90 */
|
||||
void *operator new[](size_t size, int align) {
|
||||
return EGG::Heap::alloc(size, align, nullptr);
|
||||
}
|
||||
/* 80495aa0 */ void *operator new[](size_t size, EGG::Heap *heap, int align) {
|
||||
/* 80495aa0 */
|
||||
void *operator new[](size_t size, EGG::Heap *heap, int align) {
|
||||
return EGG::Heap::alloc(size, align, heap);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
#include <egg/core/eggAllocator.h>
|
||||
#include <m/m_allocator.h>
|
||||
#include <m/m_heap.h>
|
||||
|
||||
// TODO this include is weird
|
||||
#include <egg/core/eggAssertHeap.h>
|
||||
#include <egg/core/eggHeap.h>
|
||||
|
||||
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<EGG::FrmHeap *>(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<EGG::ExpHeap *>(getHeapOfKind(heap, EGG::Heap::HEAP_KIND_EXPANDED)));
|
||||
}
|
||||
|
||||
bool 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<EGG::FrmHeap *>(getHeapOfKind(heap, EGG::Heap::HEAP_KIND_FRAME)));
|
||||
}
|
||||
|
||||
void *mHeapAllocator_c::allocOnHeap(u32 size, mHeapAllocator_c *allocator) {
|
||||
return allocator->alloc(size);
|
||||
}
|
||||
Reference in New Issue
Block a user