UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
FGPUDefragAllocator Class Referenceabstract

#include <GPUDefragAllocator.h>

Classes

class  FMemoryChunk
 
struct  FMemoryLayoutElement
 
struct  FRelocationStats
 
struct  FSettings
 

Public Types

enum  EMemoryElementType {
  MET_Allocated , MET_Free , MET_Locked , MET_Relocating ,
  MET_Resizing , MET_Resized , MET_Max
}
 
typedef TDoubleLinkedList< FAsyncReallocationRequest * > FRequestList
 
typedef TDoubleLinkedList< FAsyncReallocationRequest * >::TDoubleLinkedListNode FRequestNode
 

Public Member Functions

 FGPUDefragAllocator ()
 
virtual ~FGPUDefragAllocator ()
 
virtual void Initialize (uint8 *InMemoryBase, int64 InMemorySize, int32 InAllocationAlignment)
 
virtual void Initialize (uint8 *InMemoryBase, int64 InMemorySize)
 
void GetSettings (FSettings &OutSettings)
 
void SetSettings (const FSettings &InSettings)
 
bool IsInitialized ()
 
virtual voidAllocate (int64 AllocationSize, int32 Alignment, TStatId InStat, bool bAllowFailure)
 
virtual void Free (void *Pointer)
 
virtual void Lock (const void *Pointer)
 
virtual void Unlock (const void *Pointer)
 
void SetUserPayload (const void *Pointer, void *UserPayload)
 
voidGetUserPayload (const void *Pointer)
 
int64 GetAllocatedSize (void *Pointer)
 
voidReallocate (void *OldBaseAddress, int64 NewSize)
 
bool IsValidPoolMemory (const void *Pointer) const
 
void DumpAllocs (FOutputDevice &Ar= *GLog)
 
virtual void GetMemoryStats (int64 &OutAllocatedMemorySize, int64 &OutAvailableMemorySize, int64 &OutPendingMemoryAdjustment, int64 &OutPaddingWasteSize)
 
int64 GetTotalSize () const
 
int32 GetLargestAvailableAllocation (int32 *OutNumFreeChunks=nullptr)
 
uint32 GetBlockedCycles () const
 
bool InBenchmarkMode () const
 
bool GetTextureMemoryVisualizeData (FColor *TextureData, int32 SizeX, int32 SizeY, int32 Pitch, const int32 PixelSize)
 
void GetMemoryLayout (TArray< FMemoryLayoutElement > &MemoryLayout)
 
EMemoryElementType GetChunkType (FMemoryChunk *Chunk) const
 
void DefragmentMemory (FRelocationStats &Stats)
 
virtual int32 Tick (FRelocationStats &Stats, bool bPanicDefrag)
 
bool FinishAllRelocations ()
 
void BlockOnAsyncReallocation (FAsyncReallocationRequest *Request)
 
void CancelAsyncReallocation (FAsyncReallocationRequest *Request, const void *CurrentBaseAddress)
 
void Benchmark (int32 MinChunkSize, int32 MaxChunkSize, float FreeRatio, float LockRatio, bool bFullDefrag, bool bSaveImages, const TCHAR *Filename)
 
int32 GetAllocationAlignment () const
 

Static Public Member Functions

static bool IsAligned (const volatile void *Ptr, const uint32 Alignment)
 

Protected Types

typedef int64 memsize_t
 

Protected Member Functions

virtual void PlatformRelocate (void *Dest, const void *Source, int64 Size, void *UserPayload)=0
 
virtual uint64 PlatformInsertFence ()=0
 
virtual void PlatformBlockOnFence (uint64 Fence)=0
 
virtual bool PlatformCanRelocate (const void *Source, void *UserPayload) const =0
 
virtual void PlatformNotifyReallocationFinished (FAsyncReallocationRequest *FinishedRequest, void *UserPayload)=0
 
void Relocate (FRelocationStats &Stats, FMemoryChunk *Dest, int64 DestOffset, const void *Source, int64 Size, void *UserPayload)
 
uint32 GetCurrentSyncIndex () const
 
void PartialDefragmentationFast (FRelocationStats &Stats, double StartTime)
 
void PartialDefragmentationSlow (FRelocationStats &Stats, double StartTime)
 
void PartialDefragmentationDownshift (FRelocationStats &Stats, double StartTime)
 
void FullDefragmentation (FRelocationStats &Stats)
 
FMemoryChunkGrow (FMemoryChunk *Chunk, int64 GrowAmount)
 
