macros.h revision 66251e6ab31e5cc153fe5cae608e416dacafe9cd
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen/* several useful macros, mostly from glib.h */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define NULL ((void *)0)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define BITS_IN_UINT (CHAR_BIT * sizeof(unsigned int))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen (((size) + MEM_ALIGN_SIZE-1) & ~((unsigned int) MEM_ALIGN_SIZE-1))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen/* Don't use simply MIN/MAX, as they're often defined elsewhere in include
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen files that are included after this file generating tons of warnings. */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define I_MIN(a, b) (((a) < (b)) ? (a) : (b))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define I_MAX(a, b) (((a) > (b)) ? (a) : (b))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define NVL(str, nullstr) ((str) != NULL ? (str) : (nullstr))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define POINTER_TO_INT(p) ((int) (p))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define POINTER_TO_UINT(p) ((unsigned int) (p))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define INT_TO_POINTER(i) ((void *) (i))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define UINT_TO_POINTER(u) ((void *) (u))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen/* Define VA_COPY() to do the right thing for copying va_list variables. */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define VA_COPY(ap1, ap2) i_memmove ((ap1), (ap2), sizeof (va_list))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# else /* va_list is a pointer */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# endif /* va_list is a pointer */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen/* Provide convenience macros for handling structure
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * fields through their offsets.
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ((long) ((char *) &((struct_p)->member) - (char *) (struct_p)))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define STRUCT_MEMBER_P(struct_p, struct_offset) \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen ((void *) ((char *) (struct_p) + (long) (struct_offset)))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define STRUCT_MEMBER(member_type, struct_p, struct_offset) \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen (*(member_type *) G_STRUCT_MEMBER_P((struct_p), (struct_offset)))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen/* Provide simple macro statement wrappers (adapted from Perl):
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen STMT_START { statements; } STMT_END;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen can be used as a single statement, as in
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (x) STMT_START { ... } STMT_END; else ...
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen For gcc we will wrap the statements within `({' and `})' braces.
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen For SunOS they will be wrapped within `if (1)' and `else (void) 0',
75113e5fa7532ef628f273caac2feec6008992c6Timo Sirainen and otherwise within `do' and `while (0)'. */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#if !(defined (STMT_START) && defined (STMT_END))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define STMT_START (void)(
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define STMT_END else (void)0
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define STMT_END while (0)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen/* Provide macros to feature the GCC function attribute. */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define __attr_format__(format_idx, arg_idx) \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen __attribute__((format (printf, format_idx, arg_idx)))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define __attr_unused__ __attribute__((unused))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define __attr_noreturn__ __attribute__((noreturn))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen/* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen macros, so we can refer to them as strings unconditionally. */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen/* Provide macros for error handling. */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#elif defined (__GNUC__) && !defined (__STRICT_ANSI__)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen i_panic("file %s: line %d (%s): assertion failed: (%s)", \
75113e5fa7532ef628f273caac2feec6008992c6Timo Sirainen i_warning("file %s: line %d (%s): assertion `%s' failed.", \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define return_val_if_fail(expr,val) STMT_START{ \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen i_warning("file %s: line %d (%s): assertion `%s' failed.", \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#else /* !__GNUC__ */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen i_panic("file %s: line %d: assertion failed: (%s)", \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen i_warning("file %s: line %d: assertion `%s' failed.", \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define return_val_if_fail(expr, val) STMT_START{ \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen i_warning("file %s: line %d: assertion `%s' failed.", \