macros.h revision c3c07d6527ad28c8546cbbf84c257d178e23c184
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen/* several useful macros, mostly from glib.h */
1dd875d96ab5640f78250079961c10e99ed4aa79Timo Sirainen# define NULL ((void *)0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (((size) + MEM_ALIGN_SIZE-1) & ~((unsigned int) MEM_ALIGN_SIZE-1))
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen ((void *) (((unsigned char *) (ptr)) + (offset)))
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen ((const void *) (((const unsigned char *) (ptr)) + (offset)))
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Don't use simply MIN/MAX, as they're often defined elsewhere in include
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen files that are included after this file generating tons of warnings. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define I_MIN(a, b) (((a) < (b)) ? (a) : (b))
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define I_MAX(a, b) (((a) > (b)) ? (a) : (b))
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* make it easier to cast from/to pointers. assumes that
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen sizeof(size_t) == sizeof(void *) and they're both the largest datatypes
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen that are allowed to be used. so, long long isn't safe with these. */
8fa41238067c854435884c459963fde6f8c6436bTimo Sirainen ((void *) ((char *) NULL + (i)))
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen ((type) ((const char *) (p) - (const char *) NULL))
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainen/* Define VA_COPY() to do the right thing for copying va_list variables.
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen config.h may have already defined VA_COPY as va_copy or __va_copy. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen# if defined (__GNUC__) && defined (__PPC__) && \
5aeb15e5817fbd4b1d8de540aa7673e3819a8030Timo Sirainen# define VA_COPY(ap1, ap2) memmove ((ap1), (ap2), sizeof (va_list))
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainen# else /* va_list is a pointer */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen# endif /* va_list is a pointer */
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen/* Provide convenience macros for handling structure
a2f250a332dfc1e6cd4ffd196c621eb9dbf7b8a1Timo Sirainen * fields through their offsets.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define STRUCT_MEMBER_P(struct_p, struct_offset) \
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ((void *) ((char *) (struct_p) + (long) (struct_offset)))
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen#define CONST_STRUCT_MEMBER_P(struct_p, struct_offset) \
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ((const void *) ((const char *) (struct_p) + (long) (struct_offset)))
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen/* Provide simple macro statement wrappers (adapted from Perl):
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen STMT_START { statements; } STMT_END;
ee246b46953e4b94b2f22e093373674fa9155500Timo Sirainen can be used as a single statement, as in
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (x) STMT_START { ... } STMT_END; else ...
3ccab0bac68040f179a7de45c516cec258e28fdbTimo Sirainen For gcc we will wrap the statements within `({' and `})' braces.
648d24583c1574441c4fa0331a90bd4d6e7996c5Timo Sirainen For SunOS they will be wrapped within `if (1)' and `else (void) 0',
648d24583c1574441c4fa0331a90bd4d6e7996c5Timo Sirainen and otherwise within `do' and `while (0)'. */
ee246b46953e4b94b2f22e093373674fa9155500Timo Sirainen#if !(defined (STMT_START) && defined (STMT_END))
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen# if defined (__GNUC__) && !defined (__cplusplus) && \
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen !defined (__STRICT_ANSI__) && !defined (PEDANTIC)
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainen# define STMT_START (void)(
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen# define STMT_END else (void)0
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen# define STMT_END while (0)
80fc743146da5130de34174cdaad2576f103723fTimo Sirainen/* Provide macros to feature the GCC function attribute. */
80fc743146da5130de34174cdaad2576f103723fTimo Sirainen#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen __attribute__((format (printf, format_idx, arg_idx)))
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen __attribute__((format (scanf, format_idx, arg_idx)))
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen __attribute__((format (strftime, format_idx, 0)))
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen# define ATTR_NORETURN __attribute__((noreturn))
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen/* GCC 4.0 and later */
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen# define ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen# define ATTR_SENTINEL __attribute__((sentinel))
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen/* GCC 4.3 and later */
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen/* Macros to provide type safety for callback functions' context parameters */
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen# define CONTEXT_CALLBACK(name, callback_type, callback, context, ...) \
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen name(__VA_ARGS__, (callback_type *)callback, context); })
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen# define CONTEXT_CALLBACK2(name, callback_type, callback, arg1_type, context, ...) \
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen ({(void)(1 ? 0 : callback((arg1_type)0, context)); \
c5454841b5067a22827556ca9bc7935d190f57baTimo Sirainen name(__VA_ARGS__, (callback_type *)callback, context); })
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen# define CONTEXT_CALLBACK(name, callback_type, callback, context, ...) \
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen name(__VA_ARGS__, (callback_type *)callback, context)
59151b71059df1190acd75d8717ed04a7920c862Timo Sirainen# define CONTEXT_CALLBACK2(name, callback_type, callback, arg1_type, context, ...) \
da985034a708db2f61394b30d117050ae6829ee5Timo Sirainen name(__VA_ARGS__, (callback_type *)callback, context)
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 0)
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen# define COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE(_a, _b) \
da985034a708db2f61394b30d117050ae6829ee5Timo Sirainen !__builtin_types_compatible_p(typeof(_a), typeof(_b)))
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen# define COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE(_a, _b) 0
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen# define unlikely(expr) __builtin_expect(!!(expr), 0)
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen# define likely(expr) __builtin_expect(!!(expr), 1)
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen/* Provide macros for error handling. */
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen#elif defined (__GNUC__) && !defined (__STRICT_ANSI__)
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen i_panic("file %s: line %d (%s): assertion failed: (%s)", \
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen#else /* !__GNUC__ */
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen i_panic("file %s: line %d: assertion failed: (%s)", \
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen i_panic("file %s: line %d: unreached", __FILE__, __LINE__)