1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 /* This file must not include any other glib header file and must thus
28 * not refer to variables from glibconfig.h
31 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
32 #error "Only <glib.h> can be included directly."
35 #ifndef __G_MACROS_H__
36 #define __G_MACROS_H__
38 /* We include stddef.h to get the system's definition of NULL
42 /* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
43 * where this is valid. This allows for warningless compilation of
44 * "long long" types even in the presence of '-ansi -pedantic'.
46 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
47 # define G_GNUC_EXTENSION __extension__
49 # define G_GNUC_EXTENSION
52 /* Provide macros to feature the GCC function attribute.
54 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
56 __attribute__((__pure__))
57 #define G_GNUC_MALLOC \
58 __attribute__((__malloc__))
65 #define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
67 #define G_GNUC_NULL_TERMINATED
70 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
71 #define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x)))
72 #define G_GNUC_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y)))
74 #define G_GNUC_ALLOC_SIZE(x)
75 #define G_GNUC_ALLOC_SIZE2(x,y)
78 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
79 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
80 __attribute__((__format__ (__printf__, format_idx, arg_idx)))
81 #define G_GNUC_SCANF( format_idx, arg_idx ) \
82 __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
83 #define G_GNUC_FORMAT( arg_idx ) \
84 __attribute__((__format_arg__ (arg_idx)))
85 #define G_GNUC_NORETURN \
86 __attribute__((__noreturn__))
87 #define G_GNUC_CONST \
88 __attribute__((__const__))
89 #define G_GNUC_UNUSED \
90 __attribute__((__unused__))
91 #define G_GNUC_NO_INSTRUMENT \
92 __attribute__((__no_instrument_function__))
94 #define G_GNUC_PRINTF( format_idx, arg_idx )
95 #define G_GNUC_SCANF( format_idx, arg_idx )
96 #define G_GNUC_FORMAT( arg_idx )
97 #define G_GNUC_NORETURN
100 #define G_GNUC_NO_INSTRUMENT
101 #endif /* !__GNUC__ */
103 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
104 #define G_GNUC_DEPRECATED \
105 __attribute__((__deprecated__))
107 #define G_GNUC_DEPRECATED
108 #endif /* __GNUC__ */
110 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
111 #define G_GNUC_DEPRECATED_FOR(f) \
112 __attribute__((deprecated("Use " #f " instead")))
114 #define G_GNUC_DEPRECATED_FOR(f) G_GNUC_DEPRECATED
115 #endif /* __GNUC__ */
117 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
118 # define G_GNUC_MAY_ALIAS __attribute__((may_alias))
120 # define G_GNUC_MAY_ALIAS
123 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
124 #define G_GNUC_WARN_UNUSED_RESULT \
125 __attribute__((warn_unused_result))
127 #define G_GNUC_WARN_UNUSED_RESULT
128 #endif /* __GNUC__ */
130 #ifndef G_DISABLE_DEPRECATED
131 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
132 * macros, so we can refer to them as strings unconditionally.
133 * usage not-recommended since gcc-3.0
135 #if defined (__GNUC__) && (__GNUC__ < 3)
136 #define G_GNUC_FUNCTION __FUNCTION__
137 #define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
138 #else /* !__GNUC__ */
139 #define G_GNUC_FUNCTION ""
140 #define G_GNUC_PRETTY_FUNCTION ""
141 #endif /* !__GNUC__ */
142 #endif /* !G_DISABLE_DEPRECATED */
144 #define G_STRINGIFY(macro_or_string) G_STRINGIFY_ARG (macro_or_string)
145 #define G_STRINGIFY_ARG(contents) #contents
147 #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
148 #define G_PASTE(identifier1,identifier2) G_PASTE_ARGS (identifier1, identifier2)
149 #define G_STATIC_ASSERT(expr) typedef struct { char Compile_Time_Assertion[(expr) ? 1 : -1]; } G_PASTE (_GStaticAssert_, __LINE__)
151 /* Provide a string identifying the current code position */
152 #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)
153 # define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
155 # define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__)
158 /* Provide a string identifying the current function, non-concatenatable */
159 #if defined (__GNUC__)
160 # define G_STRFUNC ((const char*) (__PRETTY_FUNCTION__))
161 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 19901L
162 # define G_STRFUNC ((const char*) (__func__))
164 # define G_STRFUNC ((const char*) ("???"))
167 /* Guard C code in headers, while including them from C++ */
169 # define G_BEGIN_DECLS extern "C" {
170 # define G_END_DECLS }
172 # define G_BEGIN_DECLS
176 /* Provide definitions for some commonly used macros.
177 * Some of them are only provided if they haven't already
178 * been defined. It is assumed that if they are already
179 * defined then the current definition is correct.
184 # else /* !__cplusplus */
185 # define NULL ((void*) 0)
186 # endif /* !__cplusplus */
194 #define TRUE (!FALSE)
198 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
201 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
204 #define ABS(a) (((a) < 0) ? -(a) : (a))
207 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
209 /* Count the number of elements in an array. The array must be defined
210 * as such; using this with a dynamically allocated array will give
213 #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
215 /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT
217 #define GPOINTER_TO_SIZE(p) ((gsize) (p))
218 #define GSIZE_TO_POINTER(s) ((gpointer) (gsize) (s))
220 /* Provide convenience macros for handling structure
221 * fields through their offsets.
224 #if defined(__GNUC__) && __GNUC__ >= 4
225 # define G_STRUCT_OFFSET(struct_type, member) \
226 ((glong) offsetof (struct_type, member))
228 # define G_STRUCT_OFFSET(struct_type, member) \
229 ((glong) ((guint8*) &((struct_type*) 0)->member))
232 #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
233 ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
234 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
235 (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
237 /* Provide simple macro statement wrappers:
238 * G_STMT_START { statements; } G_STMT_END;
239 * This can be used as a single statement, like:
240 * if (x) G_STMT_START { ... } G_STMT_END; else ...
241 * This intentionally does not use compiler extensions like GCC's '({...})' to
242 * avoid portability issue or side effects when compiled with different compilers.
244 #if !(defined (G_STMT_START) && defined (G_STMT_END))
245 # define G_STMT_START do
246 # define G_STMT_END while (0)
249 /* Allow the app programmer to select whether or not return values
250 * (usually char*) are const or not. Don't try using this feature for
251 * functions with C++ linkage.
253 #ifdef G_DISABLE_CONST_RETURNS
254 #define G_CONST_RETURN
256 #define G_CONST_RETURN const
260 * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to
261 * the compiler about the expected result of an expression. Some compilers
262 * can use this information for optimizations.
264 * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
265 * putting assignments in g_return_if_fail ().
267 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
268 #define _G_BOOLEAN_EXPR(expr) \
270 int _g_boolean_var_; \
272 _g_boolean_var_ = 1; \
274 _g_boolean_var_ = 0; \
277 #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))
278 #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))
280 #define G_LIKELY(expr) (expr)
281 #define G_UNLIKELY(expr) (expr)
284 #endif /* __G_MACROS_H__ */