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