chiark / gitweb /
macro: add new assert_return() macro for early parameter checking in functions
authorLennart Poettering <lennart@poettering.net>
Thu, 10 Oct 2013 22:45:47 +0000 (00:45 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 10 Oct 2013 22:45:47 +0000 (00:45 +0200)
For the library functions we expose we currently repeatedly use checks
like the following:

if (!value_is_ok(parameter1))
        return -EINVAL;
if (!value_is_ok(parameter2))
        return -EINVAL;

And so on. Let's turn this into a macro:

assert_return(value_is_ok(parameter1), -EINVAL);
assert_return(value_is_ok(paramater2), -EINVAL);

This makes our code a bit shorter and simpler, and also allows us to add
a _unlikely_() around the check.

src/shared/macro.h

index d4f92b60ec83ac37a88d932cf817d3d71413a308..06e16cd4536de2dbc37f940253ad46616acfbd1d 100644 (file)
@@ -159,6 +159,12 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) {
         } while (false)
 #endif
 
         } while (false)
 #endif
 
+#define assert_return(expr, r)                    \
+        do {                                      \
+                if (!(expr))                      \
+                        return (r);               \
+        } while (false)
+
 #define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
 #define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
 #define PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
 #define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
 #define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
 #define PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))