1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
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/.
27 /* This file must not include any other glib header file and must thus
28 * not refer to variables from glibconfig.h
31 #ifndef __G_MACROS_H__
32 #define __G_MACROS_H__
34 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
35 #error "Only <glib.h> can be included directly."
38 /* We include stddef.h to get the system's definition of NULL
43 * Note: Clang (but not clang-cl) defines __GNUC__ and __GNUC_MINOR__.
44 * Both Clang 11.1 on current Arch Linux and Apple's Clang 12.0 define
45 * __GNUC__ = 4 and __GNUC_MINOR__ = 2. So G_GNUC_CHECK_VERSION(4, 2) on
46 * current Clang will be 1.
49 #define G_GNUC_CHECK_VERSION(major, minor) \
50 ((__GNUC__ > (major)) || \
51 ((__GNUC__ == (major)) && \
52 (__GNUC_MINOR__ >= (minor))))
54 #define G_GNUC_CHECK_VERSION(major, minor) 0
57 /* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
58 * where this is valid. This allows for warningless compilation of
59 * "long long" types even in the presence of '-ansi -pedantic'.
61 #if G_GNUC_CHECK_VERSION(2, 8)
62 #define G_GNUC_EXTENSION __extension__
64 #define G_GNUC_EXTENSION
67 /* Every compiler that we target supports inlining, but some of them may
68 * complain about it if we don't say "__inline". If we have C99, or if
69 * we are using C++, then we can use "inline" directly. Unfortunately
70 * Visual Studio does not support __STDC_VERSION__, so we need to check
71 * whether we are on Visual Studio 2013 or earlier to see that we need to
72 * say "__inline" in C mode.
73 * Otherwise, we say "__inline" to avoid the warning.
78 # if (_MSC_VER < 1900)
79 # define G_INLINE_DEFINE_NEEDED
81 # elif !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199900)
82 # define G_INLINE_DEFINE_NEEDED
86 #ifdef G_INLINE_DEFINE_NEEDED
88 # define inline __inline
91 #undef G_INLINE_DEFINE_NEEDED
96 * This macro used to be used to conditionally define inline functions
97 * in a compatible way before this feature was supported in all
98 * compilers. These days, GLib requires inlining support from the
99 * compiler, so your GLib-using programs can safely assume that the
100 * "inline" keyword works properly.
102 * Never use this macro anymore. Just say "static inline".
104 * Deprecated: 2.48: Use "static inline" instead
107 /* For historical reasons we need to continue to support those who
108 * define G_IMPLEMENT_INLINES to mean "don't implement this here".
110 #ifdef G_IMPLEMENT_INLINES
111 # define G_INLINE_FUNC extern GLIB_DEPRECATED_MACRO_IN_2_48_FOR(static inline)
114 # define G_INLINE_FUNC static inline GLIB_DEPRECATED_MACRO_IN_2_48_FOR(static inline)
115 #endif /* G_IMPLEMENT_INLINES */
118 * Attribute support detection. Works on clang and GCC >= 5
119 * https://clang.llvm.org/docs/LanguageExtensions.html#has-attribute
120 * https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005fattribute.html
123 #ifdef __has_attribute
124 #define g_macro__has_attribute __has_attribute
128 * Fallback for GCC < 5 and other compilers not supporting __has_attribute.
130 #define g_macro__has_attribute(x) g_macro__has_attribute_##x
132 #define g_macro__has_attribute___pure__ G_GNUC_CHECK_VERSION (2, 96)
133 #define g_macro__has_attribute___malloc__ G_GNUC_CHECK_VERSION (2, 96)
134 #define g_macro__has_attribute___noinline__ G_GNUC_CHECK_VERSION (2, 96)
135 #define g_macro__has_attribute___sentinel__ G_GNUC_CHECK_VERSION (4, 0)
136 #define g_macro__has_attribute___alloc_size__ G_GNUC_CHECK_VERSION (4, 3)
137 #define g_macro__has_attribute___format__ G_GNUC_CHECK_VERSION (2, 4)
138 #define g_macro__has_attribute___format_arg__ G_GNUC_CHECK_VERSION (2, 4)
139 #define g_macro__has_attribute___noreturn__ (G_GNUC_CHECK_VERSION (2, 8) || (0x5110 <= __SUNPRO_C))
140 #define g_macro__has_attribute___const__ G_GNUC_CHECK_VERSION (2, 4)
141 #define g_macro__has_attribute___unused__ G_GNUC_CHECK_VERSION (2, 4)
142 #define g_macro__has_attribute___no_instrument_function__ G_GNUC_CHECK_VERSION (2, 4)
143 #define g_macro__has_attribute_fallthrough G_GNUC_CHECK_VERSION (6, 0)
144 #define g_macro__has_attribute___deprecated__ G_GNUC_CHECK_VERSION (3, 1)
145 #define g_macro__has_attribute_may_alias G_GNUC_CHECK_VERSION (3, 3)
146 #define g_macro__has_attribute_warn_unused_result G_GNUC_CHECK_VERSION (3, 4)
147 #define g_macro__has_attribute_cleanup G_GNUC_CHECK_VERSION (3, 3)
151 /* Provide macros to feature the GCC function attribute.
157 * Expands to the GNU C `pure` function attribute if the compiler is gcc.
158 * Declaring a function as `pure` enables better optimization of calls to
159 * the function. A `pure` function has no effects except its return value
160 * and the return value depends only on the parameters and/or global
163 * Place the attribute after the declaration, just before the semicolon.
165 * |[<!-- language="C" -->
166 * gboolean g_type_check_value (const GValue *value) G_GNUC_PURE;
169 * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute) for more details.
176 * [GNU C `malloc` function attribute](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-functions-that-behave-like-malloc)
177 * if the compiler is gcc.
178 * Declaring a function as `malloc` enables better optimization of the function,
179 * but must only be done if the allocation behaviour of the function is fully
180 * understood, otherwise miscompilation can result.
182 * A function can have the `malloc` attribute if it returns a pointer which is
183 * guaranteed to not alias with any other pointer valid when the function
184 * returns, and moreover no pointers to valid objects occur in any storage
185 * addressed by the returned pointer.
187 * In practice, this means that `G_GNUC_MALLOC` can be used with any function
188 * which returns unallocated or zeroed-out memory, but not with functions which
189 * return initialised structures containing other pointers, or with functions
190 * that reallocate memory. This definition changed in GLib 2.58 to match the
191 * stricter definition introduced around GCC 5.
193 * Place the attribute after the declaration, just before the semicolon.
195 * |[<!-- language="C" -->
196 * gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
200 * [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-functions-that-behave-like-malloc)
209 * Expands to the GNU C `noinline` function attribute if the compiler is gcc.
210 * If the compiler is not gcc, this macro expands to nothing.
212 * Declaring a function as `noinline` prevents the function from being
213 * considered for inlining.
215 * This macro is provided for retro-compatibility and will be eventually
216 * deprecated, but %G_NO_INLINE should be used instead.
218 * The attribute may be placed before the declaration or definition,
219 * right before the `static` keyword.
221 * |[<!-- language="C" -->
224 * do_not_inline_this (void)
231 * [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noinline-function-attribute)
234 * See also: %G_NO_INLINE, %G_ALWAYS_INLINE.
239 #if g_macro__has_attribute(__pure__)
240 #define G_GNUC_PURE __attribute__((__pure__))
245 #if g_macro__has_attribute(__malloc__)
246 #define G_GNUC_MALLOC __attribute__ ((__malloc__))
248 #define G_GNUC_MALLOC
251 #if g_macro__has_attribute(__noinline__)
252 #define G_GNUC_NO_INLINE __attribute__ ((__noinline__)) \
253 GLIB_AVAILABLE_MACRO_IN_2_58
255 #define G_GNUC_NO_INLINE \
256 GLIB_AVAILABLE_MACRO_IN_2_58
260 * G_GNUC_NULL_TERMINATED:
262 * Expands to the GNU C `sentinel` function attribute if the compiler is gcc.
263 * This function attribute only applies to variadic functions and instructs
264 * the compiler to check that the argument list is terminated with an
267 * Place the attribute after the declaration, just before the semicolon.
269 * |[<!-- language="C" -->
270 * gchar *g_strconcat (const gchar *string1,
271 * ...) G_GNUC_NULL_TERMINATED;
274 * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-sentinel-function-attribute) for more details.
278 #if g_macro__has_attribute(__sentinel__)
279 #define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
281 #define G_GNUC_NULL_TERMINATED
285 * Clang feature detection: http://clang.llvm.org/docs/LanguageExtensions.html
286 * These are not available on GCC, but since the pre-processor doesn't do
287 * operator short-circuiting, we can't use it in a statement or we'll get:
289 * error: missing binary operator before token "("
291 * So we define it to 0 to satisfy the pre-processor.
295 #define g_macro__has_feature __has_feature
297 #define g_macro__has_feature(x) 0
301 #define g_macro__has_builtin __has_builtin
303 #define g_macro__has_builtin(x) 0
306 #ifdef __has_extension
307 #define g_macro__has_extension __has_extension
309 #define g_macro__has_extension(x) 0
314 * @x: the index of the argument specifying the allocation size
316 * Expands to the GNU C `alloc_size` function attribute if the compiler
317 * is a new enough gcc. This attribute tells the compiler that the
318 * function returns a pointer to memory of a size that is specified
319 * by the @xth function parameter.
321 * Place the attribute after the function declaration, just before the
324 * |[<!-- language="C" -->
325 * gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
328 * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alloc_005fsize-function-attribute) for more details.
334 * G_GNUC_ALLOC_SIZE2:
335 * @x: the index of the argument specifying one factor of the allocation size
336 * @y: the index of the argument specifying the second factor of the allocation size
338 * Expands to the GNU C `alloc_size` function attribute if the compiler is a
339 * new enough gcc. This attribute tells the compiler that the function returns
340 * a pointer to memory of a size that is specified by the product of two
341 * function parameters.
343 * Place the attribute after the function declaration, just before the
346 * |[<!-- language="C" -->
347 * gpointer g_malloc_n (gsize n_blocks,
348 * gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1, 2);
351 * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alloc_005fsize-function-attribute) for more details.
355 #if g_macro__has_attribute(__alloc_size__)
356 #define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x)))
357 #define G_GNUC_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y)))
359 #define G_GNUC_ALLOC_SIZE(x)
360 #define G_GNUC_ALLOC_SIZE2(x,y)
365 * @format_idx: the index of the argument corresponding to the
366 * format string (the arguments are numbered from 1)
367 * @arg_idx: the index of the first of the format arguments, or 0 if
368 * there are no format arguments
370 * Expands to the GNU C `format` function attribute if the compiler is gcc.
371 * This is used for declaring functions which take a variable number of
372 * arguments, with the same syntax as `printf()`. It allows the compiler
373 * to type-check the arguments passed to the function.
375 * Place the attribute after the function declaration, just before the
379 * [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-Wformat-3288)
382 * |[<!-- language="C" -->
383 * gint g_snprintf (gchar *string,
385 * gchar const *format,
386 * ...) G_GNUC_PRINTF (3, 4);
392 * @format_idx: the index of the argument corresponding to
393 * the format string (the arguments are numbered from 1)
394 * @arg_idx: the index of the first of the format arguments, or 0 if
395 * there are no format arguments
397 * Expands to the GNU C `format` function attribute if the compiler is gcc.
398 * This is used for declaring functions which take a variable number of
399 * arguments, with the same syntax as `scanf()`. It allows the compiler
400 * to type-check the arguments passed to the function.
402 * |[<!-- language="C" -->
403 * int my_scanf (MyStream *stream,
404 * const char *format,
405 * ...) G_GNUC_SCANF (2, 3);
406 * int my_vscanf (MyStream *stream,
407 * const char *format,
408 * va_list ap) G_GNUC_SCANF (2, 0);
412 * [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-Wformat-3288)
418 * @format_idx: the index of the argument corresponding to
419 * the format string (the arguments are numbered from 1)
421 * Expands to the GNU C `strftime` format function attribute if the compiler
422 * is gcc. This is used for declaring functions which take a format argument
423 * which is passed to `strftime()` or an API implementing its formats. It allows
424 * the compiler check the format passed to the function.
426 * |[<!-- language="C" -->
427 * gsize my_strftime (MyBuffer *buffer,
428 * const char *format,
429 * const struct tm *tm) G_GNUC_STRFTIME (2);
433 * [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-Wformat-3288)
441 * @arg_idx: the index of the argument
443 * Expands to the GNU C `format_arg` function attribute if the compiler
444 * is gcc. This function attribute specifies that a function takes a
445 * format string for a `printf()`, `scanf()`, `strftime()` or `strfmon()` style
446 * function and modifies it, so that the result can be passed to a `printf()`,
447 * `scanf()`, `strftime()` or `strfmon()` style function (with the remaining
448 * arguments to the format function the same as they would have been
449 * for the unmodified string).
451 * Place the attribute after the function declaration, just before the
454 * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-Wformat-nonliteral-1) for more details.
456 * |[<!-- language="C" -->
457 * gchar *g_dgettext (gchar *domain_name, gchar *msgid) G_GNUC_FORMAT (2);
464 * Expands to the GNU C `noreturn` function attribute if the compiler is gcc.
465 * It is used for declaring functions which never return. It enables
466 * optimization of the function, and avoids possible compiler warnings.
468 * Since 2.68, it is recommended that code uses %G_NORETURN instead of
469 * %G_GNUC_NORETURN, as that works on more platforms and compilers (in
470 * particular, MSVC and C++11) than %G_GNUC_NORETURN, which works with GCC and
471 * Clang only. %G_GNUC_NORETURN continues to work, so has not been deprecated
474 * Place the attribute after the declaration, just before the semicolon.
476 * |[<!-- language="C" -->
477 * void g_abort (void) G_GNUC_NORETURN;
480 * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute) for more details.
486 * Expands to the GNU C `const` function attribute if the compiler is gcc.
487 * Declaring a function as `const` enables better optimization of calls to
488 * the function. A `const` function doesn't examine any values except its
489 * parameters, and has no effects except its return value.
491 * Place the attribute after the declaration, just before the semicolon.
493 * |[<!-- language="C" -->
494 * gchar g_ascii_tolower (gchar c) G_GNUC_CONST;
497 * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute) for more details.
499 * A function that has pointer arguments and examines the data pointed to
500 * must not be declared `const`. Likewise, a function that calls a non-`const`
501 * function usually must not be `const`. It doesn't make sense for a `const`
502 * function to return `void`.
508 * Expands to the GNU C `unused` function attribute if the compiler is gcc.
509 * It is used for declaring functions and arguments which may never be used.
510 * It avoids possible compiler warnings.
512 * For functions, place the attribute after the declaration, just before the
513 * semicolon. For arguments, place the attribute at the beginning of the
514 * argument declaration.
516 * |[<!-- language="C" -->
517 * void my_unused_function (G_GNUC_UNUSED gint unused_argument,
518 * gint other_argument) G_GNUC_UNUSED;
521 * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute) for more details.
525 * G_GNUC_NO_INSTRUMENT:
527 * Expands to the GNU C `no_instrument_function` function attribute if the
528 * compiler is gcc. Functions with this attribute will not be instrumented
529 * for profiling, when the compiler is called with the
530 * `-finstrument-functions` option.
532 * Place the attribute after the declaration, just before the semicolon.
534 * |[<!-- language="C" -->
535 * int do_uninteresting_things (void) G_GNUC_NO_INSTRUMENT;
538 * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-no_005finstrument_005ffunction-function-attribute) for more details.
541 #if g_macro__has_attribute(__format__)
543 #if !defined (__clang__) && G_GNUC_CHECK_VERSION (4, 4)
544 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
545 __attribute__((__format__ (gnu_printf, format_idx, arg_idx)))
546 #define G_GNUC_SCANF( format_idx, arg_idx ) \
547 __attribute__((__format__ (gnu_scanf, format_idx, arg_idx)))
548 #define G_GNUC_STRFTIME( format_idx ) \
549 __attribute__((__format__ (gnu_strftime, format_idx, 0))) \
550 GLIB_AVAILABLE_MACRO_IN_2_60
552 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
553 __attribute__((__format__ (__printf__, format_idx, arg_idx)))
554 #define G_GNUC_SCANF( format_idx, arg_idx ) \
555 __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
556 #define G_GNUC_STRFTIME( format_idx ) \
557 __attribute__((__format__ (__strftime__, format_idx, 0))) \
558 GLIB_AVAILABLE_MACRO_IN_2_60
563 #define G_GNUC_PRINTF( format_idx, arg_idx )
564 #define G_GNUC_SCANF( format_idx, arg_idx )
565 #define G_GNUC_STRFTIME( format_idx ) \
566 GLIB_AVAILABLE_MACRO_IN_2_60
570 #if g_macro__has_attribute(__format_arg__)
571 #define G_GNUC_FORMAT(arg_idx) \
572 __attribute__ ((__format_arg__ (arg_idx)))
574 #define G_GNUC_FORMAT( arg_idx )
577 #if g_macro__has_attribute(__noreturn__)
578 #define G_GNUC_NORETURN \
579 __attribute__ ((__noreturn__))
581 /* NOTE: MSVC has __declspec(noreturn) but unlike GCC __attribute__,
582 * __declspec can only be placed at the start of the function prototype
583 * and not at the end, so we can't use it without breaking API.
585 #define G_GNUC_NORETURN
588 #if g_macro__has_attribute(__const__)
589 #define G_GNUC_CONST \
590 __attribute__ ((__const__))
595 #if g_macro__has_attribute(__unused__)
596 #define G_GNUC_UNUSED \
597 __attribute__ ((__unused__))
599 #define G_GNUC_UNUSED
602 #if g_macro__has_attribute(__no_instrument_function__)
603 #define G_GNUC_NO_INSTRUMENT \
604 __attribute__ ((__no_instrument_function__))
606 #define G_GNUC_NO_INSTRUMENT
610 * G_GNUC_FALLTHROUGH:
612 * Expands to the GNU C `fallthrough` statement attribute if the compiler supports it.
613 * This allows declaring case statement to explicitly fall through in switch
614 * statements. To enable this feature, use `-Wimplicit-fallthrough` during
617 * Put the attribute right before the case statement you want to fall through
620 * |[<!-- language="C" -->
624 * g_message ("it's 1");
625 * G_GNUC_FALLTHROUGH;
627 * g_message ("it's either 1 or 2");
633 * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html#index-fallthrough-statement-attribute) for more details.
637 #if g_macro__has_attribute(fallthrough)
638 #define G_GNUC_FALLTHROUGH __attribute__((fallthrough)) \
639 GLIB_AVAILABLE_MACRO_IN_2_60
641 #define G_GNUC_FALLTHROUGH \
642 GLIB_AVAILABLE_MACRO_IN_2_60
648 * Expands to the GNU C `deprecated` attribute if the compiler is gcc.
649 * It can be used to mark `typedef`s, variables and functions as deprecated.
650 * When called with the `-Wdeprecated-declarations` option,
651 * gcc will generate warnings when deprecated interfaces are used.
653 * Place the attribute after the declaration, just before the semicolon.
655 * |[<!-- language="C" -->
656 * int my_mistake (void) G_GNUC_DEPRECATED;
659 * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-deprecated-function-attribute) for more details.
663 #if g_macro__has_attribute(__deprecated__)
664 #define G_GNUC_DEPRECATED __attribute__((__deprecated__))
666 #define G_GNUC_DEPRECATED
667 #endif /* __GNUC__ */
670 * G_GNUC_DEPRECATED_FOR:
671 * @f: the intended replacement for the deprecated symbol,
672 * such as the name of a function
674 * Like %G_GNUC_DEPRECATED, but names the intended replacement for the
675 * deprecated symbol if the version of gcc in use is new enough to support
676 * custom deprecation messages.
678 * Place the attribute after the declaration, just before the semicolon.
680 * |[<!-- language="C" -->
681 * int my_mistake (void) G_GNUC_DEPRECATED_FOR(my_replacement);
684 * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-deprecated-function-attribute) for more details.
686 * Note that if @f is a macro, it will be expanded in the warning message.
687 * You can enclose it in quotes to prevent this. (The quotes will show up
688 * in the warning, but it's better than showing the macro expansion.)
692 #if G_GNUC_CHECK_VERSION(4, 5) || defined(__clang__)
693 #define G_GNUC_DEPRECATED_FOR(f) \
694 __attribute__((deprecated("Use " #f " instead"))) \
695 GLIB_AVAILABLE_MACRO_IN_2_26
697 #define G_GNUC_DEPRECATED_FOR(f) G_GNUC_DEPRECATED \
698 GLIB_AVAILABLE_MACRO_IN_2_26
699 #endif /* __GNUC__ */
702 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
703 _Pragma ("warning (push)") \
704 _Pragma ("warning (disable:1478)")
705 #define G_GNUC_END_IGNORE_DEPRECATIONS \
706 _Pragma ("warning (pop)")
707 #elif G_GNUC_CHECK_VERSION(4, 6)
708 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
709 _Pragma ("GCC diagnostic push") \
710 _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
711 #define G_GNUC_END_IGNORE_DEPRECATIONS \
712 _Pragma ("GCC diagnostic pop")
713 #elif defined (_MSC_VER) && (_MSC_VER >= 1500) && !defined (__clang__)
714 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
715 __pragma (warning (push)) \
716 __pragma (warning (disable : 4996))
717 #define G_GNUC_END_IGNORE_DEPRECATIONS \
718 __pragma (warning (pop))
719 #elif defined (__clang__)
720 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
721 _Pragma("clang diagnostic push") \
722 _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
723 #define G_GNUC_END_IGNORE_DEPRECATIONS \
724 _Pragma("clang diagnostic pop")
726 #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS
727 #define G_GNUC_END_IGNORE_DEPRECATIONS
728 #define GLIB_CANNOT_IGNORE_DEPRECATIONS
734 * Expands to the GNU C `may_alias` type attribute if the compiler is gcc.
735 * Types with this attribute will not be subjected to type-based alias
736 * analysis, but are assumed to alias with any other type, just like `char`.
738 * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-may_005falias-type-attribute) for details.
742 #if g_macro__has_attribute(may_alias)
743 #define G_GNUC_MAY_ALIAS __attribute__((may_alias))
745 #define G_GNUC_MAY_ALIAS
749 * G_GNUC_WARN_UNUSED_RESULT:
751 * Expands to the GNU C `warn_unused_result` function attribute if the compiler
752 * is gcc. This function attribute makes the compiler emit a warning if the
753 * result of a function call is ignored.
755 * Place the attribute after the declaration, just before the semicolon.
757 * |[<!-- language="C" -->
758 * GList *g_list_append (GList *list,
759 * gpointer data) G_GNUC_WARN_UNUSED_RESULT;
762 * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-warn_005funused_005fresult-function-attribute) for more details.
766 #if g_macro__has_attribute(warn_unused_result)
767 #define G_GNUC_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
769 #define G_GNUC_WARN_UNUSED_RESULT
770 #endif /* __GNUC__ */
775 * Expands to "" on all modern compilers, and to __FUNCTION__ on gcc
776 * version 2.x. Don't use it.
778 * Deprecated: 2.16: Use G_STRFUNC() instead
782 * G_GNUC_PRETTY_FUNCTION:
784 * Expands to "" on all modern compilers, and to __PRETTY_FUNCTION__
785 * on gcc version 2.x. Don't use it.
787 * Deprecated: 2.16: Use G_STRFUNC() instead
790 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
791 * macros, so we can refer to them as strings unconditionally.
792 * usage not-recommended since gcc-3.0
794 * Mark them as deprecated since 2.26, since that’s when version macros were
797 #if defined (__GNUC__) && (__GNUC__ < 3)
798 #define G_GNUC_FUNCTION __FUNCTION__ GLIB_DEPRECATED_MACRO_IN_2_26_FOR(G_STRFUNC)
799 #define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__ GLIB_DEPRECATED_MACRO_IN_2_26_FOR(G_STRFUNC)
800 #else /* !__GNUC__ */
801 #define G_GNUC_FUNCTION "" GLIB_DEPRECATED_MACRO_IN_2_26_FOR(G_STRFUNC)
802 #define G_GNUC_PRETTY_FUNCTION "" GLIB_DEPRECATED_MACRO_IN_2_26_FOR(G_STRFUNC)
803 #endif /* !__GNUC__ */
805 #if g_macro__has_feature(attribute_analyzer_noreturn) && defined(__clang_analyzer__)
806 #define G_ANALYZER_ANALYZING 1
807 #define G_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
808 #elif defined(__COVERITY__)
809 #define G_ANALYZER_ANALYZING 1
810 #define G_ANALYZER_NORETURN __attribute__((noreturn))
812 #define G_ANALYZER_ANALYZING 0
813 #define G_ANALYZER_NORETURN
816 #define G_STRINGIFY(macro_or_string) G_STRINGIFY_ARG (macro_or_string)
817 #define G_STRINGIFY_ARG(contents) #contents
819 #ifndef __GI_SCANNER__ /* The static assert macro really confuses the introspection parser */
820 #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
821 #define G_PASTE(identifier1,identifier2) G_PASTE_ARGS (identifier1, identifier2)
822 #if !defined(__cplusplus) && defined(__STDC_VERSION__) && \
823 (__STDC_VERSION__ >= 201112L || g_macro__has_feature(c_static_assert) || g_macro__has_extension(c_static_assert))
824 #define G_STATIC_ASSERT(expr) _Static_assert (expr, "Expression evaluates to false")
825 #elif (defined(__cplusplus) && __cplusplus >= 201103L) || \
826 (defined(__cplusplus) && defined (_MSC_VER) && (_MSC_VER >= 1600)) || \
827 (defined (_MSC_VER) && (_MSC_VER >= 1800))
828 #define G_STATIC_ASSERT(expr) static_assert (expr, "Expression evaluates to false")
831 #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __COUNTER__)[(expr) ? 1 : -1] G_GNUC_UNUSED
833 #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __LINE__)[(expr) ? 1 : -1] G_GNUC_UNUSED
835 #endif /* __STDC_VERSION__ */
836 #define G_STATIC_ASSERT_EXPR(expr) ((void) sizeof (char[(expr) ? 1 : -1]))
837 #endif /* !__GI_SCANNER__ */
839 /* Provide a string identifying the current code position */
840 #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)
841 #define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
843 #define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__)
846 /* Provide a string identifying the current function, non-concatenatable */
847 #if defined (__GNUC__) && defined (__cplusplus)
848 #define G_STRFUNC ((const char*) (__PRETTY_FUNCTION__))
849 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
850 #define G_STRFUNC ((const char*) (__func__))
851 #elif defined (__GNUC__) || (defined(_MSC_VER) && (_MSC_VER > 1300))
852 #define G_STRFUNC ((const char*) (__FUNCTION__))
854 #define G_STRFUNC ((const char*) ("???"))
857 /* Guard C code in headers, while including them from C++ */
859 #define G_BEGIN_DECLS extern "C" {
860 #define G_END_DECLS }
862 #define G_BEGIN_DECLS
866 /* Provide definitions for some commonly used macros.
867 * Some of them are only provided if they haven't already
868 * been defined. It is assumed that if they are already
869 * defined then the current definition is correct.
874 # else /* !__cplusplus */
875 # define NULL ((void*) 0)
876 # endif /* !__cplusplus */
884 #define TRUE (!FALSE)
888 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
891 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
894 #define ABS(a) (((a) < 0) ? -(a) : (a))
897 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
899 #define G_APPROX_VALUE(a, b, epsilon) \
900 (((a) > (b) ? (a) - (b) : (b) - (a)) < (epsilon))
902 /* Count the number of elements in an array. The array must be defined
903 * as such; using this with a dynamically allocated array will give
906 #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
908 /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT
910 #define GPOINTER_TO_SIZE(p) ((gsize) (p))
911 #define GSIZE_TO_POINTER(s) ((gpointer) (gsize) (s))
913 /* Provide convenience macros for handling structure
914 * fields through their offsets.
917 #if G_GNUC_CHECK_VERSION(4, 0) || defined(_MSC_VER)
918 #define G_STRUCT_OFFSET(struct_type, member) \
919 ((glong) offsetof (struct_type, member))
921 #define G_STRUCT_OFFSET(struct_type, member) \
922 ((glong) ((guint8*) &((struct_type*) 0)->member))
925 #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
926 ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
927 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
928 (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
930 /* Provide simple macro statement wrappers:
931 * G_STMT_START { statements; } G_STMT_END;
932 * This can be used as a single statement, like:
933 * if (x) G_STMT_START { ... } G_STMT_END; else ...
934 * This intentionally does not use compiler extensions like GCC's '({...})' to
935 * avoid portability issue or side effects when compiled with different compilers.
936 * MSVC complains about "while(0)": C4127: "Conditional expression is constant",
937 * so we use __pragma to avoid the warning since the use here is intentional.
939 #if !(defined (G_STMT_START) && defined (G_STMT_END))
940 #define G_STMT_START do
941 #if defined (_MSC_VER) && (_MSC_VER >= 1500)
943 __pragma(warning(push)) \
944 __pragma(warning(disable:4127)) \
946 __pragma(warning(pop))
948 #define G_STMT_END while (0)
952 /* Provide G_ALIGNOF alignment macro.
954 * Note we cannot use the gcc __alignof__ operator here, as that returns the
955 * preferred alignment rather than the minimal alignment. See
956 * https://gitlab.gnome.org/GNOME/glib/merge_requests/538/diffs#note_390790.
963 * Return the minimal alignment required by the platform ABI for values of the given
964 * type. The address of a variable or struct member of the given type must always be
965 * a multiple of this alignment. For example, most platforms require int variables
966 * to be aligned at a 4-byte boundary, so `G_ALIGNOF (int)` is 4 on most platforms.
968 * Note this is not necessarily the same as the value returned by GCC’s
969 * `__alignof__` operator, which returns the preferred alignment for a type.
970 * The preferred alignment may be a stricter alignment than the minimal
975 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__cplusplus)
976 #define G_ALIGNOF(type) _Alignof (type) \
977 GLIB_AVAILABLE_MACRO_IN_2_60
979 #define G_ALIGNOF(type) (G_STRUCT_OFFSET (struct { char a; type b; }, b)) \
980 GLIB_AVAILABLE_MACRO_IN_2_60
986 * If %G_DISABLE_CONST_RETURNS is defined, this macro expands
987 * to nothing. By default, the macro expands to const. The macro
988 * can be used in place of const for functions that return a value
989 * that should not be modified. The purpose of this macro is to allow
990 * us to turn on const for returned constant strings by default, while
991 * allowing programmers who find that annoying to turn it off. This macro
992 * should only be used for return values and for "out" parameters, it
993 * doesn't make sense for "in" parameters.
995 * Deprecated: 2.30: API providers should replace all existing uses with
996 * const and API consumers should adjust their code accordingly
998 #ifdef G_DISABLE_CONST_RETURNS
999 #define G_CONST_RETURN GLIB_DEPRECATED_MACRO_IN_2_30_FOR(const)
1001 #define G_CONST_RETURN const GLIB_DEPRECATED_MACRO_IN_2_30_FOR(const)
1007 * Expands to the GNU C or MSVC `noreturn` function attribute depending on
1008 * the compiler. It is used for declaring functions which never return.
1009 * Enables optimization of the function, and avoids possible compiler warnings.
1011 * Note that %G_NORETURN supersedes the previous %G_GNUC_NORETURN macro, which
1012 * will eventually be deprecated. %G_NORETURN supports more platforms.
1014 * Place the attribute before the function declaration as follows:
1016 * |[<!-- language="C" -->
1017 * G_NORETURN void g_abort (void);
1022 /* Note: We can’t annotate this with GLIB_AVAILABLE_MACRO_IN_2_68 because it’s
1023 * used within the GLib headers in function declarations which are always
1024 * evaluated when a header is included. This results in warnings in third party
1025 * code which includes glib.h, even if the third party code doesn’t use the new
1027 #if g_macro__has_attribute(__noreturn__)
1028 /* For compatibility with G_NORETURN_FUNCPTR on clang, use
1029 __attribute__((__noreturn__)), not _Noreturn. */
1030 # define G_NORETURN __attribute__ ((__noreturn__))
1031 #elif defined (_MSC_VER) && (1200 <= _MSC_VER)
1032 /* Use MSVC specific syntax. */
1033 # define G_NORETURN __declspec (noreturn)
1034 /* Use ISO C++11 syntax when the compiler supports it. */
1035 #elif defined (__cplusplus) && __cplusplus >= 201103
1036 # define G_NORETURN [[noreturn]]
1037 /* Use ISO C11 syntax when the compiler supports it. */
1038 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112
1039 # define G_NORETURN _Noreturn
1041 # define G_NORETURN /* empty */
1045 * G_NORETURN_FUNCPTR:
1047 * Expands to the GNU C or MSVC `noreturn` function attribute depending on
1048 * the compiler. It is used for declaring function pointers which never return.
1049 * Enables optimization of the function, and avoids possible compiler warnings.
1051 * Place the attribute before the function declaration as follows:
1053 * |[<!-- language="C" -->
1054 * G_NORETURN_FUNCPTR void (*funcptr) (void);
1057 * Note that if the function is not a function pointer, you can simply use
1058 * the %G_NORETURN macro as follows:
1060 * |[<!-- language="C" -->
1061 * G_NORETURN void g_abort (void);
1066 #if g_macro__has_attribute(__noreturn__)
1067 # define G_NORETURN_FUNCPTR __attribute__ ((__noreturn__)) \
1068 GLIB_AVAILABLE_MACRO_IN_2_68
1070 # define G_NORETURN_FUNCPTR /* empty */ \
1071 GLIB_AVAILABLE_MACRO_IN_2_68
1077 * Expands to the GNU C `always_inline` or MSVC `__forceinline` function
1078 * attribute depending on the compiler. It is used for declaring functions
1079 * as always inlined, ignoring the compiler optimization levels.
1081 * The attribute may be placed before the declaration or definition,
1082 * right before the `static` keyword.
1084 * |[<!-- language="C" -->
1087 * do_inline_this (void)
1094 * [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-always_005finline-function-attribute)
1096 * [MSVC documentation](https://docs.microsoft.com/en-us/visualstudio/misc/inline-inline-forceinline)
1100 /* Note: We can’t annotate this with GLIB_AVAILABLE_MACRO_IN_2_74 because it’s
1101 * used within the GLib headers in function declarations which are always
1102 * evaluated when a header is included. This results in warnings in third party
1103 * code which includes glib.h, even if the third party code doesn’t use the new
1105 #if g_macro__has_attribute(__always_inline__)
1106 # if defined (__cplusplus) && __cplusplus >= 201103L
1107 /* Use ISO C++11 syntax when the compiler supports it. */
1108 # define G_ALWAYS_INLINE [[gnu::always_inline]]
1110 # define G_ALWAYS_INLINE __attribute__ ((__always_inline__))
1112 #elif defined (_MSC_VER)
1113 /* Use MSVC specific syntax. */
1114 # define G_ALWAYS_INLINE __forceinline
1116 # define G_ALWAYS_INLINE /* empty */
1122 * Expands to the GNU C or MSVC `noinline` function attribute
1123 * depending on the compiler. It is used for declaring functions
1124 * preventing from being considered for inlining.
1126 * Note that %G_NO_INLINE supersedes the previous %G_GNUC_NO_INLINE
1127 * macro, which will eventually be deprecated.
1128 * %G_NO_INLINE supports more platforms.
1130 * The attribute may be placed before the declaration or definition,
1131 * right before the `static` keyword.
1133 * |[<!-- language="C" -->
1136 * do_not_inline_this (void)
1144 /* Note: We can’t annotate this with GLIB_AVAILABLE_MACRO_IN_2_74 because it’s
1145 * used within the GLib headers in function declarations which are always
1146 * evaluated when a header is included. This results in warnings in third party
1147 * code which includes glib.h, even if the third party code doesn’t use the new
1149 #if g_macro__has_attribute(__noinline__)
1150 # if defined (__cplusplus) && __cplusplus >= 201103L
1151 /* Use ISO C++11 syntax when the compiler supports it. */
1152 # define G_NO_INLINE [[gnu::noinline]]
1154 # define G_NO_INLINE __attribute__ ((__noinline__))
1156 #elif defined (_MSC_VER) && (1200 <= _MSC_VER)
1157 /* Use MSVC specific syntax. */
1158 # if defined (__cplusplus) && __cplusplus >= 201103L
1159 /* Use ISO C++11 syntax when the compiler supports it. */
1160 # define G_NO_INLINE [[msvc::noinline]]
1162 # define G_NO_INLINE __declspec (noinline)
1165 # define G_NO_INLINE /* empty */
1169 * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to
1170 * the compiler about the expected result of an expression. Some compilers
1171 * can use this information for optimizations.
1173 * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
1174 * putting assignments in g_return_if_fail ().
1176 #if G_GNUC_CHECK_VERSION(2, 0) && defined(__OPTIMIZE__)
1177 #define _G_BOOLEAN_EXPR(expr) \
1178 G_GNUC_EXTENSION ({ \
1179 int _g_boolean_var_; \
1181 _g_boolean_var_ = 1; \
1183 _g_boolean_var_ = 0; \
1186 #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))
1187 #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))
1189 #define G_LIKELY(expr) (expr)
1190 #define G_UNLIKELY(expr) (expr)
1193 /* GLIB_CANNOT_IGNORE_DEPRECATIONS is defined above for compilers that do not
1194 * have a way to temporarily suppress deprecation warnings. In these cases,
1195 * suppress the deprecated attribute altogether (otherwise a simple #include
1196 * <glib.h> will emit a barrage of warnings).
1198 #if defined(GLIB_CANNOT_IGNORE_DEPRECATIONS)
1199 #define G_DEPRECATED
1200 #elif G_GNUC_CHECK_VERSION(3, 1) || defined(__clang__)
1201 #define G_DEPRECATED __attribute__((__deprecated__))
1202 #elif defined(_MSC_VER) && (_MSC_VER >= 1300)
1203 #define G_DEPRECATED __declspec(deprecated)
1205 #define G_DEPRECATED
1208 #if defined(GLIB_CANNOT_IGNORE_DEPRECATIONS)
1209 #define G_DEPRECATED_FOR(f) G_DEPRECATED
1210 #elif G_GNUC_CHECK_VERSION(4, 5) || defined(__clang__)
1211 #define G_DEPRECATED_FOR(f) __attribute__((__deprecated__("Use '" #f "' instead")))
1212 #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320)
1213 #define G_DEPRECATED_FOR(f) __declspec(deprecated("is deprecated. Use '" #f "' instead"))
1215 #define G_DEPRECATED_FOR(f) G_DEPRECATED
1218 #if G_GNUC_CHECK_VERSION(4, 5) || defined(__clang__)
1219 #define G_UNAVAILABLE(maj,min) __attribute__((deprecated("Not available before " #maj "." #min)))
1220 #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320)
1221 #define G_UNAVAILABLE(maj,min) __declspec(deprecated("is not available before " #maj "." #min))
1223 #define G_UNAVAILABLE(maj,min) G_DEPRECATED
1226 #ifndef _GLIB_EXTERN
1227 #define _GLIB_EXTERN extern
1230 /* These macros are used to mark deprecated symbols in GLib headers,
1231 * and thus have to be exposed in installed headers. But please
1232 * do *not* use them in other projects. Instead, use G_DEPRECATED
1233 * or define your own wrappers around it.
1236 #ifdef GLIB_DISABLE_DEPRECATION_WARNINGS
1237 #define GLIB_DEPRECATED _GLIB_EXTERN
1238 #define GLIB_DEPRECATED_FOR(f) _GLIB_EXTERN
1239 #define GLIB_UNAVAILABLE(maj,min) _GLIB_EXTERN
1240 #define GLIB_UNAVAILABLE_STATIC_INLINE(maj,min)
1242 #define GLIB_DEPRECATED G_DEPRECATED _GLIB_EXTERN
1243 #define GLIB_DEPRECATED_FOR(f) G_DEPRECATED_FOR(f) _GLIB_EXTERN
1244 #define GLIB_UNAVAILABLE(maj,min) G_UNAVAILABLE(maj,min) _GLIB_EXTERN
1245 #define GLIB_UNAVAILABLE_STATIC_INLINE(maj,min) G_UNAVAILABLE(maj,min)
1248 #if !defined(GLIB_DISABLE_DEPRECATION_WARNINGS) && \
1249 (G_GNUC_CHECK_VERSION(4, 6) || \
1250 __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 4))
1251 #define _GLIB_GNUC_DO_PRAGMA(x) _Pragma(G_STRINGIFY (x))
1252 #define GLIB_DEPRECATED_MACRO _GLIB_GNUC_DO_PRAGMA(GCC warning "Deprecated pre-processor symbol")
1253 #define GLIB_DEPRECATED_MACRO_FOR(f) \
1254 _GLIB_GNUC_DO_PRAGMA(GCC warning G_STRINGIFY (Deprecated pre-processor symbol: replace with #f))
1255 #define GLIB_UNAVAILABLE_MACRO(maj,min) \
1256 _GLIB_GNUC_DO_PRAGMA(GCC warning G_STRINGIFY (Not available before maj.min))
1258 #define GLIB_DEPRECATED_MACRO
1259 #define GLIB_DEPRECATED_MACRO_FOR(f)
1260 #define GLIB_UNAVAILABLE_MACRO(maj,min)
1263 #if !defined(GLIB_DISABLE_DEPRECATION_WARNINGS) && \
1264 (G_GNUC_CHECK_VERSION(6, 1) || \
1265 (defined (__clang_major__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 0))))
1266 #define GLIB_DEPRECATED_ENUMERATOR G_DEPRECATED
1267 #define GLIB_DEPRECATED_ENUMERATOR_FOR(f) G_DEPRECATED_FOR(f)
1268 #define GLIB_UNAVAILABLE_ENUMERATOR(maj,min) G_UNAVAILABLE(maj,min)
1270 #define GLIB_DEPRECATED_ENUMERATOR
1271 #define GLIB_DEPRECATED_ENUMERATOR_FOR(f)
1272 #define GLIB_UNAVAILABLE_ENUMERATOR(maj,min)
1275 #if !defined(GLIB_DISABLE_DEPRECATION_WARNINGS) && \
1276 (G_GNUC_CHECK_VERSION(3, 1) || \
1277 (defined (__clang_major__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 0))))
1278 #define GLIB_DEPRECATED_TYPE G_DEPRECATED
1279 #define GLIB_DEPRECATED_TYPE_FOR(f) G_DEPRECATED_FOR(f)
1280 #define GLIB_UNAVAILABLE_TYPE(maj,min) G_UNAVAILABLE(maj,min)
1282 #define GLIB_DEPRECATED_TYPE
1283 #define GLIB_DEPRECATED_TYPE_FOR(f)
1284 #define GLIB_UNAVAILABLE_TYPE(maj,min)
1287 #ifndef __GI_SCANNER__
1289 #if g_macro__has_attribute(cleanup)
1291 /* these macros are private */
1292 #define _GLIB_AUTOPTR_FUNC_NAME(TypeName) glib_autoptr_cleanup_##TypeName
1293 #define _GLIB_AUTOPTR_CLEAR_FUNC_NAME(TypeName) glib_autoptr_clear_##TypeName
1294 #define _GLIB_AUTOPTR_TYPENAME(TypeName) TypeName##_autoptr
1295 #define _GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName) glib_listautoptr_cleanup_##TypeName
1296 #define _GLIB_AUTOPTR_LIST_TYPENAME(TypeName) TypeName##_listautoptr
1297 #define _GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName) glib_slistautoptr_cleanup_##TypeName
1298 #define _GLIB_AUTOPTR_SLIST_TYPENAME(TypeName) TypeName##_slistautoptr
1299 #define _GLIB_AUTOPTR_QUEUE_FUNC_NAME(TypeName) glib_queueautoptr_cleanup_##TypeName
1300 #define _GLIB_AUTOPTR_QUEUE_TYPENAME(TypeName) TypeName##_queueautoptr
1301 #define _GLIB_AUTO_FUNC_NAME(TypeName) glib_auto_cleanup_##TypeName
1302 #define _GLIB_CLEANUP(func) __attribute__((cleanup(func)))
1303 #define _GLIB_DEFINE_AUTOPTR_CLEANUP_FUNCS(TypeName, ParentName, cleanup) \
1304 typedef TypeName *_GLIB_AUTOPTR_TYPENAME(TypeName); \
1305 typedef GList *_GLIB_AUTOPTR_LIST_TYPENAME(TypeName); \
1306 typedef GSList *_GLIB_AUTOPTR_SLIST_TYPENAME(TypeName); \
1307 typedef GQueue *_GLIB_AUTOPTR_QUEUE_TYPENAME(TypeName); \
1308 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
1309 static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_CLEAR_FUNC_NAME(TypeName) (TypeName *_ptr) \
1310 { if (_ptr) (cleanup) ((ParentName *) _ptr); } \
1311 static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_FUNC_NAME(TypeName) (TypeName **_ptr) \
1312 { _GLIB_AUTOPTR_CLEAR_FUNC_NAME(TypeName) (*_ptr); } \
1313 static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName) (GList **_l) \
1314 { g_list_free_full (*_l, (GDestroyNotify) (void(*)(void)) cleanup); } \
1315 static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName) (GSList **_l) \
1316 { g_slist_free_full (*_l, (GDestroyNotify) (void(*)(void)) cleanup); } \
1317 static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_QUEUE_FUNC_NAME(TypeName) (GQueue **_q) \
1318 { if (*_q) g_queue_free_full (*_q, (GDestroyNotify) (void(*)(void)) cleanup); } \
1319 G_GNUC_END_IGNORE_DEPRECATIONS
1320 #define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName) \
1321 _GLIB_DEFINE_AUTOPTR_CLEANUP_FUNCS(ModuleObjName, ParentName, _GLIB_AUTOPTR_CLEAR_FUNC_NAME(ParentName))
1324 /* these macros are API */
1325 #define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) \
1326 _GLIB_DEFINE_AUTOPTR_CLEANUP_FUNCS(TypeName, TypeName, func)
1327 #define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func) \
1328 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
1329 static G_GNUC_UNUSED inline void _GLIB_AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { (func) (_ptr); } \
1330 G_GNUC_END_IGNORE_DEPRECATIONS
1331 #define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none) \
1332 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
1333 static G_GNUC_UNUSED inline void _GLIB_AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { if (*_ptr != none) (func) (*_ptr); } \
1334 G_GNUC_END_IGNORE_DEPRECATIONS
1335 #define g_autoptr(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_TYPENAME(TypeName)
1336 #define g_autolist(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_LIST_TYPENAME(TypeName)
1337 #define g_autoslist(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_SLIST_TYPENAME(TypeName)
1338 #define g_autoqueue(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_QUEUE_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_QUEUE_TYPENAME(TypeName)
1339 #define g_auto(TypeName) _GLIB_CLEANUP(_GLIB_AUTO_FUNC_NAME(TypeName)) TypeName
1340 #define g_autofree _GLIB_CLEANUP(g_autoptr_cleanup_generic_gfree)
1342 #else /* not GNU C */
1343 /* this (dummy) macro is private */
1344 #define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName)
1346 /* these (dummy) macros are API */
1347 #define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func)
1348 #define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func)
1349 #define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none)
1351 /* no declaration of g_auto() or g_autoptr() here */
1352 #endif /* __GNUC__ */
1356 #define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName)
1358 #define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func)
1359 #define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func)
1360 #define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none)
1362 #endif /* __GI_SCANNER__ */
1366 * @struct_type: a structure type, e.g. #GOutputVector
1367 * @member: a field in the structure, e.g. `size`
1369 * Returns the size of @member in the struct definition without having a
1370 * declared instance of @struct_type.
1372 * Returns: the size of @member in bytes.
1376 #define G_SIZEOF_MEMBER(struct_type, member) \
1377 GLIB_AVAILABLE_MACRO_IN_2_64 \
1378 sizeof (((struct_type *) 0)->member)
1380 #endif /* __G_MACROS_H__ */