]> granicus.if.org Git - libevent/commitdiff
Add an EVUTIL_ASSERT() to replace our calls to assert().
authorNick Mathewson <nickm@torproject.org>
Mon, 26 Oct 2009 20:00:08 +0000 (20:00 +0000)
committerNick Mathewson <nickm@torproject.org>
Mon, 26 Oct 2009 20:00:08 +0000 (20:00 +0000)
The big difference here is that EVUTIL_ASSERT() passes its message on
via event_errx() before aborting, so that the application has a prayer
of noticing and recording it.

svn:r1463

log-internal.h
log.c
util-internal.h

index 6468f08b3406afe74d7a334d083c4bf6f52dccbf..51e65516a91ec4f05ebf7ab70579596617268fea 100644 (file)
@@ -33,6 +33,8 @@
 #define EV_CHECK_FMT(a,b)
 #endif
 
+#define _EVENT_ERR_ABORT 0xdeaddead
+
 void event_err(int eval, const char *fmt, ...) EV_CHECK_FMT(2,3);
 void event_warn(const char *fmt, ...) EV_CHECK_FMT(1,2);
 void event_sock_err(int eval, int sock, const char *fmt, ...) EV_CHECK_FMT(3,4);
diff --git a/log.c b/log.c
index 292ec2f87f3ddc227be2cf9571168c533aebe2c9..8aa1d428a219dbab606876266e0488912064ad82 100644 (file)
--- a/log.c
+++ b/log.c
@@ -80,6 +80,8 @@ event_exit(int errcode)
 {
        if (fatal_fn)
                fatal_fn(errcode);
+       else if (errcode == _EVENT_ERR_ABORT)
+               abort();
        else
                exit(errcode);
 }
index f5580eccfe8597df2c3442b4cc9d79eaf246c796..853f22006a9aa544bba5ce28601a83dad85a53a3 100644 (file)
 #include "event-config.h"
 #include <errno.h>
 
+/* For EVUTIL_ASSERT */
+#include "log-internal.h"
+#include <stdio.h>
+#include <stdlib.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -132,6 +137,30 @@ int evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int sock
 
 int evutil_socket_finished_connecting(evutil_socket_t fd);
 
+/* Evaluates to the same boolean value as 'p', and hints to the compiler that
+ * we expect this value to be false. */
+#ifdef __GNUC__X
+#define EVUTIL_UNLIKELY(p) __builtin_expect(!!(p),0)
+#else
+#define EVUTIL_UNLIKELY(p) (p)
+#endif
+
+/* Replacement for assert() that calls event_errx on failure. */
+#define EVUTIL_ASSERT(cond)                                            \
+       do {                                                            \
+               if (EVUTIL_UNLIKELY(!(cond))) {                         \
+                       event_errx(_EVENT_ERR_ABORT,                    \
+                           "%s:%d: Assertion %s failed in %s",         \
+                           __FILE__,__LINE__,#cond,__func__);          \
+                       /* In case a user-supplied handler tries to */  \
+                       /* return control to us, log and abort here. */ \
+                       (void)fprintf(stderr,                           \
+                           "%s:%d: Assertion %s failed in %s",         \
+                           __FILE__,__LINE__,#cond,__func__);          \
+                       abort();                                        \
+               }                                                       \
+       } while(0)
+
 #ifdef __cplusplus
 }
 #endif