From 709f23169da4ad49aaf212a4956d598e8c41f73a Mon Sep 17 00:00:00 2001 From: Adam Kupczyk Date: Thu, 15 Feb 2018 14:12:36 +0100 Subject: [PATCH] common: Make code to invoke assert() smaller. Signed-off-by: Adam Kupczyk --- src/common/assert.cc | 5 +++++ src/include/assert.h | 37 +++++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/common/assert.cc b/src/common/assert.cc index 7f8bb58140718..0bc04a385a8b2 100644 --- a/src/common/assert.cc +++ b/src/common/assert.cc @@ -66,6 +66,11 @@ namespace ceph { abort(); } + void __ceph_assert_fail(const assert_data &ctx) + { + __ceph_assert_fail(ctx.assertion, ctx.file, ctx.line, ctx.function); + } + void __ceph_assertf_fail(const char *assertion, const char *file, int line, const char *func, const char* msg, ...) { diff --git a/src/include/assert.h b/src/include/assert.h index f3e0e39f9b29d..e9505336062d6 100644 --- a/src/include/assert.h +++ b/src/include/assert.h @@ -66,8 +66,18 @@ struct BackTrace; extern void register_assert_context(CephContext *cct); #endif +struct assert_data { + const char *assertion; + const char *file; + const int line; + const char *function; +}; + extern void __ceph_assert_fail(const char *assertion, const char *file, int line, const char *function) __attribute__ ((__noreturn__)); +extern void __ceph_assert_fail(const assert_data &ctx) + __attribute__ ((__noreturn__)); + extern void __ceph_assertf_fail(const char *assertion, const char *file, int line, const char *function, const char* msg, ...) __attribute__ ((__noreturn__)); extern void __ceph_assert_warn(const char *assertion, const char *file, int line, const char *function); @@ -121,21 +131,28 @@ using namespace ceph; #undef __ASSERT_FUNCTION #define __ASSERT_FUNCTION -#define assert(expr) \ - ((expr) \ - ? _CEPH_ASSERT_VOID_CAST (0) \ - : __ceph_assert_fail (__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION)) +#define assert(expr) \ + do { static const ceph::assert_data assert_data_ctx = \ + {__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION}; \ + ((expr) \ + ? _CEPH_ASSERT_VOID_CAST (0) \ + : __ceph_assert_fail(assert_data_ctx)); } while(false) + #define ceph_assert(expr) \ - ((expr) \ - ? _CEPH_ASSERT_VOID_CAST (0) \ - : __ceph_assert_fail (__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION)) + do { static const ceph::assert_data assert_data_ctx = \ + {__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION}; \ + ((expr) \ + ? _CEPH_ASSERT_VOID_CAST (0) \ + : __ceph_assert_fail(assert_data_ctx)); } while(false) // this variant will *never* get compiled out to NDEBUG in the future. // (ceph_assert currently doesn't either, but in the future it might.) #define ceph_assert_always(expr) \ - ((expr) \ - ? _CEPH_ASSERT_VOID_CAST (0) \ - : __ceph_assert_fail (__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION)) + do { static const ceph::assert_data assert_data_ctx = \ + {__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION}; \ + ((expr) \ + ? _CEPH_ASSERT_VOID_CAST (0) \ + : __ceph_assert_fail(assert_data_ctx)); } while(false) // Named by analogy with printf. Along with an expression, takes a format // string and parameters which are printed if the assertion fails. -- 2.39.5