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