1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
30 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
31 #error "Only <glib.h> can be included directly."
34 #include <glibconfig.h>
35 #include <glib/gmacros.h>
36 #include <glib/gversionmacros.h>
41 /* Provide type definitions for commonly used types.
42 * These are useful because a "gint8" can be adjusted
43 * to be 1 byte (8 bits) on all platforms. Similarly and
44 * more importantly, "gint32" can be adjusted to be
45 * 4 bytes (32 bits) on all platforms.
52 typedef gint gboolean;
54 typedef unsigned char guchar;
55 typedef unsigned short gushort;
56 typedef unsigned long gulong;
57 typedef unsigned int guint;
60 typedef double gdouble;
62 /* Define min and max constants for the fixed size numerical types */
64 * G_MININT8: (value -128)
66 * The minimum value which can be held in a #gint8.
70 #define G_MININT8 ((gint8) (-G_MAXINT8 - 1))
71 #define G_MAXINT8 ((gint8) 0x7f)
72 #define G_MAXUINT8 ((guint8) 0xff)
75 * G_MININT16: (value -32768)
77 * The minimum value which can be held in a #gint16.
81 #define G_MININT16 ((gint16) (-G_MAXINT16 - 1))
82 #define G_MAXINT16 ((gint16) 0x7fff)
83 #define G_MAXUINT16 ((guint16) 0xffff)
86 * G_MININT32: (value -2147483648)
88 * The minimum value which can be held in a #gint32.
92 #define G_MININT32 ((gint32) (-G_MAXINT32 - 1))
93 #define G_MAXINT32 ((gint32) 0x7fffffff)
94 #define G_MAXUINT32 ((guint32) 0xffffffff)
97 * G_MININT64: (value -9223372036854775808)
99 * The minimum value which can be held in a #gint64.
101 #define G_MININT64 ((gint64) (-G_MAXINT64 - G_GINT64_CONSTANT(1)))
102 #define G_MAXINT64 G_GINT64_CONSTANT(0x7fffffffffffffff)
103 #define G_MAXUINT64 G_GUINT64_CONSTANT(0xffffffffffffffff)
105 typedef void* gpointer;
106 typedef const void *gconstpointer;
108 typedef gint (*GCompareFunc) (gconstpointer a,
110 typedef gint (*GCompareDataFunc) (gconstpointer a,
113 typedef gboolean (*GEqualFunc) (gconstpointer a,
119 * @b: a value to compare with
120 * @user_data: user data provided by the caller
122 * Specifies the type of a function used to test two values for
123 * equality. The function should return %TRUE if both values are equal
124 * and %FALSE otherwise.
126 * This is a version of #GEqualFunc which provides a @user_data closure from
129 * Returns: %TRUE if @a = @b; %FALSE otherwise
132 typedef gboolean (*GEqualFuncFull) (gconstpointer a,
136 typedef void (*GDestroyNotify) (gpointer data);
137 typedef void (*GFunc) (gpointer data,
139 typedef guint (*GHashFunc) (gconstpointer key);
140 typedef void (*GHFunc) (gpointer key,
146 * @src: (not nullable): A pointer to the data which should be copied
147 * @user_data: Additional data
149 * A function of this signature is used to copy the node data
150 * when doing a deep-copy of a tree.
152 * Returns: (not nullable): A pointer to the copy
156 typedef gpointer (*GCopyFunc) (gconstpointer src,
160 * @data: a data pointer
162 * Declares a type of function which takes an arbitrary
163 * data pointer argument and has no return value. It is
164 * not currently used in GLib or GTK+.
166 typedef void (*GFreeFunc) (gpointer data);
170 * @str: the untranslated string
171 * @user_data: user data specified when installing the function, e.g.
172 * in g_option_group_set_translate_func()
174 * The type of functions which are used to translate user-visible
175 * strings, for <option>--help</option> output.
177 * Returns: a translation of the string for the current locale.
178 * The returned string is owned by GLib and must not be freed.
180 typedef const gchar * (*GTranslateFunc) (const gchar *str,
184 /* Define some mathematical constants that aren't available
185 * symbolically in some strict ISO C implementations.
187 * Note that the large number of digits used in these definitions
188 * doesn't imply that GLib or current computers in general would be
189 * able to handle floating point numbers with an accuracy like this.
190 * It's mostly an exercise in futility and future proofing. For
191 * extended precision floating point support, look somewhere else
194 #define G_E 2.7182818284590452353602874713526624977572470937000
195 #define G_LN2 0.69314718055994530941723212145817656807550013436026
196 #define G_LN10 2.3025850929940456840179914546843642076011014886288
197 #define G_PI 3.1415926535897932384626433832795028841971693993751
198 #define G_PI_2 1.5707963267948966192313216916397514420985846996876
199 #define G_PI_4 0.78539816339744830961566084581987572104929234984378
200 #define G_SQRT2 1.4142135623730950488016887242096980785696718753769
202 /* Portable endian checks and conversions
204 * glibconfig.h defines G_BYTE_ORDER which expands to one of
207 #define G_LITTLE_ENDIAN 1234
208 #define G_BIG_ENDIAN 4321
209 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
212 /* Basic bit swapping functions
214 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
215 (guint16) ((guint16) (val) >> 8) | \
216 (guint16) ((guint16) (val) << 8)))
218 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((guint32) ( \
219 (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
220 (((guint32) (val) & (guint32) 0x0000ff00U) << 8) | \
221 (((guint32) (val) & (guint32) 0x00ff0000U) >> 8) | \
222 (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
224 #define GUINT64_SWAP_LE_BE_CONSTANT(val) ((guint64) ( \
225 (((guint64) (val) & \
226 (guint64) G_GINT64_CONSTANT (0x00000000000000ffU)) << 56) | \
227 (((guint64) (val) & \
228 (guint64) G_GINT64_CONSTANT (0x000000000000ff00U)) << 40) | \
229 (((guint64) (val) & \
230 (guint64) G_GINT64_CONSTANT (0x0000000000ff0000U)) << 24) | \
231 (((guint64) (val) & \
232 (guint64) G_GINT64_CONSTANT (0x00000000ff000000U)) << 8) | \
233 (((guint64) (val) & \
234 (guint64) G_GINT64_CONSTANT (0x000000ff00000000U)) >> 8) | \
235 (((guint64) (val) & \
236 (guint64) G_GINT64_CONSTANT (0x0000ff0000000000U)) >> 24) | \
237 (((guint64) (val) & \
238 (guint64) G_GINT64_CONSTANT (0x00ff000000000000U)) >> 40) | \
239 (((guint64) (val) & \
240 (guint64) G_GINT64_CONSTANT (0xff00000000000000U)) >> 56)))
242 /* Arch specific stuff for speed
244 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
246 # if __GNUC__ >= 4 && defined (__GNUC_MINOR__) && __GNUC_MINOR__ >= 3
247 # define GUINT32_SWAP_LE_BE(val) ((guint32) __builtin_bswap32 ((guint32) (val)))
248 # define GUINT64_SWAP_LE_BE(val) ((guint64) __builtin_bswap64 ((guint64) (val)))
251 # if defined (__i386__)
252 # define GUINT16_SWAP_LE_BE_IA32(val) \
254 ({ guint16 __v, __x = ((guint16) (val)); \
255 if (__builtin_constant_p (__x)) \
256 __v = GUINT16_SWAP_LE_BE_CONSTANT (__x); \
258 __asm__ ("rorw $8, %w0" \
263 # if !defined (__i486__) && !defined (__i586__) \
264 && !defined (__pentium__) && !defined (__i686__) \
265 && !defined (__pentiumpro__) && !defined (__pentium4__)
266 # define GUINT32_SWAP_LE_BE_IA32(val) \
268 ({ guint32 __v, __x = ((guint32) (val)); \
269 if (__builtin_constant_p (__x)) \
270 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
272 __asm__ ("rorw $8, %w0\n\t" \
279 # else /* 486 and higher has bswap */
280 # define GUINT32_SWAP_LE_BE_IA32(val) \
282 ({ guint32 __v, __x = ((guint32) (val)); \
283 if (__builtin_constant_p (__x)) \
284 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
286 __asm__ ("bswap %0" \
290 # endif /* processor specific 32-bit stuff */
291 # define GUINT64_SWAP_LE_BE_IA32(val) \
293 ({ union { guint64 __ll; \
294 guint32 __l[2]; } __w, __r; \
295 __w.__ll = ((guint64) (val)); \
296 if (__builtin_constant_p (__w.__ll)) \
297 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (__w.__ll); \
300 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
301 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
304 /* Possibly just use the constant version and let gcc figure it out? */
305 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA32 (val))
306 # ifndef GUINT32_SWAP_LE_BE
307 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA32 (val))
309 # ifndef GUINT64_SWAP_LE_BE
310 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA32 (val))
312 # elif defined (__ia64__)
313 # define GUINT16_SWAP_LE_BE_IA64(val) \
315 ({ guint16 __v, __x = ((guint16) (val)); \
316 if (__builtin_constant_p (__x)) \
317 __v = GUINT16_SWAP_LE_BE_CONSTANT (__x); \
319 __asm__ __volatile__ ("shl %0 = %1, 48 ;;" \
320 "mux1 %0 = %0, @rev ;;" \
324 # define GUINT32_SWAP_LE_BE_IA64(val) \
326 ({ guint32 __v, __x = ((guint32) (val)); \
327 if (__builtin_constant_p (__x)) \
328 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
330 __asm__ __volatile__ ("shl %0 = %1, 32 ;;" \
331 "mux1 %0 = %0, @rev ;;" \
335 # define GUINT64_SWAP_LE_BE_IA64(val) \
337 ({ guint64 __v, __x = ((guint64) (val)); \
338 if (__builtin_constant_p (__x)) \
339 __v = GUINT64_SWAP_LE_BE_CONSTANT (__x); \
341 __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;" \
345 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA64 (val))
346 # ifndef GUINT32_SWAP_LE_BE
347 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA64 (val))
349 # ifndef GUINT64_SWAP_LE_BE
350 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA64 (val))
352 # elif defined (__x86_64__)
353 # define GUINT32_SWAP_LE_BE_X86_64(val) \
355 ({ guint32 __v, __x = ((guint32) (val)); \
356 if (__builtin_constant_p (__x)) \
357 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
359 __asm__ ("bswapl %0" \
363 # define GUINT64_SWAP_LE_BE_X86_64(val) \
365 ({ guint64 __v, __x = ((guint64) (val)); \
366 if (__builtin_constant_p (__x)) \
367 __v = GUINT64_SWAP_LE_BE_CONSTANT (__x); \
369 __asm__ ("bswapq %0" \
373 /* gcc seems to figure out optimal code for this on its own */
374 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
375 # ifndef GUINT32_SWAP_LE_BE
376 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86_64 (val))
378 # ifndef GUINT64_SWAP_LE_BE
379 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86_64 (val))
381 # else /* generic gcc */
382 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
383 # ifndef GUINT32_SWAP_LE_BE
384 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
386 # ifndef GUINT64_SWAP_LE_BE
387 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
391 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
392 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
393 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
396 #define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
397 #define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
398 #define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
399 (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
400 (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
401 #define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
402 (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
403 (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
405 /* The G*_TO_?E() macros are defined in glibconfig.h.
406 * The transformation is symmetric, so the FROM just maps to the TO.
408 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
409 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
410 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
411 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
412 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
413 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
414 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
415 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
417 #define GINT64_FROM_LE(val) (GINT64_TO_LE (val))
418 #define GUINT64_FROM_LE(val) (GUINT64_TO_LE (val))
419 #define GINT64_FROM_BE(val) (GINT64_TO_BE (val))
420 #define GUINT64_FROM_BE(val) (GUINT64_TO_BE (val))
422 #define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
423 #define GULONG_FROM_LE(val) (GULONG_TO_LE (val))
424 #define GLONG_FROM_BE(val) (GLONG_TO_BE (val))
425 #define GULONG_FROM_BE(val) (GULONG_TO_BE (val))
427 #define GINT_FROM_LE(val) (GINT_TO_LE (val))
428 #define GUINT_FROM_LE(val) (GUINT_TO_LE (val))
429 #define GINT_FROM_BE(val) (GINT_TO_BE (val))
430 #define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
432 #define GSIZE_FROM_LE(val) (GSIZE_TO_LE (val))
433 #define GSSIZE_FROM_LE(val) (GSSIZE_TO_LE (val))
434 #define GSIZE_FROM_BE(val) (GSIZE_TO_BE (val))
435 #define GSSIZE_FROM_BE(val) (GSSIZE_TO_BE (val))
437 /* Portable versions of host-network order stuff
439 #define g_ntohl(val) (GUINT32_FROM_BE (val))
440 #define g_ntohs(val) (GUINT16_FROM_BE (val))
441 #define g_htonl(val) (GUINT32_TO_BE (val))
442 #define g_htons(val) (GUINT16_TO_BE (val))
444 /* Overflow-checked unsigned integer arithmetic
446 #ifndef _GLIB_TEST_OVERFLOW_FALLBACK
447 /* https://bugzilla.gnome.org/show_bug.cgi?id=769104 */
448 #if __GNUC__ >= 5 && !defined(__INTEL_COMPILER)
449 #define _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS
450 #elif g_macro__has_builtin(__builtin_add_overflow)
451 #define _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS
455 #ifdef _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS
457 #define g_uint_checked_add(dest, a, b) \
458 (!__builtin_add_overflow(a, b, dest))
459 #define g_uint_checked_mul(dest, a, b) \
460 (!__builtin_mul_overflow(a, b, dest))
462 #define g_uint64_checked_add(dest, a, b) \
463 (!__builtin_add_overflow(a, b, dest))
464 #define g_uint64_checked_mul(dest, a, b) \
465 (!__builtin_mul_overflow(a, b, dest))
467 #define g_size_checked_add(dest, a, b) \
468 (!__builtin_add_overflow(a, b, dest))
469 #define g_size_checked_mul(dest, a, b) \
470 (!__builtin_mul_overflow(a, b, dest))
472 #else /* !_GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS */
474 /* The names of the following inlines are private. Use the macro
477 static inline gboolean _GLIB_CHECKED_ADD_UINT (guint *dest, guint a, guint b) {
478 *dest = a + b; return *dest >= a; }
479 static inline gboolean _GLIB_CHECKED_MUL_UINT (guint *dest, guint a, guint b) {
480 *dest = a * b; return !a || *dest / a == b; }
481 static inline gboolean _GLIB_CHECKED_ADD_UINT64 (guint64 *dest, guint64 a, guint64 b) {
482 *dest = a + b; return *dest >= a; }
483 static inline gboolean _GLIB_CHECKED_MUL_UINT64 (guint64 *dest, guint64 a, guint64 b) {
484 *dest = a * b; return !a || *dest / a == b; }
485 static inline gboolean _GLIB_CHECKED_ADD_SIZE (gsize *dest, gsize a, gsize b) {
486 *dest = a + b; return *dest >= a; }
487 static inline gboolean _GLIB_CHECKED_MUL_SIZE (gsize *dest, gsize a, gsize b) {
488 *dest = a * b; return !a || *dest / a == b; }
490 #define g_uint_checked_add(dest, a, b) \
491 _GLIB_CHECKED_ADD_UINT(dest, a, b)
492 #define g_uint_checked_mul(dest, a, b) \
493 _GLIB_CHECKED_MUL_UINT(dest, a, b)
495 #define g_uint64_checked_add(dest, a, b) \
496 _GLIB_CHECKED_ADD_UINT64(dest, a, b)
497 #define g_uint64_checked_mul(dest, a, b) \
498 _GLIB_CHECKED_MUL_UINT64(dest, a, b)
500 #define g_size_checked_add(dest, a, b) \
501 _GLIB_CHECKED_ADD_SIZE(dest, a, b)
502 #define g_size_checked_mul(dest, a, b) \
503 _GLIB_CHECKED_MUL_SIZE(dest, a, b)
505 #endif /* !_GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS */
507 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
510 * +--------+---------------+---------------+
511 * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
512 * +--------+---------------+---------------+
513 * B0------------------->B1------->B2-->B3-->
515 * IEEE Standard 754 Double Precision Storage Format (gdouble):
517 * 63 62 52 51 32 31 0
518 * +--------+----------------+----------------+ +---------------+
519 * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
520 * +--------+----------------+----------------+ +---------------+
521 * B0--------------->B1---------->B2--->B3----> B4->B5->B6->B7->
523 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
524 typedef union _GDoubleIEEE754 GDoubleIEEE754;
525 typedef union _GFloatIEEE754 GFloatIEEE754;
526 #define G_IEEE754_FLOAT_BIAS (127)
527 #define G_IEEE754_DOUBLE_BIAS (1023)
528 /* multiply with base2 exponent to get base10 exponent (normal numbers) */
529 #define G_LOG_2_BASE_10 (0.30102999566398119521)
530 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
536 guint biased_exponent : 8;
540 union _GDoubleIEEE754
544 guint mantissa_low : 32;
545 guint mantissa_high : 20;
546 guint biased_exponent : 11;
550 #elif G_BYTE_ORDER == G_BIG_ENDIAN
556 guint biased_exponent : 8;
560 union _GDoubleIEEE754
565 guint biased_exponent : 11;
566 guint mantissa_high : 20;
567 guint mantissa_low : 32;
570 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
571 #error unknown ENDIAN type
572 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
574 typedef struct _GTimeVal GTimeVal GLIB_DEPRECATED_TYPE_IN_2_62_FOR(GDateTime);
580 } GLIB_DEPRECATED_TYPE_IN_2_62_FOR(GDateTime);
582 typedef gint grefcount;
583 typedef gint gatomicrefcount; /* should be accessed only using atomics */
587 /* We prefix variable declarations so they can
588 * properly get exported in Windows DLLs.
591 # ifdef G_PLATFORM_WIN32
592 # ifdef GLIB_STATIC_COMPILATION
593 # define GLIB_VAR extern
594 # else /* !GLIB_STATIC_COMPILATION */
595 # ifdef GLIB_COMPILATION
597 # define GLIB_VAR extern __declspec(dllexport)
598 # else /* !DLL_EXPORT */
599 # define GLIB_VAR extern
600 # endif /* !DLL_EXPORT */
601 # else /* !GLIB_COMPILATION */
602 # define GLIB_VAR extern __declspec(dllimport)
603 # endif /* !GLIB_COMPILATION */
604 # endif /* !GLIB_STATIC_COMPILATION */
605 # else /* !G_PLATFORM_WIN32 */
606 # define GLIB_VAR _GLIB_EXTERN
607 # endif /* !G_PLATFORM_WIN32 */
608 #endif /* GLIB_VAR */
610 #endif /* __G_TYPES_H__ */