1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
28 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
29 #error "Only <glib.h> can be included directly."
32 #include <glibconfig.h>
33 #include <glib/gmacros.h>
34 #include <glib/gversionmacros.h>
39 /* Provide type definitions for commonly used types.
40 * These are useful because a "gint8" can be adjusted
41 * to be 1 byte (8 bits) on all platforms. Similarly and
42 * more importantly, "gint32" can be adjusted to be
43 * 4 bytes (32 bits) on all platforms.
50 typedef gint gboolean
;
52 typedef unsigned char guchar
;
53 typedef unsigned short gushort
;
54 typedef unsigned long gulong
;
55 typedef unsigned int guint
;
58 typedef double gdouble
;
60 /* Define min and max constants for the fixed size numerical types */
61 #define G_MININT8 ((gint8) -0x80)
62 #define G_MAXINT8 ((gint8) 0x7f)
63 #define G_MAXUINT8 ((guint8) 0xff)
65 #define G_MININT16 ((gint16) -0x8000)
66 #define G_MAXINT16 ((gint16) 0x7fff)
67 #define G_MAXUINT16 ((guint16) 0xffff)
69 #define G_MININT32 ((gint32) -0x80000000)
70 #define G_MAXINT32 ((gint32) 0x7fffffff)
71 #define G_MAXUINT32 ((guint32) 0xffffffff)
73 #define G_MININT64 ((gint64) G_GINT64_CONSTANT(-0x8000000000000000))
74 #define G_MAXINT64 G_GINT64_CONSTANT(0x7fffffffffffffff)
75 #define G_MAXUINT64 G_GUINT64_CONSTANT(0xffffffffffffffff)
77 typedef void* gpointer
;
78 typedef const void *gconstpointer
;
80 typedef gint (*GCompareFunc
) (gconstpointer a
,
82 typedef gint (*GCompareDataFunc
) (gconstpointer a
,
85 typedef gboolean (*GEqualFunc
) (gconstpointer a
,
87 typedef void (*GDestroyNotify
) (gpointer data
);
88 typedef void (*GFunc
) (gpointer data
,
90 typedef guint (*GHashFunc
) (gconstpointer key
);
91 typedef void (*GHFunc
) (gpointer key
,
97 * @data: a data pointer
99 * Declares a type of function which takes an arbitrary
100 * data pointer argument and has no return value. It is
101 * not currently used in GLib or GTK+.
103 typedef void (*GFreeFunc
) (gpointer data
);
107 * @str: the untranslated string
108 * @data: user data specified when installing the function, e.g.
109 * in g_option_group_set_translate_func()
111 * The type of functions which are used to translate user-visible
112 * strings, for <option>--help</option> output.
114 * Returns: a translation of the string for the current locale.
115 * The returned string is owned by GLib and must not be freed.
117 typedef const gchar
* (*GTranslateFunc
) (const gchar
*str
,
121 /* Define some mathematical constants that aren't available
122 * symbolically in some strict ISO C implementations.
124 * Note that the large number of digits used in these definitions
125 * doesn't imply that GLib or current computers in general would be
126 * able to handle floating point numbers with an accuracy like this.
127 * It's mostly an exercise in futility and future proofing. For
128 * extended precision floating point support, look somewhere else
131 #define G_E 2.7182818284590452353602874713526624977572470937000
132 #define G_LN2 0.69314718055994530941723212145817656807550013436026
133 #define G_LN10 2.3025850929940456840179914546843642076011014886288
134 #define G_PI 3.1415926535897932384626433832795028841971693993751
135 #define G_PI_2 1.5707963267948966192313216916397514420985846996876
136 #define G_PI_4 0.78539816339744830961566084581987572104929234984378
137 #define G_SQRT2 1.4142135623730950488016887242096980785696718753769
139 /* Portable endian checks and conversions
141 * glibconfig.h defines G_BYTE_ORDER which expands to one of
144 #define G_LITTLE_ENDIAN 1234
145 #define G_BIG_ENDIAN 4321
146 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
149 /* Basic bit swapping functions
151 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
152 (guint16) ((guint16) (val) >> 8) | \
153 (guint16) ((guint16) (val) << 8)))
155 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((guint32) ( \
156 (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
157 (((guint32) (val) & (guint32) 0x0000ff00U) << 8) | \
158 (((guint32) (val) & (guint32) 0x00ff0000U) >> 8) | \
159 (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
161 #define GUINT64_SWAP_LE_BE_CONSTANT(val) ((guint64) ( \
162 (((guint64) (val) & \
163 (guint64) G_GINT64_CONSTANT (0x00000000000000ffU)) << 56) | \
164 (((guint64) (val) & \
165 (guint64) G_GINT64_CONSTANT (0x000000000000ff00U)) << 40) | \
166 (((guint64) (val) & \
167 (guint64) G_GINT64_CONSTANT (0x0000000000ff0000U)) << 24) | \
168 (((guint64) (val) & \
169 (guint64) G_GINT64_CONSTANT (0x00000000ff000000U)) << 8) | \
170 (((guint64) (val) & \
171 (guint64) G_GINT64_CONSTANT (0x000000ff00000000U)) >> 8) | \
172 (((guint64) (val) & \
173 (guint64) G_GINT64_CONSTANT (0x0000ff0000000000U)) >> 24) | \
174 (((guint64) (val) & \
175 (guint64) G_GINT64_CONSTANT (0x00ff000000000000U)) >> 40) | \
176 (((guint64) (val) & \
177 (guint64) G_GINT64_CONSTANT (0xff00000000000000U)) >> 56)))
179 /* Arch specific stuff for speed
181 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
183 # if __GNUC__ >= 4 && defined (__GNUC_MINOR__) && __GNUC_MINOR__ >= 3
184 # define GUINT32_SWAP_LE_BE(val) ((guint32) __builtin_bswap32 ((gint32) (val)))
185 # define GUINT64_SWAP_LE_BE(val) ((guint64) __builtin_bswap64 ((gint64) (val)))
188 # if defined (__i386__)
189 # define GUINT16_SWAP_LE_BE_IA32(val) \
191 ({ guint16 __v, __x = ((guint16) (val)); \
192 if (__builtin_constant_p (__x)) \
193 __v = GUINT16_SWAP_LE_BE_CONSTANT (__x); \
195 __asm__ ("rorw $8, %w0" \
200 # if !defined (__i486__) && !defined (__i586__) \
201 && !defined (__pentium__) && !defined (__i686__) \
202 && !defined (__pentiumpro__) && !defined (__pentium4__)
203 # define GUINT32_SWAP_LE_BE_IA32(val) \
205 ({ guint32 __v, __x = ((guint32) (val)); \
206 if (__builtin_constant_p (__x)) \
207 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
209 __asm__ ("rorw $8, %w0\n\t" \
216 # else /* 486 and higher has bswap */
217 # define GUINT32_SWAP_LE_BE_IA32(val) \
219 ({ guint32 __v, __x = ((guint32) (val)); \
220 if (__builtin_constant_p (__x)) \
221 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
223 __asm__ ("bswap %0" \
227 # endif /* processor specific 32-bit stuff */
228 # define GUINT64_SWAP_LE_BE_IA32(val) \
230 ({ union { guint64 __ll; \
231 guint32 __l[2]; } __w, __r; \
232 __w.__ll = ((guint64) (val)); \
233 if (__builtin_constant_p (__w.__ll)) \
234 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (__w.__ll); \
237 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
238 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
241 /* Possibly just use the constant version and let gcc figure it out? */
242 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA32 (val))
243 # ifndef GUINT32_SWAP_LE_BE
244 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA32 (val))
246 # ifndef GUINT64_SWAP_LE_BE
247 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA32 (val))
249 # elif defined (__ia64__)
250 # define GUINT16_SWAP_LE_BE_IA64(val) \
252 ({ guint16 __v, __x = ((guint16) (val)); \
253 if (__builtin_constant_p (__x)) \
254 __v = GUINT16_SWAP_LE_BE_CONSTANT (__x); \
256 __asm__ __volatile__ ("shl %0 = %1, 48 ;;" \
257 "mux1 %0 = %0, @rev ;;" \
261 # define GUINT32_SWAP_LE_BE_IA64(val) \
263 ({ guint32 __v, __x = ((guint32) (val)); \
264 if (__builtin_constant_p (__x)) \
265 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
267 __asm__ __volatile__ ("shl %0 = %1, 32 ;;" \
268 "mux1 %0 = %0, @rev ;;" \
272 # define GUINT64_SWAP_LE_BE_IA64(val) \
274 ({ guint64 __v, __x = ((guint64) (val)); \
275 if (__builtin_constant_p (__x)) \
276 __v = GUINT64_SWAP_LE_BE_CONSTANT (__x); \
278 __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;" \
282 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA64 (val))
283 # ifndef GUINT32_SWAP_LE_BE
284 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA64 (val))
286 # ifndef GUINT64_SWAP_LE_BE
287 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA64 (val))
289 # elif defined (__x86_64__)
290 # define GUINT32_SWAP_LE_BE_X86_64(val) \
292 ({ guint32 __v, __x = ((guint32) (val)); \
293 if (__builtin_constant_p (__x)) \
294 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
296 __asm__ ("bswapl %0" \
300 # define GUINT64_SWAP_LE_BE_X86_64(val) \
302 ({ guint64 __v, __x = ((guint64) (val)); \
303 if (__builtin_constant_p (__x)) \
304 __v = GUINT64_SWAP_LE_BE_CONSTANT (__x); \
306 __asm__ ("bswapq %0" \
310 /* gcc seems to figure out optimal code for this on its own */
311 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
312 # ifndef GUINT32_SWAP_LE_BE
313 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86_64 (val))
315 # ifndef GUINT64_SWAP_LE_BE
316 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86_64 (val))
318 # else /* generic gcc */
319 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
320 # ifndef GUINT32_SWAP_LE_BE
321 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
323 # ifndef GUINT64_SWAP_LE_BE
324 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
328 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
329 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
330 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
333 #define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
334 #define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
335 #define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
336 (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
337 (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
338 #define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
339 (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
340 (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
342 /* The G*_TO_?E() macros are defined in glibconfig.h.
343 * The transformation is symmetric, so the FROM just maps to the TO.
345 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
346 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
347 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
348 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
349 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
350 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
351 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
352 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
354 #define GINT64_FROM_LE(val) (GINT64_TO_LE (val))
355 #define GUINT64_FROM_LE(val) (GUINT64_TO_LE (val))
356 #define GINT64_FROM_BE(val) (GINT64_TO_BE (val))
357 #define GUINT64_FROM_BE(val) (GUINT64_TO_BE (val))
359 #define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
360 #define GULONG_FROM_LE(val) (GULONG_TO_LE (val))
361 #define GLONG_FROM_BE(val) (GLONG_TO_BE (val))
362 #define GULONG_FROM_BE(val) (GULONG_TO_BE (val))
364 #define GINT_FROM_LE(val) (GINT_TO_LE (val))
365 #define GUINT_FROM_LE(val) (GUINT_TO_LE (val))
366 #define GINT_FROM_BE(val) (GINT_TO_BE (val))
367 #define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
369 #define GSIZE_FROM_LE(val) (GSIZE_TO_LE (val))
370 #define GSSIZE_FROM_LE(val) (GSSIZE_TO_LE (val))
371 #define GSIZE_FROM_BE(val) (GSIZE_TO_BE (val))
372 #define GSSIZE_FROM_BE(val) (GSSIZE_TO_BE (val))
374 /* Portable versions of host-network order stuff
376 #define g_ntohl(val) (GUINT32_FROM_BE (val))
377 #define g_ntohs(val) (GUINT16_FROM_BE (val))
378 #define g_htonl(val) (GUINT32_TO_BE (val))
379 #define g_htons(val) (GUINT16_TO_BE (val))
381 /* Overflow-checked unsigned integer arithmetic
383 #ifndef _GLIB_TEST_OVERFLOW_FALLBACK
384 /* https://bugzilla.gnome.org/show_bug.cgi?id=769104 */
385 #if __GNUC__ >= 5 && !defined(__INTEL_COMPILER)
386 #define _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS
387 #elif __has_builtin(__builtin_uadd_overflow)
388 #define _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS
392 #define g_uint_checked_add(dest, a, b) \
393 _GLIB_CHECKED_ADD_U32(dest, a, b)
394 #define g_uint_checked_mul(dest, a, b) \
395 _GLIB_CHECKED_MUL_U32(dest, a, b)
397 #define g_uint64_checked_add(dest, a, b) \
398 _GLIB_CHECKED_ADD_U64(dest, a, b)
399 #define g_uint64_checked_mul(dest, a, b) \
400 _GLIB_CHECKED_MUL_U64(dest, a, b)
402 #if GLIB_SIZEOF_SIZE_T == 8
403 #define g_size_checked_add(dest, a, b) \
404 _GLIB_CHECKED_ADD_U64(dest, a, b)
405 #define g_size_checked_mul(dest, a, b) \
406 _GLIB_CHECKED_MUL_U64(dest, a, b)
408 #define g_size_checked_add(dest, a, b) \
409 _GLIB_CHECKED_ADD_U32(dest, a, b)
410 #define g_size_checked_mul(dest, a, b) \
411 _GLIB_CHECKED_MUL_U32(dest, a, b)
414 /* The names of the following inlines are private. Use the macro
417 #ifdef _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS
418 static inline gboolean
_GLIB_CHECKED_ADD_U32 (guint32
*dest
, guint32 a
, guint32 b
) {
419 return !__builtin_uadd_overflow(a
, b
, dest
); }
420 static inline gboolean
_GLIB_CHECKED_MUL_U32 (guint32
*dest
, guint32 a
, guint32 b
) {
421 return !__builtin_umul_overflow(a
, b
, dest
); }
422 static inline gboolean
_GLIB_CHECKED_ADD_U64 (guint64
*dest
, guint64 a
, guint64 b
) {
423 G_STATIC_ASSERT(sizeof (unsigned long long) == sizeof (guint64
));
424 return !__builtin_uaddll_overflow(a
, b
, (unsigned long long *) dest
); }
425 static inline gboolean
_GLIB_CHECKED_MUL_U64 (guint64
*dest
, guint64 a
, guint64 b
) {
426 return !__builtin_umulll_overflow(a
, b
, (unsigned long long *) dest
); }
428 static inline gboolean
_GLIB_CHECKED_ADD_U32 (guint32
*dest
, guint32 a
, guint32 b
) {
429 *dest
= a
+ b
; return *dest
>= a
; }
430 static inline gboolean
_GLIB_CHECKED_MUL_U32 (guint32
*dest
, guint32 a
, guint32 b
) {
431 *dest
= a
* b
; return !a
|| *dest
/ a
== b
; }
432 static inline gboolean
_GLIB_CHECKED_ADD_U64 (guint64
*dest
, guint64 a
, guint64 b
) {
433 *dest
= a
+ b
; return *dest
>= a
; }
434 static inline gboolean
_GLIB_CHECKED_MUL_U64 (guint64
*dest
, guint64 a
, guint64 b
) {
435 *dest
= a
* b
; return !a
|| *dest
/ a
== b
; }
438 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
441 * +--------+---------------+---------------+
442 * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
443 * +--------+---------------+---------------+
444 * B0------------------->B1------->B2-->B3-->
446 * IEEE Standard 754 Double Precision Storage Format (gdouble):
448 * 63 62 52 51 32 31 0
449 * +--------+----------------+----------------+ +---------------+
450 * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
451 * +--------+----------------+----------------+ +---------------+
452 * B0--------------->B1---------->B2--->B3----> B4->B5->B6->B7->
454 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
455 typedef union _GDoubleIEEE754 GDoubleIEEE754
;
456 typedef union _GFloatIEEE754 GFloatIEEE754
;
457 #define G_IEEE754_FLOAT_BIAS (127)
458 #define G_IEEE754_DOUBLE_BIAS (1023)
459 /* multiply with base2 exponent to get base10 exponent (normal numbers) */
460 #define G_LOG_2_BASE_10 (0.30102999566398119521)
461 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
467 guint biased_exponent
: 8;
471 union _GDoubleIEEE754
475 guint mantissa_low
: 32;
476 guint mantissa_high
: 20;
477 guint biased_exponent
: 11;
481 #elif G_BYTE_ORDER == G_BIG_ENDIAN
487 guint biased_exponent
: 8;
491 union _GDoubleIEEE754
496 guint biased_exponent
: 11;
497 guint mantissa_high
: 20;
498 guint mantissa_low
: 32;
501 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
502 #error unknown ENDIAN type
503 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
505 typedef struct _GTimeVal GTimeVal
;
515 /* We prefix variable declarations so they can
516 * properly get exported in Windows DLLs.
519 # ifdef G_PLATFORM_WIN32
520 # ifdef GLIB_STATIC_COMPILATION
521 # define GLIB_VAR extern
522 # else /* !GLIB_STATIC_COMPILATION */
523 # ifdef GLIB_COMPILATION
525 # define GLIB_VAR __declspec(dllexport)
526 # else /* !DLL_EXPORT */
527 # define GLIB_VAR extern
528 # endif /* !DLL_EXPORT */
529 # else /* !GLIB_COMPILATION */
530 # define GLIB_VAR extern __declspec(dllimport)
531 # endif /* !GLIB_COMPILATION */
532 # endif /* !GLIB_STATIC_COMPILATION */
533 # else /* !G_PLATFORM_WIN32 */
534 # define GLIB_VAR _GLIB_EXTERN
535 # endif /* !G_PLATFORM_WIN32 */
536 #endif /* GLIB_VAR */
538 #endif /* __G_TYPES_H__ */