14#define LOG_EVERY_ALLOCATION 0
15#define DUMP_ALLOC_FREQUENCY 0
17#define VALIDATE_SYNC_SIZE 0
18#define VALIDATE_MOVES 0
19#define TRACK_RELOCATIONS (VALIDATE_SYNC_SIZE || VALIDATE_MOVES)
20#define VALIDATE_MEMORY_PROTECTION 0
22#define USE_ALLOCATORFIXEDSIZEFREELIST 0
98 uint32 ElementType = Element.Type;
328 return B->Base -
A->Base;
331#if USE_ALLOCATORFIXEDSIZEFREELIST
333 void*
operator new(
size_t Size);
334 void operator delete(
void *RawMemory);
477 virtual void Free(
void* Pointer);
484 virtual void Lock(
const void* Pointer);
491 virtual void Unlock(
const void* Pointer);
667 return !(
UPTRINT(Ptr) & (Alignment - 1));
683 const uint8* NewBase;
693#if VALIDATE_MEMORY_PROTECTION
705 const void* UserPayload;
807#if VALIDATE_MEMORY_PROTECTION
867 FMemoryChunk*
Grow(FMemoryChunk* Chunk,
int64 GrowAmount);
983 Chunk->LinkFree(
nullptr);
1117#if VALIDATE_MEMORY_PROTECTION
1162 , NewAddress(nullptr)
1165 , int32ernalRequestStatus(1)
1167 , bIsCanceled(
false)
1168 , MemoryChunk(nullptr)
1181 return OldAddress ==
nullptr && OldSize == 0;
1187 return OldAddress !=
nullptr;
1199 bool bHasCompleted = int32ernalRequestStatus.
GetValue() == 0;
1200 check(!bHasCompleted || NewAddress || bIsCanceled);
1201 return bHasCompleted;
1207 return NewAddress ?
true :
false;
1236 void MarkCompleted()
1240 if (ExternalRequestStatus)
1288 if (Chunk->IsLocked())
1310 check(Request->HasStarted());
1311 if (!Request->HasCompleted())
constexpr T Align(T Val, uint64 Alignment)
Definition AlignmentTemplates.h:18
#define check(expr)
Definition AssertionMacros.h:314
void * DestAddr
Definition BlueprintTypeConversions.cpp:389
#define GLog
Definition CoreGlobals.h:95
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
return true
Definition ExternalRpcRegistry.cpp:601
#define LLM_IF_ENABLED(...)
Definition LowLevelMemTracker.h:1093
void MemoryTrace_ReallocAlloc(uint64 Address, uint64 NewSize, uint32 Alignment, HeapId RootHeap=EMemoryTraceRootHeap::SystemMemory, uint32 ExternalCallstackId=0)
Definition MemoryTrace.h:202
void MemoryTrace_ReallocFree(uint64 Address, HeapId RootHeap=EMemoryTraceRootHeap::SystemMemory, uint32 ExternalCallstackId=0)
Definition MemoryTrace.h:201
#define MAX_int64
Definition NumericLimits.h:26
FRWLock Lock
Definition UnversionedPropertySerialization.cpp:921
uint32 Size
Definition VulkanMemory.cpp:4034
Definition Archive.h:1208
Definition GPUDefragAllocator.h:1151
bool HasCompleted() const
Definition GPUDefragAllocator.h:1197
bool HasStarted() const
Definition GPUDefragAllocator.h:1205
bool IsAllocation() const
Definition GPUDefragAllocator.h:1179
int32 GetNewSize() const
Definition GPUDefragAllocator.h:1223
FAsyncReallocationRequest(void *InCurrentBaseAddress, int32 InNewSize, FThreadSafeCounter *InRequestStatus)
Definition GPUDefragAllocator.h:1160
void * GetNewBaseAddress() const
Definition GPUDefragAllocator.h:1217
~FAsyncReallocationRequest()
Definition GPUDefragAllocator.h:1173
void * GetOldBaseAddress() const
Definition GPUDefragAllocator.h:1211
bool IsCanceled() const
Definition GPUDefragAllocator.h:1191
bool IsReallocation() const
Definition GPUDefragAllocator.h:1185
Definition IConsoleManager.h:1580
Definition GPUDefragAllocator.h:143
uint32 GetSyncIndex() const
Definition GPUDefragAllocator.h:318
void SetSyncIndex(uint32 InSyncIndex, int64 InSyncSize)
Definition GPUDefragAllocator.h:309
int64 OrigSize
Definition GPUDefragAllocator.h:341
FMemoryChunk(uint8 *InBase, int64 InSize, FGPUDefragAllocator &InBestFitAllocator, FMemoryChunk *&ChunkToInsertAfter, TStatId InStat)
Definition GPUDefragAllocator.h:153
int64 Size
Definition GPUDefragAllocator.h:340
int32 LockCount
Definition GPUDefragAllocator.h:345
bool IsLocked() const
Definition GPUDefragAllocator.h:183
void Link(FMemoryChunk *&ChunkToInsertAfter)
Definition GPUDefragAllocator.h:193
FMemoryChunk * PreviousChunk
Definition GPUDefragAllocator.h:352
uint32 SyncIndex
Definition GPUDefragAllocator.h:361
bool IsRelocating() const
Definition GPUDefragAllocator.h:279
uint16 DefragCounter
Definition GPUDefragAllocator.h:347
int64 GetFinalSize() const
Definition GPUDefragAllocator.h:1274
~FMemoryChunk()
Definition GPUDefragAllocator.h:175
bool bTail
Definition GPUDefragAllocator.h:370
int64 GetAvailableSize() const
Definition GPUDefragAllocator.h:287
FMemoryChunk * NextChunk
Definition GPUDefragAllocator.h:354
uint8 * Base
Definition GPUDefragAllocator.h:338
int64 SyncSize
Definition GPUDefragAllocator.h:364
FMemoryChunk * NextFreeChunk
Definition GPUDefragAllocator.h:359
void Unlink()
Definition GPUDefragAllocator.h:226
bool bIsAvailable
Definition GPUDefragAllocator.h:343
TStatId Stat
Definition GPUDefragAllocator.h:369
FGPUDefragAllocator & BestFitAllocator
Definition GPUDefragAllocator.h:350
void LinkFree(FMemoryChunk *FirstFreeChunkToSearch)
Definition GPUDefragAllocator.cpp:71
void UnlinkFree()
Definition GPUDefragAllocator.h:253
FMemoryChunk * PreviousFreeChunk
Definition GPUDefragAllocator.h:357
static uint64 Compare(const FMemoryChunk *A, const FMemoryChunk *B)
Definition GPUDefragAllocator.h:326
void * UserPayload
Definition GPUDefragAllocator.h:366
Definition GPUDefragAllocator.h:43
void Split(FMemoryChunk *BaseChunk, int64 FirstSize)
Definition GPUDefragAllocator.h:920
EMemoryElementType
Definition GPUDefragAllocator.h:70
@ MET_Resized
Definition GPUDefragAllocator.h:76
@ MET_Locked
Definition GPUDefragAllocator.h:73
@ MET_Max
Definition GPUDefragAllocator.h:77
@ MET_Relocating
Definition GPUDefragAllocator.h:74
@ MET_Free
Definition GPUDefragAllocator.h:72
@ MET_Resizing
Definition GPUDefragAllocator.h:75
@ MET_Allocated
Definition GPUDefragAllocator.h:71
volatile memsize_t AllocatedMemorySize
Definition GPUDefragAllocator.h:1069
int64 GetAllocatedSize(void *Pointer)
Definition GPUDefragAllocator.cpp:475
void GetSettings(FSettings &OutSettings)
Definition GPUDefragAllocator.h:440
int32 GetAllocationAlignment() const
Definition GPUDefragAllocator.h:670
int32 NumLockedChunks
Definition GPUDefragAllocator.h:1115
FMemoryChunk * FindAny(FMemoryChunk *FreeChunk)
Definition GPUDefragAllocator.cpp:826
TDoubleLinkedList< FAsyncReallocationRequest * >::TDoubleLinkedListNode FRequestNode
Definition GPUDefragAllocator.h:47
void BlockOnFence()
Definition GPUDefragAllocator.cpp:1210
bool InBenchmarkMode() const
Definition GPUDefragAllocator.h:588
void FullDefragmentation(FRelocationStats &Stats)
Definition GPUDefragAllocator.cpp:1458
void BlockOnAsyncReallocation(FAsyncReallocationRequest *Request)
Definition GPUDefragAllocator.h:1308
int32 NumFinishedAsyncReallocations
Definition GPUDefragAllocator.h:1107
static bool IsAligned(const volatile void *Ptr, const uint32 Alignment)
Definition GPUDefragAllocator.h:665
uint64 CurrentSyncIndex
Definition GPUDefragAllocator.h:1082
FMemoryChunk * FirstFreeChunk
Definition GPUDefragAllocator.h:1061
FCriticalSection SynchronizationObject
Definition GPUDefragAllocator.h:1048
void GetMemoryLayout(TArray< FMemoryLayoutElement > &MemoryLayout)
Definition GPUDefragAllocator.cpp:1741
FMemoryChunk * RelocateIntoFreeChunk(FRelocationStats &Stats, FMemoryChunk *FreeChunk, FMemoryChunk *UsedChunk)
Definition GPUDefragAllocator.cpp:936
bool CanRelocate(const FMemoryChunk *Chunk) const
Definition GPUDefragAllocator.h:1286
void Benchmark(int32 MinChunkSize, int32 MaxChunkSize, float FreeRatio, float LockRatio, bool bFullDefrag, bool bSaveImages, const TCHAR *Filename)
Definition GPUDefragAllocator.cpp:1926
uint64 MemorySize
Definition GPUDefragAllocator.h:1051
uint8 * MemoryBase
Definition GPUDefragAllocator.h:1053
virtual void PlatformNotifyReallocationFinished(FAsyncReallocationRequest *FinishedRequest, void *UserPayload)=0
void * Reallocate(void *OldBaseAddress, int64 NewSize)
Definition GPUDefragAllocator.cpp:489
EMemoryElementType GetChunkType(FMemoryChunk *Chunk) const
Definition GPUDefragAllocator.cpp:1753
FMemoryChunk * AllocateChunk(FMemoryChunk *FreeChunk, int64 AllocationSize, bool bAsync, bool bDoValidation=true)
Definition GPUDefragAllocator.cpp:239
void BlockOnSyncIndex(uint32 SyncIndex)
Definition GPUDefragAllocator.cpp:1234
int64 MinLargestHole
Definition GPUDefragAllocator.h:1103
bool bBenchmarkMode
Definition GPUDefragAllocator.h:1122
uint32 GetBlockedCycles() const
Definition GPUDefragAllocator.h:580
virtual void Unlock(const void *Pointer)
Definition GPUDefragAllocator.cpp:420
uint32 BlockedCycles
Definition GPUDefragAllocator.h:1113
int32 AllocationAlignment
Definition GPUDefragAllocator.h:1055
bool GetTextureMemoryVisualizeData(FColor *TextureData, int32 SizeX, int32 SizeY, int32 Pitch, const int32 PixelSize)
Definition GPUDefragAllocator.cpp:1686
FMemoryChunk * FindAdjacentToHole(FMemoryChunk *FreeChunk)
Definition GPUDefragAllocator.cpp:789
void SortFreeList(int32 &NumFreeChunks, int64 &LargestFreeChunk)
Definition GPUDefragAllocator.cpp:706
double TimeSpentInAllocator
Definition GPUDefragAllocator.h:1063
bool IsInitialized()
Definition GPUDefragAllocator.h:458
FGPUDefragAllocator()
Definition GPUDefragAllocator.h:374
virtual void GetMemoryStats(int64 &OutAllocatedMemorySize, int64 &OutAvailableMemorySize, int64 &OutPendingMemoryAdjustment, int64 &OutPaddingWasteSize)
Definition GPUDefragAllocator.h:556
virtual ~FGPUDefragAllocator()
Definition GPUDefragAllocator.h:404
volatile memsize_t AvailableMemorySize
Definition GPUDefragAllocator.h:1071
int64 GetTotalSize() const
Definition GPUDefragAllocator.h:564
FMemoryChunk * FindAdjacent(FMemoryChunk *UsedChunk, bool bAnyChunkType)
Definition GPUDefragAllocator.cpp:753
void SetUserPayload(const void *Pointer, void *UserPayload)
Definition GPUDefragAllocator.cpp:436
uint64 CurrentLargestHole
Definition GPUDefragAllocator.h:1094
virtual void PlatformRelocate(void *Dest, const void *Source, int64 Size, void *UserPayload)=0
void LinkFreeChunk(FMemoryChunk *Chunk)
Definition GPUDefragAllocator.h:979
uint64 PlatformSyncFence
Definition GPUDefragAllocator.h:1089
void InsertFence()
Definition GPUDefragAllocator.cpp:1181
FSettings Settings
Definition GPUDefragAllocator.h:1079
FMemoryChunk * LastChunk
Definition GPUDefragAllocator.h:1059
TDoubleLinkedList< FAsyncReallocationRequest * > FRequestList
Definition GPUDefragAllocator.h:46
bool FinishAllRelocations()
Definition GPUDefragAllocator.cpp:1154
virtual uint64 PlatformInsertFence()=0
volatile memsize_t PendingMemoryAdjustment
Definition GPUDefragAllocator.h:1073
uint64 TotalNumRelocations
Definition GPUDefragAllocator.h:1099
void CancelAsyncReallocation(FAsyncReallocationRequest *Request, const void *CurrentBaseAddress)
void FreeChunk(FMemoryChunk *Chunk)
Definition GPUDefragAllocator.cpp:319
int64 memsize_t
Definition GPUDefragAllocator.h:1065
bool IsValidPoolMemory(const void *Pointer) const
Definition GPUDefragAllocator.h:527
int32 NumFinishedAsyncAllocations
Definition GPUDefragAllocator.h:1109
void Coalesce(FMemoryChunk *FreedChunk)
Definition GPUDefragAllocator.cpp:1842
FMemoryChunk * RelocateAllowed(FMemoryChunk *FreeChunk, FMemoryChunk *UsedChunk)
Definition GPUDefragAllocator.cpp:1282
TDoubleLinkedList< FMemoryChunk * > PendingFreeChunks
Definition GPUDefragAllocator.h:1092
friend FScopedGPUDefragLock
Definition GPUDefragAllocator.h:1124
volatile memsize_t PaddingWasteSize
Definition GPUDefragAllocator.h:1067
int32 GetLargestAvailableAllocation(int32 *OutNumFreeChunks=nullptr)
Definition GPUDefragAllocator.cpp:1785
void Relocate(FRelocationStats &Stats, FMemoryChunk *Dest, int64 DestOffset, const void *Source, int64 Size, void *UserPayload)
Definition GPUDefragAllocator.h:794
void PartialDefragmentationFast(FRelocationStats &Stats, double StartTime)
Definition GPUDefragAllocator.cpp:1297
void CheckForErrors(bool bCheckSortedFreeList)
Definition GPUDefragAllocator.cpp:865
TMap< void *, FMemoryChunk * > PointerToChunkMap
Definition GPUDefragAllocator.h:1076
virtual void PlatformBlockOnFence(uint64 Fence)=0
void DumpAllocs(FOutputDevice &Ar= *GLog)
Definition GPUDefragAllocator.cpp:1585
virtual bool PlatformCanRelocate(const void *Source, void *UserPayload) const =0
FMemoryChunk * FirstChunk
Definition GPUDefragAllocator.h:1057
int32 NumCanceledAsyncRequests
Definition GPUDefragAllocator.h:1111
uint32 GetCurrentSyncIndex() const
Definition GPUDefragAllocator.h:822
uint64 TotalNumBytesRelocated
Definition GPUDefragAllocator.h:1101
int32 MaxNumHoles
Definition GPUDefragAllocator.h:1105
int32 CurrentNumHoles
Definition GPUDefragAllocator.h:1095
virtual void Initialize(uint8 *InMemoryBase, int64 InMemorySize, int32 InAllocationAlignment)
Definition GPUDefragAllocator.h:415
void PartialDefragmentationDownshift(FRelocationStats &Stats, double StartTime)
Definition GPUDefragAllocator.cpp:1418
void DefragmentMemory(FRelocationStats &Stats)
Definition GPUDefragAllocator.cpp:1809
virtual void * Allocate(int64 AllocationSize, int32 Alignment, TStatId InStat, bool bAllowFailure)
Definition GPUDefragAllocator.cpp:138
void PartialDefragmentationSlow(FRelocationStats &Stats, double StartTime)
Definition GPUDefragAllocator.cpp:1356
uint64 CompletedSyncIndex
Definition GPUDefragAllocator.h:1084
void SetSettings(const FSettings &InSettings)
Definition GPUDefragAllocator.h:450
void * GetUserPayload(const void *Pointer)
Definition GPUDefragAllocator.cpp:457
int32 NumRelocationsInProgress
Definition GPUDefragAllocator.h:1087
virtual void Initialize(uint8 *InMemoryBase, int64 InMemorySize)
Definition GPUDefragAllocator.h:430
Definition OutputDevice.h:133
Definition GPUDefragAllocator.h:1129
~FScopedGPUDefragLock()
Definition GPUDefragAllocator.h:1137
FScopedGPUDefragLock(FGPUDefragAllocator &InDefragAllocator)
Definition GPUDefragAllocator.h:1131
Definition ThreadSafeCounter.h:14
int32 Decrement()
Definition ThreadSafeCounter.h:75
int32 GetValue() const
Definition ThreadSafeCounter.h:120
Definition UnrealString.h.inl:34
@ false
Definition radaudio_common.h:23
Definition GPUDefragAllocator.h:81
FMemoryLayoutElement()
Definition GPUDefragAllocator.h:82
int32 Size
Definition GPUDefragAllocator.h:92
EMemoryElementType Type
Definition GPUDefragAllocator.h:93
FMemoryLayoutElement(int32 InSize, EMemoryElementType InType)
Definition GPUDefragAllocator.h:87
friend FArchive & operator<<(FArchive &Ar, FMemoryLayoutElement &Element)
Definition GPUDefragAllocator.h:95
Definition GPUDefragAllocator.h:109
int64 NumBytesRelocated
Definition GPUDefragAllocator.h:121
int64 LargestHoleSize
Definition GPUDefragAllocator.h:127
int64 NumBytesDownShifted
Definition GPUDefragAllocator.h:124
int32 NumRelocations
Definition GPUDefragAllocator.h:130
FRelocationStats()
Definition GPUDefragAllocator.h:110
int32 NumHoles
Definition GPUDefragAllocator.h:133
int32 NumLockedChunks
Definition GPUDefragAllocator.h:136
Definition GPUDefragAllocator.h:53
int32 MaxDefragDownShift
Definition GPUDefragAllocator.h:64
int32 OverlappedBandwidthScale
Definition GPUDefragAllocator.h:66
int32 MaxDefragRelocations
Definition GPUDefragAllocator.h:62
FSettings()
Definition GPUDefragAllocator.h:54
static UE_FORCEINLINE_HINT void * Memcpy(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:160
Definition LightweightStats.h:416