UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MallocStomp2.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4#include "CoreTypes.h"
5#include "HAL/MemoryBase.h"
6#include "HAL/PlatformTLS.h"
7
8#ifndef WITH_MALLOC_STOMP2
9#define WITH_MALLOC_STOMP2 WITH_EDITOR && PLATFORM_WINDOWS
10#endif
11#if WITH_MALLOC_STOMP2
12#include <atomic>
18#if PLATFORM_MICROSOFT
20#endif
21
28class FMallocStomp2 final : public FMalloc
29{
30private:
32 {
33 return (((InSize)+(InAlignment - 1)) & ~(InAlignment - 1));
34 }
35
36
37#if PLATFORM_64BITS
39 static constexpr SIZE_T SentinelExpectedValue = 0xdeadbeefdeadbeef;
40#else
42 static constexpr SIZE_T SentinelExpectedValue = 0xdeadbeef;
43#endif
44
45 const SIZE_T PageSize;
46
47 struct FAllocationData
48 {
56 SIZE_T Sentinel;
57 };
58
59public:
61 {
64 int StartIndex;
65 int EndIndex;
66 };
67
68
69
71 {
72 static constexpr SIZE_T VirtualAddressSpaceToReserve = 1024LL * 1024LL * 1024LL * 64LL; // 64 GB blocks, we try to reserve that much, but will half it and retry
73 std::atomic<UPTRINT> CurrentAddress;
77
78 bool Init()
79 {
81 // if we can't reserve the full size, half the size and try again as long as we are over 1GB
82 while (SizeToTryAndReserve >= 1024LL * 1024LL * 1024LL)
83 {
85 if (StartAddress)
86 {
90 return true;
91 }
93 }
94 return false;
95 }
96
97 void* Malloc(SIZE_T InSizeToAllocate, SIZE_T InPageSize, bool InUnderrun = false)
98 {
100 if (Ret + InSizeToAllocate > EndAddress)
101 {
102 return nullptr;
103 }
104 if (!InUnderrun)
105 {
106 // leave the last page without backing store
108 }
109 else
110 {
111 // leave the first page without backing store
113 }
114 return (void*)Ret;
115 }
116
117 void Free(void* InPtr, SIZE_T InSize)
118 {
119#if PLATFORM_WINDOWS && USING_CODE_ANALYSIS
120 MSVC_PRAGMA(warning(push))
121 MSVC_PRAGMA(warning(disable : 6250)) // Suppress C6250, as virtual address space is "leaked" by design.
122#endif
123 // Unmap physical memory, but keep virtual address range reserved to catch use-after-free errors.
125
126#if PLATFORM_WINDOWS && USING_CODE_ANALYSIS
127 MSVC_PRAGMA(warning(pop))
128#endif
129 }
130 };
131
132
133private:
134 static constexpr SIZE_T TargetAddressSpaceToReserve = 1024LL * 1024LL * 1024LL * 1024LL * 32LL; // how much address space we will try and reserve, 32 TB
135
136
137 static constexpr SIZE_T MaxAddressSpaceStompDataBlocks = 4096; // this * VirtualAddressSpaceToReserve is how much address space we will try and allocate
138
141
144
145 FMalloc* UsedMalloc;
146
147 int IsPartOf(void* InPtr);
149
150
152 const bool bUseUnderrunMode;
153
154
155public:
156 // FMalloc interface.
157 FMallocStomp2(FMalloc* InMalloc, const bool InUseUnderrunMode = false);
158
170 virtual void* Malloc(SIZE_T Size, uint32 Alignment) override;
171
172 virtual void* TryMalloc(SIZE_T Size, uint32 Alignment) override;
173
183 virtual void* Realloc(void* InPtr, SIZE_T NewSize, uint32 Alignment) override;
184
185 virtual void* TryRealloc(void* InPtr, SIZE_T NewSize, uint32 Alignment) override;
186
192 virtual void Free(void* InPtr) override;
193
203 virtual bool GetAllocationSize(void *Original, SIZE_T &SizeOut) override;
204
210 virtual void DumpAllocatorStats( FOutputDevice& Ar ) override
211 {
212 // No meaningful stats to dump.
213 }
214
218 virtual bool ValidateHeap() override
219 {
220 // Nothing to do here since validation happens as data is accessed
221 // through page protection, and on each free checking the sentinel.
222 return true;
223 }
224
225 virtual const TCHAR* GetDescriptiveName() override
226 {
227 return TEXT( "Stomp2" );
228 }
229
230 virtual bool IsInternallyThreadSafe() const override
231 {
232 // Stomp allocator 2 is thread-safe
233 return true;
234 }
235
236 static FMalloc* OverrideIfEnabled(FMalloc* InUsedAlloc);
237
238 void Init();
239
240
241 static uint32 DisableTlsSlot;
242
243
245 static SIZE_T MinSize;
247 static SIZE_T MaxSize;
248};
249
250
258{
259public:
260
265 {
266 uint64 DisableCounter = (uint64)FPlatformTLS::GetTlsValue(FMallocStomp2::DisableTlsSlot);
267 ++DisableCounter;
268 FPlatformTLS::SetTlsValue(FMallocStomp2::DisableTlsSlot, (void*)DisableCounter);
269 }
270
273 {
274 uint64 DisableCounter = (uint64)FPlatformTLS::GetTlsValue(FMallocStomp2::DisableTlsSlot);
275 --DisableCounter;
276 FPlatformTLS::SetTlsValue(FMallocStomp2::DisableTlsSlot, (void*)DisableCounter);
277 }
278};
279
282
283#endif //WITH_MALLOC_STOMP2
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
#define MSVC_PRAGMA(...)
Definition Platform.h:921
FPlatformTypes::UPTRINT UPTRINT
An unsigned integer the same size as a pointer.
Definition Platform.h:1146
#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
void Init()
Definition LockFreeList.h:4
uint32 Size
Definition VulkanMemory.cpp:4034
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition MemoryBase.h:99
virtual const TCHAR * GetDescriptiveName()
Definition MemoryBase.h:248
virtual bool GetAllocationSize(void *Original, SIZE_T &SizeOut)
Definition MemoryBase.h:158
virtual CORE_API void * TryMalloc(SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT)
Definition MemoryBase.cpp:95
virtual void * Malloc(SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT)=0
virtual bool ValidateHeap()
Definition MemoryBase.h:238
virtual bool IsInternallyThreadSafe() const
Definition MemoryBase.h:230
virtual CORE_API void * TryRealloc(void *Original, SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT)
Definition MemoryBase.cpp:100
virtual void DumpAllocatorStats(class FOutputDevice &Ar)
Definition MemoryBase.h:221
virtual void * Realloc(void *Original, SIZE_T Count, uint32 Alignment=DEFAULT_ALIGNMENT)=0
Definition OutputDevice.h:133
U16 Index
Definition radfft.cpp:71
static UE_FORCEINLINE_HINT void * GetTlsValue(uint32 SlotIndex)
Definition AndroidPlatformTLS.h:57
static UE_FORCEINLINE_HINT void SetTlsValue(uint32 SlotIndex, void *Value)
Definition AndroidPlatformTLS.h:47