refactor(memory): use static_assert's assertion limit as a constraint

This commit is contained in:
_Redstone_c_ 2022-04-10 20:14:46 +08:00
parent c6dd562446
commit 98e8e87c7a
2 changed files with 13 additions and 21 deletions

View File

@ -9,35 +9,27 @@ NAMESPACE_MODULE_BEGIN(Utility)
NAMESPACE_BEGIN(Memory)
template <typename T>
template <typename T> requires TIsIntegral<T>::Value || TIsPointer<T>::Value
FORCEINLINE constexpr T Align(T InValue, size_t Alignment)
{
static_assert(TIsIntegral<T>::Value || TIsPointer<T>::Value, "Align expects an integer or pointer type");
return (T)(((uintptr)(InValue) + static_cast<uintptr>(Alignment) - 1) & ~(static_cast<uintptr>(Alignment) - 1));
}
template <typename T>
template <typename T> requires TIsIntegral<T>::Value || TIsPointer<T>::Value
FORCEINLINE constexpr T AlignDown(T InValue, size_t Alignment)
{
static_assert(TIsIntegral<T>::Value || TIsPointer<T>::Value, "AlignDown expects an integer or pointer type");
return (T)((uintptr)(InValue) & ~(static_cast<uintptr>(Alignment) - 1));
}
template <typename T>
template <typename T> requires TIsIntegral<T>::Value || TIsPointer<T>::Value
FORCEINLINE constexpr T AlignArbitrary(T InValue, size_t Alignment)
{
static_assert(TIsIntegral<T>::Value || TIsPointer<T>::Value, "AlignArbitrary expects an integer or pointer type");
return (T)((((uintptr)(InValue) + static_cast<uintptr>(Alignment) - 1) / static_cast<uintptr>(Alignment)) * static_cast<uintptr>(Alignment));
}
template <typename T>
template <typename T> requires TIsIntegral<T>::Value || TIsPointer<T>::Value
FORCEINLINE constexpr bool IsAligned(T InValue, size_t Alignment)
{
static_assert(TIsIntegral<T>::Value || TIsPointer<T>::Value, "IsAligned expects an integer or pointer type");
return !((uintptr)(InValue) & (static_cast<uintptr>(Alignment) - 1));
}

View File

@ -13,18 +13,18 @@ NAMESPACE_MODULE_BEGIN(Utility)
NAMESPACE_BEGIN(Memory)
inline constexpr size_t DEFAULT_ALIGNMENT = 0;
inline constexpr size_t MINIMUM_ALIGNMENT = 8;
inline constexpr size_t DefaultAlignment = 0;
inline constexpr size_t MinimumAlignment = 8;
#ifdef __cpp_lib_hardware_interference_size
inline constexpr size_t DESTRUCTIVE_INTERFERENCE = std::hardware_destructive_interference_size;
inline constexpr size_t CONSTRUCTIVE_INTERFERENCE = std::hardware_constructive_interference_size;
inline constexpr size_t DestructiveInterference = std::hardware_destructive_interference_size;
inline constexpr size_t ConstructiveInterference = std::hardware_constructive_interference_size;
#else
inline constexpr size_t DESTRUCTIVE_INTERFERENCE = 64;
inline constexpr size_t CONSTRUCTIVE_INTERFERENCE = 64;
inline constexpr size_t DestructiveInterference = 64;
inline constexpr size_t ConstructiveInterference = 64;
#endif
@ -89,10 +89,10 @@ FORCEINLINE void SystemFree(void* Ptr)
std::free(Ptr);
}
REDCRAFTUTILITY_API void* Malloc(size_t Count, size_t Alignment = DEFAULT_ALIGNMENT);
REDCRAFTUTILITY_API void* Realloc(void* Ptr, size_t Count, size_t Alignment = DEFAULT_ALIGNMENT);
REDCRAFTUTILITY_API void* Malloc(size_t Count, size_t Alignment = DefaultAlignment);
REDCRAFTUTILITY_API void* Realloc(void* Ptr, size_t Count, size_t Alignment = DefaultAlignment);
REDCRAFTUTILITY_API void Free(void* Ptr);
REDCRAFTUTILITY_API size_t QuantizeSize(size_t Count, size_t Alignment = DEFAULT_ALIGNMENT);
REDCRAFTUTILITY_API size_t QuantizeSize(size_t Count, size_t Alignment = DefaultAlignment);
NAMESPACE_END(Memory)