7#if ENABLE_LOW_LEVEL_MEM_TRACKER
9#include "Algo/BinarySearch.h"
12#include "Containers/Map.h"
13#include "Containers/Set.h"
19#define LLM_PAGE_SIZE (16*1024)
21#if WITH_EDITOR && PLATFORM_64BITS
30template<
typename T,
typename SizeType=
int32>
47 void SetAllocator(UE::LLMPrivate::FLLMAllocator*
InAllocator)
57 void Clear(
bool ReleaseMemory =
false)
71 void Add(
const T& Item)
73 if (
Count == Capacity)
97 T& operator[](SizeType
Index)
103 const T& operator[](SizeType
Index)
const
163 Reserve(
Other.Count);
182 UE::LLMPrivate::FLLMAllocator*
Allocator;
194template<
typename TKey,
typename TValue1,
typename TValue2,
typename SizeType=
int32>
240 Stripe.KeyHashes.Clear(
true);
241 Stripe.Values1.Clear(
true);
242 Stripe.Values2.Clear(
true);
243 Stripe.FreeKeyIndices.Clear(
true);
256 SizeType KeyHash =
Key.GetHashCode();
263 SizeType MapIndex =
Stripe.GetMapIndex(Key, KeyHash);
264 SizeType KeyIndex =
Stripe.Map[MapIndex];
266 if (KeyIndex != InvalidIndex)
275 Stripe.Values1[KeyIndex] = Value1;
276 Stripe.Values2[KeyIndex] = Value2;
283 SizeType MaxCount = (
Stripe.Capacity >= 256U * 256U ? (
Stripe.Capacity / 256U) * Margin : (Margin *
Stripe.Capacity) / 256U);
284 if (
Stripe.Count >= MaxCount)
292 if (
Stripe.Count > MaxCount)
300 MapIndex =
Stripe.GetMapIndex(Key, KeyHash);
303 if (
Stripe.FreeKeyIndices.Num())
316 Stripe.KeyHashes.Add(KeyHash);
317 Stripe.Values1.Add(Value1);
318 Stripe.Values2.Add(Value2);
335 SizeType KeyHash =
Key.GetHashCode();
340 SizeType MapIndex =
Stripe.GetMapIndex(Key, KeyHash);
342 SizeType KeyIndex =
Stripe.Map[MapIndex];
343 if (KeyIndex == InvalidIndex)
351 return Stripe.Keys[KeyIndex];
356 SizeType KeyHash =
Key.GetHashCode();
363 SizeType MapIndex =
Stripe.GetMapIndex(Key, KeyHash);
364 if (!
Stripe.IsItemInUse(MapIndex))
369 SizeType KeyIndex =
Stripe.Map[MapIndex];
375 if (KeyIndex ==
Stripe.Keys.Num() - 1)
378 Stripe.KeyHashes.RemoveLast();
379 Stripe.Values1.RemoveLast();
380 Stripe.Values2.RemoveLast();
384 Stripe.FreeKeyIndices.Add(KeyIndex);
389 SizeType FirstIndex = MapIndex;
441 Stripe.Map[MapIndex] = InvalidIndex;
450 SizeType TotalCount = 0;
454 TotalCount +=
Stripe.Count;
459 bool HasKey(
const TKey& Key)
461 SizeType KeyHash =
Key.GetHashCode();
466 SizeType MapIndex =
Stripe.GetMapIndex(Key, KeyHash);
467 return Stripe.IsItemInUse(MapIndex);
480 Stripe.FreeKeyIndices.Trim();
487 FBaseIterator& operator++()
493 while (MapIndex <
Stripe.Capacity)
495 if (
Stripe.IsItemInUse(MapIndex))
511 FBaseIterator(ThisType&
InMap,
bool bEnd)
545 friend struct ThisType::FIterator;
548 struct FIterator :
public FBaseIterator
557 return FBaseIterator::StripeIndex !=
Other.FBaseIterator::StripeIndex ||
558 FBaseIterator::MapIndex !=
Other.FBaseIterator::MapIndex;
563 ThisType&
LocalMap = FBaseIterator::MapRef;
565 SizeType KeyIndex =
Stripe.Map[FBaseIterator::MapIndex];
570 struct FConstIterator;
583 friend struct ThisType::FConstIterator;
586 struct FConstIterator :
public FBaseIterator
588 FConstIterator(
const ThisType&
InMap,
bool bEnd)
595 return FBaseIterator::StripeIndex !=
Other.FBaseIterator::StripeIndex ||
596 FBaseIterator::MapIndex !=
Other.FBaseIterator::MapIndex;
601 ThisType&
LocalMap = FBaseIterator::MapRef;
603 SizeType KeyIndex =
Stripe.Map[FBaseIterator::MapIndex];
611 return FIterator(*
this,
false);
616 return FIterator(*
this,
true);
619 FConstIterator
begin()
const
622 return FConstIterator(*
this,
false);
625 FConstIterator
end()
const
627 return FConstIterator(*
this,
true);
660 enum { InvalidIndex = -1 };
661 static const SizeType Margin = (30 * 256) / 100;
666 return (StripeCount == 1) ? 0 : KeyHash >> (32 - FMath::Max(
StripeCountLog2, 1));
712 if (KeyIndex != InvalidIndex)
715 Map[MapIndex] = KeyIndex;
724 return Map[MapIndex] != InvalidIndex;
729 SizeType
Mask = Capacity - 1;
731 SizeType KeyIndex =
Map[MapIndex];
733 while (KeyIndex != InvalidIndex && !(Keys[KeyIndex] == Key))
735 MapIndex = (MapIndex + 1) &
Mask;
736 KeyIndex =
Map[MapIndex];
767 const SizeType
Index,
768 const SizeType StartIndex,
769 const SizeType EndIndex)
771 return (StartIndex <= EndIndex) ?
778 UE::LLMPrivate::FLLMAllocator*
Allocator;
814 static_assert(
sizeof(
InPointer) == 8,
"LLM only supports 64 bit platforms");
822 explicit operator bool ()
const {
return !!Pointer; }
823 void* GetPointer()
const {
return (
void*)(Pointer >> 16); };
829 template <
int HashSize,
int Po
interSize>
832 static_assert(HashSize == 0 &&
PointerSize == 0,
"Converting void* to a LLMNumAllocsType - sized hashkey is not implemented for the current sizes.");
890template<
int IndexSize>
897 using USizeType = std::make_unsigned_t<SizeType>;
900 enum { NeedsElementType =
false };
901 enum { RequireRangeCheck =
true };
903 class ForAnyElementType
921 template <
typename OtherAllocator>
922 FORCEINLINE void MoveToEmptyFromOtherAllocator(
typename OtherAllocator::ForAnyElementType&
Other)
928 UE::LLMPrivate::FLLMAllocator::Get()->Free(Data,
Size);
933 Other.Data =
nullptr;
954 UE::LLMPrivate::FLLMAllocator::Get()->Free(Data,
Size);
968 static_assert(
sizeof(SizeType) <=
sizeof(
SIZE_T),
"SIZE_T is expected to handle all possible sizes");
972 if constexpr (
sizeof(SizeType) ==
sizeof(
SIZE_T))
978 UE::Core::Private::OnInvalidLLMAllocatorNum(IndexSize,
NewMax, NumBytesPerElement);
982 size_t NewSize =
NewMax * NumBytesPerElement;
983 Data = UE::LLMPrivate::FLLMAllocator::Get()->Realloc(Data,
Size, NewSize);
991 FORCEINLINE SizeType CalculateSlackShrink(SizeType
NewMax, SizeType CurrentMax,
SIZE_T NumBytesPerElement)
const
1002 return CurrentMax * NumBytesPerElement;
1005 bool HasAllocation()
const
1010 SizeType GetInitialCapacity()
const
1016 ForAnyElementType(
const ForAnyElementType&);
1017 ForAnyElementType& operator=(
const ForAnyElementType&);
1025 template<
typename ElementType>
1026 class ForElementType :
public ForAnyElementType
1036 return (ElementType*)ForAnyElementType::GetAllocation();
1042extern template CORE_API FORCENOINLINE void TSizedLLMAllocator<32>::ForAnyElementType::ResizeAllocation(SizeType CurrentNum, SizeType
NewMax,
SIZE_T NumBytesPerElement);
1043extern template CORE_API FORCENOINLINE void TSizedLLMAllocator<64>::ForAnyElementType::ResizeAllocation(SizeType CurrentNum, SizeType
NewMax,
SIZE_T NumBytesPerElement);
1053template<
typename KeyType,
typename ValueType>
1054class TLLMMap :
public TMap<KeyType, ValueType, FDefaultSetLLMAllocator>
1057template<
typename KeyType>
1058class TLLMSet :
public TSet<KeyType, DefaultKeyFuncs<KeyType>, FDefaultSetLLMAllocator>
1074 template <
typename T,
typename GetEdgesType,
typename SizeType>
1091 template <
typename RangeType,
typename GetEdgesType>
1108 template <
typename RangeType,
typename GetEdgesType>
1132 template <
typename T,
typename GetEdgesType,
typename SizeType>
1135 if (NumVertices == 0)
1139 LLMCheckf(NumVertices > 0,
TEXT(
"Invalid number of vertices"));
1143 if (SortOrder == ETopologicalSortOrder::RootToLeaf)
1179 Visited.AddDefaulted(NumVertices);
1185 SizeType NumEdges =
static_cast<SizeType
>(-1);
1187 LLMCheckf(0 <= NumEdges && NumEdges <= NumVertices,
TEXT(
"GetEdges function passed into TopologicalSort did not write OutNumEdges, or wrote an invalid value"));
1199 while (Stack.Num() > 0)
1228 for (SizeType n = 0; n < NumVertices; ++n)
#define NULL
Definition oodle2base.h:134
constexpr T AlignArbitrary(T Val, uint64 Alignment)
Definition AlignmentTemplates.h:66
#define checkSlow(expr)
Definition AssertionMacros.h:332
#define ensureMsgf( InExpression, InFormat,...)
Definition AssertionMacros.h:465
UE_FORCEINLINE_HINT FLinearColor operator*(float Scalar, const FLinearColor &Color)
Definition Color.h:473
UE_FORCEINLINE_HINT SizeType DefaultCalculateSlackReserve(SizeType NewMax, SIZE_T BytesPerElement, bool bAllowQuantize, uint32 Alignment=DEFAULT_ALIGNMENT)
Definition ContainerAllocationPolicies.h:223
UE_FORCEINLINE_HINT SizeType DefaultCalculateSlackShrink(SizeType NewMax, SizeType CurrentMax, SIZE_T BytesPerElement, bool bAllowQuantize, uint32 Alignment=DEFAULT_ALIGNMENT)
Definition ContainerAllocationPolicies.h:139
UE_FORCEINLINE_HINT SizeType DefaultCalculateSlackGrow(SizeType NewMax, SizeType CurrentMax, SIZE_T BytesPerElement, bool bAllowQuantize, uint32 Alignment=DEFAULT_ALIGNMENT)
Definition ContainerAllocationPolicies.h:169
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE_FORCEINLINE_HINT bool operator!=(const FIndexedPointer &Other) const
Definition LockFreeList.h:76
#define UE_LOG(CategoryName, Verbosity, Format,...)
Definition LogMacros.h:270
const bool
Definition NetworkReplayStreaming.h:178
#define MAX_int32
Definition NumericLimits.h:25
void * GetAllocation(void *Target, uint32 Size, uint32 Offset, uint32 Alignment=16)
Definition OpenGLBuffer.cpp:57
auto GetNum(const TStringConversion< Converter, DefaultConversionSize > &Conversion) -> decltype(Conversion.Length())
Definition StringConv.h:808
auto GetData(const TStringConversion< Converter, DefaultConversionSize > &Conversion) -> decltype(Conversion.Get())
Definition StringConv.h:802
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
uint32 Size
Definition VulkanMemory.cpp:4034
memcpy(InputBufferBase, BinkBlocksData, BinkBlocksSize)
UE_FORCEINLINE_HINT SizeType AddUninitialized()
Definition Array.h:1664
UE_FORCEINLINE_HINT void Reserve(SizeType Number)
Definition Array.h:3016
Definition UnrealString.h.inl:34
Definition ContainerAllocationPolicies.h:1662
Definition ContainerAllocationPolicies.h:894
Definition ContainerAllocationPolicies.h:1383
Definition UniqueLock.h:20
UE_REWRITE bool TopologicalSort(RangeType &&UniqueRange, GetElementDependenciesType GetElementDependencies, ETopologicalSort Flags=ETopologicalSort::None)
Definition TopologicalSort.h:31
GeometryCollection::Facades::FMuscleActivationData Data
Definition MuscleActivationConstraints.h:15
T::FDataType GetValue(const UBlackboardComponent &Blackboard, const FName &Name, FBlackboard::FKey &InOutCachedKey, const typename T::FDataType &DefaultValue)
Definition ValueOrBBKey.h:51
SIZE_T GetAllocatedSize(const T &Value)
Definition ManagedArray.h:93
UE::FRecursiveMutex Mutex
Definition MeshPaintVirtualTexture.cpp:164
bool operator==(const FCachedAssetKey &A, const FCachedAssetKey &B)
Definition AssetDataMap.h:501
implementation
Definition PlayInEditorLoadingScope.h:8
FORCEINLINE FStridedReferenceIterator begin(FStridedReferenceView View)
Definition FastReferenceCollector.h:490
FORCEINLINE FStridedReferenceIterator end(FStridedReferenceView View)
Definition FastReferenceCollector.h:491
FPThreadsRecursiveMutex FPlatformRecursiveMutex
Definition AndroidPlatformMutex.h:12
TSetG< ElementType, KeyType, TDefaultHashTraits< ElementType >, CHeapRawAllocator > TSet
Definition Set.h:87
@ false
Definition radaudio_common.h:23
U16 Index
Definition radfft.cpp:71
static double Seconds()
Definition AndroidPlatformTime.h:20
Definition UnrealMathUtility.h:270
Definition ContainerAllocationPolicies.h:605
Definition NumericLimits.h:41