make it possible to disable single-file includes by defining
[platform/upstream/glib.git] / glib / gmacros.h
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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
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/.
25  */
26
27 /* This file must not include any other glib header file and must thus
28  * not refer to variables from glibconfig.h
29  */
30
31 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__G_LIB_H__) && !defined (GLIB_COMPILATION)
32 #error "Only <glib.h> can be included directly."
33 #endif
34
35 #ifndef __G_MACROS_H__
36 #define __G_MACROS_H__
37
38 /* We include stddef.h to get the system's definition of NULL
39  */
40 #include <stddef.h>
41
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'. 
45  */
46 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
47 #  define G_GNUC_EXTENSION __extension__
48 #else
49 #  define G_GNUC_EXTENSION
50 #endif
51
52 /* Provide macros to feature the GCC function attribute.
53  */
54 #if    __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
55 #define G_GNUC_PURE                            \
56   __attribute__((__pure__))
57 #define G_GNUC_MALLOC                           \
58   __attribute__((__malloc__))
59 #else
60 #define G_GNUC_PURE
61 #define G_GNUC_MALLOC
62 #endif
63
64 #if     __GNUC__ >= 4
65 #define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
66 #else
67 #define G_GNUC_NULL_TERMINATED
68 #endif
69
70 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
71 #define G_GNUC_PRINTF( format_idx, arg_idx )    \
72   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
73 #define G_GNUC_SCANF( format_idx, arg_idx )     \
74   __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
75 #define G_GNUC_FORMAT( arg_idx )                \
76   __attribute__((__format_arg__ (arg_idx)))
77 #define G_GNUC_NORETURN                         \
78   __attribute__((__noreturn__))
79 #define G_GNUC_CONST                            \
80   __attribute__((__const__))
81 #define G_GNUC_UNUSED                           \
82   __attribute__((__unused__))
83 #define G_GNUC_NO_INSTRUMENT                    \
84   __attribute__((__no_instrument_function__))
85 #else   /* !__GNUC__ */
86 #define G_GNUC_PRINTF( format_idx, arg_idx )
87 #define G_GNUC_SCANF( format_idx, arg_idx )
88 #define G_GNUC_FORMAT( arg_idx )
89 #define G_GNUC_NORETURN
90 #define G_GNUC_CONST
91 #define G_GNUC_UNUSED
92 #define G_GNUC_NO_INSTRUMENT
93 #endif  /* !__GNUC__ */
94
95 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
96 #define G_GNUC_DEPRECATED                            \
97   __attribute__((__deprecated__))
98 #else
99 #define G_GNUC_DEPRECATED
100 #endif /* __GNUC__ */
101
102 #if     __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
103 #  define G_GNUC_MAY_ALIAS __attribute__((may_alias))
104 #else
105 #  define G_GNUC_MAY_ALIAS
106 #endif
107
108 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
109 #define G_GNUC_WARN_UNUSED_RESULT               \
110   __attribute__((warn_unused_result))
111 #else
112 #define G_GNUC_WARN_UNUSED_RESULT
113 #endif /* __GNUC__ */
114
115 #ifndef G_DISABLE_DEPRECATED
116 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
117  * macros, so we can refer to them as strings unconditionally.
118  * usage not-recommended since gcc-3.0
119  */
120 #if defined (__GNUC__) && (__GNUC__ < 3)
121 #define G_GNUC_FUNCTION         __FUNCTION__
122 #define G_GNUC_PRETTY_FUNCTION  __PRETTY_FUNCTION__
123 #else   /* !__GNUC__ */
124 #define G_GNUC_FUNCTION         ""
125 #define G_GNUC_PRETTY_FUNCTION  ""
126 #endif  /* !__GNUC__ */
127 #endif  /* !G_DISABLE_DEPRECATED */
128
129 #define G_STRINGIFY(macro_or_string)    G_STRINGIFY_ARG (macro_or_string)
130 #define G_STRINGIFY_ARG(contents)       #contents
131
132 /* Provide a string identifying the current code position */
133 #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)
134 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
135 #else
136 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__)
137 #endif
138
139 /* Provide a string identifying the current function, non-concatenatable */
140 #if defined (__GNUC__)
141 #  define G_STRFUNC     ((const char*) (__PRETTY_FUNCTION__))
142 #elif defined (G_HAVE_ISO_VARARGS)
143 #  define G_STRFUNC     ((const char*) (__func__))
144 #else
145 #  define G_STRFUNC     ((const char*) ("???"))
146 #endif
147
148 /* Guard C code in headers, while including them from C++ */
149 #ifdef  __cplusplus
150 # define G_BEGIN_DECLS  extern "C" {
151 # define G_END_DECLS    }
152 #else
153 # define G_BEGIN_DECLS
154 # define G_END_DECLS
155 #endif
156
157 /* Provide definitions for some commonly used macros.
158  *  Some of them are only provided if they haven't already
159  *  been defined. It is assumed that if they are already
160  *  defined then the current definition is correct.
161  */
162 #ifndef NULL
163 #  ifdef __cplusplus
164 #    define NULL        (0L)
165 #  else /* !__cplusplus */
166 #    define NULL        ((void*) 0)
167 #  endif /* !__cplusplus */
168 #endif
169
170 #ifndef FALSE
171 #define FALSE   (0)
172 #endif
173
174 #ifndef TRUE
175 #define TRUE    (!FALSE)
176 #endif
177
178 #undef  MAX
179 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
180
181 #undef  MIN
182 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
183
184 #undef  ABS
185 #define ABS(a)     (((a) < 0) ? -(a) : (a))
186
187 #undef  CLAMP
188 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
189
190 /* Count the number of elements in an array. The array must be defined
191  * as such; using this with a dynamically allocated array will give
192  * incorrect results.
193  */
194 #define G_N_ELEMENTS(arr)               (sizeof (arr) / sizeof ((arr)[0]))
195
196 /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT
197  */
198 #define GPOINTER_TO_SIZE(p)     ((gsize) (p))
199 #define GSIZE_TO_POINTER(s)     ((gpointer) (gsize) (s))
200
201 /* Provide convenience macros for handling structure
202  * fields through their offsets.
203  */
204 #define G_STRUCT_OFFSET(struct_type, member)    \
205     ((glong) ((guint8*) &((struct_type*) 0)->member))
206 #define G_STRUCT_MEMBER_P(struct_p, struct_offset)   \
207     ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
208 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset)   \
209     (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
210
211 /* Provide simple macro statement wrappers (adapted from Perl):
212  *  G_STMT_START { statements; } G_STMT_END;
213  *  can be used as a single statement, as in
214  *  if (x) G_STMT_START { ... } G_STMT_END; else ...
215  *
216  *  When GCC is compiling C code in non-ANSI mode, it will use the
217  *  compiler __extension__ to wrap the statements wihin `({' and '})' braces.
218  *  When compiling on platforms where configure has defined
219  *  HAVE_DOWHILE_MACROS, statements will be wrapped with `do' and `while (0)'.
220  *  For any other platforms (SunOS4 is known to have this issue), wrap the
221  *  statements with `if (1)' and `else (void) 0'.
222  */
223 #if !(defined (G_STMT_START) && defined (G_STMT_END))
224 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
225 #  define G_STMT_START (void) __extension__ (
226 #  define G_STMT_END )
227 # else /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
228 #  if defined (HAVE_DOWHILE_MACROS)
229 #   define G_STMT_START do
230 #   define G_STMT_END while (0)
231 #  else /* !HAVE_DOWHILE_MACROS */
232 #   define G_STMT_START if (1)
233 #   define G_STMT_END else (void) 0
234 #  endif /* !HAVE_DOWHILE_MACROS */
235 # endif /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
236 #endif
237
238 /* Allow the app programmer to select whether or not return values
239  * (usually char*) are const or not.  Don't try using this feature for
240  * functions with C++ linkage.
241  */
242 #ifdef G_DISABLE_CONST_RETURNS
243 #define G_CONST_RETURN
244 #else
245 #define G_CONST_RETURN const
246 #endif
247
248 /*
249  * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to 
250  * the compiler about the expected result of an expression. Some compilers
251  * can use this information for optimizations.
252  *
253  * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
254  * putting assignments in g_return_if_fail ().  
255  */
256 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
257 #define _G_BOOLEAN_EXPR(expr)                   \
258  __extension__ ({                               \
259    int _g_boolean_var_;                         \
260    if (expr)                                    \
261       _g_boolean_var_ = 1;                      \
262    else                                         \
263       _g_boolean_var_ = 0;                      \
264    _g_boolean_var_;                             \
265 })
266 #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))
267 #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))
268 #else
269 #define G_LIKELY(expr) (expr)
270 #define G_UNLIKELY(expr) (expr)
271 #endif
272
273 #endif /* __G_MACROS_H__ */