UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
AllocatorFixedSizeFreeList.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
7#include "HAL/UnrealMemory.h"
8#include "CoreGlobals.h"
9
28template<uint32 AllocationSize, uint32 BlockSize>
30{
31public:
38 :FreeList(nullptr)
39 ,NumAllocated(0)
40 ,NumLive(0)
41 {
42 // need enough memory to store pointers for the free list
43 static_assert(AllocationSize >= sizeof(FreeListNode), "Allocation size must be large enough to hold pointer.");
44
45 // warm the cache
47 }
48
53 {
54 // by now all block better have been returned to me
55 check(GIsCriticalError || NumLive == 0);
56 // WRH - 2007/09/14 - Note we are stranding memory here.
57 // These pools are meant to be global and never deleted.
58 }
59
63 void *Allocate()
64 {
65 CheckInvariants();
66 if (!FreeList)
67 {
68 checkSlow(NumLive == NumAllocated);
69 Grow(BlockSize);
70 }
71 // grab next free element, updating FreeList pointer
72 void *rawMem = (void *)FreeList;
73 FreeList = FreeList->NextFreeAllocation;
74 ++NumLive;
75 CheckInvariants();
76 return rawMem;
77 }
78
83 void Free(void *Element)
84 {
85 CheckInvariants();
86 checkSlow( NumLive > 0 );
87 --NumLive;
88 FreeListNode* NewFreeElement = (FreeListNode*)Element;
89 NewFreeElement->NextFreeAllocation = FreeList;
90 FreeList = NewFreeElement;
91 CheckInvariants();
92 }
93
99 {
100 CheckInvariants();
101 return NumAllocated * AllocationSize;
102 }
103
108 void Grow(uint32 NumElements)
109 {
110 if (NumElements == 0)
111 {
112 return;
113 }
114
115 // need enough memory to store pointers for the free list
116 check(AllocationSize*NumElements >= sizeof(FreeListNode));
117
118 // allocate a block of memory
119 uint8* RawMem = (uint8*)FMemory::Malloc(AllocationSize * NumElements);
120 FreeListNode* NewFreeList = (FreeListNode*)RawMem;
121
122 // Chain the block into a list of free list nodes
123 for (uint32 i=0;i<NumElements-1;++i,NewFreeList=NewFreeList->NextFreeAllocation)
124 {
125 NewFreeList->NextFreeAllocation = (FreeListNode*)(RawMem + (i+1)*AllocationSize);
126 }
127
128 // link the last Node to the previous FreeList
129 NewFreeList->NextFreeAllocation = FreeList;
130 FreeList = (FreeListNode*)RawMem;
131
132 NumAllocated += NumElements;
133 }
134
135private:
136
137 struct FreeListNode
138 {
139 FreeListNode* NextFreeAllocation;
140 };
141
142 void CheckInvariants() const
143 {
144 checkSlow(NumAllocated >= NumLive);
145 }
146
147private:
148
150 FreeListNode* FreeList;
151
153 uint32 NumAllocated;
154
156 uint32 NumLive;
157};
#define checkSlow(expr)
Definition AssertionMacros.h:332
#define check(expr)
Definition AssertionMacros.h:314
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition AllocatorFixedSizeFreeList.h:30
void Grow(uint32 NumElements)
Definition AllocatorFixedSizeFreeList.h:108
TAllocatorFixedSizeFreeList(uint32 InitialBlockSize=0)
Definition AllocatorFixedSizeFreeList.h:37
void Free(void *Element)
Definition AllocatorFixedSizeFreeList.h:83
~TAllocatorFixedSizeFreeList()
Definition AllocatorFixedSizeFreeList.h:52
void * Allocate()
Definition AllocatorFixedSizeFreeList.h:63
uint32 GetAllocatedSize() const
Definition AllocatorFixedSizeFreeList.h:98