macros.h revision 66251e6ab31e5cc153fe5cae608e416dacafe9cd
7cb128dc4cae2a03a742f63ba7afee23c78e3af0Phil Carmody#ifndef __MACROS_H
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define __MACROS_H
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen/* several useful macros, mostly from glib.h */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#ifndef NULL
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define NULL ((void *)0)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#endif
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#ifndef FALSE
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define FALSE (0)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#endif
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#ifndef TRUE
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define TRUE (!FALSE)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#endif
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define BITS_IN_UINT (CHAR_BIT * sizeof(unsigned int))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define MEM_ALIGN(size) \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen (((size) + MEM_ALIGN_SIZE-1) & ~((unsigned int) MEM_ALIGN_SIZE-1))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
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
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#undef CLAMP
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen#undef NVL
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define NVL(str, nullstr) ((str) != NULL ? (str) : (nullstr))
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define POINTER_TO_INT(p) ((int) (p))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define POINTER_TO_UINT(p) ((unsigned int) (p))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define INT_TO_POINTER(i) ((void *) (i))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define UINT_TO_POINTER(u) ((void *) (u))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen/* Define VA_COPY() to do the right thing for copying va_list variables. */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#ifndef VA_COPY
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# elif defined (VA_COPY_AS_ARRAY)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define VA_COPY(ap1, ap2) i_memmove ((ap1), (ap2), sizeof (va_list))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# else /* va_list is a pointer */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define VA_COPY(ap1, ap2) ((ap1) = (ap2))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# endif /* va_list is a pointer */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#endif
cc4087c4d7b1b6188dc731e783cee361f1e9c185Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen/* Provide convenience macros for handling structure
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen * fields through their offsets.
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define STRUCT_OFFSET(struct_p, member) \
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
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
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 )
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen# else
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen# if (defined (sun) || defined (__sun__))
73bc59c2a56ff351ae7c4d9f52de76b1b0173995Timo Sirainen# define STMT_START if (1)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define STMT_END else (void)0
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# else
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define STMT_START do
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define STMT_END while (0)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# endif
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# endif
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#endif
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen
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_format_arg__(arg_idx) \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen __attribute__((format_arg (arg_idx)))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define __attr_unused__ __attribute__((unused))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define __attr_noreturn__ __attribute__((noreturn))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define __attr_const__ __attribute__((const))
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#else
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define __attr_format__(format_idx, arg_idx)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define __attr_format_arg__(arg_idx)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define __attr_unused__
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define __attr_noreturn__
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define __attr_const__
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define __attr_unused__
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#endif
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
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#ifdef __GNUC__
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define GNUC_FUNCTION __FUNCTION__
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#else
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define GNUC_FUNCTION ""
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define GNUC_PRETTY_FUNCTION ""
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#endif
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen/* Provide macros for error handling. */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#ifdef DISABLE_CHECKS
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define i_assert(expr)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define return_if_fail(expr)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen# define return_val_if_fail(expr,val)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#elif defined (__GNUC__) && !defined (__STRICT_ANSI__)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define i_assert(expr) STMT_START{ \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!(expr)) \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen i_panic("file %s: line %d (%s): assertion failed: (%s)", \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen __FILE__, \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen __LINE__, \
75113e5fa7532ef628f273caac2feec6008992c6Timo Sirainen __PRETTY_FUNCTION__, \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen #expr); }STMT_END
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define return_if_fail(expr) STMT_START{ \
75113e5fa7532ef628f273caac2feec6008992c6Timo Sirainen if (!(expr)) \
75113e5fa7532ef628f273caac2feec6008992c6Timo Sirainen { \
75113e5fa7532ef628f273caac2feec6008992c6Timo Sirainen i_warning("file %s: line %d (%s): assertion `%s' failed.", \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen __FILE__, \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen __LINE__, \
75113e5fa7532ef628f273caac2feec6008992c6Timo Sirainen __PRETTY_FUNCTION__, \
75113e5fa7532ef628f273caac2feec6008992c6Timo Sirainen #expr); \
75113e5fa7532ef628f273caac2feec6008992c6Timo Sirainen return; \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }; }STMT_END
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define return_val_if_fail(expr,val) STMT_START{ \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!(expr)) \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen { \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen i_warning("file %s: line %d (%s): assertion `%s' failed.", \
75113e5fa7532ef628f273caac2feec6008992c6Timo Sirainen __FILE__, \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen __LINE__, \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen __PRETTY_FUNCTION__, \
75113e5fa7532ef628f273caac2feec6008992c6Timo Sirainen #expr); \
75113e5fa7532ef628f273caac2feec6008992c6Timo Sirainen return val; \
75113e5fa7532ef628f273caac2feec6008992c6Timo Sirainen }; }STMT_END
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#else /* !__GNUC__ */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define i_assert(expr) STMT_START{ \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!(expr)) \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen i_panic("file %s: line %d: assertion failed: (%s)", \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen __FILE__, \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen __LINE__, \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen #expr); }STMT_END
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define return_if_fail(expr) STMT_START{ \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!(expr)) \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen { \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen i_warning("file %s: line %d: assertion `%s' failed.", \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen __FILE__, \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen __LINE__, \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen #expr); \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return; \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }; }STMT_END
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#define return_val_if_fail(expr, val) STMT_START{ \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen if (!(expr)) \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen { \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen i_warning("file %s: line %d: assertion `%s' failed.", \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen __FILE__, \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen __LINE__, \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen #expr); \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen return val; \
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen }; }STMT_END
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#endif
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen#endif
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen