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 #define G_GSHORT_FORMAT "hi"
548 #define G_GUSHORT_FORMAT "hu"
549 #define G_GINT_FORMAT "i"
550 #define G_GUINT_FORMAT "u"
551 #define G_GLONG_FORMAT "li"
552 #define G_GULONG_FORMAT "lu"
554 typedef float gfloat;
555 typedef double gdouble;
557 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
558 * Since gldouble isn't used anywhere, just disable it for now */
561 #ifdef HAVE_LONG_DOUBLE
562 typedef long double gldouble;
563 #else /* HAVE_LONG_DOUBLE */
564 typedef double gldouble;
565 #endif /* HAVE_LONG_DOUBLE */
568 typedef void* gpointer;
569 typedef const void *gconstpointer;
572 typedef guint32 GQuark;
573 typedef gint32 GTime;
576 /* Portable endian checks and conversions
578 * glibconfig.h defines G_BYTE_ORDER which expands to one of
581 #define G_LITTLE_ENDIAN 1234
582 #define G_BIG_ENDIAN 4321
583 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
586 /* Basic bit swapping functions
588 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
589 (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
590 (((guint16) (val) & (guint16) 0xff00U) >> 8)))
591 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((guint32) ( \
592 (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
593 (((guint32) (val) & (guint32) 0x0000ff00U) << 8) | \
594 (((guint32) (val) & (guint32) 0x00ff0000U) >> 8) | \
595 (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
597 /* Intel specific stuff for speed
599 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
600 # define GUINT16_SWAP_LE_BE_X86(val) \
602 ({ register guint16 __v; \
603 if (__builtin_constant_p (val)) \
604 __v = GUINT16_SWAP_LE_BE_CONSTANT (val); \
606 __asm__ __const__ ("rorw $8, %w0" \
608 : "0" ((guint16) (val))); \
610 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
611 # if !defined(__i486__) && !defined(__i586__) \
612 && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
613 # define GUINT32_SWAP_LE_BE_X86(val) \
615 ({ register guint32 __v; \
616 if (__builtin_constant_p (val)) \
617 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
619 __asm__ __const__ ("rorw $8, %w0\n\t" \
623 : "0" ((guint32) (val))); \
625 # else /* 486 and higher has bswap */
626 # define GUINT32_SWAP_LE_BE_X86(val) \
628 ({ register guint32 __v; \
629 if (__builtin_constant_p (val)) \
630 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
632 __asm__ __const__ ("bswap %0" \
634 : "0" ((guint32) (val))); \
636 # endif /* processor specific 32-bit stuff */
637 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
638 #else /* !__i386__ */
639 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
640 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
641 #endif /* __i386__ */
644 # define GUINT64_SWAP_LE_BE_CONSTANT(val) ((guint64) ( \
645 (((guint64) (val) & \
646 (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) | \
647 (((guint64) (val) & \
648 (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) | \
649 (((guint64) (val) & \
650 (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) | \
651 (((guint64) (val) & \
652 (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) << 8) | \
653 (((guint64) (val) & \
654 (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >> 8) | \
655 (((guint64) (val) & \
656 (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) | \
657 (((guint64) (val) & \
658 (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) | \
659 (((guint64) (val) & \
660 (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
661 # if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
662 # define GUINT64_SWAP_LE_BE_X86(val) \
664 ({ union { guint64 __ll; \
665 guint32 __l[2]; } __r; \
666 if (__builtin_constant_p (val)) \
667 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val); \
670 union { guint64 __ll; \
671 guint32 __l[2]; } __w; \
672 __w.__ll = ((guint64) val); \
673 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
674 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
677 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
678 # else /* !__i386__ */
679 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
683 #define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
684 #define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
685 #define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
686 (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
687 (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
688 #define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
689 (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
690 (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
692 /* The G*_TO_?E() macros are defined in glibconfig.h.
693 * The transformation is symmetric, so the FROM just maps to the TO.
695 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
696 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
697 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
698 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
699 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
700 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
701 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
702 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
705 #define GINT64_FROM_LE(val) (GINT64_TO_LE (val))
706 #define GUINT64_FROM_LE(val) (GUINT64_TO_LE (val))
707 #define GINT64_FROM_BE(val) (GINT64_TO_BE (val))
708 #define GUINT64_FROM_BE(val) (GUINT64_TO_BE (val))
711 #define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
712 #define GULONG_FROM_LE(val) (GULONG_TO_LE (val))
713 #define GLONG_FROM_BE(val) (GLONG_TO_BE (val))
714 #define GULONG_FROM_BE(val) (GULONG_TO_BE (val))
716 #define GINT_FROM_LE(val) (GINT_TO_LE (val))
717 #define GUINT_FROM_LE(val) (GUINT_TO_LE (val))
718 #define GINT_FROM_BE(val) (GINT_TO_BE (val))
719 #define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
722 /* Portable versions of host-network order stuff
724 #define g_ntohl(val) (GUINT32_FROM_BE (val))
725 #define g_ntohs(val) (GUINT16_FROM_BE (val))
726 #define g_htonl(val) (GUINT32_TO_BE (val))
727 #define g_htons(val) (GUINT16_TO_BE (val))
731 * we prefix variable declarations so they can
732 * properly get exported in windows dlls.
735 # ifdef GLIB_COMPILATION
736 # define GLIB_VAR __declspec(dllexport)
737 # else /* !GLIB_COMPILATION */
738 # define GLIB_VAR extern __declspec(dllimport)
739 # endif /* !GLIB_COMPILATION */
740 #else /* !G_OS_WIN32 */
741 # define GLIB_VAR extern
742 #endif /* !G_OS_WIN32 */
744 GLIB_VAR const guint glib_major_version;
745 GLIB_VAR const guint glib_minor_version;
746 GLIB_VAR const guint glib_micro_version;
747 GLIB_VAR const guint glib_interface_age;
748 GLIB_VAR const guint glib_binary_age;
750 #define GLIB_CHECK_VERSION(major,minor,micro) \
751 (GLIB_MAJOR_VERSION > (major) || \
752 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
753 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
754 GLIB_MICRO_VERSION >= (micro)))
756 /* Forward declarations of glib types.
758 typedef struct _GAllocator GAllocator;
759 typedef struct _GArray GArray;
760 typedef struct _GByteArray GByteArray;
761 typedef struct _GCache GCache;
762 typedef struct _GCompletion GCompletion;
763 typedef struct _GData GData;
764 typedef struct _GDebugKey GDebugKey;
765 typedef union _GDoubleIEEE754 GDoubleIEEE754;
766 typedef union _GFloatIEEE754 GFloatIEEE754;
767 typedef struct _GHashTable GHashTable;
768 typedef struct _GHook GHook;
769 typedef struct _GHookList GHookList;
770 typedef struct _GList GList;
771 typedef struct _GMemChunk GMemChunk;
772 typedef struct _GNode GNode;
773 typedef struct _GPtrArray GPtrArray;
774 typedef struct _GQueue GQueue;
775 typedef struct _GRand GRand;
776 typedef struct _GRelation GRelation;
777 typedef struct _GScanner GScanner;
778 typedef struct _GScannerConfig GScannerConfig;
779 typedef struct _GSList GSList;
780 typedef struct _GString GString;
781 typedef struct _GStringChunk GStringChunk;
782 typedef struct _GTimer GTimer;
783 typedef struct _GTrashStack GTrashStack;
784 typedef struct _GTree GTree;
785 typedef struct _GTuples GTuples;
786 typedef union _GTokenValue GTokenValue;
787 typedef struct _GIOChannel GIOChannel;
789 /* Tree traverse flags */
792 G_TRAVERSE_LEAFS = 1 << 0,
793 G_TRAVERSE_NON_LEAFS = 1 << 1,
794 G_TRAVERSE_ALL = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
795 G_TRAVERSE_MASK = 0x03
798 /* Tree traverse orders */
807 /* Log level shift offset for user defined
808 * log levels (0-7 are used by GLib).
810 #define G_LOG_LEVEL_USER_SHIFT (8)
812 /* Glib log levels and flags.
817 G_LOG_FLAG_RECURSION = 1 << 0,
818 G_LOG_FLAG_FATAL = 1 << 1,
820 /* GLib log levels */
821 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
822 G_LOG_LEVEL_CRITICAL = 1 << 3,
823 G_LOG_LEVEL_WARNING = 1 << 4,
824 G_LOG_LEVEL_MESSAGE = 1 << 5,
825 G_LOG_LEVEL_INFO = 1 << 6,
826 G_LOG_LEVEL_DEBUG = 1 << 7,
828 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
831 /* GLib log levels that are considered fatal by default */
832 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
835 typedef gpointer (*GCacheNewFunc) (gpointer key);
836 typedef gpointer (*GCacheDupFunc) (gpointer value);
837 typedef void (*GCacheDestroyFunc) (gpointer value);
838 typedef gint (*GCompareFunc) (gconstpointer a,
840 typedef gchar* (*GCompletionFunc) (gpointer);
841 typedef void (*GDestroyNotify) (gpointer data);
842 typedef void (*GDataForeachFunc) (GQuark key_id,
845 typedef void (*GFunc) (gpointer data,
847 typedef guint (*GHashFunc) (gconstpointer key);
848 typedef void (*GFreeFunc) (gpointer data);
849 typedef void (*GHFunc) (gpointer key,
852 typedef gboolean (*GHRFunc) (gpointer key,
855 typedef gint (*GHookCompareFunc) (GHook *new_hook,
857 typedef gboolean (*GHookFindFunc) (GHook *hook,
859 typedef void (*GHookMarshaller) (GHook *hook,
861 typedef gboolean (*GHookCheckMarshaller) (GHook *hook,
863 typedef void (*GHookFunc) (gpointer data);
864 typedef gboolean (*GHookCheckFunc) (gpointer data);
865 typedef void (*GHookFreeFunc) (GHookList *hook_list,
867 typedef void (*GLogFunc) (const gchar *log_domain,
868 GLogLevelFlags log_level,
869 const gchar *message,
871 typedef gboolean (*GNodeTraverseFunc) (GNode *node,
873 typedef void (*GNodeForeachFunc) (GNode *node,
875 typedef void (*GScannerMsgFunc) (GScanner *scanner,
878 typedef gint (*GTraverseFunc) (gpointer key,
881 typedef void (*GVoidFunc) (void);
945 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
948 * +--------+---------------+---------------+
949 * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
950 * +--------+---------------+---------------+
951 * B0------------------->B1------->B2-->B3-->
953 * IEEE Standard 754 Double Precision Storage Format (gdouble):
955 * 63 62 52 51 32 31 0
956 * +--------+----------------+----------------+ +---------------+
957 * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
958 * +--------+----------------+----------------+ +---------------+
959 * B0--------------->B1---------->B2--->B3----> B4->B5->B6->B7->
961 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
962 #define G_IEEE754_FLOAT_BIAS (127)
963 #define G_IEEE754_DOUBLE_BIAS (1023)
964 /* multiply with base2 exponent to get base10 exponent (nomal numbers) */
965 #define G_LOG_2_BASE_10 (0.30102999566398119521)
966 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
972 guint biased_exponent : 8;
976 union _GDoubleIEEE754
980 guint mantissa_low : 32;
981 guint mantissa_high : 20;
982 guint biased_exponent : 11;
986 #elif G_BYTE_ORDER == G_BIG_ENDIAN
992 guint biased_exponent : 8;
996 union _GDoubleIEEE754
1001 guint biased_exponent : 11;
1002 guint mantissa_high : 20;
1003 guint mantissa_low : 32;
1006 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
1007 #error unknown ENDIAN type
1008 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
1011 /* Doubly linked lists
1013 void g_list_push_allocator (GAllocator *allocator);
1014 void g_list_pop_allocator (void);
1015 GList* g_list_alloc (void);
1016 void g_list_free (GList *list);
1017 void g_list_free_1 (GList *list);
1018 GList* g_list_append (GList *list,
1020 GList* g_list_prepend (GList *list,
1022 GList* g_list_insert (GList *list,
1025 GList* g_list_insert_sorted (GList *list,
1028 GList* g_list_concat (GList *list1,
1030 GList* g_list_remove (GList *list,
1031 gconstpointer data);
1032 GList* g_list_remove_link (GList *list,
1034 GList* g_list_delete_link (GList *list,
1036 GList* g_list_reverse (GList *list);
1037 GList* g_list_copy (GList *list);
1038 GList* g_list_nth (GList *list,
1040 GList* g_list_find (GList *list,
1041 gconstpointer data);
1042 GList* g_list_find_custom (GList *list,
1045 gint g_list_position (GList *list,
1047 gint g_list_index (GList *list,
1048 gconstpointer data);
1049 GList* g_list_last (GList *list);
1050 GList* g_list_first (GList *list);
1051 guint g_list_length (GList *list);
1052 void g_list_foreach (GList *list,
1054 gpointer user_data);
1055 GList* g_list_sort (GList *list,
1056 GCompareFunc compare_func);
1057 gpointer g_list_nth_data (GList *list,
1059 #define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
1060 #define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
1063 /* Singly linked lists
1065 void g_slist_push_allocator (GAllocator *allocator);
1066 void g_slist_pop_allocator (void);
1067 GSList* g_slist_alloc (void);
1068 void g_slist_free (GSList *list);
1069 void g_slist_free_1 (GSList *list);
1070 GSList* g_slist_append (GSList *list,
1072 GSList* g_slist_prepend (GSList *list,
1074 GSList* g_slist_insert (GSList *list,
1077 GSList* g_slist_insert_sorted (GSList *list,
1080 GSList* g_slist_insert_before (GSList *slist,
1083 GSList* g_slist_concat (GSList *list1,
1085 GSList* g_slist_remove (GSList *list,
1086 gconstpointer data);
1087 GSList* g_slist_remove_link (GSList *list,
1089 GSList* g_slist_delete_link (GSList *list,
1091 GSList* g_slist_reverse (GSList *list);
1092 GSList* g_slist_copy (GSList *list);
1093 GSList* g_slist_nth (GSList *list,
1095 GSList* g_slist_find (GSList *list,
1096 gconstpointer data);
1097 GSList* g_slist_find_custom (GSList *list,
1100 gint g_slist_position (GSList *list,
1102 gint g_slist_index (GSList *list,
1103 gconstpointer data);
1104 GSList* g_slist_last (GSList *list);
1105 guint g_slist_length (GSList *list);
1106 void g_slist_foreach (GSList *list,
1108 gpointer user_data);
1109 GSList* g_slist_sort (GSList *list,
1110 GCompareFunc compare_func);
1111 gpointer g_slist_nth_data (GSList *list,
1113 #define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
1118 GQueue* g_queue_new (void);
1119 void g_queue_free (GQueue *queue);
1120 void g_queue_push_head (GQueue *queue,
1122 void g_queue_push_tail (GQueue *queue,
1124 gpointer g_queue_pop_head (GQueue *queue);
1125 gpointer g_queue_pop_tail (GQueue *queue);
1126 gboolean g_queue_is_empty (GQueue *queue);
1127 gpointer g_queue_peek_head (GQueue *queue);
1128 gpointer g_queue_peek_tail (GQueue *queue);
1129 void g_queue_push_head_link (GQueue *queue,
1131 void g_queue_push_tail_link (GQueue *queue,
1133 GList* g_queue_pop_head_link (GQueue *queue);
1134 GList* g_queue_pop_tail_link (GQueue *queue);
1138 GHashTable* g_hash_table_new (GHashFunc hash_func,
1139 GCompareFunc key_compare_func);
1140 void g_hash_table_destroy (GHashTable *hash_table);
1141 void g_hash_table_insert (GHashTable *hash_table,
1144 void g_hash_table_remove (GHashTable *hash_table,
1146 gpointer g_hash_table_lookup (GHashTable *hash_table,
1148 gboolean g_hash_table_lookup_extended(GHashTable *hash_table,
1149 gconstpointer lookup_key,
1152 void g_hash_table_foreach (GHashTable *hash_table,
1154 gpointer user_data);
1155 guint g_hash_table_foreach_remove (GHashTable *hash_table,
1157 gpointer user_data);
1158 guint g_hash_table_size (GHashTable *hash_table);
1160 /* The following two functions are deprecated and will be removed in
1161 * the next major release. They do no good. */
1162 void g_hash_table_freeze (GHashTable *hash_table);
1163 void g_hash_table_thaw (GHashTable *hash_table);
1167 GCache* g_cache_new (GCacheNewFunc value_new_func,
1168 GCacheDestroyFunc value_destroy_func,
1169 GCacheDupFunc key_dup_func,
1170 GCacheDestroyFunc key_destroy_func,
1171 GHashFunc hash_key_func,
1172 GHashFunc hash_value_func,
1173 GCompareFunc key_compare_func);
1174 void g_cache_destroy (GCache *cache);
1175 gpointer g_cache_insert (GCache *cache,
1177 void g_cache_remove (GCache *cache,
1178 gconstpointer value);
1179 void g_cache_key_foreach (GCache *cache,
1181 gpointer user_data);
1182 void g_cache_value_foreach (GCache *cache,
1184 gpointer user_data);
1187 /* Balanced binary trees
1189 GTree* g_tree_new (GCompareFunc key_compare_func);
1190 void g_tree_destroy (GTree *tree);
1191 void g_tree_insert (GTree *tree,
1194 void g_tree_remove (GTree *tree,
1196 gpointer g_tree_lookup (GTree *tree,
1198 void g_tree_traverse (GTree *tree,
1199 GTraverseFunc traverse_func,
1200 GTraverseType traverse_type,
1202 gpointer g_tree_search (GTree *tree,
1203 GCompareFunc search_func,
1204 gconstpointer data);
1205 gint g_tree_height (GTree *tree);
1206 gint g_tree_nnodes (GTree *tree);
1210 /* N-way tree implementation
1221 #define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
1222 ((GNode*) (node))->prev == NULL && \
1223 ((GNode*) (node))->next == NULL)
1224 #define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
1226 void g_node_push_allocator (GAllocator *allocator);
1227 void g_node_pop_allocator (void);
1228 GNode* g_node_new (gpointer data);
1229 void g_node_destroy (GNode *root);
1230 void g_node_unlink (GNode *node);
1231 GNode* g_node_copy (GNode *node);
1232 GNode* g_node_insert (GNode *parent,
1235 GNode* g_node_insert_before (GNode *parent,
1238 GNode* g_node_prepend (GNode *parent,
1240 guint g_node_n_nodes (GNode *root,
1241 GTraverseFlags flags);
1242 GNode* g_node_get_root (GNode *node);
1243 gboolean g_node_is_ancestor (GNode *node,
1245 guint g_node_depth (GNode *node);
1246 GNode* g_node_find (GNode *root,
1247 GTraverseType order,
1248 GTraverseFlags flags,
1251 /* convenience macros */
1252 #define g_node_append(parent, node) \
1253 g_node_insert_before ((parent), NULL, (node))
1254 #define g_node_insert_data(parent, position, data) \
1255 g_node_insert ((parent), (position), g_node_new (data))
1256 #define g_node_insert_data_before(parent, sibling, data) \
1257 g_node_insert_before ((parent), (sibling), g_node_new (data))
1258 #define g_node_prepend_data(parent, data) \
1259 g_node_prepend ((parent), g_node_new (data))
1260 #define g_node_append_data(parent, data) \
1261 g_node_insert_before ((parent), NULL, g_node_new (data))
1263 /* traversal function, assumes that `node' is root
1264 * (only traverses `node' and its subtree).
1265 * this function is just a high level interface to
1266 * low level traversal functions, optimized for speed.
1268 void g_node_traverse (GNode *root,
1269 GTraverseType order,
1270 GTraverseFlags flags,
1272 GNodeTraverseFunc func,
1275 /* return the maximum tree height starting with `node', this is an expensive
1276 * operation, since we need to visit all nodes. this could be shortened by
1277 * adding `guint height' to struct _GNode, but then again, this is not very
1278 * often needed, and would make g_node_insert() more time consuming.
1280 guint g_node_max_height (GNode *root);
1282 void g_node_children_foreach (GNode *node,
1283 GTraverseFlags flags,
1284 GNodeForeachFunc func,
1286 void g_node_reverse_children (GNode *node);
1287 guint g_node_n_children (GNode *node);
1288 GNode* g_node_nth_child (GNode *node,
1290 GNode* g_node_last_child (GNode *node);
1291 GNode* g_node_find_child (GNode *node,
1292 GTraverseFlags flags,
1294 gint g_node_child_position (GNode *node,
1296 gint g_node_child_index (GNode *node,
1299 GNode* g_node_first_sibling (GNode *node);
1300 GNode* g_node_last_sibling (GNode *node);
1302 #define g_node_prev_sibling(node) ((node) ? \
1303 ((GNode*) (node))->prev : NULL)
1304 #define g_node_next_sibling(node) ((node) ? \
1305 ((GNode*) (node))->next : NULL)
1306 #define g_node_first_child(node) ((node) ? \
1307 ((GNode*) (node))->children : NULL)
1310 /* Callback maintenance functions
1312 #define G_HOOK_FLAG_USER_SHIFT (4)
1315 G_HOOK_FLAG_ACTIVE = 1 << 0,
1316 G_HOOK_FLAG_IN_CALL = 1 << 1,
1317 G_HOOK_FLAG_MASK = 0x0f
1320 #define G_HOOK_DEFERRED_DESTROY ((GHookFreeFunc) 0x01)
1328 GMemChunk *hook_memchunk;
1329 GHookFreeFunc hook_free; /* virtual function */
1330 GHookFreeFunc hook_destroy; /* virtual function */
1342 GDestroyNotify destroy;
1345 #define G_HOOK_ACTIVE(hook) ((((GHook*) hook)->flags & \
1346 G_HOOK_FLAG_ACTIVE) != 0)
1347 #define G_HOOK_IN_CALL(hook) ((((GHook*) hook)->flags & \
1348 G_HOOK_FLAG_IN_CALL) != 0)
1349 #define G_HOOK_IS_VALID(hook) (((GHook*) hook)->hook_id != 0 && \
1350 G_HOOK_ACTIVE (hook))
1351 #define G_HOOK_IS_UNLINKED(hook) (((GHook*) hook)->next == NULL && \
1352 ((GHook*) hook)->prev == NULL && \
1353 ((GHook*) hook)->hook_id == 0 && \
1354 ((GHook*) hook)->ref_count == 0)
1356 void g_hook_list_init (GHookList *hook_list,
1358 void g_hook_list_clear (GHookList *hook_list);
1359 GHook* g_hook_alloc (GHookList *hook_list);
1360 void g_hook_free (GHookList *hook_list,
1362 void g_hook_ref (GHookList *hook_list,
1364 void g_hook_unref (GHookList *hook_list,
1366 gboolean g_hook_destroy (GHookList *hook_list,
1368 void g_hook_destroy_link (GHookList *hook_list,
1370 void g_hook_prepend (GHookList *hook_list,
1372 void g_hook_insert_before (GHookList *hook_list,
1375 void g_hook_insert_sorted (GHookList *hook_list,
1377 GHookCompareFunc func);
1378 GHook* g_hook_get (GHookList *hook_list,
1380 GHook* g_hook_find (GHookList *hook_list,
1381 gboolean need_valids,
1384 GHook* g_hook_find_data (GHookList *hook_list,
1385 gboolean need_valids,
1387 GHook* g_hook_find_func (GHookList *hook_list,
1388 gboolean need_valids,
1390 GHook* g_hook_find_func_data (GHookList *hook_list,
1391 gboolean need_valids,
1394 /* return the first valid hook, and increment its reference count */
1395 GHook* g_hook_first_valid (GHookList *hook_list,
1396 gboolean may_be_in_call);
1397 /* return the next valid hook with incremented reference count, and
1398 * decrement the reference count of the original hook
1400 GHook* g_hook_next_valid (GHookList *hook_list,
1402 gboolean may_be_in_call);
1404 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1405 gint g_hook_compare_ids (GHook *new_hook,
1408 /* convenience macros */
1409 #define g_hook_append( hook_list, hook ) \
1410 g_hook_insert_before ((hook_list), NULL, (hook))
1412 /* invoke all valid hooks with the (*GHookFunc) signature.
1414 void g_hook_list_invoke (GHookList *hook_list,
1415 gboolean may_recurse);
1416 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1417 * and destroy the hook if FALSE is returned.
1419 void g_hook_list_invoke_check (GHookList *hook_list,
1420 gboolean may_recurse);
1421 /* invoke a marshaller on all valid hooks.
1423 void g_hook_list_marshal (GHookList *hook_list,
1424 gboolean may_recurse,
1425 GHookMarshaller marshaller,
1427 void g_hook_list_marshal_check (GHookList *hook_list,
1428 gboolean may_recurse,
1429 GHookCheckMarshaller marshaller,
1433 /* Fatal error handlers.
1434 * g_on_error_query() will prompt the user to either
1435 * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1436 * g_on_error_stack_trace() invokes gdb, which attaches to the current
1437 * process and shows a stack trace.
1438 * These function may cause different actions on non-unix platforms.
1439 * The prg_name arg is required by gdb to find the executable, if it is
1440 * passed as NULL, g_on_error_query() will try g_get_prgname().
1442 void g_on_error_query (const gchar *prg_name);
1443 void g_on_error_stack_trace (const gchar *prg_name);
1446 /* Logging mechanism
1448 extern const gchar *g_log_domain_glib;
1449 guint g_log_set_handler (const gchar *log_domain,
1450 GLogLevelFlags log_levels,
1452 gpointer user_data);
1453 void g_log_remove_handler (const gchar *log_domain,
1455 void g_log_default_handler (const gchar *log_domain,
1456 GLogLevelFlags log_level,
1457 const gchar *message,
1458 gpointer unused_data);
1459 void g_log (const gchar *log_domain,
1460 GLogLevelFlags log_level,
1461 const gchar *format,
1462 ...) G_GNUC_PRINTF (3, 4);
1463 void g_logv (const gchar *log_domain,
1464 GLogLevelFlags log_level,
1465 const gchar *format,
1467 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
1468 GLogLevelFlags fatal_mask);
1469 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
1470 #ifndef G_LOG_DOMAIN
1471 #define G_LOG_DOMAIN ((gchar*) 0)
1472 #endif /* G_LOG_DOMAIN */
1473 #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
1474 #define g_error(...) g_log (G_LOG_DOMAIN, \
1475 G_LOG_LEVEL_ERROR, \
1477 #define g_message(...) g_log (G_LOG_DOMAIN, \
1478 G_LOG_LEVEL_MESSAGE, \
1480 #define g_critical(...) g_log (G_LOG_DOMAIN, \
1481 G_LOG_LEVEL_CRITICAL, \
1483 #define g_warning(...) g_log (G_LOG_DOMAIN, \
1484 G_LOG_LEVEL_WARNING, \
1486 #elif defined (__GNUC__)
1487 #define g_error(format...) g_log (G_LOG_DOMAIN, \
1488 G_LOG_LEVEL_ERROR, \
1490 #define g_message(format...) g_log (G_LOG_DOMAIN, \
1491 G_LOG_LEVEL_MESSAGE, \
1493 #define g_critical(format...) g_log (G_LOG_DOMAIN, \
1494 G_LOG_LEVEL_CRITICAL, \
1496 #define g_warning(format...) g_log (G_LOG_DOMAIN, \
1497 G_LOG_LEVEL_WARNING, \
1499 #else /* !__GNUC__ */
1501 g_error (const gchar *format,
1505 va_start (args, format);
1506 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1510 g_message (const gchar *format,
1514 va_start (args, format);
1515 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1519 g_critical (const gchar *format,
1523 va_start (args, format);
1524 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
1528 g_warning (const gchar *format,
1532 va_start (args, format);
1533 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1536 #endif /* !__GNUC__ */
1538 typedef void (*GPrintFunc) (const gchar *string);
1539 void g_print (const gchar *format,
1540 ...) G_GNUC_PRINTF (1, 2);
1541 GPrintFunc g_set_print_handler (GPrintFunc func);
1542 void g_printerr (const gchar *format,
1543 ...) G_GNUC_PRINTF (1, 2);
1544 GPrintFunc g_set_printerr_handler (GPrintFunc func);
1546 /* deprecated compatibility functions, use g_log_set_handler() instead */
1547 typedef void (*GErrorFunc) (const gchar *str);
1548 typedef void (*GWarningFunc) (const gchar *str);
1549 GErrorFunc g_set_error_handler (GErrorFunc func);
1550 GWarningFunc g_set_warning_handler (GWarningFunc func);
1551 GPrintFunc g_set_message_handler (GPrintFunc func);
1554 /* Memory allocation and debugging
1558 #define g_malloc(size) ((gpointer) MALLOC (size))
1559 #define g_malloc0(size) ((gpointer) CALLOC (char, size))
1560 #define g_realloc(mem,size) ((gpointer) REALLOC (mem, char, size))
1561 #define g_free(mem) FREE (mem)
1563 #else /* !USE_DMALLOC */
1565 gpointer g_malloc (gulong size);
1566 gpointer g_malloc0 (gulong size);
1567 gpointer g_realloc (gpointer mem,
1569 void g_free (gpointer mem);
1571 #endif /* !USE_DMALLOC */
1573 void g_mem_profile (void);
1574 void g_mem_check (gpointer mem);
1576 /* Generic allocators
1578 GAllocator* g_allocator_new (const gchar *name,
1580 void g_allocator_free (GAllocator *allocator);
1582 #define G_ALLOCATOR_LIST (1)
1583 #define G_ALLOCATOR_SLIST (2)
1584 #define G_ALLOCATOR_NODE (3)
1587 /* "g_mem_chunk_new" creates a new memory chunk.
1588 * Memory chunks are used to allocate pieces of memory which are
1589 * always the same size. Lists are a good example of such a data type.
1590 * The memory chunk allocates and frees blocks of memory as needed.
1591 * Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1592 * allocated in a mem chunk. ("g_free" will most likely cause a seg
1593 * fault...somewhere).
1595 * Oh yeah, GMemChunk is an opaque data type. (You don't really
1596 * want to know what's going on inside do you?)
1599 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1600 * is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1601 * atom. (They are also useful for lists which use MemChunk to allocate
1602 * memory but are also part of the MemChunk implementation).
1603 * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1606 #define G_ALLOC_ONLY 1
1607 #define G_ALLOC_AND_FREE 2
1609 GMemChunk* g_mem_chunk_new (gchar *name,
1613 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
1614 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
1615 gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
1616 void g_mem_chunk_free (GMemChunk *mem_chunk,
1618 void g_mem_chunk_clean (GMemChunk *mem_chunk);
1619 void g_mem_chunk_reset (GMemChunk *mem_chunk);
1620 void g_mem_chunk_print (GMemChunk *mem_chunk);
1621 void g_mem_chunk_info (void);
1623 /* Ah yes...we have a "g_blow_chunks" function.
1624 * "g_blow_chunks" simply compresses all the chunks. This operation
1625 * consists of freeing every memory area that should be freed (but
1626 * which we haven't gotten around to doing yet). And, no,
1627 * "g_blow_chunks" doesn't follow the naming scheme, but it is a
1628 * much better name than "g_mem_chunk_clean_all" or something
1631 void g_blow_chunks (void);
1637 #define G_MICROSEC 1000000
1639 GTimer* g_timer_new (void);
1640 void g_timer_destroy (GTimer *timer);
1641 void g_timer_start (GTimer *timer);
1642 void g_timer_stop (GTimer *timer);
1643 void g_timer_reset (GTimer *timer);
1644 gdouble g_timer_elapsed (GTimer *timer,
1645 gulong *microseconds);
1646 void g_usleep (gulong microseconds);
1648 /* String utility functions that modify a string argument or
1649 * return a constant string that must not be freed.
1651 #define G_STR_DELIMITERS "_-|> <."
1652 gchar* g_strdelimit (gchar *string,
1653 const gchar *delimiters,
1654 gchar new_delimiter);
1655 gchar* g_strcanon (gchar *string,
1656 const gchar *valid_chars,
1658 gdouble g_strtod (const gchar *nptr,
1660 gchar* g_strerror (gint errnum);
1661 gchar* g_strsignal (gint signum);
1662 gint g_strcasecmp (const gchar *s1,
1664 gint g_strncasecmp (const gchar *s1,
1667 gchar* g_strdown (gchar *string);
1668 gchar* g_strup (gchar *string);
1669 gchar* g_strreverse (gchar *string);
1670 gsize g_strlcpy (gchar *dest,
1673 gsize g_strlcat (gchar *dest,
1676 /* removes leading spaces */
1677 gchar* g_strchug (gchar *string);
1678 /* removes trailing spaces */
1679 gchar* g_strchomp (gchar *string);
1680 /* removes leading & trailing spaces */
1681 #define g_strstrip( string ) g_strchomp (g_strchug (string))
1683 /* String utility functions that return a newly allocated string which
1684 * ought to be freed with g_free from the caller at some point.
1686 gchar* g_strdup (const gchar *str);
1687 gchar* g_strdup_printf (const gchar *format,
1688 ...) G_GNUC_PRINTF (1, 2);
1689 gchar* g_strdup_vprintf (const gchar *format,
1691 gchar* g_strndup (const gchar *str,
1693 gchar* g_strnfill (guint length,
1695 gchar* g_strconcat (const gchar *string1,
1696 ...); /* NULL terminated */
1697 gchar* g_strjoin (const gchar *separator,
1698 ...); /* NULL terminated */
1699 /* Make a copy of a string interpreting C string -style escape
1700 * sequences. Inverse of g_strescape. The recognized sequences are \b
1701 * \f \n \r \t \\ \" and the octal format.
1703 gchar* g_strcompress (const gchar *source);
1705 /* Convert between the operating system (or C runtime)
1706 * representation of file names and UTF-8.
1708 gchar* g_filename_to_utf8 (const gchar *opsysstring);
1709 gchar* g_filename_from_utf8 (const gchar *utf8string);
1711 /* Copy a string escaping nonprintable characters like in C strings.
1712 * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
1713 * to a string containing characters that are not to be escaped.
1715 * Deprecated API: gchar* g_strescape (const gchar *source);
1716 * Luckily this function wasn't used much, using NULL as second parameter
1717 * provides mostly identical semantics.
1719 gchar* g_strescape (const gchar *source,
1720 const gchar *exceptions);
1722 gpointer g_memdup (gconstpointer mem,
1725 /* NULL terminated string arrays.
1726 * g_strsplit() splits up string into max_tokens tokens at delim and
1727 * returns a newly allocated string array.
1728 * g_strjoinv() concatenates all of str_array's strings, sliding in an
1729 * optional separator, the returned string is newly allocated.
1730 * g_strfreev() frees the array itself and all of its strings.
1732 gchar** g_strsplit (const gchar *string,
1733 const gchar *delimiter,
1735 gchar* g_strjoinv (const gchar *separator,
1737 void g_strfreev (gchar **str_array);
1741 /* calculate a string size, guarranteed to fit format + args.
1743 guint g_printf_string_upper_bound (const gchar* format,
1747 /* Retrive static string info
1749 gchar* g_get_user_name (void);
1750 gchar* g_get_real_name (void);
1751 gchar* g_get_home_dir (void);
1752 gchar* g_get_tmp_dir (void);
1753 gchar* g_get_prgname (void);
1754 void g_set_prgname (const gchar *prgname);
1757 /* Miscellaneous utility functions
1759 guint g_parse_debug_string (const gchar *string,
1762 gint g_snprintf (gchar *string,
1764 gchar const *format,
1765 ...) G_GNUC_PRINTF (3, 4);
1766 gint g_vsnprintf (gchar *string,
1768 gchar const *format,
1770 /* Check if a file name is an absolute path */
1771 gboolean g_path_is_absolute (const gchar *file_name);
1772 /* In case of absolute paths, skip the root part */
1773 gchar* g_path_skip_root (gchar *file_name);
1775 /* These two functions are deprecated and will be removed in the next
1776 * major release of GLib. Use g_path_get_dirname/g_path_get_basename
1777 * instead. Whatch out! The string returned by g_path_get_basename
1778 * must be g_freed, while the string returned by g_basename must not.*/
1779 gchar* g_basename (const gchar *file_name);
1780 gchar* g_dirname (const gchar *file_name);
1782 /* The returned strings are newly allocated with g_malloc() */
1783 gchar* g_get_current_dir (void);
1784 gchar* g_path_get_basename (const gchar *file_name);
1785 gchar* g_path_get_dirname (const gchar *file_name);
1787 /* Get the codeset for the current locale */
1788 /* gchar * g_get_codeset (void); */
1790 /* return the environment string for the variable. The returned memory
1791 * must not be freed. */
1792 gchar* g_getenv (const gchar *variable);
1794 /* we use a GLib function as a replacement for ATEXIT, so
1795 * the programmer is not required to check the return value
1796 * (if there is any in the implementation) and doesn't encounter
1797 * missing include files.
1799 void g_atexit (GVoidFunc func);
1804 G_INLINE_FUNC gint g_bit_nth_lsf (guint32 mask,
1808 g_bit_nth_lsf (guint32 mask,
1814 if (mask & (1 << (guint) nth_bit))
1817 while (nth_bit < 32);
1820 #endif /* G_CAN_INLINE */
1822 G_INLINE_FUNC gint g_bit_nth_msf (guint32 mask,
1826 g_bit_nth_msf (guint32 mask,
1834 if (mask & (1 << (guint) nth_bit))
1837 while (nth_bit > 0);
1840 #endif /* G_CAN_INLINE */
1842 G_INLINE_FUNC guint g_bit_storage (guint number);
1845 g_bit_storage (guint number)
1847 register guint n_bits = 0;
1857 #endif /* G_CAN_INLINE */
1861 * elements need to be >= sizeof (gpointer)
1863 G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p,
1867 g_trash_stack_push (GTrashStack **stack_p,
1870 GTrashStack *data = (GTrashStack *) data_p;
1872 data->next = *stack_p;
1875 #endif /* G_CAN_INLINE */
1877 G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p);
1879 G_INLINE_FUNC gpointer
1880 g_trash_stack_pop (GTrashStack **stack_p)
1887 *stack_p = data->next;
1888 /* NULLify private pointer here, most platforms store NULL as
1889 * subsequent 0 bytes
1896 #endif /* G_CAN_INLINE */
1898 G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p);
1900 G_INLINE_FUNC gpointer
1901 g_trash_stack_peek (GTrashStack **stack_p)
1909 #endif /* G_CAN_INLINE */
1911 G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p);
1914 g_trash_stack_height (GTrashStack **stack_p)
1919 for (data = *stack_p; data; data = data->next)
1924 #endif /* G_CAN_INLINE */
1929 GStringChunk* g_string_chunk_new (gint size);
1930 void g_string_chunk_free (GStringChunk *chunk);
1931 gchar* g_string_chunk_insert (GStringChunk *chunk,
1932 const gchar *string);
1933 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
1934 const gchar *string);
1939 GString* g_string_new (const gchar *init);
1940 GString* g_string_sized_new (guint dfl_size);
1941 gchar* g_string_free (GString *string,
1942 gboolean free_segment);
1943 gboolean g_string_equal (const GString *v,
1945 guint g_string_hash (const GString *str);
1946 GString* g_string_assign (GString *string,
1948 GString* g_string_truncate (GString *string,
1950 GString* g_string_insert_len (GString *string,
1954 GString* g_string_append (GString *string,
1956 GString* g_string_append_len (GString *string,
1959 GString* g_string_append_c (GString *string,
1961 GString* g_string_prepend (GString *string,
1963 GString* g_string_prepend_c (GString *string,
1965 GString* g_string_prepend_len (GString *string,
1968 GString* g_string_insert (GString *string,
1971 GString* g_string_insert_c (GString *string,
1974 GString* g_string_erase (GString *string,
1977 GString* g_string_down (GString *string);
1978 GString* g_string_up (GString *string);
1979 void g_string_sprintf (GString *string,
1980 const gchar *format,
1981 ...) G_GNUC_PRINTF (2, 3);
1982 void g_string_sprintfa (GString *string,
1983 const gchar *format,
1984 ...) G_GNUC_PRINTF (2, 3);
1987 /* Resizable arrays, remove fills any cleared spot and shortens the
1988 * array, while preserving the order. remove_fast will distort the
1989 * order by moving the last element to the position of the removed
1992 #define g_array_append_val(a,v) g_array_append_vals (a, &v, 1)
1993 #define g_array_prepend_val(a,v) g_array_prepend_vals (a, &v, 1)
1994 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
1995 #define g_array_index(a,t,i) (((t*) (a)->data) [(i)])
1997 GArray* g_array_new (gboolean zero_terminated,
1999 guint element_size);
2000 GArray* g_array_sized_new (gboolean zero_terminated,
2003 guint reserved_size);
2004 gchar* g_array_free (GArray *array,
2005 gboolean free_segment);
2006 GArray* g_array_append_vals (GArray *array,
2009 GArray* g_array_prepend_vals (GArray *array,
2012 GArray* g_array_insert_vals (GArray *array,
2016 GArray* g_array_set_size (GArray *array,
2018 GArray* g_array_remove_index (GArray *array,
2020 GArray* g_array_remove_index_fast (GArray *array,
2023 /* Resizable pointer array. This interface is much less complicated
2024 * than the above. Add appends appends a pointer. Remove fills any
2025 * cleared spot and shortens the array. remove_fast will again distort
2028 #define g_ptr_array_index(array,index) (array->pdata)[index]
2029 GPtrArray* g_ptr_array_new (void);
2030 GPtrArray* g_ptr_array_sized_new (guint reserved_size);
2031 gpointer* g_ptr_array_free (GPtrArray *array,
2033 void g_ptr_array_set_size (GPtrArray *array,
2035 gpointer g_ptr_array_remove_index (GPtrArray *array,
2037 gpointer g_ptr_array_remove_index_fast (GPtrArray *array,
2039 gboolean g_ptr_array_remove (GPtrArray *array,
2041 gboolean g_ptr_array_remove_fast (GPtrArray *array,
2043 void g_ptr_array_add (GPtrArray *array,
2046 /* Byte arrays, an array of guint8. Implemented as a GArray,
2050 GByteArray* g_byte_array_new (void);
2051 GByteArray* g_byte_array_sized_new (guint reserved_size);
2052 guint8* g_byte_array_free (GByteArray *array,
2053 gboolean free_segment);
2054 GByteArray* g_byte_array_append (GByteArray *array,
2057 GByteArray* g_byte_array_prepend (GByteArray *array,
2060 GByteArray* g_byte_array_set_size (GByteArray *array,
2062 GByteArray* g_byte_array_remove_index (GByteArray *array,
2064 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
2070 gboolean g_str_equal (gconstpointer v,
2072 guint g_str_hash (gconstpointer v);
2074 gint g_int_equal (gconstpointer v,
2075 gconstpointer v2) G_GNUC_CONST;
2076 guint g_int_hash (gconstpointer v) G_GNUC_CONST;
2078 /* This "hash" function will just return the key's adress as an
2079 * unsigned integer. Useful for hashing on plain adresses or
2080 * simple integer values.
2081 * passing NULL into g_hash_table_new() as GHashFunc has the
2082 * same effect as passing g_direct_hash().
2084 guint g_direct_hash (gconstpointer v) G_GNUC_CONST;
2085 gint g_direct_equal (gconstpointer v,
2086 gconstpointer v2) G_GNUC_CONST;
2089 /* Quarks (string<->id association)
2091 GQuark g_quark_try_string (const gchar *string);
2092 GQuark g_quark_from_static_string (const gchar *string);
2093 GQuark g_quark_from_string (const gchar *string);
2094 gchar* g_quark_to_string (GQuark quark) G_GNUC_CONST;
2099 void g_datalist_init (GData **datalist);
2100 void g_datalist_clear (GData **datalist);
2101 gpointer g_datalist_id_get_data (GData **datalist,
2103 void g_datalist_id_set_data_full (GData **datalist,
2106 GDestroyNotify destroy_func);
2107 gpointer g_datalist_id_remove_no_notify (GData **datalist,
2109 void g_datalist_foreach (GData **datalist,
2110 GDataForeachFunc func,
2111 gpointer user_data);
2112 #define g_datalist_id_set_data(dl, q, d) \
2113 g_datalist_id_set_data_full ((dl), (q), (d), NULL)
2114 #define g_datalist_id_remove_data(dl, q) \
2115 g_datalist_id_set_data ((dl), (q), NULL)
2116 #define g_datalist_get_data(dl, k) \
2117 (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
2118 #define g_datalist_set_data_full(dl, k, d, f) \
2119 g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
2120 #define g_datalist_remove_no_notify(dl, k) \
2121 g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
2122 #define g_datalist_set_data(dl, k, d) \
2123 g_datalist_set_data_full ((dl), (k), (d), NULL)
2124 #define g_datalist_remove_data(dl, k) \
2125 g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
2128 /* Location Associated Keyed Data
2130 void g_dataset_destroy (gconstpointer dataset_location);
2131 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
2133 void g_dataset_id_set_data_full (gconstpointer dataset_location,
2136 GDestroyNotify destroy_func);
2137 gpointer g_dataset_id_remove_no_notify (gconstpointer dataset_location,
2139 void g_dataset_foreach (gconstpointer dataset_location,
2140 GDataForeachFunc func,
2141 gpointer user_data);
2142 #define g_dataset_id_set_data(l, k, d) \
2143 g_dataset_id_set_data_full ((l), (k), (d), NULL)
2144 #define g_dataset_id_remove_data(l, k) \
2145 g_dataset_id_set_data ((l), (k), NULL)
2146 #define g_dataset_get_data(l, k) \
2147 (g_dataset_id_get_data ((l), g_quark_try_string (k)))
2148 #define g_dataset_set_data_full(l, k, d, f) \
2149 g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
2150 #define g_dataset_remove_no_notify(l, k) \
2151 g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
2152 #define g_dataset_set_data(l, k, d) \
2153 g_dataset_set_data_full ((l), (k), (d), NULL)
2154 #define g_dataset_remove_data(l, k) \
2155 g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
2158 /* GScanner: Flexible lexical scanner for general purpose.
2161 /* Character sets */
2162 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2163 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
2164 #define G_CSET_DIGITS "0123456789"
2165 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
2166 "\307\310\311\312\313\314\315\316\317\320"\
2167 "\321\322\323\324\325\326"\
2168 "\330\331\332\333\334\335\336"
2169 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
2170 "\347\350\351\352\353\354\355\356\357\360"\
2171 "\361\362\363\364\365\366"\
2172 "\370\371\372\373\374\375\376\377"
2179 G_ERR_UNEXP_EOF_IN_STRING,
2180 G_ERR_UNEXP_EOF_IN_COMMENT,
2181 G_ERR_NON_DIGIT_IN_CONST,
2184 G_ERR_FLOAT_MALFORMED
2192 G_TOKEN_LEFT_PAREN = '(',
2193 G_TOKEN_RIGHT_PAREN = ')',
2194 G_TOKEN_LEFT_CURLY = '{',
2195 G_TOKEN_RIGHT_CURLY = '}',
2196 G_TOKEN_LEFT_BRACE = '[',
2197 G_TOKEN_RIGHT_BRACE = ']',
2198 G_TOKEN_EQUAL_SIGN = '=',
2199 G_TOKEN_COMMA = ',',
2215 G_TOKEN_IDENTIFIER_NULL,
2217 G_TOKEN_COMMENT_SINGLE,
2218 G_TOKEN_COMMENT_MULTI,
2225 gchar *v_identifier;
2237 struct _GScannerConfig
2241 gchar *cset_skip_characters; /* default: " \t\n" */
2242 gchar *cset_identifier_first;
2243 gchar *cset_identifier_nth;
2244 gchar *cpair_comment_single; /* default: "#\n" */
2246 /* Should symbol lookup work case sensitive?
2248 guint case_sensitive : 1;
2250 /* Boolean values to be adjusted "on the fly"
2251 * to configure scanning behaviour.
2253 guint skip_comment_multi : 1; /* C like comment */
2254 guint skip_comment_single : 1; /* single line comment */
2255 guint scan_comment_multi : 1; /* scan multi line comments? */
2256 guint scan_identifier : 1;
2257 guint scan_identifier_1char : 1;
2258 guint scan_identifier_NULL : 1;
2259 guint scan_symbols : 1;
2260 guint scan_binary : 1;
2261 guint scan_octal : 1;
2262 guint scan_float : 1;
2263 guint scan_hex : 1; /* `0x0ff0' */
2264 guint scan_hex_dollar : 1; /* `$0ff0' */
2265 guint scan_string_sq : 1; /* string: 'anything' */
2266 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
2267 guint numbers_2_int : 1; /* bin, octal, hex => int */
2268 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
2269 guint identifier_2_string : 1;
2270 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
2271 guint symbol_2_token : 1;
2272 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
2279 guint max_parse_errors;
2281 /* g_scanner_error() increments this field */
2284 /* name of input stream, featured by the default message handler */
2285 const gchar *input_name;
2287 /* data pointer for derived structures */
2288 gpointer derived_data;
2290 /* link into the scanner configuration */
2291 GScannerConfig *config;
2293 /* fields filled in after g_scanner_get_next_token() */
2299 /* fields filled in after g_scanner_peek_next_token() */
2300 GTokenType next_token;
2301 GTokenValue next_value;
2303 guint next_position;
2305 /* to be considered private */
2306 GHashTable *symbol_table;
2309 const gchar *text_end;
2313 /* handler function for _warn and _error */
2314 GScannerMsgFunc msg_handler;
2317 GScanner* g_scanner_new (GScannerConfig *config_templ);
2318 void g_scanner_destroy (GScanner *scanner);
2319 void g_scanner_input_file (GScanner *scanner,
2321 void g_scanner_sync_file_offset (GScanner *scanner);
2322 void g_scanner_input_text (GScanner *scanner,
2325 GTokenType g_scanner_get_next_token (GScanner *scanner);
2326 GTokenType g_scanner_peek_next_token (GScanner *scanner);
2327 GTokenType g_scanner_cur_token (GScanner *scanner);
2328 GTokenValue g_scanner_cur_value (GScanner *scanner);
2329 guint g_scanner_cur_line (GScanner *scanner);
2330 guint g_scanner_cur_position (GScanner *scanner);
2331 gboolean g_scanner_eof (GScanner *scanner);
2332 guint g_scanner_set_scope (GScanner *scanner,
2334 void g_scanner_scope_add_symbol (GScanner *scanner,
2336 const gchar *symbol,
2338 void g_scanner_scope_remove_symbol (GScanner *scanner,
2340 const gchar *symbol);
2341 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
2343 const gchar *symbol);
2344 void g_scanner_scope_foreach_symbol (GScanner *scanner,
2347 gpointer user_data);
2348 gpointer g_scanner_lookup_symbol (GScanner *scanner,
2349 const gchar *symbol);
2350 void g_scanner_unexp_token (GScanner *scanner,
2351 GTokenType expected_token,
2352 const gchar *identifier_spec,
2353 const gchar *symbol_spec,
2354 const gchar *symbol_name,
2355 const gchar *message,
2357 void g_scanner_error (GScanner *scanner,
2358 const gchar *format,
2359 ...) G_GNUC_PRINTF (2,3);
2360 void g_scanner_warn (GScanner *scanner,
2361 const gchar *format,
2362 ...) G_GNUC_PRINTF (2,3);
2363 gint g_scanner_stat_mode (const gchar *filename);
2364 /* keep downward source compatibility */
2365 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
2366 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2368 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
2369 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2371 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2372 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2375 /* The following two functions are deprecated and will be removed in
2376 * the next major release. They do no good. */
2377 void g_scanner_freeze_symbol_table (GScanner *scanner);
2378 void g_scanner_thaw_symbol_table (GScanner *scanner);
2386 GCompletionFunc func;
2392 GCompletion* g_completion_new (GCompletionFunc func);
2393 void g_completion_add_items (GCompletion* cmp,
2395 void g_completion_remove_items (GCompletion* cmp,
2397 void g_completion_clear_items (GCompletion* cmp);
2398 GList* g_completion_complete (GCompletion* cmp,
2400 gchar** new_prefix);
2401 void g_completion_free (GCompletion* cmp);
2406 * Date calculations (not time for now, to be resolved). These are a
2407 * mutant combination of Steffen Beyer's DateCalc routines
2408 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2409 * date routines (written for in-house software). Written by Havoc
2410 * Pennington <hp@pobox.com>
2413 typedef guint16 GDateYear;
2414 typedef guint8 GDateDay; /* day of the month */
2415 typedef struct _GDate GDate;
2416 /* make struct tm known without having to include time.h */
2419 /* enum used to specify order of appearance in parsed date strings */
2427 /* actual week and month values */
2430 G_DATE_BAD_WEEKDAY = 0,
2433 G_DATE_WEDNESDAY = 3,
2434 G_DATE_THURSDAY = 4,
2436 G_DATE_SATURDAY = 6,
2441 G_DATE_BAD_MONTH = 0,
2443 G_DATE_FEBRUARY = 2,
2450 G_DATE_SEPTEMBER = 9,
2451 G_DATE_OCTOBER = 10,
2452 G_DATE_NOVEMBER = 11,
2453 G_DATE_DECEMBER = 12
2456 #define G_DATE_BAD_JULIAN 0U
2457 #define G_DATE_BAD_DAY 0U
2458 #define G_DATE_BAD_YEAR 0U
2460 /* Note: directly manipulating structs is generally a bad idea, but
2461 * in this case it's an *incredibly* bad idea, because all or part
2462 * of this struct can be invalid at any given time. Use the functions,
2463 * or you will get hosed, I promise.
2467 guint julian_days : 32; /* julian days representation - we use a
2468 * bitfield hoping that 64 bit platforms
2469 * will pack this whole struct in one big
2473 guint julian : 1; /* julian is valid */
2474 guint dmy : 1; /* dmy is valid */
2476 /* DMY representation */
2482 /* g_date_new() returns an invalid date, you then have to _set() stuff
2483 * to get a usable object. You can also allocate a GDate statically,
2484 * then call g_date_clear() to initialize.
2486 GDate* g_date_new (void);
2487 GDate* g_date_new_dmy (GDateDay day,
2490 GDate* g_date_new_julian (guint32 julian_day);
2491 void g_date_free (GDate *date);
2493 /* check g_date_valid() after doing an operation that might fail, like
2494 * _parse. Almost all g_date operations are undefined on invalid
2495 * dates (the exceptions are the mutators, since you need those to
2496 * return to validity).
2498 gboolean g_date_valid (GDate *date);
2499 gboolean g_date_valid_day (GDateDay day);
2500 gboolean g_date_valid_month (GDateMonth month);
2501 gboolean g_date_valid_year (GDateYear year);
2502 gboolean g_date_valid_weekday (GDateWeekday weekday);
2503 gboolean g_date_valid_julian (guint32 julian_date);
2504 gboolean g_date_valid_dmy (GDateDay day,
2508 GDateWeekday g_date_weekday (GDate *date);
2509 GDateMonth g_date_month (GDate *date);
2510 GDateYear g_date_year (GDate *date);
2511 GDateDay g_date_day (GDate *date);
2512 guint32 g_date_julian (GDate *date);
2513 guint g_date_day_of_year (GDate *date);
2515 /* First monday/sunday is the start of week 1; if we haven't reached
2516 * that day, return 0. These are not ISO weeks of the year; that
2517 * routine needs to be added.
2518 * these functions return the number of weeks, starting on the
2521 guint g_date_monday_week_of_year (GDate *date);
2522 guint g_date_sunday_week_of_year (GDate *date);
2524 /* If you create a static date struct you need to clear it to get it
2525 * in a sane state before use. You can clear a whole array at
2526 * once with the ndates argument.
2528 void g_date_clear (GDate *date,
2531 /* The parse routine is meant for dates typed in by a user, so it
2532 * permits many formats but tries to catch common typos. If your data
2533 * needs to be strictly validated, it is not an appropriate function.
2535 void g_date_set_parse (GDate *date,
2537 void g_date_set_time (GDate *date,
2539 void g_date_set_month (GDate *date,
2541 void g_date_set_day (GDate *date,
2543 void g_date_set_year (GDate *date,
2545 void g_date_set_dmy (GDate *date,
2549 void g_date_set_julian (GDate *date,
2550 guint32 julian_date);
2551 gboolean g_date_is_first_of_month (GDate *date);
2552 gboolean g_date_is_last_of_month (GDate *date);
2554 /* To go forward by some number of weeks just go forward weeks*7 days */
2555 void g_date_add_days (GDate *date,
2557 void g_date_subtract_days (GDate *date,
2560 /* If you add/sub months while day > 28, the day might change */
2561 void g_date_add_months (GDate *date,
2563 void g_date_subtract_months (GDate *date,
2566 /* If it's feb 29, changing years can move you to the 28th */
2567 void g_date_add_years (GDate *date,
2569 void g_date_subtract_years (GDate *date,
2571 gboolean g_date_is_leap_year (GDateYear year) G_GNUC_CONST;
2572 guint8 g_date_days_in_month (GDateMonth month,
2573 GDateYear year) G_GNUC_CONST;
2574 guint8 g_date_monday_weeks_in_year (GDateYear year) G_GNUC_CONST;
2575 guint8 g_date_sunday_weeks_in_year (GDateYear year) G_GNUC_CONST;
2577 /* qsort-friendly (with a cast...) */
2578 gint g_date_compare (GDate *lhs,
2580 void g_date_to_struct_tm (GDate *date,
2583 /* Just like strftime() except you can only use date-related formats.
2584 * Using a time format is undefined.
2586 gsize g_date_strftime (gchar *s,
2588 const gchar *format,
2594 * Indexed Relations. Imagine a really simple table in a
2595 * database. Relations are not ordered. This data type is meant for
2596 * maintaining a N-way mapping.
2598 * g_relation_new() creates a relation with FIELDS fields
2600 * g_relation_destroy() frees all resources
2601 * g_tuples_destroy() frees the result of g_relation_select()
2603 * g_relation_index() indexes relation FIELD with the provided
2604 * equality and hash functions. this must be done before any
2605 * calls to insert are made.
2607 * g_relation_insert() inserts a new tuple. you are expected to
2608 * provide the right number of fields.
2610 * g_relation_delete() deletes all relations with KEY in FIELD
2611 * g_relation_select() returns ...
2612 * g_relation_count() counts ...
2615 GRelation* g_relation_new (gint fields);
2616 void g_relation_destroy (GRelation *relation);
2617 void g_relation_index (GRelation *relation,
2619 GHashFunc hash_func,
2620 GCompareFunc key_compare_func);
2621 void g_relation_insert (GRelation *relation,
2623 gint g_relation_delete (GRelation *relation,
2626 GTuples* g_relation_select (GRelation *relation,
2629 gint g_relation_count (GRelation *relation,
2632 gboolean g_relation_exists (GRelation *relation,
2634 void g_relation_print (GRelation *relation);
2636 void g_tuples_destroy (GTuples *tuples);
2637 gpointer g_tuples_index (GTuples *tuples,
2642 /* GRand - a good and fast random number generator: Mersenne Twister
2643 * see http://www.math.keio.ac.jp/~matumoto/emt.html for more info.
2644 * The range functions return a value in the intervall [min,max).
2645 * int -> [0..2^32-1]
2646 * int_range -> [min..max-1]
2648 * double_range -> [min..max)
2651 GRand* g_rand_new_with_seed (guint32 seed);
2652 GRand* g_rand_new (void);
2653 void g_rand_free (GRand *rand);
2655 void g_rand_set_seed (GRand *rand,
2657 guint32 g_rand_int (GRand *rand);
2658 gint32 g_rand_int_range (GRand *rand,
2661 gdouble g_rand_double (GRand *rand);
2662 gdouble g_rand_double_range (GRand *rand,
2666 void g_random_set_seed (guint32 seed);
2667 guint32 g_random_int (void);
2668 gint32 g_random_int_range (gint32 min,
2670 gdouble g_random_double (void);
2671 gdouble g_random_double_range (gdouble min,
2678 /* This function returns prime numbers spaced by approximately 1.5-2.0
2679 * and is for use in resizing data structures which prefer
2680 * prime-valued sizes. The closest spaced prime function returns the
2681 * next largest prime, or the highest it knows about which is about
2684 guint g_spaced_primes_closest (guint num) G_GNUC_CONST;
2690 typedef struct _GIOFuncs GIOFuncs;
2706 G_IO_IN GLIB_SYSDEF_POLLIN,
2707 G_IO_OUT GLIB_SYSDEF_POLLOUT,
2708 G_IO_PRI GLIB_SYSDEF_POLLPRI,
2709 G_IO_ERR GLIB_SYSDEF_POLLERR,
2710 G_IO_HUP GLIB_SYSDEF_POLLHUP,
2711 G_IO_NVAL GLIB_SYSDEF_POLLNVAL
2716 guint channel_flags;
2721 typedef gboolean (*GIOFunc) (GIOChannel *source,
2722 GIOCondition condition,
2726 GIOError (*io_read) (GIOChannel *channel,
2730 GIOError (*io_write) (GIOChannel *channel,
2733 guint *bytes_written);
2734 GIOError (*io_seek) (GIOChannel *channel,
2737 void (*io_close) (GIOChannel *channel);
2738 guint (*io_add_watch) (GIOChannel *channel,
2740 GIOCondition condition,
2743 GDestroyNotify notify);
2744 void (*io_free) (GIOChannel *channel);
2747 void g_io_channel_init (GIOChannel *channel);
2748 void g_io_channel_ref (GIOChannel *channel);
2749 void g_io_channel_unref (GIOChannel *channel);
2750 GIOError g_io_channel_read (GIOChannel *channel,
2754 GIOError g_io_channel_write (GIOChannel *channel,
2757 guint *bytes_written);
2758 GIOError g_io_channel_seek (GIOChannel *channel,
2761 void g_io_channel_close (GIOChannel *channel);
2762 guint g_io_add_watch_full (GIOChannel *channel,
2764 GIOCondition condition,
2767 GDestroyNotify notify);
2768 guint g_io_add_watch (GIOChannel *channel,
2769 GIOCondition condition,
2771 gpointer user_data);
2776 typedef struct _GTimeVal GTimeVal;
2777 typedef struct _GSourceFuncs GSourceFuncs;
2778 typedef struct _GMainLoop GMainLoop; /* Opaque */
2785 struct _GSourceFuncs
2787 gboolean (*prepare) (gpointer source_data,
2788 GTimeVal *current_time,
2790 gpointer user_data);
2791 gboolean (*check) (gpointer source_data,
2792 GTimeVal *current_time,
2793 gpointer user_data);
2794 gboolean (*dispatch) (gpointer source_data,
2795 GTimeVal *dispatch_time,
2796 gpointer user_data);
2797 GDestroyNotify destroy;
2800 /* Standard priorities */
2802 #define G_PRIORITY_HIGH -100
2803 #define G_PRIORITY_DEFAULT 0
2804 #define G_PRIORITY_HIGH_IDLE 100
2805 #define G_PRIORITY_DEFAULT_IDLE 200
2806 #define G_PRIORITY_LOW 300
2808 typedef gboolean (*GSourceFunc) (gpointer data);
2810 /* Hooks for adding to the main loop */
2811 guint g_source_add (gint priority,
2812 gboolean can_recurse,
2813 GSourceFuncs *funcs,
2814 gpointer source_data,
2816 GDestroyNotify notify);
2817 gboolean g_source_remove (guint tag);
2818 gboolean g_source_remove_by_user_data (gpointer user_data);
2819 gboolean g_source_remove_by_source_data (gpointer source_data);
2820 gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
2821 gpointer user_data);
2823 void g_get_current_time (GTimeVal *result);
2825 /* Running the main loop */
2826 GMainLoop* g_main_new (gboolean is_running);
2827 void g_main_run (GMainLoop *loop);
2828 void g_main_quit (GMainLoop *loop);
2829 void g_main_destroy (GMainLoop *loop);
2830 gboolean g_main_is_running (GMainLoop *loop);
2832 /* Run a single iteration of the mainloop. If block is FALSE,
2835 gboolean g_main_iteration (gboolean may_block);
2837 /* See if any events are pending */
2838 gboolean g_main_pending (void);
2840 /* Idles and timeouts */
2841 guint g_timeout_add_full (gint priority,
2843 GSourceFunc function,
2845 GDestroyNotify notify);
2846 guint g_timeout_add (guint interval,
2847 GSourceFunc function,
2849 guint g_idle_add (GSourceFunc function,
2851 guint g_idle_add_full (gint priority,
2852 GSourceFunc function,
2854 GDestroyNotify destroy);
2855 gboolean g_idle_remove_by_data (gpointer data);
2859 * System-specific IO and main loop calls
2861 * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
2862 * descriptor as provided by the C runtime) that can be used by
2863 * MsgWaitForMultipleObjects. This does *not* include file handles
2864 * from CreateFile, SOCKETs, nor pipe handles. (But you can use
2865 * WSAEventSelect to signal events when a SOCKET is readable).
2867 * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
2868 * indicate polling for messages. These message queue GPollFDs should
2869 * be added with the g_main_poll_win32_msg_add function.
2871 * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
2872 * (GTK) programs, as GDK itself wants to read messages and convert them
2875 * So, unless you really know what you are doing, it's best not to try
2876 * to use the main loop polling stuff for your own needs on
2877 * Win32. It's really only written for the GIMP's needs so
2881 typedef struct _GPollFD GPollFD;
2882 typedef gint (*GPollFunc) (GPollFD *ufds,
2892 void g_main_add_poll (GPollFD *fd,
2894 void g_main_remove_poll (GPollFD *fd);
2895 void g_main_set_poll_func (GPollFunc func);
2899 /* Useful on other platforms, too? */
2901 GPollFunc g_main_win32_get_poll_func (void);
2905 /* On Unix, IO channels created with this function for any file
2906 * descriptor or socket.
2908 * On Win32, use this only for files opened with the MSVCRT (the
2909 * Microsoft run-time C library) _open() or _pipe, including file
2910 * descriptors 0, 1 and 2 (corresponding to stdin, stdout and stderr).
2912 * The term file descriptor as used in the context of Win32 refers to
2913 * the emulated Unix-like file descriptors MSVCRT provides. The native
2914 * corresponding concept is file HANDLE. There isn't as of yet a way to
2915 * get GIOChannels for file HANDLEs.
2917 GIOChannel* g_io_channel_unix_new (int fd);
2918 gint g_io_channel_unix_get_fd (GIOChannel *channel);
2922 #define G_WIN32_MSG_HANDLE 19981206
2924 /* Use this to get a GPollFD from a GIOChannel, so that you can call
2925 * g_io_channel_win32_poll(). After calling this you should only use
2926 * g_io_channel_read() to read from the GIOChannel, i.e. never read()
2927 * or recv() from the underlying file descriptor or SOCKET.
2929 void g_io_channel_win32_make_pollfd (GIOChannel *channel,
2930 GIOCondition condition,
2933 /* This can be used to wait a until at least one of the channels is readable.
2934 * On Unix you would do a select() on the file descriptors of the channels.
2935 * This should probably be available for all platforms?
2937 gint g_io_channel_win32_poll (GPollFD *fds,
2941 /* This is used to add polling for Windows messages. GDK (GTk+) programs
2942 * should *not* use this.
2944 void g_main_poll_win32_msg_add (gint priority,
2948 /* An IO channel for Windows messages for window handle hwnd. */
2949 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
2951 /* An IO channel for C runtime (emulated Unix-like) file
2952 * descriptors. Identical to g_io_channel_unix_new above.
2953 * After calling g_io_add_watch() on a IO channel returned
2954 * by this function, you shouldn't call read() on the file
2957 GIOChannel* g_io_channel_win32_new_fd (int fd);
2959 /* Get the C runtime file descriptor of a channel. */
2960 gint g_io_channel_win32_get_fd (GIOChannel *channel);
2962 /* An IO channel for a SOCK_STREAM winsock socket. The parameter
2963 * should be a SOCKET. After calling g_io_add_watch() on a IO channel
2964 * returned by this function, you shouldn't call recv() on the SOCKET.
2966 GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
2970 /* Windows emulation stubs for common Unix functions
2973 # define MAXPATHLEN 1024
2980 * To get prototypes for the following POSIXish functions, you have to
2981 * include the indicated non-POSIX headers. The functions are defined
2982 * in OLDNAMES.LIB (MSVC) or -lmoldname-msvc (mingw32).
2984 * getcwd: <direct.h> (MSVC), <io.h> (mingw32)
2985 * getpid: <process.h>
2987 * unlink: <stdio.h> or <io.h>
2988 * open, read, write, lseek, close: <io.h>
2993 /* pipe is not in OLDNAMES.LIB or -lmoldname-msvc. */
2994 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
2996 /* For some POSIX functions that are not provided by the MS runtime,
2997 * we provide emulators in glib, which are prefixed with g_win32_.
2999 # define ftruncate(fd, size) g_win32_ftruncate (fd, size)
3001 /* -lmingw32 also has emulations for these, but we need our own
3002 * for MSVC anyhow, so we might aswell use them always.
3004 # define opendir g_win32_opendir
3005 # define readdir g_win32_readdir
3006 # define rewinddir g_win32_rewinddir
3007 # define closedir g_win32_closedir
3008 # define NAME_MAX 255
3013 gboolean just_opened;
3014 guint find_file_handle;
3015 gpointer find_file_data;
3017 typedef struct DIR DIR;
3020 gchar d_name[NAME_MAX + 1];
3022 /* emulation functions */
3023 extern int g_win32_ftruncate (gint f,
3025 DIR* g_win32_opendir (const gchar *dirname);
3026 struct dirent* g_win32_readdir (DIR *dir);
3027 void g_win32_rewinddir (DIR *dir);
3028 gint g_win32_closedir (DIR *dir);
3030 /* The MS setlocale uses locale names of the form "English_United
3031 * States.1252" etc. We want the Unixish standard form "en", "zh_TW"
3032 * etc. This function gets the current thread locale from Windows and
3033 * returns it as a string of the above form for use in forming file
3034 * names etc. The returned string should be deallocated with g_free().
3036 gchar * g_win32_getlocale (void);
3038 /* Translate a Win32 error code (as returned by GetLastError()) into
3039 * the corresponding message. The returned string should be deallocated
3042 gchar * g_win32_error_message (gint error);
3044 #endif /* G_OS_WIN32 */
3047 /* GLib Thread support
3050 typedef void (*GThreadFunc) (gpointer value);
3054 G_THREAD_PRIORITY_LOW,
3055 G_THREAD_PRIORITY_NORMAL,
3056 G_THREAD_PRIORITY_HIGH,
3057 G_THREAD_PRIORITY_URGENT
3060 typedef struct _GThread GThread;
3063 GThreadPriority priority;
3068 typedef struct _GMutex GMutex;
3069 typedef struct _GCond GCond;
3070 typedef struct _GPrivate GPrivate;
3071 typedef struct _GStaticPrivate GStaticPrivate;
3072 typedef struct _GAsyncQueue GAsyncQueue;
3073 typedef struct _GThreadPool GThreadPool;
3075 typedef struct _GThreadFunctions GThreadFunctions;
3076 struct _GThreadFunctions
3078 GMutex* (*mutex_new) (void);
3079 void (*mutex_lock) (GMutex *mutex);
3080 gboolean (*mutex_trylock) (GMutex *mutex);
3081 void (*mutex_unlock) (GMutex *mutex);
3082 void (*mutex_free) (GMutex *mutex);
3083 GCond* (*cond_new) (void);
3084 void (*cond_signal) (GCond *cond);
3085 void (*cond_broadcast) (GCond *cond);
3086 void (*cond_wait) (GCond *cond,
3088 gboolean (*cond_timed_wait) (GCond *cond,
3090 GTimeVal *end_time);
3091 void (*cond_free) (GCond *cond);
3092 GPrivate* (*private_new) (GDestroyNotify destructor);
3093 gpointer (*private_get) (GPrivate *private_key);
3094 void (*private_set) (GPrivate *private_key,
3096 void (*thread_create) (GThreadFunc thread_func,
3101 GThreadPriority priority,
3103 void (*thread_yield) (void);
3104 void (*thread_join) (gpointer thread);
3105 void (*thread_exit) (void);
3106 void (*thread_set_priority)(gpointer thread,
3107 GThreadPriority priority);
3108 void (*thread_self) (gpointer thread);
3111 GLIB_VAR GThreadFunctions g_thread_functions_for_glib_use;
3112 GLIB_VAR gboolean g_thread_use_default_impl;
3113 GLIB_VAR gboolean g_threads_got_initialized;
3115 /* initializes the mutex/cond/private implementation for glib, might
3116 * only be called once, and must not be called directly or indirectly
3117 * from another glib-function, e.g. as a callback.
3119 void g_thread_init (GThreadFunctions *vtable);
3121 /* internal function for fallback static mutex implementation */
3122 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
3124 /* shorthands for conditional and unconditional function calls */
3125 #define G_THREAD_UF(name, arglist) \
3126 (*g_thread_functions_for_glib_use . name) arglist
3127 #define G_THREAD_CF(name, fail, arg) \
3128 (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
3129 /* keep in mind, all those mutexes and static mutexes are not
3130 * recursive in general, don't rely on that
3132 #define g_thread_supported() (g_threads_got_initialized)
3133 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
3134 #define g_mutex_lock(mutex) G_THREAD_CF (mutex_lock, (void)0, (mutex))
3135 #define g_mutex_trylock(mutex) G_THREAD_CF (mutex_trylock, TRUE, (mutex))
3136 #define g_mutex_unlock(mutex) G_THREAD_CF (mutex_unlock, (void)0, (mutex))
3137 #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex))
3138 #define g_cond_new() G_THREAD_UF (cond_new, ())
3139 #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
3140 #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
3141 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait, (void)0, (cond, \
3143 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
3144 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
3148 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
3149 #define g_private_get(private_key) G_THREAD_CF (private_get, \
3150 ((gpointer)private_key), \
3152 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
3153 (void) (private_key = \
3154 (GPrivate*) (value)), \
3155 (private_key, value))
3156 #define g_thread_yield() G_THREAD_CF (thread_yield, (void)0, ())
3157 #define g_thread_exit() G_THREAD_CF (thread_exit, (void)0, ())
3159 GThread* g_thread_create (GThreadFunc thread_func,
3164 GThreadPriority priority);
3165 GThread* g_thread_self ();
3166 void g_thread_join (GThread* thread);
3167 void g_thread_set_priority (GThread* thread,
3168 GThreadPriority priority);
3170 /* GStaticMutexes can be statically initialized with the value
3171 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
3172 * much easier, than having to explicitly allocate the mutex before
3175 #define g_static_mutex_lock(mutex) \
3176 g_mutex_lock (g_static_mutex_get_mutex (mutex))
3177 #define g_static_mutex_trylock(mutex) \
3178 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
3179 #define g_static_mutex_unlock(mutex) \
3180 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
3182 struct _GStaticPrivate
3186 #define G_STATIC_PRIVATE_INIT { 0 }
3187 gpointer g_static_private_get (GStaticPrivate *private_key);
3188 void g_static_private_set (GStaticPrivate *private_key,
3190 GDestroyNotify notify);
3191 gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
3193 void g_static_private_set_for_thread (GStaticPrivate *private_key,
3196 GDestroyNotify notify);
3198 typedef struct _GStaticRecMutex GStaticRecMutex;
3199 struct _GStaticRecMutex
3203 GSystemThread owner;
3206 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT }
3207 void g_static_rec_mutex_lock (GStaticRecMutex *mutex);
3208 gboolean g_static_rec_mutex_trylock (GStaticRecMutex *mutex);
3209 void g_static_rec_mutex_unlock (GStaticRecMutex *mutex);
3210 void g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
3212 guint g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
3214 typedef struct _GStaticRWLock GStaticRWLock;
3215 struct _GStaticRWLock
3222 guint want_to_write;
3225 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
3227 void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
3228 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
3229 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
3230 void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
3231 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
3232 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
3233 void g_static_rw_lock_free (GStaticRWLock* lock);
3235 /* these are some convenience macros that expand to nothing if GLib
3236 * was configured with --disable-threads. for using StaticMutexes,
3237 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
3238 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
3239 * declare such an globally defined lock. name is a unique identifier
3240 * for the protected varibale or code portion. locking, testing and
3241 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
3242 * G_TRYLOCK() respectively.
3244 extern void glib_dummy_decl (void);
3245 #define G_LOCK_NAME(name) g__ ## name ## _lock
3246 #ifdef G_THREADS_ENABLED
3247 # define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
3248 # define G_LOCK_DEFINE(name) \
3249 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
3250 # define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
3252 # ifdef G_DEBUG_LOCKS
3253 # define G_LOCK(name) G_STMT_START{ \
3254 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3255 "file %s: line %d (%s): locking: %s ", \
3256 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3258 g_static_mutex_lock (&G_LOCK_NAME (name)); \
3260 # define G_UNLOCK(name) G_STMT_START{ \
3261 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3262 "file %s: line %d (%s): unlocking: %s ", \
3263 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3265 g_static_mutex_unlock (&G_LOCK_NAME (name)); \
3267 # define G_TRYLOCK(name) \
3268 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3269 "file %s: line %d (%s): try locking: %s ", \
3270 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3271 #name), g_static_mutex_trylock (&G_LOCK_NAME (name)))
3272 # else /* !G_DEBUG_LOCKS */
3273 # define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
3274 # define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
3275 # define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
3276 # endif /* !G_DEBUG_LOCKS */
3277 #else /* !G_THREADS_ENABLED */
3278 # define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
3279 # define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
3280 # define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
3281 # define G_LOCK(name)
3282 # define G_UNLOCK(name)
3283 # define G_TRYLOCK(name) (TRUE)
3284 #endif /* !G_THREADS_ENABLED */
3286 /* Asyncronous Queues, can be used to communicate between threads
3289 /* Get a new GAsyncQueue with the ref_count 1 */
3290 GAsyncQueue* g_async_queue_new (void);
3292 /* Lock and unlock an GAsyncQueue, all functions lock the queue for
3293 * themselves, but in certain cirumstances you want to hold the lock longer,
3294 * thus you lock the queue, call the *_unlocked functions and unlock it again
3296 void g_async_queue_lock (GAsyncQueue *queue);
3297 void g_async_queue_unlock (GAsyncQueue *queue);
3299 /* Ref and unref the GAsyncQueue. g_async_queue_unref_unlocked makes
3300 * no sense, as after the unreffing the Queue might be gone and can't
3301 * be unlocked. So you have a function to call, if you don't hold the
3302 * lock (g_async_queue_unref) and one to call, when you already hold
3303 * the lock (g_async_queue_unref_and_unlock). After that however, you
3304 * don't hold the lock anymore and the Queue might in fact be
3305 * destroyed, if you unrefed to zero */
3306 void g_async_queue_ref (GAsyncQueue *queue);
3307 void g_async_queue_ref_unlocked (GAsyncQueue *queue);
3308 void g_async_queue_unref (GAsyncQueue *queue);
3309 void g_async_queue_unref_and_unlock (GAsyncQueue *queue);
3311 /* Push data into the async queue. Must not be NULL */
3312 void g_async_queue_push (GAsyncQueue *queue,
3314 void g_async_queue_push_unlocked (GAsyncQueue *queue,
3317 /* Pop data from the async queue, when no data is there, the thread is blocked
3318 * until data arrives */
3319 gpointer g_async_queue_pop (GAsyncQueue *queue);
3320 gpointer g_async_queue_pop_unlocked (GAsyncQueue *queue);
3322 /* Try to pop data, NULL is returned in case of empty queue */
3323 gpointer g_async_queue_try_pop (GAsyncQueue *queue);
3324 gpointer g_async_queue_try_pop_unlocked (GAsyncQueue *queue);
3326 /* Wait for data until at maximum until end_time is reached, NULL is returned
3327 * in case of empty queue*/
3328 gpointer g_async_queue_timed_pop (GAsyncQueue *queue,
3329 GTimeVal *end_time);
3330 gpointer g_async_queue_timed_pop_unlocked (GAsyncQueue *queue,
3331 GTimeVal *end_time);
3333 /* Return the length of the queue, negative values mean, that threads
3334 * are waiting, positve values mean, that there are entries in the
3335 * queue. Actually this function returns the length of the queue minus
3336 * the number of waiting threads, g_async_queue_length == 0 could also
3337 * mean 'n' entries in the queue and 'n' thread waiting, such can
3338 * happen due to locking of the queue or due to scheduling. */
3339 gint g_async_queue_length (GAsyncQueue *queue);
3340 gint g_async_queue_length_unlocked (GAsyncQueue *queue);
3345 /* The real GThreadPool is bigger, so you may only create a thread
3346 * pool with the constructor function */
3352 GThreadPriority priority;
3357 /* Get a thread pool with the function thread_func, at most max_threads may
3358 * run at a time (max_threads == -1 means no limit), stack_size, bound,
3359 * priority like in g_thread_create, exclusive == TRUE means, that the threads
3360 * shouldn't be shared and that they will be prestarted (otherwise they are
3361 * started, as needed) user_data is the 2nd argument to the thread_func */
3362 GThreadPool* g_thread_pool_new (GFunc thread_func,
3366 GThreadPriority priority,
3368 gpointer user_data);
3370 /* Push new data into the thread pool. This task is assigned to a thread later
3371 * (when the maximal number of threads is reached for that pool) or now
3372 * (otherwise). If necessary a new thread will be started. The function
3373 * returns immediatly */
3374 void g_thread_pool_push (GThreadPool *pool,
3377 /* Set the number of threads, which can run concurrently for that pool, -1
3378 * means no limit. 0 means has the effect, that the pool won't process
3379 * requests until the limit is set higher again */
3380 void g_thread_pool_set_max_threads (GThreadPool *pool,
3382 gint g_thread_pool_get_max_threads (GThreadPool *pool);
3384 /* Get the number of threads assigned to that pool. This number doesn't
3385 * necessarily represent the number of working threads in that pool */
3386 guint g_thread_pool_get_num_threads (GThreadPool *pool);
3388 /* Get the number of unprocessed items in the pool */
3389 guint g_thread_pool_unprocessed (GThreadPool *pool);
3391 /* Free the pool, immediate means, that all unprocessed items in the queue
3392 * wont be processed, wait means, that the function doesn't return immediatly,
3393 * but after all threads in the pool are ready processing items. immediate
3394 * does however not mean, that threads are killed. */
3395 void g_thread_pool_free (GThreadPool *pool,
3399 /* Set the maximal number of unused threads before threads will be stopped by
3400 * GLib, -1 means no limit */
3401 void g_thread_pool_set_max_unused_threads (gint max_threads);
3402 gint g_thread_pool_get_max_unused_threads (void);
3403 guint g_thread_pool_get_num_unused_threads (void);
3405 /* Stop all currently unused threads, but leave the limit untouched */
3406 void g_thread_pool_stop_unused_threads (void);
3410 #endif /* __cplusplus */
3412 #include <gunicode.h>
3415 #endif /* __G_LIB_H__ */