UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ApplePlatformMemory.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3
4/*=============================================================================================
5 ApplePlatformMemory.h: Apple platform memory functions common across all Apple OSes
6==============================================================================================*/
7
8#pragma once
10#include <libkern/OSAtomic.h>
11#include <Foundation/NSObject.h>
12
13#include <mach/mach.h>
14
16
17
18// Support for passing LLM alloc data to XCode Instruments.
19// Use USE_APPLE_SUPPORT_INSTRUMENTED_ALLOCS to enable it.
20// As APPLE_SUPPORT_INSTRUMENTED_ALLOCS define only enables the LLM API, because changes to this header trigger wide recompilation.
21#ifndef APPLE_SUPPORT_INSTRUMENTED_ALLOCS
22 #define APPLE_SUPPORT_INSTRUMENTED_ALLOCS (!UE_BUILD_SHIPPING)
23#endif // APPLE_SUPPORT_INSTRUMENTED_ALLOCS
24
30OBJC_EXPORT @interface FApplePlatformObject : NSObject
31{
32@private
33 OSQueueHead* AllocatorPtr;
34}
35
37+ (nullable OSQueueHead*)classAllocator;
38
40+ (id)allocClass: (Class)NewClass;
41
43- (void)dealloc;
44
45@end
46
47#define APPLE_PLATFORM_OBJECT_ALLOC_OVERRIDES(ClassName) \
48+ (nullable OSQueueHead*)classAllocator \
49{ \
50 static OSQueueHead Queue = OS_ATOMIC_QUEUE_INIT; \
51 return &Queue; \
52} \
53+ (id)allocWithZone:(NSZone*) Zone \
54{ \
55 return (ClassName*)[FApplePlatformObject allocClass:self]; \
56} \
57+ (id)alloc \
58{ \
59 return (ClassName*)[FApplePlatformObject allocClass:self]; \
60}
61
66{
71
73 {
75 {
76 // if platform doesn't make use of MemoryPressureStatus, use default implementation
78 }
79 else
80 {
82 }
83 }
84
86};
87
92{
93 //~ Begin FGenericPlatformMemory Interface
94 static void Init();
98 static FMalloc* BaseAllocator();
99 static bool PageProtect(void* const Ptr, const SIZE_T Size, const bool bCanRead, const bool bCanWrite);
100 static void* BinnedAllocFromOS(SIZE_T Size);
101 static void BinnedFreeToOS(void* Ptr, SIZE_T Size);
102 static bool PtrIsOSMalloc( void* Ptr);
103 static bool PtrIsFromNanoMalloc( void* Ptr);
104 static bool IsNanoMallocAvailable();
105 static void NanoMallocInit();
106 static void SetAllocatorToUse();
107
109 {
110 public:
111
115
122
123 void Commit(size_t InOffset, size_t InSize);
124 void Decommit(size_t InOffset, size_t InSize);
125 void FreeVirtual();
126
128 {
129 Commit(size_t(((uint8*)InPtr) - ((uint8*)Ptr)), InSize);
130 }
131
133 {
134 Decommit(size_t(((uint8*)InPtr) - ((uint8*)Ptr)), InSize);
135 }
136
138 {
139 Commit(0, GetActualSize());
140 }
141
143 {
144 Decommit(0, GetActualSize());
145 }
146
148 {
149 return VMSizeDivVirtualSizeAlignment * GetVirtualSizeAlignment();
150 }
151
152 static FPlatformVirtualMemoryBlock AllocateVirtual(size_t Size, size_t InAlignment = FPlatformVirtualMemoryBlock::GetVirtualSizeAlignment());
153 static size_t GetCommitAlignment();
154 static size_t GetVirtualSizeAlignment();
155 };
156
157 static bool GetLLMAllocFunctions(void* _Nonnull (* _Nonnull &OutAllocFunction)(size_t), void(* _Nonnull &OutFreeFunction)(void*, size_t), int32& OutAlignment);
158#if APPLE_SUPPORT_INSTRUMENTED_ALLOCS
159 static void OnLowLevelMemory_Alloc(void const* Pointer, uint64 Size, uint64 Tag);
160 static void OnLowLevelMemory_Free(void const* Pointer, uint64 Size, uint64 Tag);
161#endif // APPLE_SUPPORT_INSTRUMENTED_ALLOCS
162 //~ End FGenericPlatformMemory Interface
163
165 static void ConfigureDefaultCFAllocator(void);
166
167 static bool CanOverallocateVirtualMemory();
168
171};
172
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
@ BinnedAllocFromOS
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
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
uint32 Size
Definition VulkanMemory.cpp:4034
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition ApplePlatformMemory.h:109
UE_FORCEINLINE_HINT void Commit()
Definition ApplePlatformMemory.h:137
UE_FORCEINLINE_HINT void CommitByPtr(void *InPtr, size_t InSize)
Definition ApplePlatformMemory.h:127
UE_FORCEINLINE_HINT void Decommit()
Definition ApplePlatformMemory.h:142
UE_FORCEINLINE_HINT size_t GetActualSize() const
Definition ApplePlatformMemory.h:147
UE_FORCEINLINE_HINT void DecommitByPtr(void *InPtr, size_t InSize)
Definition ApplePlatformMemory.h:132
FPlatformVirtualMemoryBlock(void *InPtr, uint32 InVMSizeDivVirtualSizeAlignment)
Definition ApplePlatformMemory.h:116
FPlatformVirtualMemoryBlock()
Definition ApplePlatformMemory.h:112
FPlatformVirtualMemoryBlock(const FPlatformVirtualMemoryBlock &Other)=default
FPlatformVirtualMemoryBlock & operator=(const FPlatformVirtualMemoryBlock &Other)=default
Definition ApplePlatformMemory.cpp:106
Definition GenericPlatformMemory.h:469
Definition MemoryBase.h:99
Definition ApplePlatformMemory.h:92
static vm_address_t NanoRegionEnd
Definition ApplePlatformMemory.h:170
static void OnLowLevelMemory_Free(void const *Pointer, uint64 Size, uint64 Tag)
static void OnLowLevelMemory_Alloc(void const *Pointer, uint64 Size, uint64 Tag)
static vm_address_t NanoRegionStart
Definition ApplePlatformMemory.h:169
Definition GenericPlatformMemory.h:87
Definition GenericPlatformMemory.h:133
CORE_API EMemoryPressureStatus GetMemoryPressureStatus() const
Definition GenericPlatformMemory.cpp:163
EMemoryPressureStatus
Definition GenericPlatformMemory.h:155
Definition GenericPlatformMemory.h:225
static CORE_API void BinnedFreeToOS(void *Ptr, SIZE_T Size)
static CORE_API FMalloc * BaseAllocator()
Definition GenericPlatformMemory.cpp:276
static CORE_API uint64 GetMemoryUsedFast()
Definition GenericPlatformMemory.cpp:300
static bool PtrIsOSMalloc(void *Ptr)
Definition GenericPlatformMemory.h:437
static bool IsNanoMallocAvailable()
Definition GenericPlatformMemory.h:450
static CORE_API bool PageProtect(void *const Ptr, const SIZE_T Size, const bool bCanRead, const bool bCanWrite)
Definition GenericPlatformMemory.cpp:336
static CORE_API void Init()
Definition GenericPlatformMemory.cpp:202
static CORE_API FPlatformMemoryStats GetStats()
Definition GenericPlatformMemory.cpp:289
static UE_FORCEINLINE_HINT bool CanOverallocateVirtualMemory()
Definition GenericPlatformMemory.h:832
static void NanoMallocInit()
Definition GenericPlatformMemory.h:424
static CORE_API const FPlatformMemoryConstants & GetConstants()
Definition GenericPlatformMemory.cpp:324
static CORE_API bool GetLLMAllocFunctions(void *(*&OutAllocFunction)(size_t), void(*&OutFreeFunction)(void *, size_t), int32 &OutAlignment)
Definition GenericPlatformMemory.cpp:573
static bool PtrIsFromNanoMalloc(void *Ptr)
Definition GenericPlatformMemory.h:463
Definition AndroidPlatformMemory.h:15
FPlatformMemoryStats()
Definition ApplePlatformMemory.h:67
EMemoryPressureStatus MemoryPressureStatus
Definition ApplePlatformMemory.h:85
EMemoryPressureStatus GetMemoryPressureStatus() const
Definition ApplePlatformMemory.h:72