17 FFreeLink*
Next =
nullptr;
22 int64 AllocBlocksSize = 0;
23 int64 FreeBlocksSize = 0;
24 FFreeLink* FirstFree =
nullptr;
34 size_t MaximumAlignment;
38 FFreeLink* RecycledLinks;
43 FPerBlockSize Blocks[64];
45 void FreeVirtualByBlock(
void* Ptr, FPerBlockSize&
Block,
size_t AlignedSize)
55 uint32 Pages = MaximumAlignment / FPlatformMemory::FPlatformVirtualMemoryBlock::GetVirtualSizeAlignment();
56 check(Pages && MaximumAlignment % FPlatformMemory::FPlatformVirtualMemoryBlock::GetVirtualSizeAlignment() == 0);
62 Alloc = FMemory::Malloc(MaximumAlignment);
66 FFreeLink*
NewLink =
new ((
void*)(((
uint8*)Alloc +
Index *
sizeof(FFreeLink)))) FFreeLink;
70 LinkSize += MaximumAlignment;
73 FFreeLink*
Link = RecycledLinks;
74 RecycledLinks =
Link->Next;
80 Block.FreeBlocksSize += AlignedSize;
86 uint8* Result = NextAlloc;
88 NextAlloc = Result + AlignedSize;
101 , RecycledLinks(nullptr)
106 TotalSize = HighAddress - LowAddress;
107 check(LowAddress && HighAddress && LowAddress < HighAddress &&
IsAligned(LowAddress, MaximumAlignment));
115 size_t SizeAndAlignment = FMath::Max(
Align(
Size, Alignment), PageSize);
116 if (SizeAndAlignment * 2 >= TotalSize)
119 size_t Pages = SizeAndAlignment / PageSize;
123 size_t BlockIndex = FMath::CeilLogTwo64(FMath::Max(SizeAndAlignment, PageSize));
125 check(AlignedSize % PageSize == 0);
126 size_t Pages = AlignedSize / PageSize;
136 size_t BlockIndex = FMath::CeilLogTwo64(NumPages * PageSize);
139 if (
size_t(NumPages) * PageSize * 2 >= TotalSize)
142 AlignedSize =
size_t(NumPages) * PageSize;
156 Block.FirstFree->Ptr =
nullptr;
158 FFreeLink*
Next = RecycledLinks;
159 RecycledLinks =
Block.FirstFree;
162 RecycledLinks->Next =
Next;
164 check(
IsAligned(Result, FMath::Min(AlignedSize, MaximumAlignment)));
165 Block.FreeBlocksSize -= AlignedSize;
169 size_t AllocSize = FMath::Max(AlignedSize, MaximumAlignment);
180 Block.AllocBlocksSize += AllocSize;
196 if (
size_t(NumPages) * PageSize * 2 >= TotalSize)
199 check(!
"Huge vm blocks may not be freed.");
202 size_t BlockIndex = FMath::CeilLogTwo64(NumPages * PageSize);
207 FreeVirtualByBlock(Ptr,
Block, AlignedSize);
232 OutStats.MaximumAlignment = MaximumAlignment;
233 OutStats.VMSpaceTotal = HighAddress - LowAddress;
constexpr T Align(T Val, uint64 Alignment)
Definition AlignmentTemplates.h:18
constexpr bool IsAligned(T Val, uint64 Alignment)
Definition AlignmentTemplates.h:50
#define check(expr)
Definition AssertionMacros.h:314
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
FRWLock Lock
Definition UnversionedPropertySerialization.cpp:921
uint32 Size
Definition VulkanMemory.cpp:4034
int BlockIndex
Definition binka_ue_decode_test.cpp:38
Definition ScopeLock.h:141
Definition VirtualAllocator.h:13
virtual ~FVirtualAllocator()=default
void * AllocateVirtualPages(uint32 NumPages, size_t AlignmentForCheck=1)
Definition VirtualAllocator.h:131
uint32 GetPagesForSizeAndAlignment(size_t Size, size_t Alignment=1) const
Definition VirtualAllocator.h:112
void GetStats(FVirtualAllocatorStats &OutStats)
Definition VirtualAllocator.h:228
void FreeVirtual(void *Ptr, uint32 NumPages)
Definition VirtualAllocator.h:194
virtual uint8 * AllocNewVM(size_t AlignedSize)
Definition VirtualAllocator.h:84
FVirtualAllocator(void *InLowAdress, void *InHighAddress, size_t InPageSize, size_t InMaximumAlignment, bool bInBacksMalloc)
Definition VirtualAllocator.h:95
size_t SpaceConsumed
Definition VirtualAllocator.h:83
U16 Index
Definition radfft.cpp:71
Definition UnrealMathUtility.h:270
Definition VirtualAllocator.h:211
size_t AllocBlocksSize
Definition VirtualAllocator.h:212
size_t FreeBlocksSize
Definition VirtualAllocator.h:213
Definition VirtualAllocator.h:216
size_t FreeListLinks
Definition VirtualAllocator.h:223
size_t VMSpaceTotal
Definition VirtualAllocator.h:219
size_t VMSpaceConsumed
Definition VirtualAllocator.h:220
size_t VMSpaceConsumedPeak
Definition VirtualAllocator.h:221
size_t PageSize
Definition VirtualAllocator.h:217
size_t MaximumAlignment
Definition VirtualAllocator.h:218
FVirtualAllocatorStatsPerBlockSize BlockStats[64]
Definition VirtualAllocator.h:225