Merge remote-tracking branch 'gvdb/master'
[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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 /* This file must not include any other glib header file and must thus
28  * not refer to variables from glibconfig.h
29  */
30
31 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
32 #error "Only <glib.h> can be included directly."
33 #endif
34
35 #ifndef __G_MACROS_H__
36 #define __G_MACROS_H__
37
38 /* We include stddef.h to get the system's definition of NULL
39  */
40 #include <stddef.h>
41
42 /* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
43  * where this is valid. This allows for warningless compilation of
44  * "long long" types even in the presence of '-ansi -pedantic'. 
45  */
46 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
47 #  define G_GNUC_EXTENSION __extension__
48 #else
49 #  define G_GNUC_EXTENSION
50 #endif
51
52 /* Provide macros to feature the GCC function attribute.
53  */
54 #if    __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
55 #define G_GNUC_PURE                            \
56   __attribute__((__pure__))
57 #define G_GNUC_MALLOC                           \
58   __attribute__((__malloc__))
59 #else
60 #define G_GNUC_PURE
61 #define G_GNUC_MALLOC
62 #endif
63
64 #if     __GNUC__ >= 4
65 #define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
66 #else
67 #define G_GNUC_NULL_TERMINATED
68 #endif
69
70 #if     (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
71 #define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x)))
72 #define G_GNUC_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y)))
73 #else
74 #define G_GNUC_ALLOC_SIZE(x)
75 #define G_GNUC_ALLOC_SIZE2(x,y)
76 #endif
77
78 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
79 #define G_GNUC_PRINTF( format_idx, arg_idx )    \
80   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
81 #define G_GNUC_SCANF( format_idx, arg_idx )     \
82   __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
83 #define G_GNUC_FORMAT( arg_idx )                \
84   __attribute__((__format_arg__ (arg_idx)))
85 #define G_GNUC_NORETURN                         \
86   __attribute__((__noreturn__))
87 #define G_GNUC_CONST                            \
88   __attribute__((__const__))
89 #define G_GNUC_UNUSED                           \
90   __attribute__((__unused__))
91 #define G_GNUC_NO_INSTRUMENT                    \
92   __attribute__((__no_instrument_function__))
93 #else   /* !__GNUC__ */
94 #define G_GNUC_PRINTF( format_idx, arg_idx )
95 #define G_GNUC_SCANF( format_idx, arg_idx )
96 #define G_GNUC_FORMAT( arg_idx )
97 #define G_GNUC_NORETURN
98 #define G_GNUC_CONST
99 #define G_GNUC_UNUSED
100 #define G_GNUC_NO_INSTRUMENT
101 #endif  /* !__GNUC__ */
102
103 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
104 #define G_GNUC_DEPRECATED                            \
105   __attribute__((__deprecated__))
106 #else
107 #define G_GNUC_DEPRECATED
108 #endif /* __GNUC__ */
109
110 #if    __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
111 #define G_GNUC_DEPRECATED_FOR(f)                        \
112   __attribute__((deprecated("Use " #f " instead")))
113 #else
114 #define G_GNUC_DEPRECATED_FOR(f)        G_GNUC_DEPRECATED
115 #endif /* __GNUC__ */
116
117 #if     __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
118 #  define G_GNUC_MAY_ALIAS __attribute__((may_alias))
119 #else
120 #  define G_GNUC_MAY_ALIAS
121 #endif
122
123 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
124 #define G_GNUC_WARN_UNUSED_RESULT               \
125   __attribute__((warn_unused_result))
126 #else
127 #define G_GNUC_WARN_UNUSED_RESULT
128 #endif /* __GNUC__ */
129
130 #ifndef G_DISABLE_DEPRECATED
131 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
132  * macros, so we can refer to them as strings unconditionally.
133  * usage not-recommended since gcc-3.0
134  */
135 #if defined (__GNUC__) && (__GNUC__ < 3)
136 #define G_GNUC_FUNCTION         __FUNCTION__
137 #define G_GNUC_PRETTY_FUNCTION  __PRETTY_FUNCTION__
138 #else   /* !__GNUC__ */
139 #define G_GNUC_FUNCTION         ""
140 #define G_GNUC_PRETTY_FUNCTION  ""
141 #endif  /* !__GNUC__ */
142 #endif  /* !G_DISABLE_DEPRECATED */
143
144 #define G_STRINGIFY(macro_or_string)    G_STRINGIFY_ARG (macro_or_string)
145 #define G_STRINGIFY_ARG(contents)       #contents
146
147 #ifndef __GI_SCANNER__ /* The static assert macro really confuses the introspection parser */
148 #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
149 #define G_PASTE(identifier1,identifier2)      G_PASTE_ARGS (identifier1, identifier2)
150 #ifdef __COUNTER__
151 #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __COUNTER__)[(expr) ? 1 : -1]
152 #else
153 #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __LINE__)[(expr) ? 1 : -1]
154 #endif
155 #define G_STATIC_ASSERT_EXPR(expr) ((void) sizeof (char[(expr) ? 1 : -1]))
156 #endif
157
158 /* Provide a string identifying the current code position */
159 #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)
160 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
161 #else
162 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__)
163 #endif
164
165 /* Provide a string identifying the current function, non-concatenatable */
166 #if defined (__GNUC__)
167 #  define G_STRFUNC     ((const char*) (__PRETTY_FUNCTION__))
168 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 19901L
169 #  define G_STRFUNC     ((const char*) (__func__))
170 #else
171 #  define G_STRFUNC     ((const char*) ("???"))
172 #endif
173
174 /* Guard C code in headers, while including them from C++ */
175 #ifdef  __cplusplus
176 # define G_BEGIN_DECLS  extern "C" {
177 # define G_END_DECLS    }
178 #else
179 # define G_BEGIN_DECLS
180 # define G_END_DECLS
181 #endif
182
183 /* Provide definitions for some commonly used macros.
184  *  Some of them are only provided if they haven't already
185  *  been defined. It is assumed that if they are already
186  *  defined then the current definition is correct.
187  */
188 #ifndef NULL
189 #  ifdef __cplusplus
190 #    define NULL        (0L)
191 #  else /* !__cplusplus */
192 #    define NULL        ((void*) 0)
193 #  endif /* !__cplusplus */
194 #endif
195
196 #ifndef FALSE
197 #define FALSE   (0)
198 #endif
199
200 #ifndef TRUE
201 #define TRUE    (!FALSE)
202 #endif
203
204 #undef  MAX
205 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
206
207 #undef  MIN
208 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
209
210 #undef  ABS
211 #define ABS(a)     (((a) < 0) ? -(a) : (a))
212
213 #undef  CLAMP
214 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
215
216 /* Count the number of elements in an array. The array must be defined
217  * as such; using this with a dynamically allocated array will give
218  * incorrect results.
219  */
220 #define G_N_ELEMENTS(arr)               (sizeof (arr) / sizeof ((arr)[0]))
221
222 /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT
223  */
224 #define GPOINTER_TO_SIZE(p)     ((gsize) (p))
225 #define GSIZE_TO_POINTER(s)     ((gpointer) (gsize) (s))
226
227 /* Provide convenience macros for handling structure
228  * fields through their offsets.
229  */
230
231 #if defined(__GNUC__)  && __GNUC__ >= 4
232 #  define G_STRUCT_OFFSET(struct_type, member) \
233       ((glong) offsetof (struct_type, member))
234 #else
235 #  define G_STRUCT_OFFSET(struct_type, member)  \
236       ((glong) ((guint8*) &((struct_type*) 0)->member))
237 #endif
238
239 #define G_STRUCT_MEMBER_P(struct_p, struct_offset)   \
240     ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
241 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset)   \
242     (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
243
244 /* Provide simple macro statement wrappers:
245  *   G_STMT_START { statements; } G_STMT_END;
246  * This can be used as a single statement, like:
247  *   if (x) G_STMT_START { ... } G_STMT_END; else ...
248  * This intentionally does not use compiler extensions like GCC's '({...})' to
249  * avoid portability issue or side effects when compiled with different compilers.
250  */
251 #if !(defined (G_STMT_START) && defined (G_STMT_END))
252 #  define G_STMT_START  do
253 #  define G_STMT_END    while (0)
254 #endif
255
256 /* Deprecated -- do not use. */
257 #ifndef G_DISABLE_DEPRECATED
258 #ifdef G_DISABLE_CONST_RETURNS
259 #define G_CONST_RETURN
260 #else
261 #define G_CONST_RETURN const
262 #endif
263 #endif
264
265 /*
266  * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to 
267  * the compiler about the expected result of an expression. Some compilers
268  * can use this information for optimizations.
269  *
270  * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
271  * putting assignments in g_return_if_fail ().  
272  */
273 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
274 #define _G_BOOLEAN_EXPR(expr)                   \
275  G_GNUC_EXTENSION ({                            \
276    int _g_boolean_var_;                         \
277    if (expr)                                    \
278       _g_boolean_var_ = 1;                      \
279    else                                         \
280       _g_boolean_var_ = 0;                      \
281    _g_boolean_var_;                             \
282 })
283 #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))
284 #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))
285 #else
286 #define G_LIKELY(expr) (expr)
287 #define G_UNLIKELY(expr) (expr)
288 #endif
289
290 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
291 #define G_DEPRECATED __attribute__((__deprecated__))
292 #elif defined(_MSC_VER) && (_MSC_VER >= 1300)
293 #define G_DEPRECATED __declspec(deprecated)
294 #else
295 #define G_DEPRECATED
296 #endif
297
298 #if    __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
299 #define G_DEPRECATED_FOR(f) __attribute__((deprecated("Use '" #f "' instead")))
300 #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320)
301 #define G_DEPRECATED_FOR(f) __declspec(deprecated("is deprecated. Use '" #f "' instead"))
302 #else
303 #define G_DEPRECATED_FOR(f) G_DEPRECATED
304 #endif
305
306
307 /* These macros are used to mark deprecated functions in GLib headers,
308  * and thus have to be exposed in installed headers. But please
309  * do *not* use them in other projects. Instead, use G_DEPRECATED
310  * or define your own wrappers around it.
311  */
312
313 #ifdef GLIB_DISABLE_DEPRECATION_WARNINGS
314 #define GLIB_DEPRECATED
315 #define GLIB_DEPRECATED_FOR(f)
316 #else
317 #define GLIB_DEPRECATED G_DEPRECATED
318 #define GLIB_DEPRECATED_FOR(f) G_DEPRECATED_FOR(f)
319 #endif
320
321 /*
322   If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
323   destructors, in a sane way, including e.g. on library unload. If not you're on
324   your own.
325
326   Some compilers need #pragma to handle this, which does not work with macros,
327   so the way you need to use this is (for constructors):
328
329   #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
330   #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
331   #endif
332   G_DEFINE_CONSTRUCTOR(my_constructor)
333   static void my_constructor(void) {
334    ...
335   }
336
337 */
338
339 #if  __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
340
341 #define G_HAS_CONSTRUCTORS 1
342
343 #define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
344 #define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
345
346 #elif _MSC_VER >= 1500
347 /* Visual studio 2008 and later has _Pragma */
348
349 #define G_HAS_CONSTRUCTORS 1
350
351 #define G_DEFINE_CONSTRUCTOR(_func) \
352   static void _func(void); \
353   static int _func ## _wrapper(void) { _func(); return 0; } \
354   __pragma(section(".CRT$XCU",read)) \
355   __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _wrapper;
356
357 #define G_DEFINE_DESTRUCTOR(_func) \
358   static void _func(void); \
359   static int _func ## _constructor(void) { atexit (_func); return 0; } \
360   __pragma(section(".CRT$XCU",read)) \
361   __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
362
363 #elif defined (_MSC_VER)
364
365 #define G_HAS_CONSTRUCTORS 1
366
367 /* Pre Visual studio 2008 must use #pragma section */
368 #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
369 #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
370
371 #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
372   section(".CRT$XCU",read)
373 #define G_DEFINE_CONSTRUCTOR(_func) \
374   static void _func(void); \
375   static int _func ## _wrapper(void) { _func(); return 0; } \
376   __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
377
378 #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
379   section(".CRT$XCU",read)
380 #define G_DEFINE_DESTRUCTOR(_func) \
381   static void _func(void); \
382   static int _func ## _constructor(void) { atexit (_func); return 0; } \
383   __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
384
385 #elif defined(__SUNPRO_C)
386
387 /* This is not tested, but i believe it should work, based on:
388  * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
389  */
390
391 #define G_HAS_CONSTRUCTORS 1
392
393 #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
394 #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
395
396 #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
397   init(_func)
398 #define G_DEFINE_CONSTRUCTOR(_func) \
399   static void _func(void);
400
401 #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
402   fini(_func)
403 #define G_DEFINE_DESTRUCTOR(_func) \
404   static void _func(void);
405
406 #else
407
408 /* constructors not supported for this compiler */
409
410 #endif
411
412
413
414 #endif /* __G_MACROS_H__ */