From a15ff6980972f6c2c919baa43339d73da56cc9b1 Mon Sep 17 00:00:00 2001 From: _Redstone_c_ Date: Mon, 27 Feb 2023 23:25:40 +0800 Subject: [PATCH] refactor(memory): remove FDefaultAllocator and directly specify a specific --- .../Private/Testing/ContainersTesting.cpp | 1 - .../Source/Public/Containers/Array.h | 156 +++++++++--------- .../Source/Public/Containers/ArrayView.h | 4 +- .../Source/Public/Memory/Allocator.h | 4 +- 4 files changed, 81 insertions(+), 84 deletions(-) diff --git a/Redcraft.Utility/Source/Private/Testing/ContainersTesting.cpp b/Redcraft.Utility/Source/Private/Testing/ContainersTesting.cpp index e855b53..a8ff331 100644 --- a/Redcraft.Utility/Source/Private/Testing/ContainersTesting.cpp +++ b/Redcraft.Utility/Source/Private/Testing/ContainersTesting.cpp @@ -124,7 +124,6 @@ NAMESPACE_UNNAMED_END void TestArray() { - TestArrayTemplate(); TestArrayTemplate(); TestArrayTemplate, 8>(); TestArrayTemplate, 64>(); diff --git a/Redcraft.Utility/Source/Public/Containers/Array.h b/Redcraft.Utility/Source/Public/Containers/Array.h index db74f27..10f2ca2 100644 --- a/Redcraft.Utility/Source/Public/Containers/Array.h +++ b/Redcraft.Utility/Source/Public/Containers/Array.h @@ -18,78 +18,6 @@ NAMESPACE_MODULE_BEGIN(Utility) NAMESPACE_PRIVATE_BEGIN -template && !CFinal> -class TArrayStorage; - -template -class TArrayStorage : private A -{ -public: - - FORCEINLINE TArrayStorage() = default; - - FORCEINLINE TArrayStorage(const TArrayStorage&) = delete; - FORCEINLINE TArrayStorage(TArrayStorage&& InValue) = delete; - FORCEINLINE TArrayStorage& operator=(const TArrayStorage&) = delete; - FORCEINLINE TArrayStorage& operator=(TArrayStorage&&) = delete; - - FORCEINLINE T*& GetPointer() { return Pointer; } - FORCEINLINE T* GetPointer() const { return Pointer; } - - FORCEINLINE size_t& GetNum() { return ArrayNum; } - FORCEINLINE size_t GetNum() const { return ArrayNum; } - FORCEINLINE size_t& GetMax() { return ArrayMax; } - FORCEINLINE size_t GetMax() const { return ArrayMax; } - - FORCEINLINE A& GetAllocator() { return *this; } - FORCEINLINE const A& GetAllocator() const { return *this; } - -private: - - // NOTE: NO_UNIQUE_ADDRESS is not valid in MSVC, use base class instead of member variable - //NO_UNIQUE_ADDRESS A Allocator; - - T* Pointer; - - size_t ArrayNum; - size_t ArrayMax; - -}; - -template -class TArrayStorage -{ -public: - - FORCEINLINE TArrayStorage() = default; - - FORCEINLINE TArrayStorage(const TArrayStorage&) = delete; - FORCEINLINE TArrayStorage(TArrayStorage&& InValue) = delete; - FORCEINLINE TArrayStorage& operator=(const TArrayStorage&) = delete; - FORCEINLINE TArrayStorage& operator=(TArrayStorage&&) = delete; - - FORCEINLINE T*& GetPointer() { return Pointer; } - FORCEINLINE T* GetPointer() const { return Pointer; } - - FORCEINLINE size_t& GetNum() { return ArrayNum; } - FORCEINLINE size_t GetNum() const { return ArrayNum; } - FORCEINLINE size_t& GetMax() { return ArrayMax; } - FORCEINLINE size_t GetMax() const { return ArrayMax; } - - FORCEINLINE A& GetAllocator() { return Allocator; } - FORCEINLINE const A& GetAllocator() const { return Allocator; } - -private: - - T* Pointer; - - size_t ArrayNum; - size_t ArrayMax; - - A Allocator; - -}; - template class TArrayIterator { @@ -126,8 +54,8 @@ public: FORCEINLINE TArrayIterator& operator++() { ++Pointer; CheckThis(); return *this; } FORCEINLINE TArrayIterator& operator--() { --Pointer; CheckThis(); return *this; } - FORCEINLINE TArrayIterator operator++(int) { TArrayIterator Temp = *this; ++Pointer; CheckThis(); return Temp; } - FORCEINLINE TArrayIterator operator--(int) { TArrayIterator Temp = *this; --Pointer; CheckThis(); return Temp; } + FORCEINLINE TArrayIterator operator++(int) { TArrayIterator Temp = *this; ++*this; return Temp; } + FORCEINLINE TArrayIterator operator--(int) { TArrayIterator Temp = *this; --*this; return Temp; } FORCEINLINE TArrayIterator& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; } FORCEINLINE TArrayIterator& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; } @@ -175,7 +103,7 @@ private: NAMESPACE_PRIVATE_END /** Dynamic array. The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements. */ -template requires (!CConst) +template requires (!CConst) class TArray final { public: @@ -1105,7 +1033,7 @@ public: Storage.GetAllocator().Deallocate(OldAllocation); } - + /** @return The pointer to the underlying element storage. */ NODISCARD FORCEINLINE TObserverPtr< ElementType[]> GetData() { return TObserverPtr< ElementType[]>(Storage.GetPointer()); } NODISCARD FORCEINLINE TObserverPtr GetData() const { return TObserverPtr(Storage.GetPointer()); } @@ -1115,7 +1043,7 @@ public: NODISCARD FORCEINLINE ConstIterator Begin() const { return ConstIterator(this, Storage.GetPointer()); } NODISCARD FORCEINLINE Iterator End() { return Iterator(this, Storage.GetPointer() + Num()); } NODISCARD FORCEINLINE ConstIterator End() const { return ConstIterator(this, Storage.GetPointer() + Num()); } - + /** @return The reverse iterator to the first or end element. */ NODISCARD FORCEINLINE ReverseIterator RBegin() { return ReverseIterator(End()); } NODISCARD FORCEINLINE ConstReverseIterator RBegin() const { return ConstReverseIterator(End()); } @@ -1203,7 +1131,79 @@ public: private: - NAMESPACE_PRIVATE::TArrayStorage> Storage; + template && !CFinal> + class TStorage; + + template + class TStorage : private A + { + public: + + FORCEINLINE TStorage() = default; + + FORCEINLINE TStorage(const TStorage&) = delete; + FORCEINLINE TStorage(TStorage&& InValue) = delete; + FORCEINLINE TStorage& operator=(const TStorage&) = delete; + FORCEINLINE TStorage& operator=(TStorage&&) = delete; + + FORCEINLINE T*& GetPointer() { return Pointer; } + FORCEINLINE T* GetPointer() const { return Pointer; } + + FORCEINLINE size_t& GetNum() { return ArrayNum; } + FORCEINLINE size_t GetNum() const { return ArrayNum; } + FORCEINLINE size_t& GetMax() { return ArrayMax; } + FORCEINLINE size_t GetMax() const { return ArrayMax; } + + FORCEINLINE A& GetAllocator() { return *this; } + FORCEINLINE const A& GetAllocator() const { return *this; } + + private: + + // NOTE: NO_UNIQUE_ADDRESS is not valid in MSVC, use base class instead of member variable + //NO_UNIQUE_ADDRESS A Allocator; + + T* Pointer; + + size_t ArrayNum; + size_t ArrayMax; + + }; + + template + class TStorage + { + public: + + FORCEINLINE TStorage() = default; + + FORCEINLINE TStorage(const TStorage&) = delete; + FORCEINLINE TStorage(TStorage&& InValue) = delete; + FORCEINLINE TStorage& operator=(const TStorage&) = delete; + FORCEINLINE TStorage& operator=(TStorage&&) = delete; + + FORCEINLINE T*& GetPointer() { return Pointer; } + FORCEINLINE T* GetPointer() const { return Pointer; } + + FORCEINLINE size_t& GetNum() { return ArrayNum; } + FORCEINLINE size_t GetNum() const { return ArrayNum; } + FORCEINLINE size_t& GetMax() { return ArrayMax; } + FORCEINLINE size_t GetMax() const { return ArrayMax; } + + FORCEINLINE A& GetAllocator() { return Allocator; } + FORCEINLINE const A& GetAllocator() const { return Allocator; } + + private: + + T* Pointer; + + size_t ArrayNum; + size_t ArrayMax; + + A Allocator; + + }; + + TStorage> Storage; }; diff --git a/Redcraft.Utility/Source/Public/Containers/ArrayView.h b/Redcraft.Utility/Source/Public/Containers/ArrayView.h index df74a75..786079e 100644 --- a/Redcraft.Utility/Source/Public/Containers/ArrayView.h +++ b/Redcraft.Utility/Source/Public/Containers/ArrayView.h @@ -55,8 +55,8 @@ public: FORCEINLINE constexpr TArrayViewIterator& operator++() { ++Pointer; CheckThis(); return *this; } FORCEINLINE constexpr TArrayViewIterator& operator--() { --Pointer; CheckThis(); return *this; } - FORCEINLINE constexpr TArrayViewIterator operator++(int) { TArrayViewIterator Temp = *this; ++Pointer; CheckThis(); return Temp; } - FORCEINLINE constexpr TArrayViewIterator operator--(int) { TArrayViewIterator Temp = *this; --Pointer; CheckThis(); return Temp; } + FORCEINLINE constexpr TArrayViewIterator operator++(int) { TArrayViewIterator Temp = *this; ++*this; return Temp; } + FORCEINLINE constexpr TArrayViewIterator operator--(int) { TArrayViewIterator Temp = *this; --*this; return Temp; } FORCEINLINE constexpr TArrayViewIterator& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; } FORCEINLINE constexpr TArrayViewIterator& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; } diff --git a/Redcraft.Utility/Source/Public/Memory/Allocator.h b/Redcraft.Utility/Source/Public/Memory/Allocator.h index 885afcf..1306209 100644 --- a/Redcraft.Utility/Source/Public/Memory/Allocator.h +++ b/Redcraft.Utility/Source/Public/Memory/Allocator.h @@ -120,13 +120,11 @@ struct FHeapAllocator : public FAllocatorInterface }; }; -using FDefaultAllocator = FHeapAllocator; - /** * The inline allocator allocates up to a specified number of elements in the same allocation as the container. * Any allocation needed beyond that causes all data to be moved into an indirect allocation. */ -template +template struct TInlineAllocator : public FAllocatorInterface { template