[+] AuMemory::SetLeakFinder

This commit is contained in:
Reece Wilson 2023-10-17 08:45:44 +01:00
parent 97decebfe9
commit fe36ee5fb2
2 changed files with 105 additions and 8 deletions

View File

@ -20,6 +20,12 @@
namespace Aurora::Memory
{
using LeakFinderAlloc_f = void(__cdecl *)(void *, AuUInt);
using LeakFinderFree_f = void(__cdecl *)(void *);
AUKN_SYM void SetLeakFinder(LeakFinderAlloc_f pAlloc,
LeakFinderFree_f pFree);
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 *_FAlloc(Types::size_t length);

View File

@ -23,6 +23,9 @@ namespace Aurora::Memory
thread_local AuInt64 tlsLastOutOfMemory {};
static LeakFinderAlloc_f gLeakFinderAlloc;
static LeakFinderFree_f gLeakFinderFree;
static void AddBytesToCounter(AuUInt uBytes)
{
gBytesCounterPeak = AuMax(gBytesCounterPeak, AuAtomicAdd(&gBytesCounterAllocated, uBytes));
@ -33,6 +36,13 @@ namespace Aurora::Memory
AuAtomicSub(&gBytesCounterAllocated, uBytes);
}
AUKN_SYM void SetLeakFinder(LeakFinderAlloc_f pAlloc,
LeakFinderFree_f pFree)
{
gLeakFinderAlloc = pAlloc;
gLeakFinderFree = pFree;
}
AUKN_SYM AuUInt GetChunkSize(const void *head)
{
if (AuDebug::IsPointerReserveRange((void *)head))
@ -73,6 +83,10 @@ namespace Aurora::Memory
{ \
AddBytesToCounter(::mi_malloc_size(pRet)); \
} \
if (gLeakFinderAlloc) \
{ \
gLeakFinderAlloc(pRet, ::mi_malloc_size(pRet)); \
} \
return pRet;
AUKN_SYM void *_ZAlloc(Types::size_t length)
@ -110,9 +124,27 @@ namespace Aurora::Memory
}
else
{
RemoveBytesFromCounter(::mi_malloc_size(buffer));
auto oldLen = ::mi_malloc_size(buffer);
pRet = ::mi_rezalloc_aligned(buffer, length, align);
AddBytesToCounter(::mi_malloc_size(pRet));
if (pRet)
{
auto uNewSize = ::mi_malloc_size(pRet);
RemoveBytesFromCounter(oldLen);
AddBytesToCounter(uNewSize);
if (gLeakFinderAlloc)
{
if (gLeakFinderFree)
{
gLeakFinderFree(buffer);
}
gLeakFinderAlloc(pRet, uNewSize);
}
}
}
if (!pRet)
@ -137,9 +169,27 @@ namespace Aurora::Memory
}
else
{
RemoveBytesFromCounter(::mi_malloc_size(buffer));
auto oldLen = ::mi_malloc_size(buffer);
pRet = ::mi_rezalloc(buffer, length);
AddBytesToCounter(::mi_malloc_size(pRet));
if (pRet)
{
auto uNewSize = ::mi_malloc_size(pRet);
RemoveBytesFromCounter(oldLen);
AddBytesToCounter(uNewSize);
if (gLeakFinderAlloc)
{
if (gLeakFinderFree)
{
gLeakFinderFree(buffer);
}
gLeakFinderAlloc(pRet, uNewSize);
}
}
}
if (!pRet)
@ -164,9 +214,27 @@ namespace Aurora::Memory
}
else
{
RemoveBytesFromCounter(::mi_malloc_size(buffer));
auto oldLen = ::mi_malloc_size(buffer);
pRet = ::mi_realloc_aligned(buffer, length, align);
AddBytesToCounter(::mi_malloc_size(pRet));
if (pRet)
{
auto uNewSize = ::mi_malloc_size(pRet);
RemoveBytesFromCounter(oldLen);
AddBytesToCounter(uNewSize);
if (gLeakFinderAlloc)
{
if (gLeakFinderFree)
{
gLeakFinderFree(buffer);
}
gLeakFinderAlloc(pRet, uNewSize);
}
}
}
if (!pRet)
@ -191,9 +259,27 @@ namespace Aurora::Memory
}
else
{
RemoveBytesFromCounter(::mi_malloc_size(buffer));
auto oldLen = ::mi_malloc_size(buffer);
pRet = ::mi_realloc(buffer, length);
AddBytesToCounter(::mi_malloc_size(pRet));
if (pRet)
{
auto uNewSize = ::mi_malloc_size(pRet);
RemoveBytesFromCounter(oldLen);
AddBytesToCounter(uNewSize);
if (gLeakFinderAlloc)
{
if (gLeakFinderFree)
{
gLeakFinderFree(buffer);
}
gLeakFinderAlloc(pRet, uNewSize);
}
}
}
if (!pRet)
@ -223,6 +309,11 @@ namespace Aurora::Memory
RemoveBytesFromCounter(::mi_malloc_size(pHead));
::mi_free(pHead);
}
if (gLeakFinderFree)
{
gLeakFinderFree(pHead);
}
}
AUKN_SYM AuInt64 GetLastOutOfMemoryTimeNS()