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 * inline function bodies have to be special cased with G_CAN_INLINE and a
209 * .c file specific macro to allow one compiled instance with extern linkage
210 * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
212 #ifdef G_IMPLEMENT_INLINES
213 # define G_INLINE_FUNC extern
216 #ifndef G_INLINE_FUNC
217 # define G_CAN_INLINE 1
219 #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
221 # define inline __inline__
222 #elif !defined (G_HAVE_INLINE)
224 # if defined (G_HAVE___INLINE__)
225 # define inline __inline__
226 # elif defined (G_HAVE___INLINE)
227 # define inline __inline
228 # else /* !inline && !__inline__ && !__inline */
229 # define inline /* don't inline, then */
230 # ifndef G_INLINE_FUNC
235 #ifndef G_INLINE_FUNC
236 # if defined (__GNUC__) && (__OPTIMIZE__)
237 # define G_INLINE_FUNC extern inline
238 # elif defined (G_CAN_INLINE) && !defined (__GNUC__)
239 # define G_INLINE_FUNC static inline
240 # else /* can't inline */
241 # define G_INLINE_FUNC extern
244 #endif /* !G_INLINE_FUNC */
247 /* Provide simple macro statement wrappers (adapted from Perl):
248 * G_STMT_START { statements; } G_STMT_END;
249 * can be used as a single statement, as in
250 * if (x) G_STMT_START { ... } G_STMT_END; else ...
252 * For gcc we will wrap the statements within `({' and `})' braces.
253 * For SunOS they will be wrapped within `if (1)' and `else (void) 0',
254 * and otherwise within `do' and `while (0)'.
256 #if !(defined (G_STMT_START) && defined (G_STMT_END))
257 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
258 # define G_STMT_START (void)(
259 # define G_STMT_END )
261 # if (defined (sun) || defined (__sun__))
262 # define G_STMT_START if (1)
263 # define G_STMT_END else (void)0
265 # define G_STMT_START do
266 # define G_STMT_END while (0)
272 /* Provide macros to feature the GCC function attribute.
274 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
275 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
276 __attribute__((format (printf, format_idx, arg_idx)))
277 #define G_GNUC_SCANF( format_idx, arg_idx ) \
278 __attribute__((format (scanf, format_idx, arg_idx)))
279 #define G_GNUC_FORMAT( arg_idx ) \
280 __attribute__((format_arg (arg_idx)))
281 #define G_GNUC_NORETURN \
282 __attribute__((noreturn))
283 #define G_GNUC_CONST \
284 __attribute__((const))
285 #define G_GNUC_UNUSED \
286 __attribute__((unused))
287 #else /* !__GNUC__ */
288 #define G_GNUC_PRINTF( format_idx, arg_idx )
289 #define G_GNUC_SCANF( format_idx, arg_idx )
290 #define G_GNUC_FORMAT( arg_idx )
291 #define G_GNUC_NORETURN
293 #define G_GNUC_UNUSED
294 #endif /* !__GNUC__ */
296 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
297 * macros, so we can refer to them as strings unconditionally.
300 #define G_GNUC_FUNCTION __FUNCTION__
301 #define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
302 #else /* !__GNUC__ */
303 #define G_GNUC_FUNCTION ""
304 #define G_GNUC_PRETTY_FUNCTION ""
305 #endif /* !__GNUC__ */
307 /* we try to provide a usefull equivalent for ATEXIT if it is
308 * not defined, but use is actually abandoned. people should
309 * use g_atexit() instead.
312 # define ATEXIT(proc) g_ATEXIT(proc)
314 # define G_NATIVE_ATEXIT
317 /* Hacker macro to place breakpoints for elected machines.
318 * Actual use is strongly deprecated of course ;)
320 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
321 # define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
322 #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
323 # define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
324 #else /* !__i386__ && !__alpha__ */
325 # define G_BREAKPOINT() G_STMT_START{ raise (5 /* SIGTRAP */); }G_STMT_END
326 #endif /* __i386__ */
328 /* g_alloca handling */
329 #ifdef GLIB_HAVE_ALLOCA_H
335 /* glibc already does this for us */
337 # define alloca(size) __builtin_alloca (size)
342 # define alloca _alloca
347 # ifndef alloca /* predefined by HP cc +Olibcalls */
354 #define g_alloca(size) alloca (size)
355 /* End g_alloca handling */
357 /* Provide macros for easily allocating memory. The macros
358 * will cast the allocated memory to the specified type
359 * in order to avoid compiler warnings. (Makes the code neater).
363 # define g_new(type, count) (ALLOC (type, count))
364 # define g_new0(type, count) (CALLOC (type, count))
365 # define g_renew(type, mem, count) (REALLOC (mem, type, count))
366 #else /* __DMALLOC_H__ */
367 # define g_new(type, count) \
368 ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
369 # define g_new0(type, count) \
370 ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
371 # define g_renew(type, mem, count) \
372 ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
373 #endif /* __DMALLOC_H__ */
375 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
376 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
378 sizeof (type) * (pre_alloc), \
381 #define g_chunk_new(type, chunk) ( \
382 (type *) g_mem_chunk_alloc (chunk) \
384 #define g_chunk_new0(type, chunk) ( \
385 (type *) g_mem_chunk_alloc0 (chunk) \
387 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
388 g_mem_chunk_free ((mem_chunk), (mem)); \
392 /* Provide macros for error handling. The "assert" macros will
393 * exit on failure. The "return" macros will exit the current
394 * function. Two different definitions are given for the macros
395 * if G_DISABLE_ASSERT is not defined, in order to support gcc's
396 * __PRETTY_FUNCTION__ capability.
399 #ifdef G_DISABLE_ASSERT
401 #define g_assert(expr)
402 #define g_assert_not_reached()
404 #else /* !G_DISABLE_ASSERT */
408 #define g_assert(expr) G_STMT_START{ \
410 g_log (G_LOG_DOMAIN, \
412 "file %s: line %d (%s): assertion failed: (%s)", \
415 __PRETTY_FUNCTION__, \
418 #define g_assert_not_reached() G_STMT_START{ \
419 g_log (G_LOG_DOMAIN, \
421 "file %s: line %d (%s): should not be reached", \
424 __PRETTY_FUNCTION__); }G_STMT_END
426 #else /* !__GNUC__ */
428 #define g_assert(expr) G_STMT_START{ \
430 g_log (G_LOG_DOMAIN, \
432 "file %s: line %d: assertion failed: (%s)", \
437 #define g_assert_not_reached() G_STMT_START{ \
438 g_log (G_LOG_DOMAIN, \
440 "file %s: line %d: should not be reached", \
442 __LINE__); }G_STMT_END
444 #endif /* __GNUC__ */
446 #endif /* !G_DISABLE_ASSERT */
449 #ifdef G_DISABLE_CHECKS
451 #define g_return_if_fail(expr)
452 #define g_return_val_if_fail(expr,val)
453 #define g_return_if_reached() return
454 #define g_return_val_if_reached(val) return (val)
456 #else /* !G_DISABLE_CHECKS */
460 #define g_return_if_fail(expr) G_STMT_START{ \
463 g_log (G_LOG_DOMAIN, \
464 G_LOG_LEVEL_CRITICAL, \
465 "file %s: line %d (%s): assertion `%s' failed", \
468 __PRETTY_FUNCTION__, \
473 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
476 g_log (G_LOG_DOMAIN, \
477 G_LOG_LEVEL_CRITICAL, \
478 "file %s: line %d (%s): assertion `%s' failed", \
481 __PRETTY_FUNCTION__, \
486 #define g_return_if_reached() G_STMT_START{ \
487 g_log (G_LOG_DOMAIN, \
488 G_LOG_LEVEL_CRITICAL, \
489 "file %s: line %d (%s): should not be reached", \
492 __PRETTY_FUNCTION__); \
495 #define g_return_val_if_reached(val) G_STMT_START{ \
496 g_log (G_LOG_DOMAIN, \
497 G_LOG_LEVEL_CRITICAL, \
498 "file %s: line %d (%s): should not be reached", \
501 __PRETTY_FUNCTION__); \
502 return (val); }G_STMT_END
504 #else /* !__GNUC__ */
506 #define g_return_if_fail(expr) G_STMT_START{ \
509 g_log (G_LOG_DOMAIN, \
510 G_LOG_LEVEL_CRITICAL, \
511 "file %s: line %d: assertion `%s' failed", \
518 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
521 g_log (G_LOG_DOMAIN, \
522 G_LOG_LEVEL_CRITICAL, \
523 "file %s: line %d: assertion `%s' failed", \
530 #define g_return_if_reached() G_STMT_START{ \
531 g_log (G_LOG_DOMAIN, \
532 G_LOG_LEVEL_CRITICAL, \
533 "file %s: line %d: should not be reached", \
538 #define g_return_val_if_reached(val) G_STMT_START{ \
539 g_log (G_LOG_DOMAIN, \
540 G_LOG_LEVEL_CRITICAL, \
541 "file %s: line %d: should not be reached", \
544 return (val); }G_STMT_END
546 #endif /* !__GNUC__ */
548 #endif /* !G_DISABLE_CHECKS */
551 /* Provide type definitions for commonly used types.
552 * These are useful because a "gint8" can be adjusted
553 * to be 1 byte (8 bits) on all platforms. Similarly and
554 * more importantly, "gint32" can be adjusted to be
555 * 4 bytes (32 bits) on all platforms.
559 typedef short gshort;
562 typedef gint gboolean;
563 typedef gchar* gstring;
565 typedef unsigned char guchar;
566 typedef unsigned short gushort;
567 typedef unsigned long gulong;
568 typedef unsigned int guint;
570 typedef float gfloat;
571 typedef double gdouble;
573 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
574 * Since gldouble isn't used anywhere, just disable it for now */
577 #ifdef HAVE_LONG_DOUBLE
578 typedef long double gldouble;
579 #else /* HAVE_LONG_DOUBLE */
580 typedef double gldouble;
581 #endif /* HAVE_LONG_DOUBLE */
584 typedef void* gpointer;
585 typedef const void *gconstpointer;
588 typedef guint32 GQuark;
589 typedef gint32 GTime;
592 /* Portable endian checks and conversions
594 * glibconfig.h defines G_BYTE_ORDER which expands to one of
597 #define G_LITTLE_ENDIAN 1234
598 #define G_BIG_ENDIAN 4321
599 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
602 /* Basic bit swapping functions
604 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
605 (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
606 (((guint16) (val) & (guint16) 0xff00U) >> 8)))
607 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((guint32) ( \
608 (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
609 (((guint32) (val) & (guint32) 0x0000ff00U) << 8) | \
610 (((guint32) (val) & (guint32) 0x00ff0000U) >> 8) | \
611 (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
613 /* Intel specific stuff for speed
615 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
616 # define GUINT16_SWAP_LE_BE_X86(val) \
618 ({ register guint16 __v; \
619 if (__builtin_constant_p (val)) \
620 __v = GUINT16_SWAP_LE_BE_CONSTANT (val); \
622 __asm__ __const__ ("rorw $8, %w0" \
624 : "0" ((guint16) (val))); \
626 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
627 # if !defined(__i486__) && !defined(__i586__) \
628 && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
629 # define GUINT32_SWAP_LE_BE_X86(val) \
631 ({ register guint32 __v; \
632 if (__builtin_constant_p (val)) \
633 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
635 __asm__ __const__ ("rorw $8, %w0\n\t" \
639 : "0" ((guint32) (val))); \
641 # else /* 486 and higher has bswap */
642 # define GUINT32_SWAP_LE_BE_X86(val) \
644 ({ register guint32 __v; \
645 if (__builtin_constant_p (val)) \
646 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
648 __asm__ __const__ ("bswap %0" \
650 : "0" ((guint32) (val))); \
652 # endif /* processor specific 32-bit stuff */
653 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
654 #else /* !__i386__ */
655 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
656 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
657 #endif /* __i386__ */
660 # define GUINT64_SWAP_LE_BE_CONSTANT(val) ((guint64) ( \
661 (((guint64) (val) & \
662 (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) | \
663 (((guint64) (val) & \
664 (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) | \
665 (((guint64) (val) & \
666 (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) | \
667 (((guint64) (val) & \
668 (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) << 8) | \
669 (((guint64) (val) & \
670 (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >> 8) | \
671 (((guint64) (val) & \
672 (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) | \
673 (((guint64) (val) & \
674 (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) | \
675 (((guint64) (val) & \
676 (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
677 # if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
678 # define GUINT64_SWAP_LE_BE_X86(val) \
680 ({ union { guint64 __ll; \
681 guint32 __l[2]; } __r; \
682 if (__builtin_constant_p (val)) \
683 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val); \
686 union { guint64 __ll; \
687 guint32 __l[2]; } __w; \
688 __w.__ll = ((guint64) val); \
689 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
690 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
693 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
694 # else /* !__i386__ */
695 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
699 #define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
700 #define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
701 #define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
702 (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
703 (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
704 #define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
705 (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
706 (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
708 /* The G*_TO_?E() macros are defined in glibconfig.h.
709 * The transformation is symmetric, so the FROM just maps to the TO.
711 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
712 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
713 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
714 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
715 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
716 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
717 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
718 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
721 #define GINT64_FROM_LE(val) (GINT64_TO_LE (val))
722 #define GUINT64_FROM_LE(val) (GUINT64_TO_LE (val))
723 #define GINT64_FROM_BE(val) (GINT64_TO_BE (val))
724 #define GUINT64_FROM_BE(val) (GUINT64_TO_BE (val))
727 #define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
728 #define GULONG_FROM_LE(val) (GULONG_TO_LE (val))
729 #define GLONG_FROM_BE(val) (GLONG_TO_BE (val))
730 #define GULONG_FROM_BE(val) (GULONG_TO_BE (val))
732 #define GINT_FROM_LE(val) (GINT_TO_LE (val))
733 #define GUINT_FROM_LE(val) (GUINT_TO_LE (val))
734 #define GINT_FROM_BE(val) (GINT_TO_BE (val))
735 #define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
738 /* Portable versions of host-network order stuff
740 #define g_ntohl(val) (GUINT32_FROM_BE (val))
741 #define g_ntohs(val) (GUINT16_FROM_BE (val))
742 #define g_htonl(val) (GUINT32_TO_BE (val))
743 #define g_htons(val) (GUINT16_TO_BE (val))
747 * we prefix variable declarations so they can
748 * properly get exported in windows dlls.
751 # ifdef GLIB_COMPILATION
752 # define GLIB_VAR __declspec(dllexport)
753 # else /* !GLIB_COMPILATION */
754 # define GLIB_VAR extern __declspec(dllimport)
755 # endif /* !GLIB_COMPILATION */
756 #else /* !G_OS_WIN32 */
757 # define GLIB_VAR extern
758 #endif /* !G_OS_WIN32 */
760 GLIB_VAR const guint glib_major_version;
761 GLIB_VAR const guint glib_minor_version;
762 GLIB_VAR const guint glib_micro_version;
763 GLIB_VAR const guint glib_interface_age;
764 GLIB_VAR const guint glib_binary_age;
766 #define GLIB_CHECK_VERSION(major,minor,micro) \
767 (GLIB_MAJOR_VERSION > (major) || \
768 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
769 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
770 GLIB_MICRO_VERSION >= (micro)))
772 /* Forward declarations of glib types.
774 typedef struct _GAllocator GAllocator;
775 typedef struct _GArray GArray;
776 typedef struct _GByteArray GByteArray;
777 typedef struct _GCache GCache;
778 typedef struct _GCompletion GCompletion;
779 typedef struct _GData GData;
780 typedef struct _GDebugKey GDebugKey;
781 typedef union _GDoubleIEEE754 GDoubleIEEE754;
782 typedef union _GFloatIEEE754 GFloatIEEE754;
783 typedef struct _GHashTable GHashTable;
784 typedef struct _GHook GHook;
785 typedef struct _GHookList GHookList;
786 typedef struct _GList GList;
787 typedef struct _GMemChunk GMemChunk;
788 typedef struct _GNode GNode;
789 typedef struct _GPtrArray GPtrArray;
790 typedef struct _GQueue GQueue;
791 typedef struct _GRand GRand;
792 typedef struct _GRelation GRelation;
793 typedef struct _GScanner GScanner;
794 typedef struct _GScannerConfig GScannerConfig;
795 typedef struct _GSList GSList;
796 typedef struct _GString GString;
797 typedef struct _GStringChunk GStringChunk;
798 typedef struct _GTimer GTimer;
799 typedef struct _GTrashStack GTrashStack;
800 typedef struct _GTree GTree;
801 typedef struct _GTuples GTuples;
802 typedef union _GTokenValue GTokenValue;
803 typedef struct _GIOChannel GIOChannel;
805 /* Tree traverse flags */
808 G_TRAVERSE_LEAFS = 1 << 0,
809 G_TRAVERSE_NON_LEAFS = 1 << 1,
810 G_TRAVERSE_ALL = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
811 G_TRAVERSE_MASK = 0x03
814 /* Tree traverse orders */
823 /* Log level shift offset for user defined
824 * log levels (0-7 are used by GLib).
826 #define G_LOG_LEVEL_USER_SHIFT (8)
828 /* Glib log levels and flags.
833 G_LOG_FLAG_RECURSION = 1 << 0,
834 G_LOG_FLAG_FATAL = 1 << 1,
836 /* GLib log levels */
837 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
838 G_LOG_LEVEL_CRITICAL = 1 << 3,
839 G_LOG_LEVEL_WARNING = 1 << 4,
840 G_LOG_LEVEL_MESSAGE = 1 << 5,
841 G_LOG_LEVEL_INFO = 1 << 6,
842 G_LOG_LEVEL_DEBUG = 1 << 7,
844 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
847 /* GLib log levels that are considered fatal by default */
848 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
851 typedef gpointer (*GCacheNewFunc) (gpointer key);
852 typedef gpointer (*GCacheDupFunc) (gpointer value);
853 typedef void (*GCacheDestroyFunc) (gpointer value);
854 typedef gint (*GCompareFunc) (gconstpointer a,
856 typedef gchar* (*GCompletionFunc) (gpointer);
857 typedef void (*GDestroyNotify) (gpointer data);
858 typedef void (*GDataForeachFunc) (GQuark key_id,
861 typedef void (*GFunc) (gpointer data,
863 typedef guint (*GHashFunc) (gconstpointer key);
864 typedef void (*GFreeFunc) (gpointer data);
865 typedef void (*GHFunc) (gpointer key,
868 typedef gboolean (*GHRFunc) (gpointer key,
871 typedef gint (*GHookCompareFunc) (GHook *new_hook,
873 typedef gboolean (*GHookFindFunc) (GHook *hook,
875 typedef void (*GHookMarshaller) (GHook *hook,
877 typedef gboolean (*GHookCheckMarshaller) (GHook *hook,
879 typedef void (*GHookFunc) (gpointer data);
880 typedef gboolean (*GHookCheckFunc) (gpointer data);
881 typedef void (*GHookFreeFunc) (GHookList *hook_list,
883 typedef void (*GLogFunc) (const gchar *log_domain,
884 GLogLevelFlags log_level,
885 const gchar *message,
887 typedef gboolean (*GNodeTraverseFunc) (GNode *node,
889 typedef void (*GNodeForeachFunc) (GNode *node,
891 typedef void (*GScannerMsgFunc) (GScanner *scanner,
894 typedef gint (*GTraverseFunc) (gpointer key,
897 typedef void (*GVoidFunc) (void);
962 #endif /* __cplusplus */
968 #endif /* __cplusplus */
970 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
973 * +--------+---------------+---------------+
974 * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
975 * +--------+---------------+---------------+
976 * B0------------------->B1------->B2-->B3-->
978 * IEEE Standard 754 Double Precision Storage Format (gdouble):
980 * 63 62 52 51 32 31 0
981 * +--------+----------------+----------------+ +---------------+
982 * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
983 * +--------+----------------+----------------+ +---------------+
984 * B0--------------->B1---------->B2--->B3----> B4->B5->B6->B7->
986 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
987 #define G_IEEE754_FLOAT_BIAS (127)
988 #define G_IEEE754_DOUBLE_BIAS (1023)
989 /* multiply with base2 exponent to get base10 exponent (nomal numbers) */
990 #define G_LOG_2_BASE_10 (0.30102999566398119521)
991 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
997 guint biased_exponent : 8;
1001 union _GDoubleIEEE754
1005 guint mantissa_low : 32;
1006 guint mantissa_high : 20;
1007 guint biased_exponent : 11;
1011 #elif G_BYTE_ORDER == G_BIG_ENDIAN
1012 union _GFloatIEEE754
1017 guint biased_exponent : 8;
1018 guint mantissa : 23;
1021 union _GDoubleIEEE754
1026 guint biased_exponent : 11;
1027 guint mantissa_high : 20;
1028 guint mantissa_low : 32;
1031 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
1032 #error unknown ENDIAN type
1033 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
1036 /* Doubly linked lists
1038 void g_list_push_allocator (GAllocator *allocator);
1039 void g_list_pop_allocator (void);
1040 GList* g_list_alloc (void);
1041 void g_list_free (GList *list);
1042 void g_list_free_1 (GList *list);
1043 GList* g_list_append (GList *list,
1045 GList* g_list_prepend (GList *list,
1047 GList* g_list_insert (GList *list,
1050 GList* g_list_insert_sorted (GList *list,
1053 GList* g_list_concat (GList *list1,
1055 GList* g_list_remove (GList *list,
1056 gconstpointer data);
1057 GList* g_list_remove_link (GList *list,
1059 GList* g_list_delete_link (GList *list,
1061 GList* g_list_reverse (GList *list);
1062 GList* g_list_copy (GList *list);
1063 GList* g_list_nth (GList *list,
1065 GList* g_list_find (GList *list,
1066 gconstpointer data);
1067 GList* g_list_find_custom (GList *list,
1070 gint g_list_position (GList *list,
1072 gint g_list_index (GList *list,
1073 gconstpointer data);
1074 GList* g_list_last (GList *list);
1075 GList* g_list_first (GList *list);
1076 guint g_list_length (GList *list);
1077 void g_list_foreach (GList *list,
1079 gpointer user_data);
1080 GList* g_list_sort (GList *list,
1081 GCompareFunc compare_func);
1082 gpointer g_list_nth_data (GList *list,
1084 #define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
1085 #define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
1088 /* Singly linked lists
1090 void g_slist_push_allocator (GAllocator *allocator);
1091 void g_slist_pop_allocator (void);
1092 GSList* g_slist_alloc (void);
1093 void g_slist_free (GSList *list);
1094 void g_slist_free_1 (GSList *list);
1095 GSList* g_slist_append (GSList *list,
1097 GSList* g_slist_prepend (GSList *list,
1099 GSList* g_slist_insert (GSList *list,
1102 GSList* g_slist_insert_sorted (GSList *list,
1105 GSList* g_slist_insert_before (GSList *slist,
1108 GSList* g_slist_concat (GSList *list1,
1110 GSList* g_slist_remove (GSList *list,
1111 gconstpointer data);
1112 GSList* g_slist_remove_link (GSList *list,
1114 GSList* g_slist_delete_link (GSList *list,
1116 GSList* g_slist_reverse (GSList *list);
1117 GSList* g_slist_copy (GSList *list);
1118 GSList* g_slist_nth (GSList *list,
1120 GSList* g_slist_find (GSList *list,
1121 gconstpointer data);
1122 GSList* g_slist_find_custom (GSList *list,
1125 gint g_slist_position (GSList *list,
1127 gint g_slist_index (GSList *list,
1128 gconstpointer data);
1129 GSList* g_slist_last (GSList *list);
1130 guint g_slist_length (GSList *list);
1131 void g_slist_foreach (GSList *list,
1133 gpointer user_data);
1134 GSList* g_slist_sort (GSList *list,
1135 GCompareFunc compare_func);
1136 gpointer g_slist_nth_data (GSList *list,
1138 #define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
1143 GQueue* g_queue_new (void);
1144 void g_queue_free (GQueue *queue);
1145 void g_queue_push_head (GQueue *queue,
1147 void g_queue_push_tail (GQueue *queue,
1149 gpointer g_queue_pop_head (GQueue *queue);
1150 gpointer g_queue_pop_tail (GQueue *queue);
1151 gboolean g_queue_is_empty (GQueue *queue);
1152 gpointer g_queue_peek_head (GQueue *queue);
1153 gpointer g_queue_peek_tail (GQueue *queue);
1154 void g_queue_push_head_link (GQueue *queue,
1156 void g_queue_push_tail_link (GQueue *queue,
1158 GList* g_queue_pop_head_link (GQueue *queue);
1159 GList* g_queue_pop_tail_link (GQueue *queue);
1163 GHashTable* g_hash_table_new (GHashFunc hash_func,
1164 GCompareFunc key_compare_func);
1165 void g_hash_table_destroy (GHashTable *hash_table);
1166 void g_hash_table_insert (GHashTable *hash_table,
1169 void g_hash_table_remove (GHashTable *hash_table,
1171 gpointer g_hash_table_lookup (GHashTable *hash_table,
1173 gboolean g_hash_table_lookup_extended(GHashTable *hash_table,
1174 gconstpointer lookup_key,
1177 void g_hash_table_foreach (GHashTable *hash_table,
1179 gpointer user_data);
1180 guint g_hash_table_foreach_remove (GHashTable *hash_table,
1182 gpointer user_data);
1183 guint g_hash_table_size (GHashTable *hash_table);
1185 /* The following two functions are deprecated and will be removed in
1186 * the next major release. They do no good. */
1187 void g_hash_table_freeze (GHashTable *hash_table);
1188 void g_hash_table_thaw (GHashTable *hash_table);
1192 GCache* g_cache_new (GCacheNewFunc value_new_func,
1193 GCacheDestroyFunc value_destroy_func,
1194 GCacheDupFunc key_dup_func,
1195 GCacheDestroyFunc key_destroy_func,
1196 GHashFunc hash_key_func,
1197 GHashFunc hash_value_func,
1198 GCompareFunc key_compare_func);
1199 void g_cache_destroy (GCache *cache);
1200 gpointer g_cache_insert (GCache *cache,
1202 void g_cache_remove (GCache *cache,
1203 gconstpointer value);
1204 void g_cache_key_foreach (GCache *cache,
1206 gpointer user_data);
1207 void g_cache_value_foreach (GCache *cache,
1209 gpointer user_data);
1212 /* Balanced binary trees
1214 GTree* g_tree_new (GCompareFunc key_compare_func);
1215 void g_tree_destroy (GTree *tree);
1216 void g_tree_insert (GTree *tree,
1219 void g_tree_remove (GTree *tree,
1221 gpointer g_tree_lookup (GTree *tree,
1223 void g_tree_traverse (GTree *tree,
1224 GTraverseFunc traverse_func,
1225 GTraverseType traverse_type,
1227 gpointer g_tree_search (GTree *tree,
1228 GCompareFunc search_func,
1229 gconstpointer data);
1230 gint g_tree_height (GTree *tree);
1231 gint g_tree_nnodes (GTree *tree);
1235 /* N-way tree implementation
1246 #define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
1247 ((GNode*) (node))->prev == NULL && \
1248 ((GNode*) (node))->next == NULL)
1249 #define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
1251 void g_node_push_allocator (GAllocator *allocator);
1252 void g_node_pop_allocator (void);
1253 GNode* g_node_new (gpointer data);
1254 void g_node_destroy (GNode *root);
1255 void g_node_unlink (GNode *node);
1256 GNode* g_node_copy (GNode *node);
1257 GNode* g_node_insert (GNode *parent,
1260 GNode* g_node_insert_before (GNode *parent,
1263 GNode* g_node_prepend (GNode *parent,
1265 guint g_node_n_nodes (GNode *root,
1266 GTraverseFlags flags);
1267 GNode* g_node_get_root (GNode *node);
1268 gboolean g_node_is_ancestor (GNode *node,
1270 guint g_node_depth (GNode *node);
1271 GNode* g_node_find (GNode *root,
1272 GTraverseType order,
1273 GTraverseFlags flags,
1276 /* convenience macros */
1277 #define g_node_append(parent, node) \
1278 g_node_insert_before ((parent), NULL, (node))
1279 #define g_node_insert_data(parent, position, data) \
1280 g_node_insert ((parent), (position), g_node_new (data))
1281 #define g_node_insert_data_before(parent, sibling, data) \
1282 g_node_insert_before ((parent), (sibling), g_node_new (data))
1283 #define g_node_prepend_data(parent, data) \
1284 g_node_prepend ((parent), g_node_new (data))
1285 #define g_node_append_data(parent, data) \
1286 g_node_insert_before ((parent), NULL, g_node_new (data))
1288 /* traversal function, assumes that `node' is root
1289 * (only traverses `node' and its subtree).
1290 * this function is just a high level interface to
1291 * low level traversal functions, optimized for speed.
1293 void g_node_traverse (GNode *root,
1294 GTraverseType order,
1295 GTraverseFlags flags,
1297 GNodeTraverseFunc func,
1300 /* return the maximum tree height starting with `node', this is an expensive
1301 * operation, since we need to visit all nodes. this could be shortened by
1302 * adding `guint height' to struct _GNode, but then again, this is not very
1303 * often needed, and would make g_node_insert() more time consuming.
1305 guint g_node_max_height (GNode *root);
1307 void g_node_children_foreach (GNode *node,
1308 GTraverseFlags flags,
1309 GNodeForeachFunc func,
1311 void g_node_reverse_children (GNode *node);
1312 guint g_node_n_children (GNode *node);
1313 GNode* g_node_nth_child (GNode *node,
1315 GNode* g_node_last_child (GNode *node);
1316 GNode* g_node_find_child (GNode *node,
1317 GTraverseFlags flags,
1319 gint g_node_child_position (GNode *node,
1321 gint g_node_child_index (GNode *node,
1324 GNode* g_node_first_sibling (GNode *node);
1325 GNode* g_node_last_sibling (GNode *node);
1327 #define g_node_prev_sibling(node) ((node) ? \
1328 ((GNode*) (node))->prev : NULL)
1329 #define g_node_next_sibling(node) ((node) ? \
1330 ((GNode*) (node))->next : NULL)
1331 #define g_node_first_child(node) ((node) ? \
1332 ((GNode*) (node))->children : NULL)
1335 /* Callback maintenance functions
1337 #define G_HOOK_FLAG_USER_SHIFT (4)
1340 G_HOOK_FLAG_ACTIVE = 1 << 0,
1341 G_HOOK_FLAG_IN_CALL = 1 << 1,
1342 G_HOOK_FLAG_MASK = 0x0f
1345 #define G_HOOK_DEFERRED_DESTROY ((GHookFreeFunc) 0x01)
1353 GMemChunk *hook_memchunk;
1354 GHookFreeFunc hook_free; /* virtual function */
1355 GHookFreeFunc hook_destroy; /* virtual function */
1367 GDestroyNotify destroy;
1370 #define G_HOOK_ACTIVE(hook) ((((GHook*) hook)->flags & \
1371 G_HOOK_FLAG_ACTIVE) != 0)
1372 #define G_HOOK_IN_CALL(hook) ((((GHook*) hook)->flags & \
1373 G_HOOK_FLAG_IN_CALL) != 0)
1374 #define G_HOOK_IS_VALID(hook) (((GHook*) hook)->hook_id != 0 && \
1375 G_HOOK_ACTIVE (hook))
1376 #define G_HOOK_IS_UNLINKED(hook) (((GHook*) hook)->next == NULL && \
1377 ((GHook*) hook)->prev == NULL && \
1378 ((GHook*) hook)->hook_id == 0 && \
1379 ((GHook*) hook)->ref_count == 0)
1381 void g_hook_list_init (GHookList *hook_list,
1383 void g_hook_list_clear (GHookList *hook_list);
1384 GHook* g_hook_alloc (GHookList *hook_list);
1385 void g_hook_free (GHookList *hook_list,
1387 void g_hook_ref (GHookList *hook_list,
1389 void g_hook_unref (GHookList *hook_list,
1391 gboolean g_hook_destroy (GHookList *hook_list,
1393 void g_hook_destroy_link (GHookList *hook_list,
1395 void g_hook_prepend (GHookList *hook_list,
1397 void g_hook_insert_before (GHookList *hook_list,
1400 void g_hook_insert_sorted (GHookList *hook_list,
1402 GHookCompareFunc func);
1403 GHook* g_hook_get (GHookList *hook_list,
1405 GHook* g_hook_find (GHookList *hook_list,
1406 gboolean need_valids,
1409 GHook* g_hook_find_data (GHookList *hook_list,
1410 gboolean need_valids,
1412 GHook* g_hook_find_func (GHookList *hook_list,
1413 gboolean need_valids,
1415 GHook* g_hook_find_func_data (GHookList *hook_list,
1416 gboolean need_valids,
1419 /* return the first valid hook, and increment its reference count */
1420 GHook* g_hook_first_valid (GHookList *hook_list,
1421 gboolean may_be_in_call);
1422 /* return the next valid hook with incremented reference count, and
1423 * decrement the reference count of the original hook
1425 GHook* g_hook_next_valid (GHookList *hook_list,
1427 gboolean may_be_in_call);
1429 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1430 gint g_hook_compare_ids (GHook *new_hook,
1433 /* convenience macros */
1434 #define g_hook_append( hook_list, hook ) \
1435 g_hook_insert_before ((hook_list), NULL, (hook))
1437 /* invoke all valid hooks with the (*GHookFunc) signature.
1439 void g_hook_list_invoke (GHookList *hook_list,
1440 gboolean may_recurse);
1441 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1442 * and destroy the hook if FALSE is returned.
1444 void g_hook_list_invoke_check (GHookList *hook_list,
1445 gboolean may_recurse);
1446 /* invoke a marshaller on all valid hooks.
1448 void g_hook_list_marshal (GHookList *hook_list,
1449 gboolean may_recurse,
1450 GHookMarshaller marshaller,
1452 void g_hook_list_marshal_check (GHookList *hook_list,
1453 gboolean may_recurse,
1454 GHookCheckMarshaller marshaller,
1458 /* Fatal error handlers.
1459 * g_on_error_query() will prompt the user to either
1460 * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1461 * g_on_error_stack_trace() invokes gdb, which attaches to the current
1462 * process and shows a stack trace.
1463 * These function may cause different actions on non-unix platforms.
1464 * The prg_name arg is required by gdb to find the executable, if it is
1465 * passed as NULL, g_on_error_query() will try g_get_prgname().
1467 void g_on_error_query (const gchar *prg_name);
1468 void g_on_error_stack_trace (const gchar *prg_name);
1471 /* Logging mechanism
1473 extern const gchar *g_log_domain_glib;
1474 guint g_log_set_handler (const gchar *log_domain,
1475 GLogLevelFlags log_levels,
1477 gpointer user_data);
1478 void g_log_remove_handler (const gchar *log_domain,
1480 void g_log_default_handler (const gchar *log_domain,
1481 GLogLevelFlags log_level,
1482 const gchar *message,
1483 gpointer unused_data);
1484 void g_log (const gchar *log_domain,
1485 GLogLevelFlags log_level,
1486 const gchar *format,
1487 ...) G_GNUC_PRINTF (3, 4);
1488 void g_logv (const gchar *log_domain,
1489 GLogLevelFlags log_level,
1490 const gchar *format,
1492 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
1493 GLogLevelFlags fatal_mask);
1494 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
1495 #ifndef G_LOG_DOMAIN
1496 #define G_LOG_DOMAIN ((gchar*) 0)
1497 #endif /* G_LOG_DOMAIN */
1498 #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
1499 #define g_error(...) g_log (G_LOG_DOMAIN, \
1500 G_LOG_LEVEL_ERROR, \
1502 #define g_message(...) g_log (G_LOG_DOMAIN, \
1503 G_LOG_LEVEL_MESSAGE, \
1505 #define g_critical(...) g_log (G_LOG_DOMAIN, \
1506 G_LOG_LEVEL_CRITICAL, \
1508 #define g_warning(...) g_log (G_LOG_DOMAIN, \
1509 G_LOG_LEVEL_WARNING, \
1511 #elif __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 4)
1512 #define g_error(format...) g_log (G_LOG_DOMAIN, \
1513 G_LOG_LEVEL_ERROR, \
1515 #define g_message(format...) g_log (G_LOG_DOMAIN, \
1516 G_LOG_LEVEL_MESSAGE, \
1518 #define g_critical(format...) g_log (G_LOG_DOMAIN, \
1519 G_LOG_LEVEL_CRITICAL, \
1521 #define g_warning(format...) g_log (G_LOG_DOMAIN, \
1522 G_LOG_LEVEL_WARNING, \
1524 #else /* !__GNUC__ */
1526 g_error (const gchar *format,
1530 va_start (args, format);
1531 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1535 g_message (const gchar *format,
1539 va_start (args, format);
1540 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1544 g_critical (const gchar *format,
1548 va_start (args, format);
1549 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
1553 g_warning (const gchar *format,
1557 va_start (args, format);
1558 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1561 #endif /* !__GNUC__ */
1563 typedef void (*GPrintFunc) (const gchar *string);
1564 void g_print (const gchar *format,
1565 ...) G_GNUC_PRINTF (1, 2);
1566 GPrintFunc g_set_print_handler (GPrintFunc func);
1567 void g_printerr (const gchar *format,
1568 ...) G_GNUC_PRINTF (1, 2);
1569 GPrintFunc g_set_printerr_handler (GPrintFunc func);
1571 /* deprecated compatibility functions, use g_log_set_handler() instead */
1572 typedef void (*GErrorFunc) (const gchar *str);
1573 typedef void (*GWarningFunc) (const gchar *str);
1574 GErrorFunc g_set_error_handler (GErrorFunc func);
1575 GWarningFunc g_set_warning_handler (GWarningFunc func);
1576 GPrintFunc g_set_message_handler (GPrintFunc func);
1579 /* Memory allocation and debugging
1583 #define g_malloc(size) ((gpointer) MALLOC (size))
1584 #define g_malloc0(size) ((gpointer) CALLOC (char, size))
1585 #define g_realloc(mem,size) ((gpointer) REALLOC (mem, char, size))
1586 #define g_free(mem) FREE (mem)
1588 #else /* !USE_DMALLOC */
1590 gpointer g_malloc (gulong size);
1591 gpointer g_malloc0 (gulong size);
1592 gpointer g_realloc (gpointer mem,
1594 void g_free (gpointer mem);
1596 #endif /* !USE_DMALLOC */
1598 void g_mem_profile (void);
1599 void g_mem_check (gpointer mem);
1601 /* Generic allocators
1603 GAllocator* g_allocator_new (const gchar *name,
1605 void g_allocator_free (GAllocator *allocator);
1607 #define G_ALLOCATOR_LIST (1)
1608 #define G_ALLOCATOR_SLIST (2)
1609 #define G_ALLOCATOR_NODE (3)
1612 /* "g_mem_chunk_new" creates a new memory chunk.
1613 * Memory chunks are used to allocate pieces of memory which are
1614 * always the same size. Lists are a good example of such a data type.
1615 * The memory chunk allocates and frees blocks of memory as needed.
1616 * Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1617 * allocated in a mem chunk. ("g_free" will most likely cause a seg
1618 * fault...somewhere).
1620 * Oh yeah, GMemChunk is an opaque data type. (You don't really
1621 * want to know what's going on inside do you?)
1624 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1625 * is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1626 * atom. (They are also useful for lists which use MemChunk to allocate
1627 * memory but are also part of the MemChunk implementation).
1628 * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1631 #define G_ALLOC_ONLY 1
1632 #define G_ALLOC_AND_FREE 2
1634 GMemChunk* g_mem_chunk_new (gchar *name,
1638 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
1639 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
1640 gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
1641 void g_mem_chunk_free (GMemChunk *mem_chunk,
1643 void g_mem_chunk_clean (GMemChunk *mem_chunk);
1644 void g_mem_chunk_reset (GMemChunk *mem_chunk);
1645 void g_mem_chunk_print (GMemChunk *mem_chunk);
1646 void g_mem_chunk_info (void);
1648 /* Ah yes...we have a "g_blow_chunks" function.
1649 * "g_blow_chunks" simply compresses all the chunks. This operation
1650 * consists of freeing every memory area that should be freed (but
1651 * which we haven't gotten around to doing yet). And, no,
1652 * "g_blow_chunks" doesn't follow the naming scheme, but it is a
1653 * much better name than "g_mem_chunk_clean_all" or something
1656 void g_blow_chunks (void);
1662 /* microseconds per second */
1663 #define G_USEC_PER_SEC 1000000
1665 GTimer* g_timer_new (void);
1666 void g_timer_destroy (GTimer *timer);
1667 void g_timer_start (GTimer *timer);
1668 void g_timer_stop (GTimer *timer);
1669 void g_timer_reset (GTimer *timer);
1670 gdouble g_timer_elapsed (GTimer *timer,
1671 gulong *microseconds);
1672 void g_usleep (gulong microseconds);
1674 /* String utility functions that modify a string argument or
1675 * return a constant string that must not be freed.
1677 #define G_STR_DELIMITERS "_-|> <."
1678 gchar* g_strdelimit (gchar *string,
1679 const gchar *delimiters,
1680 gchar new_delimiter);
1681 gchar* g_strcanon (gchar *string,
1682 const gchar *valid_chars,
1684 gdouble g_strtod (const gchar *nptr,
1686 gchar* g_strerror (gint errnum) G_GNUC_CONST;
1687 gchar* g_strsignal (gint signum) G_GNUC_CONST;
1688 gint g_strcasecmp (const gchar *s1,
1690 gint g_strncasecmp (const gchar *s1,
1693 gchar* g_strdown (gchar *string);
1694 gchar* g_strup (gchar *string);
1695 gchar* g_strreverse (gchar *string);
1696 gsize g_strlcpy (gchar *dest,
1699 gsize g_strlcat (gchar *dest,
1702 /* removes leading spaces */
1703 gchar* g_strchug (gchar *string);
1704 /* removes trailing spaces */
1705 gchar* g_strchomp (gchar *string);
1706 /* removes leading & trailing spaces */
1707 #define g_strstrip( string ) g_strchomp (g_strchug (string))
1709 /* String utility functions that return a newly allocated string which
1710 * ought to be freed with g_free from the caller at some point.
1712 gchar* g_strdup (const gchar *str);
1713 gchar* g_strdup_printf (const gchar *format,
1714 ...) G_GNUC_PRINTF (1, 2);
1715 gchar* g_strdup_vprintf (const gchar *format,
1717 gchar* g_strndup (const gchar *str,
1719 gchar* g_strnfill (guint length,
1721 gchar* g_strconcat (const gchar *string1,
1722 ...); /* NULL terminated */
1723 gchar* g_strjoin (const gchar *separator,
1724 ...); /* NULL terminated */
1725 /* Make a copy of a string interpreting C string -style escape
1726 * sequences. Inverse of g_strescape. The recognized sequences are \b
1727 * \f \n \r \t \\ \" and the octal format.
1729 gchar* g_strcompress (const gchar *source);
1731 /* Convert between the operating system (or C runtime)
1732 * representation of file names and UTF-8.
1734 gchar* g_filename_to_utf8 (const gchar *opsysstring);
1735 gchar* g_filename_from_utf8 (const gchar *utf8string);
1737 /* Copy a string escaping nonprintable characters like in C strings.
1738 * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
1739 * to a string containing characters that are not to be escaped.
1741 * Deprecated API: gchar* g_strescape (const gchar *source);
1742 * Luckily this function wasn't used much, using NULL as second parameter
1743 * provides mostly identical semantics.
1745 gchar* g_strescape (const gchar *source,
1746 const gchar *exceptions);
1748 gpointer g_memdup (gconstpointer mem,
1751 /* NULL terminated string arrays.
1752 * g_strsplit() splits up string into max_tokens tokens at delim and
1753 * returns a newly allocated string array.
1754 * g_strjoinv() concatenates all of str_array's strings, sliding in an
1755 * optional separator, the returned string is newly allocated.
1756 * g_strfreev() frees the array itself and all of its strings.
1758 gchar** g_strsplit (const gchar *string,
1759 const gchar *delimiter,
1761 gchar* g_strjoinv (const gchar *separator,
1763 void g_strfreev (gchar **str_array);
1767 /* calculate a string size, guarranteed to fit format + args.
1769 guint g_printf_string_upper_bound (const gchar* format,
1773 /* Retrive static string info
1775 gchar* g_get_user_name (void);
1776 gchar* g_get_real_name (void);
1777 gchar* g_get_home_dir (void);
1778 gchar* g_get_tmp_dir (void);
1779 gchar* g_get_prgname (void);
1780 void g_set_prgname (const gchar *prgname);
1783 /* Miscellaneous utility functions
1785 guint g_parse_debug_string (const gchar *string,
1788 gint g_snprintf (gchar *string,
1790 gchar const *format,
1791 ...) G_GNUC_PRINTF (3, 4);
1792 gint g_vsnprintf (gchar *string,
1794 gchar const *format,
1796 /* Check if a file name is an absolute path */
1797 gboolean g_path_is_absolute (const gchar *file_name);
1798 /* In case of absolute paths, skip the root part */
1799 gchar* g_path_skip_root (gchar *file_name);
1801 /* These two functions are deprecated and will be removed in the next
1802 * major release of GLib. Use g_path_get_dirname/g_path_get_basename
1803 * instead. Whatch out! The string returned by g_path_get_basename
1804 * must be g_freed, while the string returned by g_basename must not.*/
1805 gchar* g_basename (const gchar *file_name);
1806 gchar* g_dirname (const gchar *file_name);
1808 /* The returned strings are newly allocated with g_malloc() */
1809 gchar* g_get_current_dir (void);
1810 gchar* g_path_get_basename (const gchar *file_name);
1811 gchar* g_path_get_dirname (const gchar *file_name);
1813 /* Get the codeset for the current locale */
1814 /* gchar * g_get_codeset (void); */
1816 /* return the environment string for the variable. The returned memory
1817 * must not be freed. */
1818 gchar* g_getenv (const gchar *variable);
1820 /* we use a GLib function as a replacement for ATEXIT, so
1821 * the programmer is not required to check the return value
1822 * (if there is any in the implementation) and doesn't encounter
1823 * missing include files.
1825 void g_atexit (GVoidFunc func);
1830 G_INLINE_FUNC gint g_bit_nth_lsf (guint32 mask,
1832 G_INLINE_FUNC gint g_bit_nth_msf (guint32 mask,
1834 G_INLINE_FUNC guint g_bit_storage (guint number);
1838 * elements need to be >= sizeof (gpointer)
1840 G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p,
1842 G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p);
1843 G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p);
1844 G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p);
1847 /* inline function implementations
1849 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
1851 g_bit_nth_lsf (guint32 mask,
1857 if (mask & (1 << (guint) nth_bit))
1860 while (nth_bit < 32);
1864 g_bit_nth_msf (guint32 mask,
1872 if (mask & (1 << (guint) nth_bit))
1875 while (nth_bit > 0);
1879 g_bit_storage (guint number)
1881 register guint n_bits = 0;
1892 g_trash_stack_push (GTrashStack **stack_p,
1895 GTrashStack *data = (GTrashStack *) data_p;
1897 data->next = *stack_p;
1900 G_INLINE_FUNC gpointer
1901 g_trash_stack_pop (GTrashStack **stack_p)
1908 *stack_p = data->next;
1909 /* NULLify private pointer here, most platforms store NULL as
1910 * subsequent 0 bytes
1917 G_INLINE_FUNC gpointer
1918 g_trash_stack_peek (GTrashStack **stack_p)
1927 g_trash_stack_height (GTrashStack **stack_p)
1932 for (data = *stack_p; data; data = data->next)
1937 #endif /* G_CAN_INLINE || __G_UTILS_C__ */
1942 GStringChunk* g_string_chunk_new (gint size);
1943 void g_string_chunk_free (GStringChunk *chunk);
1944 gchar* g_string_chunk_insert (GStringChunk *chunk,
1945 const gchar *string);
1946 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
1947 const gchar *string);
1952 GString* g_string_new (const gchar *init);
1953 GString* g_string_sized_new (guint dfl_size);
1954 gchar* g_string_free (GString *string,
1955 gboolean free_segment);
1956 gboolean g_string_equal (const GString *v,
1958 guint g_string_hash (const GString *str);
1959 GString* g_string_assign (GString *string,
1961 GString* g_string_truncate (GString *string,
1963 GString* g_string_insert_len (GString *string,
1967 GString* g_string_append (GString *string,
1969 GString* g_string_append_len (GString *string,
1972 GString* g_string_append_c (GString *string,
1974 GString* g_string_prepend (GString *string,
1976 GString* g_string_prepend_c (GString *string,
1978 GString* g_string_prepend_len (GString *string,
1981 GString* g_string_insert (GString *string,
1984 GString* g_string_insert_c (GString *string,
1987 GString* g_string_erase (GString *string,
1990 GString* g_string_down (GString *string);
1991 GString* g_string_up (GString *string);
1992 void g_string_sprintf (GString *string,
1993 const gchar *format,
1994 ...) G_GNUC_PRINTF (2, 3);
1995 void g_string_sprintfa (GString *string,
1996 const gchar *format,
1997 ...) G_GNUC_PRINTF (2, 3);
2000 /* Resizable arrays, remove fills any cleared spot and shortens the
2001 * array, while preserving the order. remove_fast will distort the
2002 * order by moving the last element to the position of the removed
2005 #define g_array_append_val(a,v) g_array_append_vals (a, &v, 1)
2006 #define g_array_prepend_val(a,v) g_array_prepend_vals (a, &v, 1)
2007 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
2008 #define g_array_index(a,t,i) (((t*) (a)->data) [(i)])
2010 GArray* g_array_new (gboolean zero_terminated,
2012 guint element_size);
2013 GArray* g_array_sized_new (gboolean zero_terminated,
2016 guint reserved_size);
2017 gchar* g_array_free (GArray *array,
2018 gboolean free_segment);
2019 GArray* g_array_append_vals (GArray *array,
2022 GArray* g_array_prepend_vals (GArray *array,
2025 GArray* g_array_insert_vals (GArray *array,
2029 GArray* g_array_set_size (GArray *array,
2031 GArray* g_array_remove_index (GArray *array,
2033 GArray* g_array_remove_index_fast (GArray *array,
2036 /* Resizable pointer array. This interface is much less complicated
2037 * than the above. Add appends appends a pointer. Remove fills any
2038 * cleared spot and shortens the array. remove_fast will again distort
2041 #define g_ptr_array_index(array,index) (array->pdata)[index]
2042 GPtrArray* g_ptr_array_new (void);
2043 GPtrArray* g_ptr_array_sized_new (guint reserved_size);
2044 gpointer* g_ptr_array_free (GPtrArray *array,
2046 void g_ptr_array_set_size (GPtrArray *array,
2048 gpointer g_ptr_array_remove_index (GPtrArray *array,
2050 gpointer g_ptr_array_remove_index_fast (GPtrArray *array,
2052 gboolean g_ptr_array_remove (GPtrArray *array,
2054 gboolean g_ptr_array_remove_fast (GPtrArray *array,
2056 void g_ptr_array_add (GPtrArray *array,
2059 /* Byte arrays, an array of guint8. Implemented as a GArray,
2063 GByteArray* g_byte_array_new (void);
2064 GByteArray* g_byte_array_sized_new (guint reserved_size);
2065 guint8* g_byte_array_free (GByteArray *array,
2066 gboolean free_segment);
2067 GByteArray* g_byte_array_append (GByteArray *array,
2070 GByteArray* g_byte_array_prepend (GByteArray *array,
2073 GByteArray* g_byte_array_set_size (GByteArray *array,
2075 GByteArray* g_byte_array_remove_index (GByteArray *array,
2077 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
2083 gboolean g_str_equal (gconstpointer v,
2085 guint g_str_hash (gconstpointer v);
2087 gint g_int_equal (gconstpointer v,
2088 gconstpointer v2) G_GNUC_CONST;
2089 guint g_int_hash (gconstpointer v) G_GNUC_CONST;
2091 /* This "hash" function will just return the key's adress as an
2092 * unsigned integer. Useful for hashing on plain adresses or
2093 * simple integer values.
2094 * passing NULL into g_hash_table_new() as GHashFunc has the
2095 * same effect as passing g_direct_hash().
2097 guint g_direct_hash (gconstpointer v) G_GNUC_CONST;
2098 gint g_direct_equal (gconstpointer v,
2099 gconstpointer v2) G_GNUC_CONST;
2102 /* Quarks (string<->id association)
2104 GQuark g_quark_try_string (const gchar *string);
2105 GQuark g_quark_from_static_string (const gchar *string);
2106 GQuark g_quark_from_string (const gchar *string);
2107 gchar* g_quark_to_string (GQuark quark) G_GNUC_CONST;
2112 void g_datalist_init (GData **datalist);
2113 void g_datalist_clear (GData **datalist);
2114 gpointer g_datalist_id_get_data (GData **datalist,
2116 void g_datalist_id_set_data_full (GData **datalist,
2119 GDestroyNotify destroy_func);
2120 gpointer g_datalist_id_remove_no_notify (GData **datalist,
2122 void g_datalist_foreach (GData **datalist,
2123 GDataForeachFunc func,
2124 gpointer user_data);
2125 #define g_datalist_id_set_data(dl, q, d) \
2126 g_datalist_id_set_data_full ((dl), (q), (d), NULL)
2127 #define g_datalist_id_remove_data(dl, q) \
2128 g_datalist_id_set_data ((dl), (q), NULL)
2129 #define g_datalist_get_data(dl, k) \
2130 (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
2131 #define g_datalist_set_data_full(dl, k, d, f) \
2132 g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
2133 #define g_datalist_remove_no_notify(dl, k) \
2134 g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
2135 #define g_datalist_set_data(dl, k, d) \
2136 g_datalist_set_data_full ((dl), (k), (d), NULL)
2137 #define g_datalist_remove_data(dl, k) \
2138 g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
2141 /* Location Associated Keyed Data
2143 void g_dataset_destroy (gconstpointer dataset_location);
2144 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
2146 void g_dataset_id_set_data_full (gconstpointer dataset_location,
2149 GDestroyNotify destroy_func);
2150 gpointer g_dataset_id_remove_no_notify (gconstpointer dataset_location,
2152 void g_dataset_foreach (gconstpointer dataset_location,
2153 GDataForeachFunc func,
2154 gpointer user_data);
2155 #define g_dataset_id_set_data(l, k, d) \
2156 g_dataset_id_set_data_full ((l), (k), (d), NULL)
2157 #define g_dataset_id_remove_data(l, k) \
2158 g_dataset_id_set_data ((l), (k), NULL)
2159 #define g_dataset_get_data(l, k) \
2160 (g_dataset_id_get_data ((l), g_quark_try_string (k)))
2161 #define g_dataset_set_data_full(l, k, d, f) \
2162 g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
2163 #define g_dataset_remove_no_notify(l, k) \
2164 g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
2165 #define g_dataset_set_data(l, k, d) \
2166 g_dataset_set_data_full ((l), (k), (d), NULL)
2167 #define g_dataset_remove_data(l, k) \
2168 g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
2171 /* GScanner: Flexible lexical scanner for general purpose.
2174 /* Character sets */
2175 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2176 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
2177 #define G_CSET_DIGITS "0123456789"
2178 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
2179 "\307\310\311\312\313\314\315\316\317\320"\
2180 "\321\322\323\324\325\326"\
2181 "\330\331\332\333\334\335\336"
2182 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
2183 "\347\350\351\352\353\354\355\356\357\360"\
2184 "\361\362\363\364\365\366"\
2185 "\370\371\372\373\374\375\376\377"
2192 G_ERR_UNEXP_EOF_IN_STRING,
2193 G_ERR_UNEXP_EOF_IN_COMMENT,
2194 G_ERR_NON_DIGIT_IN_CONST,
2197 G_ERR_FLOAT_MALFORMED
2205 G_TOKEN_LEFT_PAREN = '(',
2206 G_TOKEN_RIGHT_PAREN = ')',
2207 G_TOKEN_LEFT_CURLY = '{',
2208 G_TOKEN_RIGHT_CURLY = '}',
2209 G_TOKEN_LEFT_BRACE = '[',
2210 G_TOKEN_RIGHT_BRACE = ']',
2211 G_TOKEN_EQUAL_SIGN = '=',
2212 G_TOKEN_COMMA = ',',
2228 G_TOKEN_IDENTIFIER_NULL,
2230 G_TOKEN_COMMENT_SINGLE,
2231 G_TOKEN_COMMENT_MULTI,
2238 gchar *v_identifier;
2250 struct _GScannerConfig
2254 gchar *cset_skip_characters; /* default: " \t\n" */
2255 gchar *cset_identifier_first;
2256 gchar *cset_identifier_nth;
2257 gchar *cpair_comment_single; /* default: "#\n" */
2259 /* Should symbol lookup work case sensitive?
2261 guint case_sensitive : 1;
2263 /* Boolean values to be adjusted "on the fly"
2264 * to configure scanning behaviour.
2266 guint skip_comment_multi : 1; /* C like comment */
2267 guint skip_comment_single : 1; /* single line comment */
2268 guint scan_comment_multi : 1; /* scan multi line comments? */
2269 guint scan_identifier : 1;
2270 guint scan_identifier_1char : 1;
2271 guint scan_identifier_NULL : 1;
2272 guint scan_symbols : 1;
2273 guint scan_binary : 1;
2274 guint scan_octal : 1;
2275 guint scan_float : 1;
2276 guint scan_hex : 1; /* `0x0ff0' */
2277 guint scan_hex_dollar : 1; /* `$0ff0' */
2278 guint scan_string_sq : 1; /* string: 'anything' */
2279 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
2280 guint numbers_2_int : 1; /* bin, octal, hex => int */
2281 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
2282 guint identifier_2_string : 1;
2283 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
2284 guint symbol_2_token : 1;
2285 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
2292 guint max_parse_errors;
2294 /* g_scanner_error() increments this field */
2297 /* name of input stream, featured by the default message handler */
2298 const gchar *input_name;
2300 /* data pointer for derived structures */
2301 gpointer derived_data;
2303 /* link into the scanner configuration */
2304 GScannerConfig *config;
2306 /* fields filled in after g_scanner_get_next_token() */
2312 /* fields filled in after g_scanner_peek_next_token() */
2313 GTokenType next_token;
2314 GTokenValue next_value;
2316 guint next_position;
2318 /* to be considered private */
2319 GHashTable *symbol_table;
2322 const gchar *text_end;
2326 /* handler function for _warn and _error */
2327 GScannerMsgFunc msg_handler;
2330 GScanner* g_scanner_new (GScannerConfig *config_templ);
2331 void g_scanner_destroy (GScanner *scanner);
2332 void g_scanner_input_file (GScanner *scanner,
2334 void g_scanner_sync_file_offset (GScanner *scanner);
2335 void g_scanner_input_text (GScanner *scanner,
2338 GTokenType g_scanner_get_next_token (GScanner *scanner);
2339 GTokenType g_scanner_peek_next_token (GScanner *scanner);
2340 GTokenType g_scanner_cur_token (GScanner *scanner);
2341 GTokenValue g_scanner_cur_value (GScanner *scanner);
2342 guint g_scanner_cur_line (GScanner *scanner);
2343 guint g_scanner_cur_position (GScanner *scanner);
2344 gboolean g_scanner_eof (GScanner *scanner);
2345 guint g_scanner_set_scope (GScanner *scanner,
2347 void g_scanner_scope_add_symbol (GScanner *scanner,
2349 const gchar *symbol,
2351 void g_scanner_scope_remove_symbol (GScanner *scanner,
2353 const gchar *symbol);
2354 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
2356 const gchar *symbol);
2357 void g_scanner_scope_foreach_symbol (GScanner *scanner,
2360 gpointer user_data);
2361 gpointer g_scanner_lookup_symbol (GScanner *scanner,
2362 const gchar *symbol);
2363 void g_scanner_unexp_token (GScanner *scanner,
2364 GTokenType expected_token,
2365 const gchar *identifier_spec,
2366 const gchar *symbol_spec,
2367 const gchar *symbol_name,
2368 const gchar *message,
2370 void g_scanner_error (GScanner *scanner,
2371 const gchar *format,
2372 ...) G_GNUC_PRINTF (2,3);
2373 void g_scanner_warn (GScanner *scanner,
2374 const gchar *format,
2375 ...) G_GNUC_PRINTF (2,3);
2376 gint g_scanner_stat_mode (const gchar *filename);
2377 /* keep downward source compatibility */
2378 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
2379 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2381 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
2382 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2384 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2385 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2388 /* The following two functions are deprecated and will be removed in
2389 * the next major release. They do no good. */
2390 void g_scanner_freeze_symbol_table (GScanner *scanner);
2391 void g_scanner_thaw_symbol_table (GScanner *scanner);
2399 GCompletionFunc func;
2405 GCompletion* g_completion_new (GCompletionFunc func);
2406 void g_completion_add_items (GCompletion* cmp,
2408 void g_completion_remove_items (GCompletion* cmp,
2410 void g_completion_clear_items (GCompletion* cmp);
2411 GList* g_completion_complete (GCompletion* cmp,
2413 gchar** new_prefix);
2414 void g_completion_free (GCompletion* cmp);
2419 * Date calculations (not time for now, to be resolved). These are a
2420 * mutant combination of Steffen Beyer's DateCalc routines
2421 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2422 * date routines (written for in-house software). Written by Havoc
2423 * Pennington <hp@pobox.com>
2426 typedef guint16 GDateYear;
2427 typedef guint8 GDateDay; /* day of the month */
2428 typedef struct _GDate GDate;
2429 /* make struct tm known without having to include time.h */
2432 /* enum used to specify order of appearance in parsed date strings */
2440 /* actual week and month values */
2443 G_DATE_BAD_WEEKDAY = 0,
2446 G_DATE_WEDNESDAY = 3,
2447 G_DATE_THURSDAY = 4,
2449 G_DATE_SATURDAY = 6,
2454 G_DATE_BAD_MONTH = 0,
2456 G_DATE_FEBRUARY = 2,
2463 G_DATE_SEPTEMBER = 9,
2464 G_DATE_OCTOBER = 10,
2465 G_DATE_NOVEMBER = 11,
2466 G_DATE_DECEMBER = 12
2469 #define G_DATE_BAD_JULIAN 0U
2470 #define G_DATE_BAD_DAY 0U
2471 #define G_DATE_BAD_YEAR 0U
2473 /* Note: directly manipulating structs is generally a bad idea, but
2474 * in this case it's an *incredibly* bad idea, because all or part
2475 * of this struct can be invalid at any given time. Use the functions,
2476 * or you will get hosed, I promise.
2480 guint julian_days : 32; /* julian days representation - we use a
2481 * bitfield hoping that 64 bit platforms
2482 * will pack this whole struct in one big
2486 guint julian : 1; /* julian is valid */
2487 guint dmy : 1; /* dmy is valid */
2489 /* DMY representation */
2495 /* g_date_new() returns an invalid date, you then have to _set() stuff
2496 * to get a usable object. You can also allocate a GDate statically,
2497 * then call g_date_clear() to initialize.
2499 GDate* g_date_new (void);
2500 GDate* g_date_new_dmy (GDateDay day,
2503 GDate* g_date_new_julian (guint32 julian_day);
2504 void g_date_free (GDate *date);
2506 /* check g_date_valid() after doing an operation that might fail, like
2507 * _parse. Almost all g_date operations are undefined on invalid
2508 * dates (the exceptions are the mutators, since you need those to
2509 * return to validity).
2511 gboolean g_date_valid (GDate *date);
2512 gboolean g_date_valid_day (GDateDay day) G_GNUC_CONST;
2513 gboolean g_date_valid_month (GDateMonth month) G_GNUC_CONST;
2514 gboolean g_date_valid_year (GDateYear year) G_GNUC_CONST;
2515 gboolean g_date_valid_weekday (GDateWeekday weekday) G_GNUC_CONST;
2516 gboolean g_date_valid_julian (guint32 julian_date) G_GNUC_CONST;
2517 gboolean g_date_valid_dmy (GDateDay day,
2519 GDateYear year) G_GNUC_CONST;
2521 GDateWeekday g_date_weekday (GDate *date);
2522 GDateMonth g_date_month (GDate *date);
2523 GDateYear g_date_year (GDate *date);
2524 GDateDay g_date_day (GDate *date);
2525 guint32 g_date_julian (GDate *date);
2526 guint g_date_day_of_year (GDate *date);
2528 /* First monday/sunday is the start of week 1; if we haven't reached
2529 * that day, return 0. These are not ISO weeks of the year; that
2530 * routine needs to be added.
2531 * these functions return the number of weeks, starting on the
2534 guint g_date_monday_week_of_year (GDate *date);
2535 guint g_date_sunday_week_of_year (GDate *date);
2537 /* If you create a static date struct you need to clear it to get it
2538 * in a sane state before use. You can clear a whole array at
2539 * once with the ndates argument.
2541 void g_date_clear (GDate *date,
2544 /* The parse routine is meant for dates typed in by a user, so it
2545 * permits many formats but tries to catch common typos. If your data
2546 * needs to be strictly validated, it is not an appropriate function.
2548 void g_date_set_parse (GDate *date,
2550 void g_date_set_time (GDate *date,
2552 void g_date_set_month (GDate *date,
2554 void g_date_set_day (GDate *date,
2556 void g_date_set_year (GDate *date,
2558 void g_date_set_dmy (GDate *date,
2562 void g_date_set_julian (GDate *date,
2563 guint32 julian_date);
2564 gboolean g_date_is_first_of_month (GDate *date);
2565 gboolean g_date_is_last_of_month (GDate *date);
2567 /* To go forward by some number of weeks just go forward weeks*7 days */
2568 void g_date_add_days (GDate *date,
2570 void g_date_subtract_days (GDate *date,
2573 /* If you add/sub months while day > 28, the day might change */
2574 void g_date_add_months (GDate *date,
2576 void g_date_subtract_months (GDate *date,
2579 /* If it's feb 29, changing years can move you to the 28th */
2580 void g_date_add_years (GDate *date,
2582 void g_date_subtract_years (GDate *date,
2584 gboolean g_date_is_leap_year (GDateYear year) G_GNUC_CONST;
2585 guint8 g_date_days_in_month (GDateMonth month,
2586 GDateYear year) G_GNUC_CONST;
2587 guint8 g_date_monday_weeks_in_year (GDateYear year) G_GNUC_CONST;
2588 guint8 g_date_sunday_weeks_in_year (GDateYear year) G_GNUC_CONST;
2590 /* qsort-friendly (with a cast...) */
2591 gint g_date_compare (GDate *lhs,
2593 void g_date_to_struct_tm (GDate *date,
2596 /* Just like strftime() except you can only use date-related formats.
2597 * Using a time format is undefined.
2599 gsize g_date_strftime (gchar *s,
2601 const gchar *format,
2607 * Indexed Relations. Imagine a really simple table in a
2608 * database. Relations are not ordered. This data type is meant for
2609 * maintaining a N-way mapping.
2611 * g_relation_new() creates a relation with FIELDS fields
2613 * g_relation_destroy() frees all resources
2614 * g_tuples_destroy() frees the result of g_relation_select()
2616 * g_relation_index() indexes relation FIELD with the provided
2617 * equality and hash functions. this must be done before any
2618 * calls to insert are made.
2620 * g_relation_insert() inserts a new tuple. you are expected to
2621 * provide the right number of fields.
2623 * g_relation_delete() deletes all relations with KEY in FIELD
2624 * g_relation_select() returns ...
2625 * g_relation_count() counts ...
2628 GRelation* g_relation_new (gint fields);
2629 void g_relation_destroy (GRelation *relation);
2630 void g_relation_index (GRelation *relation,
2632 GHashFunc hash_func,
2633 GCompareFunc key_compare_func);
2634 void g_relation_insert (GRelation *relation,
2636 gint g_relation_delete (GRelation *relation,
2639 GTuples* g_relation_select (GRelation *relation,
2642 gint g_relation_count (GRelation *relation,
2645 gboolean g_relation_exists (GRelation *relation,
2647 void g_relation_print (GRelation *relation);
2649 void g_tuples_destroy (GTuples *tuples);
2650 gpointer g_tuples_index (GTuples *tuples,
2655 /* GRand - a good and fast random number generator: Mersenne Twister
2656 * see http://www.math.keio.ac.jp/~matumoto/emt.html for more info.
2657 * The range functions return a value in the intervall [min,max).
2658 * int -> [0..2^32-1]
2659 * int_range -> [min..max-1]
2661 * double_range -> [min..max)
2664 GRand* g_rand_new_with_seed (guint32 seed);
2665 GRand* g_rand_new (void);
2666 void g_rand_free (GRand *rand);
2668 void g_rand_set_seed (GRand *rand,
2670 guint32 g_rand_int (GRand *rand);
2671 gint32 g_rand_int_range (GRand *rand,
2674 gdouble g_rand_double (GRand *rand);
2675 gdouble g_rand_double_range (GRand *rand,
2679 void g_random_set_seed (guint32 seed);
2680 guint32 g_random_int (void);
2681 gint32 g_random_int_range (gint32 min,
2683 gdouble g_random_double (void);
2684 gdouble g_random_double_range (gdouble min,
2691 /* This function returns prime numbers spaced by approximately 1.5-2.0
2692 * and is for use in resizing data structures which prefer
2693 * prime-valued sizes. The closest spaced prime function returns the
2694 * next largest prime, or the highest it knows about which is about
2697 guint g_spaced_primes_closest (guint num) G_GNUC_CONST;
2703 typedef struct _GIOFuncs GIOFuncs;
2719 G_IO_IN GLIB_SYSDEF_POLLIN,
2720 G_IO_OUT GLIB_SYSDEF_POLLOUT,
2721 G_IO_PRI GLIB_SYSDEF_POLLPRI,
2722 G_IO_ERR GLIB_SYSDEF_POLLERR,
2723 G_IO_HUP GLIB_SYSDEF_POLLHUP,
2724 G_IO_NVAL GLIB_SYSDEF_POLLNVAL
2729 guint channel_flags;
2734 typedef gboolean (*GIOFunc) (GIOChannel *source,
2735 GIOCondition condition,
2739 GIOError (*io_read) (GIOChannel *channel,
2743 GIOError (*io_write) (GIOChannel *channel,
2746 guint *bytes_written);
2747 GIOError (*io_seek) (GIOChannel *channel,
2750 void (*io_close) (GIOChannel *channel);
2751 guint (*io_add_watch) (GIOChannel *channel,
2753 GIOCondition condition,
2756 GDestroyNotify notify);
2757 void (*io_free) (GIOChannel *channel);
2760 void g_io_channel_init (GIOChannel *channel);
2761 void g_io_channel_ref (GIOChannel *channel);
2762 void g_io_channel_unref (GIOChannel *channel);
2763 GIOError g_io_channel_read (GIOChannel *channel,
2767 GIOError g_io_channel_write (GIOChannel *channel,
2770 guint *bytes_written);
2771 GIOError g_io_channel_seek (GIOChannel *channel,
2774 void g_io_channel_close (GIOChannel *channel);
2775 guint g_io_add_watch_full (GIOChannel *channel,
2777 GIOCondition condition,
2780 GDestroyNotify notify);
2781 guint g_io_add_watch (GIOChannel *channel,
2782 GIOCondition condition,
2784 gpointer user_data);
2789 typedef struct _GTimeVal GTimeVal;
2790 typedef struct _GSourceFuncs GSourceFuncs;
2791 typedef struct _GMainLoop GMainLoop; /* Opaque */
2798 struct _GSourceFuncs
2800 gboolean (*prepare) (gpointer source_data,
2801 GTimeVal *current_time,
2803 gpointer user_data);
2804 gboolean (*check) (gpointer source_data,
2805 GTimeVal *current_time,
2806 gpointer user_data);
2807 gboolean (*dispatch) (gpointer source_data,
2808 GTimeVal *dispatch_time,
2809 gpointer user_data);
2810 GDestroyNotify destroy;
2813 /* Standard priorities */
2815 #define G_PRIORITY_HIGH -100
2816 #define G_PRIORITY_DEFAULT 0
2817 #define G_PRIORITY_HIGH_IDLE 100
2818 #define G_PRIORITY_DEFAULT_IDLE 200
2819 #define G_PRIORITY_LOW 300
2821 typedef gboolean (*GSourceFunc) (gpointer data);
2823 /* Hooks for adding to the main loop */
2824 guint g_source_add (gint priority,
2825 gboolean can_recurse,
2826 GSourceFuncs *funcs,
2827 gpointer source_data,
2829 GDestroyNotify notify);
2830 gboolean g_source_remove (guint tag);
2831 gboolean g_source_remove_by_user_data (gpointer user_data);
2832 gboolean g_source_remove_by_source_data (gpointer source_data);
2833 gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
2834 gpointer user_data);
2836 void g_get_current_time (GTimeVal *result);
2838 /* Running the main loop */
2839 GMainLoop* g_main_new (gboolean is_running);
2840 void g_main_run (GMainLoop *loop);
2841 void g_main_quit (GMainLoop *loop);
2842 void g_main_destroy (GMainLoop *loop);
2843 gboolean g_main_is_running (GMainLoop *loop);
2845 /* Run a single iteration of the mainloop. If block is FALSE,
2848 gboolean g_main_iteration (gboolean may_block);
2850 /* See if any events are pending */
2851 gboolean g_main_pending (void);
2853 /* Idles and timeouts */
2854 guint g_timeout_add_full (gint priority,
2856 GSourceFunc function,
2858 GDestroyNotify notify);
2859 guint g_timeout_add (guint interval,
2860 GSourceFunc function,
2862 guint g_idle_add (GSourceFunc function,
2864 guint g_idle_add_full (gint priority,
2865 GSourceFunc function,
2867 GDestroyNotify destroy);
2868 gboolean g_idle_remove_by_data (gpointer data);
2872 * System-specific IO and main loop calls
2874 * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
2875 * descriptor as provided by the C runtime) that can be used by
2876 * MsgWaitForMultipleObjects. This does *not* include file handles
2877 * from CreateFile, SOCKETs, nor pipe handles. (But you can use
2878 * WSAEventSelect to signal events when a SOCKET is readable).
2880 * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
2881 * indicate polling for messages. These message queue GPollFDs should
2882 * be added with the g_main_poll_win32_msg_add function.
2884 * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
2885 * (GTK) programs, as GDK itself wants to read messages and convert them
2888 * So, unless you really know what you are doing, it's best not to try
2889 * to use the main loop polling stuff for your own needs on
2890 * Win32. It's really only written for the GIMP's needs so
2894 typedef struct _GPollFD GPollFD;
2895 typedef gint (*GPollFunc) (GPollFD *ufds,
2905 void g_main_add_poll (GPollFD *fd,
2907 void g_main_remove_poll (GPollFD *fd);
2908 void g_main_set_poll_func (GPollFunc func);
2912 /* Useful on other platforms, too? */
2914 GPollFunc g_main_win32_get_poll_func (void);
2918 /* On Unix, IO channels created with this function for any file
2919 * descriptor or socket.
2921 * On Win32, use this only for files opened with the MSVCRT (the
2922 * Microsoft run-time C library) _open() or _pipe, including file
2923 * descriptors 0, 1 and 2 (corresponding to stdin, stdout and stderr).
2925 * The term file descriptor as used in the context of Win32 refers to
2926 * the emulated Unix-like file descriptors MSVCRT provides. The native
2927 * corresponding concept is file HANDLE. There isn't as of yet a way to
2928 * get GIOChannels for file HANDLEs.
2930 GIOChannel* g_io_channel_unix_new (int fd);
2931 gint g_io_channel_unix_get_fd (GIOChannel *channel);
2935 #define G_WIN32_MSG_HANDLE 19981206
2937 /* Use this to get a GPollFD from a GIOChannel, so that you can call
2938 * g_io_channel_win32_poll(). After calling this you should only use
2939 * g_io_channel_read() to read from the GIOChannel, i.e. never read()
2940 * or recv() from the underlying file descriptor or SOCKET.
2942 void g_io_channel_win32_make_pollfd (GIOChannel *channel,
2943 GIOCondition condition,
2946 /* This can be used to wait a until at least one of the channels is readable.
2947 * On Unix you would do a select() on the file descriptors of the channels.
2948 * This should probably be available for all platforms?
2950 gint g_io_channel_win32_poll (GPollFD *fds,
2954 /* This is used to add polling for Windows messages. GDK (GTk+) programs
2955 * should *not* use this.
2957 void g_main_poll_win32_msg_add (gint priority,
2961 /* An IO channel for Windows messages for window handle hwnd. */
2962 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
2964 /* An IO channel for C runtime (emulated Unix-like) file
2965 * descriptors. Identical to g_io_channel_unix_new above.
2966 * After calling g_io_add_watch() on a IO channel returned
2967 * by this function, you shouldn't call read() on the file
2970 GIOChannel* g_io_channel_win32_new_fd (int fd);
2972 /* Get the C runtime file descriptor of a channel. */
2973 gint g_io_channel_win32_get_fd (GIOChannel *channel);
2975 /* An IO channel for a SOCK_STREAM winsock socket. The parameter
2976 * should be a SOCKET. After calling g_io_add_watch() on a IO channel
2977 * returned by this function, you shouldn't call recv() on the SOCKET.
2979 GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
2983 /* Windows emulation stubs for common Unix functions
2986 # define MAXPATHLEN 1024
2993 * To get prototypes for the following POSIXish functions, you have to
2994 * include the indicated non-POSIX headers. The functions are defined
2995 * in OLDNAMES.LIB (MSVC) or -lmoldname-msvc (mingw32).
2997 * getcwd: <direct.h> (MSVC), <io.h> (mingw32)
2998 * getpid: <process.h>
3000 * unlink: <stdio.h> or <io.h>
3001 * open, read, write, lseek, close: <io.h>
3006 /* pipe is not in OLDNAMES.LIB or -lmoldname-msvc. */
3007 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
3009 /* For some POSIX functions that are not provided by the MS runtime,
3010 * we provide emulators in glib, which are prefixed with g_win32_.
3012 # define ftruncate(fd, size) g_win32_ftruncate (fd, size)
3014 /* -lmingw32 also has emulations for these, but we need our own
3015 * for MSVC anyhow, so we might aswell use them always.
3017 # define opendir g_win32_opendir
3018 # define readdir g_win32_readdir
3019 # define rewinddir g_win32_rewinddir
3020 # define closedir g_win32_closedir
3021 # define NAME_MAX 255
3026 gboolean just_opened;
3027 guint find_file_handle;
3028 gpointer find_file_data;
3030 typedef struct DIR DIR;
3033 gchar d_name[NAME_MAX + 1];
3035 /* emulation functions */
3036 extern int g_win32_ftruncate (gint f,
3038 DIR* g_win32_opendir (const gchar *dirname);
3039 struct dirent* g_win32_readdir (DIR *dir);
3040 void g_win32_rewinddir (DIR *dir);
3041 gint g_win32_closedir (DIR *dir);
3043 /* The MS setlocale uses locale names of the form "English_United
3044 * States.1252" etc. We want the Unixish standard form "en", "zh_TW"
3045 * etc. This function gets the current thread locale from Windows and
3046 * returns it as a string of the above form for use in forming file
3047 * names etc. The returned string should be deallocated with g_free().
3049 gchar * g_win32_getlocale (void);
3051 /* Translate a Win32 error code (as returned by GetLastError()) into
3052 * the corresponding message. The returned string should be deallocated
3055 gchar * g_win32_error_message (gint error);
3057 #endif /* G_OS_WIN32 */
3060 /* GLib Thread support
3063 extern GQuark g_thread_error_quark();
3064 #define G_THREAD_ERROR g_thread_error_quark()
3068 G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
3071 typedef void (*GThreadFunc) (gpointer value);
3075 G_THREAD_PRIORITY_LOW,
3076 G_THREAD_PRIORITY_NORMAL,
3077 G_THREAD_PRIORITY_HIGH,
3078 G_THREAD_PRIORITY_URGENT
3081 typedef struct _GThread GThread;
3084 GThreadPriority priority;
3089 typedef struct _GMutex GMutex;
3090 typedef struct _GCond GCond;
3091 typedef struct _GPrivate GPrivate;
3092 typedef struct _GStaticPrivate GStaticPrivate;
3093 typedef struct _GAsyncQueue GAsyncQueue;
3094 typedef struct _GThreadPool GThreadPool;
3096 typedef struct _GThreadFunctions GThreadFunctions;
3097 struct _GThreadFunctions
3099 GMutex* (*mutex_new) (void);
3100 void (*mutex_lock) (GMutex *mutex);
3101 gboolean (*mutex_trylock) (GMutex *mutex);
3102 void (*mutex_unlock) (GMutex *mutex);
3103 void (*mutex_free) (GMutex *mutex);
3104 GCond* (*cond_new) (void);
3105 void (*cond_signal) (GCond *cond);
3106 void (*cond_broadcast) (GCond *cond);
3107 void (*cond_wait) (GCond *cond,
3109 gboolean (*cond_timed_wait) (GCond *cond,
3111 GTimeVal *end_time);
3112 void (*cond_free) (GCond *cond);
3113 GPrivate* (*private_new) (GDestroyNotify destructor);
3114 gpointer (*private_get) (GPrivate *private_key);
3115 void (*private_set) (GPrivate *private_key,
3117 void (*thread_create) (GThreadFunc thread_func,
3122 GThreadPriority priority,
3125 void (*thread_yield) (void);
3126 void (*thread_join) (gpointer thread);
3127 void (*thread_exit) (void);
3128 void (*thread_set_priority)(gpointer thread,
3129 GThreadPriority priority);
3130 void (*thread_self) (gpointer thread);
3133 GLIB_VAR GThreadFunctions g_thread_functions_for_glib_use;
3134 GLIB_VAR gboolean g_thread_use_default_impl;
3135 GLIB_VAR gboolean g_threads_got_initialized;
3137 /* initializes the mutex/cond/private implementation for glib, might
3138 * only be called once, and must not be called directly or indirectly
3139 * from another glib-function, e.g. as a callback.
3141 void g_thread_init (GThreadFunctions *vtable);
3143 /* internal function for fallback static mutex implementation */
3144 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
3146 /* shorthands for conditional and unconditional function calls */
3147 #define G_THREAD_UF(name, arglist) \
3148 (*g_thread_functions_for_glib_use . name) arglist
3149 #define G_THREAD_CF(name, fail, arg) \
3150 (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
3151 /* keep in mind, all those mutexes and static mutexes are not
3152 * recursive in general, don't rely on that
3154 #define g_thread_supported() (g_threads_got_initialized)
3155 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
3156 #define g_mutex_lock(mutex) G_THREAD_CF (mutex_lock, (void)0, (mutex))
3157 #define g_mutex_trylock(mutex) G_THREAD_CF (mutex_trylock, TRUE, (mutex))
3158 #define g_mutex_unlock(mutex) G_THREAD_CF (mutex_unlock, (void)0, (mutex))
3159 #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex))
3160 #define g_cond_new() G_THREAD_UF (cond_new, ())
3161 #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
3162 #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
3163 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait, (void)0, (cond, \
3165 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
3166 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
3170 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
3171 #define g_private_get(private_key) G_THREAD_CF (private_get, \
3172 ((gpointer)private_key), \
3174 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
3175 (void) (private_key = \
3176 (GPrivate*) (value)), \
3177 (private_key, value))
3178 #define g_thread_yield() G_THREAD_CF (thread_yield, (void)0, ())
3179 #define g_thread_exit() G_THREAD_CF (thread_exit, (void)0, ())
3181 GThread* g_thread_create (GThreadFunc thread_func,
3186 GThreadPriority priority,
3188 GThread* g_thread_self ();
3189 void g_thread_join (GThread *thread);
3190 void g_thread_set_priority (GThread *thread,
3191 GThreadPriority priority);
3193 /* GStaticMutexes can be statically initialized with the value
3194 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
3195 * much easier, than having to explicitly allocate the mutex before
3198 #define g_static_mutex_lock(mutex) \
3199 g_mutex_lock (g_static_mutex_get_mutex (mutex))
3200 #define g_static_mutex_trylock(mutex) \
3201 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
3202 #define g_static_mutex_unlock(mutex) \
3203 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
3205 struct _GStaticPrivate
3209 #define G_STATIC_PRIVATE_INIT { 0 }
3210 gpointer g_static_private_get (GStaticPrivate *private_key);
3211 void g_static_private_set (GStaticPrivate *private_key,
3213 GDestroyNotify notify);
3214 gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
3216 void g_static_private_set_for_thread (GStaticPrivate *private_key,
3219 GDestroyNotify notify);
3221 typedef struct _GStaticRecMutex GStaticRecMutex;
3222 struct _GStaticRecMutex
3226 GSystemThread owner;
3229 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT }
3230 void g_static_rec_mutex_lock (GStaticRecMutex *mutex);
3231 gboolean g_static_rec_mutex_trylock (GStaticRecMutex *mutex);
3232 void g_static_rec_mutex_unlock (GStaticRecMutex *mutex);
3233 void g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
3235 guint g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
3237 typedef struct _GStaticRWLock GStaticRWLock;
3238 struct _GStaticRWLock
3245 guint want_to_write;
3248 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
3250 void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
3251 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
3252 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
3253 void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
3254 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
3255 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
3256 void g_static_rw_lock_free (GStaticRWLock* lock);
3258 /* these are some convenience macros that expand to nothing if GLib
3259 * was configured with --disable-threads. for using StaticMutexes,
3260 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
3261 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
3262 * declare such an globally defined lock. name is a unique identifier
3263 * for the protected varibale or code portion. locking, testing and
3264 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
3265 * G_TRYLOCK() respectively.
3267 extern void glib_dummy_decl (void);
3268 #define G_LOCK_NAME(name) g__ ## name ## _lock
3269 #ifdef G_THREADS_ENABLED
3270 # define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
3271 # define G_LOCK_DEFINE(name) \
3272 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
3273 # define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
3275 # ifdef G_DEBUG_LOCKS
3276 # define G_LOCK(name) G_STMT_START{ \
3277 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3278 "file %s: line %d (%s): locking: %s ", \
3279 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3281 g_static_mutex_lock (&G_LOCK_NAME (name)); \
3283 # define G_UNLOCK(name) G_STMT_START{ \
3284 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3285 "file %s: line %d (%s): unlocking: %s ", \
3286 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3288 g_static_mutex_unlock (&G_LOCK_NAME (name)); \
3290 # define G_TRYLOCK(name) \
3291 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3292 "file %s: line %d (%s): try locking: %s ", \
3293 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3294 #name), g_static_mutex_trylock (&G_LOCK_NAME (name)))
3295 # else /* !G_DEBUG_LOCKS */
3296 # define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
3297 # define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
3298 # define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
3299 # endif /* !G_DEBUG_LOCKS */
3300 #else /* !G_THREADS_ENABLED */
3301 # define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
3302 # define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
3303 # define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
3304 # define G_LOCK(name)
3305 # define G_UNLOCK(name)
3306 # define G_TRYLOCK(name) (TRUE)
3307 #endif /* !G_THREADS_ENABLED */
3309 /* Asyncronous Queues, can be used to communicate between threads
3312 /* Get a new GAsyncQueue with the ref_count 1 */
3313 GAsyncQueue* g_async_queue_new (void);
3315 /* Lock and unlock an GAsyncQueue, all functions lock the queue for
3316 * themselves, but in certain cirumstances you want to hold the lock longer,
3317 * thus you lock the queue, call the *_unlocked functions and unlock it again
3319 void g_async_queue_lock (GAsyncQueue *queue);
3320 void g_async_queue_unlock (GAsyncQueue *queue);
3322 /* Ref and unref the GAsyncQueue. g_async_queue_unref_unlocked makes
3323 * no sense, as after the unreffing the Queue might be gone and can't
3324 * be unlocked. So you have a function to call, if you don't hold the
3325 * lock (g_async_queue_unref) and one to call, when you already hold
3326 * the lock (g_async_queue_unref_and_unlock). After that however, you
3327 * don't hold the lock anymore and the Queue might in fact be
3328 * destroyed, if you unrefed to zero */
3329 void g_async_queue_ref (GAsyncQueue *queue);
3330 void g_async_queue_ref_unlocked (GAsyncQueue *queue);
3331 void g_async_queue_unref (GAsyncQueue *queue);
3332 void g_async_queue_unref_and_unlock (GAsyncQueue *queue);
3334 /* Push data into the async queue. Must not be NULL */
3335 void g_async_queue_push (GAsyncQueue *queue,
3337 void g_async_queue_push_unlocked (GAsyncQueue *queue,
3340 /* Pop data from the async queue, when no data is there, the thread is blocked
3341 * until data arrives */
3342 gpointer g_async_queue_pop (GAsyncQueue *queue);
3343 gpointer g_async_queue_pop_unlocked (GAsyncQueue *queue);
3345 /* Try to pop data, NULL is returned in case of empty queue */
3346 gpointer g_async_queue_try_pop (GAsyncQueue *queue);
3347 gpointer g_async_queue_try_pop_unlocked (GAsyncQueue *queue);
3349 /* Wait for data until at maximum until end_time is reached, NULL is returned
3350 * in case of empty queue*/
3351 gpointer g_async_queue_timed_pop (GAsyncQueue *queue,
3352 GTimeVal *end_time);
3353 gpointer g_async_queue_timed_pop_unlocked (GAsyncQueue *queue,
3354 GTimeVal *end_time);
3356 /* Return the length of the queue, negative values mean, that threads
3357 * are waiting, positve values mean, that there are entries in the
3358 * queue. Actually this function returns the length of the queue minus
3359 * the number of waiting threads, g_async_queue_length == 0 could also
3360 * mean 'n' entries in the queue and 'n' thread waiting, such can
3361 * happen due to locking of the queue or due to scheduling. */
3362 gint g_async_queue_length (GAsyncQueue *queue);
3363 gint g_async_queue_length_unlocked (GAsyncQueue *queue);
3368 /* The real GThreadPool is bigger, so you may only create a thread
3369 * pool with the constructor function */
3375 GThreadPriority priority;
3380 /* Get a thread pool with the function thread_func, at most max_threads may
3381 * run at a time (max_threads == -1 means no limit), stack_size, bound,
3382 * priority like in g_thread_create, exclusive == TRUE means, that the threads
3383 * shouldn't be shared and that they will be prestarted (otherwise they are
3384 * started, as needed) user_data is the 2nd argument to the thread_func */
3385 GThreadPool* g_thread_pool_new (GFunc thread_func,
3389 GThreadPriority priority,
3394 /* Push new data into the thread pool. This task is assigned to a thread later
3395 * (when the maximal number of threads is reached for that pool) or now
3396 * (otherwise). If necessary a new thread will be started. The function
3397 * returns immediatly */
3398 void g_thread_pool_push (GThreadPool *pool,
3402 /* Set the number of threads, which can run concurrently for that pool, -1
3403 * means no limit. 0 means has the effect, that the pool won't process
3404 * requests until the limit is set higher again */
3405 void g_thread_pool_set_max_threads (GThreadPool *pool,
3408 gint g_thread_pool_get_max_threads (GThreadPool *pool);
3410 /* Get the number of threads assigned to that pool. This number doesn't
3411 * necessarily represent the number of working threads in that pool */
3412 guint g_thread_pool_get_num_threads (GThreadPool *pool);
3414 /* Get the number of unprocessed items in the pool */
3415 guint g_thread_pool_unprocessed (GThreadPool *pool);
3417 /* Free the pool, immediate means, that all unprocessed items in the queue
3418 * wont be processed, wait means, that the function doesn't return immediatly,
3419 * but after all threads in the pool are ready processing items. immediate
3420 * does however not mean, that threads are killed. */
3421 void g_thread_pool_free (GThreadPool *pool,
3425 /* Set the maximal number of unused threads before threads will be stopped by
3426 * GLib, -1 means no limit */
3427 void g_thread_pool_set_max_unused_threads (gint max_threads);
3428 gint g_thread_pool_get_max_unused_threads (void);
3429 guint g_thread_pool_get_num_unused_threads (void);
3431 /* Stop all currently unused threads, but leave the limit untouched */
3432 void g_thread_pool_stop_unused_threads (void);
3436 G_CONVERT_ERROR_NO_CONVERSION,
3437 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
3438 G_CONVERT_ERROR_OTHER
3441 #define G_CONVERT_ERROR g_convert_error_quark()
3442 GQuark g_convert_error_quark();
3444 gchar* g_convert (const gchar *str,
3446 const gchar *to_codeset,
3447 const gchar *from_codeset,
3449 gint *bytes_written,
3451 gchar* g_convert_with_fallback (const gchar *str,
3453 const gchar *to_codeset,
3454 const gchar *from_codeset,
3457 gint *bytes_written,
3462 #endif /* __cplusplus */
3464 #include <gunicode.h>
3466 #endif /* __G_LIB_H__ */