UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
VirtualStackAllocator.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "HAL/Platform.h"
6#include "HAL/PlatformCrt.h"
9#include "AutoRTFM.h"
10
11#if PLATFORM_HAS_ASAN_INCLUDE
12#include <sanitizer/asan_interface.h>
13#endif
14
16
34{
35 // Default mode, does not decommit pages until the allocator is destroyed
37 // Decommits all pages once none are in use
39 // Tracks the high water mark and uses it to free excess memory that is not expected to be used again soon
40 // This enables us to quickly release memory consumed by a one-off spikey usage pattern while avoiding frequent page management in the steady state
41 // See DecommitUnusedPages() for details of the heuristic used
44};
45
47{
48public:
49
51
52private:
54
56 : RestorePointer(InRestorePointer)
57 , Owner(Owner)
58 {
59 }
60
64 void operator=(const FScopedStackAllocatorBookmark& Other) = delete;
65 void operator=(FScopedStackAllocatorBookmark&& Other) = delete;
66
67 void* RestorePointer;
69};
70
72{
73public:
74
75 // RequestedStackSize will be rounded up to a whole number of pages and reserved
76 // One page will be earmarked as a guard, so the available memory will be
77 // NextMultipleOf(ReqestedStackSize, SystemPageSize) - SystemPageSize
80
82 {
83 // Note that this depends on mandatory Return Value Optimization to ensure the memory is only freed once
84 return FScopedStackAllocatorBookmark(NextAllocationStart, this);
85 }
86
87 CORE_API void* Allocate(size_t Size, size_t Alignment);
88
89 size_t GetAllocatedBytes() const
90 {
91 return (char*)NextAllocationStart - (char*)VirtualMemory.GetVirtualPointer();
92 }
93
94 size_t GetCommittedBytes() const
95 {
96 return (char*)NextUncommittedPage - (char*)VirtualMemory.GetVirtualPointer();
97 }
98
99private:
102 void operator=(const FVirtualStackAllocator& Other) = delete;
103 void operator=(FVirtualStackAllocator&& Other) = delete;
104
106
108
109 inline void Free(void* RestorePointer)
110 {
111 check(RestorePointer <= NextAllocationStart);
112
113#if PLATFORM_HAS_ASAN_INCLUDE && !UE_AUTORTFM // Disable poisoning when running with AutoRTFM, as poisoning cannot be safely rolled back.
114 ASAN_POISON_MEMORY_REGION(RestorePointer, static_cast<uint8*>(NextAllocationStart) - static_cast<uint8*>(RestorePointer));
115#endif
116
117 NextAllocationStart = RestorePointer;
118 if (UNLIKELY(NextAllocationStart == VirtualMemory.GetVirtualPointer() && DecommitMode != EVirtualStackAllocatorDecommitMode::AllOnDestruction))
119 {
120 DecommitUnusedPages();
121 }
122 }
123
124 // Frees unused pages according to the current decommit mode
125 void DecommitUnusedPages();
126
127 // VirtualMemory, NextUncommittedPage and RecentHighWaterMark must never be modified from the closed in AutoRTFM.
129 void* NextUncommittedPage = nullptr;
130 void* RecentHighWaterMark = nullptr;
131
132 void* NextAllocationStart = nullptr;
133
134 size_t TotalReservationSize;
135 const size_t PageSize;
136
138};
#define check(expr)
Definition AssertionMacros.h:314
#define ASAN_POISON_MEMORY_REGION(addr, size)
Definition ConcurrentLinearAllocator.h:31
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
#define UNLIKELY(x)
Definition Platform.h:857
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
EVirtualStackAllocatorDecommitMode
Definition VirtualStackAllocator.h:34
uint32 Size
Definition VulkanMemory.cpp:4034
uint8_t uint8
Definition binka_ue_file_header.h:8
Definition AndroidPlatformMemory.h:38
UE_FORCEINLINE_HINT void * GetVirtualPointer() const
Definition GenericPlatformMemory.h:496
Definition VirtualStackAllocator.h:72
size_t GetCommittedBytes() const
Definition VirtualStackAllocator.h:94
UE_FORCEINLINE_HINT FScopedStackAllocatorBookmark CreateScopedBookmark()
Definition VirtualStackAllocator.h:81
CORE_API void * Allocate(size_t Size, size_t Alignment)
Definition VirtualStackAllocator.cpp:62
CORE_API ~FVirtualStackAllocator()
Definition VirtualStackAllocator.cpp:53
size_t GetAllocatedBytes() const
Definition VirtualStackAllocator.h:89
Definition VirtualStackAllocator.h:47
CORE_API ~FScopedStackAllocatorBookmark()
Definition VirtualStackAllocator.cpp:23