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