]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: Make code to invoke assert() smaller. 20445/head
authorAdam Kupczyk <akupczyk@redhat.com>
Thu, 15 Feb 2018 13:12:36 +0000 (14:12 +0100)
committerAdam Kupczyk <akupczyk@redhat.com>
Mon, 5 Mar 2018 10:47:38 +0000 (11:47 +0100)
Signed-off-by: Adam Kupczyk <akupczyk@redhat.com>
src/common/assert.cc
src/include/assert.h

index 7f8bb58140718df5f8fcf996716cea02e813e38c..0bc04a385a8b27ac246d387a258d3a1fbba13f04 100644 (file)
@@ -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, ...)
   {
index f3e0e39f9b29dab2ed5603e49e0e27d5116b86bf..e9505336062d6e8237eb7316bcb3020a892c9041 100644 (file)
@@ -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.