feat(memory): add CMultipleAllocator for non-contiguous containers

This commit is contained in:
_Redstone_c_ 2023-03-22 19:35:42 +08:00
parent 3d951a80db
commit ff155e23de
4 changed files with 23 additions and 12 deletions

View File

@ -17,7 +17,7 @@ NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility) NAMESPACE_MODULE_BEGIN(Utility)
/** 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. */ /** 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 <CElementalObject T, CInstantiableAllocator Allocator = FHeapAllocator> requires (!CConst<T>) template <CElementalObject T, CAllocator<T> Allocator = FHeapAllocator> requires (!CConst<T>)
class TArray final class TArray final
{ {
private: private:

View File

@ -17,7 +17,7 @@ NAMESPACE_MODULE_BEGIN(Utility)
template <CElementalObject T, size_t N> template <CElementalObject T, size_t N>
struct TStaticArray; struct TStaticArray;
template <CElementalObject T, CInstantiableAllocator A> requires (!CConst<T>) template <CElementalObject T, CAllocator<T> A> requires (!CConst<T>)
class TArray; class TArray;
inline constexpr size_t DynamicExtent = INDEX_NONE; inline constexpr size_t DynamicExtent = INDEX_NONE;

View File

@ -24,7 +24,7 @@ using TDefaultBitsetAllocator = TInlineAllocator<(40 - 3 * sizeof(size_t)) / siz
NAMESPACE_PRIVATE_END NAMESPACE_PRIVATE_END
template <CUnsignedIntegral InBlockType, CInstantiableAllocator Allocator = NAMESPACE_PRIVATE::TDefaultBitsetAllocator<InBlockType>> requires (!CSameAs<InBlockType, bool>) template <CUnsignedIntegral InBlockType, CAllocator<InBlockType> Allocator = NAMESPACE_PRIVATE::TDefaultBitsetAllocator<InBlockType>> requires (!CSameAs<InBlockType, bool>)
class TBitset final class TBitset final
{ {
private: private:

View File

@ -13,7 +13,7 @@ NAMESPACE_MODULE_BEGIN(Utility)
struct FAllocatorInterface; struct FAllocatorInterface;
template <typename A, typename T = int> template <typename A, typename T = int>
concept CInstantiableAllocator = !CSameAs<A, FAllocatorInterface> concept CAllocator = !CSameAs<A, FAllocatorInterface>
&& requires (typename A::template ForElementType<T>& Allocator, T* InPtr, size_t Num, size_t NumAllocated) && requires (typename A::template ForElementType<T>& Allocator, T* InPtr, size_t Num, size_t NumAllocated)
{ {
{ Allocator.Allocate(Num) } -> CSameAs<T*>; { Allocator.Allocate(Num) } -> CSameAs<T*>;
@ -24,6 +24,9 @@ concept CInstantiableAllocator = !CSameAs<A, FAllocatorInterface>
{ AsConst(Allocator).CalculateSlackReserve(Num) } -> CSameAs<size_t>; { AsConst(Allocator).CalculateSlackReserve(Num) } -> CSameAs<size_t>;
}; };
template <typename A, typename T = int>
concept CMultipleAllocator = CAllocator<A, T> && A::bSupportsMultipleAllocation;
/** /**
* This is the allocator interface, the allocator does not use virtual, this contains the default of * This is the allocator interface, the allocator does not use virtual, this contains the default of
* the allocator interface functions. Unlike std::allocator, IAllocator should be bound to only a object, * the allocator interface functions. Unlike std::allocator, IAllocator should be bound to only a object,
@ -32,6 +35,14 @@ concept CInstantiableAllocator = !CSameAs<A, FAllocatorInterface>
*/ */
struct FAllocatorInterface struct FAllocatorInterface
{ {
/**
* If this flag is false, it is possible to allocate an address that has already been allocated.
* Should be allocated according to the results given by the CalculateSlackReserve() family,
* without needing to allocate memory of the same size as the allocated memory,
* this is to support special allocators such as TInlineAllocator.
*/
static constexpr bool bSupportsMultipleAllocation = true;
template <CObject T> template <CObject T>
class ForElementType /*: private FSingleton*/ class ForElementType /*: private FSingleton*/
{ {
@ -43,13 +54,7 @@ struct FAllocatorInterface
ForElementType& operator=(const ForElementType&) = delete; ForElementType& operator=(const ForElementType&) = delete;
ForElementType& operator=(ForElementType&&) = delete; ForElementType& operator=(ForElementType&&) = delete;
/** /** Allocates uninitialized storage. If 'InNum' is zero, return nullptr. */
* Allocates uninitialized storage.
* Should be allocated according to the results given by the CalculateSlackReserve() family,
* without needing to allocate memory of the same size as the allocated memory,
* this is to support special allocators such as TInlineAllocator.
* If 'InNum' is zero, return nullptr.
*/
NODISCARD FORCEINLINE T* Allocate(size_t InNum) = delete; NODISCARD FORCEINLINE T* Allocate(size_t InNum) = delete;
/** Deallocates storage. */ /** Deallocates storage. */
@ -107,6 +112,8 @@ struct FAllocatorInterface
/** This is heap allocator that calls Memory::Malloc() directly for memory allocation. */ /** This is heap allocator that calls Memory::Malloc() directly for memory allocation. */
struct FHeapAllocator struct FHeapAllocator
{ {
static constexpr bool bSupportsMultipleAllocation = true;
template <CObject T> template <CObject T>
class ForElementType class ForElementType
{ {
@ -181,9 +188,11 @@ struct FHeapAllocator
* The inline allocator allocates up to a specified number of elements in the same allocation as the container. * 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. * Any allocation needed beyond that causes all data to be moved into an indirect allocation.
*/ */
template <size_t NumInline, CInstantiableAllocator SecondaryAllocator = FHeapAllocator> template <size_t NumInline, CAllocator SecondaryAllocator = FHeapAllocator>
struct TInlineAllocator struct TInlineAllocator
{ {
static constexpr bool bSupportsMultipleAllocation = false;
template <CObject T> template <CObject T>
class ForElementType class ForElementType
{ {
@ -264,6 +273,8 @@ struct TInlineAllocator
/** This is a null allocator for which all operations are illegal. */ /** This is a null allocator for which all operations are illegal. */
struct FNullAllocator struct FNullAllocator
{ {
static constexpr bool bSupportsMultipleAllocation = true;
template <CObject T> template <CObject T>
class ForElementType class ForElementType
{ {