From f6602667509fd71bd8abfc7d79105adc05bdd2f9 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Tue, 8 Sep 2020 10:58:55 -0700 Subject: [PATCH] Initial CGameAllocator RE --- Runtime/CGameAllocator.cpp | 59 +++++++++----------------------------- Runtime/CGameAllocator.hpp | 41 ++++++++++++++------------ 2 files changed, 37 insertions(+), 63 deletions(-) diff --git a/Runtime/CGameAllocator.cpp b/Runtime/CGameAllocator.cpp index 87f062408..ff5d22896 100644 --- a/Runtime/CGameAllocator.cpp +++ b/Runtime/CGameAllocator.cpp @@ -1,57 +1,26 @@ #include "Runtime/CGameAllocator.hpp" +#include namespace metaforce { logvisor::Module AllocLog("metaforce::CGameAllocator"); #pragma GCC diagnostic ignored "-Wclass-memaccess" -std::vector CGameAllocator::m_allocations; - -u8* CGameAllocator::Alloc(size_t len) { - size_t roundedLen = ROUND_UP_64(len + sizeof(SChunkDescription)); - for (SAllocationDescription& alloc : m_allocations) { - /* We need to supply enough room for allocation information */ - if (alloc.freeOffset + roundedLen < alloc.allocSize) { - u8* ptr = alloc.memptr.get() + alloc.freeOffset; - SChunkDescription* chunkInfo = reinterpret_cast(ptr); - *chunkInfo = SChunkDescription(); - chunkInfo->parent = &alloc; - chunkInfo->len = len; - alloc.freeOffset += roundedLen; - return ptr + sizeof(SChunkDescription); +u32 CGameAllocator::GetFreeBinEntryForSize(size_t len) { + size_t bin = 0; + size_t binSize = 32; + while (true) { + if (binSize > 0x200000) { + return 15; } + if (len < binSize) { + break; + } + binSize <<= 1; + ++bin; } - - /* 1MiB minimum allocation to prevent constantly allocating small amounts of memory */ - size_t allocSz = len; - if (allocSz < (1 * 1024 * 1024 * 1024)) - allocSz = 1 * 1024 * 1024 * 1024; - - /* Pad size to allow for allocation information */ - allocSz = ROUND_UP_64(allocSz + sizeof(SChunkDescription)); - auto& alloc = m_allocations.emplace_back(); - alloc.memptr.reset(new u8[allocSz]); - u8* ptr = alloc.memptr.get(); - alloc.allocSize = allocSz; - alloc.freeOffset += roundedLen; - SChunkDescription* chunkInfo = reinterpret_cast(ptr); - *chunkInfo = SChunkDescription(); - chunkInfo->parent = &alloc; - chunkInfo->len = len; - return ptr + sizeof(SChunkDescription); + return bin; } -void CGameAllocator::Free(u8* ptr) { - SChunkDescription* info = reinterpret_cast(ptr - sizeof(SChunkDescription)); - if (info->magic != 0xE8E8E8E8 || info->sentinal != 0xEFEFEFEF) { - AllocLog.report(logvisor::Fatal, FMT_STRING("Invalid chunk description, memory corruption!")); - return; - } - - SAllocationDescription& alloc = *info->parent; - size_t roundedLen = ROUND_UP_32(info->len + sizeof(SChunkDescription)); - alloc.freeOffset -= roundedLen; - /* Invalidate chunk allocation descriptor */ - memset(info, 0, ROUND_UP_64(info->len + sizeof(SChunkDescription))); -} +bool CGameAllocator::Initialize() { return true; } } // namespace metaforce diff --git a/Runtime/CGameAllocator.hpp b/Runtime/CGameAllocator.hpp index 6bdeb3b24..642284c23 100644 --- a/Runtime/CGameAllocator.hpp +++ b/Runtime/CGameAllocator.hpp @@ -7,24 +7,29 @@ #include "Runtime/RetroTypes.hpp" namespace metaforce { -class CGameAllocator { - struct SAllocationDescription { - std::unique_ptr memptr; - size_t allocSize = 0; - ptrdiff_t freeOffset = 0; - }; - - struct SChunkDescription { - u32 magic = 0xE8E8E8E8; - SAllocationDescription* parent; - size_t len = 0; - u32 sentinal = 0xEFEFEFEF; - }; - - static std::vector m_allocations; - +class CCallStack { + const char* x0_line; + const char* x4_type; public: - static u8* Alloc(size_t len); - static void Free(u8* ptr); + CCallStack(int lineNum, const char* lineStr, const char* type) : x0_line(lineStr), x4_type(type) {} +}; + +class CGameAllocator { + struct SGameMemInfo { + u32 x0_sentinel = 0xefefefef; + size_t x4_len = 0; + const char* x8_line; + const char* xc_type; + SGameMemInfo* x10_prev = nullptr; + void* x14_ = nullptr; + void* x18_ = nullptr; + u32 x1c_canary = 0xeaeaeaea; + }; + static u32 GetFreeBinEntryForSize(size_t len); + std::array x14_bins; +public: + + bool Initialize();//const COsContext& ctx); }; } // namespace metaforce +