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.1 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
41 #define G_GNUC_CHECK_VERSION(major, minor) \
42 ((__GNUC__ > (major)) || \
43 ((__GNUC__ == (major)) && \
44 (__GNUC_MINOR__ >= (minor))))
46 #define G_GNUC_CHECK_VERSION(major, minor) 0
49 /* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
50 * where this is valid. This allows for warningless compilation of
51 * "long long" types even in the presence of '-ansi -pedantic'.
53 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
54 #define G_GNUC_EXTENSION __extension__
56 #define G_GNUC_EXTENSION
59 /* Every compiler that we target supports inlining, but some of them may
60 * complain about it if we don't say "__inline". If we have C99, or if
61 * we are using C++, then we can use "inline" directly. Unfortunately
62 * Visual Studio does not support __STDC_VERSION__, so we need to check
63 * whether we are on Visual Studio 2013 or earlier to see that we need to
64 * say "__inline" in C mode.
65 * Otherwise, we say "__inline" to avoid the warning.
70 # if (_MSC_VER < 1900)
71 # define G_INLINE_DEFINE_NEEDED
73 # elif !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199900)
74 # define G_INLINE_DEFINE_NEEDED
78 #ifdef G_INLINE_DEFINE_NEEDED
80 # define inline __inline
83 #undef G_INLINE_DEFINE_NEEDED
85 /* For historical reasons we need to continue to support those who
86 * define G_IMPLEMENT_INLINES to mean "don't implement this here".
88 #ifdef G_IMPLEMENT_INLINES
89 # define G_INLINE_FUNC extern
92 # define G_INLINE_FUNC static inline
93 #endif /* G_IMPLEMENT_INLINES */
95 /* Provide macros to feature the GCC function attribute.
97 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
98 #define G_GNUC_PURE __attribute__((__pure__))
99 #define G_GNUC_MALLOC __attribute__((__malloc__))
100 #define G_GNUC_NO_INLINE __attribute__((noinline))
103 #define G_GNUC_MALLOC
104 #define G_GNUC_NO_INLINE
108 #define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
110 #define G_GNUC_NULL_TERMINATED
114 * Clang feature detection: http://clang.llvm.org/docs/LanguageExtensions.html
115 * These are not available on GCC, but since the pre-processor doesn't do
116 * operator short-circuiting, we can't use it in a statement or we'll get:
118 * error: missing binary operator before token "("
120 * So we define it to 0 to satisfy the pre-processor.
123 #ifdef __has_attribute
124 #define g_macro__has_attribute __has_attribute
126 #define g_macro__has_attribute(x) 0
130 #define g_macro__has_feature __has_feature
132 #define g_macro__has_feature(x) 0
136 #define g_macro__has_builtin __has_builtin
138 #define g_macro__has_builtin(x) 0
141 #if (!defined(__clang__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) || \
142 (defined(__clang__) && g_macro__has_attribute(__alloc_size__))
143 #define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x)))
144 #define G_GNUC_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y)))
146 #define G_GNUC_ALLOC_SIZE(x)
147 #define G_GNUC_ALLOC_SIZE2(x,y)
150 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
151 #if !defined (__clang__) && G_GNUC_CHECK_VERSION (4, 4)
152 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
153 __attribute__((__format__ (gnu_printf, format_idx, arg_idx)))
154 #define G_GNUC_SCANF( format_idx, arg_idx ) \
155 __attribute__((__format__ (gnu_scanf, format_idx, arg_idx)))
157 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
158 __attribute__((__format__ (__printf__, format_idx, arg_idx)))
159 #define G_GNUC_SCANF( format_idx, arg_idx ) \
160 __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
162 #define G_GNUC_FORMAT( arg_idx ) \
163 __attribute__((__format_arg__ (arg_idx)))
164 #define G_GNUC_NORETURN \
165 __attribute__((__noreturn__))
166 #define G_GNUC_CONST \
167 __attribute__((__const__))
168 #define G_GNUC_UNUSED \
169 __attribute__((__unused__))
170 #define G_GNUC_NO_INSTRUMENT \
171 __attribute__((__no_instrument_function__))
172 #else /* !__GNUC__ */
173 #define G_GNUC_PRINTF( format_idx, arg_idx )
174 #define G_GNUC_SCANF( format_idx, arg_idx )
175 #define G_GNUC_FORMAT( arg_idx )
176 #define G_GNUC_NORETURN
178 #define G_GNUC_UNUSED
179 #define G_GNUC_NO_INSTRUMENT
180 #endif /* !__GNUC__ */
182 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
183 #define G_GNUC_DEPRECATED __attribute__((__deprecated__))
185 #define G_GNUC_DEPRECATED
186 #endif /* __GNUC__ */
188 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
189 #define G_GNUC_DEPRECATED_FOR(f) \
190 __attribute__((deprecated("Use " #f " instead")))
192 #define G_GNUC_DEPRECATED_FOR(f) G_GNUC_DEPRECATED
193 #endif /* __GNUC__ */
196 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
197 _Pragma ("warning (push)") \
198 _Pragma ("warning (disable:1478)")
199 #define G_GNUC_END_IGNORE_DEPRECATIONS \
200 _Pragma ("warning (pop)")
201 #elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
202 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
203 _Pragma ("GCC diagnostic push") \
204 _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
205 #define G_GNUC_END_IGNORE_DEPRECATIONS \
206 _Pragma ("GCC diagnostic pop")
207 #elif defined (_MSC_VER) && (_MSC_VER >= 1500)
208 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
209 __pragma (warning (push)) \
210 __pragma (warning (disable : 4996))
211 #define G_GNUC_END_IGNORE_DEPRECATIONS \
212 __pragma (warning (pop))
213 #elif defined (__clang__)
214 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
215 _Pragma("clang diagnostic push") \
216 _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
217 #define G_GNUC_END_IGNORE_DEPRECATIONS \
218 _Pragma("clang diagnostic pop")
220 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS
221 #define G_GNUC_END_IGNORE_DEPRECATIONS
224 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
225 #define G_GNUC_MAY_ALIAS __attribute__((may_alias))
227 #define G_GNUC_MAY_ALIAS
230 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
231 #define G_GNUC_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
233 #define G_GNUC_WARN_UNUSED_RESULT
234 #endif /* __GNUC__ */
236 #ifndef G_DISABLE_DEPRECATED
237 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
238 * macros, so we can refer to them as strings unconditionally.
239 * usage not-recommended since gcc-3.0
241 #if defined (__GNUC__) && (__GNUC__ < 3)
242 #define G_GNUC_FUNCTION __FUNCTION__
243 #define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
244 #else /* !__GNUC__ */
245 #define G_GNUC_FUNCTION ""
246 #define G_GNUC_PRETTY_FUNCTION ""
247 #endif /* !__GNUC__ */
248 #endif /* !G_DISABLE_DEPRECATED */
250 #if g_macro__has_feature(attribute_analyzer_noreturn) && defined(__clang_analyzer__)
251 #define G_ANALYZER_ANALYZING 1
252 #define G_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
254 #define G_ANALYZER_ANALYZING 0
255 #define G_ANALYZER_NORETURN
258 #define G_STRINGIFY(macro_or_string) G_STRINGIFY_ARG (macro_or_string)
259 #define G_STRINGIFY_ARG(contents) #contents
261 #ifndef __GI_SCANNER__ /* The static assert macro really confuses the introspection parser */
262 #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
263 #define G_PASTE(identifier1,identifier2) G_PASTE_ARGS (identifier1, identifier2)
265 #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __COUNTER__)[(expr) ? 1 : -1] G_GNUC_UNUSED
267 #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __LINE__)[(expr) ? 1 : -1] G_GNUC_UNUSED
269 #define G_STATIC_ASSERT_EXPR(expr) ((void) sizeof (char[(expr) ? 1 : -1]))
272 /* Provide a string identifying the current code position */
273 #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)
274 #define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
276 #define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__)
279 /* Provide a string identifying the current function, non-concatenatable */
280 #if defined (__GNUC__) && defined (__cplusplus)
281 #define G_STRFUNC ((const char*) (__PRETTY_FUNCTION__))
282 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
283 #define G_STRFUNC ((const char*) (__func__))
284 #elif defined (__GNUC__) || (defined(_MSC_VER) && (_MSC_VER > 1300))
285 #define G_STRFUNC ((const char*) (__FUNCTION__))
287 #define G_STRFUNC ((const char*) ("???"))
290 /* Guard C code in headers, while including them from C++ */
292 #define G_BEGIN_DECLS extern "C" {
293 #define G_END_DECLS }
295 #define G_BEGIN_DECLS
299 /* Provide definitions for some commonly used macros.
300 * Some of them are only provided if they haven't already
301 * been defined. It is assumed that if they are already
302 * defined then the current definition is correct.
307 # else /* !__cplusplus */
308 # define NULL ((void*) 0)
309 # endif /* !__cplusplus */
317 #define TRUE (!FALSE)
321 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
324 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
327 #define ABS(a) (((a) < 0) ? -(a) : (a))
330 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
332 #define G_APPROX_VALUE(a, b, epsilon) \
333 (((a) > (b) ? (a) - (b) : (b) - (a)) < (epsilon))
335 /* Count the number of elements in an array. The array must be defined
336 * as such; using this with a dynamically allocated array will give
339 #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
341 /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT
343 #define GPOINTER_TO_SIZE(p) ((gsize) (p))
344 #define GSIZE_TO_POINTER(s) ((gpointer) (gsize) (s))
346 /* Provide convenience macros for handling structure
347 * fields through their offsets.
350 #if (defined(__GNUC__) && __GNUC__ >= 4) || defined (_MSC_VER)
351 #define G_STRUCT_OFFSET(struct_type, member) \
352 ((glong) offsetof (struct_type, member))
354 #define G_STRUCT_OFFSET(struct_type, member) \
355 ((glong) ((guint8*) &((struct_type*) 0)->member))
358 #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
359 ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
360 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
361 (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
363 /* Provide simple macro statement wrappers:
364 * G_STMT_START { statements; } G_STMT_END;
365 * This can be used as a single statement, like:
366 * if (x) G_STMT_START { ... } G_STMT_END; else ...
367 * This intentionally does not use compiler extensions like GCC's '({...})' to
368 * avoid portability issue or side effects when compiled with different compilers.
369 * MSVC complains about "while(0)": C4127: "Conditional expression is constant",
370 * so we use __pragma to avoid the warning since the use here is intentional.
372 #if !(defined (G_STMT_START) && defined (G_STMT_END))
373 #define G_STMT_START do
374 #if defined (_MSC_VER) && (_MSC_VER >= 1500)
376 __pragma(warning(push)) \
377 __pragma(warning(disable:4127)) \
379 __pragma(warning(pop))
381 #define G_STMT_END while (0)
385 /* Deprecated -- do not use. */
386 #ifndef G_DISABLE_DEPRECATED
387 #ifdef G_DISABLE_CONST_RETURNS
388 #define G_CONST_RETURN
390 #define G_CONST_RETURN const
395 * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to
396 * the compiler about the expected result of an expression. Some compilers
397 * can use this information for optimizations.
399 * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
400 * putting assignments in g_return_if_fail ().
402 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
403 #define _G_BOOLEAN_EXPR(expr) \
404 G_GNUC_EXTENSION ({ \
405 int _g_boolean_var_; \
407 _g_boolean_var_ = 1; \
409 _g_boolean_var_ = 0; \
412 #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR((expr)), 1))
413 #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR((expr)), 0))
415 #define G_LIKELY(expr) (expr)
416 #define G_UNLIKELY(expr) (expr)
419 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
420 #define G_DEPRECATED __attribute__((__deprecated__))
421 #elif defined(_MSC_VER) && (_MSC_VER >= 1300)
422 #define G_DEPRECATED __declspec(deprecated)
427 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
428 #define G_DEPRECATED_FOR(f) __attribute__((__deprecated__("Use '" #f "' instead")))
429 #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320)
430 #define G_DEPRECATED_FOR(f) __declspec(deprecated("is deprecated. Use '" #f "' instead"))
432 #define G_DEPRECATED_FOR(f) G_DEPRECATED
435 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
436 #define G_UNAVAILABLE(maj,min) __attribute__((deprecated("Not available before " #maj "." #min)))
437 #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320)
438 #define G_UNAVAILABLE(maj,min) __declspec(deprecated("is not available before " #maj "." #min))
440 #define G_UNAVAILABLE(maj,min) G_DEPRECATED
444 #define _GLIB_EXTERN extern
447 /* These macros are used to mark deprecated functions in GLib headers,
448 * and thus have to be exposed in installed headers. But please
449 * do *not* use them in other projects. Instead, use G_DEPRECATED
450 * or define your own wrappers around it.
453 #ifdef GLIB_DISABLE_DEPRECATION_WARNINGS
454 #define GLIB_DEPRECATED _GLIB_EXTERN
455 #define GLIB_DEPRECATED_FOR(f) _GLIB_EXTERN
456 #define GLIB_UNAVAILABLE(maj,min) _GLIB_EXTERN
458 #define GLIB_DEPRECATED G_DEPRECATED _GLIB_EXTERN
459 #define GLIB_DEPRECATED_FOR(f) G_DEPRECATED_FOR(f) _GLIB_EXTERN
460 #define GLIB_UNAVAILABLE(maj,min) G_UNAVAILABLE(maj,min) _GLIB_EXTERN
463 #ifndef __GI_SCANNER__
467 /* these macros are private */
468 #define _GLIB_AUTOPTR_FUNC_NAME(TypeName) glib_autoptr_cleanup_##TypeName
469 #define _GLIB_AUTOPTR_TYPENAME(TypeName) TypeName##_autoptr
470 #define _GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName) glib_listautoptr_cleanup_##TypeName
471 #define _GLIB_AUTOPTR_LIST_TYPENAME(TypeName) TypeName##_listautoptr
472 #define _GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName) glib_slistautoptr_cleanup_##TypeName
473 #define _GLIB_AUTOPTR_SLIST_TYPENAME(TypeName) TypeName##_slistautoptr
474 #define _GLIB_AUTO_FUNC_NAME(TypeName) glib_auto_cleanup_##TypeName
475 #define _GLIB_CLEANUP(func) __attribute__((cleanup(func)))
476 #define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName) \
477 typedef ModuleObjName *_GLIB_AUTOPTR_TYPENAME(ModuleObjName); \
478 static inline void _GLIB_AUTOPTR_FUNC_NAME(ModuleObjName) (ModuleObjName **_ptr) { \
479 _GLIB_AUTOPTR_FUNC_NAME(ParentName) ((ParentName **) _ptr); } \
482 /* these macros are API */
483 #define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) \
484 typedef TypeName *_GLIB_AUTOPTR_TYPENAME(TypeName); \
485 typedef GList *_GLIB_AUTOPTR_LIST_TYPENAME(TypeName); \
486 typedef GSList *_GLIB_AUTOPTR_SLIST_TYPENAME(TypeName); \
487 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
488 static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_FUNC_NAME(TypeName) (TypeName **_ptr) { if (*_ptr) (func) (*_ptr); } \
489 static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName) (GList **_l) { g_list_free_full (*_l, (GDestroyNotify) (void(*)(void)) func); } \
490 static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName) (GSList **_l) { g_slist_free_full (*_l, (GDestroyNotify) (void(*)(void)) func); } \
491 G_GNUC_END_IGNORE_DEPRECATIONS
492 #define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func) \
493 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
494 static inline void _GLIB_AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { (func) (_ptr); } \
495 G_GNUC_END_IGNORE_DEPRECATIONS
496 #define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none) \
497 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
498 static inline void _GLIB_AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { if (*_ptr != none) (func) (*_ptr); } \
499 G_GNUC_END_IGNORE_DEPRECATIONS
500 #define g_autoptr(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_TYPENAME(TypeName)
501 #define g_autolist(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_LIST_TYPENAME(TypeName)
502 #define g_autoslist(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_SLIST_TYPENAME(TypeName)
503 #define g_auto(TypeName) _GLIB_CLEANUP(_GLIB_AUTO_FUNC_NAME(TypeName)) TypeName
504 #define g_autofree _GLIB_CLEANUP(g_autoptr_cleanup_generic_gfree)
506 #else /* not GNU C */
507 /* this (dummy) macro is private */
508 #define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName)
510 /* these (dummy) macros are API */
511 #define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func)
512 #define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func)
513 #define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none)
515 /* no declaration of g_auto() or g_autoptr() here */
516 #endif /* __GNUC__ */
520 #define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName)
522 #define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func)
523 #define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func)
524 #define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none)
526 #endif /* __GI_SCANNER__ */
528 #endif /* __G_MACROS_H__ */