You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

285 lines
9.4 KiB

  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. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  21. * file for a list of people on the GLib Team. See the ChangeLog
  22. * files for a list of changes. These files are distributed with
  23. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  24. */
  25. /* This file must not include any other glib header file and must thus
  26. * not refer to variables from glibconfig.h
  27. */
  28. #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  29. #error "Only <glib.h> can be included directly."
  30. #endif
  31. #ifndef __G_MACROS_H__
  32. #define __G_MACROS_H__
  33. /* We include stddef.h to get the system's definition of NULL
  34. */
  35. #include <stddef.h>
  36. /* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
  37. * where this is valid. This allows for warningless compilation of
  38. * "long long" types even in the presence of '-ansi -pedantic'.
  39. */
  40. #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
  41. # define G_GNUC_EXTENSION __extension__
  42. #else
  43. # define G_GNUC_EXTENSION
  44. #endif
  45. /* Provide macros to feature the GCC function attribute.
  46. */
  47. #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
  48. #define G_GNUC_PURE \
  49. __attribute__((__pure__))
  50. #define G_GNUC_MALLOC \
  51. __attribute__((__malloc__))
  52. #else
  53. #define G_GNUC_PURE
  54. #define G_GNUC_MALLOC
  55. #endif
  56. #if __GNUC__ >= 4
  57. #define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
  58. #else
  59. #define G_GNUC_NULL_TERMINATED
  60. #endif
  61. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
  62. #define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x)))
  63. #define G_GNUC_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y)))
  64. #else
  65. #define G_GNUC_ALLOC_SIZE(x)
  66. #define G_GNUC_ALLOC_SIZE2(x,y)
  67. #endif
  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. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
  93. #define G_GNUC_DEPRECATED \
  94. __attribute__((__deprecated__))
  95. #else
  96. #define G_GNUC_DEPRECATED
  97. #endif /* __GNUC__ */
  98. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
  99. #define G_GNUC_DEPRECATED_FOR(f) \
  100. __attribute__((deprecated("Use " #f " instead")))
  101. #else
  102. #define G_GNUC_DEPRECATED_FOR(f) G_GNUC_DEPRECATED
  103. #endif /* __GNUC__ */
  104. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
  105. # define G_GNUC_MAY_ALIAS __attribute__((may_alias))
  106. #else
  107. # define G_GNUC_MAY_ALIAS
  108. #endif
  109. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
  110. #define G_GNUC_WARN_UNUSED_RESULT \
  111. __attribute__((warn_unused_result))
  112. #else
  113. #define G_GNUC_WARN_UNUSED_RESULT
  114. #endif /* __GNUC__ */
  115. #ifndef G_DISABLE_DEPRECATED
  116. /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
  117. * macros, so we can refer to them as strings unconditionally.
  118. * usage not-recommended since gcc-3.0
  119. */
  120. #if defined (__GNUC__) && (__GNUC__ < 3)
  121. #define G_GNUC_FUNCTION __FUNCTION__
  122. #define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
  123. #else /* !__GNUC__ */
  124. #define G_GNUC_FUNCTION ""
  125. #define G_GNUC_PRETTY_FUNCTION ""
  126. #endif /* !__GNUC__ */
  127. #endif /* !G_DISABLE_DEPRECATED */
  128. #define G_STRINGIFY(macro_or_string) G_STRINGIFY_ARG (macro_or_string)
  129. #define G_STRINGIFY_ARG(contents) #contents
  130. #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
  131. #define G_PASTE(identifier1,identifier2) G_PASTE_ARGS (identifier1, identifier2)
  132. #define G_STATIC_ASSERT(expr) typedef struct { char Compile_Time_Assertion[(expr) ? 1 : -1]; } G_PASTE (_GStaticAssert_, __LINE__)
  133. /* Provide a string identifying the current code position */
  134. #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)
  135. # define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
  136. #else
  137. # define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__)
  138. #endif
  139. /* Provide a string identifying the current function, non-concatenatable */
  140. #if defined (__GNUC__)
  141. # define G_STRFUNC ((const char*) (__PRETTY_FUNCTION__))
  142. #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 19901L
  143. # define G_STRFUNC ((const char*) (__func__))
  144. #else
  145. # define G_STRFUNC ((const char*) ("???"))
  146. #endif
  147. /* Guard C code in headers, while including them from C++ */
  148. #ifdef __cplusplus
  149. # define G_BEGIN_DECLS extern "C" {
  150. # define G_END_DECLS }
  151. #else
  152. # define G_BEGIN_DECLS
  153. # define G_END_DECLS
  154. #endif
  155. /* Provide definitions for some commonly used macros.
  156. * Some of them are only provided if they haven't already
  157. * been defined. It is assumed that if they are already
  158. * defined then the current definition is correct.
  159. */
  160. #ifndef NULL
  161. # ifdef __cplusplus
  162. # define NULL (0L)
  163. # else /* !__cplusplus */
  164. # define NULL ((void*) 0)
  165. # endif /* !__cplusplus */
  166. #endif
  167. #ifndef FALSE
  168. #define FALSE (0)
  169. #endif
  170. #ifndef TRUE
  171. #define TRUE (!FALSE)
  172. #endif
  173. #undef MAX
  174. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  175. #undef MIN
  176. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  177. #undef ABS
  178. #define ABS(a) (((a) < 0) ? -(a) : (a))
  179. #undef CLAMP
  180. #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
  181. /* Count the number of elements in an array. The array must be defined
  182. * as such; using this with a dynamically allocated array will give
  183. * incorrect results.
  184. */
  185. #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
  186. /* Macros by analogy to GINT_TO_POINTER, GPOINTER_TO_INT
  187. */
  188. #define GPOINTER_TO_SIZE(p) ((gsize) (p))
  189. #define GSIZE_TO_POINTER(s) ((gpointer) (gsize) (s))
  190. /* Provide convenience macros for handling structure
  191. * fields through their offsets.
  192. */
  193. #if defined(__GNUC__) && __GNUC__ >= 4
  194. # define G_STRUCT_OFFSET(struct_type, member) \
  195. ((glong) offsetof (struct_type, member))
  196. #else
  197. # define G_STRUCT_OFFSET(struct_type, member) \
  198. ((glong) ((guint8*) &((struct_type*) 0)->member))
  199. #endif
  200. #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
  201. ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
  202. #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
  203. (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
  204. /* Provide simple macro statement wrappers:
  205. * G_STMT_START { statements; } G_STMT_END;
  206. * This can be used as a single statement, like:
  207. * if (x) G_STMT_START { ... } G_STMT_END; else ...
  208. * This intentionally does not use compiler extensions like GCC's '({...})' to
  209. * avoid portability issue or side effects when compiled with different compilers.
  210. */
  211. #if !(defined (G_STMT_START) && defined (G_STMT_END))
  212. # define G_STMT_START do
  213. # define G_STMT_END while (0)
  214. #endif
  215. /* Allow the app programmer to select whether or not return values
  216. * (usually char*) are const or not. Don't try using this feature for
  217. * functions with C++ linkage.
  218. */
  219. #ifdef G_DISABLE_CONST_RETURNS
  220. #define G_CONST_RETURN
  221. #else
  222. #define G_CONST_RETURN const
  223. #endif
  224. /*
  225. * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to
  226. * the compiler about the expected result of an expression. Some compilers
  227. * can use this information for optimizations.
  228. *
  229. * The _G_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
  230. * putting assignments in g_return_if_fail ().
  231. */
  232. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  233. #define _G_BOOLEAN_EXPR(expr) \
  234. __extension__ ({ \
  235. int _g_boolean_var_; \
  236. if (expr) \
  237. _g_boolean_var_ = 1; \
  238. else \
  239. _g_boolean_var_ = 0; \
  240. _g_boolean_var_; \
  241. })
  242. #define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))
  243. #define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))
  244. #else
  245. #define G_LIKELY(expr) (expr)
  246. #define G_UNLIKELY(expr) (expr)
  247. #endif
  248. #endif /* __G_MACROS_H__ */