UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
IntrusivePool.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 "AutoRTFMDefines.h"
8#include "BlockAllocator.h"
9
10#include <cstddef>
11#include <utility>
12
13namespace AutoRTFM
14{
15
16template<typename ItemType, size_t InlineCapacity>
18{
19public:
20 using FItem = ItemType;
21
22 TIntrusivePool() = default;
23
25 {
26 while (FItem* const Item = FreeList)
27 {
28 FreeList = *Item->GetIntrusiveAddress();
29
30 Item->~FItem();
31 }
32 }
33
34 // Acquires a new Item from the pool. If the item was previously returned
35 // to the pool, we'll call `Resurrect` on it with `Arguments`. Otherwise
36 // create a new item with `Arguments`.
37 template<typename ... ArgumentTypes>
38 FItem* Take(ArgumentTypes&& ... Arguments)
39 {
40 FItem* Item = nullptr;
41
42 if (FreeList)
43 {
44 // Unlink a free entry from the free list.
45 Item = FreeList;
46 FreeList = *(FreeList->GetIntrusiveAddress());
47
48 Item->Resurrect(std::forward<ArgumentTypes>(Arguments)...);
49 }
50 else
51 {
52 // Free list is empty. Allocate another item.
53 void* const Memory = Allocator.Allocate(EntrySize, EntryAlignment);
54 NumAllocated++;
55
56 Item = new (Memory) FItem(std::forward<ArgumentTypes>(Arguments)...);
57 }
58
59 AUTORTFM_ASSERT(NumInUse < NumAllocated);
60 NumInUse++;
61
62 return Item;
63 }
64
65 // Calls `Suppress` on `Item` and returns it to the pool to be reused.
66 void Return(FItem* const Item)
67 {
68 // Suppress the item.
69 Item->Suppress();
70
71 // Place the entry onto the free list.
72 *Item->GetIntrusiveAddress() = FreeList;
73 FreeList = Item;
74
76 NumInUse--;
77 }
78
79private:
80 static constexpr size_t EntrySize = sizeof(FItem);
81 static constexpr size_t EntryAlignment = alignof(FItem);
82
83 // The underlying allocator for the pool.
85 // Free list.
86 FItem* FreeList = nullptr;
87 // Number of entries allocated.
88 size_t NumAllocated = 0;
89 // Number of entries currently in use.
90 size_t NumInUse = 0;
91
93 TIntrusivePool(const TIntrusivePool&) = delete;
94 TIntrusivePool& operator=(const TIntrusivePool&) = delete;
95 TIntrusivePool& operator=(TIntrusivePool&&) = delete;
96};
97
98} // namespace AutoRTFM
99
100#endif // (defined(__AUTORTFM) && __AUTORTFM)
#define AUTORTFM_DISABLE
Definition AutoRTFMDefines.h:116
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