macros.h revision 8939d20e53167fe5f308d41154085d1e0bbcf82a
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen/* several useful macros, mostly from glib.h */
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen# define NULL ((void *)0)
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen#define BITS_IN_UINT (CHAR_BIT * sizeof(unsigned int))
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen#define BITS_IN_SIZE_T (CHAR_BIT * sizeof(size_t))
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen (((size) + MEM_ALIGN_SIZE-1) & ~((unsigned int) MEM_ALIGN_SIZE-1))
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen ((void *) (((unsigned char *) (ptr)) + (offset)))
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen ((const void *) (((const unsigned char *) (ptr)) + (offset)))
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen/* Don't use simply MIN/MAX, as they're often defined elsewhere in include
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen files that are included after this file generating tons of warnings. */
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen#define I_MIN(a, b) (((a) < (b)) ? (a) : (b))
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen#define I_MAX(a, b) (((a) > (b)) ? (a) : (b))
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen#define NVL(str, nullstr) ((str) != NULL ? (str) : (nullstr))
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen/* make it easier to cast from/to pointers. assumes that
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen sizeof(size_t) == sizeof(void *) and they're both the largest datatypes
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen that are allowed to be used. so, long long isn't safe with these. */
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen ((void *) ((char *) NULL + (i)))
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen ((type) ((const char *) (p) - (const char *) NULL))
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen/* Define VA_COPY() to do the right thing for copying va_list variables.
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen config.h may have already defined VA_COPY as va_copy or __va_copy. */
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen# if defined (__GNUC__) && defined (__PPC__) && \
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen# define VA_COPY(ap1, ap2) memmove ((ap1), (ap2), sizeof (va_list))
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen# else /* va_list is a pointer */
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen# endif /* va_list is a pointer */
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen/* Provide convenience macros for handling structure
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen * fields through their offsets.
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen#define STRUCT_MEMBER_P(struct_p, struct_offset) \
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen ((void *) ((char *) (struct_p) + (long) (struct_offset)))
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen#define STRUCT_MEMBER(member_type, struct_p, struct_offset) \
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen (*(member_type *) G_STRUCT_MEMBER_P((struct_p), (struct_offset)))
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen/* Provide simple macro statement wrappers (adapted from Perl):
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen STMT_START { statements; } STMT_END;
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen can be used as a single statement, as in
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen if (x) STMT_START { ... } STMT_END; else ...
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen For gcc we will wrap the statements within `({' and `})' braces.
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen For SunOS they will be wrapped within `if (1)' and `else (void) 0',
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen and otherwise within `do' and `while (0)'. */
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen#if !(defined (STMT_START) && defined (STMT_END))
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen# if defined (__GNUC__) && !defined (__cplusplus) && \
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen !defined (__STRICT_ANSI__) && !defined (PEDANTIC)
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen# define STMT_START (void)(
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen# define STMT_END else (void)0
c8555d0f669c5732c8b9f5bece7b41fc65545f33Timo Sirainen# define STMT_END while (0)
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen/* Provide macros to feature the GCC function attribute. */
596f582a422eddef951ee445f9a0ed3d4bf14ff8Timo Sirainen#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
# define ATTR_SCANF
# define ATTR_UNUSED
# define ATTR_NORETURN
# define ATTR_CONST
# define ATTR_UNUSED
# define ATTR_MALLOC
# define ATTR_WARN_UNUSED_RESULT
# define ATTR_SENTINEL
#ifdef __GNUC__
# define CONTEXT_TYPE_SAFETY
#ifdef CONTEXT_TYPE_SAFETY
# define HAVE_TYPEOF
#ifdef __GNUC__
#ifdef DISABLE_ASSERTS
__FILE__, \
__LINE__, \
__FILE__, \
__LINE__, \
#define i_unreached() \