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.1 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, see <http://www.gnu.org/licenses/>. |
16 | */ |
17 | |
18 | /* |
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/. |
23 | */ |
24 | |
25 | /* This file must not include any other glib header file and must thus |
26 | * not refer to variables from glibconfig.h |
27 | */ |
28 | |
29 | #ifndef __G_MACROS_H__ |
30 | #define __G_MACROS_H__ |
31 | |
32 | #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION) |
33 | #error "Only <glib.h> can be included directly." |
34 | #endif |
35 | |
36 | /* We include stddef.h to get the system's definition of NULL |
37 | */ |
38 | #include <stddef.h> |
39 | |
40 | /* |
41 | * Note: Clang (but not clang-cl) defines __GNUC__ and __GNUC_MINOR__. |
42 | * Both Clang 11.1 on current Arch Linux and Apple's Clang 12.0 define |
43 | * __GNUC__ = 4 and __GNUC_MINOR__ = 2. So G_GNUC_CHECK_VERSION(4, 2) on |
44 | * current Clang will be 1. |
45 | */ |
46 | #ifdef __GNUC__ |
47 | #define G_GNUC_CHECK_VERSION(major, minor) \ |
48 | ((__GNUC__ > (major)) || \ |
49 | ((__GNUC__ == (major)) && \ |
50 | (__GNUC_MINOR__ >= (minor)))) |
51 | #else |
52 | #define G_GNUC_CHECK_VERSION(major, minor) 0 |
53 | #endif |
54 | |
55 | /* Here we provide G_GNUC_EXTENSION as an alias for __extension__, |
56 | * where this is valid. This allows for warningless compilation of |
57 | * "long long" types even in the presence of '-ansi -pedantic'. |
58 | */ |
59 | #if G_GNUC_CHECK_VERSION(2, 8) |
60 | #define G_GNUC_EXTENSION __extension__ |
61 | #else |
62 | #define G_GNUC_EXTENSION |
63 | #endif |
64 | |
65 | /* Every compiler that we target supports inlining, but some of them may |
66 | * complain about it if we don't say "__inline". If we have C99, or if |
67 | * we are using C++, then we can use "inline" directly. Unfortunately |
68 | * Visual Studio does not support __STDC_VERSION__, so we need to check |
69 | * whether we are on Visual Studio 2013 or earlier to see that we need to |
70 | * say "__inline" in C mode. |
71 | * Otherwise, we say "__inline" to avoid the warning. |
72 | */ |
73 | #define G_CAN_INLINE |
74 | #ifndef __cplusplus |
75 | # ifdef _MSC_VER |
76 | # if (_MSC_VER < 1900) |
77 | # define G_INLINE_DEFINE_NEEDED |
78 | # endif |
79 | # elif !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199900) |
80 | # define G_INLINE_DEFINE_NEEDED |
81 | # endif |
82 | #endif |
83 | |
84 | #ifdef G_INLINE_DEFINE_NEEDED |
85 | # undef inline |
86 | # define inline __inline |
87 | #endif |
88 | |
89 | #undef G_INLINE_DEFINE_NEEDED |
90 | |
91 | /** |
92 | * G_INLINE_FUNC: |
93 | * |
94 | * This macro used to be used to conditionally define inline functions |
95 | * in a compatible way before this feature was supported in all |
96 | * compilers. These days, GLib requires inlining support from the |
97 | * compiler, so your GLib-using programs can safely assume that the |
98 | * "inline" keyword works properly. |
99 | * |
100 | * Never use this macro anymore. Just say "static inline". |
101 | * |
102 | * Deprecated: 2.48: Use "static inline" instead |
103 | */ |
104 | |
105 | /* For historical reasons we need to continue to support those who |
106 | * define G_IMPLEMENT_INLINES to mean "don't implement this here". |
107 | */ |
108 | #ifdef G_IMPLEMENT_INLINES |
109 | # define G_INLINE_FUNC extern GLIB_DEPRECATED_MACRO_IN_2_48_FOR(static inline) |
110 | # undef G_CAN_INLINE |
111 | #else |
112 | # define G_INLINE_FUNC static inline GLIB_DEPRECATED_MACRO_IN_2_48_FOR(static inline) |
113 | #endif /* G_IMPLEMENT_INLINES */ |
114 | |
115 | /* |
116 | * Attribute support detection. Works on clang and GCC >= 5 |
117 | * https://clang.llvm.org/docs/LanguageExtensions.html#has-attribute |
118 | * https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005fattribute.html |
119 | */ |
120 | |
121 | #ifdef __has_attribute |
122 | #define g_macro__has_attribute __has_attribute |
123 | #else |
124 | |
125 | /* |
126 | * Fallback for GCC < 5 and other compilers not supporting __has_attribute. |
127 | */ |
128 | #define g_macro__has_attribute(x) g_macro__has_attribute_##x |
129 | |
130 | #define g_macro__has_attribute___pure__ G_GNUC_CHECK_VERSION (2, 96) |
131 | #define g_macro__has_attribute___malloc__ G_GNUC_CHECK_VERSION (2, 96) |
132 | #define g_macro__has_attribute___noinline__ G_GNUC_CHECK_VERSION (2, 96) |
133 | #define g_macro__has_attribute___sentinel__ G_GNUC_CHECK_VERSION (4, 0) |
134 | #define g_macro__has_attribute___alloc_size__ G_GNUC_CHECK_VERSION (4, 3) |
135 | #define g_macro__has_attribute___format__ G_GNUC_CHECK_VERSION (2, 4) |
136 | #define g_macro__has_attribute___format_arg__ G_GNUC_CHECK_VERSION (2, 4) |
137 | #define g_macro__has_attribute___noreturn__ (G_GNUC_CHECK_VERSION (2, 8) || (0x5110 <= __SUNPRO_C)) |
138 | #define g_macro__has_attribute___const__ G_GNUC_CHECK_VERSION (2, 4) |
139 | #define g_macro__has_attribute___unused__ G_GNUC_CHECK_VERSION (2, 4) |
140 | #define g_macro__has_attribute___no_instrument_function__ G_GNUC_CHECK_VERSION (2, 4) |
141 | #define g_macro__has_attribute_fallthrough G_GNUC_CHECK_VERSION (6, 0) |
142 | #define g_macro__has_attribute___deprecated__ G_GNUC_CHECK_VERSION (3, 1) |
143 | #define g_macro__has_attribute_may_alias G_GNUC_CHECK_VERSION (3, 3) |
144 | #define g_macro__has_attribute_warn_unused_result G_GNUC_CHECK_VERSION (3, 4) |
145 | #define g_macro__has_attribute_cleanup G_GNUC_CHECK_VERSION (3, 3) |
146 | |
147 | #endif |
148 | |
149 | /* Provide macros to feature the GCC function attribute. |
150 | */ |
151 | |
152 | /** |
153 | * G_GNUC_PURE: |
154 | * |
155 | * Expands to the GNU C `pure` function attribute if the compiler is gcc. |
156 | * Declaring a function as `pure` enables better optimization of calls to |
157 | * the function. A `pure` function has no effects except its return value |
158 | * and the return value depends only on the parameters and/or global |
159 | * variables. |
160 | * |
161 | * Place the attribute after the declaration, just before the semicolon. |
162 | * |
163 | * |[<!-- language="C" --> |
164 | * gboolean g_type_check_value (const GValue *value) G_GNUC_PURE; |
165 | * ]| |
166 | * |
167 | * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute) for more details. |
168 | */ |
169 | |
170 | /** |
171 | * G_GNUC_MALLOC: |
172 | * |
173 | * Expands to the |
174 | * [GNU C `malloc` function attribute](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-functions-that-behave-like-malloc) |
175 | * if the compiler is gcc. |
176 | * Declaring a function as `malloc` enables better optimization of the function, |
177 | * but must only be done if the allocation behaviour of the function is fully |
178 | * understood, otherwise miscompilation can result. |
179 | * |
180 | * A function can have the `malloc` attribute if it returns a pointer which is |
181 | * guaranteed to not alias with any other pointer valid when the function |
182 | * returns, and moreover no pointers to valid objects occur in any storage |
183 | * addressed by the returned pointer. |
184 | * |
185 | * In practice, this means that `G_GNUC_MALLOC` can be used with any function |
186 | * which returns unallocated or zeroed-out memory, but not with functions which |
187 | * return initialised structures containing other pointers, or with functions |
188 | * that reallocate memory. This definition changed in GLib 2.58 to match the |
189 | * stricter definition introduced around GCC 5. |
190 | * |
191 | * Place the attribute after the declaration, just before the semicolon. |
192 | * |
193 | * |[<!-- language="C" --> |
194 | * gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1); |
195 | * ]| |
196 | * |
197 | * See the |
198 | * [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-functions-that-behave-like-malloc) |
199 | * for more details. |
200 | * |
201 | * Since: 2.6 |
202 | */ |
203 | |
204 | /** |
205 | * G_GNUC_NO_INLINE: |
206 | * |
207 | * Expands to the GNU C `noinline` function attribute if the compiler is gcc. |
208 | * If the compiler is not gcc, this macro expands to nothing. |
209 | * |
210 | * Declaring a function as `noinline` prevents the function from being |
211 | * considered for inlining. |
212 | * |
213 | * The attribute may be placed before the declaration or definition, |
214 | * right before the `static` keyword. |
215 | * |
216 | * |[<!-- language="C" --> |
217 | * G_GNUC_NO_INLINE |
218 | * static int |
219 | * do_not_inline_this (void) |
220 | * { |
221 | * ... |
222 | * } |
223 | * ]| |
224 | * |
225 | * See the |
226 | * [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noinline-function-attribute) |
227 | * for more details. |
228 | * |
229 | * Since: 2.58 |
230 | */ |
231 | /* Note: We can’t annotate this with GLIB_AVAILABLE_MACRO_IN_2_58 because it’s |
232 | * used within the GLib headers in function declarations which are always |
233 | * evaluated when a header is included. This results in warnings in third party |
234 | * code which includes glib.h, even if the third party code doesn’t use the new |
235 | * macro itself. */ |
236 | |
237 | #if g_macro__has_attribute(__pure__) |
238 | #define G_GNUC_PURE __attribute__((__pure__)) |
239 | #else |
240 | #define G_GNUC_PURE |
241 | #endif |
242 | |
243 | #if g_macro__has_attribute(__malloc__) |
244 | #define G_GNUC_MALLOC __attribute__ ((__malloc__)) |
245 | #else |
246 | #define G_GNUC_MALLOC |
247 | #endif |
248 | |
249 | #if g_macro__has_attribute(__noinline__) |
250 | #define G_GNUC_NO_INLINE __attribute__ ((__noinline__)) |
251 | #else |
252 | #define G_GNUC_NO_INLINE |
253 | #endif |
254 | |
255 | /** |
256 | * G_GNUC_NULL_TERMINATED: |
257 | * |
258 | * Expands to the GNU C `sentinel` function attribute if the compiler is gcc. |
259 | * This function attribute only applies to variadic functions and instructs |
260 | * the compiler to check that the argument list is terminated with an |
261 | * explicit %NULL. |
262 | * |
263 | * Place the attribute after the declaration, just before the semicolon. |
264 | * |
265 | * |[<!-- language="C" --> |
266 | * gchar *g_strconcat (const gchar *string1, |
267 | * ...) G_GNUC_NULL_TERMINATED; |
268 | * ]| |
269 | * |
270 | * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-sentinel-function-attribute) for more details. |
271 | * |
272 | * Since: 2.8 |
273 | */ |
274 | #if g_macro__has_attribute(__sentinel__) |
275 | #define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__)) |
276 | #else |
277 | #define G_GNUC_NULL_TERMINATED |
278 | #endif |
279 | |
280 | /* |
281 | * Clang feature detection: http://clang.llvm.org/docs/LanguageExtensions.html |
282 | * These are not available on GCC, but since the pre-processor doesn't do |
283 | * operator short-circuiting, we can't use it in a statement or we'll get: |
284 | * |
285 | * error: missing binary operator before token "(" |
286 | * |
287 | * So we define it to 0 to satisfy the pre-processor. |
288 | */ |
289 | |
290 | #ifdef __has_feature |
291 | #define g_macro__has_feature __has_feature |
292 | #else |
293 | #define g_macro__has_feature(x) 0 |
294 | #endif |
295 | |
296 | #ifdef __has_builtin |
297 | #define g_macro__has_builtin __has_builtin |
298 | #else |
299 | #define g_macro__has_builtin(x) 0 |
300 | #endif |
301 | |
302 | #ifdef __has_extension |
303 | #define g_macro__has_extension __has_extension |
304 | #else |
305 | #define g_macro__has_extension(x) 0 |
306 | #endif |
307 | |
308 | /** |
309 | * G_GNUC_ALLOC_SIZE: |
310 | * @x: the index of the argument specifying the allocation size |
311 | * |
312 | * Expands to the GNU C `alloc_size` function attribute if the compiler |
313 | * is a new enough gcc. This attribute tells the compiler that the |
314 | * function returns a pointer to memory of a size that is specified |
315 | * by the @xth function parameter. |
316 | * |
317 | * Place the attribute after the function declaration, just before the |
318 | * semicolon. |
319 | * |
320 | * |[<!-- language="C" --> |
321 | * gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1); |
322 | * ]| |
323 | * |
324 | * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alloc_005fsize-function-attribute) for more details. |
325 | * |
326 | * Since: 2.18 |
327 | */ |
328 | |
329 | /** |
330 | * G_GNUC_ALLOC_SIZE2: |
331 | * @x: the index of the argument specifying one factor of the allocation size |
332 | * @y: the index of the argument specifying the second factor of the allocation size |
333 | * |
334 | * Expands to the GNU C `alloc_size` function attribute if the compiler is a |
335 | * new enough gcc. This attribute tells the compiler that the function returns |
336 | * a pointer to memory of a size that is specified by the product of two |
337 | * function parameters. |
338 | * |
339 | * Place the attribute after the function declaration, just before the |
340 | * semicolon. |
341 | * |
342 | * |[<!-- language="C" --> |
343 | * gpointer g_malloc_n (gsize n_blocks, |
344 | * gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1, 2); |
345 | * ]| |
346 | * |
347 | * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alloc_005fsize-function-attribute) for more details. |
348 | * |
349 | * Since: 2.18 |
350 | */ |
351 | #if g_macro__has_attribute(__alloc_size__) |
352 | #define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x))) |
353 | #define G_GNUC_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y))) |
354 | #else |
355 | #define G_GNUC_ALLOC_SIZE(x) |
356 | #define G_GNUC_ALLOC_SIZE2(x,y) |
357 | #endif |
358 | |
359 | /** |
360 | * G_GNUC_PRINTF: |
361 | * @format_idx: the index of the argument corresponding to the |
362 | * format string (the arguments are numbered from 1) |
363 | * @arg_idx: the index of the first of the format arguments, or 0 if |
364 | * there are no format arguments |
365 | * |
366 | * Expands to the GNU C `format` function attribute if the compiler is gcc. |
367 | * This is used for declaring functions which take a variable number of |
368 | * arguments, with the same syntax as `printf()`. It allows the compiler |
369 | * to type-check the arguments passed to the function. |
370 | * |
371 | * Place the attribute after the function declaration, just before the |
372 | * semicolon. |
373 | * |
374 | * See the |
375 | * [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-Wformat-3288) |
376 | * for more details. |
377 | * |
378 | * |[<!-- language="C" --> |
379 | * gint g_snprintf (gchar *string, |
380 | * gulong n, |
381 | * gchar const *format, |
382 | * ...) G_GNUC_PRINTF (3, 4); |
383 | * ]| |
384 | */ |
385 | |
386 | /** |
387 | * G_GNUC_SCANF: |
388 | * @format_idx: the index of the argument corresponding to |
389 | * the format string (the arguments are numbered from 1) |
390 | * @arg_idx: the index of the first of the format arguments, or 0 if |
391 | * there are no format arguments |
392 | * |
393 | * Expands to the GNU C `format` function attribute if the compiler is gcc. |
394 | * This is used for declaring functions which take a variable number of |
395 | * arguments, with the same syntax as `scanf()`. It allows the compiler |
396 | * to type-check the arguments passed to the function. |
397 | * |
398 | * |[<!-- language="C" --> |
399 | * int my_scanf (MyStream *stream, |
400 | * const char *format, |
401 | * ...) G_GNUC_SCANF (2, 3); |
402 | * int my_vscanf (MyStream *stream, |
403 | * const char *format, |
404 | * va_list ap) G_GNUC_SCANF (2, 0); |
405 | * ]| |
406 | * |
407 | * See the |
408 | * [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-Wformat-3288) |
409 | * for details. |
410 | */ |
411 | |
412 | /** |
413 | * G_GNUC_STRFTIME: |
414 | * @format_idx: the index of the argument corresponding to |
415 | * the format string (the arguments are numbered from 1) |
416 | * |
417 | * Expands to the GNU C `strftime` format function attribute if the compiler |
418 | * is gcc. This is used for declaring functions which take a format argument |
419 | * which is passed to `strftime()` or an API implementing its formats. It allows |
420 | * the compiler check the format passed to the function. |
421 | * |
422 | * |[<!-- language="C" --> |
423 | * gsize my_strftime (MyBuffer *buffer, |
424 | * const char *format, |
425 | * const struct tm *tm) G_GNUC_STRFTIME (2); |
426 | * ]| |
427 | * |
428 | * See the |
429 | * [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-Wformat-3288) |
430 | * for details. |
431 | * |
432 | * Since: 2.60 |
433 | */ |
434 | |
435 | /** |
436 | * G_GNUC_FORMAT: |
437 | * @arg_idx: the index of the argument |
438 | * |
439 | * Expands to the GNU C `format_arg` function attribute if the compiler |
440 | * is gcc. This function attribute specifies that a function takes a |
441 | * format string for a `printf()`, `scanf()`, `strftime()` or `strfmon()` style |
442 | * function and modifies it, so that the result can be passed to a `printf()`, |
443 | * `scanf()`, `strftime()` or `strfmon()` style function (with the remaining |
444 | * arguments to the format function the same as they would have been |
445 | * for the unmodified string). |
446 | * |
447 | * Place the attribute after the function declaration, just before the |
448 | * semicolon. |
449 | * |
450 | * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-Wformat-nonliteral-1) for more details. |
451 | * |
452 | * |[<!-- language="C" --> |
453 | * gchar *g_dgettext (gchar *domain_name, gchar *msgid) G_GNUC_FORMAT (2); |
454 | * ]| |
455 | */ |
456 | |
457 | /** |
458 | * G_GNUC_NORETURN: |
459 | * |
460 | * Expands to the GNU C `noreturn` function attribute if the compiler is gcc. |
461 | * It is used for declaring functions which never return. It enables |
462 | * optimization of the function, and avoids possible compiler warnings. |
463 | * |
464 | * Since 2.68, it is recommended that code uses %G_NORETURN instead of |
465 | * %G_GNUC_NORETURN, as that works on more platforms and compilers (in |
466 | * particular, MSVC and C++11) than %G_GNUC_NORETURN, which works with GCC and |
467 | * Clang only. %G_GNUC_NORETURN continues to work, so has not been deprecated |
468 | * yet. |
469 | * |
470 | * Place the attribute after the declaration, just before the semicolon. |
471 | * |
472 | * |[<!-- language="C" --> |
473 | * void g_abort (void) G_GNUC_NORETURN; |
474 | * ]| |
475 | * |
476 | * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute) for more details. |
477 | */ |
478 | |
479 | /** |
480 | * G_GNUC_CONST: |
481 | * |
482 | * Expands to the GNU C `const` function attribute if the compiler is gcc. |
483 | * Declaring a function as `const` enables better optimization of calls to |
484 | * the function. A `const` function doesn't examine any values except its |
485 | * parameters, and has no effects except its return value. |
486 | * |
487 | * Place the attribute after the declaration, just before the semicolon. |
488 | * |
489 | * |[<!-- language="C" --> |
490 | * gchar g_ascii_tolower (gchar c) G_GNUC_CONST; |
491 | * ]| |
492 | * |
493 | * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute) for more details. |
494 | * |
495 | * A function that has pointer arguments and examines the data pointed to |
496 | * must not be declared `const`. Likewise, a function that calls a non-`const` |
497 | * function usually must not be `const`. It doesn't make sense for a `const` |
498 | * function to return `void`. |
499 | */ |
500 | |
501 | /** |
502 | * G_GNUC_UNUSED: |
503 | * |
504 | * Expands to the GNU C `unused` function attribute if the compiler is gcc. |
505 | * It is used for declaring functions and arguments which may never be used. |
506 | * It avoids possible compiler warnings. |
507 | * |
508 | * For functions, place the attribute after the declaration, just before the |
509 | * semicolon. For arguments, place the attribute at the beginning of the |
510 | * argument declaration. |
511 | * |
512 | * |[<!-- language="C" --> |
513 | * void my_unused_function (G_GNUC_UNUSED gint unused_argument, |
514 | * gint other_argument) G_GNUC_UNUSED; |
515 | * ]| |
516 | * |
517 | * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute) for more details. |
518 | */ |
519 | |
520 | /** |
521 | * G_GNUC_NO_INSTRUMENT: |
522 | * |
523 | * Expands to the GNU C `no_instrument_function` function attribute if the |
524 | * compiler is gcc. Functions with this attribute will not be instrumented |
525 | * for profiling, when the compiler is called with the |
526 | * `-finstrument-functions` option. |
527 | * |
528 | * Place the attribute after the declaration, just before the semicolon. |
529 | * |
530 | * |[<!-- language="C" --> |
531 | * int do_uninteresting_things (void) G_GNUC_NO_INSTRUMENT; |
532 | * ]| |
533 | * |
534 | * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-no_005finstrument_005ffunction-function-attribute) for more details. |
535 | */ |
536 | |
537 | #if g_macro__has_attribute(__format__) |
538 | |
539 | #if !defined (__clang__) && G_GNUC_CHECK_VERSION (4, 4) |
540 | #define G_GNUC_PRINTF( format_idx, arg_idx ) \ |
541 | __attribute__((__format__ (gnu_printf, format_idx, arg_idx))) |
542 | #define G_GNUC_SCANF( format_idx, arg_idx ) \ |
543 | __attribute__((__format__ (gnu_scanf, format_idx, arg_idx))) |
544 | #define G_GNUC_STRFTIME( format_idx ) \ |
545 | __attribute__((__format__ (gnu_strftime, format_idx, 0))) \ |
546 | GLIB_AVAILABLE_MACRO_IN_2_60 |
547 | #else |
548 | #define G_GNUC_PRINTF( format_idx, arg_idx ) \ |
549 | __attribute__((__format__ (__printf__, format_idx, arg_idx))) |
550 | #define G_GNUC_SCANF( format_idx, arg_idx ) \ |
551 | __attribute__((__format__ (__scanf__, format_idx, arg_idx))) |
552 | #define G_GNUC_STRFTIME( format_idx ) \ |
553 | __attribute__((__format__ (__strftime__, format_idx, 0))) \ |
554 | GLIB_AVAILABLE_MACRO_IN_2_60 |
555 | #endif |
556 | |
557 | #else |
558 | |
559 | #define G_GNUC_PRINTF( format_idx, arg_idx ) |
560 | #define G_GNUC_SCANF( format_idx, arg_idx ) |
561 | #define G_GNUC_STRFTIME( format_idx ) \ |
562 | GLIB_AVAILABLE_MACRO_IN_2_60 |
563 | |
564 | #endif |
565 | |
566 | #if g_macro__has_attribute(__format_arg__) |
567 | #define G_GNUC_FORMAT(arg_idx) \ |
568 | __attribute__ ((__format_arg__ (arg_idx))) |
569 | #else |
570 | #define G_GNUC_FORMAT( arg_idx ) |
571 | #endif |
572 | |
573 | #if g_macro__has_attribute(__noreturn__) |
574 | #define G_GNUC_NORETURN \ |
575 | __attribute__ ((__noreturn__)) |
576 | #else |
577 | /* NOTE: MSVC has __declspec(noreturn) but unlike GCC __attribute__, |
578 | * __declspec can only be placed at the start of the function prototype |
579 | * and not at the end, so we can't use it without breaking API. |
580 | */ |
581 | #define G_GNUC_NORETURN |
582 | #endif |
583 | |
584 | #if g_macro__has_attribute(__const__) |
585 | #define G_GNUC_CONST \ |
586 | __attribute__ ((__const__)) |
587 | #else |
588 | #define G_GNUC_CONST |
589 | #endif |
590 | |
591 | #if g_macro__has_attribute(__unused__) |
592 | #define G_GNUC_UNUSED \ |
593 | __attribute__ ((__unused__)) |
594 | #else |
595 | #define G_GNUC_UNUSED |
596 | #endif |
597 | |
598 | #if g_macro__has_attribute(__no_instrument_function__) |
599 | #define G_GNUC_NO_INSTRUMENT \ |
600 | __attribute__ ((__no_instrument_function__)) |
601 | #else |
602 | #define G_GNUC_NO_INSTRUMENT |
603 | #endif |
604 | |
605 | /** |
606 | * G_GNUC_FALLTHROUGH: |
607 | * |
608 | * Expands to the GNU C `fallthrough` statement attribute if the compiler supports it. |
609 | * This allows declaring case statement to explicitly fall through in switch |
610 | * statements. To enable this feature, use `-Wimplicit-fallthrough` during |
611 | * compilation. |
612 | * |
613 | * Put the attribute right before the case statement you want to fall through |
614 | * to. |
615 | * |
616 | * |[<!-- language="C" --> |
617 | * switch (foo) |
618 | * { |
619 | * case 1: |
620 | * g_message ("it's 1"); |
621 | * G_GNUC_FALLTHROUGH; |
622 | * case 2: |
623 | * g_message ("it's either 1 or 2"); |
624 | * break; |
625 | * } |
626 | * ]| |
627 | * |
628 | * |
629 | * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html#index-fallthrough-statement-attribute) for more details. |
630 | * |
631 | * Since: 2.60 |
632 | */ |
633 | #if g_macro__has_attribute(fallthrough) |
634 | #define G_GNUC_FALLTHROUGH __attribute__((fallthrough)) \ |
635 | GLIB_AVAILABLE_MACRO_IN_2_60 |
636 | #else |
637 | #define G_GNUC_FALLTHROUGH \ |
638 | GLIB_AVAILABLE_MACRO_IN_2_60 |
639 | #endif |
640 | |
641 | /** |
642 | * G_GNUC_DEPRECATED: |
643 | * |
644 | * Expands to the GNU C `deprecated` attribute if the compiler is gcc. |
645 | * It can be used to mark `typedef`s, variables and functions as deprecated. |
646 | * When called with the `-Wdeprecated-declarations` option, |
647 | * gcc will generate warnings when deprecated interfaces are used. |
648 | * |
649 | * Place the attribute after the declaration, just before the semicolon. |
650 | * |
651 | * |[<!-- language="C" --> |
652 | * int my_mistake (void) G_GNUC_DEPRECATED; |
653 | * ]| |
654 | * |
655 | * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-deprecated-function-attribute) for more details. |
656 | * |
657 | * Since: 2.2 |
658 | */ |
659 | #if g_macro__has_attribute(__deprecated__) |
660 | #define G_GNUC_DEPRECATED __attribute__((__deprecated__)) |
661 | #else |
662 | #define G_GNUC_DEPRECATED |
663 | #endif /* __GNUC__ */ |
664 | |
665 | /** |
666 | * G_GNUC_DEPRECATED_FOR: |
667 | * @f: the intended replacement for the deprecated symbol, |
668 | * such as the name of a function |
669 | * |
670 | * Like %G_GNUC_DEPRECATED, but names the intended replacement for the |
671 | * deprecated symbol if the version of gcc in use is new enough to support |
672 | * custom deprecation messages. |
673 | * |
674 | * Place the attribute after the declaration, just before the semicolon. |
675 | * |
676 | * |[<!-- language="C" --> |
677 | * int my_mistake (void) G_GNUC_DEPRECATED_FOR(my_replacement); |
678 | * ]| |
679 | * |
680 | * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-deprecated-function-attribute) for more details. |
681 | * |
682 | * Note that if @f is a macro, it will be expanded in the warning message. |
683 | * You can enclose it in quotes to prevent this. (The quotes will show up |
684 | * in the warning, but it's better than showing the macro expansion.) |
685 | * |
686 | * Since: 2.26 |
687 | */ |
688 | #if G_GNUC_CHECK_VERSION(4, 5) || defined(__clang__) |
689 | #define G_GNUC_DEPRECATED_FOR(f) \ |
690 | __attribute__((deprecated("Use " #f " instead"))) \ |
691 | GLIB_AVAILABLE_MACRO_IN_2_26 |
692 | #else |
693 | #define G_GNUC_DEPRECATED_FOR(f) G_GNUC_DEPRECATED \ |
694 | GLIB_AVAILABLE_MACRO_IN_2_26 |
695 | #endif /* __GNUC__ */ |
696 | |
697 | #ifdef __ICC |
698 | #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ |
699 | _Pragma ("warning (push)") \ |
700 | _Pragma ("warning (disable:1478)") |
701 | #define G_GNUC_END_IGNORE_DEPRECATIONS \ |
702 | _Pragma ("warning (pop)") |
703 | #elif G_GNUC_CHECK_VERSION(4, 6) |
704 | #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ |
705 | _Pragma ("GCC diagnostic push") \ |
706 | _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"") |
707 | #define G_GNUC_END_IGNORE_DEPRECATIONS \ |
708 | _Pragma ("GCC diagnostic pop") |
709 | #elif defined (_MSC_VER) && (_MSC_VER >= 1500) && !defined (__clang__) |
710 | #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ |
711 | __pragma (warning (push)) \ |
712 | __pragma (warning (disable : 4996)) |
713 | #define G_GNUC_END_IGNORE_DEPRECATIONS \ |
714 | __pragma (warning (pop)) |
715 | #elif defined (__clang__) |
716 | #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ |
717 | _Pragma("clang diagnostic push") \ |
718 | _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") |
719 | #define G_GNUC_END_IGNORE_DEPRECATIONS \ |
720 | _Pragma("clang diagnostic pop") |
721 | #else |
722 | #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
723 | #define G_GNUC_END_IGNORE_DEPRECATIONS |
724 | #define GLIB_CANNOT_IGNORE_DEPRECATIONS |
725 | #endif |
726 | |
727 | /** |
728 | * G_GNUC_MAY_ALIAS: |
729 | * |
730 | * Expands to the GNU C `may_alias` type attribute if the compiler is gcc. |
731 | * Types with this attribute will not be subjected to type-based alias |
732 | * analysis, but are assumed to alias with any other type, just like `char`. |
733 | * |
734 | * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-may_005falias-type-attribute) for details. |
735 | * |
736 | * Since: 2.14 |
737 | */ |
738 | #if g_macro__has_attribute(may_alias) |
739 | #define G_GNUC_MAY_ALIAS __attribute__((may_alias)) |
740 | #else |
741 | #define G_GNUC_MAY_ALIAS |
742 | #endif |
743 | |
744 | /** |
745 | * G_GNUC_WARN_UNUSED_RESULT: |
746 | * |
747 | * Expands to the GNU C `warn_unused_result` function attribute if the compiler |
748 | * is gcc. This function attribute makes the compiler emit a warning if the |
749 | * result of a function call is ignored. |
750 | * |
751 | * Place the attribute after the declaration, just before the semicolon. |
752 | * |
753 | * |[<!-- language="C" --> |
754 | * GList *g_list_append (GList *list, |
755 | * gpointer data) G_GNUC_WARN_UNUSED_RESULT; |
756 | * ]| |
757 | * |
758 | * See the [GNU C documentation](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-warn_005funused_005fresult-function-attribute) for more details. |
759 | * |
760 | * Since: 2.10 |
761 | */ |
762 | #if g_macro__has_attribute(warn_unused_result) |
763 | #define G_GNUC_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
764 | #else |
765 | #define G_GNUC_WARN_UNUSED_RESULT |
766 | #endif /* __GNUC__ */ |
767 | |
768 | /** |
769 | * G_GNUC_FUNCTION: |
770 | * |
771 | * Expands to "" on all modern compilers, and to __FUNCTION__ on gcc |
772 | * version 2.x. Don't use it. |
773 | * |
774 | * Deprecated: 2.16: Use G_STRFUNC() instead |
775 | */ |
776 | |
777 | /** |
778 | * G_GNUC_PRETTY_FUNCTION: |
779 | * |
780 | * Expands to "" on all modern compilers, and to __PRETTY_FUNCTION__ |
781 | * on gcc version 2.x. Don't use it. |
782 | * |
783 | * Deprecated: 2.16: Use G_STRFUNC() instead |
784 | */ |
785 | |
786 | /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with |
787 | * macros, so we can refer to them as strings unconditionally. |
788 | * usage not-recommended since gcc-3.0 |
789 | * |
790 | * Mark them as deprecated since 2.26, since that’s when version macros were |
791 | * introduced. |
792 | */ |
793 | #if defined (__GNUC__) && (__GNUC__ < 3) |
794 | #define G_GNUC_FUNCTION __FUNCTION__ GLIB_DEPRECATED_MACRO_IN_2_26_FOR(G_STRFUNC) |
795 | #define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__ GLIB_DEPRECATED_MACRO_IN_2_26_FOR(G_STRFUNC) |
796 | #else /* !__GNUC__ */ |
797 | #define G_GNUC_FUNCTION "" GLIB_DEPRECATED_MACRO_IN_2_26_FOR(G_STRFUNC) |
798 | #define G_GNUC_PRETTY_FUNCTION "" GLIB_DEPRECATED_MACRO_IN_2_26_FOR(G_STRFUNC) |
799 | #endif /* !__GNUC__ */ |
800 | |
801 | #if g_macro__has_feature(attribute_analyzer_noreturn) && defined(__clang_analyzer__) |
802 | #define G_ANALYZER_ANALYZING 1 |
803 | #define G_ANALYZER_NORETURN __attribute__((analyzer_noreturn)) |
804 | #elif defined(__COVERITY__) |
805 | #define G_ANALYZER_ANALYZING 1 |
806 | #define G_ANALYZER_NORETURN __attribute__((noreturn)) |
807 | #else |
808 | #define G_ANALYZER_ANALYZING 0 |
809 | #define G_ANALYZER_NORETURN |
810 | #endif |
811 | |
812 | #define G_STRINGIFY(macro_or_string) G_STRINGIFY_ARG (macro_or_string) |
813 | #define G_STRINGIFY_ARG(contents) #contents |
814 | |
815 | #ifndef __GI_SCANNER__ /* The static assert macro really confuses the introspection parser */ |
816 | #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2 |
817 | #define G_PASTE(identifier1,identifier2) G_PASTE_ARGS (identifier1, identifier2) |
818 | #if !defined(__cplusplus) && defined(__STDC_VERSION__) && \ |
819 | (__STDC_VERSION__ >= 201112L || g_macro__has_feature(c_static_assert) || g_macro__has_extension(c_static_assert)) |
820 | #define G_STATIC_ASSERT(expr) _Static_assert (expr, "Expression evaluates to false") |
821 | #elif (defined(__cplusplus) && __cplusplus >= 201103L) || \ |
822 | (defined(__cplusplus) && defined (_MSC_VER) && (_MSC_VER >= 1600)) || \ |
823 | (defined (_MSC_VER) && (_MSC_VER >= 1800)) |
824 | #define G_STATIC_ASSERT(expr) static_assert (expr, "Expression evaluates to false") |
825 | #else |
826 | #ifdef __COUNTER__ |
827 | #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __COUNTER__)[(expr) ? 1 : -1] G_GNUC_UNUSED |
828 | #else |
829 | #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __LINE__)[(expr) ? 1 : -1] G_GNUC_UNUSED |
830 | #endif |
831 | #endif /* __STDC_VERSION__ */ |
832 | #define G_STATIC_ASSERT_EXPR(expr) ((void) sizeof (char[(expr) ? 1 : -1])) |
833 | #endif /* !__GI_SCANNER__ */ |
834 | |
835 | /* Provide a string identifying the current code position */ |
836 | #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus) |
837 | #define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()" |
838 | #else |
839 | #define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) |
840 | #endif |
841 | |
842 | /* Provide a string identifying the current function, non-concatenatable */ |
843 | #if defined (__GNUC__) && defined (__cplusplus) |
844 | #define G_STRFUNC ((const char*) (__PRETTY_FUNCTION__)) |
845 | #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L |
846 | #define G_STRFUNC ((const char*) (__func__)) |
847 | #elif defined (__GNUC__) || (defined(_MSC_VER) && (_MSC_VER > 1300)) |
848 | #define G_STRFUNC ((const char*) (__FUNCTION__)) |
849 | #else |
850 | #define G_STRFUNC ((const char*) ("???")) |
851 | #endif |
852 | |
853 | /* Guard C code in headers, while including them from C++ */ |
854 | #ifdef __cplusplus |
855 | #define G_BEGIN_DECLS extern "C" { |
856 | #define G_END_DECLS } |
857 | #else |
858 | #define G_BEGIN_DECLS |
859 | #define G_END_DECLS |
860 | #endif |
861 | |
862 | /* Provide definitions for some commonly used macros. |
863 | * Some of them are only provided if they haven't already |
864 | * been defined. It is assumed that if they are already |
865 | * defined then the current definition is correct. |
866 | */ |
867 | #ifndef NULL |
868 | # ifdef __cplusplus |
869 | # define NULL (0L) |
870 | # else /* !__cplusplus */ |
871 | # define NULL ((void*) 0) |
872 | # endif /* !__cplusplus */ |
873 | #endif |
874 | |
875 | #ifndef FALSE |
876 | #define FALSE (0) |
877 | #endif |
878 | |
879 | #ifndef TRUE |
880 | #define TRUE (!FALSE) |
881 | #endif |
882 | |
883 | #undef MAX |
884 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) |
885 | |
886 | #undef MIN |
887 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
888 | |
889 | #undef ABS |
890 | #define ABS(a) (((a) < 0) ? -(a) : (a)) |
891 | |
892 | #undef CLAMP |
893 | #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) |
894 | |
895 | #define G_APPROX_VALUE(a, b, epsilon) \ |
896 | (((a) > (b) ? (a) - (b) : (b) - (a)) < (epsilon)) |
897 | |
898 | /* Count the number of elements in an array. The array must be defined |
899 | * as such; using this with a dynamically allocated array will give |
900 | * incorrect results. |
901 | */ |
902 | #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0])) |
903 | |
904 | /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT |
905 | */ |
906 | #define GPOINTER_TO_SIZE(p) ((gsize) (p)) |
907 | #define GSIZE_TO_POINTER(s) ((gpointer) (gsize) (s)) |
908 | |
909 | /* Provide convenience macros for handling structure |
910 | * fields through their offsets. |
911 | */ |
912 | |
913 | #if G_GNUC_CHECK_VERSION(4, 0) || defined(_MSC_VER) |
914 | #define G_STRUCT_OFFSET(struct_type, member) \ |
915 | ((glong) offsetof (struct_type, member)) |
916 | #else |
917 | #define G_STRUCT_OFFSET(struct_type, member) \ |
918 | ((glong) ((guint8*) &((struct_type*) 0)->member)) |
919 | #endif |
920 | |
921 | #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \ |
922 | ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset))) |
923 | #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \ |
924 | (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset))) |
925 | |
926 | /* Provide simple macro statement wrappers: |
927 | * G_STMT_START { statements; } G_STMT_END; |
928 | * This can be used as a single statement, like: |
929 | * if (x) G_STMT_START { ... } G_STMT_END; else ... |
930 | * This intentionally does not use compiler extensions like GCC's '({...})' to |
931 | * avoid portability issue or side effects when compiled with different compilers. |
932 | * MSVC complains about "while(0)": C4127: "Conditional expression is constant", |
933 | * so we use __pragma to avoid the warning since the use here is intentional. |
934 | */ |
935 | #if !(defined (G_STMT_START) && defined (G_STMT_END)) |
936 | #define G_STMT_START do |
937 | #if defined (_MSC_VER) && (_MSC_VER >= 1500) |
938 | #define G_STMT_END \ |
939 | __pragma(warning(push)) \ |
940 | __pragma(warning(disable:4127)) \ |
941 | while(0) \ |
942 | __pragma(warning(pop)) |
943 | #else |
944 | #define G_STMT_END while (0) |
945 | #endif |
946 | #endif |
947 | |
948 | /* Provide G_ALIGNOF alignment macro. |
949 | * |
950 | * Note we cannot use the gcc __alignof__ operator here, as that returns the |
951 | * preferred alignment rather than the minimal alignment. See |
952 | * https://gitlab.gnome.org/GNOME/glib/merge_requests/538/diffs#note_390790. |
953 | */ |
954 | |
955 | /** |
956 | * G_ALIGNOF |
957 | * @type: a type-name |
958 | * |
959 | * Return the minimal alignment required by the platform ABI for values of the given |
960 | * type. The address of a variable or struct member of the given type must always be |
961 | * a multiple of this alignment. For example, most platforms require int variables |
962 | * to be aligned at a 4-byte boundary, so `G_ALIGNOF (int)` is 4 on most platforms. |
963 | * |
964 | * Note this is not necessarily the same as the value returned by GCC’s |
965 | * `__alignof__` operator, which returns the preferred alignment for a type. |
966 | * The preferred alignment may be a stricter alignment than the minimal |
967 | * alignment. |
968 | * |
969 | * Since: 2.60 |
970 | */ |
971 | #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__cplusplus) |
972 | #define G_ALIGNOF(type) _Alignof (type) \ |
973 | GLIB_AVAILABLE_MACRO_IN_2_60 |
974 | #else |
975 | #define G_ALIGNOF(type) (G_STRUCT_OFFSET (struct { char a; type b; }, b)) \ |
976 | GLIB_AVAILABLE_MACRO_IN_2_60 |
977 | #endif |
978 | |
979 | /** |
980 | * G_CONST_RETURN: |
981 | * |
982 | * If %G_DISABLE_CONST_RETURNS is defined, this macro expands |
983 | * to nothing. By default, the macro expands to const. The macro |
984 | * can be used in place of const for functions that return a value |
985 | * that should not be modified. The purpose of this macro is to allow |
986 | * us to turn on const for returned constant strings by default, while |
987 | * allowing programmers who find that annoying to turn it off. This macro |
988 | * should only be used for return values and for "out" parameters, it |
989 | * doesn't make sense for "in" parameters. |
990 | * |
991 | * Deprecated: 2.30: API providers should replace all existing uses with |
992 | * const and API consumers should adjust their code accordingly |
993 | */ |
994 | #ifdef G_DISABLE_CONST_RETURNS |
995 | #define G_CONST_RETURN GLIB_DEPRECATED_MACRO_IN_2_30_FOR(const) |
996 | #else |
997 | #define G_CONST_RETURN const GLIB_DEPRECATED_MACRO_IN_2_30_FOR(const) |
998 | #endif |
999 | |
1000 | /** |
1001 | * G_NORETURN: |
1002 | * |
1003 | * Expands to the GNU C or MSVC `noreturn` function attribute depending on |
1004 | * the compiler. It is used for declaring functions which never return. |
1005 | * Enables optimization of the function, and avoids possible compiler warnings. |
1006 | * |
1007 | * Note that %G_NORETURN supersedes the previous %G_GNUC_NORETURN macro, which |
1008 | * will eventually be deprecated. %G_NORETURN supports more platforms. |
1009 | * |
1010 | * Place the attribute before the function declaration as follows: |
1011 | * |
1012 | * |[<!-- language="C" --> |
1013 | * G_NORETURN void g_abort (void); |
1014 | * ]| |
1015 | * |
1016 | * Since: 2.68 |
1017 | */ |
1018 | /* Note: We can’t annotate this with GLIB_AVAILABLE_MACRO_IN_2_68 because it’s |
1019 | * used within the GLib headers in function declarations which are always |
1020 | * evaluated when a header is included. This results in warnings in third party |
1021 | * code which includes glib.h, even if the third party code doesn’t use the new |
1022 | * macro itself. */ |
1023 | #if g_macro__has_attribute(__noreturn__) |
1024 | /* For compatibility with G_NORETURN_FUNCPTR on clang, use |
1025 | __attribute__((__noreturn__)), not _Noreturn. */ |
1026 | # define G_NORETURN __attribute__ ((__noreturn__)) |
1027 | #elif defined (_MSC_VER) && (1200 <= _MSC_VER) |
1028 | /* Use MSVC specific syntax. */ |
1029 | # define G_NORETURN __declspec (noreturn) |
1030 | /* Use ISO C++11 syntax when the compiler supports it. */ |
1031 | #elif defined (__cplusplus) && __cplusplus >= 201103 |
1032 | # define G_NORETURN [[noreturn]] |
1033 | /* Use ISO C11 syntax when the compiler supports it. */ |
1034 | #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112 |
1035 | # define G_NORETURN _Noreturn |
1036 | #else |
1037 | # define G_NORETURN /* empty */ |
1038 | #endif |
1039 | |
1040 | /** |
1041 | * G_NORETURN_FUNCPTR: |
1042 | * |
1043 | * Expands to the GNU C or MSVC `noreturn` function attribute depending on |
1044 | * the compiler. It is used for declaring function pointers which never return. |
1045 | * Enables optimization of the function, and avoids possible compiler warnings. |
1046 | * |
1047 | * Place the attribute before the function declaration as follows: |
1048 | * |
1049 | * |[<!-- language="C" --> |
1050 | * G_NORETURN_FUNCPTR void (*funcptr) (void); |
1051 | * ]| |
1052 | * |
1053 | * Note that if the function is not a function pointer, you can simply use |
1054 | * the %G_NORETURN macro as follows: |
1055 | * |
1056 | * |[<!-- language="C" --> |
1057 | * G_NORETURN void g_abort (void); |
1058 | * ]| |
1059 | * |
1060 | * Since: 2.68 |
1061 | */ |
1062 | #if g_macro__has_attribute(__noreturn__) |
1063 | # define G_NORETURN_FUNCPTR __attribute__ ((__noreturn__)) \ |
1064 | GLIB_AVAILABLE_MACRO_IN_2_68 |
1065 | #else |
1066 | # define G_NORETURN_FUNCPTR /* empty */ \ |
1067 | GLIB_AVAILABLE_MACRO_IN_2_68 |
1068 | #endif |
1069 | |
1070 | /* |
1071 | * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to |
1072 | * the compiler about the expected result of an expression. Some compilers |
1073 | * can use this information for optimizations. |
1074 | * |
1075 | * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when |
1076 | * putting assignments in g_return_if_fail (). |
1077 | */ |
1078 | #if G_GNUC_CHECK_VERSION(2, 0) && defined(__OPTIMIZE__) |
1079 | #define _G_BOOLEAN_EXPR(expr) \ |
1080 | G_GNUC_EXTENSION ({ \ |
1081 | int _g_boolean_var_; \ |
1082 | if (expr) \ |
1083 | _g_boolean_var_ = 1; \ |
1084 | else \ |
1085 | _g_boolean_var_ = 0; \ |
1086 | _g_boolean_var_; \ |
1087 | }) |
1088 | #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1)) |
1089 | #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0)) |
1090 | #else |
1091 | #define G_LIKELY(expr) (expr) |
1092 | #define G_UNLIKELY(expr) (expr) |
1093 | #endif |
1094 | |
1095 | /* GLIB_CANNOT_IGNORE_DEPRECATIONS is defined above for compilers that do not |
1096 | * have a way to temporarily suppress deprecation warnings. In these cases, |
1097 | * suppress the deprecated attribute altogether (otherwise a simple #include |
1098 | * <glib.h> will emit a barrage of warnings). |
1099 | */ |
1100 | #if defined(GLIB_CANNOT_IGNORE_DEPRECATIONS) |
1101 | #define G_DEPRECATED |
1102 | #elif G_GNUC_CHECK_VERSION(3, 1) || defined(__clang__) |
1103 | #define G_DEPRECATED __attribute__((__deprecated__)) |
1104 | #elif defined(_MSC_VER) && (_MSC_VER >= 1300) |
1105 | #define G_DEPRECATED __declspec(deprecated) |
1106 | #else |
1107 | #define G_DEPRECATED |
1108 | #endif |
1109 | |
1110 | #if defined(GLIB_CANNOT_IGNORE_DEPRECATIONS) |
1111 | #define G_DEPRECATED_FOR(f) G_DEPRECATED |
1112 | #elif G_GNUC_CHECK_VERSION(4, 5) || defined(__clang__) |
1113 | #define G_DEPRECATED_FOR(f) __attribute__((__deprecated__("Use '" #f "' instead"))) |
1114 | #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320) |
1115 | #define G_DEPRECATED_FOR(f) __declspec(deprecated("is deprecated. Use '" #f "' instead")) |
1116 | #else |
1117 | #define G_DEPRECATED_FOR(f) G_DEPRECATED |
1118 | #endif |
1119 | |
1120 | #if G_GNUC_CHECK_VERSION(4, 5) || defined(__clang__) |
1121 | #define G_UNAVAILABLE(maj,min) __attribute__((deprecated("Not available before " #maj "." #min))) |
1122 | #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320) |
1123 | #define G_UNAVAILABLE(maj,min) __declspec(deprecated("is not available before " #maj "." #min)) |
1124 | #else |
1125 | #define G_UNAVAILABLE(maj,min) G_DEPRECATED |
1126 | #endif |
1127 | |
1128 | #ifndef _GLIB_EXTERN |
1129 | #define _GLIB_EXTERN extern |
1130 | #endif |
1131 | |
1132 | /* These macros are used to mark deprecated symbols in GLib headers, |
1133 | * and thus have to be exposed in installed headers. But please |
1134 | * do *not* use them in other projects. Instead, use G_DEPRECATED |
1135 | * or define your own wrappers around it. |
1136 | */ |
1137 | |
1138 | #ifdef GLIB_DISABLE_DEPRECATION_WARNINGS |
1139 | #define GLIB_DEPRECATED _GLIB_EXTERN |
1140 | #define GLIB_DEPRECATED_FOR(f) _GLIB_EXTERN |
1141 | #define GLIB_UNAVAILABLE(maj,min) _GLIB_EXTERN |
1142 | #define GLIB_UNAVAILABLE_STATIC_INLINE(maj,min) |
1143 | #else |
1144 | #define GLIB_DEPRECATED G_DEPRECATED _GLIB_EXTERN |
1145 | #define GLIB_DEPRECATED_FOR(f) G_DEPRECATED_FOR(f) _GLIB_EXTERN |
1146 | #define GLIB_UNAVAILABLE(maj,min) G_UNAVAILABLE(maj,min) _GLIB_EXTERN |
1147 | #define GLIB_UNAVAILABLE_STATIC_INLINE(maj,min) G_UNAVAILABLE(maj,min) |
1148 | #endif |
1149 | |
1150 | #if !defined(GLIB_DISABLE_DEPRECATION_WARNINGS) && \ |
1151 | (G_GNUC_CHECK_VERSION(4, 6) || \ |
1152 | __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 4)) |
1153 | #define _GLIB_GNUC_DO_PRAGMA(x) _Pragma(G_STRINGIFY (x)) |
1154 | #define GLIB_DEPRECATED_MACRO _GLIB_GNUC_DO_PRAGMA(GCC warning "Deprecated pre-processor symbol") |
1155 | #define GLIB_DEPRECATED_MACRO_FOR(f) \ |
1156 | _GLIB_GNUC_DO_PRAGMA(GCC warning G_STRINGIFY (Deprecated pre-processor symbol: replace with #f)) |
1157 | #define GLIB_UNAVAILABLE_MACRO(maj,min) \ |
1158 | _GLIB_GNUC_DO_PRAGMA(GCC warning G_STRINGIFY (Not available before maj.min)) |
1159 | #else |
1160 | #define GLIB_DEPRECATED_MACRO |
1161 | #define GLIB_DEPRECATED_MACRO_FOR(f) |
1162 | #define GLIB_UNAVAILABLE_MACRO(maj,min) |
1163 | #endif |
1164 | |
1165 | #if !defined(GLIB_DISABLE_DEPRECATION_WARNINGS) && \ |
1166 | (G_GNUC_CHECK_VERSION(6, 1) || \ |
1167 | (defined (__clang_major__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 0)))) |
1168 | #define GLIB_DEPRECATED_ENUMERATOR G_DEPRECATED |
1169 | #define GLIB_DEPRECATED_ENUMERATOR_FOR(f) G_DEPRECATED_FOR(f) |
1170 | #define GLIB_UNAVAILABLE_ENUMERATOR(maj,min) G_UNAVAILABLE(maj,min) |
1171 | #else |
1172 | #define GLIB_DEPRECATED_ENUMERATOR |
1173 | #define GLIB_DEPRECATED_ENUMERATOR_FOR(f) |
1174 | #define GLIB_UNAVAILABLE_ENUMERATOR(maj,min) |
1175 | #endif |
1176 | |
1177 | #if !defined(GLIB_DISABLE_DEPRECATION_WARNINGS) && \ |
1178 | (G_GNUC_CHECK_VERSION(3, 1) || \ |
1179 | (defined (__clang_major__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 0)))) |
1180 | #define GLIB_DEPRECATED_TYPE G_DEPRECATED |
1181 | #define GLIB_DEPRECATED_TYPE_FOR(f) G_DEPRECATED_FOR(f) |
1182 | #define GLIB_UNAVAILABLE_TYPE(maj,min) G_UNAVAILABLE(maj,min) |
1183 | #else |
1184 | #define GLIB_DEPRECATED_TYPE |
1185 | #define GLIB_DEPRECATED_TYPE_FOR(f) |
1186 | #define GLIB_UNAVAILABLE_TYPE(maj,min) |
1187 | #endif |
1188 | |
1189 | #ifndef __GI_SCANNER__ |
1190 | |
1191 | #if g_macro__has_attribute(cleanup) |
1192 | |
1193 | /* these macros are private */ |
1194 | #define _GLIB_AUTOPTR_FUNC_NAME(TypeName) glib_autoptr_cleanup_##TypeName |
1195 | #define _GLIB_AUTOPTR_CLEAR_FUNC_NAME(TypeName) glib_autoptr_clear_##TypeName |
1196 | #define _GLIB_AUTOPTR_TYPENAME(TypeName) TypeName##_autoptr |
1197 | #define _GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName) glib_listautoptr_cleanup_##TypeName |
1198 | #define _GLIB_AUTOPTR_LIST_TYPENAME(TypeName) TypeName##_listautoptr |
1199 | #define _GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName) glib_slistautoptr_cleanup_##TypeName |
1200 | #define _GLIB_AUTOPTR_SLIST_TYPENAME(TypeName) TypeName##_slistautoptr |
1201 | #define _GLIB_AUTOPTR_QUEUE_FUNC_NAME(TypeName) glib_queueautoptr_cleanup_##TypeName |
1202 | #define _GLIB_AUTOPTR_QUEUE_TYPENAME(TypeName) TypeName##_queueautoptr |
1203 | #define _GLIB_AUTO_FUNC_NAME(TypeName) glib_auto_cleanup_##TypeName |
1204 | #define _GLIB_CLEANUP(func) __attribute__((cleanup(func))) |
1205 | #define _GLIB_DEFINE_AUTOPTR_CLEANUP_FUNCS(TypeName, ParentName, cleanup) \ |
1206 | typedef TypeName *_GLIB_AUTOPTR_TYPENAME(TypeName); \ |
1207 | typedef GList *_GLIB_AUTOPTR_LIST_TYPENAME(TypeName); \ |
1208 | typedef GSList *_GLIB_AUTOPTR_SLIST_TYPENAME(TypeName); \ |
1209 | typedef GQueue *_GLIB_AUTOPTR_QUEUE_TYPENAME(TypeName); \ |
1210 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ |
1211 | static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_CLEAR_FUNC_NAME(TypeName) (TypeName *_ptr) \ |
1212 | { if (_ptr) (cleanup) ((ParentName *) _ptr); } \ |
1213 | static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_FUNC_NAME(TypeName) (TypeName **_ptr) \ |
1214 | { _GLIB_AUTOPTR_CLEAR_FUNC_NAME(TypeName) (*_ptr); } \ |
1215 | static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName) (GList **_l) \ |
1216 | { g_list_free_full (*_l, (GDestroyNotify) (void(*)(void)) cleanup); } \ |
1217 | static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName) (GSList **_l) \ |
1218 | { g_slist_free_full (*_l, (GDestroyNotify) (void(*)(void)) cleanup); } \ |
1219 | static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_QUEUE_FUNC_NAME(TypeName) (GQueue **_q) \ |
1220 | { if (*_q) g_queue_free_full (*_q, (GDestroyNotify) (void(*)(void)) cleanup); } \ |
1221 | G_GNUC_END_IGNORE_DEPRECATIONS |
1222 | #define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName) \ |
1223 | _GLIB_DEFINE_AUTOPTR_CLEANUP_FUNCS(ModuleObjName, ParentName, _GLIB_AUTOPTR_CLEAR_FUNC_NAME(ParentName)) |
1224 | |
1225 | |
1226 | /* these macros are API */ |
1227 | #define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) \ |
1228 | _GLIB_DEFINE_AUTOPTR_CLEANUP_FUNCS(TypeName, TypeName, func) |
1229 | #define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func) \ |
1230 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ |
1231 | static G_GNUC_UNUSED inline void _GLIB_AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { (func) (_ptr); } \ |
1232 | G_GNUC_END_IGNORE_DEPRECATIONS |
1233 | #define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none) \ |
1234 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ |
1235 | static G_GNUC_UNUSED inline void _GLIB_AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { if (*_ptr != none) (func) (*_ptr); } \ |
1236 | G_GNUC_END_IGNORE_DEPRECATIONS |
1237 | #define g_autoptr(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_TYPENAME(TypeName) |
1238 | #define g_autolist(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_LIST_TYPENAME(TypeName) |
1239 | #define g_autoslist(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_SLIST_TYPENAME(TypeName) |
1240 | #define g_autoqueue(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_QUEUE_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_QUEUE_TYPENAME(TypeName) |
1241 | #define g_auto(TypeName) _GLIB_CLEANUP(_GLIB_AUTO_FUNC_NAME(TypeName)) TypeName |
1242 | #define g_autofree _GLIB_CLEANUP(g_autoptr_cleanup_generic_gfree) |
1243 | |
1244 | #else /* not GNU C */ |
1245 | /* this (dummy) macro is private */ |
1246 | #define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName) |
1247 | |
1248 | /* these (dummy) macros are API */ |
1249 | #define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) |
1250 | #define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func) |
1251 | #define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none) |
1252 | |
1253 | /* no declaration of g_auto() or g_autoptr() here */ |
1254 | #endif /* __GNUC__ */ |
1255 | |
1256 | #else |
1257 | |
1258 | #define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName) |
1259 | |
1260 | #define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) |
1261 | #define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func) |
1262 | #define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none) |
1263 | |
1264 | #endif /* __GI_SCANNER__ */ |
1265 | |
1266 | /** |
1267 | * G_SIZEOF_MEMBER: |
1268 | * @struct_type: a structure type, e.g. #GOutputVector |
1269 | * @member: a field in the structure, e.g. `size` |
1270 | * |
1271 | * Returns the size of @member in the struct definition without having a |
1272 | * declared instance of @struct_type. |
1273 | * |
1274 | * Returns: the size of @member in bytes. |
1275 | * |
1276 | * Since: 2.64 |
1277 | */ |
1278 | #define G_SIZEOF_MEMBER(struct_type, member) \ |
1279 | GLIB_AVAILABLE_MACRO_IN_2_64 \ |
1280 | sizeof (((struct_type *) 0)->member) |
1281 | |
1282 | #endif /* __G_MACROS_H__ */ |
1283 | |