2022-12-29 13:55:02 +00:00
|
|
|
|
#pragma once
|
2022-03-18 12:17:28 +00:00
|
|
|
|
|
|
|
|
|
#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-12-29 13:55:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Default allocator alignment.
|
|
|
|
|
* Blocks >= 16 bytes will be 16-byte-aligned, Blocks < 16 will be 8-byte aligned. If the allocator does
|
|
|
|
|
* not support allocation alignment, the alignment will be ignored.
|
|
|
|
|
*/
|
2022-04-10 12:14:46 +00:00
|
|
|
|
inline constexpr size_t DefaultAlignment = 0;
|
2022-12-29 13:55:02 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Minimum allocator alignment.
|
|
|
|
|
*/
|
2022-04-10 12:14:46 +00:00
|
|
|
|
inline constexpr size_t MinimumAlignment = 8;
|
2022-03-18 12:17:28 +00:00
|
|
|
|
|
|
|
|
|
#ifdef __cpp_lib_hardware_interference_size
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Minimum offset between two objects to avoid false sharing.
|
|
|
|
|
*
|
|
|
|
|
* struct FTwoCacheLiner { // occupies two cache lines
|
|
|
|
|
* alignas(DestructiveInterference) TAtomic<uint64> X;
|
|
|
|
|
* alignas(DestructiveInterference) TAtomic<uint64> Y;
|
|
|
|
|
* };
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
inline constexpr size_t DestructiveInterference = std::hardware_destructive_interference_size;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maximum size of contiguous memory to promote true sharing.
|
|
|
|
|
*
|
|
|
|
|
* struct alignas(ConstructiveInterference) FOneCacheLiner { // occupies one cache line
|
|
|
|
|
* TAtomic<uint64> X;
|
|
|
|
|
* TAtomic<uint64> Y;
|
|
|
|
|
* };
|
|
|
|
|
*
|
|
|
|
|
*/
|
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-12-29 13:55:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Minimum offset between two objects to avoid false sharing.
|
|
|
|
|
*
|
|
|
|
|
* struct FTwoCacheLiner { // occupies two cache lines
|
|
|
|
|
* alignas(DestructiveInterference) TAtomic<uint64> X;
|
|
|
|
|
* alignas(DestructiveInterference) TAtomic<uint64> Y;
|
|
|
|
|
* };
|
|
|
|
|
*
|
|
|
|
|
*/
|
2022-11-21 15:28:19 +00:00
|
|
|
|
inline constexpr size_t DestructiveInterference = 64;
|
2022-12-29 13:55:02 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maximum size of contiguous memory to promote true sharing.
|
|
|
|
|
*
|
|
|
|
|
* struct alignas(ConstructiveInterference) FOneCacheLiner { // occupies one cache line
|
|
|
|
|
* TAtomic<uint64> X;
|
|
|
|
|
* TAtomic<uint64> Y;
|
|
|
|
|
* };
|
|
|
|
|
*
|
|
|
|
|
*/
|
2022-04-10 12:14:46 +00:00
|
|
|
|
inline constexpr size_t ConstructiveInterference = 64;
|
2022-03-18 12:17:28 +00:00
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Copies 'Count' bytes from the buffer pointed to by 'Source' to the buffer pointed to by 'Destination'.
|
|
|
|
|
* The buffers may overlap, copying takes place as if the characters were copied to a temporary character
|
|
|
|
|
* array and then the characters were copied from the array to 'Destination'.
|
|
|
|
|
*
|
|
|
|
|
* @param Destination - The pointer to the memory location to copy to.
|
|
|
|
|
* @param Source - The pointer to the memory location to copy from.
|
|
|
|
|
* @param Count - The number of bytes to copy.
|
|
|
|
|
*
|
|
|
|
|
* @return Destination
|
|
|
|
|
*/
|
2022-03-18 12:17:28 +00:00
|
|
|
|
FORCEINLINE void* Memmove(void* Destination, const void* Source, size_t Count)
|
|
|
|
|
{
|
|
|
|
|
return std::memmove(Destination, Source, Count);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Reinterprets the buffers pointed to by 'BufferLHS' and 'BufferRHS' as arrays of unsigned char and
|
|
|
|
|
* compares the first 'Count' characters of these arrays. The comparison is done lexicographically.
|
|
|
|
|
* The sign of the result is the sign of the difference between the values of the first pair of bytes
|
|
|
|
|
* (both interpreted as unsigned char) that differ in the objects being compared.
|
|
|
|
|
*
|
|
|
|
|
* @param BufferLHS - The pointers to the memory buffers to compare.
|
|
|
|
|
* @param BufferRHS - The pointers to the memory buffers to compare.
|
|
|
|
|
* @param Count - The number of bytes to examine.
|
|
|
|
|
*
|
|
|
|
|
* @return Negative value if the first differing byte in 'BufferLHS' is less than the corresponding byte in 'BufferRHS'.
|
|
|
|
|
* 0 if all 'Count' bytes of 'BufferLHS' and 'BufferRHS' are equal.
|
|
|
|
|
* Positive value if the first differing byte in 'BufferLHS' is greater than the corresponding byte in 'BufferRHS'.
|
|
|
|
|
*/
|
2022-03-18 12:17:28 +00:00
|
|
|
|
FORCEINLINE int32 Memcmp(const void* BufferLHS, const void* BufferRHS, size_t Count)
|
|
|
|
|
{
|
|
|
|
|
return std::memcmp(BufferLHS, BufferRHS, Count);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Copies 'ValueToSet' into each of the first 'Count' characters of the buffer pointed to by 'Destination'.
|
|
|
|
|
*
|
|
|
|
|
* @param Destination - The pointer to the buffer to fill.
|
|
|
|
|
* @param ValueToSet - The fill byte.
|
|
|
|
|
* @param Count - The number of bytes to fill.
|
|
|
|
|
*
|
|
|
|
|
* @return Destination
|
|
|
|
|
*/
|
|
|
|
|
FORCEINLINE void* Memset(void* Destination, uint8 ValueToSet, size_t Count)
|
2022-03-18 12:17:28 +00:00
|
|
|
|
{
|
2022-12-29 13:55:02 +00:00
|
|
|
|
return std::memset(Destination, ValueToSet, Count);
|
2022-03-18 12:17:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Copies 0 into each of the first 'Count' characters of the buffer pointed to by 'Destination'.
|
|
|
|
|
*
|
|
|
|
|
* @param Destination - The pointer to the buffer to fill.
|
|
|
|
|
* @param Count - The number of bytes to fill.
|
|
|
|
|
*
|
|
|
|
|
* @return Destination
|
|
|
|
|
*/
|
2022-03-18 12:17:28 +00:00
|
|
|
|
FORCEINLINE void* Memzero(void* Destination, size_t Count)
|
|
|
|
|
{
|
|
|
|
|
return std::memset(Destination, 0, Count);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Copies 'Count' bytes from the buffer pointed to by 'Source' to the buffer pointed to by 'Destination'.
|
|
|
|
|
* If the buffers overlap, the behavior is undefined.
|
|
|
|
|
*
|
|
|
|
|
* @param Destination - The pointer to the memory location to copy to.
|
|
|
|
|
* @param Source - The pointer to the memory location to copy from.
|
|
|
|
|
* @param Count - The number of bytes to copy.
|
|
|
|
|
*
|
|
|
|
|
* @return Destination
|
|
|
|
|
*/
|
2022-03-18 12:17:28 +00:00
|
|
|
|
FORCEINLINE void* Memcpy(void* Destination, const void* Source, size_t Count)
|
|
|
|
|
{
|
|
|
|
|
return std::memcpy(Destination, Source, Count);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Copies the object referenced to by 'Source' to the object referenced to by 'Destination'.
|
|
|
|
|
* The objects may overlap, copying takes place as if the characters were copied to a temporary character
|
|
|
|
|
* array and then the characters were copied from the array to 'Destination'.
|
|
|
|
|
*
|
|
|
|
|
* @param Destination - The reference to the object to copy to.
|
|
|
|
|
* @param Source - The reference to the object to copy from.
|
|
|
|
|
*/
|
|
|
|
|
template <typename T> requires (!CPointer<T>)
|
2022-12-15 15:37:41 +00:00
|
|
|
|
FORCEINLINE void Memmove(T& Destination, const T& Source)
|
|
|
|
|
{
|
|
|
|
|
Memmove(&Destination, &Source, sizeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Reinterprets the objects referenced to by 'BufferLHS' and 'BufferRHS' as arrays of unsigned char and
|
|
|
|
|
* compares the all characters of these arrays. The comparison is done lexicographically.
|
|
|
|
|
* The sign of the result is the sign of the difference between the values of the first pair of bytes
|
|
|
|
|
* (both interpreted as unsigned char) that differ in the objects being compared.
|
|
|
|
|
*
|
|
|
|
|
* @param BufferLHS - The reference to the object to compare.
|
|
|
|
|
* @param BufferRHS - The reference to the object to compare.
|
|
|
|
|
*
|
|
|
|
|
* @return Negative value if the first differing byte in 'BufferLHS' is less than the corresponding byte in 'BufferRHS'.
|
|
|
|
|
* 0 if all bytes of 'BufferLHS' and 'BufferRHS' are equal.
|
|
|
|
|
* Positive value if the first differing byte in 'BufferLHS' is greater than the corresponding byte in 'BufferRHS'.
|
|
|
|
|
*/
|
|
|
|
|
template <typename T> requires (!CPointer<T>)
|
2022-12-15 15:37:41 +00:00
|
|
|
|
FORCEINLINE int32 Memcmp(const T& BufferLHS, const T& BufferRHS)
|
|
|
|
|
{
|
|
|
|
|
return Memcmp(&BufferLHS, &BufferRHS, sizeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Copies 'ValueToSet' into each of the all characters of the object referenced to by 'Destination'.
|
|
|
|
|
*
|
|
|
|
|
* @param Destination - The reference to the object to fill.
|
|
|
|
|
* @param ValueToSet - The fill byte.
|
|
|
|
|
*/
|
|
|
|
|
template <typename T> requires (!CPointer<T>)
|
|
|
|
|
FORCEINLINE void Memset(T& Destination, uint8 ValueToSet)
|
2022-03-18 12:17:28 +00:00
|
|
|
|
{
|
2022-12-29 13:55:02 +00:00
|
|
|
|
Memset(&Destination, ValueToSet, sizeof(T));
|
2022-03-18 12:17:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Copies 0 into each of the all characters of the object referenced to by 'Destination'.
|
|
|
|
|
*
|
|
|
|
|
* @param Destination - The reference to the object to fill.
|
|
|
|
|
* @param ValueToSet - The fill byte.
|
|
|
|
|
*/
|
|
|
|
|
template <typename T> requires (!CPointer<T>)
|
|
|
|
|
FORCEINLINE void Memzero(T& Destination)
|
2022-03-18 12:17:28 +00:00
|
|
|
|
{
|
2022-12-29 13:55:02 +00:00
|
|
|
|
Memzero(&Destination, sizeof(T));
|
2022-03-18 12:17:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Copies the object referenced to by 'Source' to the object referenced to by 'Destination'.
|
|
|
|
|
* If the objects overlap, the behavior is undefined.
|
|
|
|
|
*
|
|
|
|
|
* @param Destination - The reference to the object to copy to.
|
|
|
|
|
* @param Source - The reference to the object to copy from.
|
|
|
|
|
*/
|
|
|
|
|
template <typename T> requires (!CPointer<T>)
|
2022-03-18 12:17:28 +00:00
|
|
|
|
FORCEINLINE void Memcpy(T& Destination, const T& Source)
|
|
|
|
|
{
|
|
|
|
|
Memcpy(&Destination, &Source, sizeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** Fallback to std::malloc(). */
|
|
|
|
|
NODISCARD FORCEINLINE void* SystemMalloc(size_t Count)
|
2022-03-18 12:17:28 +00:00
|
|
|
|
{
|
|
|
|
|
return std::malloc(Count);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** Fallback to std::realloc(). */
|
|
|
|
|
NODISCARD FORCEINLINE void* SystemRealloc(void* Ptr, size_t Count)
|
2022-03-18 12:17:28 +00:00
|
|
|
|
{
|
|
|
|
|
return std::realloc(Ptr, Count);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/** Fallback to std::free(). */
|
2022-03-18 12:17:28 +00:00
|
|
|
|
FORCEINLINE void SystemFree(void* Ptr)
|
|
|
|
|
{
|
|
|
|
|
std::free(Ptr);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 13:55:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* Allocates 'Count' bytes of uninitialized storage with 'Alignment'.
|
|
|
|
|
*
|
|
|
|
|
* @param Count - The number of bytes to allocate.
|
|
|
|
|
* @param Alignment - The alignment value, must be a power of 2.
|
|
|
|
|
*
|
|
|
|
|
* @return The non-null pointer to the beginning of newly allocated memory. To avoid a memory leak,
|
|
|
|
|
* the returned pointer must be deallocated with Free() or Realloc().
|
|
|
|
|
*
|
|
|
|
|
* @see DefaultAlignment
|
|
|
|
|
*/
|
|
|
|
|
NODISCARD REDCRAFTUTILITY_API void* Malloc(size_t Count, size_t Alignment = DefaultAlignment);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reallocates the given area of memory. It must be previously allocated by Malloc() or Realloc().
|
2023-01-11 11:24:02 +00:00
|
|
|
|
* If 'Ptr' is a nullptr, effectively the same as calling Malloc().
|
2022-12-29 13:55:02 +00:00
|
|
|
|
*
|
|
|
|
|
* @param Ptr - The pointer to the memory area to be reallocated.
|
|
|
|
|
* @param Count - The number of bytes to allocate.
|
|
|
|
|
* @param Alignment - The alignment value, must be a power of 2.
|
|
|
|
|
*
|
|
|
|
|
* @return The non-null pointer to the beginning of newly allocated memory. To avoid a memory leak,
|
|
|
|
|
* the returned pointer must be deallocated with Free() or Realloc().
|
|
|
|
|
*
|
|
|
|
|
* @see DefaultAlignment
|
|
|
|
|
*/
|
|
|
|
|
NODISCARD REDCRAFTUTILITY_API void* Realloc(void* Ptr, size_t Count, size_t Alignment = DefaultAlignment);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Deallocates the space previously allocated by Malloc() or Realloc().
|
|
|
|
|
* If 'Ptr' is a nullptr, the function does nothing.
|
|
|
|
|
*
|
|
|
|
|
* @param Ptr - The pointer to the memory to deallocate.
|
|
|
|
|
*/
|
2022-03-18 12:17:28 +00:00
|
|
|
|
REDCRAFTUTILITY_API void Free(void* Ptr);
|
2022-12-29 13:55:02 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* For some allocators this will return the actual size that should be requested to eliminate
|
|
|
|
|
* internal fragmentation. The return value will always be >= 'Count'. This can be used to grow
|
|
|
|
|
* and shrink containers to optimal sizes.
|
|
|
|
|
* This call is always fast and threadsafe with no locking.
|
|
|
|
|
*
|
|
|
|
|
* @param Count - The number of bytes to allocate.
|
|
|
|
|
* @param Alignment - The alignment value, must be a power of 2.
|
|
|
|
|
*
|
|
|
|
|
* @return The optimized new size.
|
|
|
|
|
*/
|
|
|
|
|
NODISCARD 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.
|
2022-12-29 13:55:02 +00:00
|
|
|
|
#define REPLACEMENT_OPERATOR_NEW_AND_DELETE \
|
2023-01-15 13:57:16 +00:00
|
|
|
|
NODISCARD void* operator new(std::size_t Count) { return NAMESPACE_REDCRAFT::Memory::Malloc(Count, __STDCPP_DEFAULT_NEW_ALIGNMENT__); } \
|
2022-12-29 13:55:02 +00:00
|
|
|
|
NODISCARD 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); }
|