gmacros.h

Go to the documentation of this file.
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 #ifndef __G_MACROS_H__
00032 #define __G_MACROS_H__
00033 
00034 /* We include stddef.h to get the system's definition of NULL
00035  */
00036 #include <stddef.h>
00037 
00038 /* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
00039  * where this is valid. This allows for warningless compilation of
00040  * "long long" types even in the presence of '-ansi -pedantic'. 
00041  */
00042 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
00043 #  define G_GNUC_EXTENSION __extension__
00044 #else
00045 #  define G_GNUC_EXTENSION
00046 #endif
00047 
00048 /* Provide macros to feature the GCC function attribute.
00049  */
00050 #if    __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
00051 #define G_GNUC_PURE                            \
00052   __attribute__((__pure__))
00053 #define G_GNUC_MALLOC                           \
00054   __attribute__((__malloc__))
00055 #else
00056 #define G_GNUC_PURE
00057 #define G_GNUC_MALLOC
00058 #endif
00059 
00060 #if     __GNUC__ >= 4
00061 #define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
00062 #else
00063 #define G_GNUC_NULL_TERMINATED
00064 #endif
00065 
00066 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
00067 #define G_GNUC_PRINTF( format_idx, arg_idx )    \
00068   __attribute__((__format__ (__printf__, format_idx, arg_idx)))
00069 #define G_GNUC_SCANF( format_idx, arg_idx )     \
00070   __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
00071 #define G_GNUC_FORMAT( arg_idx )                \
00072   __attribute__((__format_arg__ (arg_idx)))
00073 #define G_GNUC_NORETURN                         \
00074   __attribute__((__noreturn__))
00075 #define G_GNUC_CONST                            \
00076   __attribute__((__const__))
00077 #define G_GNUC_UNUSED                           \
00078   __attribute__((__unused__))
00079 #define G_GNUC_NO_INSTRUMENT                    \
00080   __attribute__((__no_instrument_function__))
00081 #else   /* !__GNUC__ */
00082 #define G_GNUC_PRINTF( format_idx, arg_idx )
00083 #define G_GNUC_SCANF( format_idx, arg_idx )
00084 #define G_GNUC_FORMAT( arg_idx )
00085 #define G_GNUC_NORETURN
00086 #define G_GNUC_CONST
00087 #define G_GNUC_UNUSED
00088 #define G_GNUC_NO_INSTRUMENT
00089 #endif  /* !__GNUC__ */
00090 
00091 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
00092 #define G_GNUC_DEPRECATED                            \
00093   __attribute__((__deprecated__))
00094 #else
00095 #define G_GNUC_DEPRECATED
00096 #endif /* __GNUC__ */
00097 
00098 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
00099 #define G_GNUC_WARN_UNUSED_RESULT               \
00100   __attribute__((warn_unused_result))
00101 #else
00102 #define G_GNUC_WARN_UNUSED_RESULT
00103 #endif /* __GNUC__ */
00104 
00105 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
00106  * macros, so we can refer to them as strings unconditionally.
00107  * usage not-recommended since gcc-3.0
00108  */
00109 #if defined (__GNUC__) && (__GNUC__ < 3)
00110 #define G_GNUC_FUNCTION         __FUNCTION__
00111 #define G_GNUC_PRETTY_FUNCTION  __PRETTY_FUNCTION__
00112 #else   /* !__GNUC__ */
00113 #define G_GNUC_FUNCTION         ""
00114 #define G_GNUC_PRETTY_FUNCTION  ""
00115 #endif  /* !__GNUC__ */
00116 
00117 #define G_STRINGIFY(macro_or_string)    G_STRINGIFY_ARG (macro_or_string)
00118 #define G_STRINGIFY_ARG(contents)       #contents
00119 
00120 /* Provide a string identifying the current code position */
00121 #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)
00122 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
00123 #else
00124 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__)
00125 #endif
00126 
00127 /* Provide a string identifying the current function, non-concatenatable */
00128 #if defined (__GNUC__)
00129 #  define G_STRFUNC     ((const char*) (__PRETTY_FUNCTION__))
00130 #elif defined (G_HAVE_ISO_VARARGS)
00131 #  define G_STRFUNC     ((const char*) (__func__))
00132 #else
00133 #  define G_STRFUNC     ((const char*) ("???"))
00134 #endif
00135 
00136 /* Guard C code in headers, while including them from C++ */
00137 #ifdef  __cplusplus
00138 # define G_BEGIN_DECLS  extern "C" {
00139 # define G_END_DECLS    }
00140 #else
00141 # define G_BEGIN_DECLS
00142 # define G_END_DECLS
00143 #endif
00144 
00145 /* Provide definitions for some commonly used macros.
00146  *  Some of them are only provided if they haven't already
00147  *  been defined. It is assumed that if they are already
00148  *  defined then the current definition is correct.
00149  */
00150 #ifndef NULL
00151 #  ifdef __cplusplus
00152 #    define NULL        (0L)
00153 #  else /* !__cplusplus */
00154 #    define NULL        ((void*) 0)
00155 #  endif /* !__cplusplus */
00156 #endif
00157 
00158 #ifndef FALSE
00159 #define FALSE   (0)
00160 #endif
00161 
00162 #ifndef TRUE
00163 #define TRUE    (!FALSE)
00164 #endif
00165 
00166 #undef  MAX
00167 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
00168 
00169 #undef  MIN
00170 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
00171 
00172 #undef  ABS
00173 #define ABS(a)     (((a) < 0) ? -(a) : (a))
00174 
00175 #undef  CLAMP
00176 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
00177 
00178 /* Count the number of elements in an array. The array must be defined
00179  * as such; using this with a dynamically allocated array will give
00180  * incorrect results.
00181  */
00182 #define G_N_ELEMENTS(arr)               (sizeof (arr) / sizeof ((arr)[0]))
00183 
00184 /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT
00185  */
00186 #define GPOINTER_TO_SIZE(p)     ((gsize) (p))
00187 #define GSIZE_TO_POINTER(s)     ((gpointer) (gsize) (s))
00188 
00189 /* Provide convenience macros for handling structure
00190  * fields through their offsets.
00191  */
00192 #define G_STRUCT_OFFSET(struct_type, member)    \
00193     ((glong) ((guint8*) &((struct_type*) 0)->member))
00194 #define G_STRUCT_MEMBER_P(struct_p, struct_offset)   \
00195     ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
00196 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset)   \
00197     (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
00198 
00199 /* Provide simple macro statement wrappers (adapted from Perl):
00200  *  G_STMT_START { statements; } G_STMT_END;
00201  *  can be used as a single statement, as in
00202  *  if (x) G_STMT_START { ... } G_STMT_END; else ...
00203  *
00204  *  When GCC is compiling C code in non-ANSI mode, it will use the
00205  *  compiler __extension__ to wrap the statements wihin `({' and '})' braces.
00206  *  When compiling on platforms where configure has defined
00207  *  HAVE_DOWHILE_MACROS, statements will be wrapped with `do' and `while (0)'.
00208  *  For any other platforms (SunOS4 is known to have this issue), wrap the
00209  *  statements with `if (1)' and `else (void) 0'.
00210  */
00211 #if !(defined (G_STMT_START) && defined (G_STMT_END))
00212 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
00213 #  define G_STMT_START (void) __extension__ (
00214 #  define G_STMT_END )
00215 # else /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
00216 #  if defined (HAVE_DOWHILE_MACROS)
00217 #   define G_STMT_START do
00218 #   define G_STMT_END while (0)
00219 #  else /* !HAVE_DOWHILE_MACROS */
00220 #   define G_STMT_START if (1)
00221 #   define G_STMT_END else (void) 0
00222 #  endif /* !HAVE_DOWHILE_MACROS */
00223 # endif /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
00224 #endif
00225 
00226 /* Allow the app programmer to select whether or not return values
00227  * (usually char*) are const or not.  Don't try using this feature for
00228  * functions with C++ linkage.
00229  */
00230 #ifdef G_DISABLE_CONST_RETURNS
00231 #define G_CONST_RETURN
00232 #else
00233 #define G_CONST_RETURN const
00234 #endif
00235 
00236 /*
00237  * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to 
00238  * the compiler about the expected result of an expression. Some compilers
00239  * can use this information for optimizations.
00240  *
00241  * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
00242  * putting assignments in g_return_if_fail ().  
00243  */
00244 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
00245 #define _G_BOOLEAN_EXPR(expr)                   \
00246  __extension__ ({                               \
00247    int _g_boolean_var_;                         \
00248    if (expr)                                    \
00249       _g_boolean_var_ = 1;                      \
00250    else                                         \
00251       _g_boolean_var_ = 0;                      \
00252    _g_boolean_var_;                             \
00253 })
00254 #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))
00255 #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))
00256 #else
00257 #define G_LIKELY(expr) (expr)
00258 #define G_UNLIKELY(expr) (expr)
00259 #endif
00260 
00261 #endif /* __G_MACROS_H__ */

Copyright © Nokia Corporation 2001-2008
Back to top