AuroraRuntime/Source/Memory/Memory.cpp
Reece Wilson 267c2216b0 [+] UDP over socket API via existing INetSrvDatagram layer
(...missing send)
[+] AuIO::Buffer::ViewReader
[+] AuIO::Buffer::ViewSeekableReadable
[+] AuIO::Buffer::ViewWriter
[*] Clean up AuCompression
[*[ AuLog messages must always crunch for memory
[*] Various bug fixes
[*] Refactor+clean up
2022-12-12 23:50:05 +00:00

220 lines
7.2 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Memory.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "Memory.hpp"
#include <mimalloc.h>
#include <Source/HWInfo/AuHWInfo.hpp>
#if defined(AURORA_IS_LINUX_DERIVED)
#include <sys/mman.h>
#endif
#include <Source/Debug/MemoryCrunch.hpp>
namespace Aurora::Memory
{
AuUInt gBytesCounterAllocated {};
AuUInt gBytesCounterPeak {};
static void AddBytesToCounter(AuUInt uBytes)
{
gBytesCounterPeak = AuMax(gBytesCounterPeak, AuAtomicAdd(&gBytesCounterAllocated, uBytes));
}
static void RemoveBytesFromCounter(AuUInt uBytes)
{
AuAtomicSub(&gBytesCounterAllocated, uBytes);
}
AUKN_SYM AuUInt GetChunkSize(const void *head)
{
if (AuDebug::IsPointerReserveRange((void *)head))
{
return AuDebug::gReserveHeap->GetChunkSize(head);
}
else
{
return ::mi_malloc_size(head);
}
}
#define CHECK_WRAP_RETURN(exp, exp2, string) \
auto pRet = exp; \
if (!pRet) \
{ \
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); \
} \
} \
else \
{ \
AddBytesToCounter(::mi_malloc_size(pRet)); \
} \
return pRet;
AUKN_SYM void *_ZAlloc(Types::size_t length)
{
CHECK_WRAP_RETURN(::mi_zalloc(length), (ZAlloc(length)), "ZAlloc out of memory");
}
AUKN_SYM void *_ZAlloc(Types::size_t length, Types::size_t align)
{
CHECK_WRAP_RETURN(::mi_zalloc_aligned(length, align), (ZAlloc(length, align)), "ZAlloc out of memory");
}
AUKN_SYM void *_FAlloc(Types::size_t length)
{
CHECK_WRAP_RETURN(::mi_malloc(length), (FAlloc(length)), "FAlloc out of memory");
}
AUKN_SYM void *_FAlloc(Types::size_t length, Types::size_t align)
{
CHECK_WRAP_RETURN(::mi_malloc_aligned(length, align), (FAlloc(length, align)), "FAllocEx out of memory");
}
AUKN_SYM void *_ZRealloc(void *buffer, Types::size_t length, Types::size_t align)
{
void *pRet;
if (AuDebug::IsPointerReserveRange(buffer))
{
pRet = AuDebug::gReserveHeap->ZRealloc(buffer, length, align);
}
else
{
RemoveBytesFromCounter(::mi_malloc_size(buffer));
pRet = ::mi_rezalloc_aligned(buffer, length, align);
AddBytesToCounter(::mi_malloc_size(pRet));
}
if (!pRet)
{
if (gRuntimeConfig.debug.bIsMemoryErrorFatal)
{
SysPanic("ZAllocEx out of memory");
}
}
return pRet;
}
AUKN_SYM void *_ZRealloc(void *buffer, Types::size_t length)
{
void *pRet;
if (AuDebug::IsPointerReserveRange(buffer))
{
pRet = AuDebug::gReserveHeap->ZRealloc(buffer, length);
}
else
{
RemoveBytesFromCounter(::mi_malloc_size(buffer));
pRet = ::mi_rezalloc(buffer, length);
AddBytesToCounter(::mi_malloc_size(pRet));
}
if (!pRet)
{
if (gRuntimeConfig.debug.bIsMemoryErrorFatal)
{
SysPanic("ZAlloc out of memory");
}
}
return pRet;
}
AUKN_SYM void *_FRealloc(void *buffer, Types::size_t length, Types::size_t align)
{
void *pRet;
if (AuDebug::IsPointerReserveRange(buffer))
{
pRet = AuDebug::gReserveHeap->FRealloc(buffer, length, align);
}
else
{
RemoveBytesFromCounter(::mi_malloc_size(buffer));
pRet = ::mi_realloc_aligned(buffer, length, align);
AddBytesToCounter(::mi_malloc_size(pRet));
}
if (!pRet)
{
if (gRuntimeConfig.debug.bIsMemoryErrorFatal)
{
SysPanic("FReallocEx out of memory");
}
}
return pRet;
}
AUKN_SYM void *_FRealloc(void *buffer, Types::size_t length)
{
void *pRet;
if (AuDebug::IsPointerReserveRange(buffer))
{
pRet = AuDebug::gReserveHeap->FRealloc(buffer, length);
}
else
{
RemoveBytesFromCounter(::mi_malloc_size(buffer));
pRet = ::mi_realloc(buffer, length);
AddBytesToCounter(::mi_malloc_size(pRet));
}
if (!pRet)
{
if (gRuntimeConfig.debug.bIsMemoryErrorFatal)
{
SysPanic("FRealloc out of memory");
}
}
return pRet;
}
AUKN_SYM void _Free(void *pHead)
{
if (!pHead)
{
return;
}
if (AuDebug::IsPointerReserveRange(pHead))
{
AuDebug::gReserveHeap->Free(pHead);
}
else
{
RemoveBytesFromCounter(::mi_malloc_size(pHead));
::mi_free(pHead);
}
}
AUKN_SYM AuUInt GetPageSize()
{
return AuHwInfo::GetPageSize();
}
}