Simple best fit allocator, splitting and coalescing whenever/ wherever possible. 更多...
#include <GPUDefragAllocator.h>
类 | |
| class | FMemoryChunk |
| Contains information of a single allocation or free block. 更多... | |
| struct | FRelocationStats |
| Container for allocator relocation stats. 更多... | |
| struct | FSettings |
| Container for allocator settings. 更多... | |
Public 成员函数 | |
| virtual void * | Allocate (int64 AllocationSize, int32 Alignment, TStatId InStat, bool bAllowFailure) |
| Allocate physical memory. 更多... | |
| void | 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. 更多... | |
| void | BlockOnAsyncReallocation (FAsyncReallocationRequest *Request) |
| Blocks the calling thread until the specified request has been completed. 更多... | |
| void | CancelAsyncReallocation (FAsyncReallocationRequest *Request, const void *CurrentBaseAddress) |
| Cancels the specified reallocation request. 更多... | |
| void | DefragmentMemory (FRelocationStats &Stats) |
| Fully defragments the memory and blocks until it's done. 更多... | |
| void | DumpAllocs (FOutputDevice &Ar= *GLog) |
| Dump allocation information. | |
| FGPUDefragAllocator () | |
| Constructor, zero initializing all member variables | |
| bool | FinishAllRelocations () |
| Blocks the calling thread until all relocations and reallocations that were initiated by Tick() have completed. 更多... | |
| virtual void | Free (void *Pointer) |
| Frees allocation associated with the specified Pointer. 更多... | |
| int64 | GetAllocatedSize (void *Pointer) |
| Returns the amount of memory allocated for the specified address. 更多... | |
| uint32 | GetBlockedCycles () const |
| Returns the amount of time blocked by a platform fence since the beginning of the last call to Tick(), in appCycles. | |
| int32 | GetLargestAvailableAllocation (int32 *OutNumFreeChunks=nullptr) |
| Scans the free chunks and returns the largest size you can allocate. 更多... | |
| virtual void | GetMemoryStats (int64 &OutAllocatedMemorySize, int64 &OutAvailableMemorySize, int64 &OutPendingMemoryAdjustment, int64 &OutPaddingWasteSize) |
| Retrieves allocation stats. 更多... | |
| void | GetSettings (FSettings &OutSettings) |
| Returns the current allocator settings. 更多... | |
| bool | GetTextureMemoryVisualizeData (FColor *TextureData, int32 SizeX, int32 SizeY, int32 Pitch, const int32 PixelSize) |
| Fills a texture with to visualize the texture pool memory. 更多... | |
| void * | GetUserPayload (const void *Pointer) |
| Returns the user payload for an FMemoryChunk 更多... | |
| bool | InBenchmarkMode () const |
| Returns whether we're in benchmark mode or not. | |
| virtual void | Initialize (uint8 *InMemoryBase, int64 InMemorySize, int32 InAllocationAlignment) |
| Initialize this allocator with a preallocated block of memory. 更多... | |
| bool | IsInitialized () |
| Returns whether allocator has been initialized. | |
| virtual void | Lock (const void *Pointer) |
| Locks an FMemoryChunk 更多... | |
| void * | 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. 更多... | |
| void | SetSettings (const FSettings &InSettings) |
| Sets new allocator settings. 更多... | |
| void | SetUserPayload (const void *Pointer, void *UserPayload) |
| Sets the user payload for an FMemoryChunk 更多... | |
| virtual int32 | Tick (FRelocationStats &Stats, bool bPanicDefrag) |
| Partially defragments the memory and tries to process all async reallocation requests at the same time. 更多... | |
| virtual void | Unlock (const void *Pointer) |
| Unlocks an FMemoryChunk 更多... | |
Protected 类型 | |
| typedef int64 | memsize_t |
| Allocated memory in uint8s. 更多... | |
Protected 成员函数 | |
| FMemoryChunk * | AllocateChunk (FMemoryChunk *FreeChunk, int64 AllocationSize, bool bAsync, bool bDoValidation=true) |
| Marks the specified chunk as 'allocated' and updates tracking variables. 更多... | |
| void | BlockOnFence () |
| Blocks the calling thread until the current sync fence has been completed. | |
| void | BlockOnSyncIndex (uint32 SyncIndex) |
| Blocks the calling thread until the specified sync index has been completed. 更多... | |
| bool | CanRelocate (const FMemoryChunk *Chunk) const |
| Returns true if the specified chunk is allowed to relocate at this time. 更多... | |
| void | CheckForErrors (bool bCheckSortedFreeList) |
| Checks the int32ernal state for errors. 更多... | |
| void | Coalesce (FMemoryChunk *FreedChunk) |
| Merges any adjacent free chunks into the specified free chunk. 更多... | |
| FMemoryChunk * | FindAdjacent (FMemoryChunk *UsedChunk, bool bAnyChunkType) |
| Defrag helper function. 更多... | |
| FMemoryChunk * | FindAdjacentToHole (FMemoryChunk *FreeChunk) |
| Searches for an allocated chunk that would fit within the specified free chunk. 更多... | |
| FMemoryChunk * | FindAny (FMemoryChunk *FreeChunk) |
| Searches for an allocated chunk that would fit within the specified free chunk. 更多... | |
| void | FreeChunk (FMemoryChunk *Chunk) |
| Marks the specified chunk as 'free' and updates tracking variables. 更多... | |
| void | FullDefragmentation (FRelocationStats &Stats) |
| Performs a full defrag and ignores all reallocation requests. 更多... | |
| uint32 | GetCurrentSyncIndex () const |
| Returns the sync index to be completed by the next call to FinishAllRelocations(). | |
| FMemoryChunk * | Grow (FMemoryChunk *Chunk, int64 GrowAmount) |
| Tries to immediately grow a memory chunk by moving the base address, without relocating any memory. 更多... | |
| void | InsertFence () |
| Inserts a platform fence and updates the allocator sync index to match. | |
| void | LinkFreeChunk (FMemoryChunk *Chunk) |
| Frees the passed in chunk and coalesces adjacent free chunks into 'Chunk' if possible. 更多... | |
| void | PartialDefragmentationDownshift (FRelocationStats &Stats, double StartTime) |
| Performs a partial defrag by shifting down memory to fill holes, in a brute-force manner. 更多... | |
| void | PartialDefragmentationFast (FRelocationStats &Stats, double StartTime) |
| Performs a partial defrag doing fast checks only. 更多... | |
| void | PartialDefragmentationSlow (FRelocationStats &Stats, double StartTime) |
| 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. 更多... | |
| virtual void | PlatformBlockOnFence (uint64 Fence)=0 |
| Blocks the calling thread until all relocations initiated before the fence was added has been fully completed. 更多... | |
| virtual bool | PlatformCanRelocate (const void *Source, void *UserPayload) const =0 |
| Allows each platform to decide whether an allocation can be relocated at this time. 更多... | |
| virtual uint64 | PlatformInsertFence ()=0 |
| Inserts a fence to synchronize relocations. 更多... | |
| virtual void | PlatformNotifyReallocationFinished (FAsyncReallocationRequest *FinishedRequest, void *UserPayload)=0 |
| Notifies the platform that an async reallocation request has been completed. 更多... | |
| virtual void | PlatformRelocate (void *Dest, const void *Source, int64 Size, void *UserPayload)=0 |
| Copy memory from one location to another. 更多... | |
| void | Relocate (FRelocationStats &Stats, FMemoryChunk *Dest, int64 DestOffset, const void *Source, int64 Size, void *UserPayload) |
| Copy memory from one location to another. 更多... | |
| FMemoryChunk * | RelocateIntoFreeChunk (FRelocationStats &Stats, FMemoryChunk *FreeChunk, FMemoryChunk *UsedChunk) |
| Initiates an async relocation of an allocated chunk into a free chunk. 更多... | |
| FMemoryChunk * | Shrink (FMemoryChunk *Chunk, int64 ShrinkAmount) |
| Immediately shrinks a memory chunk by moving the base address, without relocating any memory. 更多... | |
| void | SortFreeList (int32 &NumFreeChunks, int64 &LargestFreeChunk) |
| Sorts the freelist based on increasing base address. 更多... | |
| void | Split (FMemoryChunk *BaseChunk, int64 FirstSize) |
| Split allocation into two, first chunk being used and second being available. 更多... | |
Protected 属性 | |
| int32 | AllocationAlignment |
| Allocation alignment requirements. 更多... | |
| volatile memsize_t | AvailableMemorySize |
| Available memory in uint8s. 更多... | |
| bool | bBenchmarkMode |
| When in benchmark mode, don't call any Platform functions. 更多... | |
| uint32 | BlockedCycles |
| Amount of time blocked by a platform fence since the beginning of the last call to Tick(), in appCycles. 更多... | |
| uint64 | CompletedSyncIndex |
| Sync index that has been completed, so far. 更多... | |
| uint64 | CurrentSyncIndex |
| Ever-increasing index to synchronize all relocations initiated by Tick(). | |
| FMemoryChunk * | FirstChunk |
| Head of linked list of chunks. 更多... | |
| FMemoryChunk * | FirstFreeChunk |
| Head of linked list of free chunks. 更多... | |
| FMemoryChunk * | LastChunk |
| Last chunk in the linked list of chunks (see FirstChunk). 更多... | |
| int32 | MaxNumHoles |
| Maximum number of disjoint32 free memory regions we've had. | |
| uint8 * | MemoryBase |
| Base of memory pool. 更多... | |
| uint64 | MemorySize |
| Total size of memory pool, in uint8s. 更多... | |
| int64 | MinLargestHole |
| Smallest consecutive free memory region we've had. 更多... | |
| int32 | NumCanceledAsyncRequests |
| Total number of async requests that has been canceled so far. 更多... | |
| int32 | NumFinishedAsyncAllocations |
| Total number of async allocations successfully completed so far. 更多... | |
| int32 | NumFinishedAsyncReallocations |
| Total number of async reallocations successfully completed so far. 更多... | |
| int32 | NumRelocationsInProgress |
| Number of async relocations that are currently in progress. | |
| TDoubleLinkedList< FMemoryChunk * > | PendingFreeChunks |
| Chunks that couldn't be freed immediately because they were being relocated. | |
| volatile memsize_t | PendingMemoryAdjustment |
| Adjustment to allocated memory, pending all reallocations. 更多... | |
| uint64 | PlatformSyncFence |
| Platform-specific (GPU) fence, used for synchronizing the Sync Index. | |
| TMap< void *, FMemoryChunk * > | PointerToChunkMap |
| Mapping from Pointer to chunk for fast removal. 更多... | |
| FSettings | Settings |
| Allocator settings that affect its behavior. 更多... | |
| double | TimeSpentInAllocator |
| Cumulative time spent in allocator. 更多... | |
| uint64 | TotalNumBytesRelocated |
| Total number of uint8s relocated so far. 更多... | |
| uint64 | TotalNumRelocations |
| Total number of relocations performed so far. 更多... | |
Simple best fit allocator, splitting and coalescing whenever/ wherever possible.
NOT THREAD-SAFE.
|
protected |
Allocated memory in uint8s.
|
virtual |
Allocate physical memory.
| AllocationSize | Size of allocation |
| bAllowFailure | Whether to allow allocation failure or not |
|
protected |
Marks the specified chunk as 'allocated' and updates tracking variables.
Splits the chunk if only a portion of it is allocated.
| FreeChunk | Chunk to allocate |
| AllocationSize | Number of uint8s to allocate |
| bAsync | If true, allows allocating from relocating chunks and maint32ains the free-list sort order. |
| 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.
| MinChunkSize | Minimum number of uint8s per random chunk |
| MaxChunkSize | Maximum number of uint8s per random chunk |
| FreeRatio | Free 0.0-1.0 of the memory before benchmarking |
| LockRatio | Lock 0.0-1.0 % of the memory before benchmarking |
| bFullDefrag | Whether to test full defrag (true) or continuous defrag (false) |
| bSaveImages | Whether 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 |
| FORCEINLINE void FGPUDefragAllocator::BlockOnAsyncReallocation | ( | FAsyncReallocationRequest * | Request | ) |
Blocks the calling thread until the specified request has been completed.
| Request | Request to wait for. Must be a valid request. |
|
protected |
Blocks the calling thread until the specified sync index has been completed.
| SyncIndex | Sync index to wait for |
| 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.
| Request | Request to cancel. Must be a valid request. |
| CurrentBaseAddress | Current baseaddress used by the allocation. |
|
protected |
Returns true if the specified chunk is allowed to relocate at this time.
Will also call PlatformCanRelocate().
| Chunk | Chunk to check |
|
protected |
Checks the int32ernal state for errors.
(Slow)
| bCheckSortedFreeList | If true, also checks that the freelist is sorted |
|
protected |
Merges any adjacent free chunks into the specified free chunk.
Doesn't affect the free-list sort order.
| FreedChunk | Chunk that just became available. |
| void FGPUDefragAllocator::DefragmentMemory | ( | FRelocationStats & | Stats | ) |
Fully defragments the memory and blocks until it's done.
| Stats | [out] Stats |
|
protected |
Defrag helper function.
Checks if the specified allocation fits within the adjacent free chunk(s).
| UsedChunk | Allocated chunk to check for a fit |
| bAnyChunkType | If false, only succeeds if 'UsedChunk' has a reallocation request and fits |
|
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.
| FreeChunk | Free chunk we're trying to fill up |
|
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.
| FreeChunk | Free chunk we're trying to fill up |
| bool FGPUDefragAllocator::FinishAllRelocations | ( | ) |
Blocks the calling thread until all relocations and reallocations that were initiated by Tick() have completed.
|
virtual |
Frees allocation associated with the specified Pointer.
| Pointer | Pointer to free. |
|
protected |
Marks the specified chunk as 'free' and updates tracking variables.
Calls LinkFreeChunk() to coalesce adjacent free memory.
| Chunk | Chunk to free |
|
protected |
Performs a full defrag and ignores all reallocation requests.
| Stats | [out] Stats |
| int64 FGPUDefragAllocator::GetAllocatedSize | ( | void * | Pointer | ) |
Returns the amount of memory allocated for the specified address.
| Pointer | Pointer to check. |
| int32 FGPUDefragAllocator::GetLargestAvailableAllocation | ( | int32 * | OutNumFreeChunks = nullptr | ) |
Scans the free chunks and returns the largest size you can allocate.
| OutNumFreeChunks | Upon return, contains the total number of free chunks. May be nullptr. |
|
inlinevirtual |
Retrieves allocation stats.
| OutAllocatedMemorySize | [out] Size of allocated memory |
| OutAvailableMemorySize | [out] Size of available memory |
| OutPendingMemoryAdjustment | [out] Size of pending allocation change (due to async reallocation) |
|
inline |
Returns the current allocator settings.
| OutSettings | [out] Current allocator settings |
| bool FGPUDefragAllocator::GetTextureMemoryVisualizeData | ( | FColor * | TextureData, |
| int32 | SizeX, | ||
| int32 | SizeY, | ||
| int32 | Pitch, | ||
| const int32 | PixelSize | ||
| ) |
Fills a texture with to visualize the texture pool memory.
| TextureData | Start address |
| SizeX | Number of pixels along X |
| SizeY | Number of pixels along Y |
| Pitch | Number of uint8s between each row |
| PixelSize | Number of uint8s each pixel represents |
| void * FGPUDefragAllocator::GetUserPayload | ( | const void * | Pointer | ) |
Returns the user payload for an FMemoryChunk
| Pointer | Pointer indicating a chunk return The chunk's user payload |
|
protected |
Tries to immediately grow a memory chunk by moving the base address, without relocating any memory.
| Chunk | Chunk to grow |
| GrowAmount | Number of uint8s to grow by |
|
inlinevirtual |
Initialize this allocator with a preallocated block of memory.
| InMemoryBase | Base address for the block of memory |
| InMemorySize | Size of the block of memory, in uint8s |
| InAllocationAlignment | Alignment for all allocations, in uint8s |
|
inlineprotected |
Frees the passed in chunk and coalesces adjacent free chunks into 'Chunk' if possible.
Maint32ains the free-list order if bSortedFreeList is true.
| Chunk | Chunk to mark as available. |
| bSortedFreeList | If true, maintains the free-list sort order |
|
virtual |
Locks an FMemoryChunk
| Pointer | Pointer indicating which chunk to lock |
|
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.
| Stats | [out] Stats |
| StartTime | Start time, used for limiting the Tick() time |
|
protected |
Performs a partial defrag doing fast checks only.
Adjacency and freelist walk.
| Stats | [out] Stats |
| StartTime | Start time, used for limiting the Tick() time |
|
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.
| Stats | [out] Stats |
| StartTime | Start time, used for limiting the Tick() time |
|
protectedpure virtual |
Blocks the calling thread until all relocations initiated before the fence was added has been fully completed.
| Fence | Fence to block on |
|
protectedpure virtual |
Allows each platform to decide whether an allocation can be relocated at this time.
| Source | Base address of the allocation |
| UserPayload | User payload for the allocation |
|
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.
|
protectedpure virtual |
Notifies the platform that an async reallocation request has been completed.
| FinishedRequest | The request that got completed |
| UserPayload | User payload for the allocation |
|
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.
| Dest | Destination memory start address |
| Source | Source memory start address |
| Size | Number of uint8s to copy |
| UserPayload | User payload for this allocation |
| 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).
| OldBaseAddress | Pointer to the original allocation |
|
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.
| Stats | [out] Stats |
| Dest | Destination memory chunk |
| DestOffset | Destination offset, counted from the base address of the destination memory chunk, in uint8s |
| Source | Base address of the source memory |
| Size | Number of uint8s to copy |
| UserPayload | User payload for the allocation |
|
protected |
Initiates an async relocation of an allocated chunk into a free chunk.
Takes potential reallocation request into account.
| Stats | [out] Stats |
| FreeChunk | Destination chunk (free memory) |
| UsedChunk | Source chunk (allocated memory) |
|
inline |
Sets new allocator settings.
| InSettings | New allocator settings to replace the old ones. |
| void FGPUDefragAllocator::SetUserPayload | ( | const void * | Pointer, |
| void * | UserPayload | ||
| ) |
Sets the user payload for an FMemoryChunk
| Pointer | Pointer indicating a chunk |
| UserPayload | User payload to set |
|
protected |
Immediately shrinks a memory chunk by moving the base address, without relocating any memory.
Always succeeds.
| Chunk | Chunk to shrink |
| ShrinkAmount | Number of uint8s to shrink by |
|
protected |
Sorts the freelist based on increasing base address.
| NumFreeChunks | [out] Number of free chunks |
| LargestFreeChunk | [out] Size of the largest free chunk, in uint8s |
|
inlineprotected |
Split allocation into two, first chunk being used and second being available.
Maint32ains the free-list order if bSortedFreeList is true.
| BaseChunk | Chunk to split |
| FirstSize | New size of first chunk |
| bSortedFreeList | If true, maint32ains the free-list order |
|
virtual |
Partially defragments the memory and tries to process all async reallocation requests at the same time.
Call this once per frame.
| Stats | [out] Stats |
| bPanicDefrag | If true, performs a full defrag and ignores all reallocation requests |
|
virtual |
Unlocks an FMemoryChunk
| Pointer | Pointer indicating which chunk to unlock |
|
protected |
Allocation alignment requirements.
|
protected |
Available memory in uint8s.
|
protected |
When in benchmark mode, don't call any Platform functions.
|
protected |
Amount of time blocked by a platform fence since the beginning of the last call to Tick(), in appCycles.
|
protected |
Sync index that has been completed, so far.
|
protected |
Head of linked list of chunks.
Sorted by memory address.
|
protected |
Head of linked list of free chunks.
Unsorted.
|
protected |
Last chunk in the linked list of chunks (see FirstChunk).
|
protected |
Base of memory pool.
|
protected |
Total size of memory pool, in uint8s.
|
protected |
Smallest consecutive free memory region we've had.
|
protected |
Total number of async requests that has been canceled so far.
|
protected |
Total number of async allocations successfully completed so far.
|
protected |
Total number of async reallocations successfully completed so far.
|
protected |
Adjustment to allocated memory, pending all reallocations.
|
protected |
Mapping from Pointer to chunk for fast removal.
|
protected |
Allocator settings that affect its behavior.
|
protected |
Cumulative time spent in allocator.
|
protected |
Total number of uint8s relocated so far.
|
protected |
Total number of relocations performed so far.