1b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi/* GLIB - Library of useful routines for C programming 2b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald 3b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * 4b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * This library is free software; you can redistribute it and/or 5b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * modify it under the terms of the GNU Lesser General Public 6b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * License as published by the Free Software Foundation; either 7b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * version 2 of the License, or (at your option) any later version. 8b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * 9b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * This library is distributed in the hope that it will be useful, 10b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * but WITHOUT ANY WARRANTY; without even the implied warranty of 11b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * Lesser General Public License for more details. 13b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * 14b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * You should have received a copy of the GNU Lesser General Public 15b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * License along with this library; if not, write to the 16b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * Boston, MA 02111-1307, USA. 18b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi */ 19b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 20b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi/* 21b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * Modified by the GLib Team and others 1997-2000. See the AUTHORS 22b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * file for a list of people on the GLib Team. See the ChangeLog 23b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * files for a list of changes. These files are distributed with 24f4bb21aa1d405fd11043a03e0962358eb0f42a5eMichael Natterer * GLib at ftp://ftp.gtk.org/pub/gtk/. 25b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi */ 26b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 27b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi/* This file must not include any other glib header file and must thus 28f4bb21aa1d405fd11043a03e0962358eb0f42a5eMichael Natterer * not refer to variables from glibconfig.h 29b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi */ 30b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 31e0aa9d8a6135d404fe1ff60ba6858599e85edf98Michael Natterer#if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION) 32f4bb21aa1d405fd11043a03e0962358eb0f42a5eMichael Natterer#error "Only <glib.h> can be included directly." 33f4bb21aa1d405fd11043a03e0962358eb0f42a5eMichael Natterer#endif 34f4bb21aa1d405fd11043a03e0962358eb0f42a5eMichael Natterer 35b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#ifndef __G_MACROS_H__ 36b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define __G_MACROS_H__ 37b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 38f94802c2991384a1c0ffe8272139fad7073d637eOwen Taylor/* We include stddef.h to get the system's definition of NULL 39f94802c2991384a1c0ffe8272139fad7073d637eOwen Taylor */ 40f94802c2991384a1c0ffe8272139fad7073d637eOwen Taylor#include <stddef.h> 41f94802c2991384a1c0ffe8272139fad7073d637eOwen Taylor 42b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi/* Here we provide G_GNUC_EXTENSION as an alias for __extension__, 43b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * where this is valid. This allows for warningless compilation of 44b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * "long long" types even in the presence of '-ansi -pedantic'. 45b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi */ 46b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8) 47b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi# define G_GNUC_EXTENSION __extension__ 48b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#else 49b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi# define G_GNUC_EXTENSION 50b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#endif 51b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 52b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi/* Provide macros to feature the GCC function attribute. 53b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi */ 54b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) 55b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_PURE \ 568c613d7d0910c5294e9a04682300e784b59f2298Darin Adler __attribute__((__pure__)) 578f0d5d169ea3a667fc7a031b8012e3b9b4cccc1cMatthias Clasen#define G_GNUC_MALLOC \ 588f0d5d169ea3a667fc7a031b8012e3b9b4cccc1cMatthias Clasen __attribute__((__malloc__)) 59b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#else 60b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_PURE 618f0d5d169ea3a667fc7a031b8012e3b9b4cccc1cMatthias Clasen#define G_GNUC_MALLOC 62b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#endif 63b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 646aac5ce82cb8dc18602b2465c07d537e0da9d9a2Matthias Clasen#if __GNUC__ >= 4 656aac5ce82cb8dc18602b2465c07d537e0da9d9a2Matthias Clasen#define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__)) 666aac5ce82cb8dc18602b2465c07d537e0da9d9a2Matthias Clasen#else 676aac5ce82cb8dc18602b2465c07d537e0da9d9a2Matthias Clasen#define G_GNUC_NULL_TERMINATED 686aac5ce82cb8dc18602b2465c07d537e0da9d9a2Matthias Clasen#endif 696aac5ce82cb8dc18602b2465c07d537e0da9d9a2Matthias Clasen 702af49e60a13b8d2a7aba47cd6af70217d0e263e5Matthias Clasen#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) 712af49e60a13b8d2a7aba47cd6af70217d0e263e5Matthias Clasen#define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x))) 722af49e60a13b8d2a7aba47cd6af70217d0e263e5Matthias Clasen#define G_GNUC_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y))) 732af49e60a13b8d2a7aba47cd6af70217d0e263e5Matthias Clasen#else 742af49e60a13b8d2a7aba47cd6af70217d0e263e5Matthias Clasen#define G_GNUC_ALLOC_SIZE(x) 752af49e60a13b8d2a7aba47cd6af70217d0e263e5Matthias Clasen#define G_GNUC_ALLOC_SIZE2(x,y) 762af49e60a13b8d2a7aba47cd6af70217d0e263e5Matthias Clasen#endif 772af49e60a13b8d2a7aba47cd6af70217d0e263e5Matthias Clasen 78b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) 79b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_PRINTF( format_idx, arg_idx ) \ 8039e4c5afe8a4f4953e3a2905a51aeaa41a91b598Matthias Clasen __attribute__((__format__ (__printf__, format_idx, arg_idx))) 81b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_SCANF( format_idx, arg_idx ) \ 8239e4c5afe8a4f4953e3a2905a51aeaa41a91b598Matthias Clasen __attribute__((__format__ (__scanf__, format_idx, arg_idx))) 83b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_FORMAT( arg_idx ) \ 8439e4c5afe8a4f4953e3a2905a51aeaa41a91b598Matthias Clasen __attribute__((__format_arg__ (arg_idx))) 85b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_NORETURN \ 8639e4c5afe8a4f4953e3a2905a51aeaa41a91b598Matthias Clasen __attribute__((__noreturn__)) 87b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_CONST \ 8839e4c5afe8a4f4953e3a2905a51aeaa41a91b598Matthias Clasen __attribute__((__const__)) 89b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_UNUSED \ 9039e4c5afe8a4f4953e3a2905a51aeaa41a91b598Matthias Clasen __attribute__((__unused__)) 913b6fd34af77405f08633586387c7b27f87ae2a36Tim Janik#define G_GNUC_NO_INSTRUMENT \ 9239e4c5afe8a4f4953e3a2905a51aeaa41a91b598Matthias Clasen __attribute__((__no_instrument_function__)) 93b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#else /* !__GNUC__ */ 94b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_PRINTF( format_idx, arg_idx ) 95b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_SCANF( format_idx, arg_idx ) 96b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_FORMAT( arg_idx ) 97b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_NORETURN 98b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_CONST 99b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_UNUSED 1003b6fd34af77405f08633586387c7b27f87ae2a36Tim Janik#define G_GNUC_NO_INSTRUMENT 101b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#endif /* !__GNUC__ */ 1027d20e0c33ec27f00aea02f38da5151917a13a085Owen Taylor 1037d20e0c33ec27f00aea02f38da5151917a13a085Owen Taylor#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) 1047d20e0c33ec27f00aea02f38da5151917a13a085Owen Taylor#define G_GNUC_DEPRECATED \ 1057d20e0c33ec27f00aea02f38da5151917a13a085Owen Taylor __attribute__((__deprecated__)) 1067d20e0c33ec27f00aea02f38da5151917a13a085Owen Taylor#else 1077d20e0c33ec27f00aea02f38da5151917a13a085Owen Taylor#define G_GNUC_DEPRECATED 1087d20e0c33ec27f00aea02f38da5151917a13a085Owen Taylor#endif /* __GNUC__ */ 109b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 1106cf97938ec13b7bad389b98b1d2440679d774d3dTim Janik#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3) 1116cf97938ec13b7bad389b98b1d2440679d774d3dTim Janik# define G_GNUC_MAY_ALIAS __attribute__((may_alias)) 1126cf97938ec13b7bad389b98b1d2440679d774d3dTim Janik#else 1136cf97938ec13b7bad389b98b1d2440679d774d3dTim Janik# define G_GNUC_MAY_ALIAS 1146cf97938ec13b7bad389b98b1d2440679d774d3dTim Janik#endif 1156cf97938ec13b7bad389b98b1d2440679d774d3dTim Janik 11600f952f8e85527988a6171a7d9617af14c0ff5a2Matthias Clasen#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) 11700f952f8e85527988a6171a7d9617af14c0ff5a2Matthias Clasen#define G_GNUC_WARN_UNUSED_RESULT \ 11800f952f8e85527988a6171a7d9617af14c0ff5a2Matthias Clasen __attribute__((warn_unused_result)) 11900f952f8e85527988a6171a7d9617af14c0ff5a2Matthias Clasen#else 12000f952f8e85527988a6171a7d9617af14c0ff5a2Matthias Clasen#define G_GNUC_WARN_UNUSED_RESULT 12100f952f8e85527988a6171a7d9617af14c0ff5a2Matthias Clasen#endif /* __GNUC__ */ 12200f952f8e85527988a6171a7d9617af14c0ff5a2Matthias Clasen 1237bfc60de2e83b263c35e645d4251bc55eaefec00Matthias Clasen#ifndef G_DISABLE_DEPRECATED 124b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi/* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with 125b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * macros, so we can refer to them as strings unconditionally. 1265d49a7caf7e10a69fb25159f51b3eebdb92f774fTim Janik * usage not-recommended since gcc-3.0 127b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi */ 128b19bcd9604fee9251130d109f068fc97e4198fd5Havoc Pennington#if defined (__GNUC__) && (__GNUC__ < 3) 129b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_FUNCTION __FUNCTION__ 130b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__ 131b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#else /* !__GNUC__ */ 132b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_FUNCTION "" 133b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_GNUC_PRETTY_FUNCTION "" 134b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#endif /* !__GNUC__ */ 1357bfc60de2e83b263c35e645d4251bc55eaefec00Matthias Clasen#endif /* !G_DISABLE_DEPRECATED */ 136b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 137b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_STRINGIFY(macro_or_string) G_STRINGIFY_ARG (macro_or_string) 138b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_STRINGIFY_ARG(contents) #contents 139b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 1400ffa76b9e9d703f926cf5495bdacb17d3b21f9aaidentifier#define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2 1410ffa76b9e9d703f926cf5495bdacb17d3b21f9aaidentifier#define G_PASTE(identifier1,identifier2) G_PASTE_ARGS (identifier1, identifier2) 1420ffa76b9e9d703f926cf5495bdacb17d3b21f9aaexpr) typedef struct { char Compile_Time_Assertion[(expr) ?#define G_STATIC_ASSERT(expr) typedef struct { char Compile_Time_Assertion[(expr) ? 1 : -1]; } G_PASTE (_GStaticAssert_, __LINE__) 143b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 144614e5ed63a6ea732ed313f1220f9ef6d34b0cbfbOwen Taylor/* Provide a string identifying the current code position */ 145b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus) 146b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi# define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()" 147b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#else 148b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi# define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) 149b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#endif 1505d49a7caf7e10a69fb25159f51b3eebdb92f774fTim Janik 1515d49a7caf7e10a69fb25159f51b3eebdb92f774fTim Janik/* Provide a string identifying the current function, non-concatenatable */ 1525d49a7caf7e10a69fb25159f51b3eebdb92f774fTim Janik#if defined (__GNUC__) 1530b890c62986c23345de43882062c557bc1471fefMatthias Clasen# define G_STRFUNC ((const char*) (__PRETTY_FUNCTION__)) 1545d49a7caf7e10a69fb25159f51b3eebdb92f774fTim Janik#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 19901L 155db16c1be2bdb0923405f2bb59ce21ff084691cf1Manish Singh# define G_STRFUNC ((const char*) (__func__)) 1565d49a7caf7e10a69fb25159f51b3eebdb92f774fTim Janik#else 1575d49a7caf7e10a69fb25159f51b3eebdb92f774fTim Janik# define G_STRFUNC ((const char*) ("???")) 1585d49a7caf7e10a69fb25159f51b3eebdb92f774fTim Janik#endif 159b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 160b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi/* Guard C code in headers, while including them from C++ */ 161b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#ifdef __cplusplus 162b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi# define G_BEGIN_DECLS extern "C" { 163b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi# define G_END_DECLS } 164b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#else 165b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi# define G_BEGIN_DECLS 166b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi# define G_END_DECLS 167b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#endif 168b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 169b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi/* Provide definitions for some commonly used macros. 170b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * Some of them are only provided if they haven't already 171b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * been defined. It is assumed that if they are already 172b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * defined then the current definition is correct. 173b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi */ 174b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#ifndef NULL 175b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi# ifdef __cplusplus 176b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi# define NULL (0L) 177b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi# else /* !__cplusplus */ 178b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi# define NULL ((void*) 0) 179b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi# endif /* !__cplusplus */ 180b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#endif 181b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 182b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#ifndef FALSE 183b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define FALSE (0) 184b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#endif 185b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 186b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#ifndef TRUE 187b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define TRUE (!FALSE) 188b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#endif 189b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 190b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#undef MAX 191b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define MAX(a, b) (((a) > (b)) ? (a) : (b)) 192b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 193b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#undef MIN 194b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define MIN(a, b) (((a) < (b)) ? (a) : (b)) 195b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 196b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#undef ABS 197b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define ABS(a) (((a) < 0) ? -(a) : (a)) 198b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 199b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#undef CLAMP 200b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) 201b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 202b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi/* Count the number of elements in an array. The array must be defined 203b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * as such; using this with a dynamically allocated array will give 204b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * incorrect results. 205b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi */ 206b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0])) 207f5c28ce4ab8e8015a1432060b6cfe547183b2f9eOwen Taylor 208f5c28ce4ab8e8015a1432060b6cfe547183b2f9eOwen Taylor/* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT 209f5c28ce4ab8e8015a1432060b6cfe547183b2f9eOwen Taylor */ 210f5c28ce4ab8e8015a1432060b6cfe547183b2f9eOwen Taylor#define GPOINTER_TO_SIZE(p) ((gsize) (p)) 211f5c28ce4ab8e8015a1432060b6cfe547183b2f9eOwen Taylor#define GSIZE_TO_POINTER(s) ((gpointer) (gsize) (s)) 212b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 213b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi/* Provide convenience macros for handling structure 214b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * fields through their offsets. 2158013b215cdcc5d3bd77083a3c8427418e27f396eSebastian Dröge */ 2168013b215cdcc5d3bd77083a3c8427418e27f396eSebastian Dröge 2178013b215cdcc5d3bd77083a3c8427418e27f396eSebastian Dröge#if defined(__GNUC__) && __GNUC__ >= 4 218ed6c7f0e3226a6e6ead09a672c67fef55743e073Matthias Clasen# define G_STRUCT_OFFSET(struct_type, member) \ 2198013b215cdcc5d3bd77083a3c8427418e27f396eSebastian Dröge ((glong) offsetof (struct_type, member)) 2208013b215cdcc5d3bd77083a3c8427418e27f396eSebastian Dröge#else 2218013b215cdcc5d3bd77083a3c8427418e27f396eSebastian Dröge# define G_STRUCT_OFFSET(struct_type, member) \ 2228013b215cdcc5d3bd77083a3c8427418e27f396eSebastian Dröge ((glong) ((guint8*) &((struct_type*) 0)->member)) 2238013b215cdcc5d3bd77083a3c8427418e27f396eSebastian Dröge#endif 224b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi 225b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_STRUCT_MEMBER_P(struct_p, struct_offset) \ 226b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset))) 227b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \ 228b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset))) 2290782d697e5b2809f754b4416ffa7c1ae3a82c686x) G_STMT_START { ... } G_STMT_END; else ... 2300782d697e5b2809f754b4416ffa7c1ae3a82c686{...})' to/* Provide simple macro statement wrappers: 231b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * G_STMT_START { statements; } G_STMT_END; 232b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * This can be used as a single statement, like: 233b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * if (x) G_STMT_START { ... } G_STMT_END; else ... 234b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi * This intentionally does not use compiler extensions like GCC's '({...})' to 235430ae18e5be4267c00ea38542d885642f35e6714Havoc Pennington * avoid portability issue or side effects when compiled with different compilers. 236430ae18e5be4267c00ea38542d885642f35e6714Havoc Pennington */ 237430ae18e5be4267c00ea38542d885642f35e6714Havoc Pennington#if !(defined (G_STMT_START) && defined (G_STMT_END)) 238430ae18e5be4267c00ea38542d885642f35e6714Havoc Pennington# define G_STMT_START do 239430ae18e5be4267c00ea38542d885642f35e6714Havoc Pennington# define G_STMT_END while (0) 240430ae18e5be4267c00ea38542d885642f35e6714Havoc Pennington#endif 241430ae18e5be4267c00ea38542d885642f35e6714Havoc Pennington 242430ae18e5be4267c00ea38542d885642f35e6714Havoc Pennington/* Allow the app programmer to select whether or not return values 243430ae18e5be4267c00ea38542d885642f35e6714Havoc Pennington * (usually char*) are const or not. Don't try using this feature for 244430ae18e5be4267c00ea38542d885642f35e6714Havoc Pennington * functions with C++ linkage. 245dc645ce354f06c93f982a0177951c9f98627d751Matthias Clasen */ 246dc645ce354f06c93f982a0177951c9f98627d751Matthias Clasen#ifdef G_DISABLE_CONST_RETURNS 247dc645ce354f06c93f982a0177951c9f98627d751Matthias Clasen#define G_CONST_RETURN 248dc645ce354f06c93f982a0177951c9f98627d751Matthias Clasen#else 249827f3c4fa495135ce19a0e5ce2f0a0f164805b7cMatthias Clasen#define G_CONST_RETURN const 250827f3c4fa495135ce19a0e5ce2f0a0f164805b7cMatthias Clasen#endif 251827f3c4fa495135ce19a0e5ce2f0a0f164805b7cMatthias Clasen 252dc645ce354f06c93f982a0177951c9f98627d751Matthias Clasen/* 253827f3c4fa495135ce19a0e5ce2f0a0f164805b7cMatthias Clasen * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to 254827f3c4fa495135ce19a0e5ce2f0a0f164805b7cMatthias Clasen * the compiler about the expected result of an expression. Some compilers 255827f3c4fa495135ce19a0e5ce2f0a0f164805b7cMatthias Clasen * can use this information for optimizations. 256827f3c4fa495135ce19a0e5ce2f0a0f164805b7cMatthias Clasen * 257827f3c4fa495135ce19a0e5ce2f0a0f164805b7cMatthias Clasen * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when 258827f3c4fa495135ce19a0e5ce2f0a0f164805b7cMatthias Clasen * putting assignments in g_return_if_fail (). 259827f3c4fa495135ce19a0e5ce2f0a0f164805b7cMatthias Clasen */ 260827f3c4fa495135ce19a0e5ce2f0a0f164805b7cMatthias Clasen#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) 261827f3c4fa495135ce19a0e5ce2f0a0f164805b7cMatthias Clasen#define _G_BOOLEAN_EXPR(expr) \ 262827f3c4fa495135ce19a0e5ce2f0a0f164805b7cMatthias Clasen __extension__ ({ \ 26300a9d30b67ed0e5e4a58201eca85214d5eeeae86Matthias Clasen int _g_boolean_var_; \ 26400a9d30b67ed0e5e4a58201eca85214d5eeeae86Matthias Clasen if (expr) \ 265dc645ce354f06c93f982a0177951c9f98627d751Matthias Clasen _g_boolean_var_ = 1; \ 266b75a34b721862435b4a72cc920eae10d063d019cMatthias Clasen else \ 267b75a34b721862435b4a72cc920eae10d063d019cMatthias Clasen _g_boolean_var_ = 0; \ 268dc645ce354f06c93f982a0177951c9f98627d751Matthias Clasen _g_boolean_var_; \ 269dc645ce354f06c93f982a0177951c9f98627d751Matthias Clasen}) 270b1d15587479deaacb9bd983fd28c6f3d3e27142eSebastian Wilhelmi#define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1)) 271#define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0)) 272#else 273#define G_LIKELY(expr) (expr) 274#define G_UNLIKELY(expr) (expr) 275#endif 276 277#endif /* __G_MACROS_H__ */ 278