UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
LongJump.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#if (defined(__AUTORTFM) && __AUTORTFM)
6
7#include "BuildMacros.h"
8#include "Utils.h"
9
10#include <setjmp.h>
11
12#if AUTORTFM_PLATFORM_WINDOWS && AUTORTFM_ARCHITECTURE_X64 // We only have an Windows X64 implementation right now.
13#define AUTORTFM_USE_SAVE_RESTORE_CONTEXT 1
14#else
15#define AUTORTFM_USE_SAVE_RESTORE_CONTEXT 0
16#endif
17
18namespace AutoRTFM
19{
20
21// setjmp/longjmp that doesn't do any unwinding (no C++ destructor calls, no messing with OS
22// signal states - just saving/restoring CPU state). Although it's the setjmp/longjmp everyone
23// knows and loves, it's exposed as a try/catch/throw API to make it less error-prone.
24class FLongJump
25{
26public:
27 FLongJump()
28 {
29 memset(this, 0, sizeof(*this));
30 }
31
32 template<typename TTryFunctor, typename TCatchFunctor>
34
35 [[noreturn]] void Throw();
36
37private:
38#if AUTORTFM_USE_SAVE_RESTORE_CONTEXT
39 static bool SaveContext(void* Buffer);
40 [[noreturn]] static void RestoreContext(void* Context);
41 static constexpr size_t ContextSize = 256;
42 static constexpr size_t ContextAlignment = 16;
43 alignas(ContextAlignment) std::byte Context[ContextSize];
44#else
46#endif
47 bool bIsSet;
48};
49
50template<typename TTryFunctor, typename TCatchFunctor>
51void FLongJump::TryCatch(const TTryFunctor& TryFunctor, const TCatchFunctor& CatchFunctor)
52{
53 AUTORTFM_ASSERT(!bIsSet);
54#if AUTORTFM_USE_SAVE_RESTORE_CONTEXT
55 if (!SaveContext(Context))
56#elif AUTORTFM_PLATFORM_WINDOWS
57 if (!setjmp(JmpBuf))
58#else
59 if (!_setjmp(JmpBuf))
60#endif
61 {
62 bIsSet = true;
63 TryFunctor();
64 bIsSet = false;
65 }
66 else
67 {
68 AUTORTFM_ASSERT(bIsSet);
69 bIsSet = false;
71 }
72}
73
74inline void FLongJump::Throw()
75{
76 AUTORTFM_ASSERT(bIsSet);
77#if AUTORTFM_USE_SAVE_RESTORE_CONTEXT
79#elif AUTORTFM_PLATFORM_WINDOWS
80 longjmp(JmpBuf, 1);
81#else
82 _longjmp(JmpBuf, 1);
83#endif
84}
85
86} // namespace AutoRTFM
87
88#endif // (defined(__AUTORTFM) && __AUTORTFM)
89
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition API.cpp:57