aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorthenewone <thenewone@yandex-team.com>2024-11-12 12:02:01 +0300
committerthenewone <thenewone@yandex-team.com>2024-11-12 12:15:18 +0300
commitb218405128bf0cd7ef71718461548565db698398 (patch)
tree46f74d986b39a4db0ac93b64ac9e74b541600302
parent2c567f233fc0c2c2d051051acd04bd0c558e382b (diff)
downloadydb-b218405128bf0cd7ef71718461548565db698398.tar.gz
YT-23346: Introduce YT_ASSUME and YT_UNREACHABLE macros.
Y_ASSUME and Y_UNREACHABLE macros are platform-independent ways to give a hint for a compiler about some value or state. Compilier may use that hint for optimization. On the other hand if the hint is wrong it may lead to undefined behavior. YT_ASSUME and YT_UNREACHABLE are wrappers around Y_ASSUME and Y_UNREACHABLE that actually check that the hint is correct in debug build. That introduces some code safety and at the same time allows certain optimization in optimized build. commit_hash:2d0969361910a10a870bae226d838d494e656edb
-rw-r--r--library/cpp/yt/assert/assert.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/library/cpp/yt/assert/assert.h b/library/cpp/yt/assert/assert.h
index 7a9e761a3a..f2131d5bbc 100644
--- a/library/cpp/yt/assert/assert.h
+++ b/library/cpp/yt/assert/assert.h
@@ -55,6 +55,20 @@ void AssertTrapImpl(
} \
} while (false)
+//! Behaves as |YT_ASSERT| in debug mode and as |Y_ASSUME| in release.
+#ifdef NDEBUG
+ #define YT_ASSUME(expr) Y_ASSUME(expr)
+#else
+ #define YT_ASSUME(expr) YT_ASSERT(expr)
+#endif
+
+//! Behaves as |YT_ASSERT(false)| in debug mode and as |Y_UNREACHABLE| in release.
+#ifdef NDEBUG
+ #define YT_UNREACHABLE() Y_UNREACHABLE()
+#else
+ #define YT_UNREACHABLE() YT_ASSERT(false)
+#endif
+
//! Fatal error code marker. Abnormally terminates the current process.
#ifdef YT_COMPILING_UDF
#define YT_ABORT() __YT_BUILTIN_ABORT()