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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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 /* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
31 * where this is valid. This allows for warningless compilation of
32 * "long long" types even in the presence of '-ansi -pedantic'. This
33 * of course should be with the other GCC-isms below, but then
34 * glibconfig.h wouldn't load cleanly and it is better to have that
35 * here, than in glibconfig.h.
37 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
38 # define G_GNUC_EXTENSION __extension__
40 # define G_GNUC_EXTENSION
43 /* system specific config file glibconfig.h provides definitions for
44 * the extrema of many of the standard types. These are:
46 * G_MINSHORT, G_MAXSHORT
48 * G_MINLONG, G_MAXLONG
49 * G_MINFLOAT, G_MAXFLOAT
50 * G_MINDOUBLE, G_MAXDOUBLE
52 * It also provides the following typedefs:
60 * It defines the G_BYTE_ORDER symbol to one of G_*_ENDIAN (see later in
63 * And it provides a way to store and retrieve a `gint' in/from a `gpointer'.
64 * This is useful to pass an integer instead of a pointer to a callback.
66 * GINT_TO_POINTER (i), GUINT_TO_POINTER (i)
67 * GPOINTER_TO_INT (p), GPOINTER_TO_UINT (p)
69 * Finally, it provides the following wrappers to STDC functions:
71 * void g_memmove (gpointer dest, gconstpointer void *src, gulong count);
72 * A wrapper for STDC memmove, or an implementation, if memmove doesn't
73 * exist. The prototype looks like the above, give or take a const,
76 #include <glibconfig.h>
78 /* Define some mathematical constants that aren't available
79 * symbolically in some strict ISO C implementations.
81 #define G_E 2.7182818284590452354E0
82 #define G_LN2 6.9314718055994530942E-1
83 #define G_LN10 2.3025850929940456840E0
84 #define G_PI 3.14159265358979323846E0
85 #define G_PI_2 1.57079632679489661923E0
86 #define G_PI_4 0.78539816339744830962E0
87 #define G_SQRT2 1.4142135623730950488E0
89 /* include varargs functions for assertment macros
93 /* optionally feature DMALLOC memory allocation debugger
102 /* On native Win32, directory separator is the backslash, and search path
103 * separator is the semicolon.
105 #define G_DIR_SEPARATOR '\\'
106 #define G_DIR_SEPARATOR_S "\\"
107 #define G_SEARCHPATH_SEPARATOR ';'
108 #define G_SEARCHPATH_SEPARATOR_S ";"
110 #else /* !G_OS_WIN32 */
114 #define G_DIR_SEPARATOR '/'
115 #define G_DIR_SEPARATOR_S "/"
116 #define G_SEARCHPATH_SEPARATOR ':'
117 #define G_SEARCHPATH_SEPARATOR_S ":"
119 #endif /* !G_OS_WIN32 */
123 #endif /* __cplusplus */
126 /* Provide definitions for some commonly used macros.
127 * Some of them are only provided if they haven't already
128 * been defined. It is assumed that if they are already
129 * defined then the current definition is correct.
134 # else /* !__cplusplus */
135 # define NULL ((void*) 0)
136 # endif /* !__cplusplus */
144 #define TRUE (!FALSE)
148 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
151 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
154 #define ABS(a) (((a) < 0) ? -(a) : (a))
157 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
159 #define G_STRINGIFY(macro_or_string) G_STRINGIFY_ARG (macro_or_string)
160 #define G_STRINGIFY_ARG(contents) #contents
162 /* provide a string identifying the current code position */
164 # define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
166 # define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__)
170 /* Count the number of elements in an array. The array must be defined
171 * as such; using this with a dynamically allocated array will give
174 #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
176 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
177 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
179 #if !defined (G_VA_COPY)
180 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
181 # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
182 # elif defined (G_VA_COPY_AS_ARRAY)
183 # define G_VA_COPY(ap1, ap2) g_memmove ((ap1), (ap2), sizeof (va_list))
184 # else /* va_list is a pointer */
185 # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
186 # endif /* va_list is a pointer */
187 #endif /* !G_VA_COPY */
190 /* Provide convenience macros for handling structure
191 * fields through their offsets.
193 #define G_STRUCT_OFFSET(struct_type, member) \
194 ((glong) ((guint8*) &((struct_type*) 0)->member))
195 #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
196 ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
197 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
198 (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
201 /* inlining hassle. for compilers that don't allow the `inline' keyword,
202 * mostly because of strict ANSI C compliance or dumbness, we try to fall
203 * back to either `__inline__' or `__inline'.
204 * we define G_CAN_INLINE, if the compiler seems to be actually
205 * *capable* to do function inlining, in which case inline function bodys
206 * do make sense. we also define G_INLINE_FUNC to properly export the
207 * function prototypes if no inlining can be performed.
208 * we special case most of the stuff, so inline functions can have a normal
209 * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
211 #ifndef G_INLINE_FUNC
212 # define G_CAN_INLINE 1
215 # if defined (__GNUC__) && defined (__STRICT_ANSI__)
217 # define inline __inline__
219 #else /* !G_HAVE_INLINE */
221 # if defined (G_HAVE___INLINE__)
222 # define inline __inline__
223 # else /* !inline && !__inline__ */
224 # if defined (G_HAVE___INLINE)
225 # define inline __inline
226 # else /* !inline && !__inline__ && !__inline */
227 # define inline /* don't inline, then */
228 # ifndef G_INLINE_FUNC
234 #ifndef G_INLINE_FUNC
237 # define G_INLINE_FUNC extern inline
240 # define G_INLINE_FUNC extern
242 # else /* !__GNUC__ */
244 # define G_INLINE_FUNC static inline
246 # define G_INLINE_FUNC extern
248 # endif /* !__GNUC__ */
249 #endif /* !G_INLINE_FUNC */
252 /* Provide simple macro statement wrappers (adapted from Perl):
253 * G_STMT_START { statements; } G_STMT_END;
254 * can be used as a single statement, as in
255 * if (x) G_STMT_START { ... } G_STMT_END; else ...
257 * For gcc we will wrap the statements within `({' and `})' braces.
258 * For SunOS they will be wrapped within `if (1)' and `else (void) 0',
259 * and otherwise within `do' and `while (0)'.
261 #if !(defined (G_STMT_START) && defined (G_STMT_END))
262 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
263 # define G_STMT_START (void)(
264 # define G_STMT_END )
266 # if (defined (sun) || defined (__sun__))
267 # define G_STMT_START if (1)
268 # define G_STMT_END else (void)0
270 # define G_STMT_START do
271 # define G_STMT_END while (0)
277 /* Provide macros to feature the GCC function attribute.
279 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
280 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
281 __attribute__((format (printf, format_idx, arg_idx)))
282 #define G_GNUC_SCANF( format_idx, arg_idx ) \
283 __attribute__((format (scanf, format_idx, arg_idx)))
284 #define G_GNUC_FORMAT( arg_idx ) \
285 __attribute__((format_arg (arg_idx)))
286 #define G_GNUC_NORETURN \
287 __attribute__((noreturn))
288 #define G_GNUC_CONST \
289 __attribute__((const))
290 #define G_GNUC_UNUSED \
291 __attribute__((unused))
292 #else /* !__GNUC__ */
293 #define G_GNUC_PRINTF( format_idx, arg_idx )
294 #define G_GNUC_SCANF( format_idx, arg_idx )
295 #define G_GNUC_FORMAT( arg_idx )
296 #define G_GNUC_NORETURN
298 #define G_GNUC_UNUSED
299 #endif /* !__GNUC__ */
301 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
302 * macros, so we can refer to them as strings unconditionally.
305 #define G_GNUC_FUNCTION __FUNCTION__
306 #define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
307 #else /* !__GNUC__ */
308 #define G_GNUC_FUNCTION ""
309 #define G_GNUC_PRETTY_FUNCTION ""
310 #endif /* !__GNUC__ */
312 /* we try to provide a usefull equivalent for ATEXIT if it is
313 * not defined, but use is actually abandoned. people should
314 * use g_atexit() instead.
317 # define ATEXIT(proc) g_ATEXIT(proc)
319 # define G_NATIVE_ATEXIT
322 /* Hacker macro to place breakpoints for elected machines.
323 * Actual use is strongly deprecated of course ;)
325 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
326 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
327 #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
328 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
329 #else /* !__i386__ && !__alpha__ */
330 #define G_BREAKPOINT()
331 #endif /* __i386__ */
334 /* Provide macros for easily allocating memory. The macros
335 * will cast the allocated memory to the specified type
336 * in order to avoid compiler warnings. (Makes the code neater).
340 # define g_new(type, count) (ALLOC (type, count))
341 # define g_new0(type, count) (CALLOC (type, count))
342 # define g_renew(type, mem, count) (REALLOC (mem, type, count))
343 #else /* __DMALLOC_H__ */
344 # define g_new(type, count) \
345 ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
346 # define g_new0(type, count) \
347 ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
348 # define g_renew(type, mem, count) \
349 ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
350 #endif /* __DMALLOC_H__ */
352 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
353 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
355 sizeof (type) * (pre_alloc), \
358 #define g_chunk_new(type, chunk) ( \
359 (type *) g_mem_chunk_alloc (chunk) \
361 #define g_chunk_new0(type, chunk) ( \
362 (type *) g_mem_chunk_alloc0 (chunk) \
364 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
365 g_mem_chunk_free ((mem_chunk), (mem)); \
369 /* Provide macros for error handling. The "assert" macros will
370 * exit on failure. The "return" macros will exit the current
371 * function. Two different definitions are given for the macros
372 * if G_DISABLE_ASSERT is not defined, in order to support gcc's
373 * __PRETTY_FUNCTION__ capability.
376 #ifdef G_DISABLE_ASSERT
378 #define g_assert(expr)
379 #define g_assert_not_reached()
381 #else /* !G_DISABLE_ASSERT */
385 #define g_assert(expr) G_STMT_START{ \
387 g_log (G_LOG_DOMAIN, \
389 "file %s: line %d (%s): assertion failed: (%s)", \
392 __PRETTY_FUNCTION__, \
395 #define g_assert_not_reached() G_STMT_START{ \
396 g_log (G_LOG_DOMAIN, \
398 "file %s: line %d (%s): should not be reached", \
401 __PRETTY_FUNCTION__); }G_STMT_END
403 #else /* !__GNUC__ */
405 #define g_assert(expr) G_STMT_START{ \
407 g_log (G_LOG_DOMAIN, \
409 "file %s: line %d: assertion failed: (%s)", \
414 #define g_assert_not_reached() G_STMT_START{ \
415 g_log (G_LOG_DOMAIN, \
417 "file %s: line %d: should not be reached", \
419 __LINE__); }G_STMT_END
421 #endif /* __GNUC__ */
423 #endif /* !G_DISABLE_ASSERT */
426 #ifdef G_DISABLE_CHECKS
428 #define g_return_if_fail(expr)
429 #define g_return_val_if_fail(expr,val)
430 #define g_return_if_reached() return
431 #define g_return_val_if_reached(val) return (val)
433 #else /* !G_DISABLE_CHECKS */
437 #define g_return_if_fail(expr) G_STMT_START{ \
440 g_log (G_LOG_DOMAIN, \
441 G_LOG_LEVEL_CRITICAL, \
442 "file %s: line %d (%s): assertion `%s' failed", \
445 __PRETTY_FUNCTION__, \
450 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
453 g_log (G_LOG_DOMAIN, \
454 G_LOG_LEVEL_CRITICAL, \
455 "file %s: line %d (%s): assertion `%s' failed", \
458 __PRETTY_FUNCTION__, \
463 #define g_return_if_reached() G_STMT_START{ \
464 g_log (G_LOG_DOMAIN, \
465 G_LOG_LEVEL_CRITICAL, \
466 "file %s: line %d (%s): should not be reached", \
469 __PRETTY_FUNCTION__); \
472 #define g_return_val_if_reached(val) G_STMT_START{ \
473 g_log (G_LOG_DOMAIN, \
474 G_LOG_LEVEL_CRITICAL, \
475 "file %s: line %d (%s): should not be reached", \
478 __PRETTY_FUNCTION__); \
479 return (val); }G_STMT_END
481 #else /* !__GNUC__ */
483 #define g_return_if_fail(expr) G_STMT_START{ \
486 g_log (G_LOG_DOMAIN, \
487 G_LOG_LEVEL_CRITICAL, \
488 "file %s: line %d: assertion `%s' failed", \
495 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
498 g_log (G_LOG_DOMAIN, \
499 G_LOG_LEVEL_CRITICAL, \
500 "file %s: line %d: assertion `%s' failed", \
507 #define g_return_if_reached() G_STMT_START{ \
508 g_log (G_LOG_DOMAIN, \
509 G_LOG_LEVEL_CRITICAL, \
510 "file %s: line %d: should not be reached", \
515 #define g_return_val_if_reached(val) G_STMT_START{ \
516 g_log (G_LOG_DOMAIN, \
517 G_LOG_LEVEL_CRITICAL, \
518 "file %s: line %d: should not be reached", \
521 return (val); }G_STMT_END
523 #endif /* !__GNUC__ */
525 #endif /* !G_DISABLE_CHECKS */
528 /* Provide type definitions for commonly used types.
529 * These are useful because a "gint8" can be adjusted
530 * to be 1 byte (8 bits) on all platforms. Similarly and
531 * more importantly, "gint32" can be adjusted to be
532 * 4 bytes (32 bits) on all platforms.
536 typedef short gshort;
539 typedef gint gboolean;
540 typedef gchar* gstring;
542 typedef unsigned char guchar;
543 typedef unsigned short gushort;
544 typedef unsigned long gulong;
545 typedef unsigned int guint;
547 typedef float gfloat;
548 typedef double gdouble;
550 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
551 * Since gldouble isn't used anywhere, just disable it for now */
554 #ifdef HAVE_LONG_DOUBLE
555 typedef long double gldouble;
556 #else /* HAVE_LONG_DOUBLE */
557 typedef double gldouble;
558 #endif /* HAVE_LONG_DOUBLE */
561 typedef void* gpointer;
562 typedef const void *gconstpointer;
565 typedef guint32 GQuark;
566 typedef gint32 GTime;
569 /* Portable endian checks and conversions
571 * glibconfig.h defines G_BYTE_ORDER which expands to one of
574 #define G_LITTLE_ENDIAN 1234
575 #define G_BIG_ENDIAN 4321
576 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
579 /* Basic bit swapping functions
581 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
582 (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
583 (((guint16) (val) & (guint16) 0xff00U) >> 8)))
584 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((guint32) ( \
585 (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
586 (((guint32) (val) & (guint32) 0x0000ff00U) << 8) | \
587 (((guint32) (val) & (guint32) 0x00ff0000U) >> 8) | \
588 (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
590 /* Intel specific stuff for speed
592 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
593 # define GUINT16_SWAP_LE_BE_X86(val) \
595 ({ register guint16 __v; \
596 if (__builtin_constant_p (val)) \
597 __v = GUINT16_SWAP_LE_BE_CONSTANT (val); \
599 __asm__ __const__ ("rorw $8, %w0" \
601 : "0" ((guint16) (val))); \
603 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
604 # if !defined(__i486__) && !defined(__i586__) \
605 && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
606 # define GUINT32_SWAP_LE_BE_X86(val) \
608 ({ register guint32 __v; \
609 if (__builtin_constant_p (val)) \
610 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
612 __asm__ __const__ ("rorw $8, %w0\n\t" \
616 : "0" ((guint32) (val))); \
618 # else /* 486 and higher has bswap */
619 # define GUINT32_SWAP_LE_BE_X86(val) \
621 ({ register guint32 __v; \
622 if (__builtin_constant_p (val)) \
623 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
625 __asm__ __const__ ("bswap %0" \
627 : "0" ((guint32) (val))); \
629 # endif /* processor specific 32-bit stuff */
630 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
631 #else /* !__i386__ */
632 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
633 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
634 #endif /* __i386__ */
637 # define GUINT64_SWAP_LE_BE_CONSTANT(val) ((guint64) ( \
638 (((guint64) (val) & \
639 (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) | \
640 (((guint64) (val) & \
641 (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) | \
642 (((guint64) (val) & \
643 (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) | \
644 (((guint64) (val) & \
645 (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) << 8) | \
646 (((guint64) (val) & \
647 (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >> 8) | \
648 (((guint64) (val) & \
649 (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) | \
650 (((guint64) (val) & \
651 (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) | \
652 (((guint64) (val) & \
653 (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
654 # if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
655 # define GUINT64_SWAP_LE_BE_X86(val) \
657 ({ union { guint64 __ll; \
658 guint32 __l[2]; } __r; \
659 if (__builtin_constant_p (val)) \
660 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val); \
663 union { guint64 __ll; \
664 guint32 __l[2]; } __w; \
665 __w.__ll = ((guint64) val); \
666 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
667 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
670 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
671 # else /* !__i386__ */
672 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
676 #define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
677 #define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
678 #define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
679 (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
680 (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
681 #define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
682 (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
683 (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
685 /* The G*_TO_?E() macros are defined in glibconfig.h.
686 * The transformation is symmetric, so the FROM just maps to the TO.
688 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
689 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
690 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
691 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
692 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
693 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
694 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
695 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
698 #define GINT64_FROM_LE(val) (GINT64_TO_LE (val))
699 #define GUINT64_FROM_LE(val) (GUINT64_TO_LE (val))
700 #define GINT64_FROM_BE(val) (GINT64_TO_BE (val))
701 #define GUINT64_FROM_BE(val) (GUINT64_TO_BE (val))
704 #define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
705 #define GULONG_FROM_LE(val) (GULONG_TO_LE (val))
706 #define GLONG_FROM_BE(val) (GLONG_TO_BE (val))
707 #define GULONG_FROM_BE(val) (GULONG_TO_BE (val))
709 #define GINT_FROM_LE(val) (GINT_TO_LE (val))
710 #define GUINT_FROM_LE(val) (GUINT_TO_LE (val))
711 #define GINT_FROM_BE(val) (GINT_TO_BE (val))
712 #define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
715 /* Portable versions of host-network order stuff
717 #define g_ntohl(val) (GUINT32_FROM_BE (val))
718 #define g_ntohs(val) (GUINT16_FROM_BE (val))
719 #define g_htonl(val) (GUINT32_TO_BE (val))
720 #define g_htons(val) (GUINT16_TO_BE (val))
724 * we prefix variable declarations so they can
725 * properly get exported in windows dlls.
728 # ifdef GLIB_COMPILATION
729 # define GLIB_VAR __declspec(dllexport)
730 # else /* !GLIB_COMPILATION */
731 # define GLIB_VAR extern __declspec(dllimport)
732 # endif /* !GLIB_COMPILATION */
733 #else /* !G_OS_WIN32 */
734 # define GLIB_VAR extern
735 #endif /* !G_OS_WIN32 */
737 GLIB_VAR const guint glib_major_version;
738 GLIB_VAR const guint glib_minor_version;
739 GLIB_VAR const guint glib_micro_version;
740 GLIB_VAR const guint glib_interface_age;
741 GLIB_VAR const guint glib_binary_age;
743 #define GLIB_CHECK_VERSION(major,minor,micro) \
744 (GLIB_MAJOR_VERSION > (major) || \
745 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
746 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
747 GLIB_MICRO_VERSION >= (micro)))
749 /* Forward declarations of glib types.
751 typedef struct _GAllocator GAllocator;
752 typedef struct _GArray GArray;
753 typedef struct _GByteArray GByteArray;
754 typedef struct _GCache GCache;
755 typedef struct _GCompletion GCompletion;
756 typedef struct _GData GData;
757 typedef struct _GDebugKey GDebugKey;
758 typedef union _GDoubleIEEE754 GDoubleIEEE754;
759 typedef union _GFloatIEEE754 GFloatIEEE754;
760 typedef struct _GHashTable GHashTable;
761 typedef struct _GHook GHook;
762 typedef struct _GHookList GHookList;
763 typedef struct _GList GList;
764 typedef struct _GMemChunk GMemChunk;
765 typedef struct _GNode GNode;
766 typedef struct _GPtrArray GPtrArray;
767 typedef struct _GQueue GQueue;
768 typedef struct _GRand GRand;
769 typedef struct _GRelation GRelation;
770 typedef struct _GScanner GScanner;
771 typedef struct _GScannerConfig GScannerConfig;
772 typedef struct _GSList GSList;
773 typedef struct _GString GString;
774 typedef struct _GStringChunk GStringChunk;
775 typedef struct _GTimer GTimer;
776 typedef struct _GTrashStack GTrashStack;
777 typedef struct _GTree GTree;
778 typedef struct _GTuples GTuples;
779 typedef union _GTokenValue GTokenValue;
780 typedef struct _GIOChannel GIOChannel;
782 /* Tree traverse flags */
785 G_TRAVERSE_LEAFS = 1 << 0,
786 G_TRAVERSE_NON_LEAFS = 1 << 1,
787 G_TRAVERSE_ALL = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
788 G_TRAVERSE_MASK = 0x03
791 /* Tree traverse orders */
800 /* Log level shift offset for user defined
801 * log levels (0-7 are used by GLib).
803 #define G_LOG_LEVEL_USER_SHIFT (8)
805 /* Glib log levels and flags.
810 G_LOG_FLAG_RECURSION = 1 << 0,
811 G_LOG_FLAG_FATAL = 1 << 1,
813 /* GLib log levels */
814 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
815 G_LOG_LEVEL_CRITICAL = 1 << 3,
816 G_LOG_LEVEL_WARNING = 1 << 4,
817 G_LOG_LEVEL_MESSAGE = 1 << 5,
818 G_LOG_LEVEL_INFO = 1 << 6,
819 G_LOG_LEVEL_DEBUG = 1 << 7,
821 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
824 /* GLib log levels that are considered fatal by default */
825 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
828 typedef gpointer (*GCacheNewFunc) (gpointer key);
829 typedef gpointer (*GCacheDupFunc) (gpointer value);
830 typedef void (*GCacheDestroyFunc) (gpointer value);
831 typedef gint (*GCompareFunc) (gconstpointer a,
833 typedef gchar* (*GCompletionFunc) (gpointer);
834 typedef void (*GDestroyNotify) (gpointer data);
835 typedef void (*GDataForeachFunc) (GQuark key_id,
838 typedef void (*GFunc) (gpointer data,
840 typedef guint (*GHashFunc) (gconstpointer key);
841 typedef void (*GFreeFunc) (gpointer data);
842 typedef void (*GHFunc) (gpointer key,
845 typedef gboolean (*GHRFunc) (gpointer key,
848 typedef gint (*GHookCompareFunc) (GHook *new_hook,
850 typedef gboolean (*GHookFindFunc) (GHook *hook,
852 typedef void (*GHookMarshaller) (GHook *hook,
854 typedef gboolean (*GHookCheckMarshaller) (GHook *hook,
856 typedef void (*GHookFunc) (gpointer data);
857 typedef gboolean (*GHookCheckFunc) (gpointer data);
858 typedef void (*GHookFreeFunc) (GHookList *hook_list,
860 typedef void (*GLogFunc) (const gchar *log_domain,
861 GLogLevelFlags log_level,
862 const gchar *message,
864 typedef gboolean (*GNodeTraverseFunc) (GNode *node,
866 typedef void (*GNodeForeachFunc) (GNode *node,
868 typedef void (*GScannerMsgFunc) (GScanner *scanner,
871 typedef gint (*GTraverseFunc) (gpointer key,
874 typedef void (*GVoidFunc) (void);
939 #endif /* __cplusplus */
945 #endif /* __cplusplus */
947 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
950 * +--------+---------------+---------------+
951 * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
952 * +--------+---------------+---------------+
953 * B0------------------->B1------->B2-->B3-->
955 * IEEE Standard 754 Double Precision Storage Format (gdouble):
957 * 63 62 52 51 32 31 0
958 * +--------+----------------+----------------+ +---------------+
959 * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
960 * +--------+----------------+----------------+ +---------------+
961 * B0--------------->B1---------->B2--->B3----> B4->B5->B6->B7->
963 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
964 #define G_IEEE754_FLOAT_BIAS (127)
965 #define G_IEEE754_DOUBLE_BIAS (1023)
966 /* multiply with base2 exponent to get base10 exponent (nomal numbers) */
967 #define G_LOG_2_BASE_10 (0.30102999566398119521)
968 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
974 guint biased_exponent : 8;
978 union _GDoubleIEEE754
982 guint mantissa_low : 32;
983 guint mantissa_high : 20;
984 guint biased_exponent : 11;
988 #elif G_BYTE_ORDER == G_BIG_ENDIAN
994 guint biased_exponent : 8;
998 union _GDoubleIEEE754
1003 guint biased_exponent : 11;
1004 guint mantissa_high : 20;
1005 guint mantissa_low : 32;
1008 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
1009 #error unknown ENDIAN type
1010 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
1013 /* Doubly linked lists
1015 void g_list_push_allocator (GAllocator *allocator);
1016 void g_list_pop_allocator (void);
1017 GList* g_list_alloc (void);
1018 void g_list_free (GList *list);
1019 void g_list_free_1 (GList *list);
1020 GList* g_list_append (GList *list,
1022 GList* g_list_prepend (GList *list,
1024 GList* g_list_insert (GList *list,
1027 GList* g_list_insert_sorted (GList *list,
1030 GList* g_list_concat (GList *list1,
1032 GList* g_list_remove (GList *list,
1033 gconstpointer data);
1034 GList* g_list_remove_link (GList *list,
1036 GList* g_list_delete_link (GList *list,
1038 GList* g_list_reverse (GList *list);
1039 GList* g_list_copy (GList *list);
1040 GList* g_list_nth (GList *list,
1042 GList* g_list_find (GList *list,
1043 gconstpointer data);
1044 GList* g_list_find_custom (GList *list,
1047 gint g_list_position (GList *list,
1049 gint g_list_index (GList *list,
1050 gconstpointer data);
1051 GList* g_list_last (GList *list);
1052 GList* g_list_first (GList *list);
1053 guint g_list_length (GList *list);
1054 void g_list_foreach (GList *list,
1056 gpointer user_data);
1057 GList* g_list_sort (GList *list,
1058 GCompareFunc compare_func);
1059 gpointer g_list_nth_data (GList *list,
1061 #define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
1062 #define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
1065 /* Singly linked lists
1067 void g_slist_push_allocator (GAllocator *allocator);
1068 void g_slist_pop_allocator (void);
1069 GSList* g_slist_alloc (void);
1070 void g_slist_free (GSList *list);
1071 void g_slist_free_1 (GSList *list);
1072 GSList* g_slist_append (GSList *list,
1074 GSList* g_slist_prepend (GSList *list,
1076 GSList* g_slist_insert (GSList *list,
1079 GSList* g_slist_insert_sorted (GSList *list,
1082 GSList* g_slist_insert_before (GSList *slist,
1085 GSList* g_slist_concat (GSList *list1,
1087 GSList* g_slist_remove (GSList *list,
1088 gconstpointer data);
1089 GSList* g_slist_remove_link (GSList *list,
1091 GSList* g_slist_delete_link (GSList *list,
1093 GSList* g_slist_reverse (GSList *list);
1094 GSList* g_slist_copy (GSList *list);
1095 GSList* g_slist_nth (GSList *list,
1097 GSList* g_slist_find (GSList *list,
1098 gconstpointer data);
1099 GSList* g_slist_find_custom (GSList *list,
1102 gint g_slist_position (GSList *list,
1104 gint g_slist_index (GSList *list,
1105 gconstpointer data);
1106 GSList* g_slist_last (GSList *list);
1107 guint g_slist_length (GSList *list);
1108 void g_slist_foreach (GSList *list,
1110 gpointer user_data);
1111 GSList* g_slist_sort (GSList *list,
1112 GCompareFunc compare_func);
1113 gpointer g_slist_nth_data (GSList *list,
1115 #define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
1120 GQueue* g_queue_new (void);
1121 void g_queue_free (GQueue *queue);
1122 void g_queue_push_head (GQueue *queue,
1124 void g_queue_push_tail (GQueue *queue,
1126 gpointer g_queue_pop_head (GQueue *queue);
1127 gpointer g_queue_pop_tail (GQueue *queue);
1128 gboolean g_queue_is_empty (GQueue *queue);
1129 gpointer g_queue_peek_head (GQueue *queue);
1130 gpointer g_queue_peek_tail (GQueue *queue);
1131 void g_queue_push_head_link (GQueue *queue,
1133 void g_queue_push_tail_link (GQueue *queue,
1135 GList* g_queue_pop_head_link (GQueue *queue);
1136 GList* g_queue_pop_tail_link (GQueue *queue);
1140 GHashTable* g_hash_table_new (GHashFunc hash_func,
1141 GCompareFunc key_compare_func);
1142 void g_hash_table_destroy (GHashTable *hash_table);
1143 void g_hash_table_insert (GHashTable *hash_table,
1146 void g_hash_table_remove (GHashTable *hash_table,
1148 gpointer g_hash_table_lookup (GHashTable *hash_table,
1150 gboolean g_hash_table_lookup_extended(GHashTable *hash_table,
1151 gconstpointer lookup_key,
1154 void g_hash_table_foreach (GHashTable *hash_table,
1156 gpointer user_data);
1157 guint g_hash_table_foreach_remove (GHashTable *hash_table,
1159 gpointer user_data);
1160 guint g_hash_table_size (GHashTable *hash_table);
1162 /* The following two functions are deprecated and will be removed in
1163 * the next major release. They do no good. */
1164 void g_hash_table_freeze (GHashTable *hash_table);
1165 void g_hash_table_thaw (GHashTable *hash_table);
1169 GCache* g_cache_new (GCacheNewFunc value_new_func,
1170 GCacheDestroyFunc value_destroy_func,
1171 GCacheDupFunc key_dup_func,
1172 GCacheDestroyFunc key_destroy_func,
1173 GHashFunc hash_key_func,
1174 GHashFunc hash_value_func,
1175 GCompareFunc key_compare_func);
1176 void g_cache_destroy (GCache *cache);
1177 gpointer g_cache_insert (GCache *cache,
1179 void g_cache_remove (GCache *cache,
1180 gconstpointer value);
1181 void g_cache_key_foreach (GCache *cache,
1183 gpointer user_data);
1184 void g_cache_value_foreach (GCache *cache,
1186 gpointer user_data);
1189 /* Balanced binary trees
1191 GTree* g_tree_new (GCompareFunc key_compare_func);
1192 void g_tree_destroy (GTree *tree);
1193 void g_tree_insert (GTree *tree,
1196 void g_tree_remove (GTree *tree,
1198 gpointer g_tree_lookup (GTree *tree,
1200 void g_tree_traverse (GTree *tree,
1201 GTraverseFunc traverse_func,
1202 GTraverseType traverse_type,
1204 gpointer g_tree_search (GTree *tree,
1205 GCompareFunc search_func,
1206 gconstpointer data);
1207 gint g_tree_height (GTree *tree);
1208 gint g_tree_nnodes (GTree *tree);
1212 /* N-way tree implementation
1223 #define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
1224 ((GNode*) (node))->prev == NULL && \
1225 ((GNode*) (node))->next == NULL)
1226 #define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
1228 void g_node_push_allocator (GAllocator *allocator);
1229 void g_node_pop_allocator (void);
1230 GNode* g_node_new (gpointer data);
1231 void g_node_destroy (GNode *root);
1232 void g_node_unlink (GNode *node);
1233 GNode* g_node_copy (GNode *node);
1234 GNode* g_node_insert (GNode *parent,
1237 GNode* g_node_insert_before (GNode *parent,
1240 GNode* g_node_prepend (GNode *parent,
1242 guint g_node_n_nodes (GNode *root,
1243 GTraverseFlags flags);
1244 GNode* g_node_get_root (GNode *node);
1245 gboolean g_node_is_ancestor (GNode *node,
1247 guint g_node_depth (GNode *node);
1248 GNode* g_node_find (GNode *root,
1249 GTraverseType order,
1250 GTraverseFlags flags,
1253 /* convenience macros */
1254 #define g_node_append(parent, node) \
1255 g_node_insert_before ((parent), NULL, (node))
1256 #define g_node_insert_data(parent, position, data) \
1257 g_node_insert ((parent), (position), g_node_new (data))
1258 #define g_node_insert_data_before(parent, sibling, data) \
1259 g_node_insert_before ((parent), (sibling), g_node_new (data))
1260 #define g_node_prepend_data(parent, data) \
1261 g_node_prepend ((parent), g_node_new (data))
1262 #define g_node_append_data(parent, data) \
1263 g_node_insert_before ((parent), NULL, g_node_new (data))
1265 /* traversal function, assumes that `node' is root
1266 * (only traverses `node' and its subtree).
1267 * this function is just a high level interface to
1268 * low level traversal functions, optimized for speed.
1270 void g_node_traverse (GNode *root,
1271 GTraverseType order,
1272 GTraverseFlags flags,
1274 GNodeTraverseFunc func,
1277 /* return the maximum tree height starting with `node', this is an expensive
1278 * operation, since we need to visit all nodes. this could be shortened by
1279 * adding `guint height' to struct _GNode, but then again, this is not very
1280 * often needed, and would make g_node_insert() more time consuming.
1282 guint g_node_max_height (GNode *root);
1284 void g_node_children_foreach (GNode *node,
1285 GTraverseFlags flags,
1286 GNodeForeachFunc func,
1288 void g_node_reverse_children (GNode *node);
1289 guint g_node_n_children (GNode *node);
1290 GNode* g_node_nth_child (GNode *node,
1292 GNode* g_node_last_child (GNode *node);
1293 GNode* g_node_find_child (GNode *node,
1294 GTraverseFlags flags,
1296 gint g_node_child_position (GNode *node,
1298 gint g_node_child_index (GNode *node,
1301 GNode* g_node_first_sibling (GNode *node);
1302 GNode* g_node_last_sibling (GNode *node);
1304 #define g_node_prev_sibling(node) ((node) ? \
1305 ((GNode*) (node))->prev : NULL)
1306 #define g_node_next_sibling(node) ((node) ? \
1307 ((GNode*) (node))->next : NULL)
1308 #define g_node_first_child(node) ((node) ? \
1309 ((GNode*) (node))->children : NULL)
1312 /* Callback maintenance functions
1314 #define G_HOOK_FLAG_USER_SHIFT (4)
1317 G_HOOK_FLAG_ACTIVE = 1 << 0,
1318 G_HOOK_FLAG_IN_CALL = 1 << 1,
1319 G_HOOK_FLAG_MASK = 0x0f
1322 #define G_HOOK_DEFERRED_DESTROY ((GHookFreeFunc) 0x01)
1330 GMemChunk *hook_memchunk;
1331 GHookFreeFunc hook_free; /* virtual function */
1332 GHookFreeFunc hook_destroy; /* virtual function */
1344 GDestroyNotify destroy;
1347 #define G_HOOK_ACTIVE(hook) ((((GHook*) hook)->flags & \
1348 G_HOOK_FLAG_ACTIVE) != 0)
1349 #define G_HOOK_IN_CALL(hook) ((((GHook*) hook)->flags & \
1350 G_HOOK_FLAG_IN_CALL) != 0)
1351 #define G_HOOK_IS_VALID(hook) (((GHook*) hook)->hook_id != 0 && \
1352 G_HOOK_ACTIVE (hook))
1353 #define G_HOOK_IS_UNLINKED(hook) (((GHook*) hook)->next == NULL && \
1354 ((GHook*) hook)->prev == NULL && \
1355 ((GHook*) hook)->hook_id == 0 && \
1356 ((GHook*) hook)->ref_count == 0)
1358 void g_hook_list_init (GHookList *hook_list,
1360 void g_hook_list_clear (GHookList *hook_list);
1361 GHook* g_hook_alloc (GHookList *hook_list);
1362 void g_hook_free (GHookList *hook_list,
1364 void g_hook_ref (GHookList *hook_list,
1366 void g_hook_unref (GHookList *hook_list,
1368 gboolean g_hook_destroy (GHookList *hook_list,
1370 void g_hook_destroy_link (GHookList *hook_list,
1372 void g_hook_prepend (GHookList *hook_list,
1374 void g_hook_insert_before (GHookList *hook_list,
1377 void g_hook_insert_sorted (GHookList *hook_list,
1379 GHookCompareFunc func);
1380 GHook* g_hook_get (GHookList *hook_list,
1382 GHook* g_hook_find (GHookList *hook_list,
1383 gboolean need_valids,
1386 GHook* g_hook_find_data (GHookList *hook_list,
1387 gboolean need_valids,
1389 GHook* g_hook_find_func (GHookList *hook_list,
1390 gboolean need_valids,
1392 GHook* g_hook_find_func_data (GHookList *hook_list,
1393 gboolean need_valids,
1396 /* return the first valid hook, and increment its reference count */
1397 GHook* g_hook_first_valid (GHookList *hook_list,
1398 gboolean may_be_in_call);
1399 /* return the next valid hook with incremented reference count, and
1400 * decrement the reference count of the original hook
1402 GHook* g_hook_next_valid (GHookList *hook_list,
1404 gboolean may_be_in_call);
1406 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1407 gint g_hook_compare_ids (GHook *new_hook,
1410 /* convenience macros */
1411 #define g_hook_append( hook_list, hook ) \
1412 g_hook_insert_before ((hook_list), NULL, (hook))
1414 /* invoke all valid hooks with the (*GHookFunc) signature.
1416 void g_hook_list_invoke (GHookList *hook_list,
1417 gboolean may_recurse);
1418 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1419 * and destroy the hook if FALSE is returned.
1421 void g_hook_list_invoke_check (GHookList *hook_list,
1422 gboolean may_recurse);
1423 /* invoke a marshaller on all valid hooks.
1425 void g_hook_list_marshal (GHookList *hook_list,
1426 gboolean may_recurse,
1427 GHookMarshaller marshaller,
1429 void g_hook_list_marshal_check (GHookList *hook_list,
1430 gboolean may_recurse,
1431 GHookCheckMarshaller marshaller,
1435 /* Fatal error handlers.
1436 * g_on_error_query() will prompt the user to either
1437 * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1438 * g_on_error_stack_trace() invokes gdb, which attaches to the current
1439 * process and shows a stack trace.
1440 * These function may cause different actions on non-unix platforms.
1441 * The prg_name arg is required by gdb to find the executable, if it is
1442 * passed as NULL, g_on_error_query() will try g_get_prgname().
1444 void g_on_error_query (const gchar *prg_name);
1445 void g_on_error_stack_trace (const gchar *prg_name);
1448 /* Logging mechanism
1450 extern const gchar *g_log_domain_glib;
1451 guint g_log_set_handler (const gchar *log_domain,
1452 GLogLevelFlags log_levels,
1454 gpointer user_data);
1455 void g_log_remove_handler (const gchar *log_domain,
1457 void g_log_default_handler (const gchar *log_domain,
1458 GLogLevelFlags log_level,
1459 const gchar *message,
1460 gpointer unused_data);
1461 void g_log (const gchar *log_domain,
1462 GLogLevelFlags log_level,
1463 const gchar *format,
1464 ...) G_GNUC_PRINTF (3, 4);
1465 void g_logv (const gchar *log_domain,
1466 GLogLevelFlags log_level,
1467 const gchar *format,
1469 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
1470 GLogLevelFlags fatal_mask);
1471 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
1472 #ifndef G_LOG_DOMAIN
1473 #define G_LOG_DOMAIN ((gchar*) 0)
1474 #endif /* G_LOG_DOMAIN */
1475 #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
1476 #define g_error(...) g_log (G_LOG_DOMAIN, \
1477 G_LOG_LEVEL_ERROR, \
1479 #define g_message(...) g_log (G_LOG_DOMAIN, \
1480 G_LOG_LEVEL_MESSAGE, \
1482 #define g_critical(...) g_log (G_LOG_DOMAIN, \
1483 G_LOG_LEVEL_CRITICAL, \
1485 #define g_warning(...) g_log (G_LOG_DOMAIN, \
1486 G_LOG_LEVEL_WARNING, \
1488 #elif defined (__GNUC__)
1489 #define g_error(format...) g_log (G_LOG_DOMAIN, \
1490 G_LOG_LEVEL_ERROR, \
1492 #define g_message(format...) g_log (G_LOG_DOMAIN, \
1493 G_LOG_LEVEL_MESSAGE, \
1495 #define g_critical(format...) g_log (G_LOG_DOMAIN, \
1496 G_LOG_LEVEL_CRITICAL, \
1498 #define g_warning(format...) g_log (G_LOG_DOMAIN, \
1499 G_LOG_LEVEL_WARNING, \
1501 #else /* !__GNUC__ */
1503 g_error (const gchar *format,
1507 va_start (args, format);
1508 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1512 g_message (const gchar *format,
1516 va_start (args, format);
1517 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1521 g_critical (const gchar *format,
1525 va_start (args, format);
1526 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
1530 g_warning (const gchar *format,
1534 va_start (args, format);
1535 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1538 #endif /* !__GNUC__ */
1540 typedef void (*GPrintFunc) (const gchar *string);
1541 void g_print (const gchar *format,
1542 ...) G_GNUC_PRINTF (1, 2);
1543 GPrintFunc g_set_print_handler (GPrintFunc func);
1544 void g_printerr (const gchar *format,
1545 ...) G_GNUC_PRINTF (1, 2);
1546 GPrintFunc g_set_printerr_handler (GPrintFunc func);
1548 /* deprecated compatibility functions, use g_log_set_handler() instead */
1549 typedef void (*GErrorFunc) (const gchar *str);
1550 typedef void (*GWarningFunc) (const gchar *str);
1551 GErrorFunc g_set_error_handler (GErrorFunc func);
1552 GWarningFunc g_set_warning_handler (GWarningFunc func);
1553 GPrintFunc g_set_message_handler (GPrintFunc func);
1556 /* Memory allocation and debugging
1560 #define g_malloc(size) ((gpointer) MALLOC (size))
1561 #define g_malloc0(size) ((gpointer) CALLOC (char, size))
1562 #define g_realloc(mem,size) ((gpointer) REALLOC (mem, char, size))
1563 #define g_free(mem) FREE (mem)
1565 #else /* !USE_DMALLOC */
1567 gpointer g_malloc (gulong size);
1568 gpointer g_malloc0 (gulong size);
1569 gpointer g_realloc (gpointer mem,
1571 void g_free (gpointer mem);
1573 #endif /* !USE_DMALLOC */
1575 void g_mem_profile (void);
1576 void g_mem_check (gpointer mem);
1578 /* Generic allocators
1580 GAllocator* g_allocator_new (const gchar *name,
1582 void g_allocator_free (GAllocator *allocator);
1584 #define G_ALLOCATOR_LIST (1)
1585 #define G_ALLOCATOR_SLIST (2)
1586 #define G_ALLOCATOR_NODE (3)
1589 /* "g_mem_chunk_new" creates a new memory chunk.
1590 * Memory chunks are used to allocate pieces of memory which are
1591 * always the same size. Lists are a good example of such a data type.
1592 * The memory chunk allocates and frees blocks of memory as needed.
1593 * Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1594 * allocated in a mem chunk. ("g_free" will most likely cause a seg
1595 * fault...somewhere).
1597 * Oh yeah, GMemChunk is an opaque data type. (You don't really
1598 * want to know what's going on inside do you?)
1601 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1602 * is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1603 * atom. (They are also useful for lists which use MemChunk to allocate
1604 * memory but are also part of the MemChunk implementation).
1605 * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1608 #define G_ALLOC_ONLY 1
1609 #define G_ALLOC_AND_FREE 2
1611 GMemChunk* g_mem_chunk_new (gchar *name,
1615 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
1616 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
1617 gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
1618 void g_mem_chunk_free (GMemChunk *mem_chunk,
1620 void g_mem_chunk_clean (GMemChunk *mem_chunk);
1621 void g_mem_chunk_reset (GMemChunk *mem_chunk);
1622 void g_mem_chunk_print (GMemChunk *mem_chunk);
1623 void g_mem_chunk_info (void);
1625 /* Ah yes...we have a "g_blow_chunks" function.
1626 * "g_blow_chunks" simply compresses all the chunks. This operation
1627 * consists of freeing every memory area that should be freed (but
1628 * which we haven't gotten around to doing yet). And, no,
1629 * "g_blow_chunks" doesn't follow the naming scheme, but it is a
1630 * much better name than "g_mem_chunk_clean_all" or something
1633 void g_blow_chunks (void);
1639 #define G_MICROSEC 1000000
1641 GTimer* g_timer_new (void);
1642 void g_timer_destroy (GTimer *timer);
1643 void g_timer_start (GTimer *timer);
1644 void g_timer_stop (GTimer *timer);
1645 void g_timer_reset (GTimer *timer);
1646 gdouble g_timer_elapsed (GTimer *timer,
1647 gulong *microseconds);
1648 void g_usleep (gulong microseconds);
1650 /* String utility functions that modify a string argument or
1651 * return a constant string that must not be freed.
1653 #define G_STR_DELIMITERS "_-|> <."
1654 gchar* g_strdelimit (gchar *string,
1655 const gchar *delimiters,
1656 gchar new_delimiter);
1657 gchar* g_strcanon (gchar *string,
1658 const gchar *valid_chars,
1660 gdouble g_strtod (const gchar *nptr,
1662 gchar* g_strerror (gint errnum);
1663 gchar* g_strsignal (gint signum);
1664 gint g_strcasecmp (const gchar *s1,
1666 gint g_strncasecmp (const gchar *s1,
1669 gchar* g_strdown (gchar *string);
1670 gchar* g_strup (gchar *string);
1671 gchar* g_strreverse (gchar *string);
1672 gsize g_strlcpy (gchar *dest,
1675 gsize g_strlcat (gchar *dest,
1678 /* removes leading spaces */
1679 gchar* g_strchug (gchar *string);
1680 /* removes trailing spaces */
1681 gchar* g_strchomp (gchar *string);
1682 /* removes leading & trailing spaces */
1683 #define g_strstrip( string ) g_strchomp (g_strchug (string))
1685 /* String utility functions that return a newly allocated string which
1686 * ought to be freed with g_free from the caller at some point.
1688 gchar* g_strdup (const gchar *str);
1689 gchar* g_strdup_printf (const gchar *format,
1690 ...) G_GNUC_PRINTF (1, 2);
1691 gchar* g_strdup_vprintf (const gchar *format,
1693 gchar* g_strndup (const gchar *str,
1695 gchar* g_strnfill (guint length,
1697 gchar* g_strconcat (const gchar *string1,
1698 ...); /* NULL terminated */
1699 gchar* g_strjoin (const gchar *separator,
1700 ...); /* NULL terminated */
1701 /* Make a copy of a string interpreting C string -style escape
1702 * sequences. Inverse of g_strescape. The recognized sequences are \b
1703 * \f \n \r \t \\ \" and the octal format.
1705 gchar* g_strcompress (const gchar *source);
1707 /* Convert between the operating system (or C runtime)
1708 * representation of file names and UTF-8.
1710 gchar* g_filename_to_utf8 (const gchar *opsysstring);
1711 gchar* g_filename_from_utf8 (const gchar *utf8string);
1713 /* Copy a string escaping nonprintable characters like in C strings.
1714 * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
1715 * to a string containing characters that are not to be escaped.
1717 * Deprecated API: gchar* g_strescape (const gchar *source);
1718 * Luckily this function wasn't used much, using NULL as second parameter
1719 * provides mostly identical semantics.
1721 gchar* g_strescape (const gchar *source,
1722 const gchar *exceptions);
1724 gpointer g_memdup (gconstpointer mem,
1727 /* NULL terminated string arrays.
1728 * g_strsplit() splits up string into max_tokens tokens at delim and
1729 * returns a newly allocated string array.
1730 * g_strjoinv() concatenates all of str_array's strings, sliding in an
1731 * optional separator, the returned string is newly allocated.
1732 * g_strfreev() frees the array itself and all of its strings.
1734 gchar** g_strsplit (const gchar *string,
1735 const gchar *delimiter,
1737 gchar* g_strjoinv (const gchar *separator,
1739 void g_strfreev (gchar **str_array);
1743 /* calculate a string size, guarranteed to fit format + args.
1745 guint g_printf_string_upper_bound (const gchar* format,
1749 /* Retrive static string info
1751 gchar* g_get_user_name (void);
1752 gchar* g_get_real_name (void);
1753 gchar* g_get_home_dir (void);
1754 gchar* g_get_tmp_dir (void);
1755 gchar* g_get_prgname (void);
1756 void g_set_prgname (const gchar *prgname);
1759 /* Miscellaneous utility functions
1761 guint g_parse_debug_string (const gchar *string,
1764 gint g_snprintf (gchar *string,
1766 gchar const *format,
1767 ...) G_GNUC_PRINTF (3, 4);
1768 gint g_vsnprintf (gchar *string,
1770 gchar const *format,
1772 /* Check if a file name is an absolute path */
1773 gboolean g_path_is_absolute (const gchar *file_name);
1774 /* In case of absolute paths, skip the root part */
1775 gchar* g_path_skip_root (gchar *file_name);
1777 /* These two functions are deprecated and will be removed in the next
1778 * major release of GLib. Use g_path_get_dirname/g_path_get_basename
1779 * instead. Whatch out! The string returned by g_path_get_basename
1780 * must be g_freed, while the string returned by g_basename must not.*/
1781 gchar* g_basename (const gchar *file_name);
1782 gchar* g_dirname (const gchar *file_name);
1784 /* The returned strings are newly allocated with g_malloc() */
1785 gchar* g_get_current_dir (void);
1786 gchar* g_path_get_basename (const gchar *file_name);
1787 gchar* g_path_get_dirname (const gchar *file_name);
1789 /* Get the codeset for the current locale */
1790 /* gchar * g_get_codeset (void); */
1792 /* return the environment string for the variable. The returned memory
1793 * must not be freed. */
1794 gchar* g_getenv (const gchar *variable);
1796 /* we use a GLib function as a replacement for ATEXIT, so
1797 * the programmer is not required to check the return value
1798 * (if there is any in the implementation) and doesn't encounter
1799 * missing include files.
1801 void g_atexit (GVoidFunc func);
1806 G_INLINE_FUNC gint g_bit_nth_lsf (guint32 mask,
1810 g_bit_nth_lsf (guint32 mask,
1816 if (mask & (1 << (guint) nth_bit))
1819 while (nth_bit < 32);
1822 #endif /* G_CAN_INLINE */
1824 G_INLINE_FUNC gint g_bit_nth_msf (guint32 mask,
1828 g_bit_nth_msf (guint32 mask,
1836 if (mask & (1 << (guint) nth_bit))
1839 while (nth_bit > 0);
1842 #endif /* G_CAN_INLINE */
1844 G_INLINE_FUNC guint g_bit_storage (guint number);
1847 g_bit_storage (guint number)
1849 register guint n_bits = 0;
1859 #endif /* G_CAN_INLINE */
1863 * elements need to be >= sizeof (gpointer)
1865 G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p,
1869 g_trash_stack_push (GTrashStack **stack_p,
1872 GTrashStack *data = (GTrashStack *) data_p;
1874 data->next = *stack_p;
1877 #endif /* G_CAN_INLINE */
1879 G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p);
1881 G_INLINE_FUNC gpointer
1882 g_trash_stack_pop (GTrashStack **stack_p)
1889 *stack_p = data->next;
1890 /* NULLify private pointer here, most platforms store NULL as
1891 * subsequent 0 bytes
1898 #endif /* G_CAN_INLINE */
1900 G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p);
1902 G_INLINE_FUNC gpointer
1903 g_trash_stack_peek (GTrashStack **stack_p)
1911 #endif /* G_CAN_INLINE */
1913 G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p);
1916 g_trash_stack_height (GTrashStack **stack_p)
1921 for (data = *stack_p; data; data = data->next)
1926 #endif /* G_CAN_INLINE */
1931 GStringChunk* g_string_chunk_new (gint size);
1932 void g_string_chunk_free (GStringChunk *chunk);
1933 gchar* g_string_chunk_insert (GStringChunk *chunk,
1934 const gchar *string);
1935 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
1936 const gchar *string);
1941 GString* g_string_new (const gchar *init);
1942 GString* g_string_sized_new (guint dfl_size);
1943 gchar* g_string_free (GString *string,
1944 gboolean free_segment);
1945 gboolean g_string_equal (const GString *v,
1947 guint g_string_hash (const GString *str);
1948 GString* g_string_assign (GString *string,
1950 GString* g_string_truncate (GString *string,
1952 GString* g_string_insert_len (GString *string,
1956 GString* g_string_append (GString *string,
1958 GString* g_string_append_len (GString *string,
1961 GString* g_string_append_c (GString *string,
1963 GString* g_string_prepend (GString *string,
1965 GString* g_string_prepend_c (GString *string,
1967 GString* g_string_prepend_len (GString *string,
1970 GString* g_string_insert (GString *string,
1973 GString* g_string_insert_c (GString *string,
1976 GString* g_string_erase (GString *string,
1979 GString* g_string_down (GString *string);
1980 GString* g_string_up (GString *string);
1981 void g_string_sprintf (GString *string,
1982 const gchar *format,
1983 ...) G_GNUC_PRINTF (2, 3);
1984 void g_string_sprintfa (GString *string,
1985 const gchar *format,
1986 ...) G_GNUC_PRINTF (2, 3);
1989 /* Resizable arrays, remove fills any cleared spot and shortens the
1990 * array, while preserving the order. remove_fast will distort the
1991 * order by moving the last element to the position of the removed
1994 #define g_array_append_val(a,v) g_array_append_vals (a, &v, 1)
1995 #define g_array_prepend_val(a,v) g_array_prepend_vals (a, &v, 1)
1996 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
1997 #define g_array_index(a,t,i) (((t*) (a)->data) [(i)])
1999 GArray* g_array_new (gboolean zero_terminated,
2001 guint element_size);
2002 GArray* g_array_sized_new (gboolean zero_terminated,
2005 guint reserved_size);
2006 gchar* g_array_free (GArray *array,
2007 gboolean free_segment);
2008 GArray* g_array_append_vals (GArray *array,
2011 GArray* g_array_prepend_vals (GArray *array,
2014 GArray* g_array_insert_vals (GArray *array,
2018 GArray* g_array_set_size (GArray *array,
2020 GArray* g_array_remove_index (GArray *array,
2022 GArray* g_array_remove_index_fast (GArray *array,
2025 /* Resizable pointer array. This interface is much less complicated
2026 * than the above. Add appends appends a pointer. Remove fills any
2027 * cleared spot and shortens the array. remove_fast will again distort
2030 #define g_ptr_array_index(array,index) (array->pdata)[index]
2031 GPtrArray* g_ptr_array_new (void);
2032 GPtrArray* g_ptr_array_sized_new (guint reserved_size);
2033 gpointer* g_ptr_array_free (GPtrArray *array,
2035 void g_ptr_array_set_size (GPtrArray *array,
2037 gpointer g_ptr_array_remove_index (GPtrArray *array,
2039 gpointer g_ptr_array_remove_index_fast (GPtrArray *array,
2041 gboolean g_ptr_array_remove (GPtrArray *array,
2043 gboolean g_ptr_array_remove_fast (GPtrArray *array,
2045 void g_ptr_array_add (GPtrArray *array,
2048 /* Byte arrays, an array of guint8. Implemented as a GArray,
2052 GByteArray* g_byte_array_new (void);
2053 GByteArray* g_byte_array_sized_new (guint reserved_size);
2054 guint8* g_byte_array_free (GByteArray *array,
2055 gboolean free_segment);
2056 GByteArray* g_byte_array_append (GByteArray *array,
2059 GByteArray* g_byte_array_prepend (GByteArray *array,
2062 GByteArray* g_byte_array_set_size (GByteArray *array,
2064 GByteArray* g_byte_array_remove_index (GByteArray *array,
2066 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
2072 gboolean g_str_equal (gconstpointer v,
2074 guint g_str_hash (gconstpointer v);
2076 gint g_int_equal (gconstpointer v,
2077 gconstpointer v2) G_GNUC_CONST;
2078 guint g_int_hash (gconstpointer v) G_GNUC_CONST;
2080 /* This "hash" function will just return the key's adress as an
2081 * unsigned integer. Useful for hashing on plain adresses or
2082 * simple integer values.
2083 * passing NULL into g_hash_table_new() as GHashFunc has the
2084 * same effect as passing g_direct_hash().
2086 guint g_direct_hash (gconstpointer v) G_GNUC_CONST;
2087 gint g_direct_equal (gconstpointer v,
2088 gconstpointer v2) G_GNUC_CONST;
2091 /* Quarks (string<->id association)
2093 GQuark g_quark_try_string (const gchar *string);
2094 GQuark g_quark_from_static_string (const gchar *string);
2095 GQuark g_quark_from_string (const gchar *string);
2096 gchar* g_quark_to_string (GQuark quark) G_GNUC_CONST;
2101 void g_datalist_init (GData **datalist);
2102 void g_datalist_clear (GData **datalist);
2103 gpointer g_datalist_id_get_data (GData **datalist,
2105 void g_datalist_id_set_data_full (GData **datalist,
2108 GDestroyNotify destroy_func);
2109 gpointer g_datalist_id_remove_no_notify (GData **datalist,
2111 void g_datalist_foreach (GData **datalist,
2112 GDataForeachFunc func,
2113 gpointer user_data);
2114 #define g_datalist_id_set_data(dl, q, d) \
2115 g_datalist_id_set_data_full ((dl), (q), (d), NULL)
2116 #define g_datalist_id_remove_data(dl, q) \
2117 g_datalist_id_set_data ((dl), (q), NULL)
2118 #define g_datalist_get_data(dl, k) \
2119 (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
2120 #define g_datalist_set_data_full(dl, k, d, f) \
2121 g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
2122 #define g_datalist_remove_no_notify(dl, k) \
2123 g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
2124 #define g_datalist_set_data(dl, k, d) \
2125 g_datalist_set_data_full ((dl), (k), (d), NULL)
2126 #define g_datalist_remove_data(dl, k) \
2127 g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
2130 /* Location Associated Keyed Data
2132 void g_dataset_destroy (gconstpointer dataset_location);
2133 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
2135 void g_dataset_id_set_data_full (gconstpointer dataset_location,
2138 GDestroyNotify destroy_func);
2139 gpointer g_dataset_id_remove_no_notify (gconstpointer dataset_location,
2141 void g_dataset_foreach (gconstpointer dataset_location,
2142 GDataForeachFunc func,
2143 gpointer user_data);
2144 #define g_dataset_id_set_data(l, k, d) \
2145 g_dataset_id_set_data_full ((l), (k), (d), NULL)
2146 #define g_dataset_id_remove_data(l, k) \
2147 g_dataset_id_set_data ((l), (k), NULL)
2148 #define g_dataset_get_data(l, k) \
2149 (g_dataset_id_get_data ((l), g_quark_try_string (k)))
2150 #define g_dataset_set_data_full(l, k, d, f) \
2151 g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
2152 #define g_dataset_remove_no_notify(l, k) \
2153 g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
2154 #define g_dataset_set_data(l, k, d) \
2155 g_dataset_set_data_full ((l), (k), (d), NULL)
2156 #define g_dataset_remove_data(l, k) \
2157 g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
2160 /* GScanner: Flexible lexical scanner for general purpose.
2163 /* Character sets */
2164 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2165 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
2166 #define G_CSET_DIGITS "0123456789"
2167 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
2168 "\307\310\311\312\313\314\315\316\317\320"\
2169 "\321\322\323\324\325\326"\
2170 "\330\331\332\333\334\335\336"
2171 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
2172 "\347\350\351\352\353\354\355\356\357\360"\
2173 "\361\362\363\364\365\366"\
2174 "\370\371\372\373\374\375\376\377"
2181 G_ERR_UNEXP_EOF_IN_STRING,
2182 G_ERR_UNEXP_EOF_IN_COMMENT,
2183 G_ERR_NON_DIGIT_IN_CONST,
2186 G_ERR_FLOAT_MALFORMED
2194 G_TOKEN_LEFT_PAREN = '(',
2195 G_TOKEN_RIGHT_PAREN = ')',
2196 G_TOKEN_LEFT_CURLY = '{',
2197 G_TOKEN_RIGHT_CURLY = '}',
2198 G_TOKEN_LEFT_BRACE = '[',
2199 G_TOKEN_RIGHT_BRACE = ']',
2200 G_TOKEN_EQUAL_SIGN = '=',
2201 G_TOKEN_COMMA = ',',
2217 G_TOKEN_IDENTIFIER_NULL,
2219 G_TOKEN_COMMENT_SINGLE,
2220 G_TOKEN_COMMENT_MULTI,
2227 gchar *v_identifier;
2239 struct _GScannerConfig
2243 gchar *cset_skip_characters; /* default: " \t\n" */
2244 gchar *cset_identifier_first;
2245 gchar *cset_identifier_nth;
2246 gchar *cpair_comment_single; /* default: "#\n" */
2248 /* Should symbol lookup work case sensitive?
2250 guint case_sensitive : 1;
2252 /* Boolean values to be adjusted "on the fly"
2253 * to configure scanning behaviour.
2255 guint skip_comment_multi : 1; /* C like comment */
2256 guint skip_comment_single : 1; /* single line comment */
2257 guint scan_comment_multi : 1; /* scan multi line comments? */
2258 guint scan_identifier : 1;
2259 guint scan_identifier_1char : 1;
2260 guint scan_identifier_NULL : 1;
2261 guint scan_symbols : 1;
2262 guint scan_binary : 1;
2263 guint scan_octal : 1;
2264 guint scan_float : 1;
2265 guint scan_hex : 1; /* `0x0ff0' */
2266 guint scan_hex_dollar : 1; /* `$0ff0' */
2267 guint scan_string_sq : 1; /* string: 'anything' */
2268 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
2269 guint numbers_2_int : 1; /* bin, octal, hex => int */
2270 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
2271 guint identifier_2_string : 1;
2272 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
2273 guint symbol_2_token : 1;
2274 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
2281 guint max_parse_errors;
2283 /* g_scanner_error() increments this field */
2286 /* name of input stream, featured by the default message handler */
2287 const gchar *input_name;
2289 /* data pointer for derived structures */
2290 gpointer derived_data;
2292 /* link into the scanner configuration */
2293 GScannerConfig *config;
2295 /* fields filled in after g_scanner_get_next_token() */
2301 /* fields filled in after g_scanner_peek_next_token() */
2302 GTokenType next_token;
2303 GTokenValue next_value;
2305 guint next_position;
2307 /* to be considered private */
2308 GHashTable *symbol_table;
2311 const gchar *text_end;
2315 /* handler function for _warn and _error */
2316 GScannerMsgFunc msg_handler;
2319 GScanner* g_scanner_new (GScannerConfig *config_templ);
2320 void g_scanner_destroy (GScanner *scanner);
2321 void g_scanner_input_file (GScanner *scanner,
2323 void g_scanner_sync_file_offset (GScanner *scanner);
2324 void g_scanner_input_text (GScanner *scanner,
2327 GTokenType g_scanner_get_next_token (GScanner *scanner);
2328 GTokenType g_scanner_peek_next_token (GScanner *scanner);
2329 GTokenType g_scanner_cur_token (GScanner *scanner);
2330 GTokenValue g_scanner_cur_value (GScanner *scanner);
2331 guint g_scanner_cur_line (GScanner *scanner);
2332 guint g_scanner_cur_position (GScanner *scanner);
2333 gboolean g_scanner_eof (GScanner *scanner);
2334 guint g_scanner_set_scope (GScanner *scanner,
2336 void g_scanner_scope_add_symbol (GScanner *scanner,
2338 const gchar *symbol,
2340 void g_scanner_scope_remove_symbol (GScanner *scanner,
2342 const gchar *symbol);
2343 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
2345 const gchar *symbol);
2346 void g_scanner_scope_foreach_symbol (GScanner *scanner,
2349 gpointer user_data);
2350 gpointer g_scanner_lookup_symbol (GScanner *scanner,
2351 const gchar *symbol);
2352 void g_scanner_unexp_token (GScanner *scanner,
2353 GTokenType expected_token,
2354 const gchar *identifier_spec,
2355 const gchar *symbol_spec,
2356 const gchar *symbol_name,
2357 const gchar *message,
2359 void g_scanner_error (GScanner *scanner,
2360 const gchar *format,
2361 ...) G_GNUC_PRINTF (2,3);
2362 void g_scanner_warn (GScanner *scanner,
2363 const gchar *format,
2364 ...) G_GNUC_PRINTF (2,3);
2365 gint g_scanner_stat_mode (const gchar *filename);
2366 /* keep downward source compatibility */
2367 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
2368 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2370 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
2371 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2373 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2374 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2377 /* The following two functions are deprecated and will be removed in
2378 * the next major release. They do no good. */
2379 void g_scanner_freeze_symbol_table (GScanner *scanner);
2380 void g_scanner_thaw_symbol_table (GScanner *scanner);
2388 GCompletionFunc func;
2394 GCompletion* g_completion_new (GCompletionFunc func);
2395 void g_completion_add_items (GCompletion* cmp,
2397 void g_completion_remove_items (GCompletion* cmp,
2399 void g_completion_clear_items (GCompletion* cmp);
2400 GList* g_completion_complete (GCompletion* cmp,
2402 gchar** new_prefix);
2403 void g_completion_free (GCompletion* cmp);
2408 * Date calculations (not time for now, to be resolved). These are a
2409 * mutant combination of Steffen Beyer's DateCalc routines
2410 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2411 * date routines (written for in-house software). Written by Havoc
2412 * Pennington <hp@pobox.com>
2415 typedef guint16 GDateYear;
2416 typedef guint8 GDateDay; /* day of the month */
2417 typedef struct _GDate GDate;
2418 /* make struct tm known without having to include time.h */
2421 /* enum used to specify order of appearance in parsed date strings */
2429 /* actual week and month values */
2432 G_DATE_BAD_WEEKDAY = 0,
2435 G_DATE_WEDNESDAY = 3,
2436 G_DATE_THURSDAY = 4,
2438 G_DATE_SATURDAY = 6,
2443 G_DATE_BAD_MONTH = 0,
2445 G_DATE_FEBRUARY = 2,
2452 G_DATE_SEPTEMBER = 9,
2453 G_DATE_OCTOBER = 10,
2454 G_DATE_NOVEMBER = 11,
2455 G_DATE_DECEMBER = 12
2458 #define G_DATE_BAD_JULIAN 0U
2459 #define G_DATE_BAD_DAY 0U
2460 #define G_DATE_BAD_YEAR 0U
2462 /* Note: directly manipulating structs is generally a bad idea, but
2463 * in this case it's an *incredibly* bad idea, because all or part
2464 * of this struct can be invalid at any given time. Use the functions,
2465 * or you will get hosed, I promise.
2469 guint julian_days : 32; /* julian days representation - we use a
2470 * bitfield hoping that 64 bit platforms
2471 * will pack this whole struct in one big
2475 guint julian : 1; /* julian is valid */
2476 guint dmy : 1; /* dmy is valid */
2478 /* DMY representation */
2484 /* g_date_new() returns an invalid date, you then have to _set() stuff
2485 * to get a usable object. You can also allocate a GDate statically,
2486 * then call g_date_clear() to initialize.
2488 GDate* g_date_new (void);
2489 GDate* g_date_new_dmy (GDateDay day,
2492 GDate* g_date_new_julian (guint32 julian_day);
2493 void g_date_free (GDate *date);
2495 /* check g_date_valid() after doing an operation that might fail, like
2496 * _parse. Almost all g_date operations are undefined on invalid
2497 * dates (the exceptions are the mutators, since you need those to
2498 * return to validity).
2500 gboolean g_date_valid (GDate *date);
2501 gboolean g_date_valid_day (GDateDay day);
2502 gboolean g_date_valid_month (GDateMonth month);
2503 gboolean g_date_valid_year (GDateYear year);
2504 gboolean g_date_valid_weekday (GDateWeekday weekday);
2505 gboolean g_date_valid_julian (guint32 julian_date);
2506 gboolean g_date_valid_dmy (GDateDay day,
2510 GDateWeekday g_date_weekday (GDate *date);
2511 GDateMonth g_date_month (GDate *date);
2512 GDateYear g_date_year (GDate *date);
2513 GDateDay g_date_day (GDate *date);
2514 guint32 g_date_julian (GDate *date);
2515 guint g_date_day_of_year (GDate *date);
2517 /* First monday/sunday is the start of week 1; if we haven't reached
2518 * that day, return 0. These are not ISO weeks of the year; that
2519 * routine needs to be added.
2520 * these functions return the number of weeks, starting on the
2523 guint g_date_monday_week_of_year (GDate *date);
2524 guint g_date_sunday_week_of_year (GDate *date);
2526 /* If you create a static date struct you need to clear it to get it
2527 * in a sane state before use. You can clear a whole array at
2528 * once with the ndates argument.
2530 void g_date_clear (GDate *date,
2533 /* The parse routine is meant for dates typed in by a user, so it
2534 * permits many formats but tries to catch common typos. If your data
2535 * needs to be strictly validated, it is not an appropriate function.
2537 void g_date_set_parse (GDate *date,
2539 void g_date_set_time (GDate *date,
2541 void g_date_set_month (GDate *date,
2543 void g_date_set_day (GDate *date,
2545 void g_date_set_year (GDate *date,
2547 void g_date_set_dmy (GDate *date,
2551 void g_date_set_julian (GDate *date,
2552 guint32 julian_date);
2553 gboolean g_date_is_first_of_month (GDate *date);
2554 gboolean g_date_is_last_of_month (GDate *date);
2556 /* To go forward by some number of weeks just go forward weeks*7 days */
2557 void g_date_add_days (GDate *date,
2559 void g_date_subtract_days (GDate *date,
2562 /* If you add/sub months while day > 28, the day might change */
2563 void g_date_add_months (GDate *date,
2565 void g_date_subtract_months (GDate *date,
2568 /* If it's feb 29, changing years can move you to the 28th */
2569 void g_date_add_years (GDate *date,
2571 void g_date_subtract_years (GDate *date,
2573 gboolean g_date_is_leap_year (GDateYear year) G_GNUC_CONST;
2574 guint8 g_date_days_in_month (GDateMonth month,
2575 GDateYear year) G_GNUC_CONST;
2576 guint8 g_date_monday_weeks_in_year (GDateYear year) G_GNUC_CONST;
2577 guint8 g_date_sunday_weeks_in_year (GDateYear year) G_GNUC_CONST;
2579 /* qsort-friendly (with a cast...) */
2580 gint g_date_compare (GDate *lhs,
2582 void g_date_to_struct_tm (GDate *date,
2585 /* Just like strftime() except you can only use date-related formats.
2586 * Using a time format is undefined.
2588 gsize g_date_strftime (gchar *s,
2590 const gchar *format,
2596 * Indexed Relations. Imagine a really simple table in a
2597 * database. Relations are not ordered. This data type is meant for
2598 * maintaining a N-way mapping.
2600 * g_relation_new() creates a relation with FIELDS fields
2602 * g_relation_destroy() frees all resources
2603 * g_tuples_destroy() frees the result of g_relation_select()
2605 * g_relation_index() indexes relation FIELD with the provided
2606 * equality and hash functions. this must be done before any
2607 * calls to insert are made.
2609 * g_relation_insert() inserts a new tuple. you are expected to
2610 * provide the right number of fields.
2612 * g_relation_delete() deletes all relations with KEY in FIELD
2613 * g_relation_select() returns ...
2614 * g_relation_count() counts ...
2617 GRelation* g_relation_new (gint fields);
2618 void g_relation_destroy (GRelation *relation);
2619 void g_relation_index (GRelation *relation,
2621 GHashFunc hash_func,
2622 GCompareFunc key_compare_func);
2623 void g_relation_insert (GRelation *relation,
2625 gint g_relation_delete (GRelation *relation,
2628 GTuples* g_relation_select (GRelation *relation,
2631 gint g_relation_count (GRelation *relation,
2634 gboolean g_relation_exists (GRelation *relation,
2636 void g_relation_print (GRelation *relation);
2638 void g_tuples_destroy (GTuples *tuples);
2639 gpointer g_tuples_index (GTuples *tuples,
2644 /* GRand - a good and fast random number generator: Mersenne Twister
2645 * see http://www.math.keio.ac.jp/~matumoto/emt.html for more info.
2646 * The range functions return a value in the intervall [min,max).
2647 * int -> [0..2^32-1]
2648 * int_range -> [min..max-1]
2650 * double_range -> [min..max)
2653 GRand* g_rand_new_with_seed (guint32 seed);
2654 GRand* g_rand_new (void);
2655 void g_rand_free (GRand *rand);
2657 void g_rand_set_seed (GRand *rand,
2659 guint32 g_rand_int (GRand *rand);
2660 gint32 g_rand_int_range (GRand *rand,
2663 gdouble g_rand_double (GRand *rand);
2664 gdouble g_rand_double_range (GRand *rand,
2668 void g_random_set_seed (guint32 seed);
2669 guint32 g_random_int (void);
2670 gint32 g_random_int_range (gint32 min,
2672 gdouble g_random_double (void);
2673 gdouble g_random_double_range (gdouble min,
2680 /* This function returns prime numbers spaced by approximately 1.5-2.0
2681 * and is for use in resizing data structures which prefer
2682 * prime-valued sizes. The closest spaced prime function returns the
2683 * next largest prime, or the highest it knows about which is about
2686 guint g_spaced_primes_closest (guint num) G_GNUC_CONST;
2692 typedef struct _GIOFuncs GIOFuncs;
2708 G_IO_IN GLIB_SYSDEF_POLLIN,
2709 G_IO_OUT GLIB_SYSDEF_POLLOUT,
2710 G_IO_PRI GLIB_SYSDEF_POLLPRI,
2711 G_IO_ERR GLIB_SYSDEF_POLLERR,
2712 G_IO_HUP GLIB_SYSDEF_POLLHUP,
2713 G_IO_NVAL GLIB_SYSDEF_POLLNVAL
2718 guint channel_flags;
2723 typedef gboolean (*GIOFunc) (GIOChannel *source,
2724 GIOCondition condition,
2728 GIOError (*io_read) (GIOChannel *channel,
2732 GIOError (*io_write) (GIOChannel *channel,
2735 guint *bytes_written);
2736 GIOError (*io_seek) (GIOChannel *channel,
2739 void (*io_close) (GIOChannel *channel);
2740 guint (*io_add_watch) (GIOChannel *channel,
2742 GIOCondition condition,
2745 GDestroyNotify notify);
2746 void (*io_free) (GIOChannel *channel);
2749 void g_io_channel_init (GIOChannel *channel);
2750 void g_io_channel_ref (GIOChannel *channel);
2751 void g_io_channel_unref (GIOChannel *channel);
2752 GIOError g_io_channel_read (GIOChannel *channel,
2756 GIOError g_io_channel_write (GIOChannel *channel,
2759 guint *bytes_written);
2760 GIOError g_io_channel_seek (GIOChannel *channel,
2763 void g_io_channel_close (GIOChannel *channel);
2764 guint g_io_add_watch_full (GIOChannel *channel,
2766 GIOCondition condition,
2769 GDestroyNotify notify);
2770 guint g_io_add_watch (GIOChannel *channel,
2771 GIOCondition condition,
2773 gpointer user_data);
2778 typedef struct _GTimeVal GTimeVal;
2779 typedef struct _GSourceFuncs GSourceFuncs;
2780 typedef struct _GMainLoop GMainLoop; /* Opaque */
2787 struct _GSourceFuncs
2789 gboolean (*prepare) (gpointer source_data,
2790 GTimeVal *current_time,
2792 gpointer user_data);
2793 gboolean (*check) (gpointer source_data,
2794 GTimeVal *current_time,
2795 gpointer user_data);
2796 gboolean (*dispatch) (gpointer source_data,
2797 GTimeVal *dispatch_time,
2798 gpointer user_data);
2799 GDestroyNotify destroy;
2802 /* Standard priorities */
2804 #define G_PRIORITY_HIGH -100
2805 #define G_PRIORITY_DEFAULT 0
2806 #define G_PRIORITY_HIGH_IDLE 100
2807 #define G_PRIORITY_DEFAULT_IDLE 200
2808 #define G_PRIORITY_LOW 300
2810 typedef gboolean (*GSourceFunc) (gpointer data);
2812 /* Hooks for adding to the main loop */
2813 guint g_source_add (gint priority,
2814 gboolean can_recurse,
2815 GSourceFuncs *funcs,
2816 gpointer source_data,
2818 GDestroyNotify notify);
2819 gboolean g_source_remove (guint tag);
2820 gboolean g_source_remove_by_user_data (gpointer user_data);
2821 gboolean g_source_remove_by_source_data (gpointer source_data);
2822 gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
2823 gpointer user_data);
2825 void g_get_current_time (GTimeVal *result);
2827 /* Running the main loop */
2828 GMainLoop* g_main_new (gboolean is_running);
2829 void g_main_run (GMainLoop *loop);
2830 void g_main_quit (GMainLoop *loop);
2831 void g_main_destroy (GMainLoop *loop);
2832 gboolean g_main_is_running (GMainLoop *loop);
2834 /* Run a single iteration of the mainloop. If block is FALSE,
2837 gboolean g_main_iteration (gboolean may_block);
2839 /* See if any events are pending */
2840 gboolean g_main_pending (void);
2842 /* Idles and timeouts */
2843 guint g_timeout_add_full (gint priority,
2845 GSourceFunc function,
2847 GDestroyNotify notify);
2848 guint g_timeout_add (guint interval,
2849 GSourceFunc function,
2851 guint g_idle_add (GSourceFunc function,
2853 guint g_idle_add_full (gint priority,
2854 GSourceFunc function,
2856 GDestroyNotify destroy);
2857 gboolean g_idle_remove_by_data (gpointer data);
2861 * System-specific IO and main loop calls
2863 * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
2864 * descriptor as provided by the C runtime) that can be used by
2865 * MsgWaitForMultipleObjects. This does *not* include file handles
2866 * from CreateFile, SOCKETs, nor pipe handles. (But you can use
2867 * WSAEventSelect to signal events when a SOCKET is readable).
2869 * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
2870 * indicate polling for messages. These message queue GPollFDs should
2871 * be added with the g_main_poll_win32_msg_add function.
2873 * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
2874 * (GTK) programs, as GDK itself wants to read messages and convert them
2877 * So, unless you really know what you are doing, it's best not to try
2878 * to use the main loop polling stuff for your own needs on
2879 * Win32. It's really only written for the GIMP's needs so
2883 typedef struct _GPollFD GPollFD;
2884 typedef gint (*GPollFunc) (GPollFD *ufds,
2894 void g_main_add_poll (GPollFD *fd,
2896 void g_main_remove_poll (GPollFD *fd);
2897 void g_main_set_poll_func (GPollFunc func);
2901 /* Useful on other platforms, too? */
2903 GPollFunc g_main_win32_get_poll_func (void);
2907 /* On Unix, IO channels created with this function for any file
2908 * descriptor or socket.
2910 * On Win32, use this only for files opened with the MSVCRT (the
2911 * Microsoft run-time C library) _open() or _pipe, including file
2912 * descriptors 0, 1 and 2 (corresponding to stdin, stdout and stderr).
2914 * The term file descriptor as used in the context of Win32 refers to
2915 * the emulated Unix-like file descriptors MSVCRT provides. The native
2916 * corresponding concept is file HANDLE. There isn't as of yet a way to
2917 * get GIOChannels for file HANDLEs.
2919 GIOChannel* g_io_channel_unix_new (int fd);
2920 gint g_io_channel_unix_get_fd (GIOChannel *channel);
2924 #define G_WIN32_MSG_HANDLE 19981206
2926 /* Use this to get a GPollFD from a GIOChannel, so that you can call
2927 * g_io_channel_win32_poll(). After calling this you should only use
2928 * g_io_channel_read() to read from the GIOChannel, i.e. never read()
2929 * or recv() from the underlying file descriptor or SOCKET.
2931 void g_io_channel_win32_make_pollfd (GIOChannel *channel,
2932 GIOCondition condition,
2935 /* This can be used to wait a until at least one of the channels is readable.
2936 * On Unix you would do a select() on the file descriptors of the channels.
2937 * This should probably be available for all platforms?
2939 gint g_io_channel_win32_poll (GPollFD *fds,
2943 /* This is used to add polling for Windows messages. GDK (GTk+) programs
2944 * should *not* use this.
2946 void g_main_poll_win32_msg_add (gint priority,
2950 /* An IO channel for Windows messages for window handle hwnd. */
2951 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
2953 /* An IO channel for C runtime (emulated Unix-like) file
2954 * descriptors. Identical to g_io_channel_unix_new above.
2955 * After calling g_io_add_watch() on a IO channel returned
2956 * by this function, you shouldn't call read() on the file
2959 GIOChannel* g_io_channel_win32_new_fd (int fd);
2961 /* Get the C runtime file descriptor of a channel. */
2962 gint g_io_channel_win32_get_fd (GIOChannel *channel);
2964 /* An IO channel for a SOCK_STREAM winsock socket. The parameter
2965 * should be a SOCKET. After calling g_io_add_watch() on a IO channel
2966 * returned by this function, you shouldn't call recv() on the SOCKET.
2968 GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
2972 /* Windows emulation stubs for common Unix functions
2975 # define MAXPATHLEN 1024
2982 * To get prototypes for the following POSIXish functions, you have to
2983 * include the indicated non-POSIX headers. The functions are defined
2984 * in OLDNAMES.LIB (MSVC) or -lmoldname-msvc (mingw32).
2986 * getcwd: <direct.h> (MSVC), <io.h> (mingw32)
2987 * getpid: <process.h>
2989 * unlink: <stdio.h> or <io.h>
2990 * open, read, write, lseek, close: <io.h>
2995 /* pipe is not in OLDNAMES.LIB or -lmoldname-msvc. */
2996 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
2998 /* For some POSIX functions that are not provided by the MS runtime,
2999 * we provide emulators in glib, which are prefixed with g_win32_.
3001 # define ftruncate(fd, size) g_win32_ftruncate (fd, size)
3003 /* -lmingw32 also has emulations for these, but we need our own
3004 * for MSVC anyhow, so we might aswell use them always.
3006 # define opendir g_win32_opendir
3007 # define readdir g_win32_readdir
3008 # define rewinddir g_win32_rewinddir
3009 # define closedir g_win32_closedir
3010 # define NAME_MAX 255
3015 gboolean just_opened;
3016 guint find_file_handle;
3017 gpointer find_file_data;
3019 typedef struct DIR DIR;
3022 gchar d_name[NAME_MAX + 1];
3024 /* emulation functions */
3025 extern int g_win32_ftruncate (gint f,
3027 DIR* g_win32_opendir (const gchar *dirname);
3028 struct dirent* g_win32_readdir (DIR *dir);
3029 void g_win32_rewinddir (DIR *dir);
3030 gint g_win32_closedir (DIR *dir);
3032 /* The MS setlocale uses locale names of the form "English_United
3033 * States.1252" etc. We want the Unixish standard form "en", "zh_TW"
3034 * etc. This function gets the current thread locale from Windows and
3035 * returns it as a string of the above form for use in forming file
3036 * names etc. The returned string should be deallocated with g_free().
3038 gchar * g_win32_getlocale (void);
3040 /* Translate a Win32 error code (as returned by GetLastError()) into
3041 * the corresponding message. The returned string should be deallocated
3044 gchar * g_win32_error_message (gint error);
3046 #endif /* G_OS_WIN32 */
3049 /* GLib Thread support
3052 extern GQuark g_thread_error_quark();
3053 #define G_THREAD_ERROR g_thread_error_quark()
3057 G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
3060 typedef void (*GThreadFunc) (gpointer value);
3064 G_THREAD_PRIORITY_LOW,
3065 G_THREAD_PRIORITY_NORMAL,
3066 G_THREAD_PRIORITY_HIGH,
3067 G_THREAD_PRIORITY_URGENT
3070 typedef struct _GThread GThread;
3073 GThreadPriority priority;
3078 typedef struct _GMutex GMutex;
3079 typedef struct _GCond GCond;
3080 typedef struct _GPrivate GPrivate;
3081 typedef struct _GStaticPrivate GStaticPrivate;
3082 typedef struct _GAsyncQueue GAsyncQueue;
3083 typedef struct _GThreadPool GThreadPool;
3085 typedef struct _GThreadFunctions GThreadFunctions;
3086 struct _GThreadFunctions
3088 GMutex* (*mutex_new) (void);
3089 void (*mutex_lock) (GMutex *mutex);
3090 gboolean (*mutex_trylock) (GMutex *mutex);
3091 void (*mutex_unlock) (GMutex *mutex);
3092 void (*mutex_free) (GMutex *mutex);
3093 GCond* (*cond_new) (void);
3094 void (*cond_signal) (GCond *cond);
3095 void (*cond_broadcast) (GCond *cond);
3096 void (*cond_wait) (GCond *cond,
3098 gboolean (*cond_timed_wait) (GCond *cond,
3100 GTimeVal *end_time);
3101 void (*cond_free) (GCond *cond);
3102 GPrivate* (*private_new) (GDestroyNotify destructor);
3103 gpointer (*private_get) (GPrivate *private_key);
3104 void (*private_set) (GPrivate *private_key,
3106 void (*thread_create) (GThreadFunc thread_func,
3111 GThreadPriority priority,
3114 void (*thread_yield) (void);
3115 void (*thread_join) (gpointer thread);
3116 void (*thread_exit) (void);
3117 void (*thread_set_priority)(gpointer thread,
3118 GThreadPriority priority);
3119 void (*thread_self) (gpointer thread);
3122 GLIB_VAR GThreadFunctions g_thread_functions_for_glib_use;
3123 GLIB_VAR gboolean g_thread_use_default_impl;
3124 GLIB_VAR gboolean g_threads_got_initialized;
3126 /* initializes the mutex/cond/private implementation for glib, might
3127 * only be called once, and must not be called directly or indirectly
3128 * from another glib-function, e.g. as a callback.
3130 void g_thread_init (GThreadFunctions *vtable);
3132 /* internal function for fallback static mutex implementation */
3133 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
3135 /* shorthands for conditional and unconditional function calls */
3136 #define G_THREAD_UF(name, arglist) \
3137 (*g_thread_functions_for_glib_use . name) arglist
3138 #define G_THREAD_CF(name, fail, arg) \
3139 (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
3140 /* keep in mind, all those mutexes and static mutexes are not
3141 * recursive in general, don't rely on that
3143 #define g_thread_supported() (g_threads_got_initialized)
3144 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
3145 #define g_mutex_lock(mutex) G_THREAD_CF (mutex_lock, (void)0, (mutex))
3146 #define g_mutex_trylock(mutex) G_THREAD_CF (mutex_trylock, TRUE, (mutex))
3147 #define g_mutex_unlock(mutex) G_THREAD_CF (mutex_unlock, (void)0, (mutex))
3148 #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex))
3149 #define g_cond_new() G_THREAD_UF (cond_new, ())
3150 #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
3151 #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
3152 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait, (void)0, (cond, \
3154 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
3155 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
3159 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
3160 #define g_private_get(private_key) G_THREAD_CF (private_get, \
3161 ((gpointer)private_key), \
3163 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
3164 (void) (private_key = \
3165 (GPrivate*) (value)), \
3166 (private_key, value))
3167 #define g_thread_yield() G_THREAD_CF (thread_yield, (void)0, ())
3168 #define g_thread_exit() G_THREAD_CF (thread_exit, (void)0, ())
3170 GThread* g_thread_create (GThreadFunc thread_func,
3175 GThreadPriority priority,
3177 GThread* g_thread_self ();
3178 void g_thread_join (GThread *thread);
3179 void g_thread_set_priority (GThread *thread,
3180 GThreadPriority priority);
3182 /* GStaticMutexes can be statically initialized with the value
3183 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
3184 * much easier, than having to explicitly allocate the mutex before
3187 #define g_static_mutex_lock(mutex) \
3188 g_mutex_lock (g_static_mutex_get_mutex (mutex))
3189 #define g_static_mutex_trylock(mutex) \
3190 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
3191 #define g_static_mutex_unlock(mutex) \
3192 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
3194 struct _GStaticPrivate
3198 #define G_STATIC_PRIVATE_INIT { 0 }
3199 gpointer g_static_private_get (GStaticPrivate *private_key);
3200 void g_static_private_set (GStaticPrivate *private_key,
3202 GDestroyNotify notify);
3203 gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
3205 void g_static_private_set_for_thread (GStaticPrivate *private_key,
3208 GDestroyNotify notify);
3210 typedef struct _GStaticRecMutex GStaticRecMutex;
3211 struct _GStaticRecMutex
3215 GSystemThread owner;
3218 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT }
3219 void g_static_rec_mutex_lock (GStaticRecMutex *mutex);
3220 gboolean g_static_rec_mutex_trylock (GStaticRecMutex *mutex);
3221 void g_static_rec_mutex_unlock (GStaticRecMutex *mutex);
3222 void g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
3224 guint g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
3226 typedef struct _GStaticRWLock GStaticRWLock;
3227 struct _GStaticRWLock
3234 guint want_to_write;
3237 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
3239 void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
3240 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
3241 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
3242 void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
3243 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
3244 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
3245 void g_static_rw_lock_free (GStaticRWLock* lock);
3247 /* these are some convenience macros that expand to nothing if GLib
3248 * was configured with --disable-threads. for using StaticMutexes,
3249 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
3250 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
3251 * declare such an globally defined lock. name is a unique identifier
3252 * for the protected varibale or code portion. locking, testing and
3253 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
3254 * G_TRYLOCK() respectively.
3256 extern void glib_dummy_decl (void);
3257 #define G_LOCK_NAME(name) g__ ## name ## _lock
3258 #ifdef G_THREADS_ENABLED
3259 # define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
3260 # define G_LOCK_DEFINE(name) \
3261 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
3262 # define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
3264 # ifdef G_DEBUG_LOCKS
3265 # define G_LOCK(name) G_STMT_START{ \
3266 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3267 "file %s: line %d (%s): locking: %s ", \
3268 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3270 g_static_mutex_lock (&G_LOCK_NAME (name)); \
3272 # define G_UNLOCK(name) G_STMT_START{ \
3273 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3274 "file %s: line %d (%s): unlocking: %s ", \
3275 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3277 g_static_mutex_unlock (&G_LOCK_NAME (name)); \
3279 # define G_TRYLOCK(name) \
3280 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3281 "file %s: line %d (%s): try locking: %s ", \
3282 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3283 #name), g_static_mutex_trylock (&G_LOCK_NAME (name)))
3284 # else /* !G_DEBUG_LOCKS */
3285 # define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
3286 # define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
3287 # define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
3288 # endif /* !G_DEBUG_LOCKS */
3289 #else /* !G_THREADS_ENABLED */
3290 # define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
3291 # define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
3292 # define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
3293 # define G_LOCK(name)
3294 # define G_UNLOCK(name)
3295 # define G_TRYLOCK(name) (TRUE)
3296 #endif /* !G_THREADS_ENABLED */
3298 /* Asyncronous Queues, can be used to communicate between threads
3301 /* Get a new GAsyncQueue with the ref_count 1 */
3302 GAsyncQueue* g_async_queue_new (void);
3304 /* Lock and unlock an GAsyncQueue, all functions lock the queue for
3305 * themselves, but in certain cirumstances you want to hold the lock longer,
3306 * thus you lock the queue, call the *_unlocked functions and unlock it again
3308 void g_async_queue_lock (GAsyncQueue *queue);
3309 void g_async_queue_unlock (GAsyncQueue *queue);
3311 /* Ref and unref the GAsyncQueue. g_async_queue_unref_unlocked makes
3312 * no sense, as after the unreffing the Queue might be gone and can't
3313 * be unlocked. So you have a function to call, if you don't hold the
3314 * lock (g_async_queue_unref) and one to call, when you already hold
3315 * the lock (g_async_queue_unref_and_unlock). After that however, you
3316 * don't hold the lock anymore and the Queue might in fact be
3317 * destroyed, if you unrefed to zero */
3318 void g_async_queue_ref (GAsyncQueue *queue);
3319 void g_async_queue_ref_unlocked (GAsyncQueue *queue);
3320 void g_async_queue_unref (GAsyncQueue *queue);
3321 void g_async_queue_unref_and_unlock (GAsyncQueue *queue);
3323 /* Push data into the async queue. Must not be NULL */
3324 void g_async_queue_push (GAsyncQueue *queue,
3326 void g_async_queue_push_unlocked (GAsyncQueue *queue,
3329 /* Pop data from the async queue, when no data is there, the thread is blocked
3330 * until data arrives */
3331 gpointer g_async_queue_pop (GAsyncQueue *queue);
3332 gpointer g_async_queue_pop_unlocked (GAsyncQueue *queue);
3334 /* Try to pop data, NULL is returned in case of empty queue */
3335 gpointer g_async_queue_try_pop (GAsyncQueue *queue);
3336 gpointer g_async_queue_try_pop_unlocked (GAsyncQueue *queue);
3338 /* Wait for data until at maximum until end_time is reached, NULL is returned
3339 * in case of empty queue*/
3340 gpointer g_async_queue_timed_pop (GAsyncQueue *queue,
3341 GTimeVal *end_time);
3342 gpointer g_async_queue_timed_pop_unlocked (GAsyncQueue *queue,
3343 GTimeVal *end_time);
3345 /* Return the length of the queue, negative values mean, that threads
3346 * are waiting, positve values mean, that there are entries in the
3347 * queue. Actually this function returns the length of the queue minus
3348 * the number of waiting threads, g_async_queue_length == 0 could also
3349 * mean 'n' entries in the queue and 'n' thread waiting, such can
3350 * happen due to locking of the queue or due to scheduling. */
3351 gint g_async_queue_length (GAsyncQueue *queue);
3352 gint g_async_queue_length_unlocked (GAsyncQueue *queue);
3357 /* The real GThreadPool is bigger, so you may only create a thread
3358 * pool with the constructor function */
3364 GThreadPriority priority;
3369 /* Get a thread pool with the function thread_func, at most max_threads may
3370 * run at a time (max_threads == -1 means no limit), stack_size, bound,
3371 * priority like in g_thread_create, exclusive == TRUE means, that the threads
3372 * shouldn't be shared and that they will be prestarted (otherwise they are
3373 * started, as needed) user_data is the 2nd argument to the thread_func */
3374 GThreadPool* g_thread_pool_new (GFunc thread_func,
3378 GThreadPriority priority,
3383 /* Push new data into the thread pool. This task is assigned to a thread later
3384 * (when the maximal number of threads is reached for that pool) or now
3385 * (otherwise). If necessary a new thread will be started. The function
3386 * returns immediatly */
3387 void g_thread_pool_push (GThreadPool *pool,
3391 /* Set the number of threads, which can run concurrently for that pool, -1
3392 * means no limit. 0 means has the effect, that the pool won't process
3393 * requests until the limit is set higher again */
3394 void g_thread_pool_set_max_threads (GThreadPool *pool,
3397 gint g_thread_pool_get_max_threads (GThreadPool *pool);
3399 /* Get the number of threads assigned to that pool. This number doesn't
3400 * necessarily represent the number of working threads in that pool */
3401 guint g_thread_pool_get_num_threads (GThreadPool *pool);
3403 /* Get the number of unprocessed items in the pool */
3404 guint g_thread_pool_unprocessed (GThreadPool *pool);
3406 /* Free the pool, immediate means, that all unprocessed items in the queue
3407 * wont be processed, wait means, that the function doesn't return immediatly,
3408 * but after all threads in the pool are ready processing items. immediate
3409 * does however not mean, that threads are killed. */
3410 void g_thread_pool_free (GThreadPool *pool,
3414 /* Set the maximal number of unused threads before threads will be stopped by
3415 * GLib, -1 means no limit */
3416 void g_thread_pool_set_max_unused_threads (gint max_threads);
3417 gint g_thread_pool_get_max_unused_threads (void);
3418 guint g_thread_pool_get_num_unused_threads (void);
3420 /* Stop all currently unused threads, but leave the limit untouched */
3421 void g_thread_pool_stop_unused_threads (void);
3425 #endif /* __cplusplus */
3427 #include <gunicode.h>
3429 #endif /* __G_LIB_H__ */