UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
UnrealMemory.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/MemoryBase.h"
10#include "Templates/IsPointer.h"
11
12#ifndef UE_USE_VERYLARGEPAGEALLOCATOR
13#define UE_USE_VERYLARGEPAGEALLOCATOR 0
14#endif
15
16#ifndef UE_ALLOW_OSMEMORYLOCKFREE
17#define UE_ALLOW_OSMEMORYLOCKFREE 0
18#endif
19
20
21// Sizes.
22
23#if STATS
24#define MALLOC_GT_HOOKS 1
25#else
26#define MALLOC_GT_HOOKS 0
27#endif
28
29#if MALLOC_GT_HOOKS
31#else
35#endif
36
37#define TIME_MALLOC (0)
38
39#if TIME_MALLOC
40
42{
44 static CORE_API uint64 GTotalCount[4];
46
48 uint64 Cycles;
49
51 : Index(InIndex)
52 , Cycles(FPlatformTime::Cycles64())
53 {
54 }
55 inline ~FScopedMallocTimer()
56 {
58 FPlatformAtomics::InterlockedAdd((volatile int64*)&GTotalCycles[Index], Add);
59 uint64 Was = FPlatformAtomics::InterlockedAdd((volatile int64*)&GTotalCount[Index], 1);
60
61 extern CORE_API bool GIsRunning;
62 if (GIsRunning && Index == 1 && (Was & 0xffff) == 0)
63 {
64 Spew();
65 }
66 }
67 static UE_FORCEINLINE_HINT void Miss(int32 InIndex)
68 {
69 FPlatformAtomics::InterlockedAdd((volatile int64*)&GTotalMisses[InIndex], 1);
70 }
71 static CORE_API void Spew();
72};
73#else
86#endif
87
88
89/*-----------------------------------------------------------------------------
90 FMemory.
91-----------------------------------------------------------------------------*/
92
93struct FMemory
94{
105
106
109 static UE_FORCEINLINE_HINT void* Memmove( void* Dest, const void* Src, SIZE_T Count )
110 {
111 return FPlatformMemory::Memmove( Dest, Src, Count );
112 }
113
114 static UE_FORCEINLINE_HINT int32 Memcmp( const void* Buf1, const void* Buf2, SIZE_T Count )
115 {
117 }
118
119 static UE_FORCEINLINE_HINT void* Memset(void* Dest, uint8 Char, SIZE_T Count)
120 {
121 return FPlatformMemory::Memset( Dest, Char, Count );
122 }
123
124 template< class T >
125 static inline void Memset( T& Src, uint8 ValueToSet )
126 {
127 static_assert( !TIsPointer<T>::Value, "For pointers use the three parameters function");
128 Memset( &Src, ValueToSet, sizeof( T ) );
129 }
130
131 static UE_FORCEINLINE_HINT void* Memzero(void* Dest, SIZE_T Count)
132 {
133 return FPlatformMemory::Memzero( Dest, Count );
134 }
135
137 static inline bool MemIsZero(const void* Ptr, SIZE_T Count)
138 {
139 // first pass implementation
140 uint8* Start = (uint8*)Ptr;
141 uint8* End = Start + Count;
142 while (Start < End)
143 {
144 if ((*Start++) != 0)
145 {
146 return false;
147 }
148 }
149
150 return true;
151 }
152
153 template< class T >
154 static inline void Memzero( T& Src )
155 {
156 static_assert( !TIsPointer<T>::Value, "For pointers use the two parameters function");
157 Memzero( &Src, sizeof( T ) );
158 }
159
160 static UE_FORCEINLINE_HINT void* Memcpy(void* Dest, const void* Src, SIZE_T Count)
161 {
162 return FPlatformMemory::Memcpy(Dest,Src,Count);
163 }
164
165 template< class T >
166 static inline void Memcpy( T& Dest, const T& Src )
167 {
168 static_assert( !TIsPointer<T>::Value, "For pointers use the three parameters function");
169 Memcpy( &Dest, &Src, sizeof( T ) );
170 }
171
172 static UE_FORCEINLINE_HINT void* BigBlockMemcpy(void* Dest, const void* Src, SIZE_T Count)
173 {
174 return FPlatformMemory::BigBlockMemcpy(Dest,Src,Count);
175 }
176
177 static UE_FORCEINLINE_HINT void* StreamingMemcpy(void* Dest, const void* Src, SIZE_T Count)
178 {
180 }
181
183 {
184 return FPlatformMemory::ParallelMemcpy(Dest, Src, Count, Policy);
185 }
186
187 static UE_FORCEINLINE_HINT void Memswap( void* Ptr1, void* Ptr2, SIZE_T Size )
188 {
190 }
191
192 //
193 // C style memory allocation stubs that fall back to C runtime
194 //
196 {
197 void* Ptr = ::malloc(Size);
199 return Ptr;
200 }
201
202 static inline void SystemFree(void* Ptr)
203 {
205 ::free(Ptr);
206 }
207
208 //
209 // C style memory allocation stubs.
210 //
211
216
218
226
231
236
241
246
251
274
275 // These versions are called either at startup or in the event of a crash
282
283private:
284 static CORE_API void GCreateMalloc();
285};
#define FORCENOINLINE
Definition AndroidPlatform.h:142
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
#define UE_ALLOCATION_FUNCTION(...)
Definition Platform.h:823
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
return true
Definition ExternalRpcRegistry.cpp:601
EMemcpyCachePolicy
Definition GenericPlatformMemory.h:72
@ DEFAULT_ALIGNMENT
Definition MemoryBase.h:24
void MemoryTrace_Free(uint64 Address, HeapId RootHeap=EMemoryTraceRootHeap::SystemMemory, uint32 ExternalCallstackId=0)
Definition MemoryTrace.h:200
@ SystemMemory
Definition MemoryTrace.h:35
void MemoryTrace_Alloc(uint64 Address, uint64 Size, uint32 Alignment, HeapId RootHeap=EMemoryTraceRootHeap::SystemMemory, uint32 ExternalCallstackId=0)
Definition MemoryTrace.h:199
@ Char
Character type.
UE_FORCEINLINE_HINT void DoGamethreadHook(int32 Index)
Definition UnrealMemory.h:32
uint32 Size
Definition VulkanMemory.cpp:4034
free(DecoderMem)
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition MemoryBase.h:99
U16 Index
Definition radfft.cpp:71
static uint64 Cycles64()
Definition AndroidPlatformTime.h:34
static UE_FORCEINLINE_HINT void * Memzero(void *Dest, SIZE_T Count)
Definition GenericPlatformMemory.h:586
static UE_FORCEINLINE_HINT int32 Memcmp(const void *Buf1, const void *Buf2, SIZE_T Count)
Definition GenericPlatformMemory.h:576
static UE_FORCEINLINE_HINT void * StreamingMemcpy(void *Dest, const void *Src, SIZE_T Count)
Definition GenericPlatformMemory.h:603
static UE_FORCEINLINE_HINT void * BigBlockMemcpy(void *Dest, const void *Src, SIZE_T Count)
Definition GenericPlatformMemory.h:597
static UE_FORCEINLINE_HINT void * Memcpy(void *Dest, const void *Src, SIZE_T Count)
Definition GenericPlatformMemory.h:591
static void Memswap(void *Ptr1, void *Ptr2, SIZE_T Size)
Definition GenericPlatformMemory.h:631
static void * ParallelMemcpy(void *Dest, const void *Src, SIZE_T Count, EMemcpyCachePolicy Policy=EMemcpyCachePolicy::StoreCached)
Definition GenericPlatformMemory.h:609
static UE_FORCEINLINE_HINT void * Memset(void *Dest, uint8 Char, SIZE_T Count)
Definition GenericPlatformMemory.h:581
static UE_FORCEINLINE_HINT void * Memmove(void *Dest, const void *Src, SIZE_T Count)
Definition GenericPlatformMemory.h:571
Definition UnrealMemory.h:94
static void Memzero(T &Src)
Definition UnrealMemory.h:154
static UE_FORCEINLINE_HINT void * Memmove(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:109
AllocationHints
Definition UnrealMemory.h:97
@ SmallPool
Definition UnrealMemory.h:101
@ Temporary
Definition UnrealMemory.h:100
@ None
Definition UnrealMemory.h:98
@ Default
Definition UnrealMemory.h:99
@ Max
Definition UnrealMemory.h:103
static UE_FORCEINLINE_HINT void * BigBlockMemcpy(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:172
static CORE_API SIZE_T GetAllocSizeExternal(void *Original)
Definition UnrealMemory.cpp:524
static UE_FORCEINLINE_HINT void * StreamingMemcpy(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:177
static FORCENOINLINE CORE_API SIZE_T QuantizeSize(SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT)
Definition UnrealMemory.cpp:700
static CORE_API void * ReallocExternal(void *Original, SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT)
Definition UnrealMemory.cpp:501
static CORE_API void SetupTLSCachesOnCurrentThread()
Definition UnrealMemory.cpp:573
UE_ALLOCATION_FUNCTION(1) static inline void *SystemMalloc(SIZE_T Size)
Definition UnrealMemory.h:195
static void SystemFree(void *Ptr)
Definition UnrealMemory.h:202
static CORE_API void Trim(bool bTrimThreadCaches=true)
Definition UnrealMemory.cpp:556
static UE_FORCEINLINE_HINT void * ParallelMemcpy(void *Dest, const void *Src, SIZE_T Count, EMemcpyCachePolicy Policy=EMemcpyCachePolicy::StoreCached)
Definition UnrealMemory.h:182
static void Memcpy(T &Dest, const T &Src)
Definition UnrealMemory.h:166
static UE_FORCEINLINE_HINT void * Memzero(void *Dest, SIZE_T Count)
Definition UnrealMemory.h:131
static CORE_API void ExplicitInit(FMalloc &Allocator)
Definition UnrealMemory.cpp:417
static CORE_API void MarkTLSCachesAsUsedOnCurrentThread()
Definition UnrealMemory.cpp:591
uint32 Alignment
Definition UnrealMemory.h:212
static UE_FORCEINLINE_HINT int32 Memcmp(const void *Buf1, const void *Buf2, SIZE_T Count)
Definition UnrealMemory.h:114
static UE_FORCEINLINE_HINT void * Memcpy(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:160
static CORE_API void TestMemory()
Definition UnrealMemory.cpp:607
static UE_FORCEINLINE_HINT void Memswap(void *Ptr1, void *Ptr2, SIZE_T Size)
Definition UnrealMemory.h:187
static CORE_API void EnablePurgatoryTests()
Definition UnrealMemory.cpp:243
static CORE_API SIZE_T QuantizeSizeExternal(SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT)
Definition UnrealMemory.cpp:545
static CORE_API void * MallocZeroedExternal(SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT)
Definition UnrealMemory.cpp:535
static CORE_API void * MallocExternal(SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT)
Definition UnrealMemory.cpp:491
static CORE_API void FreeExternal(void *Original)
Definition UnrealMemory.cpp:511
static void Memset(T &Src, uint8 ValueToSet)
Definition UnrealMemory.h:125
SIZE_T Count
Definition UnrealMemory.h:213
static FORCENOINLINE CORE_API SIZE_T GetAllocSize(void *Original)
Definition UnrealMemory.cpp:690
static UE_FORCEINLINE_HINT void * Memset(void *Dest, uint8 Char, SIZE_T Count)
Definition UnrealMemory.h:119
static CORE_API void ClearAndDisableTLSCachesOnCurrentThread()
Definition UnrealMemory.cpp:583
static bool MemIsZero(const void *Ptr, SIZE_T Count)
Definition UnrealMemory.h:137
static CORE_API void MarkTLSCachesAsUnusedOnCurrentThread()
Definition UnrealMemory.cpp:599
static CORE_API void EnablePoisonTests()
Definition UnrealMemory.cpp:270
Definition UnrealMemory.h:75
UE_FORCEINLINE_HINT void Hit(int32 InIndex)
Definition UnrealMemory.h:82
UE_FORCEINLINE_HINT ~FScopedMallocTimer()
Definition UnrealMemory.h:79
UE_FORCEINLINE_HINT FScopedMallocTimer(int32 InIndex)
Definition UnrealMemory.h:76
Definition IsPointer.h:12