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
|
|
|
|
2016-02-13 09:02:47 +00:00
|
|
|
namespace pshag
|
2015-08-17 22:05:00 +00:00
|
|
|
{
|
|
|
|
|
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
|
|
|
{
|
2016-02-13 09:02:47 +00:00
|
|
|
if (!pshag::g_memoryAllocatorReady)
|
2015-12-02 22:05:44 +00:00
|
|
|
return malloc(sz);
|
2016-02-13 09:02:47 +00:00
|
|
|
pshag::CCallStack cs("?\?(?\?)", "UnknownType");
|
|
|
|
return pshag::CMemory::Alloc(sz,
|
|
|
|
pshag::IAllocator::EHint::None,
|
|
|
|
pshag::IAllocator::EScope::Default,
|
|
|
|
pshag::IAllocator::EType::Primitive,
|
2015-08-25 07:04:50 +00:00
|
|
|
cs);
|
2015-08-23 23:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void* operator new(std::size_t sz,
|
|
|
|
const char* funcName, const char* typeName)
|
|
|
|
{
|
2016-02-13 09:02:47 +00:00
|
|
|
if (!pshag::g_memoryAllocatorReady)
|
2015-12-02 22:05:44 +00:00
|
|
|
return malloc(sz);
|
2016-02-13 09:02:47 +00:00
|
|
|
pshag::CCallStack cs(funcName, typeName);
|
|
|
|
return pshag::CMemory::Alloc(sz,
|
|
|
|
pshag::IAllocator::EHint::None,
|
|
|
|
pshag::IAllocator::EScope::Default,
|
|
|
|
pshag::IAllocator::EType::Primitive,
|
2015-08-23 23:58:07 +00:00
|
|
|
cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete(void* ptr) noexcept
|
|
|
|
{
|
2016-02-13 09:02:47 +00:00
|
|
|
if (!pshag::g_memoryAllocatorReady)
|
2015-12-02 22:05:44 +00:00
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
return;
|
|
|
|
}
|
2016-02-13 09:02:47 +00:00
|
|
|
pshag::CMemory::Free(ptr);
|
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
|
|
|
{
|
2016-02-13 09:02:47 +00:00
|
|
|
if (!pshag::g_memoryAllocatorReady)
|
2015-12-02 22:05:44 +00:00
|
|
|
return malloc(sz);
|
2016-02-13 09:02:47 +00:00
|
|
|
pshag::CCallStack cs("?\?(?\?)", "UnknownType");
|
|
|
|
return pshag::CMemory::Alloc(sz,
|
|
|
|
pshag::IAllocator::EHint::None,
|
|
|
|
pshag::IAllocator::EScope::Default,
|
|
|
|
pshag::IAllocator::EType::Array,
|
2015-08-25 07:04:50 +00:00
|
|
|
cs);
|
2015-08-23 23:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void* operator new[](std::size_t sz,
|
|
|
|
const char* funcName, const char* typeName)
|
|
|
|
{
|
2016-02-13 09:02:47 +00:00
|
|
|
if (!pshag::g_memoryAllocatorReady)
|
2015-12-02 22:05:44 +00:00
|
|
|
return malloc(sz);
|
2016-02-13 09:02:47 +00:00
|
|
|
pshag::CCallStack cs(funcName, typeName);
|
|
|
|
return pshag::CMemory::Alloc(sz,
|
|
|
|
pshag::IAllocator::EHint::None,
|
|
|
|
pshag::IAllocator::EScope::Default,
|
|
|
|
pshag::IAllocator::EType::Array,
|
2015-08-23 23:58:07 +00:00
|
|
|
cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete[](void* ptr) noexcept
|
|
|
|
{
|
2016-02-13 09:02:47 +00:00
|
|
|
if (!pshag::g_memoryAllocatorReady)
|
2015-12-02 22:05:44 +00:00
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
return;
|
|
|
|
}
|
2016-02-13 09:02:47 +00:00
|
|
|
pshag::CMemory::Free(ptr);
|
2015-08-23 23:58:07 +00:00
|
|
|
}
|