some CMemory additions

This commit is contained in:
Jack Andersen 2015-08-23 08:53:43 -10:00
parent f3b5b9f49a
commit 5784759b62
8 changed files with 161 additions and 13 deletions

20
Runtime/CCallStack.hpp Normal file
View File

@ -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__

View File

View File

@ -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<SGameMemInfo*>(reinterpret_cast<u8*>(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;
};
}

View File

@ -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

View File

@ -1,33 +1,60 @@
#include "CMemory.hpp"
#include "CGameAllocator.hpp"
#include <LogVisor/LogVisor.hpp>
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;}
}

View File

@ -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

View File

@ -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)
{

View File

@ -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;
};
}