From 5784759b6236ea999047b9b1ffd66845419d05fc Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sun, 23 Aug 2015 08:53:43 -1000 Subject: [PATCH] some CMemory additions --- Runtime/CCallStack.hpp | 20 ++++++++++++++ Runtime/CCallStackNix.cpp | 0 Runtime/CGameAllocator.hpp | 32 ++++++++++++++++++++++ Runtime/CMakeLists.txt | 3 ++- Runtime/CMemory.cpp | 47 +++++++++++++++++++++++++------- Runtime/CMemory.hpp | 7 ++++- Runtime/CResLoader.hpp | 10 ++++++- Runtime/IAllocator.hpp | 55 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 161 insertions(+), 13 deletions(-) create mode 100644 Runtime/CCallStack.hpp create mode 100644 Runtime/CCallStackNix.cpp diff --git a/Runtime/CCallStack.hpp b/Runtime/CCallStack.hpp new file mode 100644 index 000000000..daa28ac49 --- /dev/null +++ b/Runtime/CCallStack.hpp @@ -0,0 +1,20 @@ +#ifndef __RETRO_CCALLSTACK_HPP__ +#define __RETRO_CCALLSTACK_HPP__ + +namespace Retro +{ + +class CCallStack +{ + const char* x0_fileAndLineStr; + const char* x4_typeStr; +public: + CCallStack(char const* fileAndLineStr, char const* typeStr) + : x0_fileAndLineStr(fileAndLineStr), x4_typeStr(typeStr) {} + const char* GetFileAndLineText() const {return x0_fileAndLineStr;} + const char* GetTypeText() const {return x4_typeStr;} +}; + +} + +#endif // __RETRO_CCALLSTACK_HPP__ diff --git a/Runtime/CCallStackNix.cpp b/Runtime/CCallStackNix.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/Runtime/CGameAllocator.hpp b/Runtime/CGameAllocator.hpp index 28fd38e6e..909ea1f28 100644 --- a/Runtime/CGameAllocator.hpp +++ b/Runtime/CGameAllocator.hpp @@ -5,12 +5,44 @@ namespace Retro { +class CCallStack; class CGameAllocator : public IAllocator { public: + struct SGameMemInfo + { + }; +private: + SGameMemInfo* x10_rootInfo; + u32 xbc_fakeStaticOff = 0; +public: + SGameMemInfo* FindFreeBlock(u32); + SGameMemInfo* FindFreeBlockFromTopOfHeap(u32); + u32 FixupAllocPtrs(SGameMemInfo*, u32, u32, EHint, const CCallStack&); + void UpdateAllocDebugStats(u32, u32, u32); + bool FreeNormalAllocation(void*); + u32 GetFreeBinEntryForSize(u32); + void AddFreeEntryToFreeList(SGameMemInfo*); + void RemoveFreeEntryFromFreeList(SGameMemInfo*); + void DumpAllocations() const; + u32 GetLargestFreeChunk() const; + SGameMemInfo* GetMemInfoFromBlockPtr(void* ptr) + {return reinterpret_cast(reinterpret_cast(ptr) - 32);} + bool Initialize(COsContext&); void Shutdown(); + void* Alloc(u32, EHint, EScope, EType, const CCallStack&); + void Free(void*); + void ReleaseAll(); + void* AllocSecondary(u32, EHint, EScope, EType, const CCallStack&); + void FreeSecondary(void*); + void ReleaseAllSecondary(); + void SetOutOfMemoryCallback(const TOutOfMemoryCallback, void*); + int EnumAllocations(const TAllocationVisitCallback, void*, bool) const; + SAllocInfo GetAllocInfo(void*) const; + void OffsetFakeStatics(int); + SMetrics GetMetrics() const; }; } diff --git a/Runtime/CMakeLists.txt b/Runtime/CMakeLists.txt index f1096ab2c..633265979 100644 --- a/Runtime/CMakeLists.txt +++ b/Runtime/CMakeLists.txt @@ -12,7 +12,7 @@ if(WIN32) elseif(APPLE) list(APPEND PLAT_SRCS CMemoryCardSysMac.cpp) else() - list(APPEND PLAT_SRCS CMemoryCardSysNix.cpp) + list(APPEND PLAT_SRCS CMemoryCardSysNix.cpp CCallStackNix.cpp) endif() add_library(RuntimeCommon @@ -59,6 +59,7 @@ add_library(RuntimeCommon CFactoryMgr.hpp CFactoryMgr.cpp CPakFile.hpp CPakFile.cpp CStringExtras.hpp + CCallStack.hpp rstl.hpp rstl.cpp GameGlobalObjects.hpp RetroTypes.hpp diff --git a/Runtime/CMemory.cpp b/Runtime/CMemory.cpp index 63f3e1bd7..63d8c82b3 100644 --- a/Runtime/CMemory.cpp +++ b/Runtime/CMemory.cpp @@ -1,33 +1,60 @@ #include "CMemory.hpp" #include "CGameAllocator.hpp" +#include namespace Retro { -static CGameAllocator GAME_ALLOCATOR; -static IAllocator* MEMORY_ALLOCATOR = &GAME_ALLOCATOR; -static bool MEMORY_ALLOCATOR_READY = false; +LogVisor::LogModule Log("CMemory"); + +static CGameAllocator g_gameAllocator; +static IAllocator* g_memoryAllocator = &g_gameAllocator; +static bool g_memoryAllocatorReady = false; void CMemory::Startup(COsContext& cos) { - MEMORY_ALLOCATOR_READY = MEMORY_ALLOCATOR->Initialize(cos); + g_memoryAllocatorReady = g_memoryAllocator->Initialize(cos); } void CMemory::Shutdown() { - MEMORY_ALLOCATOR->Shutdown(); - MEMORY_ALLOCATOR_READY = false; + g_memoryAllocator->Shutdown(); + g_memoryAllocatorReady = false; } void CMemory::SetAllocator(COsContext& cos, IAllocator& alloc) { - if (&alloc != MEMORY_ALLOCATOR) + if (&alloc != g_memoryAllocator) { - MEMORY_ALLOCATOR = &alloc; - MEMORY_ALLOCATOR->Initialize(cos); + g_memoryAllocator = &alloc; + g_memoryAllocator->Initialize(cos); } } +void CMemory::OffsetFakeStatics(int val) +{ + g_memoryAllocator->OffsetFakeStatics(val); +} + +void CMemory::SetOutOfMemoryCallback(const IAllocator::TOutOfMemoryCallback cb, void* ctx) +{ + g_memoryAllocator->SetOutOfMemoryCallback(cb, ctx); +} + +void CMemory::Free(void* ptr) +{ + g_memoryAllocator->Free(ptr); +} + +void* CMemory::Alloc(u32 sz, IAllocator::EHint hint, IAllocator::EScope scope, + IAllocator::EType type, const CCallStack& cs) +{ + void* newPtr = g_memoryAllocator->Alloc(sz, hint, scope, type, cs); + if (!newPtr) + Log.report(LogVisor::Error, "Alloc Failed! - Size %d", sz); + return newPtr; +} + CMemorySys::CMemorySys(COsContext& cos, IAllocator& alloc) { CMemory::Startup(cos); @@ -39,6 +66,6 @@ CMemorySys::~CMemorySys() CMemory::Shutdown(); } -IAllocator& CMemorySys::GetGameAllocator() {return GAME_ALLOCATOR;} +IAllocator& CMemorySys::GetGameAllocator() {return g_gameAllocator;} } diff --git a/Runtime/CMemory.hpp b/Runtime/CMemory.hpp index 6b4838006..bd822ea9d 100644 --- a/Runtime/CMemory.hpp +++ b/Runtime/CMemory.hpp @@ -1,9 +1,10 @@ #ifndef __RETRO_CMEMORY_HPP__ #define __RETRO_CMEMORY_HPP__ +#include "IAllocator.hpp" + namespace Retro { -class IAllocator; class COsContext; class CMemory @@ -12,6 +13,10 @@ public: static void Startup(COsContext&); static void Shutdown(); static void SetAllocator(COsContext&, IAllocator&); + static void OffsetFakeStatics(int); + static void SetOutOfMemoryCallback(const IAllocator::TOutOfMemoryCallback, void*); + static void Free(void*); + static void* Alloc(u32, IAllocator::EHint, IAllocator::EScope, IAllocator::EType, const CCallStack&); }; class CMemorySys diff --git a/Runtime/CResLoader.hpp b/Runtime/CResLoader.hpp index de43c2d98..3793f2b8b 100644 --- a/Runtime/CResLoader.hpp +++ b/Runtime/CResLoader.hpp @@ -6,6 +6,7 @@ #include "RetroTypes.hpp" #include "CPakFile.hpp" #include "CBasics.hpp" +#include "CMemory.hpp" namespace Retro { @@ -27,7 +28,14 @@ public: CInputStream* LoadNewResourcePartSync(const SObjectTag&, int, int, char*); void LoadMemResourceSync(const SObjectTag&, char*, int*); CInputStream* LoadResourceFromMemorySync(const SObjectTag&, const void*); - CInputStream* LoadNewResourceSync(const SObjectTag&, char*); + CInputStream* LoadNewResourceSync(const SObjectTag& tag, void* buf) + { + FindResourceForLoad(tag); + if (!buf) + { + + } + } CDvdRequest* LoadResourcePartAsync(const SObjectTag& tag, int offset, int length, void* buf) { diff --git a/Runtime/IAllocator.hpp b/Runtime/IAllocator.hpp index 8d3ef1c31..0697a1c51 100644 --- a/Runtime/IAllocator.hpp +++ b/Runtime/IAllocator.hpp @@ -1,15 +1,70 @@ #ifndef __RETRO_IALLOCATOR_HPP__ #define __RETRO_IALLOCATOR_HPP__ +#include "RetroTypes.hpp" + namespace Retro { class COsContext; +class CCallStack; class IAllocator { public: + virtual ~IAllocator() {} + enum EHint + { + }; + enum EScope + { + }; + enum EType + { + }; + struct SAllocInfo + { + }; + struct SMetrics + { + u32 a; + u32 b; + u32 c; + u32 d; + u32 e; + u32 f; + u32 g; + u32 h; + u32 i; + u32 j; + u32 k; + u32 l; + u32 m; + u32 n; + u32 o; + u32 p; + u32 q; + u32 r; + u32 s; + u32 t; + u32 u; + u32 v; + }; + typedef bool(*TOutOfMemoryCallback)(void*, u32); + typedef bool(*TAllocationVisitCallback)(const SAllocInfo&, void*); + virtual bool Initialize(COsContext&)=0; virtual void Shutdown()=0; + virtual void* Alloc(u32, EHint, EScope, EType, const CCallStack&)=0; + virtual void Free(void*)=0; + virtual void ReleaseAll()=0; + virtual void* AllocSecondary(u32, EHint, EScope, EType, const CCallStack&)=0; + virtual void FreeSecondary(void*)=0; + virtual void ReleaseAllSecondary()=0; + virtual void SetOutOfMemoryCallback(const TOutOfMemoryCallback, void*)=0; + virtual int EnumAllocations(const TAllocationVisitCallback, void*, bool) const=0; + virtual SAllocInfo GetAllocInfo(void*) const=0; + virtual void OffsetFakeStatics(int)=0; + virtual SMetrics GetMetrics() const=0; }; }