UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
PoolBackedArray.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "Chaos/Core.h"
5#include "Chaos/ObjectPool.h"
6
7namespace Chaos::Private
8{
12 template<typename T>
14 {
15 // TPoolBackedArray requires the items to store their index in the array for fast removal
16 static int32 GetArrayIndex(const T& Item) { return Item.GetArrayIndex(); }
17 static void SetArrayIndex(T& Item, const int32 Index) { return Item.SetArrayIndex(Index); }
18
19 // Called when an item is added to the free list.
20 // Should perform work equivalent to a destructor but does not necessarily need to free up resources.
21 static void TrashItem(T& Item) { Item.Trash(); }
22
23 // Called when an item is created by recovering from the free list.
24 // Should perform work equivalent to a constructor.
25 template<typename... TArgs>
26 static void ReuseItem(T& Item, TArgs&&... Args) { Item.Reuse(Forward<TArgs>(Args)...); }
27 };
28
35 template<typename T, typename TItemAdapter = TPoolBackedItemAdapter<T>>
37 {
38 public:
39 using FItem = T;
40 using FItemPtr = FItem*;
41 using FConstItemPtr = const FItem*;
45
50
51 template<typename... TArgs>
52 FItemPtr Alloc(TArgs&&... Args)
53 {
54 FItemPtr Item = nullptr;
55
56 // Allocate from free list or pool
57 if (!FreeItems.IsEmpty())
58 {
59 Item = FreeItems.Pop(EAllowShrinking::No);
60 FItemAdapter::ReuseItem(*Item, Forward<TArgs>(Args)...);
61 }
62 else
63 {
64 Item = Pool.Alloc(Forward<TArgs>(Args)...);
65 }
66
67 // Add to the array and set the index
68 const int32 Index = Items.Add(Item);
69 FItemAdapter::SetArrayIndex(*Item, Index);
70
71 return Item;
72 }
73
74 void Free(FItemPtr Item)
75 {
76 if (Item != nullptr)
77 {
78 // Remove item from the array using the index we set in Alloc
79 const int32 Index = FItemAdapter::GetArrayIndex(*Item);
80 check(Items[Index] == Item);
82 FItemAdapter::SetArrayIndex(*Item, INDEX_NONE);
83
84 // Update the index of the item we swapped in
85 if (Index < Items.Num())
86 {
87 FItemAdapter::SetArrayIndex(*Items[Index], Index);
88 }
89
90 // Move the item to the trash
91 FItemAdapter::TrashItem(*Item);
92 FreeItems.Add(Item);
93 }
94 }
95
96 void Reset()
97 {
98 for (int32 Index = Num() - 1; Index >= 0; --Index)
99 {
100 Free(Items[Index]);
101 }
102
103 // Clear the free list
104 for (FItemPtr Item : FreeItems)
105 {
106 Pool.Free(Item);
107 }
108 FreeItems.Reset();
109
110 check(Items.Num() == 0);
111 check(Pool.GetNumAllocated() == 0);
112 }
113
114 int32 Num() const
115 {
116 return Items.Num();
117 }
118
119 bool IsEmpty() const
120 {
121 return (Num() == 0);
122 }
123
124 void Reserve(const int32 Size)
125 {
126 Items.Reserve(Size);
127 FreeItems.Reserve(Size);
128 Pool.ReserveItems(Size);
129 }
130
132 {
133 return Items[Index];
134 }
135
136 const FItemPtr operator[](const int32 Index) const
137 {
138 return Items[Index];
139 }
140
142 {
143 return Items.begin();
144 }
145
147 {
148 return Items.end();
149 }
150
152 {
153 return Items.begin();
154 }
155
157 {
158 return Items.end();
159 }
160
162 {
163 FreeItems.Sort([](const FItem& L, const FItem& R) { return &L < &R; });
164
165 //Pool.SortFreeLists();
166 }
167
168 private:
169
170 TArray<FItemPtr> Items;
171 TArray<FItemPtr> FreeItems;
173 };
174}
#define check(expr)
Definition AssertionMacros.h:314
@ INDEX_NONE
Definition CoreMiscDefines.h:150
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
uint32 Size
Definition VulkanMemory.cpp:4034
Definition PoolBackedArray.h:37
FRangedForConstIterator begin() const
Definition PoolBackedArray.h:151
FRangedForConstIterator end() const
Definition PoolBackedArray.h:156
void Free(FItemPtr Item)
Definition PoolBackedArray.h:74
const FItem * FConstItemPtr
Definition PoolBackedArray.h:41
TPoolBackedArray(const int32 NumItemsPerBlock)
Definition PoolBackedArray.h:46
FRangedForIterator end()
Definition PoolBackedArray.h:146
bool IsEmpty() const
Definition PoolBackedArray.h:119
FItemPtr operator[](const int32 Index)
Definition PoolBackedArray.h:131
typename TArray< FItemPtr >::RangedForConstIteratorType FRangedForConstIterator
Definition PoolBackedArray.h:44
FRangedForIterator begin()
Definition PoolBackedArray.h:141
void Reset()
Definition PoolBackedArray.h:96
TItemAdapter FItemAdapter
Definition PoolBackedArray.h:42
void Reserve(const int32 Size)
Definition PoolBackedArray.h:124
FItemPtr Alloc(TArgs &&... Args)
Definition PoolBackedArray.h:52
T FItem
Definition PoolBackedArray.h:39
FItem * FItemPtr
Definition PoolBackedArray.h:40
int32 Num() const
Definition PoolBackedArray.h:114
void SortFreeLists()
Definition PoolBackedArray.h:161
const FItemPtr operator[](const int32 Index) const
Definition PoolBackedArray.h:136
typename TArray< FItemPtr >::RangedForIteratorType FRangedForIterator
Definition PoolBackedArray.h:43
Definition ObjectPool.h:25
FPtr Alloc(TArgs &&... Args)
Definition ObjectPool.h:64
void Free(FPtr Object)
Definition ObjectPool.h:119
int32 GetNumAllocated() const
Definition ObjectPool.h:238
void ReserveItems(int32 NumItems)
Definition ObjectPool.h:217
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
void Reset(SizeType NewSize=0)
Definition Array.h:2246
UE_FORCEINLINE_HINT void RemoveAtSwap(SizeType Index, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2185
UE_NODEBUG UE_FORCEINLINE_HINT RangedForIteratorType end()
Definition Array.h:3391
UE_REWRITE bool IsEmpty() const
Definition Array.h:1133
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
UE_NODEBUG UE_FORCEINLINE_HINT RangedForIteratorType begin()
Definition Array.h:3389
ElementType Pop(EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:1196
UE_NODEBUG void Sort()
Definition Array.h:3418
UE_FORCEINLINE_HINT void Reserve(SizeType Number)
Definition Array.h:3016
Definition BodyInstance.h:90
U16 Index
Definition radfft.cpp:71
Definition PoolBackedArray.h:14
static void ReuseItem(T &Item, TArgs &&... Args)
Definition PoolBackedArray.h:26
static void SetArrayIndex(T &Item, const int32 Index)
Definition PoolBackedArray.h:17
static int32 GetArrayIndex(const T &Item)
Definition PoolBackedArray.h:16
static void TrashItem(T &Item)
Definition PoolBackedArray.h:21
Definition Array.h:206