UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Containers.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6
7#if USING_INSTRUMENTATION
8
9#include "CoreTypes.h"
10#include "Containers/Map.h"
11
13
14// ------------------------------------------------------------------------------
15// Allocators (we can't use the Engine default allocators if they're instrumented)
16// ------------------------------------------------------------------------------
17#if PLATFORM_WINDOWS
19public:
21 static INSTRUMENTATION_FUNCTION_ATTRIBUTES void Free(void* Data);
24 static INSTRUMENTATION_FUNCTION_ATTRIBUTES void* Realloc(void* Data, SIZE_T Size, SIZE_T PreviousSize);
25};
26
28
29#define SAFE_OPERATOR_NEW_DELETE() \
30 INSTRUMENTATION_FUNCTION_ATTRIBUTES void * operator new(SIZE_T Size) \
31 { \
32 return FInstrumentationSafeWinAllocator::Alloc(Size); \
33 } \
34 INSTRUMENTATION_FUNCTION_ATTRIBUTES void operator delete(void* Ptr) \
35 { \
36 FInstrumentationSafeWinAllocator::Free(Ptr); \
37 }
38
39#define SAFE_OPERATOR_NEW_DELETE_WITH_GUARDS() \
40 INSTRUMENTATION_FUNCTION_ATTRIBUTES void * operator new(SIZE_T Size) \
41 { \
42 return FInstrumentationSafeWinAllocator::AllocWithGuards(Size); \
43 } \
44 INSTRUMENTATION_FUNCTION_ATTRIBUTES void operator delete(void* Ptr) \
45 { \
46 FInstrumentationSafeWinAllocator::FreeWithGuards(Ptr); \
47 }
48
49#else
51public:
52 INSTRUMENTATION_FUNCTION_ATTRIBUTES static void* Realloc(void* Data, SIZE_T Size, SIZE_T PreviousSize)
53 {
54 return realloc(Data, Size);
55 }
56 INSTRUMENTATION_FUNCTION_ATTRIBUTES static void Free(void* Data)
57 {
58 return free(Data);
59 }
60};
61
63#endif
64
65template <int IndexSize = 32, typename TBaseAllocator = TInstrumentationSafeBaseAllocator>
67public:
68 using SizeType = typename TBitsToSizeType<IndexSize>::Type;
69
70private:
71 using USizeType = std::make_unsigned_t<SizeType>;
72
73public:
74 enum { NeedsElementType = true };
75 enum { RequireRangeCheck = true };
76
77 class ForAnyElementType
78 {
79 public:
81 INSTRUMENTATION_FUNCTION_ATTRIBUTES ForAnyElementType()
82 : Data(nullptr)
83 {
84 }
85
93 FORCEINLINE INSTRUMENTATION_FUNCTION_ATTRIBUTES void MoveToEmpty(ForAnyElementType& Other)
94 {
95 if (Data)
96 {
97 TBaseAllocator::Free(Data);
98 }
99
100 Data = Other.Data;
101 Other.Data = nullptr;
102 }
103
106 {
107 if (Data)
108 {
109 TBaseAllocator::Free(Data);
110 }
111 }
112
113 // FContainerAllocatorInterface
115 {
116 return Data;
117 }
118
119 INSTRUMENTATION_FUNCTION_ATTRIBUTES void ResizeAllocation(SizeType PreviousNumElements, SizeType NumElements, SIZE_T NumBytesPerElement)
120 {
121 // Avoid calling FMemory::Realloc( nullptr, 0 ) as ANSI C mandates returning a valid pointer which is not what we want.
122 if (Data || NumElements)
123 {
124 static_assert(sizeof(SizeType) <= sizeof(SIZE_T), "SIZE_T is expected to handle all possible sizes");
125
126 // Check for under/overflow
128 if constexpr (sizeof(SizeType) == sizeof(SIZE_T))
129 {
130 bInvalidResize = bInvalidResize || (SIZE_T)(USizeType)NumElements > (SIZE_T)TNumericLimits<SizeType>::Max() / NumBytesPerElement;
131 }
133 {
134 UE::Core::Private::OnInvalidSizedHeapAllocatorNum(IndexSize, NumElements, NumBytesPerElement);
135 }
136
137 Data = TBaseAllocator::Realloc(Data, NumElements * NumBytesPerElement, PreviousNumElements * NumBytesPerElement);
138 if (NumElements)
139 {
140 checkSlow(Data);
141 }
142 }
143 }
144 FORCEINLINE INSTRUMENTATION_FUNCTION_ATTRIBUTES SizeType CalculateSlackReserve(SizeType NumElements, SIZE_T NumBytesPerElement) const
145 {
146 return DefaultCalculateSlackReserve(NumElements, NumBytesPerElement, true);
147 }
148 FORCEINLINE INSTRUMENTATION_FUNCTION_ATTRIBUTES SizeType CalculateSlackReserve(SizeType NumElements, SIZE_T NumBytesPerElement, uint32 AlignmentOfElement) const
149 {
150 return DefaultCalculateSlackReserve(NumElements, NumBytesPerElement, true, (uint32)AlignmentOfElement);
151 }
152 FORCEINLINE INSTRUMENTATION_FUNCTION_ATTRIBUTES SizeType CalculateSlackShrink(SizeType NumElements, SizeType NumAllocatedElements, SIZE_T NumBytesPerElement) const
153 {
154 return DefaultCalculateSlackShrink(NumElements, NumAllocatedElements, NumBytesPerElement, true);
155 }
156 FORCEINLINE INSTRUMENTATION_FUNCTION_ATTRIBUTES SizeType CalculateSlackShrink(SizeType NumElements, SizeType NumAllocatedElements, SIZE_T NumBytesPerElement, uint32 AlignmentOfElement) const
157 {
158 return DefaultCalculateSlackShrink(NumElements, NumAllocatedElements, NumBytesPerElement, true, (uint32)AlignmentOfElement);
159 }
160 FORCEINLINE INSTRUMENTATION_FUNCTION_ATTRIBUTES SizeType CalculateSlackGrow(SizeType NumElements, SizeType NumAllocatedElements, SIZE_T NumBytesPerElement) const
161 {
162 return DefaultCalculateSlackGrow(NumElements, NumAllocatedElements, NumBytesPerElement, true);
163 }
164 FORCEINLINE INSTRUMENTATION_FUNCTION_ATTRIBUTES SizeType CalculateSlackGrow(SizeType NumElements, SizeType NumAllocatedElements, SIZE_T NumBytesPerElement, uint32 AlignmentOfElement) const
165 {
166 return DefaultCalculateSlackGrow(NumElements, NumAllocatedElements, NumBytesPerElement, true, (uint32)AlignmentOfElement);
167 }
168
169 INSTRUMENTATION_FUNCTION_ATTRIBUTES SIZE_T GetAllocatedSize(SizeType NumAllocatedElements, SIZE_T NumBytesPerElement) const
170 {
171 return NumAllocatedElements * NumBytesPerElement;
172 }
173
174 INSTRUMENTATION_FUNCTION_ATTRIBUTES bool HasAllocation() const
175 {
176 return !!Data;
177 }
178
179 INSTRUMENTATION_FUNCTION_ATTRIBUTES SizeType GetInitialCapacity() const
180 {
181 return 0;
182 }
183
184 private:
185 ForAnyElementType(const ForAnyElementType&);
186 ForAnyElementType& operator=(const ForAnyElementType&);
187
189 void* Data;
190 };
191
192 template<typename ElementType>
193 class ForElementType : public ForAnyElementType
194 {
195 public:
198 {
199 }
200
202 {
203 return (ElementType*)ForAnyElementType::GetAllocation();
204 }
205 };
206};
207
208template <uint32 NumInlineElements, typename BaseAllocator = TInstrumentationSafeBaseAllocator>
210
211template <typename ElementType, typename BaseAllocator = TInstrumentationSafeBaseAllocator>
213
214template<
215 typename BaseAllocator = TInstrumentationSafeBaseAllocator,
219>
221{
222public:
223
226 {
228 {
229 return FPlatformMath::RoundUpToPowerOfTwo(NumHashedElements / AverageNumberOfElementsPerHashBucket + BaseNumberOfHashBuckets);
230 }
231
232 return 1;
233 }
234
237
238 typedef InSparseArrayAllocator SparseArrayAllocator;
239 typedef InHashAllocator HashAllocator;
240
241};
242
243template <typename KeyType, typename ValueType, typename BaseAllocator = TInstrumentationSafeBaseAllocator>
245
246template <typename ElementType, typename BaseAllocator = TInstrumentationSafeBaseAllocator>
248
249#endif
#define FORCEINLINE
Definition AndroidPlatform.h:140
#define checkSlow(expr)
Definition AssertionMacros.h:332
UE_FORCEINLINE_HINT SizeType DefaultCalculateSlackReserve(SizeType NewMax, SIZE_T BytesPerElement, bool bAllowQuantize, uint32 Alignment=DEFAULT_ALIGNMENT)
Definition ContainerAllocationPolicies.h:223
#define DEFAULT_MIN_NUMBER_OF_HASHED_ELEMENTS
Definition ContainerAllocationPolicies.h:1514
UE_FORCEINLINE_HINT SizeType DefaultCalculateSlackShrink(SizeType NewMax, SizeType CurrentMax, SIZE_T BytesPerElement, bool bAllowQuantize, uint32 Alignment=DEFAULT_ALIGNMENT)
Definition ContainerAllocationPolicies.h:139
#define DEFAULT_NUMBER_OF_ELEMENTS_PER_HASH_BUCKET
Definition ContainerAllocationPolicies.h:1511
#define DEFAULT_BASE_NUMBER_OF_HASH_BUCKETS
Definition ContainerAllocationPolicies.h:1513
UE_FORCEINLINE_HINT SizeType DefaultCalculateSlackGrow(SizeType NewMax, SizeType CurrentMax, SIZE_T BytesPerElement, bool bAllowQuantize, uint32 Alignment=DEFAULT_ALIGNMENT)
Definition ContainerAllocationPolicies.h:169
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
#define UNLIKELY(x)
Definition Platform.h:857
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define INSTRUMENTATION_FUNCTION_ATTRIBUTES
Definition Defines.h:5
#define MAX_int32
Definition NumericLimits.h:25
void * GetAllocation(void *Target, uint32 Size, uint32 Offset, uint32 Alignment=16)
Definition OpenGLBuffer.cpp:57
uint32 Size
Definition VulkanMemory.cpp:4034
free(DecoderMem)
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Array.h:670
Definition UnrealString.h.inl:34
Definition ContainerAllocationPolicies.h:894
Definition ContainerAllocationPolicies.h:1383
GeometryCollection::Facades::FMuscleActivationData Data
Definition MuscleActivationConstraints.h:15
SIZE_T GetAllocatedSize(const T &Value)
Definition ManagedArray.h:93
void * Alloc(int size)
Definition ContainerAllocationPolicies.h:605
Definition NumericLimits.h:41