AuroraRuntime/Source/Debug/MemoryCrunch.cpp

61 lines
1.6 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/Heap.hpp>
namespace Aurora::Debug
{
static thread_local AuSInt tlsMemoryCrunchCounter;
static auto const kAutoReservePool = 3 * 1024 * 1024;
AuUInt8 *gReservePoolStart {};
AuUInt8 *gReservePoolEnd {};
AuSPtr<AuMemory::Heap> gReserveHeap {};
void InitMemoryCrunch()
{
gReserveHeap = AuMemory::AllocHeapShared(gRuntimeConfig.debug.uDebugMemoryReserveSize ?
gRuntimeConfig.debug.uDebugMemoryReserveSize :
kAutoReservePool);
SysAssert(gReserveHeap);
auto pHeap = AuStaticCast<AuMemory::BaseHeap>(gReserveHeap);
gReservePoolStart = AuReinterpretCast<AuUInt8 *>(pHeap->base_);
gReservePoolEnd = AuReinterpretCast<AuUInt8 *>(pHeap->base_) + pHeap->length_;
}
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");
}
}