UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SpanAllocator.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6
7
15{
16public:
21
22 // Allocate a range. Returns allocated StartOffset.
23 inline int32 Allocate(int32 Num = 1)
24 {
25 int32 FoundIndex = SearchFreeList(Num, FirstNonEmptySpan);
26
27 if (FoundIndex == INDEX_NONE && !PendingFreeSpans.IsEmpty())
28 {
30 FoundIndex = SearchFreeList(Num, FirstNonEmptySpan);
31 }
32
33 CurrentSize += Num;
34
35 // Use an existing free span if one is found
36 if (FoundIndex != INDEX_NONE)
37 {
38 FLinearAllocation FreeSpan = FreeSpans[FoundIndex];
39
40 // Update existing free span with remainder,
41 // note: may become zero, not removing empty spans to avoid moving free list, will be cleaned out when consolidating.
42 FreeSpans[FoundIndex] = FLinearAllocation{ FreeSpan.StartOffset + Num, FreeSpan.Num - Num };
43
44 // If this span is now empty && the found index was the first non-empty, we update the first non-empty span index past this.
45 if (FreeSpan.Num == Num && FirstNonEmptySpan == FoundIndex)
46 {
47 FirstNonEmptySpan = FoundIndex + 1;
48 }
49
50 return FreeSpan.StartOffset;
51 }
52
53 // New allocation
54 int32 StartOffset = CurrentMaxSize;
55 CurrentMaxSize = CurrentMaxSize + Num;
56
57 PeakMaxSize = FMath::Max(PeakMaxSize, CurrentMaxSize);
58
59 return StartOffset;
60 }
61
62 // Free an already allocated range.
63 inline void Free(int32 BaseOffset, int32 Num = 1)
64 {
65 checkSlow(BaseOffset + Num <= CurrentMaxSize);
66 checkSlow(Num <= CurrentSize);
67 PendingFreeSpans.Add(FLinearAllocation{ BaseOffset, Num });
68 CurrentSize -= Num;
69 }
70
71 ENGINE_API void Reset();
72
73 ENGINE_API void Empty();
74
76 {
77 return CurrentSize;
78 }
79
80 inline int32 GetMaxSize() const
81 {
82 return bGrowOnly ? PeakMaxSize : CurrentMaxSize;
83 }
84
85 inline int32 GetNumFreeSpans() const
86 {
87 return FreeSpans.Num() + PendingFreeSpans.Num();
88 }
89
91 {
92 return PendingFreeSpans.Num();
93 }
94
96 {
97 return FreeSpans.GetAllocatedSize() + PendingFreeSpans.GetAllocatedSize();
98 }
99
100#if DO_CHECK
105 ENGINE_API bool IsFree(int32 Index) const;
106#endif // DO_CHECK
107
112 ENGINE_API void Consolidate();
113
114private:
115
116 struct FLinearAllocation
117 {
118 int32 StartOffset;
119 int32 Num;
120
121 inline bool operator<(const FLinearAllocation& Other) const
122 {
123 return StartOffset < Other.StartOffset;
124 }
125 };
126
127 // Total of number of items currently allocated
128 int32 CurrentSize;
129 // Size of the linear range used by the allocator
130 int32 CurrentMaxSize;
131 // Peak tracked size since last Reset or Empty
132 int32 PeakMaxSize;
133 // First span in free list that is not empty, used to speed up search for free items
134 int32 FirstNonEmptySpan;
135 // Ordered free list from low to high
137 // Unordered list of freed items since last consolidate
139 bool bGrowOnly;
140
141 ENGINE_API int32 SearchFreeList(int32 Num, int32 SearchStartIndex = 0);
142};
#define checkSlow(expr)
Definition AssertionMacros.h:332
@ INDEX_NONE
Definition CoreMiscDefines.h:150
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
bool operator<(const FTextFormatString &LHS, const FTextFormatString &RHS)
Definition ITextFormatArgumentModifier.h:147
@ Num
Definition MetalRHIPrivate.h:234
Definition SpanAllocator.h:15
ENGINE_API void Empty()
Definition SpanAllocator.cpp:25
void Free(int32 BaseOffset, int32 Num=1)
Definition SpanAllocator.h:63
int32 GetMaxSize() const
Definition SpanAllocator.h:80
ENGINE_API void Consolidate()
Definition SpanAllocator.cpp:58
int32 GetNumPendingFreeSpans() const
Definition SpanAllocator.h:90
int32 GetSparselyAllocatedSize() const
Definition SpanAllocator.h:75
SIZE_T GetAllocatedSize() const
Definition SpanAllocator.h:95
int32 Allocate(int32 Num=1)
Definition SpanAllocator.h:23
int32 GetNumFreeSpans() const
Definition SpanAllocator.h:85
ENGINE_API void Reset()
Definition SpanAllocator.cpp:15
Definition Array.h:670
U16 Index
Definition radfft.cpp:71