UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
VVMUniqueCreator.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#if WITH_VERSE_VM || defined(__INTELLISENSE__)
6
7#include "Async/Mutex.h"
8#include "Async/UniqueLock.h"
9#include "VVMContext.h"
10#include "VVMGlobalHeapRoot.h"
11#include "VVMLog.h"
12#include "VVMWriteBarrier.h"
14
15namespace Verse
16{
17// Ensure all created items are unique. If an old one exist then it's reused, otherwise a new is created.
18// Will use a hash table in the future, but for now a simple array and linear search.
19
20template <typename Type>
22{
23private:
25
26 // This should be a hash table
27 static constexpr uint32 MaxSize = 4 * 1024;
29 uint32 ItemsEnd = 0;
30
31 template <typename SubType, typename... ArgumentTypes>
32 Type* LookupLocked(ArgumentTypes... Arguments)
33 {
34 for (uint32 Ix = 0; Ix < ItemsEnd; ++Ix)
35 {
36 if (SubType::Equals(*Items[Ix], Arguments...))
37 {
38 return Items[Ix].Get();
39 }
40 }
41 return nullptr;
42 }
43
45 {
47 Items[ItemsEnd].Set(Context, Item);
48 ItemsEnd++;
49 return Item;
50 }
51
52public:
53 VUniqueCreator() = default;
54
55 template <typename SubType, typename... ArgumentTypes>
56 Type* GetOrCreate(FAllocationContext Context, ArgumentTypes... Arguments)
57 {
58 static_assert(std::is_base_of_v<Type, SubType>);
59 // This lock acquisition is *almost* wrong. If this UniqueCreator ever participated in census or a fixpoint,
60 // then allocation slow paths could call into some GC callback on UniqueCreator, and then that would need to
61 // grab this lock, and we'd die.
62 UE::TUniqueLock Lock(Mutex);
63 if (Type* Item = LookupLocked<SubType, ArgumentTypes...>(Arguments...))
64 {
65 return Item;
66 }
67 Type* Item = SubType::New(Context, Arguments...);
68 return AddLocked(Context, Item);
69 }
70
71 // Only call if certain that Item is unique
72 Type* Add(FAccessContext Context, Type* Item)
73 {
74 UE::TUniqueLock Lock(Mutex);
75 return AddLocked(Context, Item);
76 }
77
78 void Visit(FMarkStackVisitor& Visitor) override { VisitImpl(Visitor); }
79 void Visit(FAbstractVisitor& Visitor) override { VisitImpl(Visitor); }
80
81 template <class VisitorType>
82 void VisitImpl(VisitorType& Visitor)
83 {
84 UE::TUniqueLock Lock(Mutex);
85 Visitor.Visit(Items, ItemsEnd, TEXT("Items"));
86 }
87};
88}; // namespace Verse
89
90#endif // WITH_VERSE_VM
#define TEXT(x)
Definition Platform.h:1272
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
decltype(auto) Visit(Func &&Callable, Variants &&... Args)
Definition TVariant.h:271
FRWLock Lock
Definition UnversionedPropertySerialization.cpp:921
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Mutex.h:18
Definition UniqueLock.h:20
Type
Definition PawnAction_Move.h:11
@ Visitor
Definition XmppMultiUserChat.h:94
UE::FRecursiveMutex Mutex
Definition MeshPaintVirtualTexture.cpp:164
decltype(auto) VisitImpl(SIZE_T EncodedIndex, Func &&Callable, TIntegerSequence< SIZE_T, EncodedIndices... > &&, TIntegerSequence< SIZE_T, VariantIndices... > &&VariantIndicesSeq, Variants &&... Args)
Definition TVariantMeta.h:377
Definition Archive.h:36