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.1 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 */
62 * G_MININT8: (value -128)
64 * The minimum value which can be held in a #gint8.
68 #define G_MININT8 ((gint8) (-G_MAXINT8 - 1))
69 #define G_MAXINT8 ((gint8) 0x7f)
70 #define G_MAXUINT8 ((guint8) 0xff)
73 * G_MININT16: (value -32768)
75 * The minimum value which can be held in a #gint16.
79 #define G_MININT16 ((gint16) (-G_MAXINT16 - 1))
80 #define G_MAXINT16 ((gint16) 0x7fff)
81 #define G_MAXUINT16 ((guint16) 0xffff)
84 * G_MININT32: (value -2147483648)
86 * The minimum value which can be held in a #gint32.
90 #define G_MININT32 ((gint32) (-G_MAXINT32 - 1))
91 #define G_MAXINT32 ((gint32) 0x7fffffff)
92 #define G_MAXUINT32 ((guint32) 0xffffffff)
95 * G_MININT64: (value -9223372036854775808)
97 * The minimum value which can be held in a #gint64.
99 #define G_MININT64 ((gint64) (-G_MAXINT64 - G_GINT64_CONSTANT(1)))
100 #define G_MAXINT64 G_GINT64_CONSTANT(0x7fffffffffffffff)
101 #define G_MAXUINT64 G_GUINT64_CONSTANT(0xffffffffffffffff)
103 typedef void* gpointer;
104 typedef const void *gconstpointer;
106 typedef gint (*GCompareFunc) (gconstpointer a,
108 typedef gint (*GCompareDataFunc) (gconstpointer a,
111 typedef gboolean (*GEqualFunc) (gconstpointer a,
113 typedef void (*GDestroyNotify) (gpointer data);
114 typedef void (*GFunc) (gpointer data,
116 typedef guint (*GHashFunc) (gconstpointer key);
117 typedef void (*GHFunc) (gpointer key,
123 * @src: (not nullable): A pointer to the data which should be copied
124 * @data: Additional data
126 * A function of this signature is used to copy the node data
127 * when doing a deep-copy of a tree.
129 * Returns: (not nullable): A pointer to the copy
133 typedef gpointer (*GCopyFunc) (gconstpointer src,
137 * @data: a data pointer
139 * Declares a type of function which takes an arbitrary
140 * data pointer argument and has no return value. It is
141 * not currently used in GLib or GTK+.
143 typedef void (*GFreeFunc) (gpointer data);
147 * @str: the untranslated string
148 * @data: user data specified when installing the function, e.g.
149 * in g_option_group_set_translate_func()
151 * The type of functions which are used to translate user-visible
152 * strings, for <option>--help</option> output.
154 * Returns: a translation of the string for the current locale.
155 * The returned string is owned by GLib and must not be freed.
157 typedef const gchar * (*GTranslateFunc) (const gchar *str,
161 /* Define some mathematical constants that aren't available
162 * symbolically in some strict ISO C implementations.
164 * Note that the large number of digits used in these definitions
165 * doesn't imply that GLib or current computers in general would be
166 * able to handle floating point numbers with an accuracy like this.
167 * It's mostly an exercise in futility and future proofing. For
168 * extended precision floating point support, look somewhere else
171 #define G_E 2.7182818284590452353602874713526624977572470937000
172 #define G_LN2 0.69314718055994530941723212145817656807550013436026
173 #define G_LN10 2.3025850929940456840179914546843642076011014886288
174 #define G_PI 3.1415926535897932384626433832795028841971693993751
175 #define G_PI_2 1.5707963267948966192313216916397514420985846996876
176 #define G_PI_4 0.78539816339744830961566084581987572104929234984378
177 #define G_SQRT2 1.4142135623730950488016887242096980785696718753769
179 /* Portable endian checks and conversions
181 * glibconfig.h defines G_BYTE_ORDER which expands to one of
184 #define G_LITTLE_ENDIAN 1234
185 #define G_BIG_ENDIAN 4321
186 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
189 /* Basic bit swapping functions
191 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
192 (guint16) ((guint16) (val) >> 8) | \
193 (guint16) ((guint16) (val) << 8)))
195 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((guint32) ( \
196 (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
197 (((guint32) (val) & (guint32) 0x0000ff00U) << 8) | \
198 (((guint32) (val) & (guint32) 0x00ff0000U) >> 8) | \
199 (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
201 #define GUINT64_SWAP_LE_BE_CONSTANT(val) ((guint64) ( \
202 (((guint64) (val) & \
203 (guint64) G_GINT64_CONSTANT (0x00000000000000ffU)) << 56) | \
204 (((guint64) (val) & \
205 (guint64) G_GINT64_CONSTANT (0x000000000000ff00U)) << 40) | \
206 (((guint64) (val) & \
207 (guint64) G_GINT64_CONSTANT (0x0000000000ff0000U)) << 24) | \
208 (((guint64) (val) & \
209 (guint64) G_GINT64_CONSTANT (0x00000000ff000000U)) << 8) | \
210 (((guint64) (val) & \
211 (guint64) G_GINT64_CONSTANT (0x000000ff00000000U)) >> 8) | \
212 (((guint64) (val) & \
213 (guint64) G_GINT64_CONSTANT (0x0000ff0000000000U)) >> 24) | \
214 (((guint64) (val) & \
215 (guint64) G_GINT64_CONSTANT (0x00ff000000000000U)) >> 40) | \
216 (((guint64) (val) & \
217 (guint64) G_GINT64_CONSTANT (0xff00000000000000U)) >> 56)))
219 /* Arch specific stuff for speed
221 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
223 # if __GNUC__ >= 4 && defined (__GNUC_MINOR__) && __GNUC_MINOR__ >= 3
224 # define GUINT32_SWAP_LE_BE(val) ((guint32) __builtin_bswap32 ((guint32) (val)))
225 # define GUINT64_SWAP_LE_BE(val) ((guint64) __builtin_bswap64 ((guint64) (val)))
228 # if defined (__i386__)
229 # define GUINT16_SWAP_LE_BE_IA32(val) \
231 ({ guint16 __v, __x = ((guint16) (val)); \
232 if (__builtin_constant_p (__x)) \
233 __v = GUINT16_SWAP_LE_BE_CONSTANT (__x); \
235 __asm__ ("rorw $8, %w0" \
240 # if !defined (__i486__) && !defined (__i586__) \
241 && !defined (__pentium__) && !defined (__i686__) \
242 && !defined (__pentiumpro__) && !defined (__pentium4__)
243 # define GUINT32_SWAP_LE_BE_IA32(val) \
245 ({ guint32 __v, __x = ((guint32) (val)); \
246 if (__builtin_constant_p (__x)) \
247 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
249 __asm__ ("rorw $8, %w0\n\t" \
256 # else /* 486 and higher has bswap */
257 # define GUINT32_SWAP_LE_BE_IA32(val) \
259 ({ guint32 __v, __x = ((guint32) (val)); \
260 if (__builtin_constant_p (__x)) \
261 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
263 __asm__ ("bswap %0" \
267 # endif /* processor specific 32-bit stuff */
268 # define GUINT64_SWAP_LE_BE_IA32(val) \
270 ({ union { guint64 __ll; \
271 guint32 __l[2]; } __w, __r; \
272 __w.__ll = ((guint64) (val)); \
273 if (__builtin_constant_p (__w.__ll)) \
274 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (__w.__ll); \
277 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
278 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
281 /* Possibly just use the constant version and let gcc figure it out? */
282 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA32 (val))
283 # ifndef GUINT32_SWAP_LE_BE
284 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA32 (val))
286 # ifndef GUINT64_SWAP_LE_BE
287 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA32 (val))
289 # elif defined (__ia64__)
290 # define GUINT16_SWAP_LE_BE_IA64(val) \
292 ({ guint16 __v, __x = ((guint16) (val)); \
293 if (__builtin_constant_p (__x)) \
294 __v = GUINT16_SWAP_LE_BE_CONSTANT (__x); \
296 __asm__ __volatile__ ("shl %0 = %1, 48 ;;" \
297 "mux1 %0 = %0, @rev ;;" \
301 # define GUINT32_SWAP_LE_BE_IA64(val) \
303 ({ guint32 __v, __x = ((guint32) (val)); \
304 if (__builtin_constant_p (__x)) \
305 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
307 __asm__ __volatile__ ("shl %0 = %1, 32 ;;" \
308 "mux1 %0 = %0, @rev ;;" \
312 # define GUINT64_SWAP_LE_BE_IA64(val) \
314 ({ guint64 __v, __x = ((guint64) (val)); \
315 if (__builtin_constant_p (__x)) \
316 __v = GUINT64_SWAP_LE_BE_CONSTANT (__x); \
318 __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;" \
322 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA64 (val))
323 # ifndef GUINT32_SWAP_LE_BE
324 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA64 (val))
326 # ifndef GUINT64_SWAP_LE_BE
327 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA64 (val))
329 # elif defined (__x86_64__)
330 # define GUINT32_SWAP_LE_BE_X86_64(val) \
332 ({ guint32 __v, __x = ((guint32) (val)); \
333 if (__builtin_constant_p (__x)) \
334 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
336 __asm__ ("bswapl %0" \
340 # define GUINT64_SWAP_LE_BE_X86_64(val) \
342 ({ guint64 __v, __x = ((guint64) (val)); \
343 if (__builtin_constant_p (__x)) \
344 __v = GUINT64_SWAP_LE_BE_CONSTANT (__x); \
346 __asm__ ("bswapq %0" \
350 /* gcc seems to figure out optimal code for this on its own */
351 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
352 # ifndef GUINT32_SWAP_LE_BE
353 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86_64 (val))
355 # ifndef GUINT64_SWAP_LE_BE
356 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86_64 (val))
358 # else /* generic gcc */
359 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
360 # ifndef GUINT32_SWAP_LE_BE
361 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
363 # ifndef GUINT64_SWAP_LE_BE
364 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
368 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
369 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
370 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
373 #define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
374 #define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
375 #define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
376 (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
377 (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
378 #define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
379 (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
380 (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
382 /* The G*_TO_?E() macros are defined in glibconfig.h.
383 * The transformation is symmetric, so the FROM just maps to the TO.
385 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
386 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
387 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
388 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
389 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
390 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
391 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
392 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
394 #define GINT64_FROM_LE(val) (GINT64_TO_LE (val))
395 #define GUINT64_FROM_LE(val) (GUINT64_TO_LE (val))
396 #define GINT64_FROM_BE(val) (GINT64_TO_BE (val))
397 #define GUINT64_FROM_BE(val) (GUINT64_TO_BE (val))
399 #define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
400 #define GULONG_FROM_LE(val) (GULONG_TO_LE (val))
401 #define GLONG_FROM_BE(val) (GLONG_TO_BE (val))
402 #define GULONG_FROM_BE(val) (GULONG_TO_BE (val))
404 #define GINT_FROM_LE(val) (GINT_TO_LE (val))
405 #define GUINT_FROM_LE(val) (GUINT_TO_LE (val))
406 #define GINT_FROM_BE(val) (GINT_TO_BE (val))
407 #define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
409 #define GSIZE_FROM_LE(val) (GSIZE_TO_LE (val))
410 #define GSSIZE_FROM_LE(val) (GSSIZE_TO_LE (val))
411 #define GSIZE_FROM_BE(val) (GSIZE_TO_BE (val))
412 #define GSSIZE_FROM_BE(val) (GSSIZE_TO_BE (val))
414 /* Portable versions of host-network order stuff
416 #define g_ntohl(val) (GUINT32_FROM_BE (val))
417 #define g_ntohs(val) (GUINT16_FROM_BE (val))
418 #define g_htonl(val) (GUINT32_TO_BE (val))
419 #define g_htons(val) (GUINT16_TO_BE (val))
421 /* Overflow-checked unsigned integer arithmetic
423 #ifndef _GLIB_TEST_OVERFLOW_FALLBACK
424 /* https://bugzilla.gnome.org/show_bug.cgi?id=769104 */
425 #if __GNUC__ >= 5 && !defined(__INTEL_COMPILER)
426 #define _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS
427 #elif g_macro__has_builtin(__builtin_uadd_overflow)
428 #define _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS
432 #define g_uint_checked_add(dest, a, b) \
433 _GLIB_CHECKED_ADD_U32(dest, a, b)
434 #define g_uint_checked_mul(dest, a, b) \
435 _GLIB_CHECKED_MUL_U32(dest, a, b)
437 #define g_uint64_checked_add(dest, a, b) \
438 _GLIB_CHECKED_ADD_U64(dest, a, b)
439 #define g_uint64_checked_mul(dest, a, b) \
440 _GLIB_CHECKED_MUL_U64(dest, a, b)
442 #if GLIB_SIZEOF_SIZE_T == 8
443 #define g_size_checked_add(dest, a, b) \
444 _GLIB_CHECKED_ADD_U64(dest, a, b)
445 #define g_size_checked_mul(dest, a, b) \
446 _GLIB_CHECKED_MUL_U64(dest, a, b)
448 #define g_size_checked_add(dest, a, b) \
449 _GLIB_CHECKED_ADD_U32(dest, a, b)
450 #define g_size_checked_mul(dest, a, b) \
451 _GLIB_CHECKED_MUL_U32(dest, a, b)
454 /* FIXME: Hide this from gtkdoc scanner because it confuses its poor regexes.
455 * https://gitlab.gnome.org/GNOME/gtk-doc/issues/90 */
456 #ifndef __GTK_DOC_IGNORE__
457 /* The names of the following inlines are private. Use the macro
460 #ifdef _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS
461 static inline gboolean _GLIB_CHECKED_ADD_U32 (guint32 *dest, guint32 a, guint32 b) {
462 return !__builtin_uadd_overflow(a, b, dest); }
463 static inline gboolean _GLIB_CHECKED_MUL_U32 (guint32 *dest, guint32 a, guint32 b) {
464 return !__builtin_umul_overflow(a, b, dest); }
465 static inline gboolean _GLIB_CHECKED_ADD_U64 (guint64 *dest, guint64 a, guint64 b) {
466 G_STATIC_ASSERT(sizeof (unsigned long long) == sizeof (guint64));
467 return !__builtin_uaddll_overflow(a, b, (unsigned long long *) dest); }
468 static inline gboolean _GLIB_CHECKED_MUL_U64 (guint64 *dest, guint64 a, guint64 b) {
469 return !__builtin_umulll_overflow(a, b, (unsigned long long *) dest); }
471 static inline gboolean _GLIB_CHECKED_ADD_U32 (guint32 *dest, guint32 a, guint32 b) {
472 *dest = a + b; return *dest >= a; }
473 static inline gboolean _GLIB_CHECKED_MUL_U32 (guint32 *dest, guint32 a, guint32 b) {
474 *dest = a * b; return !a || *dest / a == b; }
475 static inline gboolean _GLIB_CHECKED_ADD_U64 (guint64 *dest, guint64 a, guint64 b) {
476 *dest = a + b; return *dest >= a; }
477 static inline gboolean _GLIB_CHECKED_MUL_U64 (guint64 *dest, guint64 a, guint64 b) {
478 *dest = a * b; return !a || *dest / a == b; }
480 #endif /* __GTK_DOC_IGNORE__ */
482 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
485 * +--------+---------------+---------------+
486 * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
487 * +--------+---------------+---------------+
488 * B0------------------->B1------->B2-->B3-->
490 * IEEE Standard 754 Double Precision Storage Format (gdouble):
492 * 63 62 52 51 32 31 0
493 * +--------+----------------+----------------+ +---------------+
494 * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
495 * +--------+----------------+----------------+ +---------------+
496 * B0--------------->B1---------->B2--->B3----> B4->B5->B6->B7->
498 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
499 typedef union _GDoubleIEEE754 GDoubleIEEE754;
500 typedef union _GFloatIEEE754 GFloatIEEE754;
501 #define G_IEEE754_FLOAT_BIAS (127)
502 #define G_IEEE754_DOUBLE_BIAS (1023)
503 /* multiply with base2 exponent to get base10 exponent (normal numbers) */
504 #define G_LOG_2_BASE_10 (0.30102999566398119521)
505 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
511 guint biased_exponent : 8;
515 union _GDoubleIEEE754
519 guint mantissa_low : 32;
520 guint mantissa_high : 20;
521 guint biased_exponent : 11;
525 #elif G_BYTE_ORDER == G_BIG_ENDIAN
531 guint biased_exponent : 8;
535 union _GDoubleIEEE754
540 guint biased_exponent : 11;
541 guint mantissa_high : 20;
542 guint mantissa_low : 32;
545 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
546 #error unknown ENDIAN type
547 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
549 typedef struct _GTimeVal GTimeVal GLIB_DEPRECATED_TYPE_IN_2_62_FOR(GDateTime);
555 } GLIB_DEPRECATED_TYPE_IN_2_62_FOR(GDateTime);
557 typedef gint grefcount;
558 typedef volatile gint gatomicrefcount;
562 /* We prefix variable declarations so they can
563 * properly get exported in Windows DLLs.
566 # ifdef G_PLATFORM_WIN32
567 # ifdef GLIB_STATIC_COMPILATION
568 # define GLIB_VAR extern
569 # else /* !GLIB_STATIC_COMPILATION */
570 # ifdef GLIB_COMPILATION
572 # define GLIB_VAR __declspec(dllexport)
573 # else /* !DLL_EXPORT */
574 # define GLIB_VAR extern
575 # endif /* !DLL_EXPORT */
576 # else /* !GLIB_COMPILATION */
577 # define GLIB_VAR extern __declspec(dllimport)
578 # endif /* !GLIB_COMPILATION */
579 # endif /* !GLIB_STATIC_COMPILATION */
580 # else /* !G_PLATFORM_WIN32 */
581 # define GLIB_VAR _GLIB_EXTERN
582 # endif /* !G_PLATFORM_WIN32 */
583 #endif /* GLIB_VAR */
585 #endif /* __G_TYPES_H__ */