1 // Copyright (C) 2007-2013 Codership Oy <info@codership.com>
2 
3 /**
4  * @file Miscellaneous macros
5  *
6  * $Id$
7  */
8 
9 #ifndef _gu_macros_h_
10 #define _gu_macros_h_
11 
12 /*
13  * Platform-dependent macros
14  */
15 
16 #if defined(_MSC_VER)
17 #  define GU_NORETURN      __declspec(noreturn)
18 #  define GU_INLINE        __forceinline
19 #  define GU_FORCE_INLINE  __forceinline
20 #  define GU_UNUSED
21 #  define GU_LONG(x)       (x)
22 #  define GU_ULONG(x)      (x)
23 #  define GU_LONG_LONG(x)  (x)
24 #  define GU_ULONG_LONG(x) (x)
25 #  define GU_DEBUG_NORETURN
26 #else   /* !defined(_MSC_VER) */
27 #  define GU_NORETURN      __attribute__((noreturn))
28 #  define GU_INLINE        inline
29 #  define GU_FORCE_INLINE  inline __attribute__((always_inline))
30 #  define GU_UNUSED        __attribute__((unused))
31 #  define GU_LONG(x)       (x##L)
32 #  define GU_ULONG(x)      (x##LU)
33 #  define GU_LONG_LONG(x)  (x##LL)
34 #  define GU_ULONG_LONG(x) (x##LLU)
35 #  ifndef __OPTIMIZE__
36 #    define GU_DEBUG_NORETURN abort();
37 #  else
38 #    define GU_DEBUG_NORETURN
39 #  endif
40 #endif /* !defined(_MSC_VER) */
41 
42 /*
43  * End of paltform-dependent macros
44  */
45 
46 /* "Shamelessly stolen" (tm) goods from Linux kernel */
47 /*
48  * min()/max() macros that also do
49  * strict type-checking.. See the
50  * "unnecessary" pointer comparison.
51  */
52 #if 0 // typeof() is not in C99
53 #define GU_MAX(x,y) ({       \
54         typeof(x) _x = (x);  \
55         typeof(y) _y = (y);  \
56         (void) (&_x == &_y); \
57         _x > _y ? _x : _y; })
58 
59 #define GU_MIN(x,y) ({       \
60         typeof(x) _x = (x);  \
61         typeof(y) _y = (y);  \
62         (void) (&_x == &_y); \
63         _x < _y ? _x : _y; })
64 #endif
65 
66 #define gu_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
67 
68 #if __GNUC__ >= 3
69 #  define gu_likely(x)   __builtin_expect((x), 1)
70 #  define gu_unlikely(x) __builtin_expect((x), 0)
71 #else
72 #  define gu_likely(x)   (x)
73 #  define gu_unlikely(x) (x)
74 #endif
75 
76 /* returns minimum multiple of A that is >= S */
77 #define GU_ALIGN(S,A) ((((S) - 1)/(A) + 1)*(A))
78 
79 #endif /* _gu_macros_h_ */
80