2022-03-18 12:17:28 +00:00
|
|
|
#include "Memory/Memory.h"
|
|
|
|
|
|
|
|
#include "Memory/Alignment.h"
|
2023-01-11 11:24:02 +00:00
|
|
|
#include "Templates/Atomic.h"
|
|
|
|
#include "Templates/ScopeHelper.h"
|
2022-04-27 14:50:56 +00:00
|
|
|
#include "Miscellaneous/AssertionMacros.h"
|
2022-03-18 12:17:28 +00:00
|
|
|
|
|
|
|
#if PLATFORM_WINDOWS
|
|
|
|
#include <corecrt_malloc.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NAMESPACE_REDCRAFT_BEGIN
|
|
|
|
NAMESPACE_MODULE_BEGIN(Redcraft)
|
|
|
|
NAMESPACE_MODULE_BEGIN(Utility)
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(Memory)
|
|
|
|
|
2023-01-11 11:24:02 +00:00
|
|
|
#if DO_CHECK
|
|
|
|
|
|
|
|
class FMemoryLeakChecker
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
TAtomic<size_t> MemoryAllocationCount;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
FORCEINLINE constexpr FMemoryLeakChecker()
|
|
|
|
: MemoryAllocationCount(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
FORCEINLINE ~FMemoryLeakChecker()
|
|
|
|
{
|
|
|
|
checkf(MemoryAllocationCount.Load() == 0, TEXT("There is unfree memory. Please check for memory leaks."));
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE void AddMemoryAllocationCount()
|
|
|
|
{
|
|
|
|
MemoryAllocationCount.FetchAdd(1, EMemoryOrder::Relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE void ReleaseMemoryAllocationCount()
|
|
|
|
{
|
|
|
|
MemoryAllocationCount.FetchSub(1, EMemoryOrder::Relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
FMemoryLeakChecker MemoryLeakChecker;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-03-18 12:17:28 +00:00
|
|
|
void* Malloc(size_t Count, size_t Alignment)
|
|
|
|
{
|
2022-04-27 14:50:56 +00:00
|
|
|
checkf(IsValidAlignment(Alignment), TEXT("The alignment value must be an integer power of 2."));
|
|
|
|
|
2023-01-11 11:24:02 +00:00
|
|
|
Count = Count != 0 ? Count : 1; // Treat zero-byte allocation as one-byte allocation.
|
|
|
|
|
2022-03-18 12:17:28 +00:00
|
|
|
const size_t MinimumAlignment = Count >= 16 ? 16 : 8;
|
|
|
|
Alignment = MinimumAlignment > Alignment ? MinimumAlignment : Alignment;
|
|
|
|
|
|
|
|
void* Result = nullptr;
|
|
|
|
|
2023-01-11 11:24:02 +00:00
|
|
|
# if PLATFORM_WINDOWS
|
2022-03-18 12:17:28 +00:00
|
|
|
{
|
2023-01-11 11:24:02 +00:00
|
|
|
Result = _aligned_malloc(Count, Alignment);
|
2022-03-18 12:17:28 +00:00
|
|
|
}
|
2023-01-11 11:24:02 +00:00
|
|
|
# else
|
|
|
|
{
|
|
|
|
void* Ptr = SystemMalloc(Count + Alignment + sizeof(void*) + sizeof(size_t));
|
|
|
|
|
|
|
|
if (Ptr != nullptr)
|
|
|
|
{
|
|
|
|
Result = Align(reinterpret_cast<uint8*>(Ptr) + sizeof(void*) + sizeof(size_t), Alignment);
|
|
|
|
*reinterpret_cast<void**>(reinterpret_cast<uint8*>(Result) - sizeof(void*)) = Ptr;
|
|
|
|
*reinterpret_cast<size_t*>(reinterpret_cast<uint8*>(Result) - sizeof(void*) - sizeof(size_t)) = Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
check(Result != nullptr);
|
|
|
|
|
|
|
|
check_code({ MemoryLeakChecker.AddMemoryAllocationCount(); });
|
2022-03-18 12:17:28 +00:00
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* Realloc(void* Ptr, size_t Count, size_t Alignment)
|
|
|
|
{
|
2022-04-27 14:50:56 +00:00
|
|
|
checkf(IsValidAlignment(Alignment), TEXT("The alignment value must be an integer power of 2."));
|
|
|
|
|
2023-01-11 11:24:02 +00:00
|
|
|
Count = Count != 0 ? Count : 1; // Treat zero-byte allocation as one-byte allocation.
|
|
|
|
|
2022-03-18 12:17:28 +00:00
|
|
|
const size_t MinimumAlignment = Count >= 16 ? 16 : 8;
|
|
|
|
Alignment = MinimumAlignment > Alignment ? MinimumAlignment : Alignment;
|
|
|
|
|
2023-01-11 11:24:02 +00:00
|
|
|
void* Result = nullptr;
|
|
|
|
|
|
|
|
if (Ptr != nullptr)
|
2022-03-18 12:17:28 +00:00
|
|
|
{
|
2023-01-11 11:24:02 +00:00
|
|
|
# if PLATFORM_WINDOWS
|
|
|
|
{
|
|
|
|
Result = _aligned_realloc(Ptr, Count, Alignment);
|
|
|
|
}
|
|
|
|
# else
|
|
|
|
{
|
|
|
|
Result = Malloc(Count, Alignment);
|
|
|
|
|
|
|
|
if (Result != nullptr)
|
|
|
|
{
|
|
|
|
size_t PtrSize = *reinterpret_cast<size_t*>(reinterpret_cast<uint8*>(Ptr) - sizeof(void*) - sizeof(size_t));
|
|
|
|
Memcpy(Result, Ptr, Count < PtrSize ? Count : PtrSize);
|
|
|
|
Free(Ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif
|
2022-03-18 12:17:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-11 11:24:02 +00:00
|
|
|
Result = Malloc(Count, Alignment);
|
2022-03-18 12:17:28 +00:00
|
|
|
}
|
2023-01-11 11:24:02 +00:00
|
|
|
|
|
|
|
check(Result != nullptr);
|
|
|
|
|
|
|
|
return Result;
|
2022-03-18 12:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Free(void* Ptr)
|
|
|
|
{
|
2023-01-11 11:24:02 +00:00
|
|
|
# if PLATFORM_WINDOWS
|
|
|
|
{
|
|
|
|
_aligned_free(Ptr);
|
|
|
|
}
|
|
|
|
# else
|
|
|
|
{
|
|
|
|
SystemFree(*reinterpret_cast<void**>(reinterpret_cast<uint8*>(Ptr) - sizeof(void*)));
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
check_code({ if (Ptr != nullptr) MemoryLeakChecker.ReleaseMemoryAllocationCount(); });
|
2022-03-18 12:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t QuantizeSize(size_t Count, size_t Alignment)
|
|
|
|
{
|
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END(Memory)
|
|
|
|
|
|
|
|
NAMESPACE_MODULE_END(Utility)
|
|
|
|
NAMESPACE_MODULE_END(Redcraft)
|
|
|
|
NAMESPACE_REDCRAFT_END
|
|
|
|
|
|
|
|
REPLACEMENT_OPERATOR_NEW_AND_DELETE
|