glib/gmacros.h: Fix up attribute to avoid namespace collisions
[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__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
118 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS                \
119   _Pragma ("GCC diagnostic push")                       \
120   _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
121 #define G_GNUC_END_IGNORE_DEPRECATIONS                  \
122   _Pragma ("GCC diagnostic pop")
123 #else
124 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS
125 #define G_GNUC_END_IGNORE_DEPRECATIONS
126 #endif
127
128 #if     __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
129 #  define G_GNUC_MAY_ALIAS __attribute__((may_alias))
130 #else
131 #  define G_GNUC_MAY_ALIAS
132 #endif
133
134 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
135 #define G_GNUC_WARN_UNUSED_RESULT               \
136   __attribute__((warn_unused_result))
137 #else
138 #define G_GNUC_WARN_UNUSED_RESULT
139 #endif /* __GNUC__ */
140
141 #ifndef G_DISABLE_DEPRECATED
142 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
143  * macros, so we can refer to them as strings unconditionally.
144  * usage not-recommended since gcc-3.0
145  */
146 #if defined (__GNUC__) && (__GNUC__ < 3)
147 #define G_GNUC_FUNCTION         __FUNCTION__
148 #define G_GNUC_PRETTY_FUNCTION  __PRETTY_FUNCTION__
149 #else   /* !__GNUC__ */
150 #define G_GNUC_FUNCTION         ""
151 #define G_GNUC_PRETTY_FUNCTION  ""
152 #endif  /* !__GNUC__ */
153 #endif  /* !G_DISABLE_DEPRECATED */
154
155 #define G_STRINGIFY(macro_or_string)    G_STRINGIFY_ARG (macro_or_string)
156 #define G_STRINGIFY_ARG(contents)       #contents
157
158 #ifndef __GI_SCANNER__ /* The static assert macro really confuses the introspection parser */
159 #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
160 #define G_PASTE(identifier1,identifier2)      G_PASTE_ARGS (identifier1, identifier2)
161 #ifdef __COUNTER__
162 #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __COUNTER__)[(expr) ? 1 : -1]
163 #else
164 #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __LINE__)[(expr) ? 1 : -1]
165 #endif
166 #define G_STATIC_ASSERT_EXPR(expr) ((void) sizeof (char[(expr) ? 1 : -1]))
167 #endif
168
169 /* Provide a string identifying the current code position */
170 #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)
171 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
172 #else
173 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__)
174 #endif
175
176 /* Provide a string identifying the current function, non-concatenatable */
177 #if defined (__GNUC__)
178 #  define G_STRFUNC     ((const char*) (__PRETTY_FUNCTION__))
179 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 19901L
180 #  define G_STRFUNC     ((const char*) (__func__))
181 #elif defined(_MSC_VER) && (_MSC_VER > 1300)
182 #  define G_STRFUNC     ((const char*) (__FUNCTION__))
183 #else
184 #  define G_STRFUNC     ((const char*) ("???"))
185 #endif
186
187 /* Guard C code in headers, while including them from C++ */
188 #ifdef  __cplusplus
189 # define G_BEGIN_DECLS  extern "C" {
190 # define G_END_DECLS    }
191 #else
192 # define G_BEGIN_DECLS
193 # define G_END_DECLS
194 #endif
195
196 /* Provide definitions for some commonly used macros.
197  *  Some of them are only provided if they haven't already
198  *  been defined. It is assumed that if they are already
199  *  defined then the current definition is correct.
200  */
201 #ifndef NULL
202 #  ifdef __cplusplus
203 #    define NULL        (0L)
204 #  else /* !__cplusplus */
205 #    define NULL        ((void*) 0)
206 #  endif /* !__cplusplus */
207 #endif
208
209 #ifndef FALSE
210 #define FALSE   (0)
211 #endif
212
213 #ifndef TRUE
214 #define TRUE    (!FALSE)
215 #endif
216
217 #undef  MAX
218 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
219
220 #undef  MIN
221 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
222
223 #undef  ABS
224 #define ABS(a)     (((a) < 0) ? -(a) : (a))
225
226 #undef  CLAMP
227 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
228
229 /* Count the number of elements in an array. The array must be defined
230  * as such; using this with a dynamically allocated array will give
231  * incorrect results.
232  */
233 #define G_N_ELEMENTS(arr)               (sizeof (arr) / sizeof ((arr)[0]))
234
235 /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT
236  */
237 #define GPOINTER_TO_SIZE(p)     ((gsize) (p))
238 #define GSIZE_TO_POINTER(s)     ((gpointer) (gsize) (s))
239
240 /* Provide convenience macros for handling structure
241  * fields through their offsets.
242  */
243
244 #if defined(__GNUC__)  && __GNUC__ >= 4
245 #  define G_STRUCT_OFFSET(struct_type, member) \
246       ((glong) offsetof (struct_type, member))
247 #else
248 #  define G_STRUCT_OFFSET(struct_type, member)  \
249       ((glong) ((guint8*) &((struct_type*) 0)->member))
250 #endif
251
252 #define G_STRUCT_MEMBER_P(struct_p, struct_offset)   \
253     ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
254 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset)   \
255     (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
256
257 /* Provide simple macro statement wrappers:
258  *   G_STMT_START { statements; } G_STMT_END;
259  * This can be used as a single statement, like:
260  *   if (x) G_STMT_START { ... } G_STMT_END; else ...
261  * This intentionally does not use compiler extensions like GCC's '({...})' to
262  * avoid portability issue or side effects when compiled with different compilers.
263  */
264 #if !(defined (G_STMT_START) && defined (G_STMT_END))
265 #  define G_STMT_START  do
266 #  define G_STMT_END    while (0)
267 #endif
268
269 /* Deprecated -- do not use. */
270 #ifndef G_DISABLE_DEPRECATED
271 #ifdef G_DISABLE_CONST_RETURNS
272 #define G_CONST_RETURN
273 #else
274 #define G_CONST_RETURN const
275 #endif
276 #endif
277
278 /*
279  * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to 
280  * the compiler about the expected result of an expression. Some compilers
281  * can use this information for optimizations.
282  *
283  * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
284  * putting assignments in g_return_if_fail ().  
285  */
286 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
287 #define _G_BOOLEAN_EXPR(expr)                   \
288  G_GNUC_EXTENSION ({                            \
289    int _g_boolean_var_;                         \
290    if (expr)                                    \
291       _g_boolean_var_ = 1;                      \
292    else                                         \
293       _g_boolean_var_ = 0;                      \
294    _g_boolean_var_;                             \
295 })
296 #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))
297 #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))
298 #else
299 #define G_LIKELY(expr) (expr)
300 #define G_UNLIKELY(expr) (expr)
301 #endif
302
303 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
304 #define G_DEPRECATED __attribute__((__deprecated__))
305 #elif defined(_MSC_VER) && (_MSC_VER >= 1300)
306 #define G_DEPRECATED __declspec(deprecated)
307 #else
308 #define G_DEPRECATED
309 #endif
310
311 #if    __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
312 #define G_DEPRECATED_FOR(f) __attribute__((__deprecated__("Use '" #f "' instead")))
313 #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320)
314 #define G_DEPRECATED_FOR(f) __declspec(deprecated("is deprecated. Use '" #f "' instead"))
315 #else
316 #define G_DEPRECATED_FOR(f) G_DEPRECATED
317 #endif
318
319 #if    __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
320 #define G_UNAVAILABLE(maj,min) __attribute__((deprecated("Not available before " #maj "." #min)))
321 #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320)
322 #define G_UNAVAILABLE(maj,min) __declspec(deprecated("is not available before " #maj "." #min))
323 #else
324 #define G_UNAVAILABLE(maj,min)
325 #endif
326
327 /* These macros are used to mark deprecated functions in GLib headers,
328  * and thus have to be exposed in installed headers. But please
329  * do *not* use them in other projects. Instead, use G_DEPRECATED
330  * or define your own wrappers around it.
331  */
332
333 #ifdef GLIB_DISABLE_DEPRECATION_WARNINGS
334 #define GLIB_DEPRECATED
335 #define GLIB_DEPRECATED_FOR(f)
336 #define GLIB_UNAVAILABLE(maj,min)
337 #else
338 #define GLIB_DEPRECATED G_DEPRECATED
339 #define GLIB_DEPRECATED_FOR(f) G_DEPRECATED_FOR(f)
340 #define GLIB_UNAVAILABLE(maj,min) G_UNAVAILABLE(maj,min)
341 #endif
342
343 #endif /* __G_MACROS_H__ */