shared: add nm_assert() to "nm-std-aux.h"

Having assertion macros that are disabled by default, is not
only useful for our glib code, but should also be available
for nm-std-aux. Move the macros.
This commit is contained in:
Thomas Haller
2020-07-04 18:44:01 +02:00
parent 5dd923cd86
commit 030d68aef7
2 changed files with 65 additions and 25 deletions

View File

@@ -1038,31 +1038,11 @@ nm_str_realloc (char *str)
/*****************************************************************************/ /*****************************************************************************/
/* glib/C provides the following kind of assertions: /* redefine assertions to use g_assert*() */
* - assert() -- disable with NDEBUG #undef _nm_assert_call
* - g_return_if_fail() -- disable with G_DISABLE_CHECKS #undef _nm_assert_call_not_reached
* - g_assert() -- disable with G_DISABLE_ASSERT #define _nm_assert_call(cond) g_assert(cond)
* but they are all enabled by default and usually even production builds have #define _nm_assert_call_not_reached() g_assert_not_reached()
* these kind of assertions enabled. It also means, that disabling assertions
* is an untested configuration, and might have bugs.
*
* Add our own assertion macro nm_assert(), which is disabled by default and must
* be explicitly enabled. They are useful for more expensive checks or checks that
* depend less on runtime conditions (that is, are generally expected to be true). */
#ifndef NM_MORE_ASSERTS
#define NM_MORE_ASSERTS 0
#endif
#if NM_MORE_ASSERTS
#define nm_assert(cond) G_STMT_START { g_assert (cond); } G_STMT_END
#define nm_assert_se(cond) G_STMT_START { if (G_LIKELY (cond)) { ; } else { g_assert (FALSE && (cond)); } } G_STMT_END
#define nm_assert_not_reached() G_STMT_START { g_assert_not_reached (); } G_STMT_END
#else
#define nm_assert(cond) G_STMT_START { if (FALSE) { if (cond) { } } } G_STMT_END
#define nm_assert_se(cond) G_STMT_START { if (G_LIKELY (cond)) { ; } } G_STMT_END
#define nm_assert_not_reached() G_STMT_START { ; } G_STMT_END
#endif
/* Usage: /* Usage:
* *

View File

@@ -3,4 +3,64 @@
#ifndef __NM_STD_AUX_H__ #ifndef __NM_STD_AUX_H__
#define __NM_STD_AUX_H__ #define __NM_STD_AUX_H__
#include <assert.h>
/*****************************************************************************/
#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
#define NM_BOOLEAN_EXPR(expr) \
({ \
int _g_boolean_var_; \
\
if (expr) \
_g_boolean_var_ = 1; \
else \
_g_boolean_var_ = 0; \
_g_boolean_var_; \
})
#define NM_LIKELY(expr) (__builtin_expect (NM_BOOLEAN_EXPR (expr), 1))
#define NM_UNLIKELY(expr) (__builtin_expect (NM_BOOLEAN_EXPR (expr), 0))
#else
#define NM_LIKELY(expr) NM_BOOLEAN_EXPR (expr)
#define NM_UNLIKELY(expr) NM_BOOLEAN_EXPR (expr)
#endif
/*****************************************************************************/
/* glib/C provides the following kind of assertions:
* - assert() -- disable with NDEBUG
* - g_return_if_fail() -- disable with G_DISABLE_CHECKS
* - g_assert() -- disable with G_DISABLE_ASSERT
* but they are all enabled by default and usually even production builds have
* these kind of assertions enabled. It also means, that disabling assertions
* is an untested configuration, and might have bugs.
*
* Add our own assertion macro nm_assert(), which is disabled by default and must
* be explicitly enabled. They are useful for more expensive checks or checks that
* depend less on runtime conditions (that is, are generally expected to be true). */
#ifndef NM_MORE_ASSERTS
#define NM_MORE_ASSERTS 0
#endif
#ifndef _nm_assert_call
#define _nm_assert_call(cond) assert(cond)
#define _nm_assert_call_not_reached() assert(0)
#endif
#if NM_MORE_ASSERTS
#define nm_assert(cond) do { _nm_assert_call (cond); } while (0)
#define nm_assert_se(cond) do { if (NM_LIKELY (cond)) { ; } else { _nm_assert_call (0 && (cond)); } } while (0)
#define nm_assert_not_reached() do { _nm_assert_call_not_reached (); } while (0)
#else
#define nm_assert(cond) do { if (0) { if (cond) { } } } while (0)
#define nm_assert_se(cond) do { if (NM_LIKELY (cond)) { ; } } while (0)
#define nm_assert_not_reached() do { ; } while (0)
#endif
#define NM_STATIC_ASSERT(cond) static_assert(cond, "")
#define NM_STATIC_ASSERT_EXPR(cond) ({ NM_STATIC_ASSERT (cond); 1; })
/*****************************************************************************/
#endif /* __NM_STD_AUX_H__ */ #endif /* __NM_STD_AUX_H__ */