From 4ba5db133d7e689ef84ecaa9a8bf8b05a0653be1 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Fri, 4 Jun 2021 22:25:46 -0700 Subject: [PATCH] More CGameAllocator --- Runtime/CGameAllocator.cpp | 12 +++++++++-- Runtime/CGameAllocator.hpp | 41 ++++++++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/Runtime/CGameAllocator.cpp b/Runtime/CGameAllocator.cpp index 7d56e385d..0f7683992 100644 --- a/Runtime/CGameAllocator.cpp +++ b/Runtime/CGameAllocator.cpp @@ -32,7 +32,15 @@ void CGameAllocator::FreeSecondary() {} void CGameAllocator::ReleaseAllSecondary() {} void CGameAllocator::SetOutOfMemoryCallback() {} void CGameAllocator::EnumAllocations() {} -SAllocInfo CGameAllocator::GetAllocInfo() { return SAllocInfo(); } -void CGameAllocator::sub80351138() {} +IAllocator::SAllocInfo CGameAllocator::GetAllocInfo(void* ptr) { + SGameMemInfo* info = GetMemInfoFromBlockPtr(ptr); + return {.x0_infoPtr = info, + .x4_len = info->x4_len, + .x8_hasPrevious = info->x10_prev != nullptr, + .x9_ = false, + .xc_fileAndLne = info->x8_line, + .x10_type = info->xc_type}; +} +void CGameAllocator::OffsetFakeStatics(s32 offset) { xb8_fakeStatics += offset; } void CGameAllocator::GetMetrics() {} } // namespace metaforce diff --git a/Runtime/CGameAllocator.hpp b/Runtime/CGameAllocator.hpp index 0622c5a34..58343bcb8 100644 --- a/Runtime/CGameAllocator.hpp +++ b/Runtime/CGameAllocator.hpp @@ -15,8 +15,6 @@ public: CCallStack(int lineNum, const char* lineStr, const char* type) : x0_line(lineStr), x4_type(type) {} }; -struct SAllocInfo {}; - enum class EHint { Unk = (1 << 0), RoundUpLen = (1 << 1), @@ -34,18 +32,27 @@ enum class EType { class IAllocator { public: + struct SAllocInfo { + void* x0_infoPtr; + size_t x4_len; + bool x8_hasPrevious; + bool x9_; + const char* xc_fileAndLne; + const char* x10_type; + }; + virtual bool Initialize() = 0; // const COSContext& ctx) = 0; - virtual void* Alloc() = 0; - virtual s32 Free(void* ptr) = 0; + virtual void* Alloc(size_t size) = 0; + virtual bool Free(void* ptr) = 0; virtual void ReleaseAll() = 0; - virtual void AllocSecondary() = 0; - virtual void FreeSecondary() = 0; + virtual void* AllocSecondary(size_t size) = 0; + virtual bool FreeSecondary(void* ptr) = 0; virtual void ReleaseAllSecondary() = 0; virtual void SetOutOfMemoryCallback() = 0; virtual void EnumAllocations() = 0; - virtual SAllocInfo GetAllocInfo() = 0; - virtual void sub80351138() = 0; + virtual SAllocInfo GetAllocInfo(void* ptr) = 0; + virtual void OffsetFakeStatics(s32 offset) = 0; virtual void GetMetrics() = 0; }; @@ -61,21 +68,25 @@ class CGameAllocator : public IAllocator { u32 x1c_canary = 0xeaeaeaea; }; static u32 GetFreeBinEntryForSize(size_t len); + SGameMemInfo* GetMemInfoFromBlockPtr(void* ptr) { + return reinterpret_cast(reinterpret_cast(intptr_t(ptr) - sizeof(SGameMemInfo))); + } std::array x14_bins; + s32 xb8_fakeStatics = 0; + public: bool Initialize() override; // const COsContext& ctx); - void* Alloc() override; - s32 Free(void* ptr) override; + void* Alloc(size_t size) override; + bool Free(void* ptr) override; void ReleaseAll() override; - void AllocSecondary() override; - void FreeSecondary() override; + void* AllocSecondary(size_t size) override; + bool FreeSecondary(void* ptr) override; void ReleaseAllSecondary() override; void SetOutOfMemoryCallback() override; void EnumAllocations() override; - SAllocInfo GetAllocInfo() override; - void sub80351138() override; + SAllocInfo GetAllocInfo(void* ptr) override; + void OffsetFakeStatics(s32 offset) override; void GetMetrics() override; }; } // namespace metaforce -