X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Fmacro.h;h=54b641be2361593e9d2b57625e5450aea626817f;hb=cabb78068899232c152f4585f19d023e373aa73d;hp=d3f4245d5d0c0a72f503e34850796a4a1af2908b;hpb=ca2871d9b027018c108e0cf7bbc4e5a919e300c3;p=elogind.git diff --git a/src/shared/macro.h b/src/shared/macro.h index d3f4245d5..54b641be2 100644 --- a/src/shared/macro.h +++ b/src/shared/macro.h @@ -53,6 +53,11 @@ #define XSTRINGIFY(x) #x #define STRINGIFY(x) XSTRINGIFY(x) +#define XCONCATENATE(x, y) x ## y +#define CONCATENATE(x, y) XCONCATENATE(x, y) + +#define UNIQUE(prefix) CONCATENATE(prefix, __LINE__) + /* Rounds up */ #define ALIGN4(l) (((l) + 3) & ~3) @@ -143,24 +148,14 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) { } while (false) #if defined(static_assert) -#define assert_cc(expr) \ - do { \ - static_assert(expr, #expr); \ - } while (false) +#define assert_cc(expr) static_assert(expr, #expr) #else -#define assert_cc(expr) \ - do { \ - switch (0) { \ - case 0: \ - case !!(expr): \ - ; \ - } \ - } while (false) +#define assert_cc(expr) struct UNIQUE(_assert_struct_) { char x[(expr) ? 0 : -1]; }; #endif #define assert_return(expr, r) \ do { \ - if (!(expr)) \ + if (_unlikely_(!(expr))) \ return (r); \ } while (false) @@ -272,19 +267,17 @@ do { \ } \ } while(false) - /* Because statfs.t_type can be int on some architecures, we have to cast + /* Because statfs.t_type can be int on some architectures, we have to cast * the const magic to the type, otherwise the compiler warns about * signed/unsigned comparison, because the magic can be 32 bit unsigned. */ #define F_TYPE_EQUAL(a, b) (a == (typeof(a)) b) - /* Returns the number of chars needed to format variables of the * specified type as a decimal string. Adds in extra space for a * negative '-' prefix. */ - #define DECIMAL_STR_MAX(type) \ - (1+(sizeof(type) <= 1 ? 3 : \ + (2+(sizeof(type) <= 1 ? 3 : \ sizeof(type) <= 2 ? 5 : \ sizeof(type) <= 4 ? 10 : \ sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)]))) @@ -292,4 +285,17 @@ do { \ #define SET_FLAG(v, flag, b) \ (v) = (b) ? ((v) | (flag)) : ((v) & ~(flag)) +#define IN_SET(x, ...) ({ \ + typeof(x) _x = (x); \ + unsigned _i; \ + bool _found = false; \ + for (_i = 0; _i < sizeof((typeof(_x)[]) { __VA_ARGS__ })/sizeof(typeof(_x)); _i++) \ + if (((typeof(_x)[]) { __VA_ARGS__ })[_i] == _x) { \ + _found = true; \ + break; \ + } \ + _found; \ + }) + + #include "log.h"