[sheepdog] [PATCH] move assert definition from util.h to logger.h

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Thu Jul 11 06:11:57 CEST 2013


If we don't use a logger, we don't need to define our assert.  In that
sense, assert() should be defined in logger.h.  The only problem is
that assert can be used before including logger.h.  To avoid it, this
patch adds an #error directive to detect assert definition before
logger.h.

This fixes a compiler error which happens when NDEBUG is not defined.

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 include/compiler.h       |  3 +++
 include/logger.h         | 14 +++++++++-----
 include/sheep.h          |  2 +-
 include/sheepdog_proto.h |  3 ++-
 include/util.h           | 24 +++++-------------------
 lib/event.c              |  2 +-
 lib/net.c                |  2 +-
 lib/util.c               |  3 +--
 lib/work.c               |  2 +-
 sheep/sheep_priv.h       |  1 +
 sheepfs/volume.c         |  1 -
 shepherd/shepherd.c      |  1 -
 12 files changed, 25 insertions(+), 33 deletions(-)

diff --git a/include/compiler.h b/include/compiler.h
index 1cd2440..b2ae002 100644
--- a/include/compiler.h
+++ b/include/compiler.h
@@ -17,4 +17,7 @@
 
 #define __printf(a, b) __attribute__((format(printf, a, b)))
 
+/* Force a compilation error if the condition is true */
+#define BUILD_BUG_ON(condition) ((void)sizeof(struct { int: -!!(condition); }))
+
 #endif	/* SD_COMPILER_H */
diff --git a/include/logger.h b/include/logger.h
index c2c4237..9bc3bbe 100644
--- a/include/logger.h
+++ b/include/logger.h
@@ -81,10 +81,14 @@ void sd_backtrace(void);
 		log_write(SDOG_DEBUG, __func__, __LINE__, fmt, ##args);	\
 	} while (0)
 
-static inline void do_assert(const char *expr, const char *func, int line)
-{
-	log_write(SDOG_EMERG, func, line, "Asserting `%s' failed.", expr);
-	abort();
-}
+#ifdef assert
+#error "logger.h must be included before assert.h"
+#endif /* assert */
+
+#ifndef NDEBUG
+#define assert(expr) ((expr) ? (void)0 : panic("Asserting `%s' failed.", #expr))
+#else
+#define assert(expr) ((void)0)
+#endif	/* NDEBUG */
 
 #endif	/* LOG_H */
diff --git a/include/sheep.h b/include/sheep.h
index 9c7f96d..0eed224 100644
--- a/include/sheep.h
+++ b/include/sheep.h
@@ -13,10 +13,10 @@
 
 #include <stdint.h>
 #include "internal_proto.h"
+#include "logger.h"
 #include "util.h"
 #include "list.h"
 #include "net.h"
-#include "logger.h"
 
 struct sd_vnode {
 	struct node_id  nid;
diff --git a/include/sheepdog_proto.h b/include/sheepdog_proto.h
index 156457a..4e302e9 100644
--- a/include/sheepdog_proto.h
+++ b/include/sheepdog_proto.h
@@ -13,9 +13,10 @@
 
 #include <inttypes.h>
 #include <stdint.h>
+#include <stdbool.h>
 #include <linux/limits.h>
 
-#include "util.h"
+#include "compiler.h"
 
 #define SD_PROTO_VER 0x02
 
diff --git a/include/util.h b/include/util.h
index 6dca655..47e3396 100644
--- a/include/util.h
+++ b/include/util.h
@@ -11,6 +11,11 @@
 #include <search.h>
 #include <urcu/uatomic.h>
 
+#ifndef assert
+/* If we don't have our assert, use standard assert. */
+#include <assert.h>
+#endif /* assert */
+
 #include "bitops.h"
 #include "list.h"
 #include "compiler.h"
@@ -157,21 +162,6 @@ int atomic_create_and_write(const char *path, char *buf, size_t len);
 	__removed;							\
 })
 
-#ifdef assert
-#undef assert
-#endif
-
-#ifndef NDEBUG
-
-#define assert(expr) ((expr) ?				\
-			(void)0 : do_assert(#expr, __func__, __LINE__))
-
-#else
-
-#define assert(expr) ((void)0)
-
-#endif	/* NDEBUG */
-
 /* urcu helpers */
 
 /* Boolean data type which can be accessed by multiple threads */
@@ -265,8 +255,4 @@ static inline bool is_stdout_console(void)
 extern mode_t sd_def_fmode;
 extern mode_t sd_def_dmode;
 
-
-/* Force a compilation error if the condition is true */
-#define BUILD_BUG_ON(condition) ((void)sizeof(struct { int: -!!(condition); }))
-
 #endif
diff --git a/lib/event.c b/lib/event.c
index 3f29d97..faca3a4 100644
--- a/lib/event.c
+++ b/lib/event.c
@@ -17,9 +17,9 @@
 #include <sys/timerfd.h>
 
 #include "list.h"
+#include "logger.h"
 #include "util.h"
 #include "event.h"
-#include "logger.h"
 
 static int efd;
 static LIST_HEAD(events_list);
diff --git a/lib/net.c b/lib/net.c
index d3e8e91..bce8498 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -29,10 +29,10 @@
 
 #include "sheepdog_proto.h"
 #include "sheep.h"
+#include "logger.h"
 #include "util.h"
 #include "event.h"
 #include "net.h"
-#include "logger.h"
 
 int conn_tx_off(struct connection *conn)
 {
diff --git a/lib/util.c b/lib/util.c
index 6fd3817..f16fe2e 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -18,14 +18,13 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <stdio.h>
-#include <assert.h>
 #include <ctype.h>
 #include <signal.h>
 #include <sys/xattr.h>
 #include <fcntl.h>
 
-#include "util.h"
 #include "logger.h"
+#include "util.h"
 
 mode_t sd_def_dmode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP;
 mode_t sd_def_fmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
diff --git a/lib/work.c b/lib/work.c
index b09c0db..fafd35d 100644
--- a/lib/work.c
+++ b/lib/work.c
@@ -28,9 +28,9 @@
 #include <linux/types.h>
 
 #include "list.h"
+#include "logger.h"
 #include "util.h"
 #include "work.h"
-#include "logger.h"
 #include "event.h"
 
 /*
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index 546f152..bb8292d 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -34,6 +34,7 @@
 #include "sheepdog_proto.h"
 #include "event.h"
 #include "logger.h"
+#include "util.h"
 #include "work.h"
 #include "net.h"
 #include "sheep.h"
diff --git a/sheepfs/volume.c b/sheepfs/volume.c
index 4000e97..f387962 100644
--- a/sheepfs/volume.c
+++ b/sheepfs/volume.c
@@ -18,7 +18,6 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <time.h>
-#include <assert.h>
 #include <urcu/uatomic.h>
 #include <pthread.h>
 
diff --git a/shepherd/shepherd.c b/shepherd/shepherd.c
index 6e102b3..f85832f 100644
--- a/shepherd/shepherd.c
+++ b/shepherd/shepherd.c
@@ -15,7 +15,6 @@
 #include <stdbool.h>
 #include <stdint.h>
 #include <errno.h>
-#include <assert.h>
 #include <getopt.h>
 
 #include <unistd.h>
-- 
1.8.1.3.566.gaa39828




More information about the sheepdog mailing list