AuroraRuntime/Source/Debug/MemoryCrunch.cpp

63 lines
1.8 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: MemoryCrunch.cpp
Date: 2022-12-07
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "Debug.hpp"
#include "ErrorStack.hpp"
#include <Source/Memory/AuHeap.hpp>
#include <Source/Memory/AuBaseHeap.hpp>
namespace Aurora::Debug
{
static thread_local AuSInt tlsMemoryCrunchCounter;
static auto const kDefaultReservePool = 3 * 1024 * 1024;
// Extern defintions
AuUInt8 *gReservePoolStart {};
AuUInt8 *gReservePoolEnd {};
AuSPtr<AuMemory::Heap> gReserveHeap {};
void InitMemoryCrunch()
{
gReserveHeap = AuMemory::AllocHeapShared(gRuntimeConfig.debug.uDebugMemoryReserveSize ?
gRuntimeConfig.debug.uDebugMemoryReserveSize :
kDefaultReservePool);
SysAssert(gReserveHeap);
auto pHeap = AuStaticCast<AuMemory::BaseHeap>(gReserveHeap);
gReservePoolStart = AuReinterpretCast<AuUInt8 *>(pHeap->GetHeapBase());
gReservePoolEnd = AuReinterpretCast<AuUInt8 *>(gReservePoolStart) + pHeap->GetHeapLength();
}
bool IsPointerReserveRange(void *ptr)
{
auto pChar = (AuUInt8 *)ptr;
return pChar >= gReservePoolStart && pChar < gReservePoolEnd;
}
AuSPtr<AuMemory::Heap> GetCrunchInterface()
{
return gReserveHeap;
}
bool IsTlsMemoryCrunchActive()
{
return bool(tlsMemoryCrunchCounter);
}
AUKN_SYM void AddMemoryCrunch()
{
tlsMemoryCrunchCounter++;
}
AUKN_SYM void DecMemoryCrunch()
{
tlsMemoryCrunchCounter--;
SysAssert(tlsMemoryCrunchCounter >= 0, "crunch counter underflow");
}
}