parlib: Add parlib_assert_perror()
authorBarret Rhoden <brho@cs.berkeley.edu>
Thu, 22 Dec 2016 22:36:06 +0000 (17:36 -0500)
committerBarret Rhoden <brho@cs.berkeley.edu>
Tue, 10 Jan 2017 00:01:40 +0000 (19:01 -0500)
Unlike glibc's lousy assert_perror(), which just takes an errno (0 being
OK), this actually takes an assertion statement, like assert().  On
assertion failure, it does a perror() and exits.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
user/parlib/include/parlib/assert.h

index cde95d9..ca8758e 100644 (file)
@@ -27,6 +27,16 @@ void _assert_failed(const char *file, int line, const char *msg)
                        _assert_failed(__FILE__, __LINE__, #x);                            \
        } while (0)
 
+/* This is better than glibc's assert_perror(), but in the interest of not
+ * causing confusion, I'll rename ours. */
+#define parlib_assert_perror(x)                                                \
+       do {                                                                       \
+               if (!(x)) {                                                            \
+                       perror("");                                                        \
+                       _assert_failed(__FILE__, __LINE__, #x);                            \
+               }                                                                      \
+       } while (0)
+
 // parlib_static_assert(x) will generate a compile-time error if 'x' is false.
 #define parlib_static_assert(x)        switch (x) case 0: case (x):