2021-06-27 21:25:29 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: Memory.cpp
|
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2021-06-27 21:25:29 +00:00
|
|
|
#include "Memory.hpp"
|
|
|
|
#include <mimalloc.h>
|
2022-11-17 07:46:07 +00:00
|
|
|
#include <Source/HWInfo/AuHWInfo.hpp>
|
2022-03-21 05:20:19 +00:00
|
|
|
|
|
|
|
#if defined(AURORA_IS_LINUX_DERIVED)
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2022-12-07 01:29:09 +00:00
|
|
|
#include <Source/Debug/MemoryCrunch.hpp>
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
namespace Aurora::Memory
|
|
|
|
{
|
2022-12-08 19:34:15 +00:00
|
|
|
AuUInt gBytesCounterAllocated {};
|
|
|
|
AuUInt gBytesCounterPeak {};
|
|
|
|
|
2023-01-15 06:05:22 +00:00
|
|
|
thread_local AuInt64 tlsLastOutOfMemory {};
|
|
|
|
|
2022-12-08 19:34:15 +00:00
|
|
|
static void AddBytesToCounter(AuUInt uBytes)
|
|
|
|
{
|
|
|
|
gBytesCounterPeak = AuMax(gBytesCounterPeak, AuAtomicAdd(&gBytesCounterAllocated, uBytes));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RemoveBytesFromCounter(AuUInt uBytes)
|
|
|
|
{
|
|
|
|
AuAtomicSub(&gBytesCounterAllocated, uBytes);
|
|
|
|
}
|
|
|
|
|
2022-03-21 05:20:19 +00:00
|
|
|
AUKN_SYM AuUInt GetChunkSize(const void *head)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-12-07 01:29:09 +00:00
|
|
|
if (AuDebug::IsPointerReserveRange((void *)head))
|
|
|
|
{
|
|
|
|
return AuDebug::gReserveHeap->GetChunkSize(head);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return ::mi_malloc_size(head);
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 16:20:13 +00:00
|
|
|
#define CHECK_WRAP_RETURN(exp, exp2, string) \
|
|
|
|
auto pRet = exp; \
|
|
|
|
if (!pRet) \
|
|
|
|
{ \
|
2023-01-15 06:05:22 +00:00
|
|
|
tlsLastOutOfMemory = AuTime::CurrentClockNS(); \
|
|
|
|
\
|
2022-12-07 16:20:13 +00:00
|
|
|
bool bCrunchFlag {}; \
|
|
|
|
if (((bCrunchFlag = AuDebug::IsTlsMemoryCrunchActive()) || \
|
|
|
|
(gRuntimeConfig.debug.bIsApplicationClientSoftwareOnJitteryMemorySystem)) && \
|
|
|
|
AuDebug::gReserveHeap) \
|
|
|
|
{ \
|
|
|
|
if (!(pRet = AuDebug::gReserveHeap-> AU_WHAT exp2)) \
|
|
|
|
{ \
|
|
|
|
if (bCrunchFlag || gRuntimeConfig.debug.bIsMemoryErrorFatal) \
|
|
|
|
{ \
|
|
|
|
SysPanic("out of contingency memory"); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
else if (gRuntimeConfig.debug.bIsMemoryErrorFatal) \
|
|
|
|
{ \
|
|
|
|
SysPanic(string); \
|
|
|
|
} \
|
|
|
|
} \
|
2022-12-08 19:34:15 +00:00
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
AddBytesToCounter(::mi_malloc_size(pRet)); \
|
|
|
|
} \
|
2022-09-12 22:38:44 +00:00
|
|
|
return pRet;
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
AUKN_SYM void *_ZAlloc(Types::size_t length)
|
|
|
|
{
|
2023-01-15 06:05:22 +00:00
|
|
|
SysAssertDbg(length);
|
2022-12-07 01:29:09 +00:00
|
|
|
CHECK_WRAP_RETURN(::mi_zalloc(length), (ZAlloc(length)), "ZAlloc out of memory");
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void *_ZAlloc(Types::size_t length, Types::size_t align)
|
|
|
|
{
|
2023-01-15 06:05:22 +00:00
|
|
|
SysAssertDbg(length);
|
2022-12-07 01:29:09 +00:00
|
|
|
CHECK_WRAP_RETURN(::mi_zalloc_aligned(length, align), (ZAlloc(length, align)), "ZAlloc out of memory");
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void *_FAlloc(Types::size_t length)
|
|
|
|
{
|
2023-01-15 06:05:22 +00:00
|
|
|
SysAssertDbg(length);
|
2022-12-07 01:29:09 +00:00
|
|
|
CHECK_WRAP_RETURN(::mi_malloc(length), (FAlloc(length)), "FAlloc out of memory");
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void *_FAlloc(Types::size_t length, Types::size_t align)
|
|
|
|
{
|
2023-01-15 06:05:22 +00:00
|
|
|
SysAssertDbg(length);
|
2022-12-07 01:29:09 +00:00
|
|
|
CHECK_WRAP_RETURN(::mi_malloc_aligned(length, align), (FAlloc(length, align)), "FAllocEx out of memory");
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void *_ZRealloc(void *buffer, Types::size_t length, Types::size_t align)
|
|
|
|
{
|
2023-01-15 06:05:22 +00:00
|
|
|
SysAssertDbg(length);
|
2022-12-12 23:50:05 +00:00
|
|
|
void *pRet;
|
|
|
|
|
2022-12-07 01:29:09 +00:00
|
|
|
if (AuDebug::IsPointerReserveRange(buffer))
|
|
|
|
{
|
2022-12-12 23:50:05 +00:00
|
|
|
pRet = AuDebug::gReserveHeap->ZRealloc(buffer, length, align);
|
2022-12-07 01:29:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-08 19:34:15 +00:00
|
|
|
RemoveBytesFromCounter(::mi_malloc_size(buffer));
|
2022-12-12 23:50:05 +00:00
|
|
|
pRet = ::mi_rezalloc_aligned(buffer, length, align);
|
2022-12-08 19:34:15 +00:00
|
|
|
AddBytesToCounter(::mi_malloc_size(pRet));
|
2022-12-12 23:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!pRet)
|
|
|
|
{
|
|
|
|
if (gRuntimeConfig.debug.bIsMemoryErrorFatal)
|
2022-12-07 01:29:09 +00:00
|
|
|
{
|
2022-12-12 23:50:05 +00:00
|
|
|
SysPanic("ZAllocEx out of memory");
|
2022-12-07 01:29:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-12 23:50:05 +00:00
|
|
|
|
|
|
|
return pRet;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void *_ZRealloc(void *buffer, Types::size_t length)
|
|
|
|
{
|
2023-01-15 06:05:22 +00:00
|
|
|
SysAssertDbg(length);
|
2022-12-12 23:50:05 +00:00
|
|
|
void *pRet;
|
|
|
|
|
2022-12-07 01:29:09 +00:00
|
|
|
if (AuDebug::IsPointerReserveRange(buffer))
|
|
|
|
{
|
2022-12-12 23:50:05 +00:00
|
|
|
pRet = AuDebug::gReserveHeap->ZRealloc(buffer, length);
|
2022-12-07 01:29:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-08 19:34:15 +00:00
|
|
|
RemoveBytesFromCounter(::mi_malloc_size(buffer));
|
2022-12-12 23:50:05 +00:00
|
|
|
pRet = ::mi_rezalloc(buffer, length);
|
2022-12-08 19:34:15 +00:00
|
|
|
AddBytesToCounter(::mi_malloc_size(pRet));
|
2022-12-12 23:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!pRet)
|
|
|
|
{
|
|
|
|
if (gRuntimeConfig.debug.bIsMemoryErrorFatal)
|
2022-12-07 01:29:09 +00:00
|
|
|
{
|
2022-12-12 23:50:05 +00:00
|
|
|
SysPanic("ZAlloc out of memory");
|
2022-12-07 01:29:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-12 23:50:05 +00:00
|
|
|
|
|
|
|
return pRet;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void *_FRealloc(void *buffer, Types::size_t length, Types::size_t align)
|
|
|
|
{
|
2023-01-15 06:05:22 +00:00
|
|
|
SysAssertDbg(length);
|
2022-12-12 23:50:05 +00:00
|
|
|
void *pRet;
|
|
|
|
|
2022-12-07 01:29:09 +00:00
|
|
|
if (AuDebug::IsPointerReserveRange(buffer))
|
|
|
|
{
|
2022-12-12 23:50:05 +00:00
|
|
|
pRet = AuDebug::gReserveHeap->FRealloc(buffer, length, align);
|
2022-12-07 01:29:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-08 19:34:15 +00:00
|
|
|
RemoveBytesFromCounter(::mi_malloc_size(buffer));
|
2022-12-12 23:50:05 +00:00
|
|
|
pRet = ::mi_realloc_aligned(buffer, length, align);
|
2022-12-08 19:34:15 +00:00
|
|
|
AddBytesToCounter(::mi_malloc_size(pRet));
|
2022-12-12 23:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!pRet)
|
|
|
|
{
|
|
|
|
if (gRuntimeConfig.debug.bIsMemoryErrorFatal)
|
2022-12-07 01:29:09 +00:00
|
|
|
{
|
2022-12-12 23:50:05 +00:00
|
|
|
SysPanic("FReallocEx out of memory");
|
2022-12-07 01:29:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-12 23:50:05 +00:00
|
|
|
|
|
|
|
return pRet;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void *_FRealloc(void *buffer, Types::size_t length)
|
|
|
|
{
|
2023-01-15 06:05:22 +00:00
|
|
|
SysAssertDbg(length);
|
2022-12-12 23:50:05 +00:00
|
|
|
void *pRet;
|
|
|
|
|
2022-12-07 01:29:09 +00:00
|
|
|
if (AuDebug::IsPointerReserveRange(buffer))
|
|
|
|
{
|
2022-12-12 23:50:05 +00:00
|
|
|
pRet = AuDebug::gReserveHeap->FRealloc(buffer, length);
|
2022-12-07 01:29:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-08 19:34:15 +00:00
|
|
|
RemoveBytesFromCounter(::mi_malloc_size(buffer));
|
2022-12-12 23:50:05 +00:00
|
|
|
pRet = ::mi_realloc(buffer, length);
|
2022-12-08 19:34:15 +00:00
|
|
|
AddBytesToCounter(::mi_malloc_size(pRet));
|
2022-12-12 23:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!pRet)
|
|
|
|
{
|
|
|
|
if (gRuntimeConfig.debug.bIsMemoryErrorFatal)
|
2022-12-07 01:29:09 +00:00
|
|
|
{
|
2022-12-12 23:50:05 +00:00
|
|
|
SysPanic("FRealloc out of memory");
|
2022-12-07 01:29:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-12 23:50:05 +00:00
|
|
|
|
|
|
|
return pRet;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 19:34:15 +00:00
|
|
|
AUKN_SYM void _Free(void *pHead)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-12-12 23:50:05 +00:00
|
|
|
if (!pHead)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-08 19:34:15 +00:00
|
|
|
if (AuDebug::IsPointerReserveRange(pHead))
|
2022-12-07 01:29:09 +00:00
|
|
|
{
|
2022-12-08 19:34:15 +00:00
|
|
|
AuDebug::gReserveHeap->Free(pHead);
|
2022-12-07 01:29:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-08 19:34:15 +00:00
|
|
|
RemoveBytesFromCounter(::mi_malloc_size(pHead));
|
|
|
|
::mi_free(pHead);
|
2022-12-07 01:29:09 +00:00
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2022-03-21 05:20:19 +00:00
|
|
|
|
2023-01-15 06:05:22 +00:00
|
|
|
AUKN_SYM AuInt64 GetLastOutOfMemoryTimeNS()
|
|
|
|
{
|
|
|
|
return tlsLastOutOfMemory;
|
|
|
|
}
|
|
|
|
|
2022-03-21 05:20:19 +00:00
|
|
|
AUKN_SYM AuUInt GetPageSize()
|
|
|
|
{
|
|
|
|
return AuHwInfo::GetPageSize();
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|