metaforce/Runtime/CMemory.cpp

147 lines
3.7 KiB
C++
Raw Normal View History

2016-04-15 20:42:40 +00:00
#if 0
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
{
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-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)
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)
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)
{
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)
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)
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)
{
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