[+] AuMemory::SetMemoryLowNotification

This commit is contained in:
Reece Wilson 2023-10-24 18:00:09 +01:00
parent 4ce49941ff
commit ca3bded0d4
2 changed files with 22 additions and 4 deletions

View File

@ -22,10 +22,14 @@ namespace Aurora::Memory
{ {
using LeakFinderAlloc_f = void(__cdecl *)(void *, AuUInt); using LeakFinderAlloc_f = void(__cdecl *)(void *, AuUInt);
using LeakFinderFree_f = void(__cdecl *)(void *); using LeakFinderFree_f = void(__cdecl *)(void *);
using MemoryLowNotification_f = void(__cdecl *)(AuUInt, int);
AUKN_SYM void SetLeakFinder(LeakFinderAlloc_f pAlloc, AUKN_SYM void SetLeakFinder(LeakFinderAlloc_f pAlloc,
LeakFinderFree_f pFree); LeakFinderFree_f pFree);
// thread-local
AUKN_SYM void SetMemoryLowNotification(MemoryLowNotification_f pFunc);
AUKN_SYM AU_ALLOC void *_ZAlloc(Types::size_t length); AUKN_SYM AU_ALLOC void *_ZAlloc(Types::size_t length);
AUKN_SYM AU_ALLOC void *_ZAlloc(Types::size_t length, Types::size_t align); AUKN_SYM AU_ALLOC void *_ZAlloc(Types::size_t length, Types::size_t align);
AUKN_SYM AU_ALLOC void *_FAlloc(Types::size_t length); AUKN_SYM AU_ALLOC void *_FAlloc(Types::size_t length);

View File

@ -25,6 +25,7 @@ namespace Aurora::Memory
static LeakFinderAlloc_f gLeakFinderAlloc; static LeakFinderAlloc_f gLeakFinderAlloc;
static LeakFinderFree_f gLeakFinderFree; static LeakFinderFree_f gLeakFinderFree;
thread_local MemoryLowNotification_f tlsMemoryLowNotification;
static void AddBytesToCounter(AuUInt uBytes) static void AddBytesToCounter(AuUInt uBytes)
{ {
@ -36,6 +37,11 @@ namespace Aurora::Memory
AuAtomicSub(&gBytesCounterAllocated, uBytes); AuAtomicSub(&gBytesCounterAllocated, uBytes);
} }
AUKN_SYM void SetMemoryLowNotification(MemoryLowNotification_f pFunc)
{
tlsMemoryLowNotification = pFunc;
}
AUKN_SYM void SetLeakFinder(LeakFinderAlloc_f pAlloc, AUKN_SYM void SetLeakFinder(LeakFinderAlloc_f pAlloc,
LeakFinderFree_f pFree) LeakFinderFree_f pFree)
{ {
@ -66,6 +72,10 @@ namespace Aurora::Memory
(gRuntimeConfig.debug.bIsApplicationClientSoftwareOnJitteryMemorySystem)) && \ (gRuntimeConfig.debug.bIsApplicationClientSoftwareOnJitteryMemorySystem)) && \
AuDebug::gReserveHeap) \ AuDebug::gReserveHeap) \
{ \ { \
if (auto pLowNotification = tlsMemoryLowNotification) \
{ \
pLowNotification(length, 2); \
} \
if (!(pRet = AuDebug::gReserveHeap-> AU_WHAT exp2)) \ if (!(pRet = AuDebug::gReserveHeap-> AU_WHAT exp2)) \
{ \ { \
if (bCrunchFlag || gRuntimeConfig.debug.bIsMemoryErrorFatal) \ if (bCrunchFlag || gRuntimeConfig.debug.bIsMemoryErrorFatal) \
@ -78,15 +88,19 @@ namespace Aurora::Memory
{ \ { \
SysPanic(string); \ SysPanic(string); \
} \ } \
if (auto pLowNotification = tlsMemoryLowNotification) \
{ \
pLowNotification(length, !pRet ? 0 : 1); \
} \
} \ } \
else \ else \
{ \ { \
AddBytesToCounter(::mi_malloc_size(pRet)); \
} \
if (gLeakFinderAlloc) \ if (gLeakFinderAlloc) \
{ \ { \
gLeakFinderAlloc(pRet, ::mi_malloc_size(pRet)); \ gLeakFinderAlloc(pRet, ::mi_malloc_size(pRet)); \
} \ } \
AddBytesToCounter(::mi_malloc_size(pRet)); \
} \
return pRet; return pRet;
AUKN_SYM void *_ZAlloc(Types::size_t length) AUKN_SYM void *_ZAlloc(Types::size_t length)