2015-08-17 05:26:58 +00:00
|
|
|
#include "CMemory.hpp"
|
|
|
|
#include "CGameAllocator.hpp"
|
2015-08-23 23:58:07 +00:00
|
|
|
#include "CCallStack.hpp"
|
2015-08-23 18:53:43 +00:00
|
|
|
#include <LogVisor/LogVisor.hpp>
|
2015-08-17 05:26:58 +00:00
|
|
|
|
2015-08-17 22:05:00 +00:00
|
|
|
namespace Retro
|
|
|
|
{
|
|
|
|
|
2015-08-23 18:53:43 +00:00
|
|
|
LogVisor::LogModule Log("CMemory");
|
|
|
|
|
|
|
|
static CGameAllocator g_gameAllocator;
|
|
|
|
static IAllocator* g_memoryAllocator = &g_gameAllocator;
|
|
|
|
static bool g_memoryAllocatorReady = false;
|
2015-08-17 05:26:58 +00:00
|
|
|
|
2015-08-28 00:11:31 +00:00
|
|
|
void CMemory::Startup()
|
2015-08-17 05:26:58 +00:00
|
|
|
{
|
2015-08-28 00:11:31 +00:00
|
|
|
g_memoryAllocatorReady = g_memoryAllocator->Initialize();
|
2015-08-17 05:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMemory::Shutdown()
|
|
|
|
{
|
2015-08-23 18:53:43 +00:00
|
|
|
g_memoryAllocator->Shutdown();
|
|
|
|
g_memoryAllocatorReady = false;
|
2015-08-17 05:26:58 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 00:11:31 +00:00
|
|
|
void CMemory::SetAllocator(IAllocator& alloc)
|
2015-08-17 05:26:58 +00:00
|
|
|
{
|
2015-08-23 18:53:43 +00:00
|
|
|
if (&alloc != g_memoryAllocator)
|
2015-08-17 05:26:58 +00:00
|
|
|
{
|
2015-08-23 18:53:43 +00:00
|
|
|
g_memoryAllocator = &alloc;
|
2015-08-28 00:11:31 +00:00
|
|
|
g_memoryAllocator->Initialize();
|
2015-08-17 05:26:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 18:53:43 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-08-23 23:58:07 +00:00
|
|
|
void* CMemory::Alloc(size_t sz, IAllocator::EHint hint, IAllocator::EScope scope,
|
2015-08-23 18:53:43 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-08-28 00:11:31 +00:00
|
|
|
CMemorySys::CMemorySys(IAllocator& alloc)
|
2015-08-17 05:26:58 +00:00
|
|
|
{
|
2015-08-28 00:11:31 +00:00
|
|
|
CMemory::Startup();
|
|
|
|
CMemory::SetAllocator(alloc);
|
2015-08-17 05:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CMemorySys::~CMemorySys()
|
|
|
|
{
|
|
|
|
CMemory::Shutdown();
|
|
|
|
}
|
|
|
|
|
2015-08-23 18:53:43 +00:00
|
|
|
IAllocator& CMemorySys::GetGameAllocator() {return g_gameAllocator;}
|
2015-08-17 05:26:58 +00:00
|
|
|
|
2015-08-17 22:05:00 +00:00
|
|
|
}
|
2015-08-23 23:58:07 +00:00
|
|
|
|
2015-08-25 07:04:50 +00:00
|
|
|
void* operator new(std::size_t sz)
|
2015-08-23 23:58:07 +00:00
|
|
|
{
|
2015-08-25 07:04:50 +00:00
|
|
|
Retro::CCallStack cs("?\?(?\?)", "UnknownType");
|
|
|
|
return Retro::CMemory::Alloc(sz,
|
|
|
|
Retro::IAllocator::HintNone,
|
|
|
|
Retro::IAllocator::ScopeDefault,
|
|
|
|
Retro::IAllocator::TypePrimitive,
|
|
|
|
cs);
|
2015-08-23 23:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void* operator new(std::size_t sz,
|
|
|
|
const char* funcName, const char* typeName)
|
|
|
|
{
|
|
|
|
Retro::CCallStack cs(funcName, typeName);
|
|
|
|
return Retro::CMemory::Alloc(sz,
|
|
|
|
Retro::IAllocator::HintNone,
|
|
|
|
Retro::IAllocator::ScopeDefault,
|
|
|
|
Retro::IAllocator::TypePrimitive,
|
|
|
|
cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete(void* ptr) noexcept
|
|
|
|
{
|
|
|
|
Retro::CMemory::Free(ptr);
|
|
|
|
}
|
|
|
|
|
2015-08-25 07:04:50 +00:00
|
|
|
void* operator new[](std::size_t sz)
|
2015-08-23 23:58:07 +00:00
|
|
|
{
|
2015-08-25 07:04:50 +00:00
|
|
|
Retro::CCallStack cs("?\?(?\?)", "UnknownType");
|
|
|
|
return Retro::CMemory::Alloc(sz,
|
|
|
|
Retro::IAllocator::HintNone,
|
|
|
|
Retro::IAllocator::ScopeDefault,
|
|
|
|
Retro::IAllocator::TypeArray,
|
|
|
|
cs);
|
2015-08-23 23:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void* operator new[](std::size_t sz,
|
|
|
|
const char* funcName, const char* typeName)
|
|
|
|
{
|
|
|
|
Retro::CCallStack cs(funcName, typeName);
|
|
|
|
return Retro::CMemory::Alloc(sz,
|
|
|
|
Retro::IAllocator::HintNone,
|
|
|
|
Retro::IAllocator::ScopeDefault,
|
|
|
|
Retro::IAllocator::TypeArray,
|
|
|
|
cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete[](void* ptr) noexcept
|
|
|
|
{
|
|
|
|
Retro::CMemory::Free(ptr);
|
|
|
|
}
|