From 18387b5983150181dd9dee8edf1573285eecbaa4 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 11 Oct 2013 00:45:47 +0200 Subject: 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. --- src/shared/macro.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src') diff --git a/src/shared/macro.h b/src/shared/macro.h index d4f92b60ec..06e16cd453 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))) -- cgit v1.2.3-54-g00ecf