From: Lennart Poettering Date: Thu, 10 Oct 2013 22:45:47 +0000 (+0200) Subject: macro: add new assert_return() macro for early parameter checking in functions X-Git-Tag: v209~1939 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=18387b5983150181dd9dee8edf1573285eecbaa4;p=elogind.git macro: add new assert_return() macro for early parameter checking in functions 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. --- diff --git a/src/shared/macro.h b/src/shared/macro.h index d4f92b60e..06e16cd45 100644 --- a/src/shared/macro.h +++ b/src/shared/macro.h @@ -159,6 +159,12 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) { } 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)))