refactor(*): fix EBO issues and remove NAMESPACE_PRIVATE about containers

This commit is contained in:
2023-03-04 19:12:47 +08:00
parent 5c91059203
commit 9045f9b3b2
5 changed files with 331 additions and 228 deletions

View File

@ -12,8 +12,17 @@ NAMESPACE_MODULE_BEGIN(Utility)
struct FAllocatorInterface;
template <typename T>
concept CInstantiableAllocator = CDerivedFrom<T, FAllocatorInterface> && !CSameAs<T, FAllocatorInterface>;
template <typename A, typename T = int>
concept CInstantiableAllocator = !CSameAs<A, FAllocatorInterface>
&& requires (typename A::template ForElementType<T>& Allocator, T* InPtr, size_t Num, size_t NumAllocated)
{
{ Allocator.Allocate(Num) } -> CSameAs<T*>;
{ Allocator.Deallocate(InPtr) } -> CSameAs<void>;
{ AsConst(Allocator).IsTransferable(InPtr) } -> CBooleanTestable;
{ AsConst(Allocator).CalculateSlackGrow(Num, NumAllocated) } -> CSameAs<size_t>;
{ AsConst(Allocator).CalculateSlackShrink(Num, NumAllocated) } -> CSameAs<size_t>;
{ AsConst(Allocator).CalculateSlackReserve(Num) } -> CSameAs<size_t>;
};
/**
* This is the allocator interface, the allocator does not use virtual, this contains the default of
@ -24,10 +33,16 @@ concept CInstantiableAllocator = CDerivedFrom<T, FAllocatorInterface> && !CSameA
struct FAllocatorInterface
{
template <CObject T>
class ForElementType : private FSingleton
class ForElementType /*: private FSingleton*/
{
public:
ForElementType() = default;
ForElementType(const ForElementType&) = delete;
ForElementType(ForElementType&&) = delete;
ForElementType& operator=(const ForElementType&) = delete;
ForElementType& operator=(ForElementType&&) = delete;
/**
* Allocates uninitialized storage.
* Should be allocated according to the results given by the CalculateSlackReserve() family,
@ -57,7 +72,7 @@ struct FAllocatorInterface
#define ALLOCATOR_WRAPPER_BEGIN(Allocator, Type, Name) \
\
struct PREPROCESSOR_JOIN(F, Name) : private FSingleton
struct PREPROCESSOR_JOIN(F, Name) /*: private FSingleton*/
#define ALLOCATOR_WRAPPER_END(Allocator, Type, Name) ; \
\
@ -90,13 +105,19 @@ struct FAllocatorInterface
PREPROCESSOR_JOIN(T, Name)<typename Allocator::template ForElementType<Type>> Name;
/** This is heap allocator that calls Memory::Malloc() directly for memory allocation. */
struct FHeapAllocator : public FAllocatorInterface
struct FHeapAllocator
{
template <CObject T>
class ForElementType : public FAllocatorInterface::ForElementType<T>
class ForElementType
{
public:
ForElementType() = default;
ForElementType(const ForElementType&) = delete;
ForElementType(ForElementType&&) = delete;
ForElementType& operator=(const ForElementType&) = delete;
ForElementType& operator=(ForElementType&&) = delete;
NODISCARD FORCEINLINE T* Allocate(size_t InNum)
{
return InNum != 0 ? static_cast<T*>(Memory::Malloc(Memory::QuantizeSize(InNum * sizeof(T)), alignof(T))) : nullptr;
@ -107,6 +128,8 @@ struct FHeapAllocator : public FAllocatorInterface
Memory::Free(InPtr);
}
NODISCARD FORCEINLINE bool IsTransferable(T* InPtr) const { return true; }
NODISCARD FORCEINLINE size_t CalculateSlackGrow(size_t Num, size_t NumAllocated) const
{
const size_t FirstGrow = 4;
@ -159,13 +182,19 @@ struct FHeapAllocator : public FAllocatorInterface
* Any allocation needed beyond that causes all data to be moved into an indirect allocation.
*/
template <size_t NumInline, CInstantiableAllocator SecondaryAllocator = FHeapAllocator>
struct TInlineAllocator : public FAllocatorInterface
struct TInlineAllocator
{
template <CObject T>
class ForElementType : public FAllocatorInterface::ForElementType<T>
class ForElementType
{
public:
ForElementType() = default;
ForElementType(const ForElementType&) = delete;
ForElementType(ForElementType&&) = delete;
ForElementType& operator=(const ForElementType&) = delete;
ForElementType& operator=(ForElementType&&) = delete;
NODISCARD FORCEINLINE T* Allocate(size_t InNum)
{
if (InNum == 0) return nullptr;
@ -233,13 +262,19 @@ struct TInlineAllocator : public FAllocatorInterface
};
/** This is a null allocator for which all operations are illegal. */
struct FNullAllocator : public FAllocatorInterface
struct FNullAllocator
{
template <CObject T>
class ForElementType : public FAllocatorInterface::ForElementType<T>
class ForElementType
{
public:
ForElementType() = default;
ForElementType(const ForElementType&) = delete;
ForElementType(ForElementType&&) = delete;
ForElementType& operator=(const ForElementType&) = delete;
ForElementType& operator=(ForElementType&&) = delete;
NODISCARD FORCEINLINE T* Allocate(size_t InNum) { check_no_entry(); return nullptr; }
FORCEINLINE void Deallocate(T* InPtr) { check_no_entry(); }