FMemoryChunkShrink (FMemoryChunk *Chunk, int64 ShrinkAmount)
 
void CheckForErrors (bool bCheckSortedFreeList)
 
bool CanRelocate (const FMemoryChunk *Chunk) const
 
void InsertFence ()
 
void BlockOnFence ()
 
void BlockOnSyncIndex (uint32 SyncIndex)
 
void Split (FMemoryChunk *BaseChunk, int64 FirstSize)
 
FMemoryChunkAllocateChunk (FMemoryChunk *FreeChunk, int64 AllocationSize, bool bAsync, bool bDoValidation=true)
 
void FreeChunk (FMemoryChunk *Chunk)
 
void LinkFreeChunk (FMemoryChunk *Chunk)
 
void Coalesce (FMemoryChunk *FreedChunk)
 
void SortFreeList (int32 &NumFreeChunks, int64 &LargestFreeChunk)
 
FMemoryChunkFindAdjacent (FMemoryChunk *UsedChunk, bool bAnyChunkType)
 
FMemoryChunkFindAdjacentToHole (FMemoryChunk *FreeChunk)
 
FMemoryChunkFindAny (FMemoryChunk *FreeChunk)
 
FMemoryChunkRelocateIntoFreeChunk (FRelocationStats &Stats, FMemoryChunk *FreeChunk, FMemoryChunk *UsedChunk)
 
FMemoryChunkRelocateAllowed (FMemoryChunk *FreeChunk, FMemoryChunk *UsedChunk)
 

Protected Attributes

FCriticalSection SynchronizationObject
 
uint64 MemorySize
 
uint8MemoryBase
 
int32 AllocationAlignment
 
FMemoryChunkFirstChunk
 
FMemoryChunkLastChunk
 
FMemoryChunkFirstFreeChunk
 
double TimeSpentInAllocator
 
volatile memsize_t PaddingWasteSize
 
volatile memsize_t AllocatedMemorySize
 
volatile memsize_t AvailableMemorySize
 
volatile memsize_t PendingMemoryAdjustment
 
TMap< void *, FMemoryChunk * > PointerToChunkMap
 
FSettings Settings
 
uint64 CurrentSyncIndex
 
uint64 CompletedSyncIndex
 
int32 NumRelocationsInProgress
 
uint64 PlatformSyncFence
 
TDoubleLinkedList< FMemoryChunk * > PendingFreeChunks
 
uint64 CurrentLargestHole
 
int32 CurrentNumHoles
 
uint64 TotalNumRelocations
 
uint64 TotalNumBytesRelocated
 
int64 MinLargestHole
 
int32 MaxNumHoles
 
int32 NumFinishedAsyncReallocations
 
int32 NumFinishedAsyncAllocations
 
int32 NumCanceledAsyncRequests
 
uint32 BlockedCycles
 
int32 NumLockedChunks
 
bool bBenchmarkMode
 
friend FScopedGPUDefragLock
 

Detailed Description

Simple best fit allocator, splitting and coalescing whenever/ wherever possible. NOT THREAD-SAFE.

  • uses TMap to find memory chunk given a Pointer (potentially colliding with malloc/ free from main thread)
  • uses separate linked list for free allocations, assuming that relatively few free chunks due to coalescing

Member Typedef Documentation

◆ FRequestList

◆ FRequestNode

◆ memsize_t

Allocated memory in uint8s.

Member Enumeration Documentation

◆ EMemoryElementType

Enumerator
MET_Allocated 
MET_Free 
MET_Locked 
MET_Relocating 
MET_Resizing 
MET_Resized 
MET_Max 

Constructor & Destructor Documentation

◆ FGPUDefragAllocator()

FGPUDefragAllocator::FGPUDefragAllocator ( )
inline

Constructor, zero initializing all member variables

◆ ~FGPUDefragAllocator()

virtual FGPUDefragAllocator::~FGPUDefragAllocator ( )
inlinevirtual

Member Function Documentation

◆ Allocate()

void * FGPUDefragAllocator::Allocate ( int64  AllocationSize,
int32  Alignment,
TStatId  InStat,
bool  bAllowFailure 
)
virtual

Allocate physical memory.

Parameters
AllocationSizeSize of allocation
bAllowFailureWhether to allow allocation failure or not
Returns
Pointer to allocated memory

◆ AllocateChunk()

