UDocumentation
UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
CachedOSVeryLargePageAllocator.h
Go to the documentation of this file.
1
// Copyright Epic Games, Inc. All Rights Reserved.
2
3
#pragma once
4
#include "
Templates/UnrealTemplate.h
"
5
#include "
Containers/List.h
"
6
#include "
CoreTypes.h
"
7
#include "
HAL/Allocators/CachedOSPageAllocator.h
"
8
#include "
HAL/PlatformMemory.h
"
9
#include "
HAL/PlatformMutex.h
"
10
#include "
HAL/UnrealMemory.h
"
11
12
13
#if UE_USE_VERYLARGEPAGEALLOCATOR
14
15
#if PLATFORM_64BITS
16
#define CACHEDOSVERYLARGEPAGEALLOCATOR_BYTE_LIMIT (128*1024*1024)
17
#else
18
#define CACHEDOSVERYLARGEPAGEALLOCATOR_BYTE_LIMIT (16*1024*1024)
19
#endif
20
//#define CACHEDOSVERYLARGEPAGEALLOCATOR_MAX_CACHED_OS_FREES (CACHEDOSVERYLARGEPAGEALLOCATOR_BYTE_LIMIT/(1024*64))
21
#define CACHEDOSVERYLARGEPAGEALLOCATOR_MAX_CACHED_OS_FREES (256)
22
23
#ifndef UE_VERYLARGEPAGEALLOCATOR_RESERVED_SIZE_IN_GB
24
#define UE_VERYLARGEPAGEALLOCATOR_RESERVED_SIZE_IN_GB 4
//default to 4GB
25
#endif
26
27
#ifndef UE_VERYLARGEPAGEALLOCATOR_PAGESIZE_KB
28
#define UE_VERYLARGEPAGEALLOCATOR_PAGESIZE_KB 2048
//default to 2MB
29
#endif
30
31
class
FCachedOSVeryLargePageAllocator
32
{
33
static
constexpr
uint64
AddressSpaceToReserve
= ((1024LL * 1024LL * 1024LL) *
UE_VERYLARGEPAGEALLOCATOR_RESERVED_SIZE_IN_GB
);
34
static
constexpr
uint64
AddressSpaceToReserveForSmallPool
=
AddressSpaceToReserve
/ 2;
35
36
static
constexpr
uint64
SizeOfLargePage
= (
UE_VERYLARGEPAGEALLOCATOR_PAGESIZE_KB
* 1024);
37
static
constexpr
uint64
SizeOfSubPage
= (1024 * 64);
38
static
constexpr
uint64
NumberOfLargePages
= (
AddressSpaceToReserve
/
SizeOfLargePage
);
39
static
constexpr
uint64
NumberOfSubPagesPerLargePage
= (
SizeOfLargePage
/
SizeOfSubPage
);
40
public
:
41
42
FCachedOSVeryLargePageAllocator
()
43
: bEnabled(
true
)
44
,
CachedFree
(0)
45
,
ImmediatelyFreeable
(0)
46
{
47
Init
();
48
}
49
50
~FCachedOSVeryLargePageAllocator
()
51
{
52
// this leaks everything!
53
}
54
55
void
* Allocate(
SIZE_T
Size
,
uint32
AllocationHint
= 0,
UE::FPlatformRecursiveMutex
* Mutex =
nullptr
);
56
57
void
Free
(
void
* Ptr,
SIZE_T
Size
,
UE::FPlatformRecursiveMutex
* Mutex =
nullptr
,
bool
ThreadIsTimeCritical
=
false
);
58
59
void
FreeAll(
UE::FPlatformRecursiveMutex
* Mutex =
nullptr
);
60
61
// Refresh cached os allocator if needed. Will preallocate / reduce backstore if preallocation is enabled
62
void
Refresh
();
63
64
void
UpdateStats();
65
66
uint64
GetCachedFreeTotal()
const
67
{
68
return
CachedFree
+ CachedOSPageAllocator.GetCachedFreeTotal();
69
}
70
71
uint64
GetCachedImmediatelyFreeable()
const
72
{
73
return
ImmediatelyFreeable
+ CachedOSPageAllocator.GetCachedImmediatelyFreeable();
74
}
75
76
inline
bool
IsSmallBlockAllocation
(
const
void
* Ptr)
const
77
{
78
if
(((
uintptr_t
)Ptr -
AddressSpaceReserved
) <
AddressSpaceToReserveForSmallPool
)
79
{
80
return
true
;
81
}
82
return
false
;
83
}
84
85
private
:
86
struct
FLargePage
:
public
TIntrusiveLinkedList
<FLargePage>
87
{
88
uintptr_t
FreeSubPages
[
NumberOfSubPagesPerLargePage
];
89
int32
NumberOfFreeSubPages
;
90
uint32
AllocationHint
;
91
92
uintptr_t
BaseAddress;
93
94
void
Init
(
void
*
InBaseAddress
)
95
{
96
BaseAddress = (
uintptr_t
)
InBaseAddress
;
97
NumberOfFreeSubPages
=
NumberOfSubPagesPerLargePage
;
98
uintptr_t
Ptr = BaseAddress;
99
for
(
int
i = 0; i <
NumberOfFreeSubPages
; i++)
100
{
101
FreeSubPages
[i] = Ptr;
102
Ptr +=
SizeOfSubPage
;
103
}
104
}
105
106
void
Free
(
void
* Ptr)
107
{
108
FreeSubPages
[
NumberOfFreeSubPages
++] = (
uintptr_t
)Ptr;
109
}
110
111
void
* Allocate()
112
{
113
void
*
ret
=
nullptr
;
114
if
(
NumberOfFreeSubPages
)
115
{
116
ret
= (
void
*)
FreeSubPages
[--
NumberOfFreeSubPages
];
117
}
118
return
ret
;
119
}
120
};
121
122
void
Init
();
123
void
ShrinkEmptyBackStore
(
int32
NewEmptyBackStoreSize
,
FMemory::AllocationHints
AllocationHint
);
124
FLargePage
*
GetOrAllocLargePage
(
uint32
AllocationHint
,
UE::FPlatformRecursiveMutex
* Mutex);
125
FLargePage
*
AllocNewLargePage
(
uint32
AllocationHint
,
UE::FPlatformRecursiveMutex
* Mutex,
bool
&
bOutCommitFailure
);
126
127
bool
bEnabled;
128
uintptr_t
AddressSpaceReserved
;
129
uintptr_t
AddressSpaceReservedEndSmallPool
;
130
uintptr_t
AddressSpaceReservedEnd
;
131
uint64
CachedFree
;
132
uint64
ImmediatelyFreeable
;
// the amount of memory that can be immediately returned to the OS
133
int32
EmptyBackStoreCount
[
FMemory::AllocationHints::Max
];
134
int32
CommittedLargePagesCount
[
FMemory::AllocationHints::Max
];
135
136
FPlatformMemory::FPlatformVirtualMemoryBlock
Block
;
137
138
FLargePage
*
FreeLargePagesHead
[
FMemory::AllocationHints::Max
];
// no backing store
139
140
FLargePage
*
UsedLargePagesHead
[
FMemory::AllocationHints::Max
];
// has backing store and is full
141
142
FLargePage
*
UsedLargePagesWithSpaceHead
[
FMemory::AllocationHints::Max
];
// has backing store and still has room
143
144
FLargePage
*
EmptyButAvailableLargePagesHead
[
FMemory::AllocationHints::Max
];
// has backing store and is empty
145
146
FLargePage
LargePagesArray
[
NumberOfLargePages
];
147
148
TCachedOSPageAllocator<CACHEDOSVERYLARGEPAGEALLOCATOR_MAX_CACHED_OS_FREES, CACHEDOSVERYLARGEPAGEALLOCATOR_BYTE_LIMIT>
CachedOSPageAllocator;
149
150
};
151
CORE_API
extern
bool
GEnableVeryLargePageAllocator
;
152
153
#if UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_7
154
#include "
HAL/CriticalSection.h
"
155
#endif
156
157
#endif
// UE_USE_VERYLARGEPAGEALLOCATOR
AnimPhysLinearConstraintType::Free
@ Free
CachedOSPageAllocator.h
EChaosVDCollisionQueryHitType::Block
@ Block
CoreTypes.h
SIZE_T
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition
Platform.h:1150
int32
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition
Platform.h:1125
uint64
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition
Platform.h:1117
StaticCastSharedRef
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition
SharedPointer.h:127
CriticalSection.h
true
return true
Definition
ExternalRpcRegistry.cpp:601
List.h
Init
void Init()
Definition
LockFreeList.h:4
PlatformMemory.h
PlatformMutex.h
UnrealMemory.h
UnrealTemplate.h
Size
uint32 Size
Definition
VulkanMemory.cpp:4034
uint32
uint32_t uint32
Definition
binka_ue_file_header.h:6
FAndroidPlatformMemory::FPlatformVirtualMemoryBlock
Definition
AndroidPlatformMemory.h:38
TIntrusiveLinkedList
Definition
List.h:349
Chaos::EClusterUnionGeometryOperation::Refresh
@ Refresh
UE::FPlatformRecursiveMutex
FPThreadsRecursiveMutex FPlatformRecursiveMutex
Definition
AndroidPlatformMutex.h:12
FMemory::AllocationHints
AllocationHints
Definition
UnrealMemory.h:97
FMemory::Max
@ Max
Definition
UnrealMemory.h:103
TCachedOSPageAllocator
Definition
CachedOSPageAllocator.h:37
Engine
Source
Runtime
Core
Public
HAL
Allocators
CachedOSVeryLargePageAllocator.h
Generated by
1.9.8