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>
38 /* Must be included after the 3 headers above */
39 #include <glib/glib-visibility.h>
45 /* Provide type definitions for commonly used types.
46 * These are useful because a "gint8" can be adjusted
47 * to be 1 byte (8 bits) on all platforms. Similarly and
48 * more importantly, "gint32" can be adjusted to be
49 * 4 bytes (32 bits) on all platforms.
56 typedef gint gboolean;
58 typedef unsigned char guchar;
59 typedef unsigned short gushort;
60 typedef unsigned long gulong;
61 typedef unsigned int guint;
64 typedef double gdouble;
66 /* Define min and max constants for the fixed size numerical types */
68 * G_MININT8: (value -128)
70 * The minimum value which can be held in a #gint8.
74 #define G_MININT8 ((gint8) (-G_MAXINT8 - 1))
75 #define G_MAXINT8 ((gint8) 0x7f)
76 #define G_MAXUINT8 ((guint8) 0xff)
79 * G_MININT16: (value -32768)
81 * The minimum value which can be held in a #gint16.
85 #define G_MININT16 ((gint16) (-G_MAXINT16 - 1))
86 #define G_MAXINT16 ((gint16) 0x7fff)
87 #define G_MAXUINT16 ((guint16) 0xffff)
90 * G_MININT32: (value -2147483648)
92 * The minimum value which can be held in a #gint32.
96 #define G_MININT32 ((gint32) (-G_MAXINT32 - 1))
97 #define G_MAXINT32 ((gint32) 0x7fffffff)
98 #define G_MAXUINT32 ((guint32) 0xffffffff)
101 * G_MININT64: (value -9223372036854775808)
103 * The minimum value which can be held in a #gint64.
105 #define G_MININT64 ((gint64) (-G_MAXINT64 - G_GINT64_CONSTANT(1)))
106 #define G_MAXINT64 G_GINT64_CONSTANT(0x7fffffffffffffff)
107 #define G_MAXUINT64 G_GUINT64_CONSTANT(0xffffffffffffffff)
109 typedef void* gpointer;
110 typedef const void *gconstpointer;
112 typedef gint (*GCompareFunc) (gconstpointer a,
114 typedef gint (*GCompareDataFunc) (gconstpointer a,
117 typedef gboolean (*GEqualFunc) (gconstpointer a,
123 * @b: a value to compare with
124 * @user_data: user data provided by the caller
126 * Specifies the type of a function used to test two values for
127 * equality. The function should return %TRUE if both values are equal
128 * and %FALSE otherwise.
130 * This is a version of #GEqualFunc which provides a @user_data closure from
133 * Returns: %TRUE if @a = @b; %FALSE otherwise
136 typedef gboolean (*GEqualFuncFull) (gconstpointer a,
140 typedef void (*GDestroyNotify) (gpointer data);
141 typedef void (*GFunc) (gpointer data,
143 typedef guint (*GHashFunc) (gconstpointer key);
144 typedef void (*GHFunc) (gpointer key,
150 * @src: (not nullable): A pointer to the data which should be copied
151 * @data: Additional data
153 * A function of this signature is used to copy the node data
154 * when doing a deep-copy of a tree.
156 * Returns: (not nullable): A pointer to the copy
160 typedef gpointer (*GCopyFunc) (gconstpointer src,
164 * @data: a data pointer
166 * Declares a type of function which takes an arbitrary
167 * data pointer argument and has no return value. It is
168 * not currently used in GLib or GTK.
170 typedef void (*GFreeFunc) (gpointer data);
174 * @str: the untranslated string
175 * @data: user data specified when installing the function, e.g.
176 * in g_option_group_set_translate_func()
178 * The type of functions which are used to translate user-visible
179 * strings, for <option>--help</option> output.
181 * Returns: a translation of the string for the current locale.
182 * The returned string is owned by GLib and must not be freed.
184 typedef const gchar * (*GTranslateFunc) (const gchar *str,
188 /* Define some mathematical constants that aren't available
189 * symbolically in some strict ISO C implementations.
191 * Note that the large number of digits used in these definitions
192 * doesn't imply that GLib or current computers in general would be
193 * able to handle floating point numbers with an accuracy like this.
194 * It's mostly an exercise in futility and future proofing. For
195 * extended precision floating point support, look somewhere else
198 #define G_E 2.7182818284590452353602874713526624977572470937000
199 #define G_LN2 0.69314718055994530941723212145817656807550013436026
200 #define G_LN10 2.3025850929940456840179914546843642076011014886288
201 #define G_PI 3.1415926535897932384626433832795028841971693993751
202 #define G_PI_2 1.5707963267948966192313216916397514420985846996876
203 #define G_PI_4 0.78539816339744830961566084581987572104929234984378
204 #define G_SQRT2 1.4142135623730950488016887242096980785696718753769
206 /* Portable endian checks and conversions
208 * glibconfig.h defines G_BYTE_ORDER which expands to one of
211 #define G_LITTLE_ENDIAN 1234
212 #define G_BIG_ENDIAN 4321
213 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
216 /* Basic bit swapping functions
218 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
219 (guint16) ((guint16) (val) >> 8) | \
220 (guint16) ((guint16) (val) << 8)))
222 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((guint32) ( \
223 (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
224 (((guint32) (val) & (guint32) 0x0000ff00U) << 8) | \
225 (((guint32) (val) & (guint32) 0x00ff0000U) >> 8) | \
226 (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
228 #define GUINT64_SWAP_LE_BE_CONSTANT(val) ((guint64) ( \
229 (((guint64) (val) & \
230 (guint64) G_GINT64_CONSTANT (0x00000000000000ffU)) << 56) | \
231 (((guint64) (val) & \
232 (guint64) G_GINT64_CONSTANT (0x000000000000ff00U)) << 40) | \
233 (((guint64) (val) & \
234 (guint64) G_GINT64_CONSTANT (0x0000000000ff0000U)) << 24) | \
235 (((guint64) (val) & \
236 (guint64) G_GINT64_CONSTANT (0x00000000ff000000U)) << 8) | \
237 (((guint64) (val) & \
238 (guint64) G_GINT64_CONSTANT (0x000000ff00000000U)) >> 8) | \
239 (((guint64) (val) & \
240 (guint64) G_GINT64_CONSTANT (0x0000ff0000000000U)) >> 24) | \
241 (((guint64) (val) & \
242 (guint64) G_GINT64_CONSTANT (0x00ff000000000000U)) >> 40) | \
243 (((guint64) (val) & \
244 (guint64) G_GINT64_CONSTANT (0xff00000000000000U)) >> 56)))
246 /* Arch specific stuff for speed
248 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
250 # if __GNUC__ >= 4 && defined (__GNUC_MINOR__) && __GNUC_MINOR__ >= 3
251 # define GUINT32_SWAP_LE_BE(val) ((guint32) __builtin_bswap32 ((guint32) (val)))
252 # define GUINT64_SWAP_LE_BE(val) ((guint64) __builtin_bswap64 ((guint64) (val)))
255 # if defined (__i386__)
256 # define GUINT16_SWAP_LE_BE_IA32(val) \
258 ({ guint16 __v, __x = ((guint16) (val)); \
259 if (__builtin_constant_p (__x)) \
260 __v = GUINT16_SWAP_LE_BE_CONSTANT (__x); \
262 __asm__ ("rorw $8, %w0" \
267 # if !defined (__i486__) && !defined (__i586__) \
268 && !defined (__pentium__) && !defined (__i686__) \
269 && !defined (__pentiumpro__) && !defined (__pentium4__)
270 # define GUINT32_SWAP_LE_BE_IA32(val) \
272 ({ guint32 __v, __x = ((guint32) (val)); \
273 if (__builtin_constant_p (__x)) \
274 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
276 __asm__ ("rorw $8, %w0\n\t" \
283 # else /* 486 and higher has bswap */
284 # define GUINT32_SWAP_LE_BE_IA32(val) \
286 ({ guint32 __v, __x = ((guint32) (val)); \
287 if (__builtin_constant_p (__x)) \
288 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
290 __asm__ ("bswap %0" \
294 # endif /* processor specific 32-bit stuff */
295 # define GUINT64_SWAP_LE_BE_IA32(val) \
297 ({ union { guint64 __ll; \
298 guint32 __l[2]; } __w, __r; \
299 __w.__ll = ((guint64) (val)); \
300 if (__builtin_constant_p (__w.__ll)) \
301 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (__w.__ll); \
304 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
305 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
308 /* Possibly just use the constant version and let gcc figure it out? */
309 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA32 (val))
310 # ifndef GUINT32_SWAP_LE_BE
311 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA32 (val))
313 # ifndef GUINT64_SWAP_LE_BE
314 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA32 (val))
316 # elif defined (__ia64__)
317 # define GUINT16_SWAP_LE_BE_IA64(val) \
319 ({ guint16 __v, __x = ((guint16) (val)); \
320 if (__builtin_constant_p (__x)) \
321 __v = GUINT16_SWAP_LE_BE_CONSTANT (__x); \
323 __asm__ __volatile__ ("shl %0 = %1, 48 ;;" \
324 "mux1 %0 = %0, @rev ;;" \
328 # define GUINT32_SWAP_LE_BE_IA64(val) \
330 ({ guint32 __v, __x = ((guint32) (val)); \
331 if (__builtin_constant_p (__x)) \
332 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
334 __asm__ __volatile__ ("shl %0 = %1, 32 ;;" \
335 "mux1 %0 = %0, @rev ;;" \
339 # define GUINT64_SWAP_LE_BE_IA64(val) \
341 ({ guint64 __v, __x = ((guint64) (val)); \
342 if (__builtin_constant_p (__x)) \
343 __v = GUINT64_SWAP_LE_BE_CONSTANT (__x); \
345 __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;" \
349 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA64 (val))
350 # ifndef GUINT32_SWAP_LE_BE
351 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA64 (val))
353 # ifndef GUINT64_SWAP_LE_BE
354 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA64 (val))
356 # elif defined (__x86_64__)
357 # define GUINT32_SWAP_LE_BE_X86_64(val) \
359 ({ guint32 __v, __x = ((guint32) (val)); \
360 if (__builtin_constant_p (__x)) \
361 __v = GUINT32_SWAP_LE_BE_CONSTANT (__x); \
363 __asm__ ("bswapl %0" \
367 # define GUINT64_SWAP_LE_BE_X86_64(val) \
369 ({ guint64 __v, __x = ((guint64) (val)); \
370 if (__builtin_constant_p (__x)) \
371 __v = GUINT64_SWAP_LE_BE_CONSTANT (__x); \
373 __asm__ ("bswapq %0" \
377 /* gcc seems to figure out optimal code for this on its own */
378 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
379 # ifndef GUINT32_SWAP_LE_BE
380 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86_64 (val))
382 # ifndef GUINT64_SWAP_LE_BE
383 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86_64 (val))
385 # else /* generic gcc */
386 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
387 # ifndef GUINT32_SWAP_LE_BE
388 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
390 # ifndef GUINT64_SWAP_LE_BE
391 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
395 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
396 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
397 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
400 #define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
401 #define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
402 #define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
403 (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
404 (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
405 #define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
406 (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
407 (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
409 /* The G*_TO_?E() macros are defined in glibconfig.h.
410 * The transformation is symmetric, so the FROM just maps to the TO.
412 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
413 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
414 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
415 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
416 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
417 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
418 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
419 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
421 #define GINT64_FROM_LE(val) (GINT64_TO_LE (val))
422 #define GUINT64_FROM_LE(val) (GUINT64_TO_LE (val))
423 #define GINT64_FROM_BE(val) (GINT64_TO_BE (val))
424 #define GUINT64_FROM_BE(val) (GUINT64_TO_BE (val))
426 #define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
427 #define GULONG_FROM_LE(val) (GULONG_TO_LE (val))
428 #define GLONG_FROM_BE(val) (GLONG_TO_BE (val))
429 #define GULONG_FROM_BE(val) (GULONG_TO_BE (val))
431 #define GINT_FROM_LE(val) (GINT_TO_LE (val))
432 #define GUINT_FROM_LE(val) (GUINT_TO_LE (val))
433 #define GINT_FROM_BE(val) (GINT_TO_BE (val))
434 #define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
436 #define GSIZE_FROM_LE(val) (GSIZE_TO_LE (val))
437 #define GSSIZE_FROM_LE(val) (GSSIZE_TO_LE (val))
438 #define GSIZE_FROM_BE(val) (GSIZE_TO_BE (val))
439 #define GSSIZE_FROM_BE(val) (GSSIZE_TO_BE (val))
441 /* Portable versions of host-network order stuff
443 #define g_ntohl(val) (GUINT32_FROM_BE (val))
444 #define g_ntohs(val) (GUINT16_FROM_BE (val))
445 #define g_htonl(val) (GUINT32_TO_BE (val))
446 #define g_htons(val) (GUINT16_TO_BE (val))
448 /* Overflow-checked unsigned integer arithmetic
450 #ifndef _GLIB_TEST_OVERFLOW_FALLBACK
451 /* https://bugzilla.gnome.org/show_bug.cgi?id=769104 */
452 #if __GNUC__ >= 5 && !defined(__INTEL_COMPILER)
453 #define _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS
454 #elif g_macro__has_builtin(__builtin_add_overflow)
455 #define _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS
459 #ifdef _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS
461 #define g_uint_checked_add(dest, a, b) \
462 (!__builtin_add_overflow(a, b, dest))
463 #define g_uint_checked_mul(dest, a, b) \
464 (!__builtin_mul_overflow(a, b, dest))
466 #define g_uint64_checked_add(dest, a, b) \
467 (!__builtin_add_overflow(a, b, dest))
468 #define g_uint64_checked_mul(dest, a, b) \
469 (!__builtin_mul_overflow(a, b, dest))
471 #define g_size_checked_add(dest, a, b) \
472 (!__builtin_add_overflow(a, b, dest))
473 #define g_size_checked_mul(dest, a, b) \
474 (!__builtin_mul_overflow(a, b, dest))
476 #else /* !_GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS */
478 /* The names of the following inlines are private. Use the macro
481 static inline gboolean _GLIB_CHECKED_ADD_UINT (guint *dest, guint a, guint b) {
482 *dest = a + b; return *dest >= a; }
483 static inline gboolean _GLIB_CHECKED_MUL_UINT (guint *dest, guint a, guint b) {
484 *dest = a * b; return !a || *dest / a == b; }
485 static inline gboolean _GLIB_CHECKED_ADD_UINT64 (guint64 *dest, guint64 a, guint64 b) {
486 *dest = a + b; return *dest >= a; }
487 static inline gboolean _GLIB_CHECKED_MUL_UINT64 (guint64 *dest, guint64 a, guint64 b) {
488 *dest = a * b; return !a || *dest / a == b; }
489 static inline gboolean _GLIB_CHECKED_ADD_SIZE (gsize *dest, gsize a, gsize b) {
490 *dest = a + b; return *dest >= a; }
491 static inline gboolean _GLIB_CHECKED_MUL_SIZE (gsize *dest, gsize a, gsize b) {
492 *dest = a * b; return !a || *dest / a == b; }
494 #define g_uint_checked_add(dest, a, b) \
495 _GLIB_CHECKED_ADD_UINT(dest, a, b)
496 #define g_uint_checked_mul(dest, a, b) \
497 _GLIB_CHECKED_MUL_UINT(dest, a, b)
499 #define g_uint64_checked_add(dest, a, b) \
500 _GLIB_CHECKED_ADD_UINT64(dest, a, b)
501 #define g_uint64_checked_mul(dest, a, b) \
502 _GLIB_CHECKED_MUL_UINT64(dest, a, b)
504 #define g_size_checked_add(dest, a, b) \
505 _GLIB_CHECKED_ADD_SIZE(dest, a, b)
506 #define g_size_checked_mul(dest, a, b) \
507 _GLIB_CHECKED_MUL_SIZE(dest, a, b)
509 #endif /* !_GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS */
511 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
514 * +--------+---------------+---------------+
515 * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
516 * +--------+---------------+---------------+
517 * B0------------------->B1------->B2-->B3-->
519 * IEEE Standard 754 Double Precision Storage Format (gdouble):
521 * 63 62 52 51 32 31 0
522 * +--------+----------------+----------------+ +---------------+
523 * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
524 * +--------+----------------+----------------+ +---------------+
525 * B0--------------->B1---------->B2--->B3----> B4->B5->B6->B7->
527 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
528 typedef union _GDoubleIEEE754 GDoubleIEEE754;
529 typedef union _GFloatIEEE754 GFloatIEEE754;
530 #define G_IEEE754_FLOAT_BIAS (127)
531 #define G_IEEE754_DOUBLE_BIAS (1023)
532 /* multiply with base2 exponent to get base10 exponent (normal numbers) */
533 #define G_LOG_2_BASE_10 (0.30102999566398119521)
534 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
540 guint biased_exponent : 8;
544 union _GDoubleIEEE754
548 guint mantissa_low : 32;
549 guint mantissa_high : 20;
550 guint biased_exponent : 11;
554 #elif G_BYTE_ORDER == G_BIG_ENDIAN
560 guint biased_exponent : 8;
564 union _GDoubleIEEE754
569 guint biased_exponent : 11;
570 guint mantissa_high : 20;
571 guint mantissa_low : 32;
574 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
575 #error unknown ENDIAN type
576 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
578 typedef struct _GTimeVal GTimeVal GLIB_DEPRECATED_TYPE_IN_2_62_FOR(GDateTime);
584 } GLIB_DEPRECATED_TYPE_IN_2_62_FOR(GDateTime);
586 typedef gint grefcount;
587 typedef gint gatomicrefcount; /* should be accessed only using atomics */
591 #endif /* __G_TYPES_H__ */