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"
|
2016-03-04 23:04:53 +00:00
|
|
|
#include "logvisor/logvisor.hpp"
|
2015-08-17 05:26:58 +00:00
|
|
|
|
2016-03-04 23:04:53 +00:00
|
|
|
namespace urde
|
2015-08-17 22:05:00 +00:00
|
|
|
{
|
|
|
|
|
2016-03-05 00:03:41 +00:00
|
|
|
static logvisor::Module Log("CMemory");
|
2015-08-23 18:53:43 +00:00
|
|
|
|
|
|
|
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)
|
2016-03-04 23:04:53 +00:00
|
|
|
Log.report(logvisor::Error, "Alloc Failed! - Size %d", sz);
|
2015-08-23 18:53:43 +00:00
|
|
|
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
|
|
|
|
2016-04-15 03:02:21 +00:00
|
|
|
#if 0
|
2015-08-25 07:04:50 +00:00
|
|
|
void* operator new(std::size_t sz)
|
2015-08-23 23:58:07 +00:00
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
if (!urde::g_memoryAllocatorReady)
|
2015-12-02 22:05:44 +00:00
|
|
|
return malloc(sz);
|
2016-03-04 23:04:53 +00:00
|
|
|
urde::CCallStack cs("?\?(?\?)", "UnknownType");
|
|
|
|
return urde::CMemory::Alloc(sz,
|
|
|
|
urde::IAllocator::EHint::None,
|
|
|
|
urde::IAllocator::EScope::Default,
|
|
|
|
urde::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-03-04 23:04:53 +00:00
|
|
|
if (!urde::g_memoryAllocatorReady)
|
2015-12-02 22:05:44 +00:00
|
|
|
return malloc(sz);
|
2016-03-04 23:04:53 +00:00
|
|
|
urde::CCallStack cs(funcName, typeName);
|
|
|
|
return urde::CMemory::Alloc(sz,
|
|
|
|
urde::IAllocator::EHint::None,
|
|
|
|
urde::IAllocator::EScope::Default,
|
|
|
|
urde::IAllocator::EType::Primitive,
|
2015-08-23 23:58:07 +00:00
|
|
|
cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete(void* ptr) noexcept
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
if (!urde::g_memoryAllocatorReady)
|
2015-12-02 22:05:44 +00:00
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
return;
|
|
|
|
}
|
2016-03-04 23:04:53 +00:00
|
|
|
urde::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-03-04 23:04:53 +00:00
|
|
|
if (!urde::g_memoryAllocatorReady)
|
2015-12-02 22:05:44 +00:00
|
|
|
return malloc(sz);
|
2016-03-04 23:04:53 +00:00
|
|
|
urde::CCallStack cs("?\?(?\?)", "UnknownType");
|
|
|
|
return urde::CMemory::Alloc(sz,
|
|
|
|
urde::IAllocator::EHint::None,
|
|
|
|
urde::IAllocator::EScope::Default,
|
|
|
|
urde::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-03-04 23:04:53 +00:00
|
|
|
if (!urde::g_memoryAllocatorReady)
|
2015-12-02 22:05:44 +00:00
|
|
|
return malloc(sz);
|
2016-03-04 23:04:53 +00:00
|
|
|
urde::CCallStack cs(funcName, typeName);
|
|
|
|
return urde::CMemory::Alloc(sz,
|
|
|
|
urde::IAllocator::EHint::None,
|
|
|
|
urde::IAllocator::EScope::Default,
|
|
|
|
urde::IAllocator::EType::Array,
|
2015-08-23 23:58:07 +00:00
|
|
|
cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete[](void* ptr) noexcept
|
|
|
|
{
|
2016-03-04 23:04:53 +00:00
|
|
|
if (!urde::g_memoryAllocatorReady)
|
2015-12-02 22:05:44 +00:00
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
return;
|
|
|
|
}
|
2016-03-04 23:04:53 +00:00
|
|
|
urde::CMemory::Free(ptr);
|
2015-08-23 23:58:07 +00:00
|
|
|
}
|
2016-04-15 03:02:21 +00:00
|
|
|
|
|
|
|
#endif
|