UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Pool.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#if (defined(__AUTORTFM) && __AUTORTFM)
6
7#include "BlockAllocator.h"
8
9#include <cstddef>
10#include <utility>
11
12namespace AutoRTFM
13{
14
15// An allocator of objects of a single type, backed by a TBlockAllocator with
16// and internal linked list for free items.
17// Items can be constructed and returned from the pool with Take().
18// The memory of items returned to the pool with Return() will be reused for
19// the next calls to Take() in a LIFO order.
20//
21// Template parameters:
22// ItemType - the pooled item type.
23// InlineCapacity - the number of items that can be held before spilling to
24// the heap.
25template<typename ItemType, size_t InlineCapacity>
26class TPool
27{
28public:
29 using FItem = ItemType;
30
31 // Constructor
32 TPool() = default;
33
34 // Acquires a new Item from the pool, forward-constructing the item with Arguments.
35 template<typename ... ArgumentTypes>
36 FItem* Take(ArgumentTypes&& ... Arguments)
37 {
38 void* Memory = nullptr;
39 if (FreeList)
40 {
41 // Unlink a free entry from the free list.
42 Memory = FreeList;
43 FreeList = FreeList->Next;
44 }
45 else
46 {
47 // Free list is empty. Allocate another item.
49 NumAllocated++;
50 }
51
52 AUTORTFM_ASSERT(NumInUse < NumAllocated);
53 NumInUse++;
54
55 return new (Memory) FItem(std::forward<ArgumentTypes>(Arguments)...);
56 }
57
58 // Destructs the item and returns it back to the pool so the memory can be
59 // reused.
60 void Return(FItem* Item)
61 {
62 // Destruct the item
63 Item->~FItem();
64
65 // Place the entry onto the free list
66 TFreeEntry* FreeEntry = reinterpret_cast<TFreeEntry*>(Item);
67 FreeEntry->Next = FreeList;
68 FreeList = FreeEntry;
69
71 NumInUse--;
72 }
73
74 // Frees the memory allocated for the item list.
75 // No items can be in use when calling.
76 void Reset(bool bIgnoreNonReturned = false)
77 {
79 Allocator.FreeAll();
80 NumInUse = 0;
81 NumAllocated = 0;
82 FreeList = nullptr;
83 }
84
85 // Returns the number of items allocated.
86 size_t GetNumAllocated() const { return NumAllocated; }
87
88 // Returns the number of items currently in use (not returned).
89 size_t GetNumInUse() const { return NumInUse; }
90
91private:
92 struct TFreeEntry
93 {
95 };
96
97 // The memory of each entry is used for both an Item (when in use) and a
98 // TFreeEntry (when in the pool), so the size and alignment needs to be
99 // the max of both.
100 static constexpr size_t EntrySize = std::max<size_t>(sizeof(FItem), sizeof(TFreeEntry));
101 static constexpr size_t EntryAlignment = std::max<size_t>(alignof(FItem), alignof(TFreeEntry));
102
103 TPool(TPool&&) = delete;
104 TPool(const TPool&) = delete;
105 TPool& operator=(const TPool&) = delete;
106 TPool& operator=(TPool&&) = delete;
107
108 // The underlying allocator for the pool.
110 // Free list.
111 TFreeEntry* FreeList = nullptr;
112 // Number of entries allocated.
113 size_t NumAllocated = 0;
114 // Number of entries currently in use.
115 size_t NumInUse = 0;
116};
117
118} // namespace AutoRTFM
119
120#endif // (defined(__AUTORTFM) && __AUTORTFM)
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition IoDispatcher.cpp:201
Definition API.cpp:57
@ Return
Definition KismetSystemLibrary.h:68