FGPUDefragAllocator::FMemoryChunk * FGPUDefragAllocator::AllocateChunk ( FMemoryChunk FreeChunk,
int64  AllocationSize,
bool  bAsync,
bool  bDoValidation = true 
)
protected

Marks the specified chunk as 'allocated' and updates tracking variables. Splits the chunk if only a portion of it is allocated.

Parameters
FreeChunkChunk to allocate
AllocationSizeNumber of uint8s to allocate
bAsyncIf true, allows allocating from relocating chunks and maint32ains the free-list sort order.
Returns
The memory chunk that was allocated (the original chunk could've been split).

Marks the specified chunk as 'allocated' and updates tracking variables. Splits the chunk if only a portion of it is allocated.

Parameters
FreeChunkChunk to allocate
AllocationSizeNumber of bytes to allocate
bAsyncIf true, allows allocating from relocating chunks and maintains the free-list sort order.
Returns
The memory chunk that was allocated (the original chunk could've been split).

◆ Benchmark()

void FGPUDefragAllocator::Benchmark ( int32  MinChunkSize,
int32  MaxChunkSize,
float  FreeRatio,
float  LockRatio,
bool  bFullDefrag,
bool  bSaveImages,
const TCHAR Filename 
)

Performs a benchmark of the allocator and outputs the result to the log.

Parameters
MinChunkSizeMinimum number of uint8s per random chunk
MaxChunkSizeMaximum number of uint8s per random chunk
FreeRatioFree 0.0-1.0 of the memory before benchmarking
LockRatioLock 0.0-1.0 % of the memory before benchmarking
bFullDefragWhether to test full defrag (true) or continuous defrag (false)
bSaveImagesWhether to save before/after images to hard disk (TexturePoolBenchmark-*.bmp)
Filename[opt] Filename to a previously saved memory layout to use for benchmarking, or nullptr

Performs a benchmark of the allocator and outputs the result to the log.

Parameters
MinChunkSizeMinimum number of bytes per random chunk
MaxChunkSizeMaximum number of bytes per random chunk
FreeRatioFree 0.0-1.0 % of the memory before benchmarking
LockRatioLock 0.0-1.0 % of the memory before benchmarking
bFullDefragWhether to test full defrag (true) or continuous defrag (false)
bSaveImagesWhether to save before/after images to hard disk (TexturePoolBenchmark-*.bmp)
Filename[opt] Filename to a previously saved memory layout to use for benchmarking, or nullptr

◆ BlockOnAsyncReallocation()

void FGPUDefragAllocator::BlockOnAsyncReallocation ( FAsyncReallocationRequest Request)
inline

Blocks the calling thread until the specified request has been completed.

Parameters
RequestRequest to wait for. Must be a valid request.

◆ BlockOnFence()

void FGPUDefragAllocator::BlockOnFence ( )
protected

Blocks the calling thread until the current sync fence has been completed.

◆ BlockOnSyncIndex()

void FGPUDefragAllocator::BlockOnSyncIndex ( uint32  SyncIndex)
protected

Blocks the calling thread until the specified sync index has been completed.

Parameters
SyncIndexSync index to wait for

◆ CancelAsyncReallocation()

void FGPUDefragAllocator::CancelAsyncReallocation ( FAsyncReallocationRequest Request,
const void CurrentBaseAddress 
)

Cancels the specified reallocation request. Note that the allocator doesn't keep track of requests after it's been completed, so the user must provide the current base address. This may not match any of the addresses in the (old) request since the memory may have been relocated since then.

Parameters
RequestRequest to cancel. Must be a valid request.
CurrentBaseAddressCurrent baseaddress used by the allocation.

◆ CanRelocate()

bool FGPUDefragAllocator::CanRelocate ( const FMemoryChunk Chunk) const
inlineprotected

Returns true if the specified chunk is allowed to relocate at this time. Will also call PlatformCanRelocate().

Parameters
ChunkChunk to check
Returns
true if the allocation can be relocated at this time

◆ CheckForErrors()

void FGPUDefragAllocator::CheckForErrors ( bool  bCheckSortedFreeList)
protected

Checks the int32ernal state for errors. (Slow)

Parameters
bCheckSortedFreeListIf true, also checks that the freelist is sorted

Checks the internal state for errors. (Slow)

Parameters
bCheckSortedFreeListIf true, also checks that the freelist is sorted

◆ Coalesce()

void FGPUDefragAllocator::Coalesce ( FMemoryChunk FreedChunk)
protected

Merges any adjacent free chunks into the specified free chunk. Doesn't affect the free-list sort order.

Parameters
FreedChunkChunk that just became available.

◆ DefragmentMemory()

void FGPUDefragAllocator::DefragmentMemory ( FRelocationStats Stats)

Fully defragments the memory and blocks until it's done.

Parameters
Stats[out] Stats

◆ DumpAllocs()

void FGPUDefragAllocator::DumpAllocs ( FOutputDevice Ar = *GLog)

Dump allocation information.

◆ FindAdjacent()

FGPUDefragAllocator::FMemoryChunk * FGPUDefragAllocator::FindAdjacent ( FMemoryChunk UsedChunk,
bool  bAnyChunkType 
)
protected

Defrag helper function. Checks if the specified allocation fits within the adjacent free chunk(s).

Parameters
UsedChunkAllocated chunk to check for a fit
bAnyChunkTypeIf false, only succeeds if 'UsedChunk' has a reallocation request and fits
Returns
Returns 'UsedChunk' if it fits the criteria, otherwise nullptr

◆ FindAdjacentToHole()

FGPUDefragAllocator::FMemoryChunk * FGPUDefragAllocator::FindAdjacentToHole ( FMemoryChunk FreeChunk)
protected

Searches for an allocated chunk that would fit within the specified free chunk. The allocated chunk must be adjacent to a free chunk and have a larger base address than 'FreeChunk'. Starts searching from the end of the texture pool.

Parameters
FreeChunkFree chunk we're trying to fill up
Returns
Pointer to a suitable chunk, or nullptr

◆ FindAny()

FGPUDefragAllocator::FMemoryChunk * FGPUDefragAllocator::FindAny ( FMemoryChunk FreeChunk)
protected

Searches for an allocated chunk that would fit within the specified free chunk. Any chunk that fits and has a larger base address than 'FreeChunk' is accepted. Starts searching from the end of the texture pool.

Parameters
FreeChunkFree chunk we're trying to fill up
Returns
Pointer to a suitable chunk, or nullptr

◆ FinishAllRelocations()

bool FGPUDefragAllocator::FinishAllRelocations ( )

Blocks the calling thread until all relocations and reallocations that were initiated by Tick() have completed.

Returns
true if there were any relocations in progress before this call

◆ Free()

void FGPUDefragAllocator::Free ( void Pointer)
virtual

Frees allocation associated with the specified Pointer.

Parameters
PointerPointer to free.

Frees allocation associated with passed in pointer.

Parameters
PointerPointer to free.

◆ FreeChunk()

void FGPUDefragAllocator::FreeChunk ( FMemoryChunk Chunk)
protected

Marks the specified chunk as 'free' and updates tracking variables. Calls LinkFreeChunk() to coalesce adjacent free memory.

Parameters
ChunkChunk to free

Marks the specified chunk as 'free' and updates tracking variables. Calls LinkFreeChunk() to coalesce adjacent free memory.

Parameters
ChunkChunk to free
bMaintainSortedFreelistIf true, maintains the free-list sort order

◆ FullDefragmentation()

void FGPUDefragAllocator::FullDefragmentation ( FRelocationStats Stats)
protected

Performs a full defrag and ignores all reallocation requests.

Parameters
Stats[out] Stats

◆ GetAllocatedSize()

int64 FGPUDefragAllocator::GetAllocatedSize ( void Pointer)

Returns the amount of memory allocated for the specified address.

Parameters
PointerPointer to check.
Returns
Number of uint8s allocated

Returns the amount of memory allocated for the specified address.

Parameters
PointerPointer to check.
Returns
Number of bytes allocated

◆ GetAllocationAlignment()

int32 FGPUDefragAllocator::GetAllocationAlignment ( ) const
inline

◆ GetBlockedCycles()

uint32 FGPUDefragAllocator::GetBlockedCycles ( ) const
inline

Returns the amount of time blocked by a platform fence since the beginning of the last call to Tick(), in appCycles.

◆ GetChunkType()

FGPUDefragAllocator::EMemoryElementType FGPUDefragAllocator::GetChunkType ( FMemoryChunk Chunk) const

◆ GetCurrentSyncIndex()

uint32 FGPUDefragAllocator::GetCurrentSyncIndex ( ) const
inlineprotected

Returns the sync index to be completed by the next call to FinishAllRelocations().

◆ GetLargestAvailableAllocation()

int32 FGPUDefragAllocator::GetLargestAvailableAllocation ( int32 OutNumFreeChunks = nullptr)

Scans the free chunks and returns the largest size you can allocate.

Parameters
OutNumFreeChunksUpon return, contains the total number of free chunks. May be nullptr.
Returns
The largest size of all free chunks.

◆ GetMemoryLayout()

void FGPUDefragAllocator::GetMemoryLayout ( TArray< FMemoryLayoutElement > &  MemoryLayout)

◆ GetMemoryStats()

virtual void FGPUDefragAllocator::GetMemoryStats ( int64 OutAllocatedMemorySize,
int64 OutAvailableMemorySize,
int64 OutPendingMemoryAdjustment,
int64 OutPaddingWasteSize 
)
inlinevirtual

Retrieves allocation stats.

Parameters
OutAllocatedMemorySize[out] Size of allocated memory
OutAvailableMemorySize[out] Size of available memory
OutPendingMemoryAdjustment[out] Size of pending allocation change (due to async reallocation)

◆ GetSettings()

void FGPUDefragAllocator::GetSettings ( FSettings OutSettings)
inline

Returns the current allocator settings.

Parameters
OutSettings[out] Current allocator settings

◆ GetTextureMemoryVisualizeData()

bool FGPUDefragAllocator::GetTextureMemoryVisualizeData ( FColor TextureData,
int32  SizeX,
int32  SizeY,
int32  Pitch,
const int32  PixelSize 
)

Fills a texture with to visualize the texture pool memory.

Parameters
TextureDataStart address
SizeXNumber of pixels along X
SizeYNumber of pixels along Y
PitchNumber of uint8s between each row
PixelSizeNumber of uint8s each pixel represents
Returns
true if successful, false otherwise

Fills a texture with to visualize the texture pool memory.

Parameters
TextureDataStart address
SizeXNumber of pixels along X
SizeYNumber of pixels along Y
PitchNumber of bytes between each row
PixelSizeNumber of bytes each pixel represents
Returns
true if successful, false otherwise

◆ GetTotalSize()

int64 FGPUDefragAllocator::GetTotalSize ( ) const
inline

◆ GetUserPayload()

void * FGPUDefragAllocator::GetUserPayload ( const void Pointer)

Returns the user payload for an FMemoryChunk

Parameters
PointerPointer indicating a chunk return The chunk's user payload

◆ Grow()

FGPUDefragAllocator::FMemoryChunk * FGPUDefragAllocator::Grow ( FMemoryChunk Chunk,
int64  GrowAmount 
)
protected

Tries to immediately grow a memory chunk by moving the base address, without relocating any memory.

Parameters
ChunkChunk to grow
GrowAmountNumber of uint8s to grow by
Returns
nullptr if it failed, otherwise the new grown chunk

Tries to immediately grow a memory chunk by moving the base address, without relocating any memory.

Parameters
ChunkChunk to grow
GrowAmountNumber of bytes to grow by
Returns
nullptr if it failed, otherwise the new grown chunk

◆ InBenchmarkMode()

bool FGPUDefragAllocator::InBenchmarkMode ( ) const
inline

Returns whether we're in benchmark mode or not.

◆ Initialize() [1/2]

virtual void FGPUDefragAllocator::Initialize ( uint8 InMemoryBase,
int64  InMemorySize 
)
inlinevirtual

◆ Initialize() [2/2]

virtual void FGPUDefragAllocator::Initialize ( uint8 InMemoryBase,
int64  InMemorySize,
int32  InAllocationAlignment 
)
inlinevirtual

Initialize this allocator with a preallocated block of memory.

Parameters
InMemoryBaseBase address for the block of memory
InMemorySizeSize of the block of memory, in uint8s
InAllocationAlignmentAlignment for all allocations, in uint8s

◆ InsertFence()

void FGPUDefragAllocator::InsertFence ( )
protected

Inserts a platform fence and updates the allocator sync index to match.

◆ IsAligned()

static bool FGPUDefragAllocator::IsAligned ( const volatile void Ptr,
const uint32  Alignment 
)
inlinestatic

◆ IsInitialized()

bool FGPUDefragAllocator::IsInitialized ( )
inline

Returns whether allocator has been initialized.

◆ IsValidPoolMemory()

bool FGPUDefragAllocator::IsValidPoolMemory ( const void Pointer) const
inline

◆ LinkFreeChunk()

void FGPUDefragAllocator::LinkFreeChunk ( FMemoryChunk Chunk)
inlineprotected

Frees the passed in chunk and coalesces adjacent free chunks into 'Chunk' if possible. Maint32ains the free-list order if bSortedFreeList is true.

Parameters
ChunkChunk to mark as available.
bSortedFreeListIf true, maintains the free-list sort order

◆ Lock()

void FGPUDefragAllocator::Lock ( const void Pointer)
virtual

Locks an FMemoryChunk

Parameters
PointerPointer indicating which chunk to lock

◆ PartialDefragmentationDownshift()

void FGPUDefragAllocator::PartialDefragmentationDownshift ( FRelocationStats Stats,
double  StartTime 
)
protected

Performs a partial defrag by shifting down memory to fill holes, in a brute-force manner. Takes consideration to async reallocations, but processes the all memory in order.

Parameters
Stats[out] Stats
StartTimeStart time, used for limiting the Tick() time

◆ PartialDefragmentationFast()

void FGPUDefragAllocator::PartialDefragmentationFast ( FRelocationStats Stats,
double  StartTime 
)
protected

Performs a partial defrag doing fast checks only. Adjacency and freelist walk.

Parameters
Stats[out] Stats
StartTimeStart time, used for limiting the Tick() time

◆ PartialDefragmentationSlow()

void FGPUDefragAllocator::PartialDefragmentationSlow ( FRelocationStats Stats,
double  StartTime 
)
protected

Performs a partial defrag doing slow all chunk search to find used chunks to move that are surrounded by other used chunks That a freechunk walk won't find.

Parameters
Stats[out] Stats
StartTimeStart time, used for limiting the Tick() time

◆ PlatformBlockOnFence()

virtual void FGPUDefragAllocator::PlatformBlockOnFence ( uint64  Fence)
protectedpure virtual

Blocks the calling thread until all relocations initiated before the fence was added has been fully completed.

Parameters
FenceFence to block on

◆ PlatformCanRelocate()

virtual bool FGPUDefragAllocator::PlatformCanRelocate ( const void Source,
void UserPayload 
) const
protectedpure virtual

Allows each platform to decide whether an allocation can be relocated at this time.

Parameters
SourceBase address of the allocation
UserPayloadUser payload for the allocation
Returns
true if the allocation can be relocated at this time

◆ PlatformInsertFence()

virtual uint64 FGPUDefragAllocator::PlatformInsertFence ( )
protectedpure virtual

Inserts a fence to synchronize relocations. The fence can be blocked on at a later time to ensure that all relocations initiated so far has been fully completed.

Returns
New fence value

◆ PlatformNotifyReallocationFinished()

virtual void FGPUDefragAllocator::PlatformNotifyReallocationFinished ( FAsyncReallocationRequest FinishedRequest,
void UserPayload 
)
protectedpure virtual

Notifies the platform that an async reallocation request has been completed.

Parameters
FinishedRequestThe request that got completed
UserPayloadUser payload for the allocation

◆ PlatformRelocate()

virtual void FGPUDefragAllocator::PlatformRelocate ( void Dest,
const void Source,
int64  Size,
void UserPayload 
)
protectedpure virtual

Copy memory from one location to another. If it returns false, the defragmentation process will assume the memory is not relocatable and keep it in place. Note: Source and destination may overlap.

Parameters
DestDestination memory start address
SourceSource memory start address
SizeNumber of uint8s to copy
UserPayloadUser payload for this allocation

◆ Reallocate()

void * FGPUDefragAllocator::Reallocate ( void OldBaseAddress,
int64  NewSize 
)

Tries to reallocate texture memory in-place (without relocating), by adjusting the base address of the allocation but keeping the end address the same. Note: Newly freed memory due to shrinking won't be available for allocation right away (need GPU sync).

Parameters
OldBaseAddressPointer to the original allocation
Returns
New base address if it succeeded, otherwise nullptr

Tries to reallocate texture memory in-place (without relocating), by adjusting the base address of the allocation but keeping the end address the same.

Parameters
OldBaseAddressPointer to the original allocation
Returns
New base address if it succeeded, otherwise nullptr

◆ Relocate()

void FGPUDefragAllocator::Relocate ( FRelocationStats Stats,
FMemoryChunk Dest,
int64  DestOffset,
const void Source,
int64  Size,
void UserPayload 
)
inlineprotected

Copy memory from one location to another. If it returns false, the defragmentation process will assume the memory is not relocatable and keep it in place. Note: Source and destination may overlap.

Parameters
Stats[out] Stats
DestDestination memory chunk
DestOffsetDestination offset, counted from the base address of the destination memory chunk, in uint8s
SourceBase address of the source memory
SizeNumber of uint8s to copy
UserPayloadUser payload for the allocation

◆ RelocateAllowed()

FGPUDefragAllocator::FMemoryChunk * FGPUDefragAllocator::RelocateAllowed ( FMemoryChunk FreeChunk,
FMemoryChunk UsedChunk 
)
protected

Performs a partial defrag while trying to process any pending async reallocation requests.

Parameters
Stats[out] Stats
StartTimeStart time, used for limiting the Tick() time

◆ RelocateIntoFreeChunk()

FGPUDefragAllocator::FMemoryChunk * FGPUDefragAllocator::RelocateIntoFreeChunk ( FRelocationStats Stats,
FMemoryChunk FreeChunk,
FMemoryChunk SourceChunk 
)
protected

Initiates an async relocation of an allocated chunk into a free chunk. Takes potential reallocation request into account.

Parameters
Stats[out] Stats
FreeChunkDestination chunk (free memory)
UsedChunkSource chunk (allocated memory)
Returns
Next Free chunk to try to fill up

Initiates an async relocation of an allocated chunk into a free chunk. Takes potential reallocation request into account.

Parameters
Stats[out] Stats
FreeChunkDestination chunk (free memory)
SourceChunkSource chunk (allocated memory)
Returns
Next Free chunk to try to fill up

◆ SetSettings()

void FGPUDefragAllocator::SetSettings ( const FSettings InSettings)
inline

Sets new allocator settings.

Parameters
InSettingsNew allocator settings to replace the old ones.

◆ SetUserPayload()

void FGPUDefragAllocator::SetUserPayload ( const void Pointer,
void UserPayload 
)

Sets the user payload for an FMemoryChunk

Parameters
PointerPointer indicating a chunk
UserPayloadUser payload to set

◆ Shrink()

FGPUDefragAllocator::FMemoryChunk * FGPUDefragAllocator::Shrink ( FMemoryChunk Chunk,
int64  ShrinkAmount 
)
protected

Immediately shrinks a memory chunk by moving the base address, without relocating any memory. Always succeeds.

Parameters
ChunkChunk to shrink
ShrinkAmountNumber of uint8s to shrink by
Returns
The new shrunken chunk

Immediately shrinks a memory chunk by moving the base address, without relocating any memory. Always succeeds.

Parameters
ChunkChunk to shrink
ShrinkAmountNumber of bytes to shrink by
Returns
The new shrunken chunk

◆ SortFreeList()

void FGPUDefragAllocator::SortFreeList ( int32 NumFreeChunks,
int64 LargestFreeChunk 
)
protected

Sorts the freelist based on increasing base address.

Parameters
NumFreeChunks[out] Number of free chunks
LargestFreeChunk[out] Size of the largest free chunk, in uint8s

Sorts the freelist based on increasing base address.

Parameters
NumFreeChunks[out] Number of free chunks
LargestFreeChunk[out] Size of the largest free chunk, in bytes

◆ Split()

void FGPUDefragAllocator::Split ( FMemoryChunk BaseChunk,
int64  FirstSize 
)
inlineprotected

Split allocation into two, first chunk being used and second being available. Maint32ains the free-list order if bSortedFreeList is true.

Parameters
BaseChunkChunk to split
FirstSizeNew size of first chunk
bSortedFreeListIf true, maint32ains the free-list order

◆ Tick()

int32 FGPUDefragAllocator::Tick ( FRelocationStats Stats,
bool  bPanicDefrag 
)
virtual

Partially defragments the memory and tries to process all async reallocation requests at the same time. Call this once per frame.

Parameters
Stats[out] Stats
bPanicDefragIf true, performs a full defrag and ignores all reallocation requests
Returns
Num bytes relocated.

Partially defragments the memory and tries to process all async reallocation requests at the same time. Call this once per frame.

Parameters
Stats[out] Stats
bPanicDefragIf true, performs a full defrag and ignores all reallocation requests

◆ Unlock()

void FGPUDefragAllocator::Unlock ( const void Pointer)
virtual

Unlocks an FMemoryChunk

Parameters
PointerPointer indicating which chunk to unlock

Member Data Documentation

◆ AllocatedMemorySize

volatile memsize_t FGPUDefragAllocator::AllocatedMemorySize
protected

◆ AllocationAlignment

int32 FGPUDefragAllocator::AllocationAlignment
protected

Allocation alignment requirements.

◆ AvailableMemorySize

volatile memsize_t FGPUDefragAllocator::AvailableMemorySize
protected

Available memory in uint8s.

◆ bBenchmarkMode

bool FGPUDefragAllocator::bBenchmarkMode
protected

When in benchmark mode, don't call any Platform functions.

◆ BlockedCycles

uint32 FGPUDefragAllocator::BlockedCycles
protected

Amount of time blocked by a platform fence since the beginning of the last call to Tick(), in appCycles.

◆ CompletedSyncIndex

uint64 FGPUDefragAllocator::CompletedSyncIndex
protected

Sync index that has been completed, so far.

◆ CurrentLargestHole

uint64 FGPUDefragAllocator::CurrentLargestHole
protected

◆ CurrentNumHoles

int32 FGPUDefragAllocator::CurrentNumHoles
protected

◆ CurrentSyncIndex

uint64 FGPUDefragAllocator::CurrentSyncIndex
protected

Ever-increasing index to synchronize all relocations initiated by Tick().

◆ FirstChunk

FMemoryChunk* FGPUDefragAllocator::FirstChunk
protected

Head of linked list of chunks. Sorted by memory address.

◆ FirstFreeChunk

FMemoryChunk* FGPUDefragAllocator::FirstFreeChunk
protected

Head of linked list of free chunks. Unsorted.

◆ FScopedGPUDefragLock

friend FGPUDefragAllocator::FScopedGPUDefragLock
protected

◆ LastChunk

FMemoryChunk* FGPUDefragAllocator::LastChunk
protected

Last chunk in the linked list of chunks (see FirstChunk).

◆ MaxNumHoles

int32 FGPUDefragAllocator::MaxNumHoles
protected

Maximum number of disjoint32 free memory regions we've had.

◆ MemoryBase

uint8* FGPUDefragAllocator::MemoryBase
protected

Base of memory pool.

◆ MemorySize

uint64 FGPUDefragAllocator::MemorySize
protected

Total size of memory pool, in uint8s.

◆ MinLargestHole

int64 FGPUDefragAllocator::MinLargestHole
protected

Smallest consecutive free memory region we've had.

◆ NumCanceledAsyncRequests

int32 FGPUDefragAllocator::NumCanceledAsyncRequests
protected

Total number of async requests that has been canceled so far.

◆ NumFinishedAsyncAllocations

int32 FGPUDefragAllocator::NumFinishedAsyncAllocations
protected

Total number of async allocations successfully completed so far.

◆ NumFinishedAsyncReallocations

int32 FGPUDefragAllocator::NumFinishedAsyncReallocations
protected

Total number of async reallocations successfully completed so far.

◆ NumLockedChunks

int32 FGPUDefragAllocator::NumLockedChunks
protected

◆ NumRelocationsInProgress

int32 FGPUDefragAllocator::NumRelocationsInProgress
protected

Number of async relocations that are currently in progress.

◆ PaddingWasteSize

volatile memsize_t FGPUDefragAllocator::PaddingWasteSize
protected

◆ PendingFreeChunks

TDoubleLinkedList<FMemoryChunk*> FGPUDefragAllocator::PendingFreeChunks
protected

Chunks that couldn't be freed immediately because they were being relocated.

◆ PendingMemoryAdjustment

volatile memsize_t FGPUDefragAllocator::PendingMemoryAdjustment
protected

Adjustment to allocated memory, pending all reallocations.

◆ PlatformSyncFence

uint64 FGPUDefragAllocator::PlatformSyncFence
protected

Platform-specific (GPU) fence, used for synchronizing the Sync Index.

◆ PointerToChunkMap

TMap<void*, FMemoryChunk*> FGPUDefragAllocator::PointerToChunkMap
protected

Mapping from Pointer to chunk for fast removal.

◆ Settings

FSettings FGPUDefragAllocator::Settings
protected

Allocator settings that affect its behavior.

◆ SynchronizationObject

FCriticalSection FGPUDefragAllocator::SynchronizationObject
protected

◆ TimeSpentInAllocator

double FGPUDefragAllocator::TimeSpentInAllocator
protected

Cumulative time spent in allocator.

◆ TotalNumBytesRelocated

uint64 FGPUDefragAllocator::TotalNumBytesRelocated
protected

Total number of uint8s relocated so far.

◆ TotalNumRelocations

uint64 FGPUDefragAllocator::TotalNumRelocations
protected

Total number of relocations performed so far.


The documentation for this class was generated from the following files: