diff options
author | Andreas Rheinhardt <andreas.rheinhardt@outlook.com> | 2021-10-03 14:03:25 +0200 |
---|---|---|
committer | Andreas Rheinhardt <andreas.rheinhardt@outlook.com> | 2025-05-21 10:45:23 +0200 |
commit | 004cc60f0e30a7d37499d60d7d0acaf43d3755a0 (patch) | |
tree | f95c36eface81517e2f214f7e110fbe19d861282 | |
parent | b6f84cd72acd84ba725922bd6a8967b416b2230a (diff) | |
download | ffmpeg-004cc60f0e30a7d37499d60d7d0acaf43d3755a0.tar.gz |
avutil/avassert: Add av_unreachable() and av_assume() macros
Useful to let the compiler and static analyzers know that
something is unreachable without adding an av_assert
(which would be either dead for the compiler or add runtime
overhead) for this.
The implementation used here enforces the use of a message
to provide a reason why a particular code is supposed to be
unreachable.
Reviewed-by: Ramiro Polla <ramiro.polla@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
-rw-r--r-- | doc/APIchanges | 3 | ||||
-rw-r--r-- | libavutil/avassert.h | 42 | ||||
-rw-r--r-- | libavutil/version.h | 2 |
3 files changed, 46 insertions, 1 deletions
diff --git a/doc/APIchanges b/doc/APIchanges index d0869561f3..91710bb27d 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -2,6 +2,9 @@ The last version increases of all libraries were on 2025-03-28 API changes, most recent first: +2025-05-21 - xxxxxxxxxx - lavu 60.3.100 - avassert.h + Add av_unreachable() and av_assume() macros. + 2025-02-xx - xxxxxxxxxx - lavfi 10.10.100 - avfilter.h Add avfilter_link_get_hw_frames_ctx(). diff --git a/libavutil/avassert.h b/libavutil/avassert.h index 1895fb7551..8dbdb01566 100644 --- a/libavutil/avassert.h +++ b/libavutil/avassert.h @@ -31,6 +31,7 @@ #ifdef HAVE_AV_CONFIG_H # include "config.h" #endif +#include "attributes.h" #include "log.h" #include "macros.h" @@ -75,4 +76,45 @@ */ void av_assert0_fpu(void); +/** + * Asserts that are used as compiler optimization hints depending + * upon ASSERT_LEVEL and NBDEBUG. + * + * Undefined behaviour occurs if execution reaches a point marked + * with av_unreachable() or if a condition used with av_assume() + * is false. + * + * The condition used with av_assume() should not have side-effects + * and should be visible to the compiler. + */ +#if defined(ASSERT_LEVEL) ? ASSERT_LEVEL > 0 : !defined(HAVE_AV_CONFIG_H) && !defined(NDEBUG) +#define av_unreachable(msg) \ +do { \ + av_log(NULL, AV_LOG_PANIC, \ + "Reached supposedly unreachable code at %s:%d: %s\n", \ + __FILE__, __LINE__, msg); \ + abort(); \ +} while (0) +#define av_assume(cond) av_assert0(cond) +#else +#if AV_GCC_VERSION_AT_LEAST(4, 5) || AV_HAS_BUILTIN(__builtin_unreachable) +#define av_unreachable(msg) __builtin_unreachable() +#elif defined(_MSC_VER) +#define av_unreachable(msg) __assume(0) +#define av_assume(cond) __assume(cond) +#elif __STDC_VERSION__ >= 202311L +#include <stddef.h> +#define av_unreachable(msg) unreachable() +#else +#define av_unreachable(msg) ((void)0) +#endif + +#ifndef av_assume +#define av_assume(cond) do { \ + if (!(cond)) \ + av_unreachable(); \ +} while (0) +#endif +#endif + #endif /* AVUTIL_AVASSERT_H */ diff --git a/libavutil/version.h b/libavutil/version.h index 4717cd562b..2979f80233 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -79,7 +79,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 60 -#define LIBAVUTIL_VERSION_MINOR 2 +#define LIBAVUTIL_VERSION_MINOR 3 #define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ |