Navit SVN

/work/compile/navit/src/navit/support/glib/gmacros.h

00001 /* GLIB - Library of useful routines for C programming
00002  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019 
00020 /*
00021  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
00022  * file for a list of people on the GLib Team.  See the ChangeLog
00023  * files for a list of changes.  These files are distributed with
00024  * GLib at ftp://ftp.gtk.org/pub/gtk/.
00025  */
00026 
00027 /* This file must not include any other glib header file and must thus
00028  * not refer to variables from glibconfig.h
00029  */
00030 
00031 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
00032 #error "Only <glib.h> can be included directly."
00033 #endif
00034 
00035 #ifndef __G_MACROS_H__
00036 #define __G_MACROS_H__
00037 
00038 /* We include stddef.h to get the system's definition of NULL
00039  */
00040 #include <stddef.h>
00041 
00042 /* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
00043  * where this is valid. This allows for warningless compilation of
00044  * "long long" types even in the presence of '-ansi -pedantic'. 
00045  */
00046 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
00047 #  define G_GNUC_EXTENSION __extension__
00048 #else
00049 #  define G_GNUC_EXTENSION
00050 #endif
00051 
00052 /* Provide macros to feature the GCC function attribute.
00053  */
00054 #if    __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
00055 #define G_GNUC_PURE                            \
00056   __attribute__((__pure__))
00057 #define G_GNUC_MALLOC                           \
00058   __attribute__((__malloc__))
00059 #else
00060 #define G_GNUC_PURE
00061 #define G_GNUC_MALLOC
00062 #endif
00063 
00064 #if     __GNUC__ >= 4
00065 #define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
00066 #else
00067 #define G_GNUC_NULL_TERMINATED
00068 #endif
00069 
00070 #if     (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
00071 #define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x)))
00072 #define G_GNUC_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y)))
00073 #else
00074 #define G_GNUC_ALLOC_SIZE(x)
00075 #define G_GNUC_ALLOC_SIZE2(x,y)
00076 #endif
00077 
00078 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
00079 #define G_GNUC_PRINTF( format_idx, arg_idx )    \
00080   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
00081 #define G_GNUC_SCANF( format_idx, arg_idx )     \
00082   __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
00083 #define G_GNUC_FORMAT( arg_idx )                \
00084   __attribute__((__format_arg__ (arg_idx)))
00085 #define G_GNUC_NORETURN                         \
00086   __attribute__((__noreturn__))
00087 #define G_GNUC_CONST                            \
00088   __attribute__((__const__))
00089 #define G_GNUC_UNUSED                           \
00090   __attribute__((__unused__))
00091 #define G_GNUC_NO_INSTRUMENT                    \
00092   __attribute__((__no_instrument_function__))
00093 #else   /* !__GNUC__ */
00094 #define G_GNUC_PRINTF( format_idx, arg_idx )
00095 #define G_GNUC_SCANF( format_idx, arg_idx )
00096 #define G_GNUC_FORMAT( arg_idx )
00097 #define G_GNUC_NORETURN
00098 #define G_GNUC_CONST
00099 #define G_GNUC_UNUSED
00100 #define G_GNUC_NO_INSTRUMENT
00101 #endif  /* !__GNUC__ */
00102 
00103 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
00104 #define G_GNUC_DEPRECATED                            \
00105   __attribute__((__deprecated__))
00106 #else
00107 #define G_GNUC_DEPRECATED
00108 #endif /* __GNUC__ */
00109 
00110 #if     __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
00111 #  define G_GNUC_MAY_ALIAS __attribute__((may_alias))
00112 #else
00113 #  define G_GNUC_MAY_ALIAS
00114 #endif
00115 
00116 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
00117 #define G_GNUC_WARN_UNUSED_RESULT               \
00118   __attribute__((warn_unused_result))
00119 #else
00120 #define G_GNUC_WARN_UNUSED_RESULT
00121 #endif /* __GNUC__ */
00122 
00123 #ifndef G_DISABLE_DEPRECATED
00124 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
00125  * macros, so we can refer to them as strings unconditionally.
00126  * usage not-recommended since gcc-3.0
00127  */
00128 #if defined (__GNUC__) && (__GNUC__ < 3)
00129 #define G_GNUC_FUNCTION         __FUNCTION__
00130 #define G_GNUC_PRETTY_FUNCTION  __PRETTY_FUNCTION__
00131 #else   /* !__GNUC__ */
00132 #define G_GNUC_FUNCTION         ""
00133 #define G_GNUC_PRETTY_FUNCTION  ""
00134 #endif  /* !__GNUC__ */
00135 #endif  /* !G_DISABLE_DEPRECATED */
00136 
00137 #define G_STRINGIFY(macro_or_string)    G_STRINGIFY_ARG (macro_or_string)
00138 #define G_STRINGIFY_ARG(contents)       #contents
00139 
00140 /* Provide a string identifying the current code position */
00141 #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)
00142 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
00143 #else
00144 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__)
00145 #endif
00146 
00147 /* Provide a string identifying the current function, non-concatenatable */
00148 #if defined (__GNUC__)
00149 #  define G_STRFUNC     ((const char*) (__PRETTY_FUNCTION__))
00150 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 19901L
00151 #  define G_STRFUNC     ((const char*) (__func__))
00152 #else
00153 #  define G_STRFUNC     ((const char*) ("???"))
00154 #endif
00155 
00156 /* Guard C code in headers, while including them from C++ */
00157 #ifdef  __cplusplus
00158 # define G_BEGIN_DECLS  extern "C" {
00159 # define G_END_DECLS    }
00160 #else
00161 # define G_BEGIN_DECLS
00162 # define G_END_DECLS
00163 #endif
00164 
00165 /* Provide definitions for some commonly used macros.
00166  *  Some of them are only provided if they haven't already
00167  *  been defined. It is assumed that if they are already
00168  *  defined then the current definition is correct.
00169  */
00170 #ifndef NULL
00171 #  ifdef __cplusplus
00172 #    define NULL        (0L)
00173 #  else /* !__cplusplus */
00174 #    define NULL        ((void*) 0)
00175 #  endif /* !__cplusplus */
00176 #endif
00177 
00178 #ifndef FALSE
00179 #define FALSE   (0)
00180 #endif
00181 
00182 #ifndef TRUE
00183 #define TRUE    (!FALSE)
00184 #endif
00185 
00186 #undef  MAX
00187 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
00188 
00189 #undef  MIN
00190 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
00191 
00192 #undef  ABS
00193 #define ABS(a)     (((a) < 0) ? -(a) : (a))
00194 
00195 #undef  CLAMP
00196 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
00197 
00198 /* Count the number of elements in an array. The array must be defined
00199  * as such; using this with a dynamically allocated array will give
00200  * incorrect results.
00201  */
00202 #define G_N_ELEMENTS(arr)               (sizeof (arr) / sizeof ((arr)[0]))
00203 
00204 /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT
00205  */
00206 #define GPOINTER_TO_SIZE(p)     ((gsize) (p))
00207 #define GSIZE_TO_POINTER(s)     ((gpointer) (gsize) (s))
00208 
00209 /* Provide convenience macros for handling structure
00210  * fields through their offsets.
00211  */
00212 
00213 #if defined(__GNUC__)  && __GNUC__ >= 4
00214 #  define G_STRUCT_OFFSET(struct_type, member) \
00215       ((glong) __builtin_offsetof (struct_type, member))
00216 #else
00217 #  define G_STRUCT_OFFSET(struct_type, member)  \
00218       ((glong) ((guint8*) &((struct_type*) 0)->member))
00219 #endif
00220 
00221 #define G_STRUCT_MEMBER_P(struct_p, struct_offset)   \
00222     ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
00223 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset)   \
00224     (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
00225 
00226 /* Provide simple macro statement wrappers:
00227  *   G_STMT_START { statements; } G_STMT_END;
00228  * This can be used as a single statement, like:
00229  *   if (x) G_STMT_START { ... } G_STMT_END; else ...
00230  * This intentionally does not use compiler extensions like GCC's '({...})' to
00231  * avoid portability issue or side effects when compiled with different compilers.
00232  */
00233 #if !(defined (G_STMT_START) && defined (G_STMT_END))
00234 #  define G_STMT_START  do
00235 #  define G_STMT_END    while (0)
00236 #endif
00237 
00238 /* Allow the app programmer to select whether or not return values
00239  * (usually char*) are const or not.  Don't try using this feature for
00240  * functions with C++ linkage.
00241  */
00242 #ifdef G_DISABLE_CONST_RETURNS
00243 #define G_CONST_RETURN
00244 #else
00245 #define G_CONST_RETURN const
00246 #endif
00247 
00248 /*
00249  * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to 
00250  * the compiler about the expected result of an expression. Some compilers
00251  * can use this information for optimizations.
00252  *
00253  * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
00254  * putting assignments in g_return_if_fail ().  
00255  */
00256 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
00257 #define _G_BOOLEAN_EXPR(expr)                   \
00258  __extension__ ({                               \
00259    int _g_boolean_var_;                         \
00260    if (expr)                                    \
00261       _g_boolean_var_ = 1;                      \
00262    else                                         \
00263       _g_boolean_var_ = 0;                      \
00264    _g_boolean_var_;                             \
00265 })
00266 #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))
00267 #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))
00268 #else
00269 #define G_LIKELY(expr) (expr)
00270 #define G_UNLIKELY(expr) (expr)
00271 #endif
00272 
00273 #endif /* __G_MACROS_H__ */