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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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:
59 * It defines the G_BYTE_ORDER symbol to one of G_*_ENDIAN (see later in
62 * And it provides a way to store and retrieve a `gint' in/from a `gpointer'.
63 * This is useful to pass an integer instead of a pointer to a callback.
65 * GINT_TO_POINTER (i), GUINT_TO_POINTER (i)
66 * GPOINTER_TO_INT (p), GPOINTER_TO_UINT (p)
68 * Finally, it provides the following wrappers to STDC functions:
70 * void g_memmove (gpointer dest, gconstpointer void *src, gulong count);
71 * A wrapper for STDC memmove, or an implementation, if memmove doesn't
72 * exist. The prototype looks like the above, give or take a const,
75 #include <glibconfig.h>
77 /* Define some mathematical constants that aren't available
78 * symbolically in some strict ISO C implementations.
80 #define G_E 2.7182818284590452354E0
81 #define G_LN2 6.9314718055994530942E-1
82 #define G_LN10 2.3025850929940456840E0
83 #define G_PI 3.14159265358979323846E0
84 #define G_PI_2 1.57079632679489661923E0
85 #define G_PI_4 0.78539816339744830962E0
86 #define G_SQRT2 1.4142135623730950488E0
88 /* include varargs functions for assertment macros
92 /* optionally feature DMALLOC memory allocation debugger
101 /* On native Win32, directory separator is the backslash, and search path
102 * separator is the semicolon.
104 #define G_DIR_SEPARATOR '\\'
105 #define G_DIR_SEPARATOR_S "\\"
106 #define G_SEARCHPATH_SEPARATOR ';'
107 #define G_SEARCHPATH_SEPARATOR_S ";"
109 #else /* !G_OS_WIN32 */
113 #define G_DIR_SEPARATOR '/'
114 #define G_DIR_SEPARATOR_S "/"
115 #define G_SEARCHPATH_SEPARATOR ':'
116 #define G_SEARCHPATH_SEPARATOR_S ":"
118 #endif /* !G_OS_WIN32 */
122 #endif /* __cplusplus */
125 /* Provide definitions for some commonly used macros.
126 * Some of them are only provided if they haven't already
127 * been defined. It is assumed that if they are already
128 * defined then the current definition is correct.
133 # else /* !__cplusplus */
134 # define NULL ((void*) 0)
135 # endif /* !__cplusplus */
143 #define TRUE (!FALSE)
147 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
150 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
153 #define ABS(a) (((a) < 0) ? -(a) : (a))
156 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
158 #define G_STRINGIFY(macro_or_string) G_STRINGIFY_ARG (macro_or_string)
159 #define G_STRINGIFY_ARG(contents) #contents
161 /* provide a string identifying the current code position */
163 # define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
165 # define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__)
169 /* Count the number of elements in an array. The array must be defined
170 * as such; using this with a dynamically allocated array will give
173 #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
175 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
176 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
178 #if !defined (G_VA_COPY)
179 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
180 # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
181 # elif defined (G_VA_COPY_AS_ARRAY)
182 # define G_VA_COPY(ap1, ap2) g_memmove ((ap1), (ap2), sizeof (va_list))
183 # else /* va_list is a pointer */
184 # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
185 # endif /* va_list is a pointer */
186 #endif /* !G_VA_COPY */
189 /* Provide convenience macros for handling structure
190 * fields through their offsets.
192 #define G_STRUCT_OFFSET(struct_type, member) \
193 ((glong) ((guint8*) &((struct_type*) 0)->member))
194 #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
195 ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
196 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
197 (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
200 /* inlining hassle. for compilers that don't allow the `inline' keyword,
201 * mostly because of strict ANSI C compliance or dumbness, we try to fall
202 * back to either `__inline__' or `__inline'.
203 * we define G_CAN_INLINE, if the compiler seems to be actually
204 * *capable* to do function inlining, in which case inline function bodys
205 * do make sense. we also define G_INLINE_FUNC to properly export the
206 * function prototypes if no inlining can be performed.
207 * we special case most of the stuff, so inline functions can have a normal
208 * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
210 #ifndef G_INLINE_FUNC
211 # define G_CAN_INLINE 1
214 # if defined (__GNUC__) && defined (__STRICT_ANSI__)
216 # define inline __inline__
218 #else /* !G_HAVE_INLINE */
220 # if defined (G_HAVE___INLINE__)
221 # define inline __inline__
222 # else /* !inline && !__inline__ */
223 # if defined (G_HAVE___INLINE)
224 # define inline __inline
225 # else /* !inline && !__inline__ && !__inline */
226 # define inline /* don't inline, then */
227 # ifndef G_INLINE_FUNC
233 #ifndef G_INLINE_FUNC
236 # define G_INLINE_FUNC extern inline
239 # define G_INLINE_FUNC extern
241 # else /* !__GNUC__ */
243 # define G_INLINE_FUNC static inline
245 # define G_INLINE_FUNC extern
247 # endif /* !__GNUC__ */
248 #endif /* !G_INLINE_FUNC */
251 /* Provide simple macro statement wrappers (adapted from Perl):
252 * G_STMT_START { statements; } G_STMT_END;
253 * can be used as a single statement, as in
254 * if (x) G_STMT_START { ... } G_STMT_END; else ...
256 * For gcc we will wrap the statements within `({' and `})' braces.
257 * For SunOS they will be wrapped within `if (1)' and `else (void) 0',
258 * and otherwise within `do' and `while (0)'.
260 #if !(defined (G_STMT_START) && defined (G_STMT_END))
261 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
262 # define G_STMT_START (void)(
263 # define G_STMT_END )
265 # if (defined (sun) || defined (__sun__))
266 # define G_STMT_START if (1)
267 # define G_STMT_END else (void)0
269 # define G_STMT_START do
270 # define G_STMT_END while (0)
276 /* Provide macros to feature the GCC function attribute.
278 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
279 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
280 __attribute__((format (printf, format_idx, arg_idx)))
281 #define G_GNUC_SCANF( format_idx, arg_idx ) \
282 __attribute__((format (scanf, format_idx, arg_idx)))
283 #define G_GNUC_FORMAT( arg_idx ) \
284 __attribute__((format_arg (arg_idx)))
285 #define G_GNUC_NORETURN \
286 __attribute__((noreturn))
287 #define G_GNUC_CONST \
288 __attribute__((const))
289 #define G_GNUC_UNUSED \
290 __attribute__((unused))
291 #else /* !__GNUC__ */
292 #define G_GNUC_PRINTF( format_idx, arg_idx )
293 #define G_GNUC_SCANF( format_idx, arg_idx )
294 #define G_GNUC_FORMAT( arg_idx )
295 #define G_GNUC_NORETURN
297 #define G_GNUC_UNUSED
298 #endif /* !__GNUC__ */
300 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
301 * macros, so we can refer to them as strings unconditionally.
304 #define G_GNUC_FUNCTION __FUNCTION__
305 #define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
306 #else /* !__GNUC__ */
307 #define G_GNUC_FUNCTION ""
308 #define G_GNUC_PRETTY_FUNCTION ""
309 #endif /* !__GNUC__ */
311 /* we try to provide a usefull equivalent for ATEXIT if it is
312 * not defined, but use is actually abandoned. people should
313 * use g_atexit() instead.
316 # define ATEXIT(proc) g_ATEXIT(proc)
318 # define G_NATIVE_ATEXIT
321 /* Hacker macro to place breakpoints for elected machines.
322 * Actual use is strongly deprecated of course ;)
324 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
325 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
326 #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
327 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
328 #else /* !__i386__ && !__alpha__ */
329 #define G_BREAKPOINT()
330 #endif /* __i386__ */
333 /* Provide macros for easily allocating memory. The macros
334 * will cast the allocated memory to the specified type
335 * in order to avoid compiler warnings. (Makes the code neater).
339 # define g_new(type, count) (ALLOC (type, count))
340 # define g_new0(type, count) (CALLOC (type, count))
341 # define g_renew(type, mem, count) (REALLOC (mem, type, count))
342 #else /* __DMALLOC_H__ */
343 # define g_new(type, count) \
344 ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
345 # define g_new0(type, count) \
346 ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
347 # define g_renew(type, mem, count) \
348 ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
349 #endif /* __DMALLOC_H__ */
351 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
352 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
354 sizeof (type) * (pre_alloc), \
357 #define g_chunk_new(type, chunk) ( \
358 (type *) g_mem_chunk_alloc (chunk) \
360 #define g_chunk_new0(type, chunk) ( \
361 (type *) g_mem_chunk_alloc0 (chunk) \
363 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
364 g_mem_chunk_free ((mem_chunk), (mem)); \
368 /* Provide macros for error handling. The "assert" macros will
369 * exit on failure. The "return" macros will exit the current
370 * function. Two different definitions are given for the macros
371 * if G_DISABLE_ASSERT is not defined, in order to support gcc's
372 * __PRETTY_FUNCTION__ capability.
375 #ifdef G_DISABLE_ASSERT
377 #define g_assert(expr)
378 #define g_assert_not_reached()
380 #else /* !G_DISABLE_ASSERT */
384 #define g_assert(expr) G_STMT_START{ \
386 g_log (G_LOG_DOMAIN, \
388 "file %s: line %d (%s): assertion failed: (%s)", \
391 __PRETTY_FUNCTION__, \
394 #define g_assert_not_reached() G_STMT_START{ \
395 g_log (G_LOG_DOMAIN, \
397 "file %s: line %d (%s): should not be reached", \
400 __PRETTY_FUNCTION__); }G_STMT_END
402 #else /* !__GNUC__ */
404 #define g_assert(expr) G_STMT_START{ \
406 g_log (G_LOG_DOMAIN, \
408 "file %s: line %d: assertion failed: (%s)", \
413 #define g_assert_not_reached() G_STMT_START{ \
414 g_log (G_LOG_DOMAIN, \
416 "file %s: line %d: should not be reached", \
418 __LINE__); }G_STMT_END
420 #endif /* __GNUC__ */
422 #endif /* !G_DISABLE_ASSERT */
425 #ifdef G_DISABLE_CHECKS
427 #define g_return_if_fail(expr)
428 #define g_return_val_if_fail(expr,val)
430 #else /* !G_DISABLE_CHECKS */
434 #define g_return_if_fail(expr) G_STMT_START{ \
437 g_log (G_LOG_DOMAIN, \
438 G_LOG_LEVEL_CRITICAL, \
439 "file %s: line %d (%s): assertion `%s' failed.", \
442 __PRETTY_FUNCTION__, \
447 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
450 g_log (G_LOG_DOMAIN, \
451 G_LOG_LEVEL_CRITICAL, \
452 "file %s: line %d (%s): assertion `%s' failed.", \
455 __PRETTY_FUNCTION__, \
460 #else /* !__GNUC__ */
462 #define g_return_if_fail(expr) G_STMT_START{ \
465 g_log (G_LOG_DOMAIN, \
466 G_LOG_LEVEL_CRITICAL, \
467 "file %s: line %d: assertion `%s' failed.", \
474 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
477 g_log (G_LOG_DOMAIN, \
478 G_LOG_LEVEL_CRITICAL, \
479 "file %s: line %d: assertion `%s' failed.", \
486 #endif /* !__GNUC__ */
488 #endif /* !G_DISABLE_CHECKS */
491 /* Provide type definitions for commonly used types.
492 * These are useful because a "gint8" can be adjusted
493 * to be 1 byte (8 bits) on all platforms. Similarly and
494 * more importantly, "gint32" can be adjusted to be
495 * 4 bytes (32 bits) on all platforms.
499 typedef short gshort;
502 typedef gint gboolean;
503 typedef gchar* gstring;
505 typedef unsigned char guchar;
506 typedef unsigned short gushort;
507 typedef unsigned long gulong;
508 typedef unsigned int guint;
510 #define G_GSHORT_FORMAT "hi"
511 #define G_GUSHORT_FORMAT "hu"
512 #define G_GINT_FORMAT "i"
513 #define G_GUINT_FORMAT "u"
514 #define G_GLONG_FORMAT "li"
515 #define G_GULONG_FORMAT "lu"
517 typedef float gfloat;
518 typedef double gdouble;
520 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
521 * Since gldouble isn't used anywhere, just disable it for now */
524 #ifdef HAVE_LONG_DOUBLE
525 typedef long double gldouble;
526 #else /* HAVE_LONG_DOUBLE */
527 typedef double gldouble;
528 #endif /* HAVE_LONG_DOUBLE */
531 typedef void* gpointer;
532 typedef const void *gconstpointer;
535 typedef gint32 gssize;
536 typedef guint32 gsize;
537 typedef guint32 GQuark;
538 typedef gint32 GTime;
541 /* Portable endian checks and conversions
543 * glibconfig.h defines G_BYTE_ORDER which expands to one of
546 #define G_LITTLE_ENDIAN 1234
547 #define G_BIG_ENDIAN 4321
548 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
551 /* Basic bit swapping functions
553 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
554 (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
555 (((guint16) (val) & (guint16) 0xff00U) >> 8)))
556 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((guint32) ( \
557 (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
558 (((guint32) (val) & (guint32) 0x0000ff00U) << 8) | \
559 (((guint32) (val) & (guint32) 0x00ff0000U) >> 8) | \
560 (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
562 /* Intel specific stuff for speed
564 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
565 # define GUINT16_SWAP_LE_BE_X86(val) \
567 ({ register guint16 __v; \
568 if (__builtin_constant_p (val)) \
569 __v = GUINT16_SWAP_LE_BE_CONSTANT (val); \
571 __asm__ __const__ ("rorw $8, %w0" \
573 : "0" ((guint16) (val))); \
575 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
576 # if !defined(__i486__) && !defined(__i586__) \
577 && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
578 # define GUINT32_SWAP_LE_BE_X86(val) \
580 ({ register guint32 __v; \
581 if (__builtin_constant_p (val)) \
582 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
584 __asm__ __const__ ("rorw $8, %w0\n\t" \
588 : "0" ((guint32) (val))); \
590 # else /* 486 and higher has bswap */
591 # define GUINT32_SWAP_LE_BE_X86(val) \
593 ({ register guint32 __v; \
594 if (__builtin_constant_p (val)) \
595 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
597 __asm__ __const__ ("bswap %0" \
599 : "0" ((guint32) (val))); \
601 # endif /* processor specific 32-bit stuff */
602 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
603 #else /* !__i386__ */
604 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
605 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
606 #endif /* __i386__ */
609 # define GUINT64_SWAP_LE_BE_CONSTANT(val) ((guint64) ( \
610 (((guint64) (val) & \
611 (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) | \
612 (((guint64) (val) & \
613 (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) | \
614 (((guint64) (val) & \
615 (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) | \
616 (((guint64) (val) & \
617 (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) << 8) | \
618 (((guint64) (val) & \
619 (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >> 8) | \
620 (((guint64) (val) & \
621 (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) | \
622 (((guint64) (val) & \
623 (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) | \
624 (((guint64) (val) & \
625 (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
626 # if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
627 # define GUINT64_SWAP_LE_BE_X86(val) \
629 ({ union { guint64 __ll; \
630 guint32 __l[2]; } __r; \
631 if (__builtin_constant_p (val)) \
632 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val); \
635 union { guint64 __ll; \
636 guint32 __l[2]; } __w; \
637 __w.__ll = ((guint64) val); \
638 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
639 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
642 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
643 # else /* !__i386__ */
644 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
648 #define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
649 #define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
650 #define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
651 (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
652 (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
653 #define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
654 (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
655 (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
657 /* The G*_TO_?E() macros are defined in glibconfig.h.
658 * The transformation is symmetric, so the FROM just maps to the TO.
660 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
661 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
662 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
663 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
664 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
665 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
666 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
667 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
670 #define GINT64_FROM_LE(val) (GINT64_TO_LE (val))
671 #define GUINT64_FROM_LE(val) (GUINT64_TO_LE (val))
672 #define GINT64_FROM_BE(val) (GINT64_TO_BE (val))
673 #define GUINT64_FROM_BE(val) (GUINT64_TO_BE (val))
676 #define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
677 #define GULONG_FROM_LE(val) (GULONG_TO_LE (val))
678 #define GLONG_FROM_BE(val) (GLONG_TO_BE (val))
679 #define GULONG_FROM_BE(val) (GULONG_TO_BE (val))
681 #define GINT_FROM_LE(val) (GINT_TO_LE (val))
682 #define GUINT_FROM_LE(val) (GUINT_TO_LE (val))
683 #define GINT_FROM_BE(val) (GINT_TO_BE (val))
684 #define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
687 /* Portable versions of host-network order stuff
689 #define g_ntohl(val) (GUINT32_FROM_BE (val))
690 #define g_ntohs(val) (GUINT16_FROM_BE (val))
691 #define g_htonl(val) (GUINT32_TO_BE (val))
692 #define g_htons(val) (GUINT16_TO_BE (val))
696 * we prefix variable declarations so they can
697 * properly get exported in windows dlls.
700 # ifdef GLIB_COMPILATION
701 # define GUTILS_C_VAR __declspec(dllexport)
702 # else /* !GLIB_COMPILATION */
703 # define GUTILS_C_VAR extern __declspec(dllimport)
704 # endif /* !GLIB_COMPILATION */
705 #else /* !G_OS_WIN32 */
706 # define GUTILS_C_VAR extern
707 #endif /* !G_OS_WIN32 */
709 GUTILS_C_VAR const guint glib_major_version;
710 GUTILS_C_VAR const guint glib_minor_version;
711 GUTILS_C_VAR const guint glib_micro_version;
712 GUTILS_C_VAR const guint glib_interface_age;
713 GUTILS_C_VAR const guint glib_binary_age;
715 #define GLIB_CHECK_VERSION(major,minor,micro) \
716 (GLIB_MAJOR_VERSION > (major) || \
717 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
718 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
719 GLIB_MICRO_VERSION >= (micro)))
721 /* Forward declarations of glib types.
723 typedef struct _GAllocator GAllocator;
724 typedef struct _GArray GArray;
725 typedef struct _GByteArray GByteArray;
726 typedef struct _GCache GCache;
727 typedef struct _GCompletion GCompletion;
728 typedef struct _GData GData;
729 typedef struct _GDebugKey GDebugKey;
730 typedef union _GDoubleIEEE754 GDoubleIEEE754;
731 typedef union _GFloatIEEE754 GFloatIEEE754;
732 typedef struct _GHashTable GHashTable;
733 typedef struct _GHook GHook;
734 typedef struct _GHookList GHookList;
735 typedef struct _GList GList;
736 typedef struct _GMemChunk GMemChunk;
737 typedef struct _GNode GNode;
738 typedef struct _GPtrArray GPtrArray;
739 typedef struct _GQueue GQueue;
740 typedef struct _GRand GRand;
741 typedef struct _GRelation GRelation;
742 typedef struct _GScanner GScanner;
743 typedef struct _GScannerConfig GScannerConfig;
744 typedef struct _GSList GSList;
745 typedef struct _GString GString;
746 typedef struct _GStringChunk GStringChunk;
747 typedef struct _GTimer GTimer;
748 typedef struct _GTrashStack GTrashStack;
749 typedef struct _GTree GTree;
750 typedef struct _GTuples GTuples;
751 typedef union _GTokenValue GTokenValue;
752 typedef struct _GIOChannel GIOChannel;
754 /* Tree traverse flags */
757 G_TRAVERSE_LEAFS = 1 << 0,
758 G_TRAVERSE_NON_LEAFS = 1 << 1,
759 G_TRAVERSE_ALL = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
760 G_TRAVERSE_MASK = 0x03
763 /* Tree traverse orders */
772 /* Log level shift offset for user defined
773 * log levels (0-7 are used by GLib).
775 #define G_LOG_LEVEL_USER_SHIFT (8)
777 /* Glib log levels and flags.
782 G_LOG_FLAG_RECURSION = 1 << 0,
783 G_LOG_FLAG_FATAL = 1 << 1,
785 /* GLib log levels */
786 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
787 G_LOG_LEVEL_CRITICAL = 1 << 3,
788 G_LOG_LEVEL_WARNING = 1 << 4,
789 G_LOG_LEVEL_MESSAGE = 1 << 5,
790 G_LOG_LEVEL_INFO = 1 << 6,
791 G_LOG_LEVEL_DEBUG = 1 << 7,
793 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
796 /* GLib log levels that are considered fatal by default */
797 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
800 typedef gpointer (*GCacheNewFunc) (gpointer key);
801 typedef gpointer (*GCacheDupFunc) (gpointer value);
802 typedef void (*GCacheDestroyFunc) (gpointer value);
803 typedef gint (*GCompareFunc) (gconstpointer a,
805 typedef gchar* (*GCompletionFunc) (gpointer);
806 typedef void (*GDestroyNotify) (gpointer data);
807 typedef void (*GDataForeachFunc) (GQuark key_id,
810 typedef void (*GFunc) (gpointer data,
812 typedef guint (*GHashFunc) (gconstpointer key);
813 typedef void (*GFreeFunc) (gpointer data);
814 typedef void (*GHFunc) (gpointer key,
817 typedef gboolean (*GHRFunc) (gpointer key,
820 typedef gint (*GHookCompareFunc) (GHook *new_hook,
822 typedef gboolean (*GHookFindFunc) (GHook *hook,
824 typedef void (*GHookMarshaller) (GHook *hook,
826 typedef gboolean (*GHookCheckMarshaller) (GHook *hook,
828 typedef void (*GHookFunc) (gpointer data);
829 typedef gboolean (*GHookCheckFunc) (gpointer data);
830 typedef void (*GHookFreeFunc) (GHookList *hook_list,
832 typedef void (*GLogFunc) (const gchar *log_domain,
833 GLogLevelFlags log_level,
834 const gchar *message,
836 typedef gboolean (*GNodeTraverseFunc) (GNode *node,
838 typedef void (*GNodeForeachFunc) (GNode *node,
840 typedef void (*GScannerMsgFunc) (GScanner *scanner,
843 typedef gint (*GTraverseFunc) (gpointer key,
846 typedef void (*GVoidFunc) (void);
910 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
913 * +--------+---------------+---------------+
914 * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
915 * +--------+---------------+---------------+
916 * B0------------------->B1------->B2-->B3-->
918 * IEEE Standard 754 Double Precision Storage Format (gdouble):
920 * 63 62 52 51 32 31 0
921 * +--------+----------------+----------------+ +---------------+
922 * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
923 * +--------+----------------+----------------+ +---------------+
924 * B0--------------->B1---------->B2--->B3----> B4->B5->B6->B7->
926 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
927 #define G_IEEE754_FLOAT_BIAS (127)
928 #define G_IEEE754_DOUBLE_BIAS (1023)
929 /* multiply with base2 exponent to get base10 exponent (nomal numbers) */
930 #define G_LOG_2_BASE_10 (0.30102999566398119521)
931 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
937 guint biased_exponent : 8;
941 union _GDoubleIEEE754
945 guint mantissa_low : 32;
946 guint mantissa_high : 20;
947 guint biased_exponent : 11;
951 #elif G_BYTE_ORDER == G_BIG_ENDIAN
957 guint biased_exponent : 8;
961 union _GDoubleIEEE754
966 guint biased_exponent : 11;
967 guint mantissa_high : 20;
968 guint mantissa_low : 32;
971 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
972 #error unknown ENDIAN type
973 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
976 /* Doubly linked lists
978 void g_list_push_allocator (GAllocator *allocator);
979 void g_list_pop_allocator (void);
980 GList* g_list_alloc (void);
981 void g_list_free (GList *list);
982 void g_list_free_1 (GList *list);
983 GList* g_list_append (GList *list,
985 GList* g_list_prepend (GList *list,
987 GList* g_list_insert (GList *list,
990 GList* g_list_insert_sorted (GList *list,
993 GList* g_list_concat (GList *list1,
995 GList* g_list_remove (GList *list,
997 GList* g_list_remove_link (GList *list,
999 GList* g_list_delete_link (GList *list,
1001 GList* g_list_reverse (GList *list);
1002 GList* g_list_copy (GList *list);
1003 GList* g_list_nth (GList *list,
1005 GList* g_list_find (GList *list,
1006 gconstpointer data);
1007 GList* g_list_find_custom (GList *list,
1010 gint g_list_position (GList *list,
1012 gint g_list_index (GList *list,
1013 gconstpointer data);
1014 GList* g_list_last (GList *list);
1015 GList* g_list_first (GList *list);
1016 guint g_list_length (GList *list);
1017 void g_list_foreach (GList *list,
1019 gpointer user_data);
1020 GList* g_list_sort (GList *list,
1021 GCompareFunc compare_func);
1022 gpointer g_list_nth_data (GList *list,
1024 #define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
1025 #define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
1028 /* Singly linked lists
1030 void g_slist_push_allocator (GAllocator *allocator);
1031 void g_slist_pop_allocator (void);
1032 GSList* g_slist_alloc (void);
1033 void g_slist_free (GSList *list);
1034 void g_slist_free_1 (GSList *list);
1035 GSList* g_slist_append (GSList *list,
1037 GSList* g_slist_prepend (GSList *list,
1039 GSList* g_slist_insert (GSList *list,
1042 GSList* g_slist_insert_sorted (GSList *list,
1045 GSList* g_slist_insert_before (GSList *slist,
1048 GSList* g_slist_concat (GSList *list1,
1050 GSList* g_slist_remove (GSList *list,
1051 gconstpointer data);
1052 GSList* g_slist_remove_link (GSList *list,
1054 GSList* g_slist_delete_link (GSList *list,
1056 GSList* g_slist_reverse (GSList *list);
1057 GSList* g_slist_copy (GSList *list);
1058 GSList* g_slist_nth (GSList *list,
1060 GSList* g_slist_find (GSList *list,
1061 gconstpointer data);
1062 GSList* g_slist_find_custom (GSList *list,
1065 gint g_slist_position (GSList *list,
1067 gint g_slist_index (GSList *list,
1068 gconstpointer data);
1069 GSList* g_slist_last (GSList *list);
1070 guint g_slist_length (GSList *list);
1071 void g_slist_foreach (GSList *list,
1073 gpointer user_data);
1074 GSList* g_slist_sort (GSList *list,
1075 GCompareFunc compare_func);
1076 gpointer g_slist_nth_data (GSList *list,
1078 #define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
1083 GQueue* g_queue_new (void);
1084 void g_queue_free (GQueue *queue);
1085 void g_queue_push_head (GQueue *queue,
1087 void g_queue_push_tail (GQueue *queue,
1089 gpointer g_queue_pop_head (GQueue *queue);
1090 gpointer g_queue_pop_tail (GQueue *queue);
1091 gboolean g_queue_is_empty (GQueue *queue);
1092 gpointer g_queue_peek_head (GQueue *queue);
1093 gpointer g_queue_peek_tail (GQueue *queue);
1094 void g_queue_push_head_link (GQueue *queue,
1096 void g_queue_push_tail_link (GQueue *queue,
1098 GList* g_queue_pop_head_link (GQueue *queue);
1099 GList* g_queue_pop_tail_link (GQueue *queue);
1103 GHashTable* g_hash_table_new (GHashFunc hash_func,
1104 GCompareFunc key_compare_func);
1105 void g_hash_table_destroy (GHashTable *hash_table);
1106 void g_hash_table_insert (GHashTable *hash_table,
1109 void g_hash_table_remove (GHashTable *hash_table,
1111 gpointer g_hash_table_lookup (GHashTable *hash_table,
1113 gboolean g_hash_table_lookup_extended(GHashTable *hash_table,
1114 gconstpointer lookup_key,
1117 void g_hash_table_freeze (GHashTable *hash_table);
1118 void g_hash_table_thaw (GHashTable *hash_table);
1119 void g_hash_table_foreach (GHashTable *hash_table,
1121 gpointer user_data);
1122 guint g_hash_table_foreach_remove (GHashTable *hash_table,
1124 gpointer user_data);
1125 guint g_hash_table_size (GHashTable *hash_table);
1130 GCache* g_cache_new (GCacheNewFunc value_new_func,
1131 GCacheDestroyFunc value_destroy_func,
1132 GCacheDupFunc key_dup_func,
1133 GCacheDestroyFunc key_destroy_func,
1134 GHashFunc hash_key_func,
1135 GHashFunc hash_value_func,
1136 GCompareFunc key_compare_func);
1137 void g_cache_destroy (GCache *cache);
1138 gpointer g_cache_insert (GCache *cache,
1140 void g_cache_remove (GCache *cache,
1141 gconstpointer value);
1142 void g_cache_key_foreach (GCache *cache,
1144 gpointer user_data);
1145 void g_cache_value_foreach (GCache *cache,
1147 gpointer user_data);
1150 /* Balanced binary trees
1152 GTree* g_tree_new (GCompareFunc key_compare_func);
1153 void g_tree_destroy (GTree *tree);
1154 void g_tree_insert (GTree *tree,
1157 void g_tree_remove (GTree *tree,
1159 gpointer g_tree_lookup (GTree *tree,
1161 void g_tree_traverse (GTree *tree,
1162 GTraverseFunc traverse_func,
1163 GTraverseType traverse_type,
1165 gpointer g_tree_search (GTree *tree,
1166 GCompareFunc search_func,
1167 gconstpointer data);
1168 gint g_tree_height (GTree *tree);
1169 gint g_tree_nnodes (GTree *tree);
1173 /* N-way tree implementation
1184 #define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
1185 ((GNode*) (node))->prev == NULL && \
1186 ((GNode*) (node))->next == NULL)
1187 #define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
1189 void g_node_push_allocator (GAllocator *allocator);
1190 void g_node_pop_allocator (void);
1191 GNode* g_node_new (gpointer data);
1192 void g_node_destroy (GNode *root);
1193 void g_node_unlink (GNode *node);
1194 GNode* g_node_copy (GNode *node);
1195 GNode* g_node_insert (GNode *parent,
1198 GNode* g_node_insert_before (GNode *parent,
1201 GNode* g_node_prepend (GNode *parent,
1203 guint g_node_n_nodes (GNode *root,
1204 GTraverseFlags flags);
1205 GNode* g_node_get_root (GNode *node);
1206 gboolean g_node_is_ancestor (GNode *node,
1208 guint g_node_depth (GNode *node);
1209 GNode* g_node_find (GNode *root,
1210 GTraverseType order,
1211 GTraverseFlags flags,
1214 /* convenience macros */
1215 #define g_node_append(parent, node) \
1216 g_node_insert_before ((parent), NULL, (node))
1217 #define g_node_insert_data(parent, position, data) \
1218 g_node_insert ((parent), (position), g_node_new (data))
1219 #define g_node_insert_data_before(parent, sibling, data) \
1220 g_node_insert_before ((parent), (sibling), g_node_new (data))
1221 #define g_node_prepend_data(parent, data) \
1222 g_node_prepend ((parent), g_node_new (data))
1223 #define g_node_append_data(parent, data) \
1224 g_node_insert_before ((parent), NULL, g_node_new (data))
1226 /* traversal function, assumes that `node' is root
1227 * (only traverses `node' and its subtree).
1228 * this function is just a high level interface to
1229 * low level traversal functions, optimized for speed.
1231 void g_node_traverse (GNode *root,
1232 GTraverseType order,
1233 GTraverseFlags flags,
1235 GNodeTraverseFunc func,
1238 /* return the maximum tree height starting with `node', this is an expensive
1239 * operation, since we need to visit all nodes. this could be shortened by
1240 * adding `guint height' to struct _GNode, but then again, this is not very
1241 * often needed, and would make g_node_insert() more time consuming.
1243 guint g_node_max_height (GNode *root);
1245 void g_node_children_foreach (GNode *node,
1246 GTraverseFlags flags,
1247 GNodeForeachFunc func,
1249 void g_node_reverse_children (GNode *node);
1250 guint g_node_n_children (GNode *node);
1251 GNode* g_node_nth_child (GNode *node,
1253 GNode* g_node_last_child (GNode *node);
1254 GNode* g_node_find_child (GNode *node,
1255 GTraverseFlags flags,
1257 gint g_node_child_position (GNode *node,
1259 gint g_node_child_index (GNode *node,
1262 GNode* g_node_first_sibling (GNode *node);
1263 GNode* g_node_last_sibling (GNode *node);
1265 #define g_node_prev_sibling(node) ((node) ? \
1266 ((GNode*) (node))->prev : NULL)
1267 #define g_node_next_sibling(node) ((node) ? \
1268 ((GNode*) (node))->next : NULL)
1269 #define g_node_first_child(node) ((node) ? \
1270 ((GNode*) (node))->children : NULL)
1273 /* Callback maintenance functions
1275 #define G_HOOK_FLAG_USER_SHIFT (4)
1278 G_HOOK_FLAG_ACTIVE = 1 << 0,
1279 G_HOOK_FLAG_IN_CALL = 1 << 1,
1280 G_HOOK_FLAG_MASK = 0x0f
1283 #define G_HOOK_DEFERRED_DESTROY ((GHookFreeFunc) 0x01)
1291 GMemChunk *hook_memchunk;
1292 GHookFreeFunc hook_free; /* virtual function */
1293 GHookFreeFunc hook_destroy; /* virtual function */
1305 GDestroyNotify destroy;
1308 #define G_HOOK_ACTIVE(hook) ((((GHook*) hook)->flags & \
1309 G_HOOK_FLAG_ACTIVE) != 0)
1310 #define G_HOOK_IN_CALL(hook) ((((GHook*) hook)->flags & \
1311 G_HOOK_FLAG_IN_CALL) != 0)
1312 #define G_HOOK_IS_VALID(hook) (((GHook*) hook)->hook_id != 0 && \
1313 G_HOOK_ACTIVE (hook))
1314 #define G_HOOK_IS_UNLINKED(hook) (((GHook*) hook)->next == NULL && \
1315 ((GHook*) hook)->prev == NULL && \
1316 ((GHook*) hook)->hook_id == 0 && \
1317 ((GHook*) hook)->ref_count == 0)
1319 void g_hook_list_init (GHookList *hook_list,
1321 void g_hook_list_clear (GHookList *hook_list);
1322 GHook* g_hook_alloc (GHookList *hook_list);
1323 void g_hook_free (GHookList *hook_list,
1325 void g_hook_ref (GHookList *hook_list,
1327 void g_hook_unref (GHookList *hook_list,
1329 gboolean g_hook_destroy (GHookList *hook_list,
1331 void g_hook_destroy_link (GHookList *hook_list,
1333 void g_hook_prepend (GHookList *hook_list,
1335 void g_hook_insert_before (GHookList *hook_list,
1338 void g_hook_insert_sorted (GHookList *hook_list,
1340 GHookCompareFunc func);
1341 GHook* g_hook_get (GHookList *hook_list,
1343 GHook* g_hook_find (GHookList *hook_list,
1344 gboolean need_valids,
1347 GHook* g_hook_find_data (GHookList *hook_list,
1348 gboolean need_valids,
1350 GHook* g_hook_find_func (GHookList *hook_list,
1351 gboolean need_valids,
1353 GHook* g_hook_find_func_data (GHookList *hook_list,
1354 gboolean need_valids,
1357 /* return the first valid hook, and increment its reference count */
1358 GHook* g_hook_first_valid (GHookList *hook_list,
1359 gboolean may_be_in_call);
1360 /* return the next valid hook with incremented reference count, and
1361 * decrement the reference count of the original hook
1363 GHook* g_hook_next_valid (GHookList *hook_list,
1365 gboolean may_be_in_call);
1367 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1368 gint g_hook_compare_ids (GHook *new_hook,
1371 /* convenience macros */
1372 #define g_hook_append( hook_list, hook ) \
1373 g_hook_insert_before ((hook_list), NULL, (hook))
1375 /* invoke all valid hooks with the (*GHookFunc) signature.
1377 void g_hook_list_invoke (GHookList *hook_list,
1378 gboolean may_recurse);
1379 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1380 * and destroy the hook if FALSE is returned.
1382 void g_hook_list_invoke_check (GHookList *hook_list,
1383 gboolean may_recurse);
1384 /* invoke a marshaller on all valid hooks.
1386 void g_hook_list_marshal (GHookList *hook_list,
1387 gboolean may_recurse,
1388 GHookMarshaller marshaller,
1390 void g_hook_list_marshal_check (GHookList *hook_list,
1391 gboolean may_recurse,
1392 GHookCheckMarshaller marshaller,
1396 /* Fatal error handlers.
1397 * g_on_error_query() will prompt the user to either
1398 * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1399 * g_on_error_stack_trace() invokes gdb, which attaches to the current
1400 * process and shows a stack trace.
1401 * These function may cause different actions on non-unix platforms.
1402 * The prg_name arg is required by gdb to find the executable, if it is
1403 * passed as NULL, g_on_error_query() will try g_get_prgname().
1405 void g_on_error_query (const gchar *prg_name);
1406 void g_on_error_stack_trace (const gchar *prg_name);
1409 /* Logging mechanism
1411 extern const gchar *g_log_domain_glib;
1412 guint g_log_set_handler (const gchar *log_domain,
1413 GLogLevelFlags log_levels,
1415 gpointer user_data);
1416 void g_log_remove_handler (const gchar *log_domain,
1418 void g_log_default_handler (const gchar *log_domain,
1419 GLogLevelFlags log_level,
1420 const gchar *message,
1421 gpointer unused_data);
1422 void g_log (const gchar *log_domain,
1423 GLogLevelFlags log_level,
1424 const gchar *format,
1425 ...) G_GNUC_PRINTF (3, 4);
1426 void g_logv (const gchar *log_domain,
1427 GLogLevelFlags log_level,
1428 const gchar *format,
1430 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
1431 GLogLevelFlags fatal_mask);
1432 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
1433 #ifndef G_LOG_DOMAIN
1434 #define G_LOG_DOMAIN ((gchar*) 0)
1435 #endif /* G_LOG_DOMAIN */
1437 #define g_error(format, args...) g_log (G_LOG_DOMAIN, \
1438 G_LOG_LEVEL_ERROR, \
1440 #define g_message(format, args...) g_log (G_LOG_DOMAIN, \
1441 G_LOG_LEVEL_MESSAGE, \
1443 #define g_warning(format, args...) g_log (G_LOG_DOMAIN, \
1444 G_LOG_LEVEL_WARNING, \
1446 #else /* !__GNUC__ */
1448 g_error (const gchar *format,
1452 va_start (args, format);
1453 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1457 g_message (const gchar *format,
1461 va_start (args, format);
1462 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1466 g_warning (const gchar *format,
1470 va_start (args, format);
1471 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1474 #endif /* !__GNUC__ */
1476 typedef void (*GPrintFunc) (const gchar *string);
1477 void g_print (const gchar *format,
1478 ...) G_GNUC_PRINTF (1, 2);
1479 GPrintFunc g_set_print_handler (GPrintFunc func);
1480 void g_printerr (const gchar *format,
1481 ...) G_GNUC_PRINTF (1, 2);
1482 GPrintFunc g_set_printerr_handler (GPrintFunc func);
1484 /* deprecated compatibility functions, use g_log_set_handler() instead */
1485 typedef void (*GErrorFunc) (const gchar *str);
1486 typedef void (*GWarningFunc) (const gchar *str);
1487 GErrorFunc g_set_error_handler (GErrorFunc func);
1488 GWarningFunc g_set_warning_handler (GWarningFunc func);
1489 GPrintFunc g_set_message_handler (GPrintFunc func);
1492 /* Memory allocation and debugging
1496 #define g_malloc(size) ((gpointer) MALLOC (size))
1497 #define g_malloc0(size) ((gpointer) CALLOC (char, size))
1498 #define g_realloc(mem,size) ((gpointer) REALLOC (mem, char, size))
1499 #define g_free(mem) FREE (mem)
1501 #else /* !USE_DMALLOC */
1503 gpointer g_malloc (gulong size);
1504 gpointer g_malloc0 (gulong size);
1505 gpointer g_realloc (gpointer mem,
1507 void g_free (gpointer mem);
1509 #endif /* !USE_DMALLOC */
1511 void g_mem_profile (void);
1512 void g_mem_check (gpointer mem);
1514 /* Generic allocators
1516 GAllocator* g_allocator_new (const gchar *name,
1518 void g_allocator_free (GAllocator *allocator);
1520 #define G_ALLOCATOR_LIST (1)
1521 #define G_ALLOCATOR_SLIST (2)
1522 #define G_ALLOCATOR_NODE (3)
1525 /* "g_mem_chunk_new" creates a new memory chunk.
1526 * Memory chunks are used to allocate pieces of memory which are
1527 * always the same size. Lists are a good example of such a data type.
1528 * The memory chunk allocates and frees blocks of memory as needed.
1529 * Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1530 * allocated in a mem chunk. ("g_free" will most likely cause a seg
1531 * fault...somewhere).
1533 * Oh yeah, GMemChunk is an opaque data type. (You don't really
1534 * want to know what's going on inside do you?)
1537 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1538 * is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1539 * atom. (They are also useful for lists which use MemChunk to allocate
1540 * memory but are also part of the MemChunk implementation).
1541 * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1544 #define G_ALLOC_ONLY 1
1545 #define G_ALLOC_AND_FREE 2
1547 GMemChunk* g_mem_chunk_new (gchar *name,
1551 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
1552 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
1553 gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
1554 void g_mem_chunk_free (GMemChunk *mem_chunk,
1556 void g_mem_chunk_clean (GMemChunk *mem_chunk);
1557 void g_mem_chunk_reset (GMemChunk *mem_chunk);
1558 void g_mem_chunk_print (GMemChunk *mem_chunk);
1559 void g_mem_chunk_info (void);
1561 /* Ah yes...we have a "g_blow_chunks" function.
1562 * "g_blow_chunks" simply compresses all the chunks. This operation
1563 * consists of freeing every memory area that should be freed (but
1564 * which we haven't gotten around to doing yet). And, no,
1565 * "g_blow_chunks" doesn't follow the naming scheme, but it is a
1566 * much better name than "g_mem_chunk_clean_all" or something
1569 void g_blow_chunks (void);
1575 #define G_MICROSEC 1000000
1577 GTimer* g_timer_new (void);
1578 void g_timer_destroy (GTimer *timer);
1579 void g_timer_start (GTimer *timer);
1580 void g_timer_stop (GTimer *timer);
1581 void g_timer_reset (GTimer *timer);
1582 gdouble g_timer_elapsed (GTimer *timer,
1583 gulong *microseconds);
1584 void g_usleep (gulong microseconds);
1586 /* String utility functions that modify a string argument or
1587 * return a constant string that must not be freed.
1589 #define G_STR_DELIMITERS "_-|> <."
1590 gchar* g_strdelimit (gchar *string,
1591 const gchar *delimiters,
1592 gchar new_delimiter);
1593 gchar* g_strcanon (gchar *string,
1594 const gchar *valid_chars,
1596 gdouble g_strtod (const gchar *nptr,
1598 gchar* g_strerror (gint errnum);
1599 gchar* g_strsignal (gint signum);
1600 gint g_strcasecmp (const gchar *s1,
1602 gint g_strncasecmp (const gchar *s1,
1605 gchar* g_strdown (gchar *string);
1606 gchar* g_strup (gchar *string);
1607 gchar* g_strreverse (gchar *string);
1608 /* removes leading spaces */
1609 gchar* g_strchug (gchar *string);
1610 /* removes trailing spaces */
1611 gchar* g_strchomp (gchar *string);
1612 /* removes leading & trailing spaces */
1613 #define g_strstrip( string ) g_strchomp (g_strchug (string))
1615 /* String utility functions that return a newly allocated string which
1616 * ought to be freed with g_free from the caller at some point.
1618 gchar* g_strdup (const gchar *str);
1619 gchar* g_strdup_printf (const gchar *format,
1620 ...) G_GNUC_PRINTF (1, 2);
1621 gchar* g_strdup_vprintf (const gchar *format,
1623 gchar* g_strndup (const gchar *str,
1625 gchar* g_strnfill (guint length,
1627 gchar* g_strconcat (const gchar *string1,
1628 ...); /* NULL terminated */
1629 gchar* g_strjoin (const gchar *separator,
1630 ...); /* NULL terminated */
1631 /* Make a copy of a string interpreting C string -style escape
1632 * sequences. Inverse of g_strescape. The recognized sequences are \b
1633 * \f \n \r \t \\ \" and the octal format.
1635 gchar* g_strcompress (const gchar *source);
1637 /* Convert between the operating system (or C runtime)
1638 * representation of file names and UTF-8.
1640 gchar* g_filename_to_utf8 (const gchar *opsysstring);
1641 gchar* g_filename_from_utf8 (const gchar *utf8string);
1643 /* Copy a string escaping nonprintable characters like in C strings.
1644 * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
1645 * to a string containing characters that are not to be escaped.
1647 * Deprecated API: gchar* g_strescape (const gchar *source);
1648 * Luckily this function wasn't used much, using NULL as second parameter
1649 * provides mostly identical semantics.
1651 gchar* g_strescape (const gchar *source,
1652 const gchar *exceptions);
1654 gpointer g_memdup (gconstpointer mem,
1657 /* NULL terminated string arrays.
1658 * g_strsplit() splits up string into max_tokens tokens at delim and
1659 * returns a newly allocated string array.
1660 * g_strjoinv() concatenates all of str_array's strings, sliding in an
1661 * optional separator, the returned string is newly allocated.
1662 * g_strfreev() frees the array itself and all of its strings.
1664 gchar** g_strsplit (const gchar *string,
1665 const gchar *delimiter,
1667 gchar* g_strjoinv (const gchar *separator,
1669 void g_strfreev (gchar **str_array);
1673 /* calculate a string size, guarranteed to fit format + args.
1675 guint g_printf_string_upper_bound (const gchar* format,
1679 /* Retrive static string info
1681 gchar* g_get_user_name (void);
1682 gchar* g_get_real_name (void);
1683 gchar* g_get_home_dir (void);
1684 gchar* g_get_tmp_dir (void);
1685 gchar* g_get_prgname (void);
1686 void g_set_prgname (const gchar *prgname);
1689 /* Miscellaneous utility functions
1691 guint g_parse_debug_string (const gchar *string,
1694 gint g_snprintf (gchar *string,
1696 gchar const *format,
1697 ...) G_GNUC_PRINTF (3, 4);
1698 gint g_vsnprintf (gchar *string,
1700 gchar const *format,
1702 gchar* g_basename (const gchar *file_name);
1703 /* Check if a file name is an absolute path */
1704 gboolean g_path_is_absolute (const gchar *file_name);
1705 /* In case of absolute paths, skip the root part */
1706 gchar* g_path_skip_root (gchar *file_name);
1708 /* strings are newly allocated with g_malloc() */
1709 gchar* g_dirname (const gchar *file_name);
1710 gchar* g_get_current_dir (void);
1712 /* Get the codeset for the current locale */
1713 /* gchar * g_get_codeset (void); */
1715 /* return the environment string for the variable. The returned memory
1716 * must not be freed. */
1717 gchar* g_getenv (const gchar *variable);
1719 /* we use a GLib function as a replacement for ATEXIT, so
1720 * the programmer is not required to check the return value
1721 * (if there is any in the implementation) and doesn't encounter
1722 * missing include files.
1724 void g_atexit (GVoidFunc func);
1729 G_INLINE_FUNC gint g_bit_nth_lsf (guint32 mask,
1733 g_bit_nth_lsf (guint32 mask,
1739 if (mask & (1 << (guint) nth_bit))
1742 while (nth_bit < 32);
1745 #endif /* G_CAN_INLINE */
1747 G_INLINE_FUNC gint g_bit_nth_msf (guint32 mask,
1751 g_bit_nth_msf (guint32 mask,
1759 if (mask & (1 << (guint) nth_bit))
1762 while (nth_bit > 0);
1765 #endif /* G_CAN_INLINE */
1767 G_INLINE_FUNC guint g_bit_storage (guint number);
1770 g_bit_storage (guint number)
1772 register guint n_bits = 0;
1782 #endif /* G_CAN_INLINE */
1786 * elements need to be >= sizeof (gpointer)
1788 G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p,
1792 g_trash_stack_push (GTrashStack **stack_p,
1795 GTrashStack *data = (GTrashStack *) data_p;
1797 data->next = *stack_p;
1800 #endif /* G_CAN_INLINE */
1802 G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p);
1804 G_INLINE_FUNC gpointer
1805 g_trash_stack_pop (GTrashStack **stack_p)
1812 *stack_p = data->next;
1813 /* NULLify private pointer here, most platforms store NULL as
1814 * subsequent 0 bytes
1821 #endif /* G_CAN_INLINE */
1823 G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p);
1825 G_INLINE_FUNC gpointer
1826 g_trash_stack_peek (GTrashStack **stack_p)
1834 #endif /* G_CAN_INLINE */
1836 G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p);
1839 g_trash_stack_height (GTrashStack **stack_p)
1844 for (data = *stack_p; data; data = data->next)
1849 #endif /* G_CAN_INLINE */
1854 GStringChunk* g_string_chunk_new (gint size);
1855 void g_string_chunk_free (GStringChunk *chunk);
1856 gchar* g_string_chunk_insert (GStringChunk *chunk,
1857 const gchar *string);
1858 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
1859 const gchar *string);
1864 GString* g_string_new (const gchar *init);
1865 GString* g_string_sized_new (guint dfl_size);
1866 void g_string_free (GString *string,
1867 gboolean free_segment);
1868 gboolean g_string_equal (const GString *v,
1870 guint g_string_hash (const GString *str);
1871 GString* g_string_assign (GString *string,
1873 GString* g_string_truncate (GString *string,
1875 GString* g_string_insert_len (GString *string,
1879 GString* g_string_append (GString *string,
1881 GString* g_string_append_len (GString *string,
1884 GString* g_string_append_c (GString *string,
1886 GString* g_string_prepend (GString *string,
1888 GString* g_string_prepend_c (GString *string,
1890 GString* g_string_prepend_len (GString *string,
1893 GString* g_string_insert (GString *string,
1896 GString* g_string_insert_c (GString *string,
1899 GString* g_string_erase (GString *string,
1902 GString* g_string_down (GString *string);
1903 GString* g_string_up (GString *string);
1904 void g_string_sprintf (GString *string,
1905 const gchar *format,
1906 ...) G_GNUC_PRINTF (2, 3);
1907 void g_string_sprintfa (GString *string,
1908 const gchar *format,
1909 ...) G_GNUC_PRINTF (2, 3);
1912 /* Resizable arrays, remove fills any cleared spot and shortens the
1913 * array, while preserving the order. remove_fast will distort the
1914 * order by moving the last element to the position of the removed
1917 #define g_array_append_val(a,v) g_array_append_vals (a, &v, 1)
1918 #define g_array_prepend_val(a,v) g_array_prepend_vals (a, &v, 1)
1919 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
1920 #define g_array_index(a,t,i) (((t*) (a)->data) [(i)])
1922 GArray* g_array_new (gboolean zero_terminated,
1924 guint element_size);
1925 GArray* g_array_sized_new (gboolean zero_terminated,
1928 guint reserved_size);
1929 void g_array_free (GArray *array,
1930 gboolean free_segment);
1931 GArray* g_array_append_vals (GArray *array,
1934 GArray* g_array_prepend_vals (GArray *array,
1937 GArray* g_array_insert_vals (GArray *array,
1941 GArray* g_array_set_size (GArray *array,
1943 GArray* g_array_remove_index (GArray *array,
1945 GArray* g_array_remove_index_fast (GArray *array,
1948 /* Resizable pointer array. This interface is much less complicated
1949 * than the above. Add appends appends a pointer. Remove fills any
1950 * cleared spot and shortens the array. remove_fast will again distort
1953 #define g_ptr_array_index(array,index) (array->pdata)[index]
1954 GPtrArray* g_ptr_array_new (void);
1955 GPtrArray* g_ptr_array_sized_new (guint reserved_size);
1956 void g_ptr_array_free (GPtrArray *array,
1958 void g_ptr_array_set_size (GPtrArray *array,
1960 gpointer g_ptr_array_remove_index (GPtrArray *array,
1962 gpointer g_ptr_array_remove_index_fast (GPtrArray *array,
1964 gboolean g_ptr_array_remove (GPtrArray *array,
1966 gboolean g_ptr_array_remove_fast (GPtrArray *array,
1968 void g_ptr_array_add (GPtrArray *array,
1971 /* Byte arrays, an array of guint8. Implemented as a GArray,
1975 GByteArray* g_byte_array_new (void);
1976 GByteArray* g_byte_array_sized_new (guint reserved_size);
1977 void g_byte_array_free (GByteArray *array,
1978 gboolean free_segment);
1979 GByteArray* g_byte_array_append (GByteArray *array,
1982 GByteArray* g_byte_array_prepend (GByteArray *array,
1985 GByteArray* g_byte_array_set_size (GByteArray *array,
1987 GByteArray* g_byte_array_remove_index (GByteArray *array,
1989 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1995 gboolean g_str_equal (gconstpointer v,
1997 guint g_str_hash (gconstpointer v);
1999 gint g_int_equal (gconstpointer v,
2001 guint g_int_hash (gconstpointer v);
2003 /* This "hash" function will just return the key's adress as an
2004 * unsigned integer. Useful for hashing on plain adresses or
2005 * simple integer values.
2006 * passing NULL into g_hash_table_new() as GHashFunc has the
2007 * same effect as passing g_direct_hash().
2009 guint g_direct_hash (gconstpointer v);
2010 gint g_direct_equal (gconstpointer v,
2014 /* Quarks (string<->id association)
2016 GQuark g_quark_try_string (const gchar *string);
2017 GQuark g_quark_from_static_string (const gchar *string);
2018 GQuark g_quark_from_string (const gchar *string);
2019 gchar* g_quark_to_string (GQuark quark);
2024 void g_datalist_init (GData **datalist);
2025 void g_datalist_clear (GData **datalist);
2026 gpointer g_datalist_id_get_data (GData **datalist,
2028 void g_datalist_id_set_data_full (GData **datalist,
2031 GDestroyNotify destroy_func);
2032 gpointer g_datalist_id_remove_no_notify (GData **datalist,
2034 void g_datalist_foreach (GData **datalist,
2035 GDataForeachFunc func,
2036 gpointer user_data);
2037 #define g_datalist_id_set_data(dl, q, d) \
2038 g_datalist_id_set_data_full ((dl), (q), (d), NULL)
2039 #define g_datalist_id_remove_data(dl, q) \
2040 g_datalist_id_set_data ((dl), (q), NULL)
2041 #define g_datalist_get_data(dl, k) \
2042 (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
2043 #define g_datalist_set_data_full(dl, k, d, f) \
2044 g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
2045 #define g_datalist_remove_no_notify(dl, k) \
2046 g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
2047 #define g_datalist_set_data(dl, k, d) \
2048 g_datalist_set_data_full ((dl), (k), (d), NULL)
2049 #define g_datalist_remove_data(dl, k) \
2050 g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
2053 /* Location Associated Keyed Data
2055 void g_dataset_destroy (gconstpointer dataset_location);
2056 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
2058 void g_dataset_id_set_data_full (gconstpointer dataset_location,
2061 GDestroyNotify destroy_func);
2062 gpointer g_dataset_id_remove_no_notify (gconstpointer dataset_location,
2064 void g_dataset_foreach (gconstpointer dataset_location,
2065 GDataForeachFunc func,
2066 gpointer user_data);
2067 #define g_dataset_id_set_data(l, k, d) \
2068 g_dataset_id_set_data_full ((l), (k), (d), NULL)
2069 #define g_dataset_id_remove_data(l, k) \
2070 g_dataset_id_set_data ((l), (k), NULL)
2071 #define g_dataset_get_data(l, k) \
2072 (g_dataset_id_get_data ((l), g_quark_try_string (k)))
2073 #define g_dataset_set_data_full(l, k, d, f) \
2074 g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
2075 #define g_dataset_remove_no_notify(l, k) \
2076 g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
2077 #define g_dataset_set_data(l, k, d) \
2078 g_dataset_set_data_full ((l), (k), (d), NULL)
2079 #define g_dataset_remove_data(l, k) \
2080 g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
2083 /* GScanner: Flexible lexical scanner for general purpose.
2086 /* Character sets */
2087 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2088 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
2089 #define G_CSET_DIGITS "0123456789"
2090 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
2091 "\307\310\311\312\313\314\315\316\317\320"\
2092 "\321\322\323\324\325\326"\
2093 "\330\331\332\333\334\335\336"
2094 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
2095 "\347\350\351\352\353\354\355\356\357\360"\
2096 "\361\362\363\364\365\366"\
2097 "\370\371\372\373\374\375\376\377"
2104 G_ERR_UNEXP_EOF_IN_STRING,
2105 G_ERR_UNEXP_EOF_IN_COMMENT,
2106 G_ERR_NON_DIGIT_IN_CONST,
2109 G_ERR_FLOAT_MALFORMED
2117 G_TOKEN_LEFT_PAREN = '(',
2118 G_TOKEN_RIGHT_PAREN = ')',
2119 G_TOKEN_LEFT_CURLY = '{',
2120 G_TOKEN_RIGHT_CURLY = '}',
2121 G_TOKEN_LEFT_BRACE = '[',
2122 G_TOKEN_RIGHT_BRACE = ']',
2123 G_TOKEN_EQUAL_SIGN = '=',
2124 G_TOKEN_COMMA = ',',
2140 G_TOKEN_IDENTIFIER_NULL,
2142 G_TOKEN_COMMENT_SINGLE,
2143 G_TOKEN_COMMENT_MULTI,
2150 gchar *v_identifier;
2162 struct _GScannerConfig
2166 gchar *cset_skip_characters; /* default: " \t\n" */
2167 gchar *cset_identifier_first;
2168 gchar *cset_identifier_nth;
2169 gchar *cpair_comment_single; /* default: "#\n" */
2171 /* Should symbol lookup work case sensitive?
2173 guint case_sensitive : 1;
2175 /* Boolean values to be adjusted "on the fly"
2176 * to configure scanning behaviour.
2178 guint skip_comment_multi : 1; /* C like comment */
2179 guint skip_comment_single : 1; /* single line comment */
2180 guint scan_comment_multi : 1; /* scan multi line comments? */
2181 guint scan_identifier : 1;
2182 guint scan_identifier_1char : 1;
2183 guint scan_identifier_NULL : 1;
2184 guint scan_symbols : 1;
2185 guint scan_binary : 1;
2186 guint scan_octal : 1;
2187 guint scan_float : 1;
2188 guint scan_hex : 1; /* `0x0ff0' */
2189 guint scan_hex_dollar : 1; /* `$0ff0' */
2190 guint scan_string_sq : 1; /* string: 'anything' */
2191 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
2192 guint numbers_2_int : 1; /* bin, octal, hex => int */
2193 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
2194 guint identifier_2_string : 1;
2195 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
2196 guint symbol_2_token : 1;
2197 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
2204 guint max_parse_errors;
2206 /* g_scanner_error() increments this field */
2209 /* name of input stream, featured by the default message handler */
2210 const gchar *input_name;
2212 /* data pointer for derived structures */
2213 gpointer derived_data;
2215 /* link into the scanner configuration */
2216 GScannerConfig *config;
2218 /* fields filled in after g_scanner_get_next_token() */
2224 /* fields filled in after g_scanner_peek_next_token() */
2225 GTokenType next_token;
2226 GTokenValue next_value;
2228 guint next_position;
2230 /* to be considered private */
2231 GHashTable *symbol_table;
2234 const gchar *text_end;
2238 /* handler function for _warn and _error */
2239 GScannerMsgFunc msg_handler;
2242 GScanner* g_scanner_new (GScannerConfig *config_templ);
2243 void g_scanner_destroy (GScanner *scanner);
2244 void g_scanner_input_file (GScanner *scanner,
2246 void g_scanner_sync_file_offset (GScanner *scanner);
2247 void g_scanner_input_text (GScanner *scanner,
2250 GTokenType g_scanner_get_next_token (GScanner *scanner);
2251 GTokenType g_scanner_peek_next_token (GScanner *scanner);
2252 GTokenType g_scanner_cur_token (GScanner *scanner);
2253 GTokenValue g_scanner_cur_value (GScanner *scanner);
2254 guint g_scanner_cur_line (GScanner *scanner);
2255 guint g_scanner_cur_position (GScanner *scanner);
2256 gboolean g_scanner_eof (GScanner *scanner);
2257 guint g_scanner_set_scope (GScanner *scanner,
2259 void g_scanner_scope_add_symbol (GScanner *scanner,
2261 const gchar *symbol,
2263 void g_scanner_scope_remove_symbol (GScanner *scanner,
2265 const gchar *symbol);
2266 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
2268 const gchar *symbol);
2269 void g_scanner_scope_foreach_symbol (GScanner *scanner,
2272 gpointer user_data);
2273 gpointer g_scanner_lookup_symbol (GScanner *scanner,
2274 const gchar *symbol);
2275 void g_scanner_freeze_symbol_table (GScanner *scanner);
2276 void g_scanner_thaw_symbol_table (GScanner *scanner);
2277 void g_scanner_unexp_token (GScanner *scanner,
2278 GTokenType expected_token,
2279 const gchar *identifier_spec,
2280 const gchar *symbol_spec,
2281 const gchar *symbol_name,
2282 const gchar *message,
2284 void g_scanner_error (GScanner *scanner,
2285 const gchar *format,
2286 ...) G_GNUC_PRINTF (2,3);
2287 void g_scanner_warn (GScanner *scanner,
2288 const gchar *format,
2289 ...) G_GNUC_PRINTF (2,3);
2290 gint g_scanner_stat_mode (const gchar *filename);
2291 /* keep downward source compatibility */
2292 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
2293 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2295 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
2296 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2298 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2299 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2309 GCompletionFunc func;
2315 GCompletion* g_completion_new (GCompletionFunc func);
2316 void g_completion_add_items (GCompletion* cmp,
2318 void g_completion_remove_items (GCompletion* cmp,
2320 void g_completion_clear_items (GCompletion* cmp);
2321 GList* g_completion_complete (GCompletion* cmp,
2323 gchar** new_prefix);
2324 void g_completion_free (GCompletion* cmp);
2329 * Date calculations (not time for now, to be resolved). These are a
2330 * mutant combination of Steffen Beyer's DateCalc routines
2331 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2332 * date routines (written for in-house software). Written by Havoc
2333 * Pennington <hp@pobox.com>
2336 typedef guint16 GDateYear;
2337 typedef guint8 GDateDay; /* day of the month */
2338 typedef struct _GDate GDate;
2339 /* make struct tm known without having to include time.h */
2342 /* enum used to specify order of appearance in parsed date strings */
2350 /* actual week and month values */
2353 G_DATE_BAD_WEEKDAY = 0,
2356 G_DATE_WEDNESDAY = 3,
2357 G_DATE_THURSDAY = 4,
2359 G_DATE_SATURDAY = 6,
2364 G_DATE_BAD_MONTH = 0,
2366 G_DATE_FEBRUARY = 2,
2373 G_DATE_SEPTEMBER = 9,
2374 G_DATE_OCTOBER = 10,
2375 G_DATE_NOVEMBER = 11,
2376 G_DATE_DECEMBER = 12
2379 #define G_DATE_BAD_JULIAN 0U
2380 #define G_DATE_BAD_DAY 0U
2381 #define G_DATE_BAD_YEAR 0U
2383 /* Note: directly manipulating structs is generally a bad idea, but
2384 * in this case it's an *incredibly* bad idea, because all or part
2385 * of this struct can be invalid at any given time. Use the functions,
2386 * or you will get hosed, I promise.
2390 guint julian_days : 32; /* julian days representation - we use a
2391 * bitfield hoping that 64 bit platforms
2392 * will pack this whole struct in one big
2396 guint julian : 1; /* julian is valid */
2397 guint dmy : 1; /* dmy is valid */
2399 /* DMY representation */
2405 /* g_date_new() returns an invalid date, you then have to _set() stuff
2406 * to get a usable object. You can also allocate a GDate statically,
2407 * then call g_date_clear() to initialize.
2409 GDate* g_date_new (void);
2410 GDate* g_date_new_dmy (GDateDay day,
2413 GDate* g_date_new_julian (guint32 julian_day);
2414 void g_date_free (GDate *date);
2416 /* check g_date_valid() after doing an operation that might fail, like
2417 * _parse. Almost all g_date operations are undefined on invalid
2418 * dates (the exceptions are the mutators, since you need those to
2419 * return to validity).
2421 gboolean g_date_valid (GDate *date);
2422 gboolean g_date_valid_day (GDateDay day);
2423 gboolean g_date_valid_month (GDateMonth month);
2424 gboolean g_date_valid_year (GDateYear year);
2425 gboolean g_date_valid_weekday (GDateWeekday weekday);
2426 gboolean g_date_valid_julian (guint32 julian_date);
2427 gboolean g_date_valid_dmy (GDateDay day,
2431 GDateWeekday g_date_weekday (GDate *date);
2432 GDateMonth g_date_month (GDate *date);
2433 GDateYear g_date_year (GDate *date);
2434 GDateDay g_date_day (GDate *date);
2435 guint32 g_date_julian (GDate *date);
2436 guint g_date_day_of_year (GDate *date);
2438 /* First monday/sunday is the start of week 1; if we haven't reached
2439 * that day, return 0. These are not ISO weeks of the year; that
2440 * routine needs to be added.
2441 * these functions return the number of weeks, starting on the
2444 guint g_date_monday_week_of_year (GDate *date);
2445 guint g_date_sunday_week_of_year (GDate *date);
2447 /* If you create a static date struct you need to clear it to get it
2448 * in a sane state before use. You can clear a whole array at
2449 * once with the ndates argument.
2451 void g_date_clear (GDate *date,
2454 /* The parse routine is meant for dates typed in by a user, so it
2455 * permits many formats but tries to catch common typos. If your data
2456 * needs to be strictly validated, it is not an appropriate function.
2458 void g_date_set_parse (GDate *date,
2460 void g_date_set_time (GDate *date,
2462 void g_date_set_month (GDate *date,
2464 void g_date_set_day (GDate *date,
2466 void g_date_set_year (GDate *date,
2468 void g_date_set_dmy (GDate *date,
2472 void g_date_set_julian (GDate *date,
2473 guint32 julian_date);
2474 gboolean g_date_is_first_of_month (GDate *date);
2475 gboolean g_date_is_last_of_month (GDate *date);
2477 /* To go forward by some number of weeks just go forward weeks*7 days */
2478 void g_date_add_days (GDate *date,
2480 void g_date_subtract_days (GDate *date,
2483 /* If you add/sub months while day > 28, the day might change */
2484 void g_date_add_months (GDate *date,
2486 void g_date_subtract_months (GDate *date,
2489 /* If it's feb 29, changing years can move you to the 28th */
2490 void g_date_add_years (GDate *date,
2492 void g_date_subtract_years (GDate *date,
2494 gboolean g_date_is_leap_year (GDateYear year);
2495 guint8 g_date_days_in_month (GDateMonth month,
2497 guint8 g_date_monday_weeks_in_year (GDateYear year);
2498 guint8 g_date_sunday_weeks_in_year (GDateYear year);
2500 /* qsort-friendly (with a cast...) */
2501 gint g_date_compare (GDate *lhs,
2503 void g_date_to_struct_tm (GDate *date,
2506 /* Just like strftime() except you can only use date-related formats.
2507 * Using a time format is undefined.
2509 gsize g_date_strftime (gchar *s,
2511 const gchar *format,
2517 * Indexed Relations. Imagine a really simple table in a
2518 * database. Relations are not ordered. This data type is meant for
2519 * maintaining a N-way mapping.
2521 * g_relation_new() creates a relation with FIELDS fields
2523 * g_relation_destroy() frees all resources
2524 * g_tuples_destroy() frees the result of g_relation_select()
2526 * g_relation_index() indexes relation FIELD with the provided
2527 * equality and hash functions. this must be done before any
2528 * calls to insert are made.
2530 * g_relation_insert() inserts a new tuple. you are expected to
2531 * provide the right number of fields.
2533 * g_relation_delete() deletes all relations with KEY in FIELD
2534 * g_relation_select() returns ...
2535 * g_relation_count() counts ...
2538 GRelation* g_relation_new (gint fields);
2539 void g_relation_destroy (GRelation *relation);
2540 void g_relation_index (GRelation *relation,
2542 GHashFunc hash_func,
2543 GCompareFunc key_compare_func);
2544 void g_relation_insert (GRelation *relation,
2546 gint g_relation_delete (GRelation *relation,
2549 GTuples* g_relation_select (GRelation *relation,
2552 gint g_relation_count (GRelation *relation,
2555 gboolean g_relation_exists (GRelation *relation,
2557 void g_relation_print (GRelation *relation);
2559 void g_tuples_destroy (GTuples *tuples);
2560 gpointer g_tuples_index (GTuples *tuples,
2565 /* GRand - a good and fast random number generator: Mersenne Twister
2566 * see http://www.math.keio.ac.jp/~matumoto/emt.html for more info.
2567 * The range functions return a value in the intervall [min,max).
2568 * int -> [0..2^32-1]
2569 * int_range -> [min..max-1]
2571 * double_range -> [min..max)
2574 GRand* g_rand_new_with_seed (guint32 seed);
2575 GRand* g_rand_new (void);
2576 void g_rand_free (GRand *rand);
2578 void g_rand_set_seed (GRand *rand,
2580 guint32 g_rand_int (GRand *rand);
2581 gint32 g_rand_int_range (GRand *rand,
2584 gdouble g_rand_double (GRand *rand);
2585 gdouble g_rand_double_range (GRand *rand,
2589 void g_random_set_seed (guint32 seed);
2590 guint32 g_random_int (void);
2591 gint32 g_random_int_range (gint32 min,
2593 gdouble g_random_double (void);
2594 gdouble g_random_double_range (gdouble min,
2601 /* This function returns prime numbers spaced by approximately 1.5-2.0
2602 * and is for use in resizing data structures which prefer
2603 * prime-valued sizes. The closest spaced prime function returns the
2604 * next largest prime, or the highest it knows about which is about
2607 guint g_spaced_primes_closest (guint num);
2613 typedef struct _GIOFuncs GIOFuncs;
2629 G_IO_IN GLIB_SYSDEF_POLLIN,
2630 G_IO_OUT GLIB_SYSDEF_POLLOUT,
2631 G_IO_PRI GLIB_SYSDEF_POLLPRI,
2632 G_IO_ERR GLIB_SYSDEF_POLLERR,
2633 G_IO_HUP GLIB_SYSDEF_POLLHUP,
2634 G_IO_NVAL GLIB_SYSDEF_POLLNVAL
2639 guint channel_flags;
2644 typedef gboolean (*GIOFunc) (GIOChannel *source,
2645 GIOCondition condition,
2649 GIOError (*io_read) (GIOChannel *channel,
2653 GIOError (*io_write) (GIOChannel *channel,
2656 guint *bytes_written);
2657 GIOError (*io_seek) (GIOChannel *channel,
2660 void (*io_close) (GIOChannel *channel);
2661 guint (*io_add_watch) (GIOChannel *channel,
2663 GIOCondition condition,
2666 GDestroyNotify notify);
2667 void (*io_free) (GIOChannel *channel);
2670 void g_io_channel_init (GIOChannel *channel);
2671 void g_io_channel_ref (GIOChannel *channel);
2672 void g_io_channel_unref (GIOChannel *channel);
2673 GIOError g_io_channel_read (GIOChannel *channel,
2677 GIOError g_io_channel_write (GIOChannel *channel,
2680 guint *bytes_written);
2681 GIOError g_io_channel_seek (GIOChannel *channel,
2684 void g_io_channel_close (GIOChannel *channel);
2685 guint g_io_add_watch_full (GIOChannel *channel,
2687 GIOCondition condition,
2690 GDestroyNotify notify);
2691 guint g_io_add_watch (GIOChannel *channel,
2692 GIOCondition condition,
2694 gpointer user_data);
2699 typedef struct _GTimeVal GTimeVal;
2700 typedef struct _GSourceFuncs GSourceFuncs;
2701 typedef struct _GMainLoop GMainLoop; /* Opaque */
2708 struct _GSourceFuncs
2710 gboolean (*prepare) (gpointer source_data,
2711 GTimeVal *current_time,
2713 gpointer user_data);
2714 gboolean (*check) (gpointer source_data,
2715 GTimeVal *current_time,
2716 gpointer user_data);
2717 gboolean (*dispatch) (gpointer source_data,
2718 GTimeVal *dispatch_time,
2719 gpointer user_data);
2720 GDestroyNotify destroy;
2723 /* Standard priorities */
2725 #define G_PRIORITY_HIGH -100
2726 #define G_PRIORITY_DEFAULT 0
2727 #define G_PRIORITY_HIGH_IDLE 100
2728 #define G_PRIORITY_DEFAULT_IDLE 200
2729 #define G_PRIORITY_LOW 300
2731 typedef gboolean (*GSourceFunc) (gpointer data);
2733 /* Hooks for adding to the main loop */
2734 guint g_source_add (gint priority,
2735 gboolean can_recurse,
2736 GSourceFuncs *funcs,
2737 gpointer source_data,
2739 GDestroyNotify notify);
2740 gboolean g_source_remove (guint tag);
2741 gboolean g_source_remove_by_user_data (gpointer user_data);
2742 gboolean g_source_remove_by_source_data (gpointer source_data);
2743 gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
2744 gpointer user_data);
2746 void g_get_current_time (GTimeVal *result);
2748 /* Running the main loop */
2749 GMainLoop* g_main_new (gboolean is_running);
2750 void g_main_run (GMainLoop *loop);
2751 void g_main_quit (GMainLoop *loop);
2752 void g_main_destroy (GMainLoop *loop);
2753 gboolean g_main_is_running (GMainLoop *loop);
2755 /* Run a single iteration of the mainloop. If block is FALSE,
2758 gboolean g_main_iteration (gboolean may_block);
2760 /* See if any events are pending */
2761 gboolean g_main_pending (void);
2763 /* Idles and timeouts */
2764 guint g_timeout_add_full (gint priority,
2766 GSourceFunc function,
2768 GDestroyNotify notify);
2769 guint g_timeout_add (guint interval,
2770 GSourceFunc function,
2772 guint g_idle_add (GSourceFunc function,
2774 guint g_idle_add_full (gint priority,
2775 GSourceFunc function,
2777 GDestroyNotify destroy);
2778 gboolean g_idle_remove_by_data (gpointer data);
2782 * System-specific IO and main loop calls
2784 * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
2785 * descriptor as provided by the C runtime) that can be used by
2786 * MsgWaitForMultipleObjects. This does *not* include file handles
2787 * from CreateFile, SOCKETs, nor pipe handles. (But you can use
2788 * WSAEventSelect to signal events when a SOCKET is readable).
2790 * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
2791 * indicate polling for messages. These message queue GPollFDs should
2792 * be added with the g_main_poll_win32_msg_add function.
2794 * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
2795 * (GTK) programs, as GDK itself wants to read messages and convert them
2798 * So, unless you really know what you are doing, it's best not to try
2799 * to use the main loop polling stuff for your own needs on
2800 * Win32. It's really only written for the GIMP's needs so
2804 typedef struct _GPollFD GPollFD;
2805 typedef gint (*GPollFunc) (GPollFD *ufds,
2815 void g_main_add_poll (GPollFD *fd,
2817 void g_main_remove_poll (GPollFD *fd);
2818 void g_main_set_poll_func (GPollFunc func);
2820 /* On Unix, IO channels created with this function for any file
2821 * descriptor or socket.
2823 * On Win32, use this only for plain files opened with the MSVCRT (the
2824 * Microsoft run-time C library) _open(), including file descriptors
2825 * 0, 1 and 2 (corresponding to stdin, stdout and stderr).
2826 * Actually, don't do even that, this code isn't done yet.
2828 * The term file descriptor as used in the context of Win32 refers to
2829 * the emulated Unix-like file descriptors MSVCRT provides.
2831 GIOChannel* g_io_channel_unix_new (int fd);
2832 gint g_io_channel_unix_get_fd (GIOChannel *channel);
2836 GUTILS_C_VAR guint g_pipe_readable_msg;
2838 #define G_WIN32_MSG_HANDLE 19981206
2840 /* This is used to add polling for Windows messages. GDK (GTk+) programs
2841 * should *not* use this. (In fact, I can't think of any program that
2842 * would want to use this, but it's here just for completeness's sake.
2844 void g_main_poll_win32_msg_add(gint priority,
2848 /* An IO channel for Windows messages for window handle hwnd. */
2849 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
2851 /* An IO channel for an anonymous pipe as returned from the MSVCRT
2852 * _pipe(), with no mechanism for the writer to tell the reader when
2853 * there is data in the pipe.
2855 * This is not really implemented yet.
2857 GIOChannel *g_io_channel_win32_new_pipe (int fd);
2859 /* An IO channel for a pipe as returned from the MSVCRT _pipe(), with
2860 * Windows user messages used to signal data in the pipe for the
2863 * fd is the file descriptor. For the write end, peer is the thread id
2864 * of the reader, and peer_fd is his file descriptor for the read end
2867 * This is used by the GIMP, and works.
2869 GIOChannel *g_io_channel_win32_new_pipe_with_wakeups (int fd,
2873 void g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
2877 void g_io_channel_win32_pipe_readable (int fd,
2880 /* Get the C runtime file descriptor of a channel. */
2881 gint g_io_channel_win32_get_fd (GIOChannel *channel);
2883 /* An IO channel for a SOCK_STREAM winsock socket. The parameter is
2884 * actually a SOCKET.
2886 GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
2890 /* Windows emulation stubs for common Unix functions
2893 # define MAXPATHLEN 1024
2900 * To get prototypes for the following POSIXish functions, you have to
2901 * include the indicated non-POSIX headers. The functions are defined
2902 * in OLDNAMES.LIB (MSVC) or -lmoldname-msvc (mingw32).
2904 * getcwd: <direct.h> (MSVC), <io.h> (mingw32)
2905 * getpid: <process.h>
2907 * unlink: <stdio.h> or <io.h>
2908 * open, read, write, lseek, close: <io.h>
2913 /* pipe is not in OLDNAMES.LIB or -lmoldname-msvc. */
2914 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
2916 /* For some POSIX functions that are not provided by the MS runtime,
2917 * we provide emulators in glib, which are prefixed with g_win32_.
2919 # define ftruncate(fd, size) g_win32_ftruncate (fd, size)
2921 /* -lmingw32 also has emulations for these, but we need our own
2922 * for MSVC anyhow, so we might aswell use them always.
2924 # define opendir g_win32_opendir
2925 # define readdir g_win32_readdir
2926 # define rewinddir g_win32_rewinddir
2927 # define closedir g_win32_closedir
2928 # define NAME_MAX 255
2933 gboolean just_opened;
2934 guint find_file_handle;
2935 gpointer find_file_data;
2937 typedef struct DIR DIR;
2940 gchar d_name[NAME_MAX + 1];
2942 /* emulation functions */
2943 extern int g_win32_ftruncate (gint f,
2945 DIR* g_win32_opendir (const gchar *dirname);
2946 struct dirent* g_win32_readdir (DIR *dir);
2947 void g_win32_rewinddir (DIR *dir);
2948 gint g_win32_closedir (DIR *dir);
2950 /* The MS setlocale uses locale names of the form "English_United
2951 * States.1252" etc. We want the Unixish standard form "en", "zh_TW"
2952 * etc. This function gets the current thread locale from Windows and
2953 * returns it as a string of the above form for use in forming file
2954 * names etc. The returned string should be deallocated with g_free().
2956 gchar * g_win32_getlocale (void);
2958 /* Translate a Win32 error code (as returned by GetLastError()) into
2959 * the corresponding message. The returned string should be deallocated
2962 gchar * g_win32_error_message (gint error);
2964 #endif /* G_OS_WIN32 */
2967 /* GLib Thread support
2970 typedef void (*GThreadFunc) (gpointer value);
2974 G_THREAD_PRIORITY_LOW,
2975 G_THREAD_PRIORITY_NORMAL,
2976 G_THREAD_PRIORITY_HIGH,
2977 G_THREAD_PRIORITY_URGENT
2980 typedef struct _GThread GThread;
2983 GThreadPriority priority;
2988 typedef struct _GMutex GMutex;
2989 typedef struct _GCond GCond;
2990 typedef struct _GPrivate GPrivate;
2991 typedef struct _GStaticPrivate GStaticPrivate;
2992 typedef struct _GAsyncQueue GAsyncQueue;
2993 typedef struct _GThreadPool GThreadPool;
2995 typedef struct _GThreadFunctions GThreadFunctions;
2996 struct _GThreadFunctions
2998 GMutex* (*mutex_new) (void);
2999 void (*mutex_lock) (GMutex *mutex);
3000 gboolean (*mutex_trylock) (GMutex *mutex);
3001 void (*mutex_unlock) (GMutex *mutex);
3002 void (*mutex_free) (GMutex *mutex);
3003 GCond* (*cond_new) (void);
3004 void (*cond_signal) (GCond *cond);
3005 void (*cond_broadcast) (GCond *cond);
3006 void (*cond_wait) (GCond *cond,
3008 gboolean (*cond_timed_wait) (GCond *cond,
3010 GTimeVal *end_time);
3011 void (*cond_free) (GCond *cond);
3012 GPrivate* (*private_new) (GDestroyNotify destructor);
3013 gpointer (*private_get) (GPrivate *private_key);
3014 void (*private_set) (GPrivate *private_key,
3016 void (*thread_create) (GThreadFunc thread_func,
3021 GThreadPriority priority,
3023 void (*thread_yield) (void);
3024 void (*thread_join) (gpointer thread);
3025 void (*thread_exit) (void);
3026 void (*thread_set_priority)(gpointer thread,
3027 GThreadPriority priority);
3028 void (*thread_self) (gpointer thread);
3031 GUTILS_C_VAR GThreadFunctions g_thread_functions_for_glib_use;
3032 GUTILS_C_VAR gboolean g_thread_use_default_impl;
3033 GUTILS_C_VAR gboolean g_threads_got_initialized;
3035 /* initializes the mutex/cond/private implementation for glib, might
3036 * only be called once, and must not be called directly or indirectly
3037 * from another glib-function, e.g. as a callback.
3039 void g_thread_init (GThreadFunctions *vtable);
3041 /* internal function for fallback static mutex implementation */
3042 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
3044 /* shorthands for conditional and unconditional function calls */
3045 #define G_THREAD_UF(name, arglist) \
3046 (*g_thread_functions_for_glib_use . name) arglist
3047 #define G_THREAD_CF(name, fail, arg) \
3048 (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
3049 /* keep in mind, all those mutexes and static mutexes are not
3050 * recursive in general, don't rely on that
3052 #define g_thread_supported() (g_threads_got_initialized)
3053 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
3054 #define g_mutex_lock(mutex) G_THREAD_CF (mutex_lock, (void)0, (mutex))
3055 #define g_mutex_trylock(mutex) G_THREAD_CF (mutex_trylock, TRUE, (mutex))
3056 #define g_mutex_unlock(mutex) G_THREAD_CF (mutex_unlock, (void)0, (mutex))
3057 #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex))
3058 #define g_cond_new() G_THREAD_UF (cond_new, ())
3059 #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
3060 #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
3061 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait, (void)0, (cond, \
3063 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
3064 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
3068 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
3069 #define g_private_get(private_key) G_THREAD_CF (private_get, \
3070 ((gpointer)private_key), \
3072 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
3073 (void) (private_key = \
3074 (GPrivate*) (value)), \
3075 (private_key, value))
3076 #define g_thread_yield() G_THREAD_CF (thread_yield, (void)0, ())
3077 #define g_thread_exit() G_THREAD_CF (thread_exit, (void)0, ())
3079 GThread* g_thread_create (GThreadFunc thread_func,
3084 GThreadPriority priority);
3085 GThread* g_thread_self ();
3086 void g_thread_join (GThread* thread);
3087 void g_thread_set_priority (GThread* thread,
3088 GThreadPriority priority);
3090 /* GStaticMutexes can be statically initialized with the value
3091 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
3092 * much easier, than having to explicitly allocate the mutex before
3095 #define g_static_mutex_lock(mutex) \
3096 g_mutex_lock (g_static_mutex_get_mutex (mutex))
3097 #define g_static_mutex_trylock(mutex) \
3098 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
3099 #define g_static_mutex_unlock(mutex) \
3100 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
3102 struct _GStaticPrivate
3106 #define G_STATIC_PRIVATE_INIT { 0 }
3107 gpointer g_static_private_get (GStaticPrivate *private_key);
3108 void g_static_private_set (GStaticPrivate *private_key,
3110 GDestroyNotify notify);
3111 gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
3113 void g_static_private_set_for_thread (GStaticPrivate *private_key,
3116 GDestroyNotify notify);
3118 typedef struct _GStaticRecMutex GStaticRecMutex;
3119 struct _GStaticRecMutex
3123 GSystemThread owner;
3126 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT }
3127 void g_static_rec_mutex_lock (GStaticRecMutex *mutex);
3128 gboolean g_static_rec_mutex_trylock (GStaticRecMutex *mutex);
3129 void g_static_rec_mutex_unlock (GStaticRecMutex *mutex);
3130 void g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
3132 guint g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
3134 typedef struct _GStaticRWLock GStaticRWLock;
3135 struct _GStaticRWLock
3142 guint want_to_write;
3145 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
3147 void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
3148 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
3149 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
3150 void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
3151 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
3152 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
3153 void g_static_rw_lock_free (GStaticRWLock* lock);
3155 /* these are some convenience macros that expand to nothing if GLib
3156 * was configured with --disable-threads. for using StaticMutexes,
3157 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
3158 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
3159 * declare such an globally defined lock. name is a unique identifier
3160 * for the protected varibale or code portion. locking, testing and
3161 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
3162 * G_TRYLOCK() respectively.
3164 extern void glib_dummy_decl (void);
3165 #define G_LOCK_NAME(name) g__ ## name ## _lock
3166 #ifdef G_THREADS_ENABLED
3167 # define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
3168 # define G_LOCK_DEFINE(name) \
3169 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
3170 # define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
3172 # ifdef G_DEBUG_LOCKS
3173 # define G_LOCK(name) G_STMT_START{ \
3174 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3175 "file %s: line %d (%s): locking: %s ", \
3176 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3178 g_static_mutex_lock (&G_LOCK_NAME (name)); \
3180 # define G_UNLOCK(name) G_STMT_START{ \
3181 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3182 "file %s: line %d (%s): unlocking: %s ", \
3183 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3185 g_static_mutex_unlock (&G_LOCK_NAME (name)); \
3187 # define G_TRYLOCK(name) \
3188 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3189 "file %s: line %d (%s): try locking: %s ", \
3190 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3191 #name), g_static_mutex_trylock (&G_LOCK_NAME (name)))
3192 # else /* !G_DEBUG_LOCKS */
3193 # define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
3194 # define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
3195 # define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
3196 # endif /* !G_DEBUG_LOCKS */
3197 #else /* !G_THREADS_ENABLED */
3198 # define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
3199 # define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
3200 # define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
3201 # define G_LOCK(name)
3202 # define G_UNLOCK(name)
3203 # define G_TRYLOCK(name) (TRUE)
3204 #endif /* !G_THREADS_ENABLED */
3206 /* Asyncronous Queues, can be used to communicate between threads
3209 /* Get a new GAsyncQueue with the ref_count 1 */
3210 GAsyncQueue* g_async_queue_new (void);
3212 /* Lock and unlock an GAsyncQueue, all functions lock the queue for
3213 * themselves, but in certain cirumstances you want to hold the lock longer,
3214 * thus you lock the queue, call the *_unlocked functions and unlock it again
3216 void g_async_queue_lock (GAsyncQueue *queue);
3217 void g_async_queue_unlock (GAsyncQueue *queue);
3219 /* Ref and unref the GAsyncQueue. g_async_queue_unref_unlocked makes
3220 * no sense, as after the unreffing the Queue might be gone and can't
3221 * be unlocked. So you have a function to call, if you don't hold the
3222 * lock (g_async_queue_unref) and one to call, when you already hold
3223 * the lock (g_async_queue_unref_and_unlock). After that however, you
3224 * don't hold the lock anymore and the Queue might in fact be
3225 * destroyed, if you unrefed to zero */
3226 void g_async_queue_ref (GAsyncQueue *queue);
3227 void g_async_queue_ref_unlocked (GAsyncQueue *queue);
3228 void g_async_queue_unref (GAsyncQueue *queue);
3229 void g_async_queue_unref_and_unlock (GAsyncQueue *queue);
3231 /* Push data into the async queue. Must not be NULL */
3232 void g_async_queue_push (GAsyncQueue *queue,
3234 void g_async_queue_push_unlocked (GAsyncQueue *queue,
3237 /* Pop data from the async queue, when no data is there, the thread is blocked
3238 * until data arrives */
3239 gpointer g_async_queue_pop (GAsyncQueue *queue);
3240 gpointer g_async_queue_pop_unlocked (GAsyncQueue *queue);
3242 /* Try to pop data, NULL is returned in case of empty queue */
3243 gpointer g_async_queue_try_pop (GAsyncQueue *queue);
3244 gpointer g_async_queue_try_pop_unlocked (GAsyncQueue *queue);
3246 /* Wait for data until at maximum until end_time is reached, NULL is returned
3247 * in case of empty queue*/
3248 gpointer g_async_queue_timed_pop (GAsyncQueue *queue,
3249 GTimeVal *end_time);
3250 gpointer g_async_queue_timed_pop_unlocked (GAsyncQueue *queue,
3251 GTimeVal *end_time);
3253 /* Return the length of the queue, negative values mean, that threads
3254 * are waiting, positve values mean, that there are entries in the
3255 * queue. Actually this function returns the length of the queue minus
3256 * the number of waiting threads, g_async_queue_length == 0 could also
3257 * mean 'n' entries in the queue and 'n' thread waiting, such can
3258 * happen due to locking of the queue or due to scheduling. */
3259 gint g_async_queue_length (GAsyncQueue *queue);
3260 gint g_async_queue_length_unlocked (GAsyncQueue *queue);
3265 /* The real GThreadPool is bigger, so you may only create a thread
3266 * pool with the constructor function */
3272 GThreadPriority priority;
3277 /* Get a thread pool with the function thread_func, at most max_threads may
3278 * run at a time (max_threads == -1 means no limit), stack_size, bound,
3279 * priority like in g_thread_create, exclusive == TRUE means, that the threads
3280 * shouldn't be shared and that they will be prestarted (otherwise they are
3281 * started, as needed) user_data is the 2nd argument to the thread_func */
3282 GThreadPool* g_thread_pool_new (GFunc thread_func,
3286 GThreadPriority priority,
3288 gpointer user_data);
3290 /* Push new data into the thread pool. This task is assigned to a thread later
3291 * (when the maximal number of threads is reached for that pool) or now
3292 * (otherwise). If necessary a new thread will be started. The function
3293 * returns immediatly */
3294 void g_thread_pool_push (GThreadPool *pool,
3297 /* Set the number of threads, which can run concurrently for that pool, -1
3298 * means no limit. 0 means has the effect, that the pool won't process
3299 * requests until the limit is set higher again */
3300 void g_thread_pool_set_max_threads (GThreadPool *pool,
3302 gint g_thread_pool_get_max_threads (GThreadPool *pool);
3304 /* Get the number of threads assigned to that pool. This number doesn't
3305 * necessarily represent the number of working threads in that pool */
3306 guint g_thread_pool_get_num_threads (GThreadPool *pool);
3308 /* Get the number of unprocessed items in the pool */
3309 guint g_thread_pool_unprocessed (GThreadPool *pool);
3311 /* Free the pool, immediate means, that all unprocessed items in the queue
3312 * wont be processed, wait means, that the function doesn't return immediatly,
3313 * but after all threads in the pool are ready processing items. immediate
3314 * does however not mean, that threads are killed. */
3315 void g_thread_pool_free (GThreadPool *pool,
3319 /* Set the maximal number of unused threads before threads will be stopped by
3320 * GLib, -1 means no limit */
3321 void g_thread_pool_set_max_unused_threads (gint max_threads);
3322 gint g_thread_pool_get_max_unused_threads (void);
3323 guint g_thread_pool_get_num_unused_threads (void);
3325 /* Stop all currently unused threads, but leave the limit untouched */
3326 void g_thread_pool_stop_unused_threads (void);
3328 #include <gunicode.h>
3332 #endif /* __cplusplus */
3334 #endif /* __G_LIB_H__ */