UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ScratchBuffer.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "Chaos/Core.h"
5#include "Containers/Array.h"
7#include <type_traits>
8
9#define CHAOS_SCRATCHBUFFER_CHECKSENTINEL (DO_CHECK)
10
11namespace Chaos
12{
13 namespace Private
14 {
15
16
18 {
19 private:
20 static const size_t SentinelValue = 0xA1B2C3D4A1B2C3D4ll;
21
22 public:
23
25 : BufferNext(nullptr)
26 , BufferBegin(nullptr)
27 , BufferEnd(nullptr)
28 {
29 }
30
32 {
33 DestroyBuffer();
34 }
35
36 size_t BufferSize() const
37 {
38 // NOTE: this size does not include the sentinel
39 return BufferEnd - BufferBegin;
40 }
41
42 void Empty()
43 {
44 DestroyBuffer();
45 }
46
47 void Reset(const size_t InMaxBytes)
48 {
49 if (InMaxBytes > BufferSize())
50 {
51 CreateBuffer(InMaxBytes);
52 }
53 BufferNext = BufferBegin;
54 }
55
56 template<typename T> T* AllocUninitialized()
57 {
58 static_assert(std::is_trivially_destructible_v<T>, "FScratchBuffer only supports trivially destructible types");
59
60 void* Address = AllocAligned(sizeof(T), alignof(T));
61 return (T*)Address;
62 }
63
64 template<typename T> T* AllocArrayUninitialized(const int32 Num)
65 {
66 static_assert(std::is_trivially_destructible_v<T>, "FScratchBuffer only supports trivially destructible types");
67
68 const size_t AlignedSize = Align(sizeof(T), alignof(T));
69 void* Address = AllocAligned(Num * AlignedSize, alignof(T));
70 return (T*)Address;
71 }
72
73 template<typename T, typename... TArgs> T* Alloc(TArgs... Args)
74 {
76 if (Object != nullptr)
77 {
78 new(Object) T(Args...);
79 }
80 return Object;
81 }
82
83 template<typename T, typename... TArgs> T* AllocArray(const int32 Num, TArgs... Args)
84 {
85 T* Objects = AllocArrayUninitialized<T>(Num);
86 if (Objects != nullptr)
87 {
88 for (int32 Index = 0; Index < Num; ++Index)
89 {
90 new(&Objects[Index]) T(Args...);
91 }
92 }
93 return Objects;
94 }
95
96 private:
97 // Allocate some bytes with the specified size and slignment
98 void* AllocAligned(const size_t InSize, const size_t InAlign)
99 {
100 uint8* const Address = Align(BufferNext, InAlign);
101
102 uint8* const NewBufferNext = Address + InSize;
103 if (NewBufferNext <= BufferEnd)
104 {
105 BufferNext = NewBufferNext;
106 return Address;
107 }
108
109 return nullptr;
110 }
111
112 void CreateBuffer(const int32 InMaxBytes)
113 {
114 CheckSentinel();
115
116 if (InMaxBytes != BufferSize())
117 {
118 DestroyBuffer();
119
120 if (InMaxBytes > 0)
121 {
122 // Over-allocate and store a sentinel after the block
123 BufferBegin = new uint8[InMaxBytes + sizeof(SentinelValue)];
124 }
125
126 if (BufferBegin != nullptr)
127 {
128 BufferEnd = BufferBegin + InMaxBytes;
129 }
130
131 InitSentinel();
132 }
133
134 BufferNext = BufferBegin;
135 }
136
137 void DestroyBuffer()
138 {
139 CheckSentinel();
140
141 if (BufferBegin != nullptr)
142 {
143 delete[] BufferBegin;
144 }
145
146 BufferBegin = nullptr;
147 BufferEnd = nullptr;
148 BufferNext = nullptr;
149 }
150
151 size_t* Sentinel()
152 {
153 return (size_t*)BufferEnd;
154 }
155
156 void InitSentinel()
157 {
158 if (Sentinel() != nullptr)
159 {
160 *Sentinel() = SentinelValue;
161 }
162 }
163
164 void CheckSentinel()
165 {
166#if CHAOS_SCRATCHBUFFER_CHECKSENTINEL
167 if (Sentinel() != nullptr)
168 {
169 check(*Sentinel() == SentinelValue);
170 }
171#endif
172 }
173
174 uint8* BufferNext;
175 uint8* BufferBegin;
176 uint8* BufferEnd;
177 };
178
179 }
180}
181
182#if UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_5
184#endif
constexpr T Align(T Val, uint64 Alignment)
Definition AlignmentTemplates.h:18
#define check(expr)
Definition AssertionMacros.h:314
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
@ Num
Definition MetalRHIPrivate.h:234
uint8_t uint8
Definition binka_ue_file_header.h:8
Definition ScratchBuffer.h:18
T * AllocArray(const int32 Num, TArgs... Args)
Definition ScratchBuffer.h:83
size_t BufferSize() const
Definition ScratchBuffer.h:36
~FScratchBuffer()
Definition ScratchBuffer.h:31
FScratchBuffer()
Definition ScratchBuffer.h:24
T * AllocArrayUninitialized(const int32 Num)
Definition ScratchBuffer.h:64
T * Alloc(TArgs... Args)
Definition ScratchBuffer.h:73
T * AllocUninitialized()
Definition ScratchBuffer.h:56
void Reset(const size_t InMaxBytes)
Definition ScratchBuffer.h:47
void Empty()
Definition ScratchBuffer.h:42
Definition SkeletalMeshComponent.h:307
Definition OverriddenPropertySet.cpp:45
U16 Index
Definition radfft.cpp:71