UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MemoryArena.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"
6
7#if !defined(UE_RESTRICT)
8# if defined _MSC_VER
9# define UE_RESTRICT __declspec(restrict)
10# else
11# define UE_RESTRICT
12# endif
13#endif
14
15#if !defined(UE_NOALIAS)
16# if defined _MSC_VER
17# define UE_NOALIAS __declspec(noalias)
18# else
19# define UE_NOALIAS
20# endif
21#endif
22
23#define UE_WITH_HEAPARENA 0
24#define UE_WITH_ARENAMAP 0
25
26class FHeapArena;
27class FMemoryArena;
28
42{
43public:
44 FArenaPointer() = default;
45
46 enum { NoTag = 0 };
47
48#if PLATFORM_32BITS
49private:
50 void* Ptr = 0;
51 uint16 ArenaTag = 0;
52
53public:
54 UE_FORCEINLINE_HINT const uint16 GetArenaIndex() const { return ArenaTag; }
55 UE_FORCEINLINE_HINT void* GetPointer() { return Ptr; }
56 UE_FORCEINLINE_HINT const void* GetPointer() const { return Ptr; }
58
60
61#else
62
63private:
64 static constexpr int ArenaShift = 48;
65 static constexpr uint64 ArenaMask = 0xffff000000000000;
66 static constexpr uint64 PointerMask = 0x0000ffffFFFFffff;
67
68 void* TaggedPointer = nullptr;
69
70public:
71 UE_FORCEINLINE_HINT const uint16 GetArenaIndex() const { return uint16(UPTRINT(TaggedPointer) >> ArenaShift); }
72 UE_FORCEINLINE_HINT void* GetPointer() { return reinterpret_cast<void*>(UPTRINT(TaggedPointer) & PointerMask); }
73 UE_FORCEINLINE_HINT const void* GetPointer() const { return reinterpret_cast<const void*>(UPTRINT(TaggedPointer) & PointerMask); }
74 UE_FORCEINLINE_HINT void SetPointerAndArena(void* InPtr, uint16 InArenaTag) { TaggedPointer = reinterpret_cast<void*>(UPTRINT(InPtr) | (UPTRINT(InArenaTag) << ArenaShift)); }
75
76 inline FArenaPointer(void* Ptr, uint16 ArenaIndex) : TaggedPointer(reinterpret_cast<void*>(UPTRINT(Ptr) | (UPTRINT(ArenaIndex) << ArenaShift))) {};
77#endif
78
79 inline operator bool() const { return !!GetPointer(); }
80
81 void Free() const;
82
84};
85
86template<typename T>
88{
89public:
90 TArenaPointer() = default;
92
93 inline TArenaPointer& operator=(T* Rhs) { SetPointerAndArena(Rhs, NoTag); return *this; }
94
95 inline operator T* () { return reinterpret_cast<T*>(GetPointer()); }
96 inline operator const T* () const { return reinterpret_cast<const T*>(GetPointer()); }
97 inline T* operator -> () { return reinterpret_cast<T*>(GetPointer()); }
98 inline const T* operator -> () const { return reinterpret_cast<const T*>(GetPointer()); }
99};
100
104{
105public:
107 CORE_API virtual ~FMemoryArena();
108
109 FMemoryArena(const FMemoryArena&) = delete;
111
113 CORE_API UE_NOALIAS void Free(const void* MemoryBlock);
114
115 CORE_API SIZE_T GetBlockSize(const void* MemoryBlock) const;
116 CORE_API const TCHAR* GetDebugName() const;
117
118private:
119 virtual void* InternalAlloc(SIZE_T Size, SIZE_T Alignment) = 0;
120 CORE_API virtual void InternalFree(const void* MemoryBlock, SIZE_T MemoryBlockSize);
121 virtual SIZE_T InternalGetBlockSize(const void* MemoryBlock) const = 0;
122
123 CORE_API virtual const TCHAR* InternalGetDebugName() const;
124
125 enum { FlagNoFree = 1 << 0 };
126
127 uint16 ArenaFlags = 0;
128
129public:
131};
132
133inline void FArenaPointer::Free() const
134{
135 return Arena().Free(GetPointer());
136}
137
138// These are somewhat temporary, to support (experimental) arena-based container allocators
139
141CORE_API FArenaPointer ArenaRealloc(FMemoryArena* Arena, void* InPtr, SIZE_T OldSize, SIZE_T NewSize, SIZE_T Alignment);
142
149#if UE_WITH_HEAPARENA
150class FHeapArena : public FMemoryArena
151{
152public:
155
156private:
157 CORE_API virtual void* InternalAlloc(SIZE_T Size, SIZE_T Alignment) override;
158 CORE_API virtual void InternalFree(const void* MemoryBlock, SIZE_T MemoryBlockSize) override;
159 CORE_API virtual SIZE_T InternalGetBlockSize(const void* MemoryBlock) const override;
160 CORE_API virtual const TCHAR* InternalGetDebugName() const override;
161
162 void* HeapHandle = nullptr;
163};
164#endif
165
171class FMallocArena final : public FMemoryArena
172{
173public:
176
177private:
178 CORE_API virtual void* InternalAlloc(SIZE_T Size, SIZE_T Alignment) override;
179 CORE_API virtual void InternalFree(const void* MemoryBlock, SIZE_T MemoryBlockSize) override;
180 CORE_API virtual SIZE_T InternalGetBlockSize(const void* MemoryBlock) const override;
181 CORE_API virtual const TCHAR* InternalGetDebugName() const override;
182};
183
189class FAnsiArena final : public FMemoryArena
190{
191public:
194
195private:
196 CORE_API virtual void* InternalAlloc(SIZE_T Size, SIZE_T Alignment) override;
197 CORE_API virtual void InternalFree(const void* MemoryBlock, SIZE_T MemoryBlockSize) override;
198 CORE_API virtual SIZE_T InternalGetBlockSize(const void* MemoryBlock) const override;
199 CORE_API virtual const TCHAR* InternalGetDebugName() const override;
200};
201
207#if UE_WITH_ARENAMAP
208class FArenaMap
209{
210public:
211 CORE_API static void Initialize();
212
213 // Reset the state of the arena map - this is here only for testing and benchmarking purposes
214 // and would never normally be called during normal execution
215 CORE_API static void Reset();
216
217 // Associate an arena with a virtual address range
219
220 // Mark range as unallocated
221 CORE_API static void ClearRange(const void* VaBase, SIZE_T VaSize);
222
223 // Map a virtual address to a memory arena
224 CORE_API static FMemoryArena* MapPtrToArena(const void* VaBase);
225};
226#endif
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
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
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
#define UE_RESTRICT
Definition MemoryArena.h:11
CORE_API FArenaPointer ArenaRealloc(FArenaPointer InPtr, SIZE_T OldSize, SIZE_T NewSize, SIZE_T Alignment)
Definition MemoryArena.cpp:213
#define UE_NOALIAS
Definition MemoryArena.h:19
const bool
Definition NetworkReplayStreaming.h:178
uint32 Size
Definition VulkanMemory.cpp:4034
uint16_t uint16
Definition binka_ue_file_header.h:7
Definition MemoryArena.h:190
CORE_API ~FAnsiArena()
CORE_API FAnsiArena()
Definition MemoryArena.h:42
UE_FORCEINLINE_HINT const uint16 GetArenaIndex() const
Definition MemoryArena.h:71
UE_FORCEINLINE_HINT void * GetPointer()
Definition MemoryArena.h:72
UE_FORCEINLINE_HINT const void * GetPointer() const
Definition MemoryArena.h:73
UE_FORCEINLINE_HINT void SetPointerAndArena(void *InPtr, uint16 InArenaTag)
Definition MemoryArena.h:74
FArenaPointer(void *Ptr, uint16 ArenaIndex)
Definition MemoryArena.h:76
FArenaPointer()=default
@ NoTag
Definition MemoryArena.h:46
void Free() const
Definition MemoryArena.h:133
CORE_API FMemoryArena & Arena() const
Definition MemoryArena.cpp:131
Definition MemoryArena.h:172
CORE_API FMallocArena()
Definition MemoryArena.cpp:256
CORE_API ~FMallocArena()
Definition MemoryArena.cpp:260
Definition MemoryArena.h:104
uint16 ArenaId
Definition MemoryArena.h:130
CORE_API SIZE_T GetBlockSize(const void *MemoryBlock) const
Definition MemoryArena.cpp:166
CORE_API const TCHAR * GetDebugName() const
Definition MemoryArena.cpp:171
CORE_API FMemoryArena()
Definition MemoryArena.cpp:140
FMemoryArena & operator=(const FMemoryArena &)=delete
CORE_API UE_RESTRICT UE_NOALIAS void * Alloc(SIZE_T Size, SIZE_T Alignment)
Definition MemoryArena.cpp:151
virtual CORE_API ~FMemoryArena()
Definition MemoryArena.cpp:145
CORE_API UE_NOALIAS void Free(const void *MemoryBlock)
Definition MemoryArena.cpp:156
FMemoryArena(const FMemoryArena &)=delete
Definition MemoryArena.h:88
TArenaPointer()=default
T * operator->()
Definition MemoryArena.h:97
TArenaPointer & operator=(T *Rhs)
Definition MemoryArena.h:93
TArenaPointer(T *Ptr, uint16 ArenaIndex)
Definition MemoryArena.h:91