cleanup
[platform/upstream/glib.git] / glib / gmacros.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GLib Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GLib at ftp://ftp.gtk.org/pub/gtk/.
23  */
24
25 /* This file must not include any other glib header file and must thus
26  * not refer to variables from glibconfig.h
27  */
28
29 #ifndef __G_MACROS_H__
30 #define __G_MACROS_H__
31
32 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
33 #error "Only <glib.h> can be included directly."
34 #endif
35
36 /* We include stddef.h to get the system's definition of NULL
37  */
38 #include <stddef.h>
39
40 /* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
41  * where this is valid. This allows for warningless compilation of
42  * "long long" types even in the presence of '-ansi -pedantic'. 
43  */
44 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
45 #define G_GNUC_EXTENSION __extension__
46 #else
47 #define G_GNUC_EXTENSION
48 #endif
49
50 /* Provide macros to feature the GCC function attribute.
51  */
52 #if    __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
53 #define G_GNUC_PURE __attribute__((__pure__))
54 #define G_GNUC_MALLOC __attribute__((__malloc__))
55 #else
56 #define G_GNUC_PURE
57 #define G_GNUC_MALLOC
58 #endif
59
60 #if     __GNUC__ >= 4
61 #define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
62 #else
63 #define G_GNUC_NULL_TERMINATED
64 #endif
65
66 #if     (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
67 #define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x)))
68 #define G_GNUC_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y)))
69 #else
70 #define G_GNUC_ALLOC_SIZE(x)
71 #define G_GNUC_ALLOC_SIZE2(x,y)
72 #endif
73
74 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
75 #define G_GNUC_PRINTF( format_idx, arg_idx )    \
76   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
77 #define G_GNUC_SCANF( format_idx, arg_idx )     \
78   __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
79 #define G_GNUC_FORMAT( arg_idx )                \
80   __attribute__((__format_arg__ (arg_idx)))
81 #define G_GNUC_NORETURN                         \
82   __attribute__((__noreturn__))
83 #define G_GNUC_CONST                            \
84   __attribute__((__const__))
85 #define G_GNUC_UNUSED                           \
86   __attribute__((__unused__))
87 #define G_GNUC_NO_INSTRUMENT                    \
88   __attribute__((__no_instrument_function__))
89 #else   /* !__GNUC__ */
90 #define G_GNUC_PRINTF( format_idx, arg_idx )
91 #define G_GNUC_SCANF( format_idx, arg_idx )
92 #define G_GNUC_FORMAT( arg_idx )
93 #define G_GNUC_NORETURN
94 #define G_GNUC_CONST
95 #define G_GNUC_UNUSED
96 #define G_GNUC_NO_INSTRUMENT
97 #endif  /* !__GNUC__ */
98
99 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
100 #define G_GNUC_DEPRECATED __attribute__((__deprecated__))
101 #else
102 #define G_GNUC_DEPRECATED
103 #endif /* __GNUC__ */
104
105 #if    __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
106 #define G_GNUC_DEPRECATED_FOR(f)                        \
107   __attribute__((deprecated("Use " #f " instead")))
108 #else
109 #define G_GNUC_DEPRECATED_FOR(f)        G_GNUC_DEPRECATED
110 #endif /* __GNUC__ */
111
112 #if    __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
113 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS                \
114   _Pragma ("GCC diagnostic push")                       \
115   _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
116 #define G_GNUC_END_IGNORE_DEPRECATIONS                  \
117   _Pragma ("GCC diagnostic pop")
118 #elif defined (_MSC_VER) && (_MSC_VER >= 1500)
119 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS                \
120   __pragma (warning (push))  \
121   __pragma (warning (disable : 4996))
122 #define G_GNUC_END_IGNORE_DEPRECATIONS                  \
123   __pragma (warning (pop))
124 #elif defined (__clang__)
125 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
126   _Pragma("clang diagnostic push") \
127   _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
128 #define G_GNUC_END_IGNORE_DEPRECATIONS \
129   _Pragma("clang diagnostic pop")
130 #else
131 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS
132 #define G_GNUC_END_IGNORE_DEPRECATIONS
133 #endif
134
135 #if     __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
136 #define G_GNUC_MAY_ALIAS __attribute__((may_alias))
137 #else
138 #define G_GNUC_MAY_ALIAS
139 #endif
140
141 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
142 #define G_GNUC_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
143 #else
144 #define G_GNUC_WARN_UNUSED_RESULT
145 #endif /* __GNUC__ */
146
147 #ifndef G_DISABLE_DEPRECATED
148 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
149  * macros, so we can refer to them as strings unconditionally.
150  * usage not-recommended since gcc-3.0
151  */
152 #if defined (__GNUC__) && (__GNUC__ < 3)
153 #define G_GNUC_FUNCTION         __FUNCTION__
154 #define G_GNUC_PRETTY_FUNCTION  __PRETTY_FUNCTION__
155 #else   /* !__GNUC__ */
156 #define G_GNUC_FUNCTION         ""
157 #define G_GNUC_PRETTY_FUNCTION  ""
158 #endif  /* !__GNUC__ */
159 #endif  /* !G_DISABLE_DEPRECATED */
160
161 /* Clang feature detection: http://clang.llvm.org/docs/LanguageExtensions.html */
162 #ifndef __has_feature
163 #define __has_feature(x) 0
164 #endif
165
166 #if __has_feature(attribute_analyzer_noreturn)
167 #define G_ANALYZER_ANALYZING 1
168 #define G_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
169 #else
170 #define G_ANALYZER_ANALYZING 0
171 #define G_ANALYZER_NORETURN
172 #endif
173
174 #define G_STRINGIFY(macro_or_string)    G_STRINGIFY_ARG (macro_or_string)
175 #define G_STRINGIFY_ARG(contents)       #contents
176
177 #ifndef __GI_SCANNER__ /* The static assert macro really confuses the introspection parser */
178 #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
179 #define G_PASTE(identifier1,identifier2)      G_PASTE_ARGS (identifier1, identifier2)
180 #ifdef __COUNTER__
181 #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __COUNTER__)[(expr) ? 1 : -1] G_GNUC_UNUSED
182 #else
183 #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __LINE__)[(expr) ? 1 : -1] G_GNUC_UNUSED
184 #endif
185 #define G_STATIC_ASSERT_EXPR(expr) ((void) sizeof (char[(expr) ? 1 : -1]))
186 #endif
187
188 /* Provide a string identifying the current code position */
189 #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)
190 #define G_STRLOC        __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
191 #else
192 #define G_STRLOC        __FILE__ ":" G_STRINGIFY (__LINE__)
193 #endif
194
195 /* Provide a string identifying the current function, non-concatenatable */
196 #if defined (__GNUC__) && defined (__cplusplus)
197 #define G_STRFUNC     ((const char*) (__PRETTY_FUNCTION__))
198 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
199 #define G_STRFUNC     ((const char*) (__func__))
200 #elif defined (__GNUC__) || (defined(_MSC_VER) && (_MSC_VER > 1300))
201 #define G_STRFUNC     ((const char*) (__FUNCTION__))
202 #else
203 #define G_STRFUNC     ((const char*) ("???"))
204 #endif
205
206 /* Guard C code in headers, while including them from C++ */
207 #ifdef  __cplusplus
208 #define G_BEGIN_DECLS  extern "C" {
209 #define G_END_DECLS    }
210 #else
211 #define G_BEGIN_DECLS
212 #define G_END_DECLS
213 #endif
214
215 /* Provide definitions for some commonly used macros.
216  *  Some of them are only provided if they haven't already
217  *  been defined. It is assumed that if they are already
218  *  defined then the current definition is correct.
219  */
220 #ifndef NULL
221 #  ifdef __cplusplus
222 #  define NULL        (0L)
223 #  else /* !__cplusplus */
224 #  define NULL        ((void*) 0)
225 #  endif /* !__cplusplus */
226 #endif
227
228 #ifndef FALSE
229 #define FALSE   (0)
230 #endif
231
232 #ifndef TRUE
233 #define TRUE    (!FALSE)
234 #endif
235
236 #undef  MAX
237 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
238
239 #undef  MIN
240 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
241
242 #undef  ABS
243 #define ABS(a)     (((a) < 0) ? -(a) : (a))
244
245 #undef  CLAMP
246 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
247
248 /* Count the number of elements in an array. The array must be defined
249  * as such; using this with a dynamically allocated array will give
250  * incorrect results.
251  */
252 #define G_N_ELEMENTS(arr)               (sizeof (arr) / sizeof ((arr)[0]))
253
254 /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT
255  */
256 #define GPOINTER_TO_SIZE(p)     ((gsize) (p))
257 #define GSIZE_TO_POINTER(s)     ((gpointer) (gsize) (s))
258
259 /* Provide convenience macros for handling structure
260  * fields through their offsets.
261  */
262
263 #if defined(__GNUC__)  && __GNUC__ >= 4
264 #define G_STRUCT_OFFSET(struct_type, member) \
265       ((glong) offsetof (struct_type, member))
266 #else
267 #define G_STRUCT_OFFSET(struct_type, member)    \
268       ((glong) ((guint8*) &((struct_type*) 0)->member))
269 #endif
270
271 #define G_STRUCT_MEMBER_P(struct_p, struct_offset)   \
272     ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
273 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset)   \
274     (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
275
276 /* Provide simple macro statement wrappers:
277  *   G_STMT_START { statements; } G_STMT_END;
278  * This can be used as a single statement, like:
279  *   if (x) G_STMT_START { ... } G_STMT_END; else ...
280  * This intentionally does not use compiler extensions like GCC's '({...})' to
281  * avoid portability issue or side effects when compiled with different compilers.
282  */
283 #if !(defined (G_STMT_START) && defined (G_STMT_END))
284 #define G_STMT_START  do
285 #define G_STMT_END    while (0)
286 #endif
287
288 /* Deprecated -- do not use. */
289 #ifndef G_DISABLE_DEPRECATED
290 #ifdef G_DISABLE_CONST_RETURNS
291 #define G_CONST_RETURN
292 #else
293 #define G_CONST_RETURN const
294 #endif
295 #endif
296
297 /*
298  * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to 
299  * the compiler about the expected result of an expression. Some compilers
300  * can use this information for optimizations.
301  *
302  * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
303  * putting assignments in g_return_if_fail ().  
304  */
305 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
306 #define _G_BOOLEAN_EXPR(expr)                   \
307  G_GNUC_EXTENSION ({                            \
308    int _g_boolean_var_;                         \
309    if (expr)                                    \
310       _g_boolean_var_ = 1;                      \
311    else                                         \
312       _g_boolean_var_ = 0;                      \
313    _g_boolean_var_;                             \
314 })
315 #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))
316 #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))
317 #else
318 #define G_LIKELY(expr) (expr)
319 #define G_UNLIKELY(expr) (expr)
320 #endif
321
322 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
323 #define G_DEPRECATED __attribute__((__deprecated__))
324 #elif defined(_MSC_VER) && (_MSC_VER >= 1300)
325 #define G_DEPRECATED __declspec(deprecated)
326 #else
327 #define G_DEPRECATED
328 #endif
329
330 #if    __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
331 #define G_DEPRECATED_FOR(f) __attribute__((__deprecated__("Use '" #f "' instead")))
332 #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320)
333 #define G_DEPRECATED_FOR(f) __declspec(deprecated("is deprecated. Use '" #f "' instead"))
334 #else
335 #define G_DEPRECATED_FOR(f) G_DEPRECATED
336 #endif
337
338 #if    __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
339 #define G_UNAVAILABLE(maj,min) __attribute__((deprecated("Not available before " #maj "." #min)))
340 #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320)
341 #define G_UNAVAILABLE(maj,min) __declspec(deprecated("is not available before " #maj "." #min))
342 #else
343 #define G_UNAVAILABLE(maj,min) G_DEPRECATED
344 #endif
345
346 #ifndef _GLIB_EXTERN
347 #define _GLIB_EXTERN extern
348 #endif
349
350 /* These macros are used to mark deprecated functions in GLib headers,
351  * and thus have to be exposed in installed headers. But please
352  * do *not* use them in other projects. Instead, use G_DEPRECATED
353  * or define your own wrappers around it.
354  */
355
356 #ifdef GLIB_DISABLE_DEPRECATION_WARNINGS
357 #define GLIB_DEPRECATED _GLIB_EXTERN
358 #define GLIB_DEPRECATED_FOR(f) _GLIB_EXTERN
359 #define GLIB_UNAVAILABLE(maj,min) _GLIB_EXTERN
360 #else
361 #define GLIB_DEPRECATED G_DEPRECATED _GLIB_EXTERN
362 #define GLIB_DEPRECATED_FOR(f) G_DEPRECATED_FOR(f) _GLIB_EXTERN
363 #define GLIB_UNAVAILABLE(maj,min) G_UNAVAILABLE(maj,min) _GLIB_EXTERN
364 #endif
365
366 #endif /* __G_MACROS_H__ */