Added errorcheck mutexes. These are activated through the preprocessor
[platform/upstream/glib.git] / glib / glib.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #ifndef __G_LIB_H__
28 #define __G_LIB_H__
29
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.  
36  */
37 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
38 #  define G_GNUC_EXTENSION __extension__
39 #else
40 #  define G_GNUC_EXTENSION
41 #endif
42
43 /* system specific config file glibconfig.h provides definitions for
44  * the extrema of many of the standard types. These are:
45  *
46  *  G_MINSHORT, G_MAXSHORT
47  *  G_MININT, G_MAXINT
48  *  G_MINLONG, G_MAXLONG
49  *  G_MINFLOAT, G_MAXFLOAT
50  *  G_MINDOUBLE, G_MAXDOUBLE
51  *
52  * It also provides the following typedefs:
53  *
54  *  gint8, guint8
55  *  gint16, guint16
56  *  gint32, guint32
57  *  gint64, guint64
58  *  gssize, gsize
59  *
60  * It defines the G_BYTE_ORDER symbol to one of G_*_ENDIAN (see later in
61  * this file). 
62  *
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.
65  *
66  *  GINT_TO_POINTER (i), GUINT_TO_POINTER (i)
67  *  GPOINTER_TO_INT (p), GPOINTER_TO_UINT (p)
68  *
69  * Finally, it provides the following wrappers to STDC functions:
70  *
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,
74  *    or size_t.
75  */
76 #include <glibconfig.h>
77
78 /* Define some mathematical constants that aren't available
79  * symbolically in some strict ISO C implementations.
80  */
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
88
89 /* include varargs functions for assertment macros
90  */
91 #include <stdarg.h>
92
93 /* optionally feature DMALLOC memory allocation debugger
94  */
95 #ifdef USE_DMALLOC
96 #include "dmalloc.h"
97 #endif
98
99
100 #ifdef G_OS_WIN32
101
102 /* On native Win32, directory separator is the backslash, and search path
103  * separator is the semicolon.
104  */
105 #define G_DIR_SEPARATOR '\\'
106 #define G_DIR_SEPARATOR_S "\\"
107 #define G_SEARCHPATH_SEPARATOR ';'
108 #define G_SEARCHPATH_SEPARATOR_S ";"
109
110 #else  /* !G_OS_WIN32 */
111
112 /* Unix */
113
114 #define G_DIR_SEPARATOR '/'
115 #define G_DIR_SEPARATOR_S "/"
116 #define G_SEARCHPATH_SEPARATOR ':'
117 #define G_SEARCHPATH_SEPARATOR_S ":"
118
119 #endif /* !G_OS_WIN32 */
120
121 #ifdef __cplusplus
122 extern "C" {
123 #endif /* __cplusplus */
124
125
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.
130  */
131 #ifndef NULL
132 #  ifdef __cplusplus
133 #    define NULL        (0L)
134 #  else /* !__cplusplus */
135 #    define NULL        ((void*) 0)
136 #  endif /* !__cplusplus */
137 #endif
138
139 #ifndef FALSE
140 #define FALSE   (0)
141 #endif
142
143 #ifndef TRUE
144 #define TRUE    (!FALSE)
145 #endif
146
147 #undef  MAX
148 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
149
150 #undef  MIN
151 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
152
153 #undef  ABS
154 #define ABS(a)     (((a) < 0) ? -(a) : (a))
155
156 #undef  CLAMP
157 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
158
159 #define G_STRINGIFY(macro_or_string)    G_STRINGIFY_ARG (macro_or_string)
160 #define G_STRINGIFY_ARG(contents)       #contents
161
162 /* provide a string identifying the current code position */
163 #ifdef  __GNUC__
164 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
165 #else
166 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__)
167 #endif
168
169
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
172  * incorrect results.
173  */
174 #define G_N_ELEMENTS(arr)               (sizeof (arr) / sizeof ((arr)[0]))
175
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.
178  */
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 */
188
189
190 /* Provide convenience macros for handling structure
191  * fields through their offsets.
192  */
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)))
199
200
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.
211  */
212 #ifdef G_IMPLEMENT_INLINES
213 #  define G_INLINE_FUNC extern
214 #  undef  G_CAN_INLINE
215 #endif
216 #ifndef G_INLINE_FUNC
217 #  define G_CAN_INLINE 1
218 #endif
219 #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
220 #  undef inline
221 #  define inline __inline__
222 #elif !defined (G_HAVE_INLINE)
223 #  undef 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
231 #      undef G_CAN_INLINE
232 #    endif
233 #  endif
234 #endif
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
242 #    undef G_CAN_INLINE
243 #  endif
244 #endif /* !G_INLINE_FUNC */
245
246
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 ...
251  *
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)'.
255  */
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          )
260 #  else
261 #    if (defined (sun) || defined (__sun__))
262 #      define G_STMT_START      if (1)
263 #      define G_STMT_END        else (void)0
264 #    else
265 #      define G_STMT_START      do
266 #      define G_STMT_END        while (0)
267 #    endif
268 #  endif
269 #endif
270
271
272 /* Provide macros to feature the GCC function attribute.
273  */
274 #if    __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
275 #define G_GNUC_PURE                            \
276   __attribute__((pure))
277 #else
278 #define G_GNUC_PURE
279 #endif
280
281 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
282 #define G_GNUC_PRINTF( format_idx, arg_idx )    \
283   __attribute__((format (printf, format_idx, arg_idx)))
284 #define G_GNUC_SCANF( format_idx, arg_idx )     \
285   __attribute__((format (scanf, format_idx, arg_idx)))
286 #define G_GNUC_FORMAT( arg_idx )                \
287   __attribute__((format_arg (arg_idx)))
288 #define G_GNUC_NORETURN                         \
289   __attribute__((noreturn))
290 #define G_GNUC_CONST                            \
291   __attribute__((const))
292 #define G_GNUC_UNUSED                           \
293   __attribute__((unused))
294 #else   /* !__GNUC__ */
295 #define G_GNUC_PRINTF( format_idx, arg_idx )
296 #define G_GNUC_SCANF( format_idx, arg_idx )
297 #define G_GNUC_FORMAT( arg_idx )
298 #define G_GNUC_NORETURN
299 #define G_GNUC_CONST
300 #define G_GNUC_UNUSED
301 #endif  /* !__GNUC__ */
302
303 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
304  * macros, so we can refer to them as strings unconditionally.
305  */
306 #ifdef  __GNUC__
307 #define G_GNUC_FUNCTION         __FUNCTION__
308 #define G_GNUC_PRETTY_FUNCTION  __PRETTY_FUNCTION__
309 #else   /* !__GNUC__ */
310 #define G_GNUC_FUNCTION         ""
311 #define G_GNUC_PRETTY_FUNCTION  ""
312 #endif  /* !__GNUC__ */
313
314 /* we try to provide a usefull equivalent for ATEXIT if it is
315  * not defined, but use is actually abandoned. people should
316  * use g_atexit() instead.
317  */
318 #ifndef ATEXIT
319 # define ATEXIT(proc)   g_ATEXIT(proc)
320 #else
321 # define G_NATIVE_ATEXIT
322 #endif /* ATEXIT */
323
324 /* Hacker macro to place breakpoints for elected machines.
325  * Actual use is strongly deprecated of course ;)
326  */
327 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
328 #  define G_BREAKPOINT()        G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
329 #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
330 #  define G_BREAKPOINT()        G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
331 #else   /* !__i386__ && !__alpha__ */
332 #  define G_BREAKPOINT()        G_STMT_START{ raise (5 /* SIGTRAP */); }G_STMT_END
333 #endif  /* __i386__ */
334
335 /* g_alloca handling */
336 #ifdef GLIB_HAVE_ALLOCA_H
337 #include <alloca.h>
338 #endif
339
340 #include <string.h>
341 #ifdef __GNUC__
342 /* glibc already does this for us */
343 #ifndef alloca
344 # define alloca(size) __builtin_alloca (size)
345 #endif
346 #else
347 # ifdef _MSC_VER
348 #  include <malloc.h>
349 #  define alloca _alloca
350 # else
351 #   ifdef _AIX
352  #pragma alloca
353 #   else
354 #    ifndef alloca /* predefined by HP cc +Olibcalls */
355 char *alloca ();
356 #    endif
357 #   endif
358 # endif
359 #endif
360
361 #define g_alloca(size) alloca (size)
362 /* End g_alloca handling */
363
364 /* Provide macros for easily allocating memory. The macros
365  *  will cast the allocated memory to the specified type
366  *  in order to avoid compiler warnings. (Makes the code neater).
367  */
368
369 #ifdef __DMALLOC_H__
370 #  define g_new(type, count)            (ALLOC (type, count))
371 #  define g_new0(type, count)           (CALLOC (type, count))
372 #  define g_renew(type, mem, count)     (REALLOC (mem, type, count))
373 #else /* __DMALLOC_H__ */
374 #  define g_new(type, count)      \
375       ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
376 #  define g_new0(type, count)     \
377       ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
378 #  define g_renew(type, mem, count)       \
379       ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
380 #endif /* __DMALLOC_H__ */
381
382 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
383   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
384                    sizeof (type), \
385                    sizeof (type) * (pre_alloc), \
386                    (alloc_type)) \
387 )
388 #define g_chunk_new(type, chunk)        ( \
389   (type *) g_mem_chunk_alloc (chunk) \
390 )
391 #define g_chunk_new0(type, chunk)       ( \
392   (type *) g_mem_chunk_alloc0 (chunk) \
393 )
394 #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
395   g_mem_chunk_free ((mem_chunk), (mem)); \
396 } G_STMT_END
397
398
399 /* Provide macros for error handling. The "assert" macros will
400  *  exit on failure. The "return" macros will exit the current
401  *  function. Two different definitions are given for the macros
402  *  if G_DISABLE_ASSERT is not defined, in order to support gcc's
403  *  __PRETTY_FUNCTION__ capability.
404  */
405
406 #ifdef G_DISABLE_ASSERT
407
408 #define g_assert(expr)
409 #define g_assert_not_reached()
410
411 #else /* !G_DISABLE_ASSERT */
412
413 #ifdef __GNUC__
414
415 #define g_assert(expr)                  G_STMT_START{           \
416      if (!(expr))                                               \
417        g_log (G_LOG_DOMAIN,                                     \
418               G_LOG_LEVEL_ERROR,                                \
419               "file %s: line %d (%s): assertion failed: (%s)",  \
420               __FILE__,                                         \
421               __LINE__,                                         \
422               __PRETTY_FUNCTION__,                              \
423               #expr);                   }G_STMT_END
424
425 #define g_assert_not_reached()          G_STMT_START{           \
426      g_log (G_LOG_DOMAIN,                                       \
427             G_LOG_LEVEL_ERROR,                                  \
428             "file %s: line %d (%s): should not be reached",     \
429             __FILE__,                                           \
430             __LINE__,                                           \
431             __PRETTY_FUNCTION__);       }G_STMT_END
432
433 #else /* !__GNUC__ */
434
435 #define g_assert(expr)                  G_STMT_START{           \
436      if (!(expr))                                               \
437        g_log (G_LOG_DOMAIN,                                     \
438               G_LOG_LEVEL_ERROR,                                \
439               "file %s: line %d: assertion failed: (%s)",       \
440               __FILE__,                                         \
441               __LINE__,                                         \
442               #expr);                   }G_STMT_END
443
444 #define g_assert_not_reached()          G_STMT_START{   \
445      g_log (G_LOG_DOMAIN,                               \
446             G_LOG_LEVEL_ERROR,                          \
447             "file %s: line %d: should not be reached",  \
448             __FILE__,                                   \
449             __LINE__);          }G_STMT_END
450
451 #endif /* __GNUC__ */
452
453 #endif /* !G_DISABLE_ASSERT */
454
455
456 #ifdef G_DISABLE_CHECKS
457
458 #define g_return_if_fail(expr)
459 #define g_return_val_if_fail(expr,val)
460 #define g_return_if_reached() return
461 #define g_return_val_if_reached(val) return (val)
462
463 #else /* !G_DISABLE_CHECKS */
464
465 #ifdef __GNUC__
466
467 #define g_return_if_fail(expr)          G_STMT_START{                   \
468      if (!(expr))                                                       \
469        {                                                                \
470          g_log (G_LOG_DOMAIN,                                           \
471                 G_LOG_LEVEL_CRITICAL,                                   \
472                 "file %s: line %d (%s): assertion `%s' failed",         \
473                 __FILE__,                                               \
474                 __LINE__,                                               \
475                 __PRETTY_FUNCTION__,                                    \
476                 #expr);                                                 \
477          return;                                                        \
478        };                               }G_STMT_END
479
480 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
481      if (!(expr))                                                       \
482        {                                                                \
483          g_log (G_LOG_DOMAIN,                                           \
484                 G_LOG_LEVEL_CRITICAL,                                   \
485                 "file %s: line %d (%s): assertion `%s' failed",         \
486                 __FILE__,                                               \
487                 __LINE__,                                               \
488                 __PRETTY_FUNCTION__,                                    \
489                 #expr);                                                 \
490          return (val);                                                  \
491        };                               }G_STMT_END
492
493 #define g_return_if_reached()           G_STMT_START{                   \
494      g_log (G_LOG_DOMAIN,                                               \
495             G_LOG_LEVEL_CRITICAL,                                       \
496             "file %s: line %d (%s): should not be reached",             \
497             __FILE__,                                                   \
498             __LINE__,                                                   \
499             __PRETTY_FUNCTION__);                                       \
500      return;                            }G_STMT_END
501
502 #define g_return_val_if_reached(val)    G_STMT_START{                   \
503      g_log (G_LOG_DOMAIN,                                               \
504             G_LOG_LEVEL_CRITICAL,                                       \
505             "file %s: line %d (%s): should not be reached",             \
506             __FILE__,                                                   \
507             __LINE__,                                                   \
508             __PRETTY_FUNCTION__);                                       \
509      return (val);                      }G_STMT_END
510
511 #else /* !__GNUC__ */
512
513 #define g_return_if_fail(expr)          G_STMT_START{           \
514      if (!(expr))                                               \
515        {                                                        \
516          g_log (G_LOG_DOMAIN,                                   \
517                 G_LOG_LEVEL_CRITICAL,                           \
518                 "file %s: line %d: assertion `%s' failed",      \
519                 __FILE__,                                       \
520                 __LINE__,                                       \
521                 #expr);                                         \
522          return;                                                \
523        };                               }G_STMT_END
524
525 #define g_return_val_if_fail(expr, val) G_STMT_START{           \
526      if (!(expr))                                               \
527        {                                                        \
528          g_log (G_LOG_DOMAIN,                                   \
529                 G_LOG_LEVEL_CRITICAL,                           \
530                 "file %s: line %d: assertion `%s' failed",      \
531                 __FILE__,                                       \
532                 __LINE__,                                       \
533                 #expr);                                         \
534          return (val);                                          \
535        };                               }G_STMT_END
536
537 #define g_return_if_reached()           G_STMT_START{           \
538      g_log (G_LOG_DOMAIN,                                       \
539             G_LOG_LEVEL_CRITICAL,                               \
540             "file %s: line %d: should not be reached",          \
541             __FILE__,                                           \
542             __LINE__);                                          \
543      return;                            }G_STMT_END
544
545 #define g_return_val_if_reached(val)    G_STMT_START{           \
546      g_log (G_LOG_DOMAIN,                                       \
547             G_LOG_LEVEL_CRITICAL,                               \
548             "file %s: line %d: should not be reached",          \
549             __FILE__,                                           \
550             __LINE__);                                          \
551      return (val);                      }G_STMT_END
552
553 #endif /* !__GNUC__ */
554
555 #endif /* !G_DISABLE_CHECKS */
556
557
558 /* Provide type definitions for commonly used types.
559  *  These are useful because a "gint8" can be adjusted
560  *  to be 1 byte (8 bits) on all platforms. Similarly and
561  *  more importantly, "gint32" can be adjusted to be
562  *  4 bytes (32 bits) on all platforms.
563  */
564
565 typedef char   gchar;
566 typedef short  gshort;
567 typedef long   glong;
568 typedef int    gint;
569 typedef gint   gboolean;
570 typedef gchar* gstring;
571
572 typedef unsigned char   guchar;
573 typedef unsigned short  gushort;
574 typedef unsigned long   gulong;
575 typedef unsigned int    guint;
576
577 typedef float   gfloat;
578 typedef double  gdouble;
579
580 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
581  * Since gldouble isn't used anywhere, just disable it for now */
582
583 #if 0
584 #ifdef HAVE_LONG_DOUBLE
585 typedef long double gldouble;
586 #else /* HAVE_LONG_DOUBLE */
587 typedef double gldouble;
588 #endif /* HAVE_LONG_DOUBLE */
589 #endif /* 0 */
590
591 typedef void* gpointer;
592 typedef const void *gconstpointer;
593
594
595 typedef guint32 GQuark;
596 typedef gint32  GTime;
597
598
599 /* Portable endian checks and conversions
600  *
601  * glibconfig.h defines G_BYTE_ORDER which expands to one of
602  * the below macros.
603  */
604 #define G_LITTLE_ENDIAN 1234
605 #define G_BIG_ENDIAN    4321
606 #define G_PDP_ENDIAN    3412            /* unused, need specific PDP check */   
607
608
609 /* Basic bit swapping functions
610  */
611 #define GUINT16_SWAP_LE_BE_CONSTANT(val)        ((guint16) ( \
612     (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
613     (((guint16) (val) & (guint16) 0xff00U) >> 8)))
614 #define GUINT32_SWAP_LE_BE_CONSTANT(val)        ((guint32) ( \
615     (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
616     (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) | \
617     (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) | \
618     (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
619
620 /* Intel specific stuff for speed
621  */
622 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
623 #  define GUINT16_SWAP_LE_BE_X86(val) \
624      (__extension__                                     \
625       ({ register guint16 __v;                          \
626          if (__builtin_constant_p (val))                \
627            __v = GUINT16_SWAP_LE_BE_CONSTANT (val);     \
628          else                                           \
629            __asm__ __const__ ("rorw $8, %w0"            \
630                               : "=r" (__v)              \
631                               : "0" ((guint16) (val))); \
632         __v; }))
633 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
634 #  if !defined(__i486__) && !defined(__i586__) \
635       && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
636 #     define GUINT32_SWAP_LE_BE_X86(val) \
637         (__extension__                                          \
638          ({ register guint32 __v;                               \
639             if (__builtin_constant_p (val))                     \
640               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
641           else                                                  \
642             __asm__ __const__ ("rorw $8, %w0\n\t"               \
643                                "rorl $16, %0\n\t"               \
644                                "rorw $8, %w0"                   \
645                                : "=r" (__v)                     \
646                                : "0" ((guint32) (val)));        \
647         __v; }))
648 #  else /* 486 and higher has bswap */
649 #     define GUINT32_SWAP_LE_BE_X86(val) \
650         (__extension__                                          \
651          ({ register guint32 __v;                               \
652             if (__builtin_constant_p (val))                     \
653               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
654           else                                                  \
655             __asm__ __const__ ("bswap %0"                       \
656                                : "=r" (__v)                     \
657                                : "0" ((guint32) (val)));        \
658         __v; }))
659 #  endif /* processor specific 32-bit stuff */
660 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
661 #else /* !__i386__ */
662 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
663 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
664 #endif /* __i386__ */
665
666 #ifdef G_HAVE_GINT64
667 #  define GUINT64_SWAP_LE_BE_CONSTANT(val)      ((guint64) ( \
668       (((guint64) (val) &                                               \
669         (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) |      \
670       (((guint64) (val) &                                               \
671         (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) |      \
672       (((guint64) (val) &                                               \
673         (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) |      \
674       (((guint64) (val) &                                               \
675         (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) <<  8) |      \
676       (((guint64) (val) &                                               \
677         (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >>  8) |      \
678       (((guint64) (val) &                                               \
679         (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) |      \
680       (((guint64) (val) &                                               \
681         (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) |      \
682       (((guint64) (val) &                                               \
683         (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
684 #  if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
685 #    define GUINT64_SWAP_LE_BE_X86(val) \
686         (__extension__                                          \
687          ({ union { guint64 __ll;                               \
688                     guint32 __l[2]; } __r;                      \
689             if (__builtin_constant_p (val))                     \
690               __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val);     \
691             else                                                \
692               {                                                 \
693                 union { guint64 __ll;                           \
694                         guint32 __l[2]; } __w;                  \
695                 __w.__ll = ((guint64) val);                     \
696                 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]);   \
697                 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]);   \
698               }                                                 \
699           __r.__ll; }))
700 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
701 #  else /* !__i386__ */
702 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
703 #  endif
704 #endif
705
706 #define GUINT16_SWAP_LE_PDP(val)        ((guint16) (val))
707 #define GUINT16_SWAP_BE_PDP(val)        (GUINT16_SWAP_LE_BE (val))
708 #define GUINT32_SWAP_LE_PDP(val)        ((guint32) ( \
709     (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
710     (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
711 #define GUINT32_SWAP_BE_PDP(val)        ((guint32) ( \
712     (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
713     (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
714
715 /* The G*_TO_?E() macros are defined in glibconfig.h.
716  * The transformation is symmetric, so the FROM just maps to the TO.
717  */
718 #define GINT16_FROM_LE(val)     (GINT16_TO_LE (val))
719 #define GUINT16_FROM_LE(val)    (GUINT16_TO_LE (val))
720 #define GINT16_FROM_BE(val)     (GINT16_TO_BE (val))
721 #define GUINT16_FROM_BE(val)    (GUINT16_TO_BE (val))
722 #define GINT32_FROM_LE(val)     (GINT32_TO_LE (val))
723 #define GUINT32_FROM_LE(val)    (GUINT32_TO_LE (val))
724 #define GINT32_FROM_BE(val)     (GINT32_TO_BE (val))
725 #define GUINT32_FROM_BE(val)    (GUINT32_TO_BE (val))
726
727 #ifdef G_HAVE_GINT64
728 #define GINT64_FROM_LE(val)     (GINT64_TO_LE (val))
729 #define GUINT64_FROM_LE(val)    (GUINT64_TO_LE (val))
730 #define GINT64_FROM_BE(val)     (GINT64_TO_BE (val))
731 #define GUINT64_FROM_BE(val)    (GUINT64_TO_BE (val))
732 #endif
733
734 #define GLONG_FROM_LE(val)      (GLONG_TO_LE (val))
735 #define GULONG_FROM_LE(val)     (GULONG_TO_LE (val))
736 #define GLONG_FROM_BE(val)      (GLONG_TO_BE (val))
737 #define GULONG_FROM_BE(val)     (GULONG_TO_BE (val))
738
739 #define GINT_FROM_LE(val)       (GINT_TO_LE (val))
740 #define GUINT_FROM_LE(val)      (GUINT_TO_LE (val))
741 #define GINT_FROM_BE(val)       (GINT_TO_BE (val))
742 #define GUINT_FROM_BE(val)      (GUINT_TO_BE (val))
743
744
745 /* Portable versions of host-network order stuff
746  */
747 #define g_ntohl(val) (GUINT32_FROM_BE (val))
748 #define g_ntohs(val) (GUINT16_FROM_BE (val))
749 #define g_htonl(val) (GUINT32_TO_BE (val))
750 #define g_htons(val) (GUINT16_TO_BE (val))
751
752
753 /* Glib version.
754  * we prefix variable declarations so they can
755  * properly get exported in windows dlls.
756  */
757 #ifdef G_OS_WIN32
758 #  ifdef GLIB_COMPILATION
759 #    define GLIB_VAR __declspec(dllexport)
760 #  else /* !GLIB_COMPILATION */
761 #    define GLIB_VAR extern __declspec(dllimport)
762 #  endif /* !GLIB_COMPILATION */
763 #else /* !G_OS_WIN32 */
764 #  define GLIB_VAR extern
765 #endif /* !G_OS_WIN32 */
766
767 GLIB_VAR const guint glib_major_version;
768 GLIB_VAR const guint glib_minor_version;
769 GLIB_VAR const guint glib_micro_version;
770 GLIB_VAR const guint glib_interface_age;
771 GLIB_VAR const guint glib_binary_age;
772
773 #define GLIB_CHECK_VERSION(major,minor,micro)    \
774     (GLIB_MAJOR_VERSION > (major) || \
775      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
776      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
777       GLIB_MICRO_VERSION >= (micro)))
778
779 /* Forward declarations of glib types.
780  */
781 typedef struct _GAllocator      GAllocator;
782 typedef struct _GArray          GArray;
783 typedef struct _GByteArray      GByteArray;
784 typedef struct _GCache          GCache;
785 typedef struct _GCompletion     GCompletion;
786 typedef struct _GData           GData;
787 typedef struct _GDebugKey       GDebugKey;
788 typedef union  _GDoubleIEEE754  GDoubleIEEE754;
789 typedef union  _GFloatIEEE754   GFloatIEEE754;
790 typedef struct _GHashTable      GHashTable;
791 typedef struct _GHook           GHook;
792 typedef struct _GHookList       GHookList;
793 typedef struct _GList           GList;
794 typedef struct _GMemChunk       GMemChunk;
795 typedef struct _GNode           GNode;
796 typedef struct _GPtrArray       GPtrArray;
797 typedef struct _GQueue          GQueue;
798 typedef struct _GRand           GRand;
799 typedef struct _GRelation       GRelation;
800 typedef struct _GScanner        GScanner;
801 typedef struct _GScannerConfig  GScannerConfig;
802 typedef struct _GSList          GSList;
803 typedef struct _GString         GString;
804 typedef struct _GStringChunk    GStringChunk;
805 typedef struct _GTimer          GTimer;
806 typedef struct _GTrashStack     GTrashStack;
807 typedef struct _GTree           GTree;
808 typedef struct _GTuples         GTuples;
809 typedef union  _GTokenValue     GTokenValue;
810 typedef struct _GIOChannel      GIOChannel;
811
812 /* Tree traverse flags */
813 typedef enum
814 {
815   G_TRAVERSE_LEAFS      = 1 << 0,
816   G_TRAVERSE_NON_LEAFS  = 1 << 1,
817   G_TRAVERSE_ALL        = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
818   G_TRAVERSE_MASK       = 0x03
819 } GTraverseFlags;
820
821 /* Tree traverse orders */
822 typedef enum
823 {
824   G_IN_ORDER,
825   G_PRE_ORDER,
826   G_POST_ORDER,
827   G_LEVEL_ORDER
828 } GTraverseType;
829
830 /* Log level shift offset for user defined
831  * log levels (0-7 are used by GLib).
832  */
833 #define G_LOG_LEVEL_USER_SHIFT  (8)
834
835 /* Glib log levels and flags.
836  */
837 typedef enum
838 {
839   /* log flags */
840   G_LOG_FLAG_RECURSION          = 1 << 0,
841   G_LOG_FLAG_FATAL              = 1 << 1,
842   
843   /* GLib log levels */
844   G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
845   G_LOG_LEVEL_CRITICAL          = 1 << 3,
846   G_LOG_LEVEL_WARNING           = 1 << 4,
847   G_LOG_LEVEL_MESSAGE           = 1 << 5,
848   G_LOG_LEVEL_INFO              = 1 << 6,
849   G_LOG_LEVEL_DEBUG             = 1 << 7,
850   
851   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
852 } GLogLevelFlags;
853
854 /* GLib log levels that are considered fatal by default */
855 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
856
857
858 typedef gpointer        (*GCacheNewFunc)        (gpointer       key);
859 typedef gpointer        (*GCacheDupFunc)        (gpointer       value);
860 typedef void            (*GCacheDestroyFunc)    (gpointer       value);
861 typedef gint            (*GCompareFunc)         (gconstpointer  a,
862                                                  gconstpointer  b);
863 typedef gchar*          (*GCompletionFunc)      (gpointer);
864 typedef void            (*GDestroyNotify)       (gpointer       data);
865 typedef void            (*GDataForeachFunc)     (GQuark         key_id,
866                                                  gpointer       data,
867                                                  gpointer       user_data);
868 typedef void            (*GFunc)                (gpointer       data,
869                                                  gpointer       user_data);
870 typedef guint           (*GHashFunc)            (gconstpointer  key);
871 typedef void            (*GFreeFunc)            (gpointer       data);
872 typedef void            (*GHFunc)               (gpointer       key,
873                                                  gpointer       value,
874                                                  gpointer       user_data);
875 typedef gboolean        (*GHRFunc)              (gpointer       key,
876                                                  gpointer       value,
877                                                  gpointer       user_data);
878 typedef gint            (*GHookCompareFunc)     (GHook          *new_hook,
879                                                  GHook          *sibling);
880 typedef gboolean        (*GHookFindFunc)        (GHook          *hook,
881                                                  gpointer        data);
882 typedef void            (*GHookMarshaller)      (GHook          *hook,
883                                                  gpointer        data);
884 typedef gboolean        (*GHookCheckMarshaller) (GHook          *hook,
885                                                  gpointer        data);
886 typedef void            (*GHookFunc)            (gpointer        data);
887 typedef gboolean        (*GHookCheckFunc)       (gpointer        data);
888 typedef void            (*GHookFreeFunc)        (GHookList      *hook_list,
889                                                  GHook          *hook);
890 typedef void            (*GLogFunc)             (const gchar   *log_domain,
891                                                  GLogLevelFlags log_level,
892                                                  const gchar   *message,
893                                                  gpointer       user_data);
894 typedef gboolean        (*GNodeTraverseFunc)    (GNode         *node,
895                                                  gpointer       data);
896 typedef void            (*GNodeForeachFunc)     (GNode         *node,
897                                                  gpointer       data);
898 typedef void            (*GScannerMsgFunc)      (GScanner      *scanner,
899                                                  gchar         *message,
900                                                  gint           error);
901 typedef gint            (*GTraverseFunc)        (gpointer       key,
902                                                  gpointer       value,
903                                                  gpointer       data);
904 typedef void            (*GVoidFunc)            (void);
905
906
907 struct _GArray
908 {
909   gchar *data;
910   guint len;
911 };
912
913 struct _GByteArray
914 {
915   guint8 *data;
916   guint   len;
917 };
918
919 struct _GDebugKey
920 {
921   gchar *key;
922   guint  value;
923 };
924
925 struct _GList
926 {
927   gpointer data;
928   GList *next;
929   GList *prev;
930 };
931
932 struct _GPtrArray
933 {
934   gpointer *pdata;
935   guint     len;
936 };
937
938 struct _GQueue
939 {
940   GList *head;
941   GList *tail;
942   guint  length;
943 };
944
945 struct _GSList
946 {
947   gpointer data;
948   GSList *next;
949 };
950
951 struct _GString
952 {
953   gchar *str;
954   gint len;
955 };
956
957 struct _GTrashStack
958 {
959   GTrashStack *next;
960 };
961
962 struct _GTuples
963 {
964   guint len;
965 };
966
967 #ifdef __cplusplus
968 }
969 #endif /* __cplusplus */
970
971 #include <gerror.h>
972
973 #ifdef __cplusplus
974 extern "C" {
975 #endif /* __cplusplus */
976
977 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
978  *
979  *        31 30           23 22            0
980  * +--------+---------------+---------------+
981  * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
982  * +--------+---------------+---------------+
983  * B0------------------->B1------->B2-->B3-->
984  *
985  * IEEE Standard 754 Double Precision Storage Format (gdouble):
986  *
987  *        63 62            52 51            32   31            0
988  * +--------+----------------+----------------+ +---------------+
989  * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
990  * +--------+----------------+----------------+ +---------------+
991  * B0--------------->B1---------->B2--->B3---->  B4->B5->B6->B7->
992  */
993 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
994 #define G_IEEE754_FLOAT_BIAS    (127)
995 #define G_IEEE754_DOUBLE_BIAS   (1023)
996 /* multiply with base2 exponent to get base10 exponent (nomal numbers) */
997 #define G_LOG_2_BASE_10         (0.30102999566398119521)
998 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
999 union _GFloatIEEE754
1000 {
1001   gfloat v_float;
1002   struct {
1003     guint mantissa : 23;
1004     guint biased_exponent : 8;
1005     guint sign : 1;
1006   } mpn;
1007 };
1008 union _GDoubleIEEE754
1009 {
1010   gdouble v_double;
1011   struct {
1012     guint mantissa_low : 32;
1013     guint mantissa_high : 20;
1014     guint biased_exponent : 11;
1015     guint sign : 1;
1016   } mpn;
1017 };
1018 #elif G_BYTE_ORDER == G_BIG_ENDIAN
1019 union _GFloatIEEE754
1020 {
1021   gfloat v_float;
1022   struct {
1023     guint sign : 1;
1024     guint biased_exponent : 8;
1025     guint mantissa : 23;
1026   } mpn;
1027 };
1028 union _GDoubleIEEE754
1029 {
1030   gdouble v_double;
1031   struct {
1032     guint sign : 1;
1033     guint biased_exponent : 11;
1034     guint mantissa_high : 20;
1035     guint mantissa_low : 32;
1036   } mpn;
1037 };
1038 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
1039 #error unknown ENDIAN type
1040 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
1041
1042
1043 /* Doubly linked lists
1044  */
1045 void   g_list_push_allocator    (GAllocator     *allocator);
1046 void   g_list_pop_allocator     (void);
1047 GList* g_list_alloc             (void);
1048 void   g_list_free              (GList          *list);
1049 void   g_list_free_1            (GList          *list);
1050 GList* g_list_append            (GList          *list,
1051                                  gpointer        data);
1052 GList* g_list_prepend           (GList          *list,
1053                                  gpointer        data);
1054 GList* g_list_insert            (GList          *list,
1055                                  gpointer        data,
1056                                  gint            position);
1057 GList* g_list_insert_sorted     (GList          *list,
1058                                  gpointer        data,
1059                                  GCompareFunc    func);
1060 GList* g_list_concat            (GList          *list1,
1061                                  GList          *list2);
1062 GList* g_list_remove            (GList          *list,
1063                                  gconstpointer   data);
1064 GList* g_list_remove_link       (GList          *list,
1065                                  GList          *llink);
1066 GList* g_list_delete_link       (GList          *list,
1067                                  GList          *link);
1068 GList* g_list_reverse           (GList          *list);
1069 GList* g_list_copy              (GList          *list);
1070 GList* g_list_nth               (GList          *list,
1071                                  guint           n);
1072 GList* g_list_find              (GList          *list,
1073                                  gconstpointer   data);
1074 GList* g_list_find_custom       (GList          *list,
1075                                  gconstpointer   data,
1076                                  GCompareFunc    func);
1077 gint   g_list_position          (GList          *list,
1078                                  GList          *llink);
1079 gint   g_list_index             (GList          *list,
1080                                  gconstpointer   data);
1081 GList* g_list_last              (GList          *list);
1082 GList* g_list_first             (GList          *list);
1083 guint  g_list_length            (GList          *list);
1084 void   g_list_foreach           (GList          *list,
1085                                  GFunc           func,
1086                                  gpointer        user_data);
1087 GList* g_list_sort              (GList          *list,
1088                                  GCompareFunc    compare_func);
1089 gpointer g_list_nth_data        (GList          *list,
1090                                  guint           n);
1091 #define g_list_previous(list)   ((list) ? (((GList *)(list))->prev) : NULL)
1092 #define g_list_next(list)       ((list) ? (((GList *)(list))->next) : NULL)
1093
1094
1095 /* Singly linked lists
1096  */
1097 void    g_slist_push_allocator  (GAllocator     *allocator);
1098 void    g_slist_pop_allocator   (void);
1099 GSList* g_slist_alloc           (void);
1100 void    g_slist_free            (GSList         *list);
1101 void    g_slist_free_1          (GSList         *list);
1102 GSList* g_slist_append          (GSList         *list,
1103                                  gpointer        data);
1104 GSList* g_slist_prepend         (GSList         *list,
1105                                  gpointer        data);
1106 GSList* g_slist_insert          (GSList         *list,
1107                                  gpointer        data,
1108                                  gint            position);
1109 GSList* g_slist_insert_sorted   (GSList         *list,
1110                                  gpointer        data,
1111                                  GCompareFunc    func);
1112 GSList* g_slist_insert_before   (GSList         *slist,
1113                                  GSList         *sibling,
1114                                  gpointer        data);
1115 GSList* g_slist_concat          (GSList         *list1,
1116                                  GSList         *list2);
1117 GSList* g_slist_remove          (GSList         *list,
1118                                  gconstpointer   data);
1119 GSList* g_slist_remove_link     (GSList         *list,
1120                                  GSList         *link);
1121 GSList* g_slist_delete_link     (GSList         *list,
1122                                  GSList         *link);
1123 GSList* g_slist_reverse         (GSList         *list);
1124 GSList* g_slist_copy            (GSList         *list);
1125 GSList* g_slist_nth             (GSList         *list,
1126                                  guint           n);
1127 GSList* g_slist_find            (GSList         *list,
1128                                  gconstpointer   data);
1129 GSList* g_slist_find_custom     (GSList         *list,
1130                                  gconstpointer   data,
1131                                  GCompareFunc    func);
1132 gint    g_slist_position        (GSList         *list,
1133                                  GSList         *llink);
1134 gint    g_slist_index           (GSList         *list,
1135                                  gconstpointer   data);
1136 GSList* g_slist_last            (GSList         *list);
1137 guint   g_slist_length          (GSList         *list);
1138 void    g_slist_foreach         (GSList         *list,
1139                                  GFunc           func,
1140                                  gpointer        user_data);
1141 GSList*  g_slist_sort           (GSList          *list,
1142                                  GCompareFunc    compare_func);
1143 gpointer g_slist_nth_data       (GSList         *list,
1144                                  guint           n);
1145 #define  g_slist_next(slist)    ((slist) ? (((GSList *)(slist))->next) : NULL)
1146
1147
1148 /* Queues
1149  */
1150 GQueue*  g_queue_new            (void);
1151 void     g_queue_free           (GQueue  *queue);
1152 void     g_queue_push_head      (GQueue  *queue,
1153                                  gpointer data);
1154 void     g_queue_push_tail      (GQueue  *queue,
1155                                  gpointer data);
1156 gpointer g_queue_pop_head       (GQueue  *queue);
1157 gpointer g_queue_pop_tail       (GQueue  *queue);
1158 gboolean g_queue_is_empty       (GQueue  *queue);
1159 gpointer g_queue_peek_head      (GQueue  *queue);
1160 gpointer g_queue_peek_tail      (GQueue  *queue);
1161 void     g_queue_push_head_link (GQueue  *queue,
1162                                  GList   *link);
1163 void     g_queue_push_tail_link (GQueue  *queue,
1164                                  GList   *link);
1165 GList*   g_queue_pop_head_link  (GQueue  *queue);
1166 GList*   g_queue_pop_tail_link  (GQueue  *queue);
1167
1168 /* Hash tables
1169  */
1170 GHashTable* g_hash_table_new            (GHashFunc       hash_func,
1171                                          GCompareFunc    key_compare_func);
1172 void        g_hash_table_destroy        (GHashTable     *hash_table);
1173 void        g_hash_table_insert         (GHashTable     *hash_table,
1174                                          gpointer        key,
1175                                          gpointer        value);
1176 void        g_hash_table_remove         (GHashTable     *hash_table,
1177                                          gconstpointer   key);
1178 gpointer    g_hash_table_lookup         (GHashTable     *hash_table,
1179                                          gconstpointer   key);
1180 gboolean    g_hash_table_lookup_extended(GHashTable     *hash_table,
1181                                          gconstpointer   lookup_key,
1182                                          gpointer       *orig_key,
1183                                          gpointer       *value);
1184 void        g_hash_table_foreach        (GHashTable     *hash_table,
1185                                          GHFunc          func,
1186                                          gpointer        user_data);
1187 guint       g_hash_table_foreach_remove (GHashTable     *hash_table,
1188                                          GHRFunc         func,
1189                                          gpointer        user_data);
1190 guint       g_hash_table_size           (GHashTable     *hash_table);
1191
1192 /* The following two functions are deprecated and will be removed in
1193  * the next major release. They do no good. */
1194 void        g_hash_table_freeze         (GHashTable     *hash_table);
1195 void        g_hash_table_thaw           (GHashTable     *hash_table);
1196
1197 /* Caches
1198  */
1199 GCache*  g_cache_new           (GCacheNewFunc      value_new_func,
1200                                 GCacheDestroyFunc  value_destroy_func,
1201                                 GCacheDupFunc      key_dup_func,
1202                                 GCacheDestroyFunc  key_destroy_func,
1203                                 GHashFunc          hash_key_func,
1204                                 GHashFunc          hash_value_func,
1205                                 GCompareFunc       key_compare_func);
1206 void     g_cache_destroy       (GCache            *cache);
1207 gpointer g_cache_insert        (GCache            *cache,
1208                                 gpointer           key);
1209 void     g_cache_remove        (GCache            *cache,
1210                                 gconstpointer      value);
1211 void     g_cache_key_foreach   (GCache            *cache,
1212                                 GHFunc             func,
1213                                 gpointer           user_data);
1214 void     g_cache_value_foreach (GCache            *cache,
1215                                 GHFunc             func,
1216                                 gpointer           user_data);
1217
1218
1219 /* Balanced binary trees
1220  */
1221 GTree*   g_tree_new      (GCompareFunc   key_compare_func);
1222 void     g_tree_destroy  (GTree         *tree);
1223 void     g_tree_insert   (GTree         *tree,
1224                           gpointer       key,
1225                           gpointer       value);
1226 void     g_tree_remove   (GTree         *tree,
1227                           gconstpointer  key);
1228 gpointer g_tree_lookup   (GTree         *tree,
1229                           gconstpointer  key);
1230 void     g_tree_traverse (GTree         *tree,
1231                           GTraverseFunc  traverse_func,
1232                           GTraverseType  traverse_type,
1233                           gpointer       data);
1234 gpointer g_tree_search   (GTree         *tree,
1235                           GCompareFunc   search_func,
1236                           gconstpointer  data);
1237 gint     g_tree_height   (GTree         *tree);
1238 gint     g_tree_nnodes   (GTree         *tree);
1239
1240
1241
1242 /* N-way tree implementation
1243  */
1244 struct _GNode
1245 {
1246   gpointer data;
1247   GNode   *next;
1248   GNode   *prev;
1249   GNode   *parent;
1250   GNode   *children;
1251 };
1252
1253 #define  G_NODE_IS_ROOT(node)   (((GNode*) (node))->parent == NULL && \
1254                                  ((GNode*) (node))->prev == NULL && \
1255                                  ((GNode*) (node))->next == NULL)
1256 #define  G_NODE_IS_LEAF(node)   (((GNode*) (node))->children == NULL)
1257
1258 void     g_node_push_allocator  (GAllocator       *allocator);
1259 void     g_node_pop_allocator   (void);
1260 GNode*   g_node_new             (gpointer          data);
1261 void     g_node_destroy         (GNode            *root);
1262 void     g_node_unlink          (GNode            *node);
1263 GNode*   g_node_copy            (GNode            *node);
1264 GNode*   g_node_insert          (GNode            *parent,
1265                                  gint              position,
1266                                  GNode            *node);
1267 GNode*   g_node_insert_before   (GNode            *parent,
1268                                  GNode            *sibling,
1269                                  GNode            *node);
1270 GNode*   g_node_prepend         (GNode            *parent,
1271                                  GNode            *node);
1272 guint    g_node_n_nodes         (GNode            *root,
1273                                  GTraverseFlags    flags);
1274 GNode*   g_node_get_root        (GNode            *node);
1275 gboolean g_node_is_ancestor     (GNode            *node,
1276                                  GNode            *descendant);
1277 guint    g_node_depth           (GNode            *node);
1278 GNode*   g_node_find            (GNode            *root,
1279                                  GTraverseType     order,
1280                                  GTraverseFlags    flags,
1281                                  gpointer          data);
1282
1283 /* convenience macros */
1284 #define g_node_append(parent, node)                             \
1285      g_node_insert_before ((parent), NULL, (node))
1286 #define g_node_insert_data(parent, position, data)              \
1287      g_node_insert ((parent), (position), g_node_new (data))
1288 #define g_node_insert_data_before(parent, sibling, data)        \
1289      g_node_insert_before ((parent), (sibling), g_node_new (data))
1290 #define g_node_prepend_data(parent, data)                       \
1291      g_node_prepend ((parent), g_node_new (data))
1292 #define g_node_append_data(parent, data)                        \
1293      g_node_insert_before ((parent), NULL, g_node_new (data))
1294
1295 /* traversal function, assumes that `node' is root
1296  * (only traverses `node' and its subtree).
1297  * this function is just a high level interface to
1298  * low level traversal functions, optimized for speed.
1299  */
1300 void     g_node_traverse        (GNode            *root,
1301                                  GTraverseType     order,
1302                                  GTraverseFlags    flags,
1303                                  gint              max_depth,
1304                                  GNodeTraverseFunc func,
1305                                  gpointer          data);
1306
1307 /* return the maximum tree height starting with `node', this is an expensive
1308  * operation, since we need to visit all nodes. this could be shortened by
1309  * adding `guint height' to struct _GNode, but then again, this is not very
1310  * often needed, and would make g_node_insert() more time consuming.
1311  */
1312 guint    g_node_max_height       (GNode *root);
1313
1314 void     g_node_children_foreach (GNode           *node,
1315                                   GTraverseFlags   flags,
1316                                   GNodeForeachFunc func,
1317                                   gpointer         data);
1318 void     g_node_reverse_children (GNode           *node);
1319 guint    g_node_n_children       (GNode           *node);
1320 GNode*   g_node_nth_child        (GNode           *node,
1321                                   guint            n);
1322 GNode*   g_node_last_child       (GNode           *node);
1323 GNode*   g_node_find_child       (GNode           *node,
1324                                   GTraverseFlags   flags,
1325                                   gpointer         data);
1326 gint     g_node_child_position   (GNode           *node,
1327                                   GNode           *child);
1328 gint     g_node_child_index      (GNode           *node,
1329                                   gpointer         data);
1330
1331 GNode*   g_node_first_sibling    (GNode           *node);
1332 GNode*   g_node_last_sibling     (GNode           *node);
1333
1334 #define  g_node_prev_sibling(node)      ((node) ? \
1335                                          ((GNode*) (node))->prev : NULL)
1336 #define  g_node_next_sibling(node)      ((node) ? \
1337                                          ((GNode*) (node))->next : NULL)
1338 #define  g_node_first_child(node)       ((node) ? \
1339                                          ((GNode*) (node))->children : NULL)
1340
1341
1342 /* Callback maintenance functions
1343  */
1344 #define G_HOOK_FLAG_USER_SHIFT  (4)
1345 typedef enum
1346 {
1347   G_HOOK_FLAG_ACTIVE    = 1 << 0,
1348   G_HOOK_FLAG_IN_CALL   = 1 << 1,
1349   G_HOOK_FLAG_MASK      = 0x0f
1350 } GHookFlagMask;
1351
1352 #define G_HOOK_DEFERRED_DESTROY ((GHookFreeFunc) 0x01)
1353
1354 struct _GHookList
1355 {
1356   guint          seq_id;
1357   guint          hook_size;
1358   guint          is_setup : 1;
1359   GHook         *hooks;
1360   GMemChunk     *hook_memchunk;
1361   GHookFreeFunc  hook_free; /* virtual function */
1362   GHookFreeFunc  hook_destroy; /* virtual function */
1363 };
1364
1365 struct _GHook
1366 {
1367   gpointer       data;
1368   GHook         *next;
1369   GHook         *prev;
1370   guint          ref_count;
1371   guint          hook_id;
1372   guint          flags;
1373   gpointer       func;
1374   GDestroyNotify destroy;
1375 };
1376
1377 #define G_HOOK_ACTIVE(hook)             ((((GHook*) hook)->flags & \
1378                                           G_HOOK_FLAG_ACTIVE) != 0)
1379 #define G_HOOK_IN_CALL(hook)            ((((GHook*) hook)->flags & \
1380                                           G_HOOK_FLAG_IN_CALL) != 0)
1381 #define G_HOOK_IS_VALID(hook)           (((GHook*) hook)->hook_id != 0 && \
1382                                          G_HOOK_ACTIVE (hook))
1383 #define G_HOOK_IS_UNLINKED(hook)        (((GHook*) hook)->next == NULL && \
1384                                          ((GHook*) hook)->prev == NULL && \
1385                                          ((GHook*) hook)->hook_id == 0 && \
1386                                          ((GHook*) hook)->ref_count == 0)
1387
1388 void     g_hook_list_init               (GHookList              *hook_list,
1389                                          guint                   hook_size);
1390 void     g_hook_list_clear              (GHookList              *hook_list);
1391 GHook*   g_hook_alloc                   (GHookList              *hook_list);
1392 void     g_hook_free                    (GHookList              *hook_list,
1393                                          GHook                  *hook);
1394 void     g_hook_ref                     (GHookList              *hook_list,
1395                                          GHook                  *hook);
1396 void     g_hook_unref                   (GHookList              *hook_list,
1397                                          GHook                  *hook);
1398 gboolean g_hook_destroy                 (GHookList              *hook_list,
1399                                          guint                   hook_id);
1400 void     g_hook_destroy_link            (GHookList              *hook_list,
1401                                          GHook                  *hook);
1402 void     g_hook_prepend                 (GHookList              *hook_list,
1403                                          GHook                  *hook);
1404 void     g_hook_insert_before           (GHookList              *hook_list,
1405                                          GHook                  *sibling,
1406                                          GHook                  *hook);
1407 void     g_hook_insert_sorted           (GHookList              *hook_list,
1408                                          GHook                  *hook,
1409                                          GHookCompareFunc        func);
1410 GHook*   g_hook_get                     (GHookList              *hook_list,
1411                                          guint                   hook_id);
1412 GHook*   g_hook_find                    (GHookList              *hook_list,
1413                                          gboolean                need_valids,
1414                                          GHookFindFunc           func,
1415                                          gpointer                data);
1416 GHook*   g_hook_find_data               (GHookList              *hook_list,
1417                                          gboolean                need_valids,
1418                                          gpointer                data);
1419 GHook*   g_hook_find_func               (GHookList              *hook_list,
1420                                          gboolean                need_valids,
1421                                          gpointer                func);
1422 GHook*   g_hook_find_func_data          (GHookList              *hook_list,
1423                                          gboolean                need_valids,
1424                                          gpointer                func,
1425                                          gpointer                data);
1426 /* return the first valid hook, and increment its reference count */
1427 GHook*   g_hook_first_valid             (GHookList              *hook_list,
1428                                          gboolean                may_be_in_call);
1429 /* return the next valid hook with incremented reference count, and
1430  * decrement the reference count of the original hook
1431  */
1432 GHook*   g_hook_next_valid              (GHookList              *hook_list,
1433                                          GHook                  *hook,
1434                                          gboolean                may_be_in_call);
1435
1436 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1437 gint     g_hook_compare_ids             (GHook                  *new_hook,
1438                                          GHook                  *sibling);
1439
1440 /* convenience macros */
1441 #define  g_hook_append( hook_list, hook )  \
1442      g_hook_insert_before ((hook_list), NULL, (hook))
1443
1444 /* invoke all valid hooks with the (*GHookFunc) signature.
1445  */
1446 void     g_hook_list_invoke             (GHookList              *hook_list,
1447                                          gboolean                may_recurse);
1448 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1449  * and destroy the hook if FALSE is returned.
1450  */
1451 void     g_hook_list_invoke_check       (GHookList              *hook_list,
1452                                          gboolean                may_recurse);
1453 /* invoke a marshaller on all valid hooks.
1454  */
1455 void     g_hook_list_marshal            (GHookList              *hook_list,
1456                                          gboolean                may_recurse,
1457                                          GHookMarshaller         marshaller,
1458                                          gpointer                data);
1459 void     g_hook_list_marshal_check      (GHookList              *hook_list,
1460                                          gboolean                may_recurse,
1461                                          GHookCheckMarshaller    marshaller,
1462                                          gpointer                data);
1463
1464
1465 /* Fatal error handlers.
1466  * g_on_error_query() will prompt the user to either
1467  * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1468  * g_on_error_stack_trace() invokes gdb, which attaches to the current
1469  * process and shows a stack trace.
1470  * These function may cause different actions on non-unix platforms.
1471  * The prg_name arg is required by gdb to find the executable, if it is
1472  * passed as NULL, g_on_error_query() will try g_get_prgname().
1473  */
1474 void g_on_error_query (const gchar *prg_name);
1475 void g_on_error_stack_trace (const gchar *prg_name);
1476
1477
1478 /* Logging mechanism
1479  */
1480 extern          const gchar             *g_log_domain_glib;
1481 guint           g_log_set_handler       (const gchar    *log_domain,
1482                                          GLogLevelFlags  log_levels,
1483                                          GLogFunc        log_func,
1484                                          gpointer        user_data);
1485 void            g_log_remove_handler    (const gchar    *log_domain,
1486                                          guint           handler_id);
1487 void            g_log_default_handler   (const gchar    *log_domain,
1488                                          GLogLevelFlags  log_level,
1489                                          const gchar    *message,
1490                                          gpointer        unused_data);
1491 void            g_log                   (const gchar    *log_domain,
1492                                          GLogLevelFlags  log_level,
1493                                          const gchar    *format,
1494                                          ...) G_GNUC_PRINTF (3, 4);
1495 void            g_logv                  (const gchar    *log_domain,
1496                                          GLogLevelFlags  log_level,
1497                                          const gchar    *format,
1498                                          va_list         args);
1499 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
1500                                          GLogLevelFlags  fatal_mask);
1501 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
1502 #ifndef G_LOG_DOMAIN
1503 #define G_LOG_DOMAIN    ((gchar*) 0)
1504 #endif  /* G_LOG_DOMAIN */
1505 #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
1506 #define g_error(...)    g_log (G_LOG_DOMAIN,         \
1507                                G_LOG_LEVEL_ERROR,    \
1508                                __VA_ARGS__)
1509 #define g_message(...)  g_log (G_LOG_DOMAIN,         \
1510                                G_LOG_LEVEL_MESSAGE,  \
1511                                __VA_ARGS__)
1512 #define g_critical(...) g_log (G_LOG_DOMAIN,         \
1513                                G_LOG_LEVEL_CRITICAL, \
1514                                __VA_ARGS__)
1515 #define g_warning(...)  g_log (G_LOG_DOMAIN,         \
1516                                G_LOG_LEVEL_WARNING,  \
1517                                __VA_ARGS__)
1518 #elif __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 4)
1519 #define g_error(format...)      g_log (G_LOG_DOMAIN,         \
1520                                        G_LOG_LEVEL_ERROR,    \
1521                                        format)
1522 #define g_message(format...)    g_log (G_LOG_DOMAIN,         \
1523                                        G_LOG_LEVEL_MESSAGE,  \
1524                                        format)
1525 #define g_critical(format...)   g_log (G_LOG_DOMAIN,         \
1526                                        G_LOG_LEVEL_CRITICAL, \
1527                                        format)
1528 #define g_warning(format...)    g_log (G_LOG_DOMAIN,         \
1529                                        G_LOG_LEVEL_WARNING,  \
1530                                        format)
1531 #else   /* !__GNUC__ */
1532 static void
1533 g_error (const gchar *format,
1534          ...)
1535 {
1536   va_list args;
1537   va_start (args, format);
1538   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1539   va_end (args);
1540 }
1541 static void
1542 g_message (const gchar *format,
1543            ...)
1544 {
1545   va_list args;
1546   va_start (args, format);
1547   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1548   va_end (args);
1549 }
1550 static void
1551 g_critical (const gchar *format,
1552             ...)
1553 {
1554   va_list args;
1555   va_start (args, format);
1556   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
1557   va_end (args);
1558 }
1559 static void
1560 g_warning (const gchar *format,
1561            ...)
1562 {
1563   va_list args;
1564   va_start (args, format);
1565   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1566   va_end (args);
1567 }
1568 #endif  /* !__GNUC__ */
1569
1570 typedef void    (*GPrintFunc)           (const gchar    *string);
1571 void            g_print                 (const gchar    *format,
1572                                          ...) G_GNUC_PRINTF (1, 2);
1573 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
1574 void            g_printerr              (const gchar    *format,
1575                                          ...) G_GNUC_PRINTF (1, 2);
1576 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
1577
1578 /* deprecated compatibility functions, use g_log_set_handler() instead */
1579 typedef void            (*GErrorFunc)           (const gchar *str);
1580 typedef void            (*GWarningFunc)         (const gchar *str);
1581 GErrorFunc   g_set_error_handler   (GErrorFunc   func);
1582 GWarningFunc g_set_warning_handler (GWarningFunc func);
1583 GPrintFunc   g_set_message_handler (GPrintFunc func);
1584
1585
1586 /* Memory allocation and debugging
1587  */
1588 #ifdef USE_DMALLOC
1589
1590 #define g_malloc(size)       ((gpointer) MALLOC (size))
1591 #define g_malloc0(size)      ((gpointer) CALLOC (char, size))
1592 #define g_realloc(mem,size)  ((gpointer) REALLOC (mem, char, size))
1593 #define g_free(mem)          FREE (mem)
1594
1595 #else /* !USE_DMALLOC */
1596
1597 gpointer g_malloc      (gulong    size);
1598 gpointer g_malloc0     (gulong    size);
1599 gpointer g_realloc     (gpointer  mem,
1600                         gulong    size);
1601 void     g_free        (gpointer  mem);
1602
1603 #endif /* !USE_DMALLOC */
1604
1605 void     g_mem_profile (void);
1606 void     g_mem_check   (gpointer  mem);
1607
1608 /* Generic allocators
1609  */
1610 GAllocator* g_allocator_new   (const gchar  *name,
1611                                guint         n_preallocs);
1612 void        g_allocator_free  (GAllocator   *allocator);
1613
1614 #define G_ALLOCATOR_LIST        (1)
1615 #define G_ALLOCATOR_SLIST       (2)
1616 #define G_ALLOCATOR_NODE        (3)
1617
1618
1619 /* "g_mem_chunk_new" creates a new memory chunk.
1620  * Memory chunks are used to allocate pieces of memory which are
1621  *  always the same size. Lists are a good example of such a data type.
1622  * The memory chunk allocates and frees blocks of memory as needed.
1623  *  Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1624  *  allocated in a mem chunk. ("g_free" will most likely cause a seg
1625  *  fault...somewhere).
1626  *
1627  * Oh yeah, GMemChunk is an opaque data type. (You don't really
1628  *  want to know what's going on inside do you?)
1629  */
1630
1631 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1632  *  is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1633  *  atom. (They are also useful for lists which use MemChunk to allocate
1634  *  memory but are also part of the MemChunk implementation).
1635  * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1636  */
1637
1638 #define G_ALLOC_ONLY      1
1639 #define G_ALLOC_AND_FREE  2
1640
1641 GMemChunk* g_mem_chunk_new     (gchar     *name,
1642                                 gint       atom_size,
1643                                 gulong     area_size,
1644                                 gint       type);
1645 void       g_mem_chunk_destroy (GMemChunk *mem_chunk);
1646 gpointer   g_mem_chunk_alloc   (GMemChunk *mem_chunk);
1647 gpointer   g_mem_chunk_alloc0  (GMemChunk *mem_chunk);
1648 void       g_mem_chunk_free    (GMemChunk *mem_chunk,
1649                                 gpointer   mem);
1650 void       g_mem_chunk_clean   (GMemChunk *mem_chunk);
1651 void       g_mem_chunk_reset   (GMemChunk *mem_chunk);
1652 void       g_mem_chunk_print   (GMemChunk *mem_chunk);
1653 void       g_mem_chunk_info    (void);
1654
1655 /* Ah yes...we have a "g_blow_chunks" function.
1656  * "g_blow_chunks" simply compresses all the chunks. This operation
1657  *  consists of freeing every memory area that should be freed (but
1658  *  which we haven't gotten around to doing yet). And, no,
1659  *  "g_blow_chunks" doesn't follow the naming scheme, but it is a
1660  *  much better name than "g_mem_chunk_clean_all" or something
1661  *  similar.
1662  */
1663 void g_blow_chunks (void);
1664
1665
1666 /* Timer
1667  */
1668
1669 /* microseconds per second */
1670 #define G_USEC_PER_SEC 1000000
1671
1672 GTimer* g_timer_new     (void);
1673 void    g_timer_destroy (GTimer  *timer);
1674 void    g_timer_start   (GTimer  *timer);
1675 void    g_timer_stop    (GTimer  *timer);
1676 void    g_timer_reset   (GTimer  *timer);
1677 gdouble g_timer_elapsed (GTimer  *timer,
1678                          gulong  *microseconds);
1679 void    g_usleep        (gulong microseconds);
1680
1681 /* String utility functions that modify a string argument or
1682  * return a constant string that must not be freed.
1683  */
1684 #define  G_STR_DELIMITERS       "_-|> <."
1685 gchar*   g_strdelimit           (gchar       *string,
1686                                  const gchar *delimiters,
1687                                  gchar        new_delimiter);
1688 gchar*   g_strcanon             (gchar       *string,
1689                                  const gchar *valid_chars,
1690                                  gchar        subsitutor);
1691 gdouble  g_strtod               (const gchar *nptr,
1692                                  gchar      **endptr);
1693 gchar*   g_strerror             (gint         errnum) G_GNUC_CONST;
1694 gchar*   g_strsignal            (gint         signum) G_GNUC_CONST;
1695 gint     g_strcasecmp           (const gchar *s1,
1696                                  const gchar *s2);
1697 gint     g_strncasecmp          (const gchar *s1,
1698                                  const gchar *s2,
1699                                  guint        n);
1700 gchar*   g_strdown              (gchar       *string);
1701 gchar*   g_strup                (gchar       *string);
1702 gchar*   g_strreverse           (gchar       *string);
1703 gsize    g_strlcpy              (gchar       *dest,
1704                                  const gchar *src,
1705                                  gsize        dest_size);
1706 gsize    g_strlcat              (gchar       *dest,
1707                                  const gchar *src,
1708                                  gsize        dest_size);
1709 /* removes leading spaces */
1710 gchar*   g_strchug              (gchar        *string);
1711 /* removes trailing spaces */
1712 gchar*  g_strchomp              (gchar        *string);
1713 /* removes leading & trailing spaces */
1714 #define g_strstrip( string )    g_strchomp (g_strchug (string))
1715
1716 /* String utility functions that return a newly allocated string which
1717  * ought to be freed with g_free from the caller at some point.
1718  */
1719 gchar*   g_strdup               (const gchar *str);
1720 gchar*   g_strdup_printf        (const gchar *format,
1721                                  ...) G_GNUC_PRINTF (1, 2);
1722 gchar*   g_strdup_vprintf       (const gchar *format,
1723                                  va_list      args);
1724 gchar*   g_strndup              (const gchar *str,
1725                                  guint        n);
1726 gchar*   g_strnfill             (guint        length,
1727                                  gchar        fill_char);
1728 gchar*   g_strconcat            (const gchar *string1,
1729                                  ...); /* NULL terminated */
1730 gchar*   g_strjoin              (const gchar  *separator,
1731                                  ...); /* NULL terminated */
1732 /* Make a copy of a string interpreting C string -style escape
1733  * sequences. Inverse of g_strescape. The recognized sequences are \b
1734  * \f \n \r \t \\ \" and the octal format.
1735  */
1736 gchar*   g_strcompress          (const gchar *source);
1737
1738 /* Convert between the operating system (or C runtime)
1739  * representation of file names and UTF-8.
1740  */
1741 gchar*   g_filename_to_utf8 (const gchar *opsysstring);
1742 gchar*   g_filename_from_utf8 (const gchar *utf8string);
1743
1744 /* Copy a string escaping nonprintable characters like in C strings.
1745  * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
1746  * to a string containing characters that are not to be escaped.
1747  *
1748  * Deprecated API: gchar* g_strescape (const gchar *source);
1749  * Luckily this function wasn't used much, using NULL as second parameter
1750  * provides mostly identical semantics.
1751  */
1752 gchar*   g_strescape            (const gchar *source,
1753                                  const gchar *exceptions);
1754
1755 gpointer g_memdup               (gconstpointer mem,
1756                                  guint         byte_size);
1757
1758 /* NULL terminated string arrays.
1759  * g_strsplit() splits up string into max_tokens tokens at delim and
1760  * returns a newly allocated string array.
1761  * g_strjoinv() concatenates all of str_array's strings, sliding in an
1762  * optional separator, the returned string is newly allocated.
1763  * g_strfreev() frees the array itself and all of its strings.
1764  */
1765 gchar**  g_strsplit             (const gchar  *string,
1766                                  const gchar  *delimiter,
1767                                  gint          max_tokens);
1768 gchar*   g_strjoinv             (const gchar  *separator,
1769                                  gchar       **str_array);
1770 void     g_strfreev             (gchar       **str_array);
1771
1772
1773
1774 /* calculate a string size, guarranteed to fit format + args.
1775  */
1776 guint   g_printf_string_upper_bound (const gchar* format,
1777                                      va_list      args);
1778
1779
1780 /* Retrive static string info
1781  */
1782 gchar*  g_get_user_name         (void);
1783 gchar*  g_get_real_name         (void);
1784 gchar*  g_get_home_dir          (void);
1785 gchar*  g_get_tmp_dir           (void);
1786 gchar*  g_get_prgname           (void);
1787 void    g_set_prgname           (const gchar *prgname);
1788
1789
1790 /* Miscellaneous utility functions
1791  */
1792 guint   g_parse_debug_string    (const gchar *string,
1793                                  GDebugKey   *keys,
1794                                  guint        nkeys);
1795 gint    g_snprintf              (gchar       *string,
1796                                  gulong       n,
1797                                  gchar const *format,
1798                                  ...) G_GNUC_PRINTF (3, 4);
1799 gint    g_vsnprintf             (gchar       *string,
1800                                  gulong       n,
1801                                  gchar const *format,
1802                                  va_list      args);
1803 /* Check if a file name is an absolute path */
1804 gboolean g_path_is_absolute     (const gchar *file_name);
1805 /* In case of absolute paths, skip the root part */
1806 gchar*  g_path_skip_root        (gchar       *file_name);
1807
1808 /* These two functions are deprecated and will be removed in the next
1809  * major release of GLib. Use g_path_get_dirname/g_path_get_basename
1810  * instead. Whatch out! The string returned by g_path_get_basename
1811  * must be g_freed, while the string returned by g_basename must not.*/
1812 gchar*  g_basename              (const gchar *file_name);
1813 gchar*  g_dirname               (const gchar *file_name);
1814
1815 /* The returned strings are newly allocated with g_malloc() */
1816 gchar*  g_get_current_dir       (void);
1817 gchar*  g_path_get_basename     (const gchar *file_name);
1818 gchar*  g_path_get_dirname      (const gchar *file_name);
1819
1820 /* Get the codeset for the current locale */
1821 /* gchar * g_get_codeset    (void); */
1822
1823 /* return the environment string for the variable. The returned memory
1824  * must not be freed. */
1825 gchar*  g_getenv                (const gchar *variable);
1826
1827 /* we use a GLib function as a replacement for ATEXIT, so
1828  * the programmer is not required to check the return value
1829  * (if there is any in the implementation) and doesn't encounter
1830  * missing include files.
1831  */
1832 void    g_atexit                (GVoidFunc    func);
1833
1834
1835 /* Bit tests
1836  */
1837 G_INLINE_FUNC gint      g_bit_nth_lsf (guint32 mask,
1838                                        gint    nth_bit);
1839 G_INLINE_FUNC gint      g_bit_nth_msf (guint32 mask,
1840                                        gint    nth_bit);
1841 G_INLINE_FUNC guint     g_bit_storage (guint number);
1842
1843
1844 /* Trash Stacks
1845  * elements need to be >= sizeof (gpointer)
1846  */
1847 G_INLINE_FUNC void      g_trash_stack_push      (GTrashStack **stack_p,
1848                                                  gpointer      data_p);
1849 G_INLINE_FUNC gpointer  g_trash_stack_pop       (GTrashStack **stack_p);
1850 G_INLINE_FUNC gpointer  g_trash_stack_peek      (GTrashStack **stack_p);
1851 G_INLINE_FUNC guint     g_trash_stack_height    (GTrashStack **stack_p);
1852
1853
1854 /* inline function implementations
1855  */
1856 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
1857 G_INLINE_FUNC gint
1858 g_bit_nth_lsf (guint32 mask,
1859                gint    nth_bit)
1860 {
1861   do
1862     {
1863       nth_bit++;
1864       if (mask & (1 << (guint) nth_bit))
1865         return nth_bit;
1866     }
1867   while (nth_bit < 32);
1868   return -1;
1869 }
1870 G_INLINE_FUNC gint
1871 g_bit_nth_msf (guint32 mask,
1872                gint    nth_bit)
1873 {
1874   if (nth_bit < 0)
1875     nth_bit = 32;
1876   do
1877     {
1878       nth_bit--;
1879       if (mask & (1 << (guint) nth_bit))
1880         return nth_bit;
1881     }
1882   while (nth_bit > 0);
1883   return -1;
1884 }
1885 G_INLINE_FUNC guint
1886 g_bit_storage (guint number)
1887 {
1888   register guint n_bits = 0;
1889   
1890   do
1891     {
1892       n_bits++;
1893       number >>= 1;
1894     }
1895   while (number);
1896   return n_bits;
1897 }
1898 G_INLINE_FUNC void
1899 g_trash_stack_push (GTrashStack **stack_p,
1900                     gpointer      data_p)
1901 {
1902   GTrashStack *data = (GTrashStack *) data_p;
1903
1904   data->next = *stack_p;
1905   *stack_p = data;
1906 }
1907 G_INLINE_FUNC gpointer
1908 g_trash_stack_pop (GTrashStack **stack_p)
1909 {
1910   GTrashStack *data;
1911
1912   data = *stack_p;
1913   if (data)
1914     {
1915       *stack_p = data->next;
1916       /* NULLify private pointer here, most platforms store NULL as
1917        * subsequent 0 bytes
1918        */
1919       data->next = NULL;
1920     }
1921
1922   return data;
1923 }
1924 G_INLINE_FUNC gpointer
1925 g_trash_stack_peek (GTrashStack **stack_p)
1926 {
1927   GTrashStack *data;
1928
1929   data = *stack_p;
1930
1931   return data;
1932 }
1933 G_INLINE_FUNC guint
1934 g_trash_stack_height (GTrashStack **stack_p)
1935 {
1936   GTrashStack *data;
1937   guint i = 0;
1938
1939   for (data = *stack_p; data; data = data->next)
1940     i++;
1941
1942   return i;
1943 }
1944 #endif  /* G_CAN_INLINE || __G_UTILS_C__ */
1945
1946
1947 /* String Chunks
1948  */
1949 GStringChunk* g_string_chunk_new           (gint size);
1950 void          g_string_chunk_free          (GStringChunk *chunk);
1951 gchar*        g_string_chunk_insert        (GStringChunk *chunk,
1952                                             const gchar  *string);
1953 gchar*        g_string_chunk_insert_const  (GStringChunk *chunk,
1954                                             const gchar  *string);
1955
1956
1957 /* Strings
1958  */
1959 GString*     g_string_new               (const gchar     *init);
1960 GString*     g_string_sized_new         (guint            dfl_size);
1961 gchar*       g_string_free              (GString         *string,
1962                                          gboolean         free_segment);
1963 gboolean     g_string_equal             (const GString   *v,
1964                                          const GString   *v2);
1965 guint        g_string_hash              (const GString   *str);
1966 GString*     g_string_assign            (GString         *string,
1967                                          const gchar     *rval);
1968 GString*     g_string_truncate          (GString         *string,
1969                                          guint            len);
1970 GString*     g_string_insert_len        (GString         *string,
1971                                          gint             pos,
1972                                          const gchar     *val,
1973                                          gint             len);
1974 GString*     g_string_append            (GString         *string,
1975                                          const gchar     *val);
1976 GString*     g_string_append_len        (GString         *string,
1977                                          const gchar     *val,
1978                                          gint             len);
1979 GString*     g_string_append_c          (GString         *string,
1980                                          gchar            c);
1981 GString*     g_string_prepend           (GString         *string,
1982                                          const gchar     *val);
1983 GString*     g_string_prepend_c         (GString         *string,
1984                                          gchar            c);
1985 GString*     g_string_prepend_len       (GString         *string,
1986                                          const gchar     *val,
1987                                          gint             len);
1988 GString*     g_string_insert            (GString         *string,
1989                                          gint             pos,
1990                                          const gchar     *val);
1991 GString*     g_string_insert_c          (GString         *string,
1992                                          gint             pos,
1993                                          gchar            c);
1994 GString*     g_string_erase             (GString         *string,
1995                                          gint             pos,
1996                                          gint             len);
1997 GString*     g_string_down              (GString         *string);
1998 GString*     g_string_up                (GString         *string);
1999 void         g_string_sprintf           (GString         *string,
2000                                          const gchar     *format,
2001                                          ...) G_GNUC_PRINTF (2, 3);
2002 void         g_string_sprintfa          (GString         *string,
2003                                          const gchar     *format,
2004                                          ...) G_GNUC_PRINTF (2, 3);
2005
2006
2007 /* Resizable arrays, remove fills any cleared spot and shortens the
2008  * array, while preserving the order. remove_fast will distort the
2009  * order by moving the last element to the position of the removed 
2010  */
2011
2012 #define g_array_append_val(a,v)   g_array_append_vals (a, &v, 1)
2013 #define g_array_prepend_val(a,v)  g_array_prepend_vals (a, &v, 1)
2014 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
2015 #define g_array_index(a,t,i)      (((t*) (a)->data) [(i)])
2016
2017 GArray* g_array_new               (gboolean         zero_terminated,
2018                                    gboolean         clear,
2019                                    guint            element_size);
2020 GArray* g_array_sized_new         (gboolean         zero_terminated,
2021                                    gboolean         clear,
2022                                    guint            element_size,
2023                                    guint            reserved_size);
2024 gchar*  g_array_free              (GArray          *array,
2025                                    gboolean         free_segment);
2026 GArray* g_array_append_vals       (GArray          *array,
2027                                    gconstpointer    data,
2028                                    guint            len);
2029 GArray* g_array_prepend_vals      (GArray          *array,
2030                                    gconstpointer    data,
2031                                    guint            len);
2032 GArray* g_array_insert_vals       (GArray          *array,
2033                                    guint            index,
2034                                    gconstpointer    data,
2035                                    guint            len);
2036 GArray* g_array_set_size          (GArray          *array,
2037                                    guint            length);
2038 GArray* g_array_remove_index      (GArray          *array,
2039                                    guint            index);
2040 GArray* g_array_remove_index_fast (GArray          *array,
2041                                    guint            index);
2042
2043 /* Resizable pointer array.  This interface is much less complicated
2044  * than the above.  Add appends appends a pointer.  Remove fills any
2045  * cleared spot and shortens the array. remove_fast will again distort
2046  * order.  
2047  */
2048 #define     g_ptr_array_index(array,index) (array->pdata)[index]
2049 GPtrArray*  g_ptr_array_new                (void);
2050 GPtrArray*  g_ptr_array_sized_new          (guint        reserved_size);
2051 gpointer*   g_ptr_array_free               (GPtrArray   *array,
2052                                             gboolean     free_seg);
2053 void        g_ptr_array_set_size           (GPtrArray   *array,
2054                                             gint         length);
2055 gpointer    g_ptr_array_remove_index       (GPtrArray   *array,
2056                                             guint        index);
2057 gpointer    g_ptr_array_remove_index_fast  (GPtrArray   *array,
2058                                             guint        index);
2059 gboolean    g_ptr_array_remove             (GPtrArray   *array,
2060                                             gpointer     data);
2061 gboolean    g_ptr_array_remove_fast        (GPtrArray   *array,
2062                                             gpointer     data);
2063 void        g_ptr_array_add                (GPtrArray   *array,
2064                                             gpointer     data);
2065
2066 /* Byte arrays, an array of guint8.  Implemented as a GArray,
2067  * but type-safe.
2068  */
2069
2070 GByteArray* g_byte_array_new               (void);
2071 GByteArray* g_byte_array_sized_new         (guint        reserved_size);
2072 guint8*     g_byte_array_free              (GByteArray   *array,
2073                                             gboolean      free_segment);
2074 GByteArray* g_byte_array_append            (GByteArray   *array,
2075                                             const guint8 *data,
2076                                             guint         len);
2077 GByteArray* g_byte_array_prepend           (GByteArray   *array,
2078                                             const guint8 *data,
2079                                             guint         len);
2080 GByteArray* g_byte_array_set_size          (GByteArray   *array,
2081                                             guint         length);
2082 GByteArray* g_byte_array_remove_index      (GByteArray   *array,
2083                                             guint         index);
2084 GByteArray* g_byte_array_remove_index_fast (GByteArray   *array,
2085                                             guint         index);
2086
2087
2088 /* Hash Functions
2089  */
2090 gboolean g_str_equal (gconstpointer   v,
2091                       gconstpointer   v2);
2092 guint    g_str_hash  (gconstpointer   v);
2093
2094 gint     g_int_equal (gconstpointer   v,
2095                       gconstpointer   v2) G_GNUC_CONST;
2096 guint    g_int_hash  (gconstpointer   v) G_GNUC_CONST;
2097
2098 /* This "hash" function will just return the key's adress as an
2099  * unsigned integer. Useful for hashing on plain adresses or
2100  * simple integer values.
2101  * passing NULL into g_hash_table_new() as GHashFunc has the
2102  * same effect as passing g_direct_hash().
2103  */
2104 guint g_direct_hash  (gconstpointer v) G_GNUC_CONST;
2105 gint  g_direct_equal (gconstpointer v,
2106                       gconstpointer v2) G_GNUC_CONST;
2107
2108
2109 /* Quarks (string<->id association)
2110  */
2111 GQuark    g_quark_try_string            (const gchar    *string);
2112 GQuark    g_quark_from_static_string    (const gchar    *string);
2113 GQuark    g_quark_from_string           (const gchar    *string);
2114 gchar*    g_quark_to_string             (GQuark          quark) G_GNUC_CONST;
2115
2116
2117 /* Keyed Data List
2118  */
2119 void      g_datalist_init                (GData          **datalist);
2120 void      g_datalist_clear               (GData          **datalist);
2121 gpointer  g_datalist_id_get_data         (GData          **datalist,
2122                                           GQuark           key_id);
2123 void      g_datalist_id_set_data_full    (GData          **datalist,
2124                                           GQuark           key_id,
2125                                           gpointer         data,
2126                                           GDestroyNotify   destroy_func);
2127 gpointer  g_datalist_id_remove_no_notify (GData          **datalist,
2128                                           GQuark           key_id);
2129 void      g_datalist_foreach             (GData          **datalist,
2130                                           GDataForeachFunc func,
2131                                           gpointer         user_data);
2132 #define   g_datalist_id_set_data(dl, q, d)      \
2133      g_datalist_id_set_data_full ((dl), (q), (d), NULL)
2134 #define   g_datalist_id_remove_data(dl, q)      \
2135      g_datalist_id_set_data ((dl), (q), NULL)
2136 #define   g_datalist_get_data(dl, k)            \
2137      (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
2138 #define   g_datalist_set_data_full(dl, k, d, f) \
2139      g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
2140 #define   g_datalist_remove_no_notify(dl, k)    \
2141      g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
2142 #define   g_datalist_set_data(dl, k, d)         \
2143      g_datalist_set_data_full ((dl), (k), (d), NULL)
2144 #define   g_datalist_remove_data(dl, k)         \
2145      g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
2146
2147
2148 /* Location Associated Keyed Data
2149  */
2150 void      g_dataset_destroy             (gconstpointer    dataset_location);
2151 gpointer  g_dataset_id_get_data         (gconstpointer    dataset_location,
2152                                          GQuark           key_id);
2153 void      g_dataset_id_set_data_full    (gconstpointer    dataset_location,
2154                                          GQuark           key_id,
2155                                          gpointer         data,
2156                                          GDestroyNotify   destroy_func);
2157 gpointer  g_dataset_id_remove_no_notify (gconstpointer    dataset_location,
2158                                          GQuark           key_id);
2159 void      g_dataset_foreach             (gconstpointer    dataset_location,
2160                                          GDataForeachFunc func,
2161                                          gpointer         user_data);
2162 #define   g_dataset_id_set_data(l, k, d)        \
2163      g_dataset_id_set_data_full ((l), (k), (d), NULL)
2164 #define   g_dataset_id_remove_data(l, k)        \
2165      g_dataset_id_set_data ((l), (k), NULL)
2166 #define   g_dataset_get_data(l, k)              \
2167      (g_dataset_id_get_data ((l), g_quark_try_string (k)))
2168 #define   g_dataset_set_data_full(l, k, d, f)   \
2169      g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
2170 #define   g_dataset_remove_no_notify(l, k)      \
2171      g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
2172 #define   g_dataset_set_data(l, k, d)           \
2173      g_dataset_set_data_full ((l), (k), (d), NULL)
2174 #define   g_dataset_remove_data(l, k)           \
2175      g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
2176
2177
2178 /* GScanner: Flexible lexical scanner for general purpose.
2179  */
2180
2181 /* Character sets */
2182 #define G_CSET_A_2_Z    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2183 #define G_CSET_a_2_z    "abcdefghijklmnopqrstuvwxyz"
2184 #define G_CSET_DIGITS   "0123456789"
2185 #define G_CSET_LATINC   "\300\301\302\303\304\305\306"\
2186                         "\307\310\311\312\313\314\315\316\317\320"\
2187                         "\321\322\323\324\325\326"\
2188                         "\330\331\332\333\334\335\336"
2189 #define G_CSET_LATINS   "\337\340\341\342\343\344\345\346"\
2190                         "\347\350\351\352\353\354\355\356\357\360"\
2191                         "\361\362\363\364\365\366"\
2192                         "\370\371\372\373\374\375\376\377"
2193
2194 /* Error types */
2195 typedef enum
2196 {
2197   G_ERR_UNKNOWN,
2198   G_ERR_UNEXP_EOF,
2199   G_ERR_UNEXP_EOF_IN_STRING,
2200   G_ERR_UNEXP_EOF_IN_COMMENT,
2201   G_ERR_NON_DIGIT_IN_CONST,
2202   G_ERR_DIGIT_RADIX,
2203   G_ERR_FLOAT_RADIX,
2204   G_ERR_FLOAT_MALFORMED
2205 } GErrorType;
2206
2207 /* Token types */
2208 typedef enum
2209 {
2210   G_TOKEN_EOF                   =   0,
2211   
2212   G_TOKEN_LEFT_PAREN            = '(',
2213   G_TOKEN_RIGHT_PAREN           = ')',
2214   G_TOKEN_LEFT_CURLY            = '{',
2215   G_TOKEN_RIGHT_CURLY           = '}',
2216   G_TOKEN_LEFT_BRACE            = '[',
2217   G_TOKEN_RIGHT_BRACE           = ']',
2218   G_TOKEN_EQUAL_SIGN            = '=',
2219   G_TOKEN_COMMA                 = ',',
2220   
2221   G_TOKEN_NONE                  = 256,
2222   
2223   G_TOKEN_ERROR,
2224   
2225   G_TOKEN_CHAR,
2226   G_TOKEN_BINARY,
2227   G_TOKEN_OCTAL,
2228   G_TOKEN_INT,
2229   G_TOKEN_HEX,
2230   G_TOKEN_FLOAT,
2231   G_TOKEN_STRING,
2232   
2233   G_TOKEN_SYMBOL,
2234   G_TOKEN_IDENTIFIER,
2235   G_TOKEN_IDENTIFIER_NULL,
2236   
2237   G_TOKEN_COMMENT_SINGLE,
2238   G_TOKEN_COMMENT_MULTI,
2239   G_TOKEN_LAST
2240 } GTokenType;
2241
2242 union   _GTokenValue
2243 {
2244   gpointer      v_symbol;
2245   gchar         *v_identifier;
2246   gulong        v_binary;
2247   gulong        v_octal;
2248   gulong        v_int;
2249   gdouble       v_float;
2250   gulong        v_hex;
2251   gchar         *v_string;
2252   gchar         *v_comment;
2253   guchar        v_char;
2254   guint         v_error;
2255 };
2256
2257 struct  _GScannerConfig
2258 {
2259   /* Character sets
2260    */
2261   gchar         *cset_skip_characters;          /* default: " \t\n" */
2262   gchar         *cset_identifier_first;
2263   gchar         *cset_identifier_nth;
2264   gchar         *cpair_comment_single;          /* default: "#\n" */
2265   
2266   /* Should symbol lookup work case sensitive?
2267    */
2268   guint         case_sensitive : 1;
2269   
2270   /* Boolean values to be adjusted "on the fly"
2271    * to configure scanning behaviour.
2272    */
2273   guint         skip_comment_multi : 1;         /* C like comment */
2274   guint         skip_comment_single : 1;        /* single line comment */
2275   guint         scan_comment_multi : 1;         /* scan multi line comments? */
2276   guint         scan_identifier : 1;
2277   guint         scan_identifier_1char : 1;
2278   guint         scan_identifier_NULL : 1;
2279   guint         scan_symbols : 1;
2280   guint         scan_binary : 1;
2281   guint         scan_octal : 1;
2282   guint         scan_float : 1;
2283   guint         scan_hex : 1;                   /* `0x0ff0' */
2284   guint         scan_hex_dollar : 1;            /* `$0ff0' */
2285   guint         scan_string_sq : 1;             /* string: 'anything' */
2286   guint         scan_string_dq : 1;             /* string: "\\-escapes!\n" */
2287   guint         numbers_2_int : 1;              /* bin, octal, hex => int */
2288   guint         int_2_float : 1;                /* int => G_TOKEN_FLOAT? */
2289   guint         identifier_2_string : 1;
2290   guint         char_2_token : 1;               /* return G_TOKEN_CHAR? */
2291   guint         symbol_2_token : 1;
2292   guint         scope_0_fallback : 1;           /* try scope 0 on lookups? */
2293 };
2294
2295 struct  _GScanner
2296 {
2297   /* unused fields */
2298   gpointer              user_data;
2299   guint                 max_parse_errors;
2300   
2301   /* g_scanner_error() increments this field */
2302   guint                 parse_errors;
2303   
2304   /* name of input stream, featured by the default message handler */
2305   const gchar           *input_name;
2306   
2307   /* data pointer for derived structures */
2308   gpointer              derived_data;
2309   
2310   /* link into the scanner configuration */
2311   GScannerConfig        *config;
2312   
2313   /* fields filled in after g_scanner_get_next_token() */
2314   GTokenType            token;
2315   GTokenValue           value;
2316   guint                 line;
2317   guint                 position;
2318   
2319   /* fields filled in after g_scanner_peek_next_token() */
2320   GTokenType            next_token;
2321   GTokenValue           next_value;
2322   guint                 next_line;
2323   guint                 next_position;
2324   
2325   /* to be considered private */
2326   GHashTable            *symbol_table;
2327   gint                  input_fd;
2328   const gchar           *text;
2329   const gchar           *text_end;
2330   gchar                 *buffer;
2331   guint                 scope_id;
2332   
2333   /* handler function for _warn and _error */
2334   GScannerMsgFunc       msg_handler;
2335 };
2336
2337 GScanner*       g_scanner_new                   (GScannerConfig *config_templ);
2338 void            g_scanner_destroy               (GScanner       *scanner);
2339 void            g_scanner_input_file            (GScanner       *scanner,
2340                                                  gint           input_fd);
2341 void            g_scanner_sync_file_offset      (GScanner       *scanner);
2342 void            g_scanner_input_text            (GScanner       *scanner,
2343                                                  const  gchar   *text,
2344                                                  guint          text_len);
2345 GTokenType      g_scanner_get_next_token        (GScanner       *scanner);
2346 GTokenType      g_scanner_peek_next_token       (GScanner       *scanner);
2347 GTokenType      g_scanner_cur_token             (GScanner       *scanner);
2348 GTokenValue     g_scanner_cur_value             (GScanner       *scanner);
2349 guint           g_scanner_cur_line              (GScanner       *scanner);
2350 guint           g_scanner_cur_position          (GScanner       *scanner);
2351 gboolean        g_scanner_eof                   (GScanner       *scanner);
2352 guint           g_scanner_set_scope             (GScanner       *scanner,
2353                                                  guint           scope_id);
2354 void            g_scanner_scope_add_symbol      (GScanner       *scanner,
2355                                                  guint           scope_id,
2356                                                  const gchar    *symbol,
2357                                                  gpointer       value);
2358 void            g_scanner_scope_remove_symbol   (GScanner       *scanner,
2359                                                  guint           scope_id,
2360                                                  const gchar    *symbol);
2361 gpointer        g_scanner_scope_lookup_symbol   (GScanner       *scanner,
2362                                                  guint           scope_id,
2363                                                  const gchar    *symbol);
2364 void            g_scanner_scope_foreach_symbol  (GScanner       *scanner,
2365                                                  guint           scope_id,
2366                                                  GHFunc          func,
2367                                                  gpointer        user_data);
2368 gpointer        g_scanner_lookup_symbol         (GScanner       *scanner,
2369                                                  const gchar    *symbol);
2370 void            g_scanner_unexp_token           (GScanner       *scanner,
2371                                                  GTokenType     expected_token,
2372                                                  const gchar    *identifier_spec,
2373                                                  const gchar    *symbol_spec,
2374                                                  const gchar    *symbol_name,
2375                                                  const gchar    *message,
2376                                                  gint            is_error);
2377 void            g_scanner_error                 (GScanner       *scanner,
2378                                                  const gchar    *format,
2379                                                  ...) G_GNUC_PRINTF (2,3);
2380 void            g_scanner_warn                  (GScanner       *scanner,
2381                                                  const gchar    *format,
2382                                                  ...) G_GNUC_PRINTF (2,3);
2383 gint            g_scanner_stat_mode             (const gchar    *filename);
2384 /* keep downward source compatibility */
2385 #define         g_scanner_add_symbol( scanner, symbol, value )  G_STMT_START { \
2386   g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2387 } G_STMT_END
2388 #define         g_scanner_remove_symbol( scanner, symbol )      G_STMT_START { \
2389   g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2390 } G_STMT_END
2391 #define         g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2392   g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2393 } G_STMT_END
2394
2395 /* The following two functions are deprecated and will be removed in
2396  * the next major release. They do no good. */
2397 void            g_scanner_freeze_symbol_table   (GScanner       *scanner);
2398 void            g_scanner_thaw_symbol_table     (GScanner       *scanner);
2399
2400 /* GCompletion
2401  */
2402
2403 struct _GCompletion
2404 {
2405   GList* items;
2406   GCompletionFunc func;
2407   
2408   gchar* prefix;
2409   GList* cache;
2410 };
2411
2412 GCompletion* g_completion_new          (GCompletionFunc func);
2413 void         g_completion_add_items    (GCompletion*    cmp,
2414                                         GList*          items);
2415 void         g_completion_remove_items (GCompletion*    cmp,
2416                                         GList*          items);
2417 void         g_completion_clear_items  (GCompletion*    cmp);
2418 GList*       g_completion_complete     (GCompletion*    cmp,
2419                                         gchar*          prefix,
2420                                         gchar**         new_prefix);
2421 void         g_completion_free         (GCompletion*    cmp);
2422
2423
2424 /* GDate
2425  *
2426  * Date calculations (not time for now, to be resolved). These are a
2427  * mutant combination of Steffen Beyer's DateCalc routines
2428  * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2429  * date routines (written for in-house software).  Written by Havoc
2430  * Pennington <hp@pobox.com> 
2431  */
2432
2433 typedef guint16 GDateYear;
2434 typedef guint8  GDateDay;   /* day of the month */
2435 typedef struct _GDate GDate;
2436 /* make struct tm known without having to include time.h */
2437 struct tm;
2438
2439 /* enum used to specify order of appearance in parsed date strings */
2440 typedef enum
2441 {
2442   G_DATE_DAY   = 0,
2443   G_DATE_MONTH = 1,
2444   G_DATE_YEAR  = 2
2445 } GDateDMY;
2446
2447 /* actual week and month values */
2448 typedef enum
2449 {
2450   G_DATE_BAD_WEEKDAY  = 0,
2451   G_DATE_MONDAY       = 1,
2452   G_DATE_TUESDAY      = 2,
2453   G_DATE_WEDNESDAY    = 3,
2454   G_DATE_THURSDAY     = 4,
2455   G_DATE_FRIDAY       = 5,
2456   G_DATE_SATURDAY     = 6,
2457   G_DATE_SUNDAY       = 7
2458 } GDateWeekday;
2459 typedef enum
2460 {
2461   G_DATE_BAD_MONTH = 0,
2462   G_DATE_JANUARY   = 1,
2463   G_DATE_FEBRUARY  = 2,
2464   G_DATE_MARCH     = 3,
2465   G_DATE_APRIL     = 4,
2466   G_DATE_MAY       = 5,
2467   G_DATE_JUNE      = 6,
2468   G_DATE_JULY      = 7,
2469   G_DATE_AUGUST    = 8,
2470   G_DATE_SEPTEMBER = 9,
2471   G_DATE_OCTOBER   = 10,
2472   G_DATE_NOVEMBER  = 11,
2473   G_DATE_DECEMBER  = 12
2474 } GDateMonth;
2475
2476 #define G_DATE_BAD_JULIAN 0U
2477 #define G_DATE_BAD_DAY    0U
2478 #define G_DATE_BAD_YEAR   0U
2479
2480 /* Note: directly manipulating structs is generally a bad idea, but
2481  * in this case it's an *incredibly* bad idea, because all or part
2482  * of this struct can be invalid at any given time. Use the functions,
2483  * or you will get hosed, I promise.
2484  */
2485 struct _GDate
2486
2487   guint julian_days : 32; /* julian days representation - we use a
2488                            *  bitfield hoping that 64 bit platforms
2489                            *  will pack this whole struct in one big
2490                            *  int 
2491                            */
2492
2493   guint julian : 1;    /* julian is valid */
2494   guint dmy    : 1;    /* dmy is valid */
2495
2496   /* DMY representation */
2497   guint day    : 6;  
2498   guint month  : 4; 
2499   guint year   : 16; 
2500 };
2501
2502 /* g_date_new() returns an invalid date, you then have to _set() stuff 
2503  * to get a usable object. You can also allocate a GDate statically,
2504  * then call g_date_clear() to initialize.
2505  */
2506 GDate*       g_date_new                   (void);
2507 GDate*       g_date_new_dmy               (GDateDay     day, 
2508                                            GDateMonth   month, 
2509                                            GDateYear    year);
2510 GDate*       g_date_new_julian            (guint32      julian_day);
2511 void         g_date_free                  (GDate       *date);
2512
2513 /* check g_date_valid() after doing an operation that might fail, like
2514  * _parse.  Almost all g_date operations are undefined on invalid
2515  * dates (the exceptions are the mutators, since you need those to
2516  * return to validity).  
2517  */
2518 gboolean     g_date_valid                 (GDate       *date);
2519 gboolean     g_date_valid_day             (GDateDay     day) G_GNUC_CONST;
2520 gboolean     g_date_valid_month           (GDateMonth month) G_GNUC_CONST;
2521 gboolean     g_date_valid_year            (GDateYear  year) G_GNUC_CONST;
2522 gboolean     g_date_valid_weekday         (GDateWeekday weekday) G_GNUC_CONST;
2523 gboolean     g_date_valid_julian          (guint32 julian_date) G_GNUC_CONST;
2524 gboolean     g_date_valid_dmy             (GDateDay     day,
2525                                            GDateMonth   month,
2526                                            GDateYear    year) G_GNUC_CONST;
2527
2528 GDateWeekday g_date_weekday               (GDate       *date);
2529 GDateMonth   g_date_month                 (GDate       *date);
2530 GDateYear    g_date_year                  (GDate       *date);
2531 GDateDay     g_date_day                   (GDate       *date);
2532 guint32      g_date_julian                (GDate       *date);
2533 guint        g_date_day_of_year           (GDate       *date);
2534
2535 /* First monday/sunday is the start of week 1; if we haven't reached
2536  * that day, return 0. These are not ISO weeks of the year; that
2537  * routine needs to be added.
2538  * these functions return the number of weeks, starting on the
2539  * corrsponding day
2540  */
2541 guint        g_date_monday_week_of_year   (GDate      *date);
2542 guint        g_date_sunday_week_of_year   (GDate      *date);
2543
2544 /* If you create a static date struct you need to clear it to get it
2545  * in a sane state before use. You can clear a whole array at
2546  * once with the ndates argument.
2547  */
2548 void         g_date_clear                 (GDate       *date, 
2549                                            guint        n_dates);
2550
2551 /* The parse routine is meant for dates typed in by a user, so it
2552  * permits many formats but tries to catch common typos. If your data
2553  * needs to be strictly validated, it is not an appropriate function.
2554  */
2555 void         g_date_set_parse             (GDate       *date,
2556                                            const gchar *str);
2557 void         g_date_set_time              (GDate       *date, 
2558                                            GTime        time);
2559 void         g_date_set_month             (GDate       *date, 
2560                                            GDateMonth   month);
2561 void         g_date_set_day               (GDate       *date, 
2562                                            GDateDay     day);
2563 void         g_date_set_year              (GDate       *date,
2564                                            GDateYear    year);
2565 void         g_date_set_dmy               (GDate       *date,
2566                                            GDateDay     day,
2567                                            GDateMonth   month,
2568                                            GDateYear    y);
2569 void         g_date_set_julian            (GDate       *date,
2570                                            guint32      julian_date);
2571 gboolean     g_date_is_first_of_month     (GDate       *date);
2572 gboolean     g_date_is_last_of_month      (GDate       *date);
2573
2574 /* To go forward by some number of weeks just go forward weeks*7 days */
2575 void         g_date_add_days              (GDate       *date, 
2576                                            guint        n_days);
2577 void         g_date_subtract_days         (GDate       *date, 
2578                                            guint        n_days);
2579
2580 /* If you add/sub months while day > 28, the day might change */
2581 void         g_date_add_months            (GDate       *date,
2582                                            guint        n_months);
2583 void         g_date_subtract_months       (GDate       *date,
2584                                            guint        n_months);
2585
2586 /* If it's feb 29, changing years can move you to the 28th */
2587 void         g_date_add_years             (GDate       *date,
2588                                            guint        n_years);
2589 void         g_date_subtract_years        (GDate       *date,
2590                                            guint        n_years);
2591 gboolean     g_date_is_leap_year          (GDateYear    year) G_GNUC_CONST;
2592 guint8       g_date_days_in_month         (GDateMonth   month, 
2593                                            GDateYear    year) G_GNUC_CONST;
2594 guint8       g_date_monday_weeks_in_year  (GDateYear    year) G_GNUC_CONST;
2595 guint8       g_date_sunday_weeks_in_year  (GDateYear    year) G_GNUC_CONST;
2596
2597 /* qsort-friendly (with a cast...) */
2598 gint         g_date_compare               (GDate       *lhs,
2599                                            GDate       *rhs);
2600 void         g_date_to_struct_tm          (GDate       *date,
2601                                            struct tm   *tm);
2602
2603 /* Just like strftime() except you can only use date-related formats.
2604  *   Using a time format is undefined.
2605  */
2606 gsize        g_date_strftime              (gchar       *s,
2607                                            gsize        slen,
2608                                            const gchar *format,
2609                                            GDate       *date);
2610
2611
2612 /* GRelation
2613  *
2614  * Indexed Relations.  Imagine a really simple table in a
2615  * database.  Relations are not ordered.  This data type is meant for
2616  * maintaining a N-way mapping.
2617  *
2618  * g_relation_new() creates a relation with FIELDS fields
2619  *
2620  * g_relation_destroy() frees all resources
2621  * g_tuples_destroy() frees the result of g_relation_select()
2622  *
2623  * g_relation_index() indexes relation FIELD with the provided
2624  *   equality and hash functions.  this must be done before any
2625  *   calls to insert are made.
2626  *
2627  * g_relation_insert() inserts a new tuple.  you are expected to
2628  *   provide the right number of fields.
2629  *
2630  * g_relation_delete() deletes all relations with KEY in FIELD
2631  * g_relation_select() returns ...
2632  * g_relation_count() counts ...
2633  */
2634
2635 GRelation* g_relation_new     (gint         fields);
2636 void       g_relation_destroy (GRelation   *relation);
2637 void       g_relation_index   (GRelation   *relation,
2638                                gint         field,
2639                                GHashFunc    hash_func,
2640                                GCompareFunc key_compare_func);
2641 void       g_relation_insert  (GRelation   *relation,
2642                                ...);
2643 gint       g_relation_delete  (GRelation   *relation,
2644                                gconstpointer  key,
2645                                gint         field);
2646 GTuples*   g_relation_select  (GRelation   *relation,
2647                                gconstpointer  key,
2648                                gint         field);
2649 gint       g_relation_count   (GRelation   *relation,
2650                                gconstpointer  key,
2651                                gint         field);
2652 gboolean   g_relation_exists  (GRelation   *relation,
2653                                ...);
2654 void       g_relation_print   (GRelation   *relation);
2655
2656 void       g_tuples_destroy   (GTuples     *tuples);
2657 gpointer   g_tuples_index     (GTuples     *tuples,
2658                                gint         index,
2659                                gint         field);
2660
2661
2662 /* GRand - a good and fast random number generator: Mersenne Twister 
2663  * see http://www.math.keio.ac.jp/~matumoto/emt.html for more info.
2664  * The range functions return a value in the intervall [min,max).
2665  * int          -> [0..2^32-1]
2666  * int_range    -> [min..max-1]
2667  * double       -> [0..1)
2668  * double_range -> [min..max)
2669  */
2670
2671 GRand*  g_rand_new_with_seed   (guint32     seed);
2672 GRand*  g_rand_new             (void);
2673 void    g_rand_free            (GRand      *rand);
2674
2675 void    g_rand_set_seed        (GRand      *rand, 
2676                                 guint32     seed);
2677 guint32 g_rand_int             (GRand      *rand);
2678 gint32  g_rand_int_range       (GRand      *rand, 
2679                                 gint32      min, 
2680                                 gint32      max);
2681 gdouble g_rand_double          (GRand      *rand);
2682 gdouble g_rand_double_range    (GRand      *rand, 
2683                                 gdouble     min, 
2684                                 gdouble     max);
2685
2686 void    g_random_set_seed      (guint32     seed);
2687 guint32 g_random_int           (void);
2688 gint32  g_random_int_range     (gint32      min, 
2689                                 gint32      max);
2690 gdouble g_random_double        (void);
2691 gdouble g_random_double_range  (gdouble     min, 
2692                                 gdouble     max);
2693  
2694
2695 /* Prime numbers.
2696  */
2697
2698 /* This function returns prime numbers spaced by approximately 1.5-2.0
2699  * and is for use in resizing data structures which prefer
2700  * prime-valued sizes.  The closest spaced prime function returns the
2701  * next largest prime, or the highest it knows about which is about
2702  * MAXINT/4.
2703  */
2704 guint      g_spaced_primes_closest (guint num) G_GNUC_CONST;
2705
2706
2707 /* GIOChannel
2708  */
2709
2710 typedef struct _GIOFuncs GIOFuncs;
2711 typedef enum
2712 {
2713   G_IO_ERROR_NONE,
2714   G_IO_ERROR_AGAIN,
2715   G_IO_ERROR_INVAL,
2716   G_IO_ERROR_UNKNOWN
2717 } GIOError;
2718 typedef enum
2719 {
2720   G_SEEK_CUR,
2721   G_SEEK_SET,
2722   G_SEEK_END
2723 } GSeekType;
2724 typedef enum
2725 {
2726   G_IO_IN       GLIB_SYSDEF_POLLIN,
2727   G_IO_OUT      GLIB_SYSDEF_POLLOUT,
2728   G_IO_PRI      GLIB_SYSDEF_POLLPRI,
2729   G_IO_ERR      GLIB_SYSDEF_POLLERR,
2730   G_IO_HUP      GLIB_SYSDEF_POLLHUP,
2731   G_IO_NVAL     GLIB_SYSDEF_POLLNVAL
2732 } GIOCondition;
2733
2734 struct _GIOChannel
2735 {
2736   guint channel_flags;
2737   guint ref_count;
2738   GIOFuncs *funcs;
2739 };
2740
2741 typedef gboolean (*GIOFunc) (GIOChannel   *source,
2742                              GIOCondition  condition,
2743                              gpointer      data);
2744 struct _GIOFuncs
2745 {
2746   GIOError (*io_read)   (GIOChannel     *channel, 
2747                          gchar          *buf, 
2748                          guint           count,
2749                          guint          *bytes_read);
2750   GIOError (*io_write)  (GIOChannel     *channel, 
2751                          gchar          *buf, 
2752                          guint           count,
2753                          guint          *bytes_written);
2754   GIOError (*io_seek)   (GIOChannel     *channel, 
2755                          gint            offset, 
2756                          GSeekType       type);
2757   void (*io_close)      (GIOChannel     *channel);
2758   guint (*io_add_watch) (GIOChannel     *channel,
2759                          gint            priority,
2760                          GIOCondition    condition,
2761                          GIOFunc         func,
2762                          gpointer        user_data,
2763                          GDestroyNotify  notify);
2764   void (*io_free)       (GIOChannel     *channel);
2765 };
2766
2767 void        g_io_channel_init   (GIOChannel    *channel);
2768 void        g_io_channel_ref    (GIOChannel    *channel);
2769 void        g_io_channel_unref  (GIOChannel    *channel);
2770 GIOError    g_io_channel_read   (GIOChannel    *channel, 
2771                                  gchar         *buf, 
2772                                  guint          count,
2773                                  guint         *bytes_read);
2774 GIOError  g_io_channel_write    (GIOChannel    *channel, 
2775                                  gchar         *buf, 
2776                                  guint          count,
2777                                  guint         *bytes_written);
2778 GIOError  g_io_channel_seek     (GIOChannel    *channel,
2779                                  gint           offset, 
2780                                  GSeekType      type);
2781 void      g_io_channel_close    (GIOChannel    *channel);
2782 guint     g_io_add_watch_full   (GIOChannel    *channel,
2783                                  gint           priority,
2784                                  GIOCondition   condition,
2785                                  GIOFunc        func,
2786                                  gpointer       user_data,
2787                                  GDestroyNotify notify);
2788 guint    g_io_add_watch         (GIOChannel    *channel,
2789                                  GIOCondition   condition,
2790                                  GIOFunc        func,
2791                                  gpointer       user_data);
2792
2793
2794 /* Main loop
2795  */
2796 typedef struct _GTimeVal        GTimeVal;
2797 typedef struct _GSourceFuncs    GSourceFuncs;
2798 typedef struct _GMainLoop       GMainLoop;      /* Opaque */
2799
2800 struct _GTimeVal
2801 {
2802   glong tv_sec;
2803   glong tv_usec;
2804 };
2805 struct _GSourceFuncs
2806 {
2807   gboolean (*prepare)  (gpointer  source_data, 
2808                         GTimeVal *current_time,
2809                         gint     *timeout,
2810                         gpointer  user_data);
2811   gboolean (*check)    (gpointer  source_data,
2812                         GTimeVal *current_time,
2813                         gpointer  user_data);
2814   gboolean (*dispatch) (gpointer  source_data, 
2815                         GTimeVal *dispatch_time,
2816                         gpointer  user_data);
2817   GDestroyNotify destroy;
2818 };
2819
2820 /* Standard priorities */
2821
2822 #define G_PRIORITY_HIGH            -100
2823 #define G_PRIORITY_DEFAULT          0
2824 #define G_PRIORITY_HIGH_IDLE        100
2825 #define G_PRIORITY_DEFAULT_IDLE     200
2826 #define G_PRIORITY_LOW              300
2827
2828 typedef gboolean (*GSourceFunc) (gpointer data);
2829
2830 /* Hooks for adding to the main loop */
2831 guint    g_source_add                        (gint           priority, 
2832                                               gboolean       can_recurse,
2833                                               GSourceFuncs  *funcs,
2834                                               gpointer       source_data, 
2835                                               gpointer       user_data,
2836                                               GDestroyNotify notify);
2837 gboolean g_source_remove                     (guint          tag);
2838 gboolean g_source_remove_by_user_data        (gpointer       user_data);
2839 gboolean g_source_remove_by_source_data      (gpointer       source_data);
2840 gboolean g_source_remove_by_funcs_user_data  (GSourceFuncs  *funcs,
2841                                               gpointer       user_data);
2842
2843 void g_get_current_time                 (GTimeVal       *result);
2844
2845 /* Running the main loop */
2846 GMainLoop*      g_main_new              (gboolean        is_running);
2847 void            g_main_run              (GMainLoop      *loop);
2848 void            g_main_quit             (GMainLoop      *loop);
2849 void            g_main_destroy          (GMainLoop      *loop);
2850 gboolean        g_main_is_running       (GMainLoop      *loop);
2851
2852 /* Run a single iteration of the mainloop. If block is FALSE,
2853  * will never block
2854  */
2855 gboolean        g_main_iteration        (gboolean       may_block);
2856
2857 /* See if any events are pending */
2858 gboolean        g_main_pending          (void);
2859
2860 /* Idles and timeouts */
2861 guint           g_timeout_add_full      (gint           priority,
2862                                          guint          interval, 
2863                                          GSourceFunc    function,
2864                                          gpointer       data,
2865                                          GDestroyNotify notify);
2866 guint           g_timeout_add           (guint          interval,
2867                                          GSourceFunc    function,
2868                                          gpointer       data);
2869 guint           g_idle_add              (GSourceFunc    function,
2870                                          gpointer       data);
2871 guint           g_idle_add_full         (gint           priority,
2872                                          GSourceFunc    function,
2873                                          gpointer       data,
2874                                          GDestroyNotify destroy);
2875 gboolean        g_idle_remove_by_data   (gpointer       data);
2876
2877 /* GPollFD
2878  *
2879  * System-specific IO and main loop calls
2880  *
2881  * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
2882  * descriptor as provided by the C runtime) that can be used by
2883  * MsgWaitForMultipleObjects. This does *not* include file handles
2884  * from CreateFile, SOCKETs, nor pipe handles. (But you can use
2885  * WSAEventSelect to signal events when a SOCKET is readable).
2886  *
2887  * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
2888  * indicate polling for messages. These message queue GPollFDs should
2889  * be added with the g_main_poll_win32_msg_add function.
2890  *
2891  * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
2892  * (GTK) programs, as GDK itself wants to read messages and convert them
2893  * to GDK events.
2894  *
2895  * So, unless you really know what you are doing, it's best not to try
2896  * to use the main loop polling stuff for your own needs on
2897  * Win32. It's really only written for the GIMP's needs so
2898  * far.
2899  */
2900
2901 typedef struct _GPollFD GPollFD;
2902 typedef gint    (*GPollFunc)    (GPollFD *ufds,
2903                                  guint    nfsd,
2904                                  gint     timeout);
2905 struct _GPollFD
2906 {
2907   gint          fd;
2908   gushort       events;
2909   gushort       revents;
2910 };
2911
2912 void        g_main_add_poll          (GPollFD    *fd,
2913                                       gint        priority);
2914 void        g_main_remove_poll       (GPollFD    *fd);
2915 void        g_main_set_poll_func     (GPollFunc   func);
2916
2917 #ifdef G_OS_WIN32
2918
2919 /* Useful on other platforms, too? */
2920
2921 GPollFunc   g_main_win32_get_poll_func (void);
2922
2923 #endif
2924
2925 /* On Unix, IO channels created with this function for any file
2926  * descriptor or socket.
2927  *
2928  * On Win32, use this only for files opened with the MSVCRT (the
2929  * Microsoft run-time C library) _open() or _pipe, including file
2930  * descriptors 0, 1 and 2 (corresponding to stdin, stdout and stderr).
2931  *
2932  * The term file descriptor as used in the context of Win32 refers to
2933  * the emulated Unix-like file descriptors MSVCRT provides. The native
2934  * corresponding concept is file HANDLE. There isn't as of yet a way to
2935  * get GIOChannels for file HANDLEs.
2936  */
2937 GIOChannel* g_io_channel_unix_new    (int         fd);
2938 gint        g_io_channel_unix_get_fd (GIOChannel *channel);
2939
2940 #ifdef G_OS_WIN32
2941
2942 #define G_WIN32_MSG_HANDLE 19981206
2943
2944 /* Use this to get a GPollFD from a GIOChannel, so that you can call
2945  * g_io_channel_win32_poll(). After calling this you should only use
2946  * g_io_channel_read() to read from the GIOChannel, i.e. never read()
2947  * or recv() from the underlying file descriptor or SOCKET.
2948  */
2949 void        g_io_channel_win32_make_pollfd (GIOChannel   *channel,
2950                                             GIOCondition  condition,
2951                                             GPollFD      *fd);
2952
2953 /* This can be used to wait a until at least one of the channels is readable.
2954  * On Unix you would do a select() on the file descriptors of the channels.
2955  * This should probably be available for all platforms?
2956  */
2957 gint        g_io_channel_win32_poll   (GPollFD    *fds,
2958                                        gint        n_fds,
2959                                        gint        timeout);
2960
2961 /* This is used to add polling for Windows messages. GDK (GTk+) programs
2962  * should *not* use this.
2963  */
2964 void        g_main_poll_win32_msg_add (gint        priority,
2965                                        GPollFD    *fd,
2966                                        guint       hwnd);
2967
2968 /* An IO channel for Windows messages for window handle hwnd. */
2969 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
2970
2971 /* An IO channel for C runtime (emulated Unix-like) file
2972  * descriptors. Identical to g_io_channel_unix_new above.
2973  * After calling g_io_add_watch() on a IO channel returned
2974  * by this function, you shouldn't call read() on the file
2975  * descriptor.
2976  */
2977 GIOChannel* g_io_channel_win32_new_fd (int         fd);
2978
2979 /* Get the C runtime file descriptor of a channel. */
2980 gint        g_io_channel_win32_get_fd (GIOChannel *channel);
2981
2982 /* An IO channel for a SOCK_STREAM winsock socket. The parameter
2983  * should be a SOCKET. After calling g_io_add_watch() on a IO channel
2984  * returned by this function, you shouldn't call recv() on the SOCKET.
2985  */
2986 GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
2987
2988 #endif
2989
2990 /* Windows emulation stubs for common Unix functions
2991  */
2992 #ifdef G_OS_WIN32
2993 #  define MAXPATHLEN 1024
2994
2995 #ifdef _MSC_VER
2996 typedef int pid_t;
2997 #endif
2998
2999 /*
3000  * To get prototypes for the following POSIXish functions, you have to
3001  * include the indicated non-POSIX headers. The functions are defined
3002  * in OLDNAMES.LIB (MSVC) or -lmoldname-msvc (mingw32).
3003  *
3004  * getcwd: <direct.h> (MSVC), <io.h> (mingw32)
3005  * getpid: <process.h>
3006  * access: <io.h>
3007  * unlink: <stdio.h> or <io.h>
3008  * open, read, write, lseek, close: <io.h>
3009  * rmdir: <direct.h>
3010  * pipe: <direct.h>
3011  */
3012
3013 /* pipe is not in OLDNAMES.LIB or -lmoldname-msvc. */
3014 #define pipe(phandles)  _pipe (phandles, 4096, _O_BINARY)
3015
3016 /* For some POSIX functions that are not provided by the MS runtime,
3017  * we provide emulators in glib, which are prefixed with g_win32_.
3018  */
3019 #    define ftruncate(fd, size) g_win32_ftruncate (fd, size)
3020
3021 /* -lmingw32 also has emulations for these, but we need our own
3022  * for MSVC anyhow, so we might aswell use them always.
3023  */
3024 #    define opendir             g_win32_opendir
3025 #    define readdir             g_win32_readdir
3026 #    define rewinddir           g_win32_rewinddir
3027 #    define closedir            g_win32_closedir
3028 #    define NAME_MAX 255
3029
3030 struct DIR
3031 {
3032   gchar    *dir_name;
3033   gboolean  just_opened;
3034   guint     find_file_handle;
3035   gpointer  find_file_data;
3036 };
3037 typedef struct DIR DIR;
3038 struct dirent
3039 {
3040   gchar  d_name[NAME_MAX + 1];
3041 };
3042 /* emulation functions */
3043 extern int      g_win32_ftruncate       (gint            f,
3044                                          guint           size);
3045 DIR*            g_win32_opendir         (const gchar    *dirname);
3046 struct dirent*  g_win32_readdir         (DIR            *dir);
3047 void            g_win32_rewinddir       (DIR            *dir);
3048 gint            g_win32_closedir        (DIR            *dir);
3049
3050 /* The MS setlocale uses locale names of the form "English_United
3051  * States.1252" etc. We want the Unixish standard form "en", "zh_TW"
3052  * etc. This function gets the current thread locale from Windows and
3053  * returns it as a string of the above form for use in forming file
3054  * names etc. The returned string should be deallocated with g_free().
3055  */
3056 gchar *         g_win32_getlocale  (void);
3057
3058 /* Translate a Win32 error code (as returned by GetLastError()) into
3059  * the corresponding message. The returned string should be deallocated
3060  * with g_free().
3061  */
3062 gchar *         g_win32_error_message (gint error);
3063
3064 #endif   /* G_OS_WIN32 */
3065
3066
3067 /* GLib Thread support
3068  */
3069
3070 extern GQuark g_thread_error_quark();
3071 #define G_THREAD_ERROR g_thread_error_quark()
3072
3073 typedef enum
3074 {
3075   G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
3076 } GThreadError;
3077
3078 typedef void            (*GThreadFunc)          (gpointer       value);
3079
3080 typedef enum
3081 {
3082   G_THREAD_PRIORITY_LOW,
3083   G_THREAD_PRIORITY_NORMAL,
3084   G_THREAD_PRIORITY_HIGH,
3085   G_THREAD_PRIORITY_URGENT
3086 } GThreadPriority;
3087
3088 typedef struct _GThread         GThread;
3089 struct  _GThread
3090 {
3091   GThreadPriority priority;
3092   gboolean bound;
3093   gboolean joinable;
3094 };
3095
3096 typedef struct _GMutex          GMutex;
3097 typedef struct _GCond           GCond;
3098 typedef struct _GPrivate        GPrivate;
3099 typedef struct _GStaticPrivate  GStaticPrivate;
3100 typedef struct _GAsyncQueue     GAsyncQueue;
3101 typedef struct _GThreadPool     GThreadPool;
3102
3103 typedef struct _GThreadFunctions GThreadFunctions;
3104 struct _GThreadFunctions
3105 {
3106   GMutex*  (*mutex_new)           (void);
3107   void     (*mutex_lock)          (GMutex               *mutex);
3108   gboolean (*mutex_trylock)       (GMutex               *mutex);
3109   void     (*mutex_unlock)        (GMutex               *mutex);
3110   void     (*mutex_free)          (GMutex               *mutex);
3111   GCond*   (*cond_new)            (void);
3112   void     (*cond_signal)         (GCond                *cond);
3113   void     (*cond_broadcast)      (GCond                *cond);
3114   void     (*cond_wait)           (GCond                *cond,
3115                                    GMutex               *mutex);
3116   gboolean (*cond_timed_wait)     (GCond                *cond,
3117                                    GMutex               *mutex, 
3118                                    GTimeVal             *end_time);
3119   void      (*cond_free)          (GCond                *cond);
3120   GPrivate* (*private_new)        (GDestroyNotify        destructor);
3121   gpointer  (*private_get)        (GPrivate             *private_key);
3122   void      (*private_set)        (GPrivate             *private_key,
3123                                    gpointer              data);
3124   void      (*thread_create)      (GThreadFunc           thread_func,
3125                                    gpointer              arg,
3126                                    gulong                stack_size,
3127                                    gboolean              joinable,
3128                                    gboolean              bound,
3129                                    GThreadPriority       priority,
3130                                    gpointer              thread,
3131                                    GError              **error);
3132   void      (*thread_yield)       (void);
3133   void      (*thread_join)        (gpointer              thread);
3134   void      (*thread_exit)        (void);
3135   void      (*thread_set_priority)(gpointer              thread, 
3136                                    GThreadPriority       priority);
3137   void      (*thread_self)        (gpointer              thread);
3138 };
3139
3140 GLIB_VAR GThreadFunctions       g_thread_functions_for_glib_use;
3141 GLIB_VAR gboolean               g_thread_use_default_impl;
3142 GLIB_VAR gboolean               g_threads_got_initialized;
3143
3144 /* initializes the mutex/cond/private implementation for glib, might
3145  * only be called once, and must not be called directly or indirectly
3146  * from another glib-function, e.g. as a callback.
3147  */
3148 void    g_thread_init   (GThreadFunctions       *vtable);
3149
3150 /* Errorcheck mutexes. If you define G_ERRORCHECK_MUTEXES, then all
3151  * mutexes will check for re-locking and re-unlocking */
3152
3153 /* Initialize thread system with errorcheck mutexes. vtable must be
3154  * NULL.Do not call directly. Use #define G_ERRORCHECK_MUTEXES
3155  * instead.  
3156  */
3157 void    g_thread_init_with_errorcheck_mutexes (GThreadFunctions* vtable);
3158
3159 /* A random number to recognize debug calls to g_mutex_... */
3160 #define G_MUTEX_DEBUG_MAGIC 0xf8e18ad7
3161
3162 #ifdef G_ERRORCHECK_MUTEXES
3163 #define g_thread_init(vtable) g_thread_init_with_errorcheck_mutexes (vtable) 
3164 #endif
3165
3166 /* internal function for fallback static mutex implementation */
3167 GMutex* g_static_mutex_get_mutex_impl   (GMutex **mutex);
3168
3169 /* shorthands for conditional and unconditional function calls */
3170 #define G_THREAD_UF(op, arglist) \
3171     (*g_thread_functions_for_glib_use . op) arglist
3172 #define G_THREAD_CF(op, fail, arg) \
3173     (g_thread_supported () ? G_THREAD_UF (op, arg) : (fail))
3174 #define G_THREAD_ECF(op, fail, mutex, name, type)                       \
3175     (g_thread_supported () ?                                            \
3176      ((type(*)(GMutex*, gulong, gchar*, gchar*))                        \
3177       (*g_thread_functions_for_glib_use . op))                          \
3178      (mutex, G_MUTEX_DEBUG_MAGIC, G_STRINGIFY (name),  G_STRLOC) : (fail))
3179 #ifndef G_ERRORCHECK_MUTEXES
3180 #define g_mutex_lock_with_debug_name(mutex, name)                       \
3181   G_THREAD_CF (mutex_lock,     (void)0, (mutex))
3182 #define g_mutex_trylock_with_debug_name(mutex, name)                    \
3183    G_THREAD_CF (mutex_trylock,  TRUE,    (mutex))
3184 #define g_mutex_unlock_with_debug_name(mutex, name)                     \
3185    G_THREAD_CF (mutex_unlock,   (void)0, (mutex))
3186 #else /* G_ERRORCHECK_MUTEXES */
3187 #define g_mutex_lock_with_debug_name(mutex, name)                       \
3188   G_THREAD_ECF (mutex_lock,    (void)0, mutex, name, void)
3189 #define g_mutex_trylock_with_debug_name(mutex, name)                    \
3190   G_THREAD_ECF (mutex_trylock, TRUE,    mutex, name, gboolean)
3191 #define g_mutex_unlock_with_debug_name(mutex, name)                     \
3192   G_THREAD_ECF (mutex_unlock,  (void)0, mutex, name, void)
3193 #endif /* G_ERRORCHECK_MUTEXES */
3194
3195 #define g_thread_supported()    (g_threads_got_initialized)
3196 #define g_mutex_new()            G_THREAD_UF (mutex_new,      ())
3197 #define g_mutex_lock(mutex)      g_mutex_lock_with_debug_name(mutex, mutex)
3198 #define g_mutex_trylock(mutex)   g_mutex_trylock_with_debug_name(mutex, mutex)
3199 #define g_mutex_unlock(mutex)    g_mutex_unlock_with_debug_name(mutex, mutex)
3200 #define g_mutex_free(mutex)      G_THREAD_CF (mutex_free,     (void)0, (mutex))
3201 #define g_cond_new()             G_THREAD_UF (cond_new,       ())
3202 #define g_cond_signal(cond)      G_THREAD_CF (cond_signal,    (void)0, (cond))
3203 #define g_cond_broadcast(cond)   G_THREAD_CF (cond_broadcast, (void)0, (cond))
3204 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait,      (void)0, (cond, \
3205                                                                         mutex))
3206 #define g_cond_free(cond)        G_THREAD_CF (cond_free,      (void)0, (cond))
3207 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
3208                                                               TRUE, \
3209                                                               (cond, mutex, \
3210                                                                abs_time))
3211 #define g_private_new(destructor)         G_THREAD_UF (private_new, (destructor))
3212 #define g_private_get(private_key)        G_THREAD_CF (private_get, \
3213                                                        ((gpointer)private_key), \
3214                                                        (private_key))
3215 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
3216                                                        (void) (private_key = \
3217                                                         (GPrivate*) (value)), \
3218                                                        (private_key, value))
3219 #define g_thread_yield()              G_THREAD_CF (thread_yield, (void)0, ())
3220 #define g_thread_exit()               G_THREAD_CF (thread_exit, (void)0, ())
3221
3222 GThread* g_thread_create (GThreadFunc            thread_func,
3223                           gpointer               arg,
3224                           gulong                 stack_size,
3225                           gboolean               joinable,
3226                           gboolean               bound,
3227                           GThreadPriority        priority,
3228                           GError               **error);
3229 GThread* g_thread_self ();
3230 void g_thread_join (GThread *thread);
3231 void g_thread_set_priority (GThread         *thread, 
3232                             GThreadPriority  priority);
3233
3234 /* GStaticMutexes can be statically initialized with the value
3235  * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
3236  * much easier, than having to explicitly allocate the mutex before
3237  * use
3238  */
3239 #define g_static_mutex_lock(mutex) \
3240     g_mutex_lock_with_debug_name (g_static_mutex_get_mutex (mutex), mutex)
3241 #define g_static_mutex_trylock(mutex) \
3242     g_mutex_trylock_with_debug_name (g_static_mutex_get_mutex (mutex), mutex)
3243 #define g_static_mutex_unlock(mutex) \
3244     g_mutex_unlock_with_debug_name (g_static_mutex_get_mutex (mutex), mutex) 
3245
3246 struct _GStaticPrivate
3247 {
3248   guint index;
3249 };
3250 #define G_STATIC_PRIVATE_INIT { 0 }
3251 gpointer g_static_private_get (GStaticPrivate   *private_key);
3252 void     g_static_private_set (GStaticPrivate   *private_key, 
3253                                gpointer          data,
3254                                GDestroyNotify    notify);
3255 gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
3256                                           GThread        *thread);
3257 void g_static_private_set_for_thread (GStaticPrivate *private_key, 
3258                                       GThread        *thread,
3259                                       gpointer        data,
3260                                       GDestroyNotify  notify);
3261
3262 typedef struct _GStaticRecMutex GStaticRecMutex;
3263 struct _GStaticRecMutex
3264 {
3265   GStaticMutex mutex;
3266   unsigned int depth;
3267   GSystemThread owner;
3268 };
3269
3270 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT }
3271 void     g_static_rec_mutex_lock        (GStaticRecMutex *mutex);
3272 gboolean g_static_rec_mutex_trylock     (GStaticRecMutex *mutex);
3273 void     g_static_rec_mutex_unlock      (GStaticRecMutex *mutex);
3274 void     g_static_rec_mutex_lock_full   (GStaticRecMutex *mutex,
3275                                          guint            depth);
3276 guint    g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
3277
3278 typedef struct _GStaticRWLock GStaticRWLock;
3279 struct _GStaticRWLock
3280 {
3281   GStaticMutex mutex; 
3282   GCond *read_cond;
3283   GCond *write_cond;
3284   guint read_counter;
3285   gboolean write;
3286   guint want_to_write;
3287 };
3288
3289 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
3290
3291 void      g_static_rw_lock_reader_lock    (GStaticRWLock* lock);
3292 gboolean  g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
3293 void      g_static_rw_lock_reader_unlock  (GStaticRWLock* lock);
3294 void      g_static_rw_lock_writer_lock    (GStaticRWLock* lock);
3295 gboolean  g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
3296 void      g_static_rw_lock_writer_unlock  (GStaticRWLock* lock);
3297 void      g_static_rw_lock_free (GStaticRWLock* lock);
3298
3299 /* these are some convenience macros that expand to nothing if GLib
3300  * was configured with --disable-threads. for using StaticMutexes,
3301  * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
3302  * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
3303  * declare such an globally defined lock. name is a unique identifier
3304  * for the protected varibale or code portion. locking, testing and
3305  * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
3306  * G_TRYLOCK() respectively.  
3307  */
3308 extern void glib_dummy_decl (void);
3309 #define G_LOCK_NAME(name)               g__ ## name ## _lock
3310 #ifdef  G_THREADS_ENABLED
3311 #  define G_LOCK_DEFINE_STATIC(name)    static G_LOCK_DEFINE (name)
3312 #  define G_LOCK_DEFINE(name)           \
3313     GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT 
3314 #  define G_LOCK_EXTERN(name)           extern GStaticMutex G_LOCK_NAME (name)
3315
3316 #  ifdef G_DEBUG_LOCKS
3317 #    define G_LOCK(name)                G_STMT_START{             \
3318         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
3319                "file %s: line %d (%s): locking: %s ",             \
3320                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
3321                #name);                                            \
3322         g_static_mutex_lock (&G_LOCK_NAME (name));                \
3323      }G_STMT_END
3324 #    define G_UNLOCK(name)              G_STMT_START{             \
3325         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
3326                "file %s: line %d (%s): unlocking: %s ",           \
3327                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
3328                #name);                                            \
3329        g_static_mutex_unlock (&G_LOCK_NAME (name));               \
3330      }G_STMT_END
3331 #    define G_TRYLOCK(name)                                       \
3332         (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                  \
3333                "file %s: line %d (%s): try locking: %s ",         \
3334                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
3335                #name), g_static_mutex_trylock (&G_LOCK_NAME (name)))
3336 #  else  /* !G_DEBUG_LOCKS */
3337 #    define G_LOCK(name) g_static_mutex_lock       (&G_LOCK_NAME (name)) 
3338 #    define G_UNLOCK(name) g_static_mutex_unlock   (&G_LOCK_NAME (name))
3339 #    define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
3340 #  endif /* !G_DEBUG_LOCKS */
3341 #else   /* !G_THREADS_ENABLED */
3342 #  define G_LOCK_DEFINE_STATIC(name)    extern void glib_dummy_decl (void)
3343 #  define G_LOCK_DEFINE(name)           extern void glib_dummy_decl (void)
3344 #  define G_LOCK_EXTERN(name)           extern void glib_dummy_decl (void)
3345 #  define G_LOCK(name)
3346 #  define G_UNLOCK(name)
3347 #  define G_TRYLOCK(name)               (TRUE)
3348 #endif  /* !G_THREADS_ENABLED */
3349
3350 /* Asyncronous Queues, can be used to communicate between threads
3351  */
3352
3353 /* Get a new GAsyncQueue with the ref_count 1 */
3354 GAsyncQueue*  g_async_queue_new                (void);
3355
3356 /* Lock and unlock an GAsyncQueue, all functions lock the queue for
3357  * themselves, but in certain cirumstances you want to hold the lock longer,
3358  * thus you lock the queue, call the *_unlocked functions and unlock it again
3359  */
3360 void          g_async_queue_lock               (GAsyncQueue *queue);
3361 void          g_async_queue_unlock             (GAsyncQueue *queue);
3362
3363 /* Ref and unref the GAsyncQueue. g_async_queue_unref_unlocked makes
3364  * no sense, as after the unreffing the Queue might be gone and can't
3365  * be unlocked. So you have a function to call, if you don't hold the
3366  * lock (g_async_queue_unref) and one to call, when you already hold
3367  * the lock (g_async_queue_unref_and_unlock). After that however, you
3368  * don't hold the lock anymore and the Queue might in fact be
3369  * destroyed, if you unrefed to zero */
3370 void          g_async_queue_ref                (GAsyncQueue *queue);
3371 void          g_async_queue_ref_unlocked       (GAsyncQueue *queue);
3372 void          g_async_queue_unref              (GAsyncQueue *queue);
3373 void          g_async_queue_unref_and_unlock   (GAsyncQueue *queue);
3374
3375 /* Push data into the async queue. Must not be NULL */
3376 void          g_async_queue_push               (GAsyncQueue *queue,
3377                                                 gpointer     data);
3378 void          g_async_queue_push_unlocked      (GAsyncQueue *queue,
3379                                                 gpointer     data);
3380
3381 /* Pop data from the async queue, when no data is there, the thread is blocked
3382  * until data arrives */
3383 gpointer      g_async_queue_pop                (GAsyncQueue *queue);
3384 gpointer      g_async_queue_pop_unlocked       (GAsyncQueue *queue);
3385
3386 /* Try to pop data, NULL is returned in case of empty queue */
3387 gpointer      g_async_queue_try_pop            (GAsyncQueue *queue);
3388 gpointer      g_async_queue_try_pop_unlocked   (GAsyncQueue *queue);
3389
3390 /* Wait for data until at maximum until end_time is reached, NULL is returned
3391  * in case of empty queue*/
3392 gpointer      g_async_queue_timed_pop          (GAsyncQueue *queue, 
3393                                                 GTimeVal    *end_time);
3394 gpointer      g_async_queue_timed_pop_unlocked (GAsyncQueue *queue, 
3395                                                 GTimeVal    *end_time);
3396
3397 /* Return the length of the queue, negative values mean, that threads
3398  * are waiting, positve values mean, that there are entries in the
3399  * queue. Actually this function returns the length of the queue minus
3400  * the number of waiting threads, g_async_queue_length == 0 could also
3401  * mean 'n' entries in the queue and 'n' thread waiting, such can
3402  * happen due to locking of the queue or due to scheduling. */
3403 gint          g_async_queue_length             (GAsyncQueue *queue);
3404 gint          g_async_queue_length_unlocked    (GAsyncQueue *queue);
3405
3406 /* Thread Pools
3407  */
3408
3409 /* The real GThreadPool is bigger, so you may only create a thread
3410  * pool with the constructor function */
3411 struct _GThreadPool
3412 {
3413   GFunc thread_func;
3414   gulong stack_size;
3415   gboolean bound; 
3416   GThreadPriority priority;
3417   gboolean exclusive;
3418   gpointer user_data;
3419 };
3420
3421 /* Get a thread pool with the function thread_func, at most max_threads may
3422  * run at a time (max_threads == -1 means no limit), stack_size, bound,
3423  * priority like in g_thread_create, exclusive == TRUE means, that the threads
3424  * shouldn't be shared and that they will be prestarted (otherwise they are
3425  * started, as needed) user_data is the 2nd argument to the thread_func */
3426 GThreadPool*    g_thread_pool_new             (GFunc            thread_func,
3427                                                gint             max_threads,
3428                                                gulong           stack_size,
3429                                                gboolean         bound,
3430                                                GThreadPriority  priority,
3431                                                gboolean         exclusive,
3432                                                gpointer         user_data,
3433                                                GError         **error);
3434
3435 /* Push new data into the thread pool. This task is assigned to a thread later
3436  * (when the maximal number of threads is reached for that pool) or now
3437  * (otherwise). If necessary a new thread will be started. The function
3438  * returns immediatly */
3439 void            g_thread_pool_push            (GThreadPool     *pool,
3440                                                gpointer         data,
3441                                                GError         **error);
3442
3443 /* Set the number of threads, which can run concurrently for that pool, -1
3444  * means no limit. 0 means has the effect, that the pool won't process
3445  * requests until the limit is set higher again */
3446 void            g_thread_pool_set_max_threads (GThreadPool     *pool,
3447                                                gint             max_threads,
3448                                                GError         **error);
3449 gint            g_thread_pool_get_max_threads (GThreadPool     *pool);
3450
3451 /* Get the number of threads assigned to that pool. This number doesn't
3452  * necessarily represent the number of working threads in that pool */
3453 guint           g_thread_pool_get_num_threads (GThreadPool     *pool);
3454
3455 /* Get the number of unprocessed items in the pool */
3456 guint           g_thread_pool_unprocessed     (GThreadPool     *pool);
3457
3458 /* Free the pool, immediate means, that all unprocessed items in the queue
3459  * wont be processed, wait means, that the function doesn't return immediatly,
3460  * but after all threads in the pool are ready processing items. immediate
3461  * does however not mean, that threads are killed. */
3462 void            g_thread_pool_free            (GThreadPool     *pool,
3463                                                gboolean         immediate,
3464                                                gboolean         wait);
3465
3466 /* Set the maximal number of unused threads before threads will be stopped by
3467  * GLib, -1 means no limit */
3468 void            g_thread_pool_set_max_unused_threads (gint      max_threads);
3469 gint            g_thread_pool_get_max_unused_threads (void);
3470 guint           g_thread_pool_get_num_unused_threads (void);
3471
3472 /* Stop all currently unused threads, but leave the limit untouched */
3473 void            g_thread_pool_stop_unused_threads    (void);
3474
3475 typedef enum 
3476 {
3477   G_CONVERT_ERROR_NO_CONVERSION,
3478   G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
3479   G_CONVERT_ERROR_FAILED
3480 } GConvertError;
3481
3482 #define G_CONVERT_ERROR g_convert_error_quark()
3483 GQuark g_convert_error_quark();
3484
3485 gchar* g_convert               (const gchar  *str,
3486                                 gint          len,
3487                                 const gchar  *to_codeset,
3488                                 const gchar  *from_codeset,
3489                                 gint         *bytes_read,
3490                                 gint         *bytes_written,
3491                                 GError      **error);
3492 gchar* g_convert_with_fallback (const gchar  *str,
3493                                 gint          len,
3494                                 const gchar  *to_codeset,
3495                                 const gchar  *from_codeset,
3496                                 gchar        *fallback,
3497                                 gint         *bytes_read,
3498                                 gint         *bytes_written,
3499                                 GError      **error);
3500
3501 #ifdef __cplusplus
3502 }
3503 #endif /* __cplusplus */
3504
3505 #include <gunicode.h>
3506
3507 #endif /* __G_LIB_H__ */