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