1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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/>.
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/.
25 /* This file must not include any other glib header file and must thus
26 * not refer to variables from glibconfig.h
29 #ifndef __G_MACROS_H__
30 #define __G_MACROS_H__
32 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
33 #error "Only <glib.h> can be included directly."
36 /* We include stddef.h to get the system's definition of NULL
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'.
44 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
45 #define G_GNUC_EXTENSION __extension__
47 #define G_GNUC_EXTENSION
50 /* Provide macros to feature the GCC function attribute.
52 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
53 #define G_GNUC_PURE __attribute__((__pure__))
54 #define G_GNUC_MALLOC __attribute__((__malloc__))
61 #define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
63 #define G_GNUC_NULL_TERMINATED
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)))
70 #define G_GNUC_ALLOC_SIZE(x)
71 #define G_GNUC_ALLOC_SIZE2(x,y)
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__))
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
96 #define G_GNUC_NO_INSTRUMENT
97 #endif /* !__GNUC__ */
99 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
100 #define G_GNUC_DEPRECATED __attribute__((__deprecated__))
102 #define G_GNUC_DEPRECATED
103 #endif /* __GNUC__ */
105 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
106 #define G_GNUC_DEPRECATED_FOR(f) \
107 __attribute__((deprecated("Use " #f " instead")))
109 #define G_GNUC_DEPRECATED_FOR(f) G_GNUC_DEPRECATED
110 #endif /* __GNUC__ */
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))
125 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS
126 #define G_GNUC_END_IGNORE_DEPRECATIONS
129 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
130 #define G_GNUC_MAY_ALIAS __attribute__((may_alias))
132 #define G_GNUC_MAY_ALIAS
135 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
136 #define G_GNUC_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
138 #define G_GNUC_WARN_UNUSED_RESULT
139 #endif /* __GNUC__ */
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
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 */
155 /* Clang feature detection: http://clang.llvm.org/docs/LanguageExtensions.html */
156 #ifndef __has_feature
157 #define __has_feature(x) 0
160 #if __has_feature(attribute_analyzer_noreturn)
161 #define G_ANALYZER_ANALYZING 1
162 #define G_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
164 #define G_ANALYZER_ANALYZING 0
165 #define G_ANALYZER_NORETURN
168 #define G_STRINGIFY(macro_or_string) G_STRINGIFY_ARG (macro_or_string)
169 #define G_STRINGIFY_ARG(contents) #contents
171 #ifndef __GI_SCANNER__ /* The static assert macro really confuses the introspection parser */
172 #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
173 #define G_PASTE(identifier1,identifier2) G_PASTE_ARGS (identifier1, identifier2)
175 #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __COUNTER__)[(expr) ? 1 : -1] G_GNUC_UNUSED
177 #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __LINE__)[(expr) ? 1 : -1] G_GNUC_UNUSED
179 #define G_STATIC_ASSERT_EXPR(expr) ((void) sizeof (char[(expr) ? 1 : -1]))
182 /* Provide a string identifying the current code position */
183 #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)
184 #define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
186 #define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__)
189 /* Provide a string identifying the current function, non-concatenatable */
190 #if defined (__GNUC__) && defined (__cplusplus)
191 #define G_STRFUNC ((const char*) (__PRETTY_FUNCTION__))
192 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
193 #define G_STRFUNC ((const char*) (__func__))
194 #elif defined (__GNUC__) || (defined(_MSC_VER) && (_MSC_VER > 1300))
195 #define G_STRFUNC ((const char*) (__FUNCTION__))
197 #define G_STRFUNC ((const char*) ("???"))
200 /* Guard C code in headers, while including them from C++ */
202 #define G_BEGIN_DECLS extern "C" {
203 #define G_END_DECLS }
205 #define G_BEGIN_DECLS
209 /* Provide definitions for some commonly used macros.
210 * Some of them are only provided if they haven't already
211 * been defined. It is assumed that if they are already
212 * defined then the current definition is correct.
217 # else /* !__cplusplus */
218 # define NULL ((void*) 0)
219 # endif /* !__cplusplus */
227 #define TRUE (!FALSE)
231 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
234 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
237 #define ABS(a) (((a) < 0) ? -(a) : (a))
240 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
242 /* Count the number of elements in an array. The array must be defined
243 * as such; using this with a dynamically allocated array will give
246 #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
248 /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT
250 #define GPOINTER_TO_SIZE(p) ((gsize) (p))
251 #define GSIZE_TO_POINTER(s) ((gpointer) (gsize) (s))
253 /* Provide convenience macros for handling structure
254 * fields through their offsets.
257 #if defined(__GNUC__) && __GNUC__ >= 4
258 #define G_STRUCT_OFFSET(struct_type, member) \
259 ((glong) offsetof (struct_type, member))
261 #define G_STRUCT_OFFSET(struct_type, member) \
262 ((glong) ((guint8*) &((struct_type*) 0)->member))
265 #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
266 ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
267 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
268 (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
270 /* Provide simple macro statement wrappers:
271 * G_STMT_START { statements; } G_STMT_END;
272 * This can be used as a single statement, like:
273 * if (x) G_STMT_START { ... } G_STMT_END; else ...
274 * This intentionally does not use compiler extensions like GCC's '({...})' to
275 * avoid portability issue or side effects when compiled with different compilers.
277 #if !(defined (G_STMT_START) && defined (G_STMT_END))
278 #define G_STMT_START do
279 #define G_STMT_END while (0)
282 /* Deprecated -- do not use. */
283 #ifndef G_DISABLE_DEPRECATED
284 #ifdef G_DISABLE_CONST_RETURNS
285 #define G_CONST_RETURN
287 #define G_CONST_RETURN const
292 * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to
293 * the compiler about the expected result of an expression. Some compilers
294 * can use this information for optimizations.
296 * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
297 * putting assignments in g_return_if_fail ().
299 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
300 #define _G_BOOLEAN_EXPR(expr) \
301 G_GNUC_EXTENSION ({ \
302 int _g_boolean_var_; \
304 _g_boolean_var_ = 1; \
306 _g_boolean_var_ = 0; \
309 #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))
310 #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))
312 #define G_LIKELY(expr) (expr)
313 #define G_UNLIKELY(expr) (expr)
316 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
317 #define G_DEPRECATED __attribute__((__deprecated__))
318 #elif defined(_MSC_VER) && (_MSC_VER >= 1300)
319 #define G_DEPRECATED __declspec(deprecated)
324 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
325 #define G_DEPRECATED_FOR(f) __attribute__((__deprecated__("Use '" #f "' instead")))
326 #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320)
327 #define G_DEPRECATED_FOR(f) __declspec(deprecated("is deprecated. Use '" #f "' instead"))
329 #define G_DEPRECATED_FOR(f) G_DEPRECATED
332 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
333 #define G_UNAVAILABLE(maj,min) __attribute__((deprecated("Not available before " #maj "." #min)))
334 #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320)
335 #define G_UNAVAILABLE(maj,min) __declspec(deprecated("is not available before " #maj "." #min))
337 #define G_UNAVAILABLE(maj,min) G_DEPRECATED
341 #define _GLIB_EXTERN extern
344 /* These macros are used to mark deprecated functions in GLib headers,
345 * and thus have to be exposed in installed headers. But please
346 * do *not* use them in other projects. Instead, use G_DEPRECATED
347 * or define your own wrappers around it.
350 #ifdef GLIB_DISABLE_DEPRECATION_WARNINGS
351 #define GLIB_DEPRECATED _GLIB_EXTERN
352 #define GLIB_DEPRECATED_FOR(f) _GLIB_EXTERN
353 #define GLIB_UNAVAILABLE(maj,min) _GLIB_EXTERN
355 #define GLIB_DEPRECATED G_DEPRECATED _GLIB_EXTERN
356 #define GLIB_DEPRECATED_FOR(f) G_DEPRECATED_FOR(f) _GLIB_EXTERN
357 #define GLIB_UNAVAILABLE(maj,min) G_UNAVAILABLE(maj,min) _GLIB_EXTERN
360 #endif /* __G_MACROS_H__ */