comparison src/lib/macros.h @ 0:3b1985cbc908 HEAD

Initial revision
author Timo Sirainen <tss@iki.fi>
date Fri, 09 Aug 2002 12:15:38 +0300
parents
children af686373b971
comparison
equal deleted inserted replaced
-1:000000000000 0:3b1985cbc908
1 #ifndef __MACROS_H
2 #define __MACROS_H
3
4 /* several useful macros, mostly from glib.h */
5
6 #ifndef NULL
7 # define NULL ((void *)0)
8 #endif
9
10 #ifndef FALSE
11 # define FALSE (0)
12 #endif
13
14 #ifndef TRUE
15 # define TRUE (!FALSE)
16 #endif
17
18 #define BITS_IN_UINT (CHAR_BIT * sizeof(unsigned int))
19
20 #define MEM_ALIGN(size) \
21 (((size) + MEM_ALIGN_SIZE-1) & ~((unsigned int) MEM_ALIGN_SIZE-1))
22
23 /* Don't use simply MIN/MAX, as they're often defined elsewhere in include
24 files that are included after this file generating tons of warnings. */
25 #define I_MIN(a, b) (((a) < (b)) ? (a) : (b))
26 #define I_MAX(a, b) (((a) > (b)) ? (a) : (b))
27
28 #undef CLAMP
29 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
30
31 #undef NVL
32 #define NVL(str, nullstr) ((str) != NULL ? (str) : (nullstr))
33
34 #define POINTER_TO_INT(p) ((int) (p))
35 #define POINTER_TO_UINT(p) ((unsigned int) (p))
36
37 #define INT_TO_POINTER(i) ((void *) (i))
38 #define UINT_TO_POINTER(u) ((void *) (u))
39
40 /* Define VA_COPY() to do the right thing for copying va_list variables. */
41 #ifndef VA_COPY
42 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
43 # define VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
44 # elif defined (VA_COPY_AS_ARRAY)
45 # define VA_COPY(ap1, ap2) i_memmove ((ap1), (ap2), sizeof (va_list))
46 # else /* va_list is a pointer */
47 # define VA_COPY(ap1, ap2) ((ap1) = (ap2))
48 # endif /* va_list is a pointer */
49 #endif
50
51 /* Provide convenience macros for handling structure
52 * fields through their offsets.
53 */
54 #define STRUCT_OFFSET(struct_p, member) \
55 ((long) ((char *) &((struct_p)->member) - (char *) (struct_p)))
56 #define STRUCT_MEMBER_P(struct_p, struct_offset) \
57 ((void *) ((char *) (struct_p) + (long) (struct_offset)))
58 #define STRUCT_MEMBER(member_type, struct_p, struct_offset) \
59 (*(member_type *) G_STRUCT_MEMBER_P((struct_p), (struct_offset)))
60
61 /* Provide simple macro statement wrappers (adapted from Perl):
62 STMT_START { statements; } STMT_END;
63 can be used as a single statement, as in
64 if (x) STMT_START { ... } STMT_END; else ...
65
66 For gcc we will wrap the statements within `({' and `})' braces.
67 For SunOS they will be wrapped within `if (1)' and `else (void) 0',
68 and otherwise within `do' and `while (0)'. */
69 #if !(defined (STMT_START) && defined (STMT_END))
70 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
71 # define STMT_START (void)(
72 # define STMT_END )
73 # else
74 # if (defined (sun) || defined (__sun__))
75 # define STMT_START if (1)
76 # define STMT_END else (void)0
77 # else
78 # define STMT_START do
79 # define STMT_END while (0)
80 # endif
81 # endif
82 #endif
83
84 /* Provide macros to feature the GCC function attribute. */
85 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
86 # define __attr_format__(format_idx, arg_idx) \
87 __attribute__((format (printf, format_idx, arg_idx)))
88 # define __attr_format_arg__(arg_idx) \
89 __attribute__((format_arg (arg_idx)))
90 # define __attr_unused__ __attribute__((unused))
91 # define __attr_noreturn__ __attribute__((noreturn))
92 # define __attr_const__ __attribute__((const))
93 #else
94 # define __attr_format__(format_idx, arg_idx)
95 # define __attr_format_arg__(arg_idx)
96 # define __attr_unused__
97 # define __attr_noreturn__
98 # define __attr_const__
99 # define __attr_unused__
100 #endif
101
102 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
103 macros, so we can refer to them as strings unconditionally. */
104 #ifdef __GNUC__
105 # define GNUC_FUNCTION __FUNCTION__
106 # define GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
107 #else
108 # define GNUC_FUNCTION ""
109 # define GNUC_PRETTY_FUNCTION ""
110 #endif
111
112 /* Provide macros for error handling. */
113 #ifdef DISABLE_CHECKS
114 # define i_assert(expr)
115 # define return_if_fail(expr)
116 # define return_val_if_fail(expr,val)
117 #elif defined (__GNUC__) && !defined (__STRICT_ANSI__)
118
119 #define i_assert(expr) STMT_START{ \
120 if (!(expr)) \
121 i_panic("file %s: line %d (%s): assertion failed: (%s)", \
122 __FILE__, \
123 __LINE__, \
124 __PRETTY_FUNCTION__, \
125 #expr); }STMT_END
126
127 #define return_if_fail(expr) STMT_START{ \
128 if (!(expr)) \
129 { \
130 i_warning("file %s: line %d (%s): assertion `%s' failed.", \
131 __FILE__, \
132 __LINE__, \
133 __PRETTY_FUNCTION__, \
134 #expr); \
135 return; \
136 }; }STMT_END
137
138 #define return_val_if_fail(expr,val) STMT_START{ \
139 if (!(expr)) \
140 { \
141 i_warning("file %s: line %d (%s): assertion `%s' failed.", \
142 __FILE__, \
143 __LINE__, \
144 __PRETTY_FUNCTION__, \
145 #expr); \
146 return val; \
147 }; }STMT_END
148
149 #else /* !__GNUC__ */
150
151 #define i_assert(expr) STMT_START{ \
152 if (!(expr)) \
153 i_panic("file %s: line %d: assertion failed: (%s)", \
154 __FILE__, \
155 __LINE__, \
156 #expr); }STMT_END
157
158 #define return_if_fail(expr) STMT_START{ \
159 if (!(expr)) \
160 { \
161 i_warning("file %s: line %d: assertion `%s' failed.", \
162 __FILE__, \
163 __LINE__, \
164 #expr); \
165 return; \
166 }; }STMT_END
167
168 #define return_val_if_fail(expr, val) STMT_START{ \
169 if (!(expr)) \
170 { \
171 i_warning("file %s: line %d: assertion `%s' failed.", \
172 __FILE__, \
173 __LINE__, \
174 #expr); \
175 return val; \
176 }; }STMT_END
177
178 #endif
179
180 #endif