1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 2006-2019 Free Software Foundation, Inc.
3 *
4 * This file is not part of the GNU gettext program, but is used with
5 * GNU gettext.
6 *
7 * The original copyright notice is as follows:
8 */
9
10 /* GLIB - Library of useful routines for C programming
11 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the
25 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 * Boston, MA 02111-1307, USA.
27 */
28
29 /*
30 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
31 * file for a list of people on the GLib Team. See the ChangeLog
32 * files for a list of changes. These files are distributed with
33 * GLib at ftp://ftp.gtk.org/pub/gtk/.
34 */
35
36 /*
37 * Modified by Bruno Haible for use as a gnulib module.
38 */
39
40 /* This file must not include any other glib header file and must thus
41 * not refer to variables from glibconfig.h
42 */
43
44 #ifndef __G_MACROS_H__
45 #define __G_MACROS_H__
46
47 /* We include stddef.h to get the system's definition of NULL
48 */
49 #include <stddef.h>
50
51 /* Get LONG_MAX.
52 */
53 #include <limits.h>
54
55 #if 0
56
57 /* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
58 * where this is valid. This allows for warningless compilation of
59 * "long long" types even in the presence of '-ansi -pedantic'.
60 */
61 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
62 # define G_GNUC_EXTENSION __extension__
63 #else
64 # define G_GNUC_EXTENSION
65 #endif
66
67 /* Provide macros to feature the GCC function attribute.
68 */
69 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
70 #define G_GNUC_PURE \
71 __attribute__((__pure__))
72 #define G_GNUC_MALLOC \
73 __attribute__((__malloc__))
74 #else
75 #define G_GNUC_PURE
76 #define G_GNUC_MALLOC
77 #endif
78
79 #if __GNUC__ >= 4
80 #define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
81 #else
82 #define G_GNUC_NULL_TERMINATED
83 #endif
84
85 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
86 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
87 __attribute__((__format__ (__printf__, format_idx, arg_idx)))
88 #define G_GNUC_SCANF( format_idx, arg_idx ) \
89 __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
90 #define G_GNUC_FORMAT( arg_idx ) \
91 __attribute__((__format_arg__ (arg_idx)))
92 #define G_GNUC_NORETURN \
93 __attribute__((__noreturn__))
94 #define G_GNUC_CONST \
95 __attribute__((__const__))
96 #define G_GNUC_UNUSED \
97 __attribute__((__unused__))
98 #define G_GNUC_NO_INSTRUMENT \
99 __attribute__((__no_instrument_function__))
100 #else /* !__GNUC__ */
101 #define G_GNUC_PRINTF( format_idx, arg_idx )
102 #define G_GNUC_SCANF( format_idx, arg_idx )
103 #define G_GNUC_FORMAT( arg_idx )
104 #define G_GNUC_NORETURN
105 #define G_GNUC_CONST
106 #define G_GNUC_UNUSED
107 #define G_GNUC_NO_INSTRUMENT
108 #endif /* !__GNUC__ */
109
110 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
111 #define G_GNUC_DEPRECATED \
112 __attribute__((__deprecated__))
113 #else
114 #define G_GNUC_DEPRECATED
115 #endif /* __GNUC__ */
116
117 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
118 #define G_GNUC_WARN_UNUSED_RESULT \
119 __attribute__((warn_unused_result))
120 #else
121 #define G_GNUC_WARN_UNUSED_RESULT
122 #endif /* __GNUC__ */
123
124 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
125 * macros, so we can refer to them as strings unconditionally.
126 * usage not-recommended since gcc-3.0
127 */
128 #if defined (__GNUC__) && (__GNUC__ < 3)
129 #define G_GNUC_FUNCTION __FUNCTION__
130 #define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
131 #else /* !__GNUC__ */
132 #define G_GNUC_FUNCTION ""
133 #define G_GNUC_PRETTY_FUNCTION ""
134 #endif /* !__GNUC__ */
135
136 #define G_STRINGIFY(macro_or_string) G_STRINGIFY_ARG (macro_or_string)
137 #define G_STRINGIFY_ARG(contents) #contents
138
139 /* Provide a string identifying the current code position */
140 #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)
141 # define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
142 #else
143 # define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__)
144 #endif
145
146 /* Provide a string identifying the current function, non-concatenatable */
147 #if defined (__GNUC__)
148 # define G_STRFUNC ((const char*) (__PRETTY_FUNCTION__))
149 #elif defined (G_HAVE_ISO_VARARGS)
150 # define G_STRFUNC ((const char*) (__func__))
151 #else
152 # define G_STRFUNC ((const char*) ("???"))
153 #endif
154
155 /* Guard C code in headers, while including them from C++ */
156 #ifdef __cplusplus
157 # define G_BEGIN_DECLS extern "C" {
158 # define G_END_DECLS }
159 #else
160 # define G_BEGIN_DECLS
161 # define G_END_DECLS
162 #endif
163
164 /* Provide definitions for some commonly used macros.
165 * Some of them are only provided if they haven't already
166 * been defined. It is assumed that if they are already
167 * defined then the current definition is correct.
168 */
169 #ifndef NULL
170 # ifdef __cplusplus
171 # define NULL (0L)
172 # else /* !__cplusplus */
173 # define NULL ((void*) 0)
174 # endif /* !__cplusplus */
175 #endif
176
177 #ifndef FALSE
178 #define FALSE (0)
179 #endif
180
181 #ifndef TRUE
182 #define TRUE (!FALSE)
183 #endif
184
185 #undef MAX
186 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
187
188 #undef MIN
189 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
190
191 #undef ABS
192 #define ABS(a) (((a) < 0) ? -(a) : (a))
193
194 #undef CLAMP
195 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
196
197 #endif
198
199 /* Count the number of elements in an array. The array must be defined
200 * as such; using this with a dynamically allocated array will give
201 * incorrect results.
202 */
203 #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
204
205 #if 0
206
207 /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT
208 */
209 #define GPOINTER_TO_SIZE(p) ((gsize) (p))
210 #define GSIZE_TO_POINTER(s) ((gpointer) (gsize) (s))
211
212 /* Provide convenience macros for handling structure
213 * fields through their offsets.
214 */
215 #define G_STRUCT_OFFSET(struct_type, member) \
216 ((glong) ((guint8*) &((struct_type*) 0)->member))
217 #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
218 ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
219 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
220 (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
221
222 /* Provide simple macro statement wrappers (adapted from Perl):
223 * G_STMT_START { statements; } G_STMT_END;
224 * can be used as a single statement, as in
225 * if (x) G_STMT_START { ... } G_STMT_END; else ...
226 *
227 * When GCC is compiling C code in non-ANSI mode, it will use the
228 * compiler __extension__ to wrap the statements wihin `({' and '})' braces.
229 * When compiling on platforms where configure has defined
230 * HAVE_DOWHILE_MACROS, statements will be wrapped with `do' and `while (0)'.
231 * For any other platforms (SunOS4 is known to have this issue), wrap the
232 * statements with `if (1)' and `else (void) 0'.
233 */
234 #if !(defined (G_STMT_START) && defined (G_STMT_END))
235 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
236 # define G_STMT_START (void) __extension__ (
237 # define G_STMT_END )
238 # else /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
239 # if defined (HAVE_DOWHILE_MACROS)
240 # define G_STMT_START do
241 # define G_STMT_END while (0)
242 # else /* !HAVE_DOWHILE_MACROS */
243 # define G_STMT_START if (1)
244 # define G_STMT_END else (void) 0
245 # endif /* !HAVE_DOWHILE_MACROS */
246 # endif /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
247 #endif
248
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.
252 */
253 #ifdef G_DISABLE_CONST_RETURNS
254 #define G_CONST_RETURN
255 #else
256 #define G_CONST_RETURN const
257 #endif
258
259 /*
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.
263 *
264 * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
265 * putting assignments in g_return_if_fail ().
266 */
267 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
268 #define _G_BOOLEAN_EXPR(expr) \
269 __extension__ ({ \
270 int _g_boolean_var_; \
271 if (expr) \
272 _g_boolean_var_ = 1; \
273 else \
274 _g_boolean_var_ = 0; \
275 _g_boolean_var_; \
276 })
277 #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))
278 #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))
279 #else
280 #define G_LIKELY(expr) (expr)
281 #define G_UNLIKELY(expr) (expr)
282 #endif
283
284 #endif
285
286 /* Backport from glib-2.18.4.
287 */
288 #define G_MAXLONG LONG_MAX
289
290 #endif /* __G_MACROS_H__ */