macros.h revision ac7aa955db4c77bbb169baa5d104a4c128674646
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen/* several useful macros, mostly from glib.h */
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen# define NULL ((void *)0)
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen (((size) + MEM_ALIGN_SIZE-1) & ~((size_t) MEM_ALIGN_SIZE-1))
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen ((const void *) (((uintptr_t) (ptr)) + (offset)))
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen (type *)((uintptr_t)(ptr) - (uintptr_t)offsetof(type, name) + \
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE(ptr, &((type *) 0)->name))
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen/* Don't use simply MIN/MAX, as they're often defined elsewhere in include
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen files that are included after this file generating tons of warnings. */
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen#define I_MIN(a, b) (((a) < (b)) ? (a) : (b))
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen#define I_MAX(a, b) (((a) > (b)) ? (a) : (b))
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen/* make it easier to cast from/to pointers. assumes that
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen sizeof(size_t) == sizeof(void *) and they're both the largest datatypes
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen that are allowed to be used. so, long long isn't safe with these. */
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen ((type) ((const char *) (p) - (const char *) NULL))
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen/* Define VA_COPY() to do the right thing for copying va_list variables.
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen config.h may have already defined VA_COPY as va_copy or __va_copy. */
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen# if defined (__GNUC__) && defined (__PPC__) && \
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen# define VA_COPY(ap1, ap2) memmove ((ap1), (ap2), sizeof (va_list))
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen# else /* va_list is a pointer */
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen# endif /* va_list is a pointer */
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen/* Provide convenience macros for handling structure
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen * fields through their offsets.
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen#define STRUCT_MEMBER_P(struct_p, struct_offset) \
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen ((void *) ((char *) (struct_p) + (long) (struct_offset)))
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen#define CONST_STRUCT_MEMBER_P(struct_p, struct_offset) \
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen ((const void *) ((const char *) (struct_p) + (long) (struct_offset)))
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen/* Provide simple macro statement wrappers (adapted from Perl):
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen STMT_START { statements; } STMT_END;
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen can be used as a single statement, as in
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen if (x) STMT_START { ... } STMT_END; else ...
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen For gcc we will wrap the statements within `({' and `})' braces.
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen For SunOS they will be wrapped within `if (1)' and `else (void) 0',
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen and otherwise within `do' and `while (0)'. */
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen#if !(defined (STMT_START) && defined (STMT_END))
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen# if defined (__GNUC__) && !defined (__cplusplus) && \
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen !defined (__STRICT_ANSI__) && !defined (PEDANTIC)
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen# define STMT_START (void)(
# define STMT_END else (void)0
# define STMT_START do
# define STMT_END while (0)
# define ATTRS_DEFINED
# define ATTR_UNUSED
# define ATTR_NORETURN
# define ATTR_CONST
# define ATTR_PURE
#ifdef HAVE_ATTR_NULL
# define ATTR_NULL(...)
# define ATTR_NOWARN_UNUSED_RESULT
# define ATTR_MALLOC
# define ATTR_WARN_UNUSED_RESULT
# define ATTR_SENTINEL
# define ATTR_HOT
# define ATTR_COLD
# define ATTR_RETURNS_NONNULL
#ifdef HAVE_TYPE_CHECKS
#ifdef HAVE_TYPE_CHECKS
# define ATTR_UNSIGNED_WRAPS
#ifdef DISABLE_ASSERTS
__FILE__, \
__LINE__, \
__func__, \
#ifndef STATIC_CHECKER
# define i_unreached() \
#ifdef __cplusplus
# define STATIC_ARRAY