Match and link CMemory

This commit is contained in:
2022-10-12 01:07:35 -07:00
parent 0787cc17d0
commit 16d013b419
10 changed files with 115 additions and 12 deletions

View File

@@ -112,7 +112,7 @@ void* CGameAllocator::Alloc(size_t size, EHint hint, EScope scope, EType type,
buf = x74_mediumPool->Alloc(size);
if (buf == nullptr) {
buf = Alloc(0x21000, kHI_None, kSC_Unk1, kTP_Unk0,
buf = Alloc(0x21000, kHI_None, kSC_Unk1, kTP_Heap,
CCallStack(-1, "MediumAllocMainData ", " - Ignore"));
x74_mediumPool->AddPuddle(0x1000, buf, 1);
buf = x74_mediumPool->Alloc(size);

View File

@@ -0,0 +1,90 @@
#include "Kyoto/Alloc/CMemory.hpp"
#include "Kyoto/Alloc/CMemorySys.hpp"
#include "Kyoto/Alloc/CCallStack.hpp"
#include "Kyoto/Alloc/CGameAllocator.hpp"
#include "Kyoto/Basics/RAssertDolphin.hpp"
#include "dolphin/os.h"
static CGameAllocator gGameAllocator;
IAllocator* CMemory::mpAllocator = &gGameAllocator;
bool CMemory::mInitialized;
uint gLeakCount = 0;
uint gLeakBytes = 0;
CMemorySys::CMemorySys(COsContext& ctx, IAllocator& allocator) {
CMemory::Startup(ctx);
CMemory::SetAllocator(ctx, allocator);
}
CMemorySys::~CMemorySys() { CMemory::Shutdown(); }
IAllocator& CMemorySys::GetGameAllocator() { return gGameAllocator; }
void CMemory::Startup(COsContext& ctx) { mInitialized = mpAllocator->Initialize(ctx); }
void CMemory::SetAllocator(COsContext& ctx, IAllocator& allocator) {
if (mpAllocator != &allocator) {
if (mpAllocator != nullptr) {
mpAllocator->ReleaseAll();
}
mpAllocator = &allocator;
mpAllocator->Initialize(ctx);
}
}
static bool cmemory_enum_alloc_cb(const IAllocator::SAllocInfo& info, const void* ptr) {
if (info.x8_isAllocated && info.x9_ == 0) {
++gLeakCount;
gLeakBytes += info.x4_len;
}
return true;
}
void CMemory::Shutdown() {
CMemory::mInitialized = false;
if (mpAllocator->GetMetrics().x8_ != 0) {
gLeakCount = 0;
gLeakBytes = 0;
mpAllocator->EnumAllocations((IAllocator::FEnumAllocationsCb)cmemory_enum_alloc_cb, nullptr,
false);
}
mpAllocator->Shutdown();
}
void* CMemory::Alloc(size_t len, IAllocator::EHint hint, IAllocator::EScope scope,
IAllocator::EType type, const CCallStack& callstack) {
volatile bool enabled = OSDisableInterrupts();
void* ret = mpAllocator->Alloc(len, hint, scope, type, callstack);
if (ret == nullptr) {
rs_debugger_printf("Alloc failed - Size: %d", len);
}
OSRestoreInterrupts(enabled);
return ret;
}
void CMemory::Free(const void* ptr) {
volatile bool enabled = OSDisableInterrupts();
if (ptr != nullptr) {
mpAllocator->Free(ptr);
}
OSRestoreInterrupts(enabled);
}
void CMemory::SetOutOfMemoryCallback(IAllocator::FOutOfMemoryCb cb, const void* context) {
mpAllocator->SetOutOfMemoryCallback(cb, context);
}
void CMemory::OffsetFakeStatics(int offset) { mpAllocator->OffsetFakeStatics(offset); }
void* operator new(size_t sz, const char* fileAndLine, const char* type) {
return CMemory::Alloc(sz, IAllocator::kHI_None, IAllocator::kSC_Unk1, IAllocator::kTP_Heap,
CCallStack(-1, fileAndLine, type));
}
void* operator new[](size_t sz, const char* fileAndLine, const char* type) {
return CMemory::Alloc(sz, IAllocator::kHI_None, IAllocator::kSC_Unk1, IAllocator::kTP_Array,
CCallStack(-1, fileAndLine, type));
}

View File

@@ -1,5 +1,6 @@
#include "Kyoto/Alloc/IAllocator.hpp"
#include "Kyoto/Alloc/CMemory.hpp"
#include "Kyoto/Basics/COsContext.hpp"
IAllocator::SMetrics::SMetrics(uint heapSize, uint unk1, uint unk2, uint unk3, uint unk4,