mirror of
https://github.com/PrimeDecomp/prime.git
synced 2025-12-15 23:37:01 +00:00
Match and link CMemory
This commit is contained in:
@@ -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);
|
||||
|
||||
90
src/Kyoto/Alloc/CMemory.cpp
Normal file
90
src/Kyoto/Alloc/CMemory.cpp
Normal 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));
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user