AuroraRuntime/Include/Aurora/Debug/MemoryCrunch.hpp

50 lines
1.5 KiB
C++

/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: MemoryCrunch.hpp
Date: 2021-6-9 - 2023-08-10
Author: Reece
***/
#pragma once
namespace Aurora::Debug
{
AUKN_SYM void AddMemoryCrunch();
AUKN_SYM void DecMemoryCrunch();
struct MemoryCrunch
{
inline MemoryCrunch()
{
AddMemoryCrunch();
}
inline ~MemoryCrunch()
{
DecMemoryCrunch();
}
};
struct MemoryCrunchCallOut
{
inline MemoryCrunchCallOut()
{
DecMemoryCrunch();
}
inline ~MemoryCrunchCallOut()
{
AddMemoryCrunch();
}
};
// Memory crunches are used to inform the main allocation functions that failing is a critical mistake.
// Instead of outright failing pursuant to the RuntimeConfig information, we should first try to access the
// emergency memory heap allocated during init. These are useful for trivial functions you wouldn't want
// blowing up under exceptionless paths. In addition, this heap as utilized by ::AddMemoryCrunch is used
// by the debug system to ensure exception handlers, logging, telemetry, and exit conditions do not fail early.
// This way we can still use standard containers of no particular allocator in order to continue without grief.
#define AU_DEBUG_MEMCRUNCH Aurora::Debug::MemoryCrunch AU_CONCAT(__crunch, __COUNTER__);
#define AU_DEBUG_REVERSE_MEMCRUNCH Aurora::Debug::MemoryCrunchCallOut AU_CONCAT(__crunch, __COUNTER__);
}