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 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#define G_GNUC_CHECK_VERSION(major, minor) \
41 (defined(__GNUC__) && \
42 ((__GNUC__ > (major)) || \
43 ((__GNUC__ == (major)) && \
44 (__GNUC_MINOR__ >= (minor)))))
45
46/* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
47 * where this is valid. This allows for warningless compilation of
48 * "long long" types even in the presence of '-ansi -pedantic'.
49 */
50#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
51#define G_GNUC_EXTENSION __extension__
52#else
53#define G_GNUC_EXTENSION
54#endif
55
56/* Every compiler that we target supports inlining, but some of them may
57 * complain about it if we don't say "__inline". If we have C99, or if
58 * we are using C++, then we can use "inline" directly. Unfortunately
59 * Visual Studio does not support __STDC_VERSION__, so we need to check
60 * whether we are on Visual Studio 2013 or earlier to see that we need to
61 * say "__inline" in C mode.
62 * Otherwise, we say "__inline" to avoid the warning.
63 */
64#define G_CAN_INLINE
65#ifndef __cplusplus
66# ifdef _MSC_VER
67# if (_MSC_VER < 1900)
68# define G_INLINE_DEFINE_NEEDED
69# endif
70# elif !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199900)
71# define G_INLINE_DEFINE_NEEDED
72# endif
73#endif
74
75#ifdef G_INLINE_DEFINE_NEEDED
76# undef inline
77# define inline __inline
78#endif
79
80#undef G_INLINE_DEFINE_NEEDED
81
82/* For historical reasons we need to continue to support those who
83 * define G_IMPLEMENT_INLINES to mean "don't implement this here".
84 */
85#ifdef G_IMPLEMENT_INLINES
86# define G_INLINE_FUNC extern
87# undef G_CAN_INLINE
88#else
89# define G_INLINE_FUNC static inline
90#endif /* G_IMPLEMENT_INLINES */
91
92/* Provide macros to feature the GCC function attribute.
93 */
94#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
95#define G_GNUC_PURE __attribute__((__pure__))
96#define G_GNUC_MALLOC __attribute__((__malloc__))
97#else
98#define G_GNUC_PURE
99#define G_GNUC_MALLOC
100#endif
101
102#if __GNUC__ >= 4
103#define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
104#else
105#define G_GNUC_NULL_TERMINATED
106#endif
107
108/* Clang feature detection: http://clang.llvm.org/docs/LanguageExtensions.html */
109#ifndef __has_attribute
110#define __has_attribute(x) 0
111#endif
112
113#ifndef __has_feature
114#define __has_feature(x) 0
115#endif
116
117#ifndef __has_builtin
118#define __has_builtin(x) 0
119#endif
120
121#if (!defined(__clang__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) || \
122 (defined(__clang__) && __has_attribute(__alloc_size__))
123#define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x)))
124#define G_GNUC_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y)))
125#else
126#define G_GNUC_ALLOC_SIZE(x)
127#define G_GNUC_ALLOC_SIZE2(x,y)
128#endif
129
130#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
131#define G_GNUC_PRINTF( format_idx, arg_idx ) \
132 __attribute__((__format__ (__printf__, format_idx, arg_idx)))
133#define G_GNUC_SCANF( format_idx, arg_idx ) \
134 __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
135#define G_GNUC_FORMAT( arg_idx ) \
136 __attribute__((__format_arg__ (arg_idx)))
137#define G_GNUC_NORETURN \
138 __attribute__((__noreturn__))
139#define G_GNUC_CONST \
140 __attribute__((__const__))
141#define G_GNUC_UNUSED \
142 __attribute__((__unused__))
143#define G_GNUC_NO_INSTRUMENT \
144 __attribute__((__no_instrument_function__))
145#else /* !__GNUC__ */
146#define G_GNUC_PRINTF( format_idx, arg_idx )
147#define G_GNUC_SCANF( format_idx, arg_idx )
148#define G_GNUC_FORMAT( arg_idx )
149#define G_GNUC_NORETURN
150#define G_GNUC_CONST
151#define G_GNUC_UNUSED
152#define G_GNUC_NO_INSTRUMENT
153#endif /* !__GNUC__ */
154
155#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
156#define G_GNUC_DEPRECATED __attribute__((__deprecated__))
157#else
158#define G_GNUC_DEPRECATED
159#endif /* __GNUC__ */
160
161#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
162#define G_GNUC_DEPRECATED_FOR(f) \
163 __attribute__((deprecated("Use " #f " instead")))
164#else
165#define G_GNUC_DEPRECATED_FOR(f) G_GNUC_DEPRECATED
166#endif /* __GNUC__ */
167
168#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
169#define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
170 _Pragma ("GCC diagnostic push") \
171 _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
172#define G_GNUC_END_IGNORE_DEPRECATIONS \
173 _Pragma ("GCC diagnostic pop")
174#elif defined (_MSC_VER) && (_MSC_VER >= 1500)
175#define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
176 __pragma (warning (push)) \
177 __pragma (warning (disable : 4996))
178#define G_GNUC_END_IGNORE_DEPRECATIONS \
179 __pragma (warning (pop))
180#elif defined (__clang__)
181#define G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
182 _Pragma("clang diagnostic push") \
183 _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
184#define G_GNUC_END_IGNORE_DEPRECATIONS \
185 _Pragma("clang diagnostic pop")
186#else
187#define G_GNUC_BEGIN_IGNORE_DEPRECATIONS
188#define G_GNUC_END_IGNORE_DEPRECATIONS
189#endif
190
191#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
192#define G_GNUC_MAY_ALIAS __attribute__((may_alias))
193#else
194#define G_GNUC_MAY_ALIAS
195#endif
196
197#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
198#define G_GNUC_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
199#else
200#define G_GNUC_WARN_UNUSED_RESULT
201#endif /* __GNUC__ */
202
203#ifndef G_DISABLE_DEPRECATED
204/* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
205 * macros, so we can refer to them as strings unconditionally.
206 * usage not-recommended since gcc-3.0
207 */
208#if defined (__GNUC__) && (__GNUC__ < 3)
209#define G_GNUC_FUNCTION __FUNCTION__
210#define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
211#else /* !__GNUC__ */
212#define G_GNUC_FUNCTION ""
213#define G_GNUC_PRETTY_FUNCTION ""
214#endif /* !__GNUC__ */
215#endif /* !G_DISABLE_DEPRECATED */
216
217#if __has_feature(attribute_analyzer_noreturn) && defined(__clang_analyzer__)
218#define G_ANALYZER_ANALYZING 1
219#define G_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
220#else
221#define G_ANALYZER_ANALYZING 0
222#define G_ANALYZER_NORETURN
223#endif
224
225#define G_STRINGIFY(macro_or_string) G_STRINGIFY_ARG (macro_or_string)
226#define G_STRINGIFY_ARG(contents) #contents
227
228#ifndef __GI_SCANNER__ /* The static assert macro really confuses the introspection parser */
229#define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
230#define G_PASTE(identifier1,identifier2) G_PASTE_ARGS (identifier1, identifier2)
231#ifdef __COUNTER__
232#define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __COUNTER__)[(expr) ? 1 : -1] G_GNUC_UNUSED
233#else
234#define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __LINE__)[(expr) ? 1 : -1] G_GNUC_UNUSED
235#endif
236#define G_STATIC_ASSERT_EXPR(expr) ((void) sizeof (char[(expr) ? 1 : -1]))
237#endif
238
239/* Provide a string identifying the current code position */
240#if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)
241#define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
242#else
243#define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__)
244#endif
245
246/* Provide a string identifying the current function, non-concatenatable */
247#if defined (__GNUC__) && defined (__cplusplus)
248#define G_STRFUNC ((const char*) (__PRETTY_FUNCTION__))
249#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
250#define G_STRFUNC ((const char*) (__func__))
251#elif defined (__GNUC__) || (defined(_MSC_VER) && (_MSC_VER > 1300))
252#define G_STRFUNC ((const char*) (__FUNCTION__))
253#else
254#define G_STRFUNC ((const char*) ("???"))
255#endif
256
257/* Guard C code in headers, while including them from C++ */
258#ifdef __cplusplus
259#define G_BEGIN_DECLS extern "C" {
260#define G_END_DECLS }
261#else
262#define G_BEGIN_DECLS
263#define G_END_DECLS
264#endif
265
266/* Provide definitions for some commonly used macros.
267 * Some of them are only provided if they haven't already
268 * been defined. It is assumed that if they are already
269 * defined then the current definition is correct.
270 */
271#ifndef NULL
272# ifdef __cplusplus
273# define NULL (0L)
274# else /* !__cplusplus */
275# define NULL ((void*) 0)
276# endif /* !__cplusplus */
277#endif
278
279#ifndef FALSE
280#define FALSE (0)
281#endif
282
283#ifndef TRUE
284#define TRUE (!FALSE)
285#endif
286
287#undef MAX
288#define MAX(a, b) (((a) > (b)) ? (a) : (b))
289
290#undef MIN
291#define MIN(a, b) (((a) < (b)) ? (a) : (b))
292
293#undef ABS
294#define ABS(a) (((a) < 0) ? -(a) : (a))
295
296#undef CLAMP
297#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
298
299/* Count the number of elements in an array. The array must be defined
300 * as such; using this with a dynamically allocated array will give
301 * incorrect results.
302 */
303#define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
304
305/* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT
306 */
307#define GPOINTER_TO_SIZE(p) ((gsize) (p))
308#define GSIZE_TO_POINTER(s) ((gpointer) (gsize) (s))
309
310/* Provide convenience macros for handling structure
311 * fields through their offsets.
312 */
313
314#if defined(__GNUC__) && __GNUC__ >= 4
315#define G_STRUCT_OFFSET(struct_type, member) \
316 ((glong) offsetof (struct_type, member))
317#else
318#define G_STRUCT_OFFSET(struct_type, member) \
319 ((glong) ((guint8*) &((struct_type*) 0)->member))
320#endif
321
322#define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
323 ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
324#define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
325 (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
326
327/* Provide simple macro statement wrappers:
328 * G_STMT_START { statements; } G_STMT_END;
329 * This can be used as a single statement, like:
330 * if (x) G_STMT_START { ... } G_STMT_END; else ...
331 * This intentionally does not use compiler extensions like GCC's '({...})' to
332 * avoid portability issue or side effects when compiled with different compilers.
333 * MSVC complains about "while(0)": C4127: "Conditional expression is constant",
334 * so we use __pragma to avoid the warning since the use here is intentional.
335 */
336#if !(defined (G_STMT_START) && defined (G_STMT_END))
337#define G_STMT_START do
338#if defined (_MSC_VER) && (_MSC_VER >= 1500)
339#define G_STMT_END \
340 __pragma(warning(push)) \
341 __pragma(warning(disable:4127)) \
342 while(0) \
343 __pragma(warning(pop))
344#else
345#define G_STMT_END while (0)
346#endif
347#endif
348
349/* Deprecated -- do not use. */
350#ifndef G_DISABLE_DEPRECATED
351#ifdef G_DISABLE_CONST_RETURNS
352#define G_CONST_RETURN
353#else
354#define G_CONST_RETURN const
355#endif
356#endif
357
358/*
359 * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to
360 * the compiler about the expected result of an expression. Some compilers
361 * can use this information for optimizations.
362 *
363 * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
364 * putting assignments in g_return_if_fail ().
365 */
366#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
367#define _G_BOOLEAN_EXPR(expr) \
368 G_GNUC_EXTENSION ({ \
369 int _g_boolean_var_; \
370 if (expr) \
371 _g_boolean_var_ = 1; \
372 else \
373 _g_boolean_var_ = 0; \
374 _g_boolean_var_; \
375})
376#define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR((expr)), 1))
377#define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR((expr)), 0))
378#else
379#define G_LIKELY(expr) (expr)
380#define G_UNLIKELY(expr) (expr)
381#endif
382
383#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
384#define G_DEPRECATED __attribute__((__deprecated__))
385#elif defined(_MSC_VER) && (_MSC_VER >= 1300)
386#define G_DEPRECATED __declspec(deprecated)
387#else
388#define G_DEPRECATED
389#endif
390
391#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
392#define G_DEPRECATED_FOR(f) __attribute__((__deprecated__("Use '" #f "' instead")))
393#elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320)
394#define G_DEPRECATED_FOR(f) __declspec(deprecated("is deprecated. Use '" #f "' instead"))
395#else
396#define G_DEPRECATED_FOR(f) G_DEPRECATED
397#endif
398
399#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
400#define G_UNAVAILABLE(maj,min) __attribute__((deprecated("Not available before " #maj "." #min)))
401#elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320)
402#define G_UNAVAILABLE(maj,min) __declspec(deprecated("is not available before " #maj "." #min))
403#else
404#define G_UNAVAILABLE(maj,min) G_DEPRECATED
405#endif
406
407#ifndef _GLIB_EXTERN
408#define _GLIB_EXTERN extern
409#endif
410
411/* These macros are used to mark deprecated functions in GLib headers,
412 * and thus have to be exposed in installed headers. But please
413 * do *not* use them in other projects. Instead, use G_DEPRECATED
414 * or define your own wrappers around it.
415 */
416
417#ifdef GLIB_DISABLE_DEPRECATION_WARNINGS
418#define GLIB_DEPRECATED _GLIB_EXTERN
419#define GLIB_DEPRECATED_FOR(f) _GLIB_EXTERN
420#define GLIB_UNAVAILABLE(maj,min) _GLIB_EXTERN
421#else
422#define GLIB_DEPRECATED G_DEPRECATED _GLIB_EXTERN
423#define GLIB_DEPRECATED_FOR(f) G_DEPRECATED_FOR(f) _GLIB_EXTERN
424#define GLIB_UNAVAILABLE(maj,min) G_UNAVAILABLE(maj,min) _GLIB_EXTERN
425#endif
426
427#ifdef __GNUC__
428
429/* these macros are private */
430#define _GLIB_AUTOPTR_FUNC_NAME(TypeName) glib_autoptr_cleanup_##TypeName
431#define _GLIB_AUTOPTR_TYPENAME(TypeName) TypeName##_autoptr
432#define _GLIB_AUTO_FUNC_NAME(TypeName) glib_auto_cleanup_##TypeName
433#define _GLIB_CLEANUP(func) __attribute__((cleanup(func)))
434#define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName) \
435 typedef ModuleObjName *_GLIB_AUTOPTR_TYPENAME(ModuleObjName); \
436 static inline void _GLIB_AUTOPTR_FUNC_NAME(ModuleObjName) (ModuleObjName **_ptr) { \
437 _GLIB_AUTOPTR_FUNC_NAME(ParentName) ((ParentName **) _ptr); } \
438
439
440/* these macros are API */
441#define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) \
442 typedef TypeName *_GLIB_AUTOPTR_TYPENAME(TypeName); \
443 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
444 static inline void _GLIB_AUTOPTR_FUNC_NAME(TypeName) (TypeName **_ptr) { if (*_ptr) (func) (*_ptr); } \
445 G_GNUC_END_IGNORE_DEPRECATIONS
446#define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func) \
447 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
448 static inline void _GLIB_AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { (func) (_ptr); } \
449 G_GNUC_END_IGNORE_DEPRECATIONS
450#define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none) \
451 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
452 static inline void _GLIB_AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { if (*_ptr != none) (func) (*_ptr); } \
453 G_GNUC_END_IGNORE_DEPRECATIONS
454#define g_autoptr(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_TYPENAME(TypeName)
455#define g_auto(TypeName) _GLIB_CLEANUP(_GLIB_AUTO_FUNC_NAME(TypeName)) TypeName
456#define g_autofree _GLIB_CLEANUP(g_autoptr_cleanup_generic_gfree)
457
458#else /* not GNU C */
459/* this (dummy) macro is private */
460#define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName)
461
462/* these (dummy) macros are API */
463#define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func)
464#define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func)
465#define G_DEFINE_AUTO_CLEANUP_FREE_FUNC(TypeName, func, none)
466
467/* no declaration of g_auto() or g_autoptr() here */
468#endif
469
470#endif /* __G_MACROS_H__ */
471