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 | /* For historical reasons we need to continue to support those who |
86 | * define G_IMPLEMENT_INLINES to mean "don't implement this here". |
87 | */ |
88 | #ifdef G_IMPLEMENT_INLINES |
89 | # define G_INLINE_FUNC extern |
90 | # undef G_CAN_INLINE |
91 | #else |
92 | # define G_INLINE_FUNC static inline |
93 | #endif /* G_IMPLEMENT_INLINES */ |
94 | |
95 | /* Provide macros to feature the GCC function attribute. |
96 | */ |
97 | #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) |
98 | #define G_GNUC_PURE __attribute__((__pure__)) |
99 | #define G_GNUC_MALLOC __attribute__((__malloc__)) |
100 | #define G_GNUC_NO_INLINE __attribute__((noinline)) |
101 | #else |
102 | #define G_GNUC_PURE |
103 | #define G_GNUC_MALLOC |
104 | #define G_GNUC_NO_INLINE |
105 | #endif |
106 | |
107 | #if __GNUC__ >= 4 |
108 | #define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__)) |
109 | #else |
110 | #define G_GNUC_NULL_TERMINATED |
111 | #endif |
112 | |
113 | /* |
114 | * We can only use __typeof__ on GCC >= 4.8, and not when compiling C++. Since |
115 | * __typeof__ is used in a few places in GLib, provide a pre-processor symbol |
116 | * to factor the check out from callers. |
117 | * |
118 | * This symbol is private. |
119 | */ |
120 | #undef g_has_typeof |
121 | #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) && !defined(__cplusplus) |
122 | #define g_has_typeof |
123 | #endif |
124 | |
125 | /* |
126 | * Clang feature detection: http://clang.llvm.org/docs/LanguageExtensions.html |
127 | * These are not available on GCC, but since the pre-processor doesn't do |
128 | * operator short-circuiting, we can't use it in a statement or we'll get: |
129 | * |
130 | * error: missing binary operator before token "(" |
131 | * |
132 | * So we define it to 0 to satisfy the pre-processor. |
133 | */ |
134 | |
135 | #ifdef __has_attribute |
136 | #define g_macro__has_attribute __has_attribute |
137 | #else |
138 | #define g_macro__has_attribute(x) 0 |
139 | #endif |
140 | |
141 | #ifdef __has_feature |
142 | #define g_macro__has_feature __has_feature |
143 | #else |
144 | #define g_macro__has_feature(x) 0 |
145 | #endif |
146 | |
147 | #ifdef __has_builtin |
148 | #define g_macro__has_builtin __has_builtin |
149 | #else |
150 | #define g_macro__has_builtin(x) 0 |
151 | #endif |
152 | |
153 | #if (!defined(__clang__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) || \ |
154 | (defined(__clang__) && g_macro__has_attribute(__alloc_size__)) |
155 | #define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x))) |
156 | #define G_GNUC_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y))) |
157 | #else |
158 | #define G_GNUC_ALLOC_SIZE(x) |
159 | #define G_GNUC_ALLOC_SIZE2(x,y) |
160 | #endif |
161 | |
162 | #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) |
163 | #if !defined (__clang__) && G_GNUC_CHECK_VERSION (4, 4) |
164 | #define G_GNUC_PRINTF( format_idx, arg_idx ) \ |
165 | __attribute__((__format__ (gnu_printf, format_idx, arg_idx))) |
166 | #define G_GNUC_SCANF( format_idx, arg_idx ) \ |
167 | __attribute__((__format__ (gnu_scanf, format_idx, arg_idx))) |
168 | #else |
169 | #define G_GNUC_PRINTF( format_idx, arg_idx ) \ |
170 | __attribute__((__format__ (__printf__, format_idx, arg_idx))) |
171 | #define G_GNUC_SCANF( format_idx, arg_idx ) \ |
172 | __attribute__((__format__ (__scanf__, format_idx, arg_idx))) |
173 | #endif |
174 | #define G_GNUC_FORMAT( arg_idx ) \ |
175 | __attribute__((__format_arg__ (arg_idx))) |
176 | #define G_GNUC_NORETURN \ |
177 | __attribute__((__noreturn__)) |
178 | #define G_GNUC_CONST \ |
179 | __attribute__((__const__)) |
180 | #define G_GNUC_UNUSED \ |
181 | __attribute__((__unused__)) |
182 | #define G_GNUC_NO_INSTRUMENT \ |
183 | __attribute__((__no_instrument_function__)) |
184 | #else /* !__GNUC__ */ |
185 | #define G_GNUC_PRINTF( format_idx, arg_idx ) |
186 | #define G_GNUC_SCANF( format_idx, arg_idx ) |
187 | #define G_GNUC_FORMAT( arg_idx ) |
188 | #define G_GNUC_NORETURN |
189 | #define G_GNUC_CONST |
190 | #define G_GNUC_UNUSED |
191 | #define G_GNUC_NO_INSTRUMENT |
192 | #endif /* !__GNUC__ */ |
193 | |
194 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) |
195 | #define G_GNUC_DEPRECATED __attribute__((__deprecated__)) |
196 | #else |
197 | #define G_GNUC_DEPRECATED |
198 | #endif /* __GNUC__ */ |
199 | |
200 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) |
201 | #define G_GNUC_DEPRECATED_FOR(f) \ |
202 | __attribute__((deprecated("Use " #f " instead"))) |
203 | #else |
204 | #define G_GNUC_DEPRECATED_FOR(f) G_GNUC_DEPRECATED |
205 | #endif /* __GNUC__ */ |
206 | |
207 | #ifdef __ICC |
208 | #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ |
209 | _Pragma ("warning (push)") \ |
210 | _Pragma ("warning (disable:1478)") |
211 | #define G_GNUC_END_IGNORE_DEPRECATIONS \ |
212 | _Pragma ("warning (pop)") |
213 | #elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
214 | #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ |
215 | _Pragma ("GCC diagnostic push") \ |
216 | _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"") |
217 | #define G_GNUC_END_IGNORE_DEPRECATIONS \ |
218 | _Pragma ("GCC diagnostic pop") |
219 | #elif defined (_MSC_VER) && (_MSC_VER >= 1500) |
220 | #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ |
221 | __pragma (warning (push)) \ |
222 | __pragma (warning (disable : 4996)) |
223 | #define G_GNUC_END_IGNORE_DEPRECATIONS \ |
224 | __pragma (warning (pop)) |
225 | #elif defined (__clang__) |
226 | #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ |
227 | _Pragma("clang diagnostic push") \ |
228 | _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") |
229 | #define G_GNUC_END_IGNORE_DEPRECATIONS \ |
230 | _Pragma("clang diagnostic pop") |
231 | #else |
232 | #define G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
233 | #define G_GNUC_END_IGNORE_DEPRECATIONS |
234 | #endif |
235 | |
236 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) |
237 | #define G_GNUC_MAY_ALIAS __attribute__((may_alias)) |
238 | #else |
239 | #define G_GNUC_MAY_ALIAS |
240 | #endif |
241 | |
242 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) |
243 | #define G_GNUC_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
244 | #else |
245 | #define G_GNUC_WARN_UNUSED_RESULT |
246 | #endif /* __GNUC__ */ |
247 | |
248 | #ifndef G_DISABLE_DEPRECATED |
249 | /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with |
250 | * macros, so we can refer to them as strings unconditionally. |
251 | * usage not-recommended since gcc-3.0 |
252 | */ |
253 | #if defined (__GNUC__) && (__GNUC__ < 3) |
254 | #define G_GNUC_FUNCTION __FUNCTION__ |
255 | #define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__ |
256 | #else /* !__GNUC__ */ |
257 | #define G_GNUC_FUNCTION "" |
258 | #define G_GNUC_PRETTY_FUNCTION "" |
259 | #endif /* !__GNUC__ */ |
260 | #endif /* !G_DISABLE_DEPRECATED */ |
261 | |
262 | #if g_macro__has_feature(attribute_analyzer_noreturn) && defined(__clang_analyzer__) |
263 | #define G_ANALYZER_ANALYZING 1 |
264 | #define G_ANALYZER_NORETURN __attribute__((analyzer_noreturn)) |
265 | #else |
266 | #define G_ANALYZER_ANALYZING 0 |
267 | #define G_ANALYZER_NORETURN |
268 | #endif |
269 | |
270 | #define G_STRINGIFY(macro_or_string) G_STRINGIFY_ARG (macro_or_string) |
271 | #define G_STRINGIFY_ARG(contents) #contents |
272 | |
273 | #ifndef __GI_SCANNER__ /* The static assert macro really confuses the introspection parser */ |
274 | #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2 |
275 | #define G_PASTE(identifier1,identifier2) G_PASTE_ARGS (identifier1, identifier2) |
276 | #ifdef __COUNTER__ |
277 | #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __COUNTER__)[(expr) ? 1 : -1] G_GNUC_UNUSED |
278 | #else |
279 | #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __LINE__)[(expr) ? 1 : -1] G_GNUC_UNUSED |
280 | #endif |
281 | #define G_STATIC_ASSERT_EXPR(expr) ((void) sizeof (char[(expr) ? 1 : -1])) |
282 | #endif |
283 | |
284 | /* Provide a string identifying the current code position */ |
285 | #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus) |
286 | #define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()" |
287 | #else |
288 | #define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) |
289 | #endif |
290 | |
291 | /* Provide a string identifying the current function, non-concatenatable */ |
292 | #if defined (__GNUC__) && defined (__cplusplus) |
293 | #define G_STRFUNC ((const char*) (__PRETTY_FUNCTION__)) |
294 | #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L |
295 | #define G_STRFUNC ((const char*) (__func__)) |
296 | #elif defined (__GNUC__) || (defined(_MSC_VER) && (_MSC_VER > 1300)) |
297 | #define G_STRFUNC ((const char*) (__FUNCTION__)) |
298 | #else |
299 | #define G_STRFUNC ((const char*) ("???")) |
300 | #endif |
301 | |
302 | /* Guard C code in headers, while including them from C++ */ |
303 | #ifdef __cplusplus |
304 | #define G_BEGIN_DECLS extern "C" { |
305 | #define G_END_DECLS } |
306 | #else |
307 | #define G_BEGIN_DECLS |
308 | #define G_END_DECLS |
309 | #endif |
310 | |
311 | /* Provide definitions for some commonly used macros. |
312 | * Some of them are only provided if they haven't already |
313 | * been defined. It is assumed that if they are already |
314 | * defined then the current definition is correct. |
315 | */ |
316 | #ifndef NULL |
317 | # ifdef __cplusplus |
318 | # define NULL (0L) |
319 | # else /* !__cplusplus */ |
320 | # define NULL ((void*) 0) |
321 | # endif /* !__cplusplus */ |
322 | #endif |
323 | |
324 | #ifndef FALSE |
325 | #define FALSE (0) |
326 | #endif |
327 | |
328 | #ifndef TRUE |
329 | #define TRUE (!FALSE) |
330 | #endif |
331 | |
332 | #undef MAX |
333 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) |
334 | |
335 | #undef MIN |
336 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
337 | |
338 | #undef ABS |
339 | #define ABS(a) (((a) < 0) ? -(a) : (a)) |
340 | |
341 | #undef CLAMP |
342 | #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) |
343 | |
344 | #define G_APPROX_VALUE(a, b, epsilon) \ |
345 | (((a) > (b) ? (a) - (b) : (b) - (a)) < (epsilon)) |
346 | |
347 | /* Count the number of elements in an array. The array must be defined |
348 | * as such; using this with a dynamically allocated array will give |
349 | * incorrect results. |
350 | */ |
351 | #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0])) |
352 | |
353 | /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT |
354 | */ |
355 | #define GPOINTER_TO_SIZE(p) ((gsize) (p)) |
356 | #define GSIZE_TO_POINTER(s) ((gpointer) (gsize) (s)) |
357 | |
358 | /* Provide convenience macros for handling structure |
359 | * fields through their offsets. |
360 | */ |
361 | |
362 | #if (defined(__GNUC__) && __GNUC__ >= 4) || defined (_MSC_VER) |
363 | #define G_STRUCT_OFFSET(struct_type, member) \ |
364 | ((glong) offsetof (struct_type, member)) |
365 | #else |
366 | #define G_STRUCT_OFFSET(struct_type, member) \ |
367 | ((glong) ((guint8*) &((struct_type*) 0)->member)) |
368 | #endif |
369 | |
370 | #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \ |
371 | ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset))) |
372 | #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \ |
373 | (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset))) |
374 | |
375 | /* Provide simple macro statement wrappers: |
376 | * G_STMT_START { statements; } G_STMT_END; |
377 | * This can be used as a single statement, like: |
378 | * if (x) G_STMT_START { ... } G_STMT_END; else ... |
379 | * This intentionally does not use compiler extensions like GCC's '({...})' to |
380 | * avoid portability issue or side effects when compiled with different compilers. |
381 | * MSVC complains about "while(0)": C4127: "Conditional expression is constant", |
382 | * so we use __pragma to avoid the warning since the use here is intentional. |
383 | */ |
384 | #if !(defined (G_STMT_START) && defined (G_STMT_END)) |
385 | #define G_STMT_START do |
386 | #if defined (_MSC_VER) && (_MSC_VER >= 1500) |
387 | #define G_STMT_END \ |
388 | __pragma(warning(push)) \ |
389 | __pragma(warning(disable:4127)) \ |
390 | while(0) \ |
391 | __pragma(warning(pop)) |
392 | #else |
393 | #define G_STMT_END while (0) |
394 | #endif |
395 | #endif |
396 | |
397 | /* Deprecated -- do not use. */ |
398 | #ifndef G_DISABLE_DEPRECATED |
399 | #ifdef G_DISABLE_CONST_RETURNS |
400 | #define G_CONST_RETURN |
401 | #else |
402 | #define G_CONST_RETURN const |
403 | #endif |
404 | #endif |
405 | |
406 | /* |
407 | * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to |
408 | * the compiler about the expected result of an expression. Some compilers |
409 | * can use this information for optimizations. |
410 | * |
411 | * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when |
412 | * putting assignments in g_return_if_fail (). |
413 | */ |
414 | #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) |
415 | #define _G_BOOLEAN_EXPR(expr) \ |
416 | G_GNUC_EXTENSION ({ \ |
417 | int _g_boolean_var_; \ |
418 | if (expr) \ |
419 | _g_boolean_var_ = 1; \ |
420 | else \ |
421 | _g_boolean_var_ = 0; \ |
422 | _g_boolean_var_; \ |
423 | }) |
424 | #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR((expr)), 1)) |
425 | #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR((expr)), 0)) |
426 | #else |
427 | #define G_LIKELY(expr) (expr) |
428 | #define G_UNLIKELY(expr) (expr) |
429 | #endif |
430 | |
431 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) |
432 | #define G_DEPRECATED __attribute__((__deprecated__)) |
433 | #elif defined(_MSC_VER) && (_MSC_VER >= 1300) |
434 | #define G_DEPRECATED __declspec(deprecated) |
435 | #else |
436 | #define G_DEPRECATED |
437 | #endif |
438 | |
439 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) |
440 | #define G_DEPRECATED_FOR(f) __attribute__((__deprecated__("Use '" #f "' instead"))) |
441 | #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320) |
442 | #define G_DEPRECATED_FOR(f) __declspec(deprecated("is deprecated. Use '" #f "' instead")) |
443 | #else |
444 | #define G_DEPRECATED_FOR(f) G_DEPRECATED |
445 | #endif |
446 | |
447 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) |
448 | #define G_UNAVAILABLE(maj,min) __attribute__((deprecated("Not available before " #maj "." #min))) |
449 | #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320) |
450 | #define G_UNAVAILABLE(maj,min) __declspec(deprecated("is not available before " #maj "." #min)) |
451 | #else |
452 | #define G_UNAVAILABLE(maj,min) G_DEPRECATED |
453 | #endif |
454 | |
455 | #ifndef _GLIB_EXTERN |
456 | #define _GLIB_EXTERN extern |
457 | #endif |
458 | |
459 | /* These macros are used to mark deprecated functions in GLib headers, |
460 | * and thus have to be exposed in installed headers. But please |
461 | * do *not* use them in other projects. Instead, use G_DEPRECATED |
462 | * or define your own wrappers around it. |
463 | */ |
464 | |
465 | #ifdef GLIB_DISABLE_DEPRECATION_WARNINGS |
466 | #define GLIB_DEPRECATED _GLIB_EXTERN |
467 | #define GLIB_DEPRECATED_FOR(f) _GLIB_EXTERN |
468 | #define GLIB_UNAVAILABLE(maj,min) _GLIB_EXTERN |
469 | #else |
470 | #define GLIB_DEPRECATED G_DEPRECATED _GLIB_EXTERN |
471 | #define GLIB_DEPRECATED_FOR(f) G_DEPRECATED_FOR(f) _GLIB_EXTERN |
472 | #define GLIB_UNAVAILABLE(maj,min) G_UNAVAILABLE(maj,min) _GLIB_EXTERN |
473 | #endif |
474 | |
475 | #ifndef __GI_SCANNER__ |
476 | |
477 | #ifdef __GNUC__ |
478 | |
479 | /* these macros are private */ |
480 | #define _GLIB_AUTOPTR_FUNC_NAME(TypeName) glib_autoptr_cleanup_##TypeName |
481 | #define _GLIB_AUTOPTR_TYPENAME(TypeName) TypeName##_autoptr |
482 | #define _GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName) glib_listautoptr_cleanup_##TypeName |
483 | #define _GLIB_AUTOPTR_LIST_TYPENAME(TypeName) TypeName##_listautoptr |
484 | #define _GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName) glib_slistautoptr_cleanup_##TypeName |
485 | #define _GLIB_AUTOPTR_SLIST_TYPENAME(TypeName) TypeName##_slistautoptr |
486 | #define _GLIB_AUTO_FUNC_NAME(TypeName) glib_auto_cleanup_##TypeName |
487 | #define _GLIB_CLEANUP(func) __attribute__((cleanup(func))) |
488 | #define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName) \ |
489 | typedef ModuleObjName *_GLIB_AUTOPTR_TYPENAME(ModuleObjName); \ |
490 | static inline void _GLIB_AUTOPTR_FUNC_NAME(ModuleObjName) (ModuleObjName **_ptr) { \ |
491 | _GLIB_AUTOPTR_FUNC_NAME(ParentName) ((ParentName **) _ptr); } \ |
492 | |
493 | |
494 | /* these macros are API */ |
495 | #define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) \ |
496 | typedef TypeName *_GLIB_AUTOPTR_TYPENAME(TypeName); \ |
497 | typedef GList *_GLIB_AUTOPTR_LIST_TYPENAME(TypeName); \ |
498 | typedef GSList *_GLIB_AUTOPTR_SLIST_TYPENAME(TypeName); \ |
499 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ |
500 | static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_FUNC_NAME(TypeName) (TypeName **_ptr) { if (*_ptr) (func) (*_ptr); } \ |
501 | static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName) (GList **_l) { g_list_free_full (*_l, (GDestroyNotify) (void(*)(void)) func); } \ |
502 | static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName) (GSList **_l) { g_slist_free_full (*_l, (GDestroyNotify) (void(*)(void)) func); } \ |
503 | G_GNUC_END_IGNORE_DEPRECATIONS |
504 | #define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func) \ |
505 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ |
506 | static inline void _GLIB_AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { (func) (_ptr); } \ |
507 | G_GNUC_END_IGNORE_DEPRECATIONS |
508 | #define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none) \ |
509 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ |
510 | static inline void _GLIB_AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { if (*_ptr != none) (func) (*_ptr); } \ |
511 | G_GNUC_END_IGNORE_DEPRECATIONS |
512 | #define g_autoptr(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_TYPENAME(TypeName) |
513 | #define g_autolist(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_LIST_TYPENAME(TypeName) |
514 | #define g_autoslist(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_SLIST_TYPENAME(TypeName) |
515 | #define g_auto(TypeName) _GLIB_CLEANUP(_GLIB_AUTO_FUNC_NAME(TypeName)) TypeName |
516 | #define g_autofree _GLIB_CLEANUP(g_autoptr_cleanup_generic_gfree) |
517 | |
518 | #else /* not GNU C */ |
519 | /* this (dummy) macro is private */ |
520 | #define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName) |
521 | |
522 | /* these (dummy) macros are API */ |
523 | #define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) |
524 | #define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func) |
525 | #define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none) |
526 | |
527 | /* no declaration of g_auto() or g_autoptr() here */ |
528 | #endif /* __GNUC__ */ |
529 | |
530 | #else |
531 | |
532 | #define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName) |
533 | |
534 | #define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) |
535 | #define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func) |
536 | #define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none) |
537 | |
538 | #endif /* __GI_SCANNER__ */ |
539 | |
540 | #endif /* __G_MACROS_H__ */ |
541 | |