实现基本内存操作 Linux 平台测试

This commit is contained in:
2021-10-05 18:33:18 +08:00
parent 7361a673e4
commit f53211ef50
11 changed files with 330 additions and 26 deletions

View File

@ -0,0 +1,43 @@
#pragma once
#include "CoreTypes.h"
NS_REDCRAFT_BEGIN
NS_BEGIN(Memory)
constexpr uint32 DEFAULT_ALIGNMENT = 0;
constexpr uint32 MIN_ALIGNMENT = 8;
FORCEINLINE void* Memmove(void* Dest, const void* Src, size_t Count);
FORCEINLINE int32 Memcmp(const void* Buf1, const void* Buf2, size_t Count);
FORCEINLINE void Memset(void* Dest, uint8 ValueToSet, size_t Count);
FORCEINLINE void* Memzero(void* Dest, size_t Count);
FORCEINLINE void* Memcpy(void* Dest, const void* Src, size_t Count);
template<typename T>
static FORCEINLINE void Memset(T& Src, uint8 ValueToSet);
template<typename T>
static FORCEINLINE void Memzero(T& Src);
template<typename T>
static FORCEINLINE void Memcpy(T& Dest, const T& Src);
FORCEINLINE void* SystemMalloc(size_t Count);
FORCEINLINE void SystemFree(void* Ptr);
REDCRAFTCORE_API void* Malloc(size_t Count, uint32 Alignment = DEFAULT_ALIGNMENT);
REDCRAFTCORE_API void* Realloc(void* Ptr, size_t Count, uint32 Alignment = DEFAULT_ALIGNMENT);
REDCRAFTCORE_API void Free(void* Ptr);
REDCRAFTCORE_API size_t QuantizeSize(size_t Count, uint32 Alignment = DEFAULT_ALIGNMENT);
NS_END(Memory)
NS_REDCRAFT_END
#include "HAL/Memory.inl"
void* operator new(std::size_t Count) { return NS_REDCRAFT::Memory::Malloc(Count); };
void* operator new(std::size_t Count, std::align_val_t Alignment) { return NS_REDCRAFT::Memory::Malloc(Count, (NS_REDCRAFT::uint32)Alignment); };
void operator delete(void* Ptr) noexcept { NS_REDCRAFT::Memory::Free(Ptr); }
void operator delete(void* Ptr, std::align_val_t Alignment) noexcept { NS_REDCRAFT::Memory::Free(Ptr); }

View File

@ -0,0 +1,67 @@
#include "Math/MathUtility.h"
#include "Templates/TypeTraits.h"
#include <cstring>
#include <cstdlib>
NS_REDCRAFT_BEGIN
NS_BEGIN(Memory)
void* Memmove(void* Dest, const void* Src, size_t Count)
{
return std::memmove(Dest, Src, Count);
}
int32 Memcmp(const void* Buf1, const void* Buf2, size_t Count)
{
return std::memcmp(Buf1, Buf2, Count);
}
void Memset(void* Dest, uint8 ValueToSet, size_t Count)
{
std::memset(Dest, ValueToSet, Count);
}
void* Memzero(void* Dest, size_t Count)
{
return std::memset(Dest, 0, Count);
}
void* Memcpy(void* Dest, const void* Src, size_t Count)
{
return std::memcpy(Dest, Src, Count);
}
template<typename T>
static FORCEINLINE void Memset(T& Src, uint8 ValueToSet)
{
static_assert(!TypeTraits::TIsPointer<T>::Value, "For pointers use the three parameters function");
Memset(&Src, ValueToSet, sizeof(T));
}
template<typename T>
static FORCEINLINE void Memzero(T& Src)
{
static_assert(!TypeTraits::TIsPointer<T>::Value, "For pointers use the two parameters function");
Memzero(&Src, sizeof(T));
}
template<typename T>
static FORCEINLINE void Memcpy(T& Dest, const T& Src)
{
static_assert(!TypeTraits::TIsPointer<T>::Value, "For pointers use the three parameters function");
Memcpy(&Dest, &Src, sizeof(T));
}
void* SystemMalloc(size_t Count)
{
return std::malloc(Count);
}
void SystemFree(void* Ptr)
{
std::free(Ptr);
}
NS_END(Memory)
NS_REDCRAFT_END

View File

@ -2,6 +2,7 @@
#include "Misc/CoreDefines.h"
#include <new>
#include <cstdint>
#include <cstdlib>
#include <cstddef>
@ -46,20 +47,36 @@ NS_REDCRAFT_BEGIN
// Alignment.
#if defined(__clang__)
#if PLATFORM_WINDOWS
#define GCC_PACK(n) __attribute__((packed,aligned(n)))
#define GCC_ALIGN(n) __attribute__((aligned(n)))
#if defined(_MSC_VER)
#define MS_ALIGN(n) __declspec(align(n))
#if defined(__clang__)
#define GCC_PACK(n) __attribute__((packed,aligned(n)))
#define GCC_ALIGN(n) __attribute__((aligned(n)))
#if defined(_MSC_VER)
#define MS_ALIGN(n) __declspec(align(n))
#endif
#else
#define GCC_PACK(n)
#define GCC_ALIGN(n)
#define MS_ALIGN(n) __declspec(align(n))
#endif
#elif PLATFORM_LINUX
#define GCC_PACK(n) __attribute__((packed,aligned(n)))
#define GCC_ALIGN(n) __attribute__((aligned(n)))
#define MS_ALIGN(n)
#else
#define GCC_PACK(n)
#define GCC_ALIGN(n)
#define MS_ALIGN(n) __declspec(align(n))
#define MS_ALIGN(n)
#endif
@ -114,10 +131,6 @@ typedef intptr_t ssize_t;
typedef decltype(NULL) null_t;
typedef std::nullptr_t nullptr_t;
// Alignment.
typedef std::max_align_t max_align_t;
#if PLATFORM_LINUX
#define PLATFORM_TCHAR_IS_CHAR16 1
#else