2022-03-18 12:17:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "CoreTypes.h"
|
|
|
|
#include "TypeTraits/TypeTraits.h"
|
|
|
|
|
|
|
|
#include <new>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
NAMESPACE_REDCRAFT_BEGIN
|
|
|
|
NAMESPACE_MODULE_BEGIN(Redcraft)
|
|
|
|
NAMESPACE_MODULE_BEGIN(Utility)
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(Memory)
|
|
|
|
|
2022-04-10 12:14:46 +00:00
|
|
|
inline constexpr size_t DefaultAlignment = 0;
|
|
|
|
inline constexpr size_t MinimumAlignment = 8;
|
2022-03-18 12:17:28 +00:00
|
|
|
|
|
|
|
#ifdef __cpp_lib_hardware_interference_size
|
|
|
|
|
2022-11-21 15:28:19 +00:00
|
|
|
inline constexpr size_t DestructiveInterference = std::hardware_destructive_interference_size;
|
2022-04-10 12:14:46 +00:00
|
|
|
inline constexpr size_t ConstructiveInterference = std::hardware_constructive_interference_size;
|
2022-03-18 12:17:28 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2022-11-21 15:28:19 +00:00
|
|
|
inline constexpr size_t DestructiveInterference = 64;
|
2022-04-10 12:14:46 +00:00
|
|
|
inline constexpr size_t ConstructiveInterference = 64;
|
2022-03-18 12:17:28 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
FORCEINLINE void* Memmove(void* Destination, const void* Source, size_t Count)
|
|
|
|
{
|
|
|
|
return std::memmove(Destination, Source, Count);
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE int32 Memcmp(const void* BufferLHS, const void* BufferRHS, size_t Count)
|
|
|
|
{
|
|
|
|
return std::memcmp(BufferLHS, BufferRHS, Count);
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE void Memset(void* Destination, uint8 ValueToSet, size_t Count)
|
|
|
|
{
|
|
|
|
std::memset(Destination, ValueToSet, Count);
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE void* Memzero(void* Destination, size_t Count)
|
|
|
|
{
|
|
|
|
return std::memset(Destination, 0, Count);
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE void* Memcpy(void* Destination, const void* Source, size_t Count)
|
|
|
|
{
|
|
|
|
return std::memcpy(Destination, Source, Count);
|
|
|
|
}
|
|
|
|
|
2022-03-31 08:40:31 +00:00
|
|
|
template <typename T>
|
2022-03-18 12:17:28 +00:00
|
|
|
FORCEINLINE void Memset(T& Source, uint8 ValueToSet)
|
|
|
|
{
|
2022-05-15 14:56:53 +00:00
|
|
|
static_assert(!CPointer<T>, "For pointers use the three parameters function");
|
2022-03-18 12:17:28 +00:00
|
|
|
Memset(&Source, ValueToSet, sizeof(T));
|
|
|
|
}
|
|
|
|
|
2022-03-31 08:40:31 +00:00
|
|
|
template <typename T>
|
2022-03-18 12:17:28 +00:00
|
|
|
FORCEINLINE void Memzero(T& Source)
|
|
|
|
{
|
2022-05-15 14:56:53 +00:00
|
|
|
static_assert(!CPointer<T>, "For pointers use the two parameters function");
|
2022-03-18 12:17:28 +00:00
|
|
|
Memzero(&Source, sizeof(T));
|
|
|
|
}
|
|
|
|
|
2022-03-31 08:40:31 +00:00
|
|
|
template <typename T>
|
2022-03-18 12:17:28 +00:00
|
|
|
FORCEINLINE void Memcpy(T& Destination, const T& Source)
|
|
|
|
{
|
2022-05-15 14:56:53 +00:00
|
|
|
static_assert(!CPointer<T>, "For pointers use the three parameters function");
|
2022-03-18 12:17:28 +00:00
|
|
|
Memcpy(&Destination, &Source, sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE void* SystemMalloc(size_t Count)
|
|
|
|
{
|
|
|
|
return std::malloc(Count);
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE void* SystemRealloc(void* Ptr, size_t Count)
|
|
|
|
{
|
|
|
|
return std::realloc(Ptr, Count);
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE void SystemFree(void* Ptr)
|
|
|
|
{
|
|
|
|
std::free(Ptr);
|
|
|
|
}
|
|
|
|
|
2022-04-10 12:14:46 +00:00
|
|
|
REDCRAFTUTILITY_API void* Malloc(size_t Count, size_t Alignment = DefaultAlignment);
|
|
|
|
REDCRAFTUTILITY_API void* Realloc(void* Ptr, size_t Count, size_t Alignment = DefaultAlignment);
|
2022-03-18 12:17:28 +00:00
|
|
|
REDCRAFTUTILITY_API void Free(void* Ptr);
|
2022-04-10 12:14:46 +00:00
|
|
|
REDCRAFTUTILITY_API size_t QuantizeSize(size_t Count, size_t Alignment = DefaultAlignment);
|
2022-03-18 12:17:28 +00:00
|
|
|
|
|
|
|
NAMESPACE_END(Memory)
|
|
|
|
|
|
|
|
NAMESPACE_MODULE_END(Utility)
|
|
|
|
NAMESPACE_MODULE_END(Redcraft)
|
|
|
|
NAMESPACE_REDCRAFT_END
|
|
|
|
|
|
|
|
#pragma warning(disable : 28251)
|
|
|
|
|
|
|
|
// The global overload operators new/delete do not cross .dll boundaries, and the macros should be placed in the .cpp of each module.
|
|
|
|
#define REPLACEMENT_OPERATOR_NEW_AND_DELETE \
|
|
|
|
void* operator new(std::size_t Count) { return NAMESPACE_REDCRAFT::Memory::Malloc(Count); } \
|
|
|
|
void* operator new(std::size_t Count, std::align_val_t Alignment) { return NAMESPACE_REDCRAFT::Memory::Malloc(Count, static_cast<NAMESPACE_REDCRAFT::size_t>(Alignment)); } \
|
|
|
|
void operator delete(void* Ptr) noexcept { NAMESPACE_REDCRAFT::Memory::Free(Ptr); } \
|
|
|
|
void operator delete(void* Ptr, std::align_val_t Alignment) noexcept { NAMESPACE_REDCRAFT::Memory::Free(Ptr); }
|