Added g_alloca, g_new_a, g_new0_a macros.
[platform/upstream/glib.git] / 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 Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #ifndef __G_LIB_H__
20 #define __G_LIB_H__
21
22 /* system specific config file glibconfig.h provides definitions for
23  * the extrema of many of the standard types. These are:
24  *
25  *  G_MINSHORT, G_MAXSHORT
26  *  G_MININT, G_MAXINT
27  *  G_MINLONG, G_MAXLONG
28  *  G_MINFLOAT, G_MAXFLOAT
29  *  G_MINDOUBLE, G_MAXDOUBLE
30  *
31  * It also provides the following typedefs:
32  *
33  *  gint8, guint8
34  *  gint16, guint16
35  *  gint32, guint32
36  *  gint64, guint64
37  *
38  * It defines the G_BYTE_ORDER symbol to one of G_*_ENDIAN (see later in
39  * this file). 
40  *
41  * And it provides a way to store and retrieve a `gint' in/from a `gpointer'.
42  * This is useful to pass an integer instead of a pointer to a callback.
43  *
44  *  GINT_TO_POINTER(i), GUINT_TO_POINTER(i)
45  *  GPOINTER_TO_INT(p), GPOINTER_TO_UINT(p)
46  *
47  * Finally, it provide the following wrappers to STDC functions:
48  *
49  *  g_ATEXIT
50  *    To register hooks which are executed on exit().
51  *    Usually a wrapper for STDC atexit.
52  *
53  *  void *g_memmove(void *dest, const void *src, guint count);
54  *    A wrapper for STDC memmove, or an implementation, if memmove doesn't
55  *    exist.  The prototype looks like the above, give or take a const,
56  *    or size_t.
57  */
58 #include <glibconfig.h>
59
60 /* include varargs functions for assertment macros
61  */
62 #include <stdarg.h>
63
64 /* optionally feature DMALLOC memory allocation debugger
65  */
66 #ifdef USE_DMALLOC
67 #include "dmalloc.h"
68 #endif
69
70
71 #ifdef NATIVE_WIN32
72
73 /* On native Win32, directory separator is the backslash, and search path
74  * separator is the semicolon.
75  */
76 #define G_DIR_SEPARATOR '\\'
77 #define G_DIR_SEPARATOR_S "\\"
78 #define G_SEARCHPATH_SEPARATOR ';'
79 #define G_SEARCHPATH_SEPARATOR_S ";"
80
81 #else  /* !NATIVE_WIN32 */
82
83 /* Unix */
84
85 #define G_DIR_SEPARATOR '/'
86 #define G_DIR_SEPARATOR_S "/"
87 #define G_SEARCHPATH_SEPARATOR ':'
88 #define G_SEARCHPATH_SEPARATOR_S ":"
89
90 #endif /* !NATIVE_WIN32 */
91
92 #ifdef __cplusplus
93 extern "C" {
94 #endif /* __cplusplus */
95
96
97 /* Provide definitions for some commonly used macros.
98  *  Some of them are only provided if they haven't already
99  *  been defined. It is assumed that if they are already
100  *  defined then the current definition is correct.
101  */
102 #ifndef NULL
103 #define NULL    ((void*) 0)
104 #endif
105
106 #ifndef FALSE
107 #define FALSE   (0)
108 #endif
109
110 #ifndef TRUE
111 #define TRUE    (!FALSE)
112 #endif
113
114 #undef  MAX
115 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
116
117 #undef  MIN
118 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
119
120 #undef  ABS
121 #define ABS(a)     (((a) < 0) ? -(a) : (a))
122
123 #undef  CLAMP
124 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
125
126
127 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
128  * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
129  */
130 #if !defined (G_VA_COPY)
131 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
132 #  define G_VA_COPY(ap1, ap2)     (*(ap1) = *(ap2))
133 #  elif defined (G_VA_COPY_AS_ARRAY)
134 #  define G_VA_COPY(ap1, ap2)     g_memmove ((ap1), (ap2), sizeof (va_list))
135 #  else /* va_list is a pointer */
136 #  define G_VA_COPY(ap1, ap2)     ((ap1) = (ap2))
137 #  endif /* va_list is a pointer */
138 #endif /* !G_VA_COPY */
139
140
141 /* Provide convenience macros for handling structure
142  * fields through their offsets.
143  */
144 #define G_STRUCT_OFFSET(struct_type, member)    \
145     ((gulong) ((gchar*) &((struct_type*) 0)->member))
146 #define G_STRUCT_MEMBER_P(struct_p, struct_offset)   \
147     ((gpointer) ((gchar*) (struct_p) + (gulong) (struct_offset)))
148 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset)   \
149     (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
150
151
152 /* inlining hassle. for compilers that don't allow the `inline' keyword,
153  * mostly because of strict ANSI C compliance or dumbness, we try to fall
154  * back to either `__inline__' or `__inline'.
155  * we define G_CAN_INLINE, if the compiler seems to be actually
156  * *capable* to do function inlining, in which case inline function bodys
157  * do make sense. we also define G_INLINE_FUNC to properly export the
158  * function prototypes if no inlining can be performed.
159  * we special case most of the stuff, so inline functions can have a normal
160  * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
161  */
162 #ifndef G_INLINE_FUNC
163 #  define G_CAN_INLINE 1
164 #endif
165 #ifdef G_HAVE_INLINE
166 #  if defined (__GNUC__) && defined (__STRICT_ANSI__)
167 #    undef inline
168 #    define inline __inline__
169 #  endif
170 #else /* !G_HAVE_INLINE */
171 #  undef inline
172 #  if defined (G_HAVE___INLINE__)
173 #    define inline __inline__
174 #  else /* !inline && !__inline__ */
175 #    if defined (G_HAVE___INLINE)
176 #      define inline __inline
177 #    else /* !inline && !__inline__ && !__inline */
178 #      define inline /* don't inline, then */
179 #      ifndef G_INLINE_FUNC
180 #        undef G_CAN_INLINE
181 #      endif
182 #    endif
183 #  endif
184 #endif
185 #ifndef G_INLINE_FUNC
186 #  ifdef __GNUC__
187 #    ifdef __OPTIMIZE__
188 #      define G_INLINE_FUNC extern inline
189 #    else
190 #      undef G_CAN_INLINE
191 #      define G_INLINE_FUNC extern
192 #    endif
193 #  else /* !__GNUC__ */
194 #    ifdef G_CAN_INLINE
195 #      define G_INLINE_FUNC static inline
196 #    else
197 #      define G_INLINE_FUNC extern
198 #    endif
199 #  endif /* !__GNUC__ */
200 #endif /* !G_INLINE_FUNC */
201
202
203 /* Provide simple macro statement wrappers (adapted from Perl):
204  *  G_STMT_START { statements; } G_STMT_END;
205  *  can be used as a single statement, as in
206  *  if (x) G_STMT_START { ... } G_STMT_END; else ...
207  *
208  *  For gcc we will wrap the statements within `({' and `})' braces.
209  *  For SunOS they will be wrapped within `if (1)' and `else (void) 0',
210  *  and otherwise within `do' and `while (0)'.
211  */
212 #if !(defined (G_STMT_START) && defined (G_STMT_END))
213 #  if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
214 #    define G_STMT_START        (void)(
215 #    define G_STMT_END          )
216 #  else
217 #    if (defined (sun) || defined (__sun__))
218 #      define G_STMT_START      if (1)
219 #      define G_STMT_END        else (void)0
220 #    else
221 #      define G_STMT_START      do
222 #      define G_STMT_END        while (0)
223 #    endif
224 #  endif
225 #endif
226
227
228 /* Provide macros to feature the GCC function attribute.
229  */
230 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
231 #define G_GNUC_PRINTF( format_idx, arg_idx )    \
232   __attribute__((format (printf, format_idx, arg_idx)))
233 #define G_GNUC_SCANF( format_idx, arg_idx )     \
234   __attribute__((format (scanf, format_idx, arg_idx)))
235 #define G_GNUC_FORMAT( arg_idx )                \
236   __attribute__((format_arg (arg_idx)))
237 #define G_GNUC_NORETURN                         \
238   __attribute__((noreturn))
239 #define G_GNUC_CONST                            \
240   __attribute__((const))
241 #define G_GNUC_UNUSED                           \
242   __attribute__((unused))
243 #else   /* !__GNUC__ */
244 #define G_GNUC_PRINTF( format_idx, arg_idx )
245 #define G_GNUC_SCANF( format_idx, arg_idx )
246 #define G_GNUC_FORMAT( arg_idx )
247 #define G_GNUC_NORETURN
248 #define G_GNUC_CONST
249 #define G_GNUC_UNUSED
250 #endif  /* !__GNUC__ */
251
252
253 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
254  * macros, so we can refer to them as strings unconditionally.
255  */
256 #ifdef  __GNUC__
257 #define G_GNUC_FUNCTION         (__FUNCTION__)
258 #define G_GNUC_PRETTY_FUNCTION  (__PRETTY_FUNCTION__)
259 #else   /* !__GNUC__ */
260 #define G_GNUC_FUNCTION         ("")
261 #define G_GNUC_PRETTY_FUNCTION  ("")
262 #endif  /* !__GNUC__ */
263
264 /* we try to provide a usefull equivalent for ATEXIT if it is
265  * not defined, but use is actually abandoned. people should
266  * use g_atexit() instead.
267  */
268 #ifndef ATEXIT
269 # define ATEXIT(proc)   g_ATEXIT(proc)
270 #else
271 # define G_NATIVE_ATEXIT
272 #endif /* ATEXIT */
273
274 /* Hacker macro to place breakpoints for elected machines.
275  * Actual use is strongly deprecated of course ;)
276  */
277 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
278 #define G_BREAKPOINT()          G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
279 #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
280 #define G_BREAKPOINT()          G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
281 #else   /* !__i386__ && !__alpha__ */
282 #define G_BREAKPOINT()
283 #endif  /* __i386__ */
284
285
286 /* Provide macros for easily allocating memory. The macros
287  *  will cast the allocated memory to the specified type
288  *  in order to avoid compiler warnings. (Makes the code neater).
289  */
290
291 #ifdef __DMALLOC_H__
292 #  define g_new(type, count)            (ALLOC (type, count))
293 #  define g_new0(type, count)           (CALLOC (type, count))
294 #  define g_renew(type, mem, count)     (REALLOC (mem, type, count))
295 #else /* __DMALLOC_H__ */
296 #  define g_new(type, count)      \
297       ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
298 #  define g_new0(type, count)     \
299       ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
300 #  define g_renew(type, mem, count)       \
301       ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
302 #endif /* __DMALLOC_H__ */
303
304 /* alloca-based counterparts of the above routines */
305 #if G_HAVE_ALLOCA
306 #  define g_alloca alloca
307 #  define g_new_a(type, count)    \
308       ((type *) alloca ((unsigned) sizeof (type) * (count)))
309 #  define g_new0_a(type, count)   \
310       ((type *) memset (alloca ((unsigned) sizeof (type) * (count)), 0, \
311                         ((unsigned) sizeof (type) * (count))))
312 #endif
313
314 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
315   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
316                    sizeof (type), \
317                    sizeof (type) * (pre_alloc), \
318                    (alloc_type)) \
319 )
320 #define g_chunk_new(type, chunk)        ( \
321   (type *) g_mem_chunk_alloc (chunk) \
322 )
323 #define g_chunk_new0(type, chunk)       ( \
324   (type *) g_mem_chunk_alloc0 (chunk) \
325 )
326 #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
327   g_mem_chunk_free ((mem_chunk), (mem)); \
328 } G_STMT_END
329
330
331 #define g_string(x) #x
332
333
334 /* Provide macros for error handling. The "assert" macros will
335  *  exit on failure. The "return" macros will exit the current
336  *  function. Two different definitions are given for the macros
337  *  if G_DISABLE_ASSERT is not defined, in order to support gcc's
338  *  __PRETTY_FUNCTION__ capability.
339  */
340
341 #ifdef G_DISABLE_ASSERT
342
343 #define g_assert(expr)
344 #define g_assert_not_reached()
345
346 #else /* !G_DISABLE_ASSERT */
347
348 #ifdef __GNUC__
349
350 #define g_assert(expr)                  G_STMT_START{           \
351      if (!(expr))                                               \
352        g_log (G_LOG_DOMAIN,                                     \
353               G_LOG_LEVEL_ERROR,                                \
354               "file %s: line %d (%s): assertion failed: (%s)",  \
355               __FILE__,                                         \
356               __LINE__,                                         \
357               __PRETTY_FUNCTION__,                              \
358               #expr);                   }G_STMT_END
359
360 #define g_assert_not_reached()          G_STMT_START{           \
361      g_log (G_LOG_DOMAIN,                                       \
362             G_LOG_LEVEL_ERROR,                                  \
363             "file %s: line %d (%s): should not be reached",     \
364             __FILE__,                                           \
365             __LINE__,                                           \
366             __PRETTY_FUNCTION__);       }G_STMT_END
367
368 #else /* !__GNUC__ */
369
370 #define g_assert(expr)                  G_STMT_START{           \
371      if (!(expr))                                               \
372        g_log (G_LOG_DOMAIN,                                     \
373               G_LOG_LEVEL_ERROR,                                \
374               "file %s: line %d: assertion failed: (%s)",       \
375               __FILE__,                                         \
376               __LINE__,                                         \
377               #expr);                   }G_STMT_END
378
379 #define g_assert_not_reached()          G_STMT_START{   \
380      g_log (G_LOG_DOMAIN,                               \
381             G_LOG_LEVEL_ERROR,                          \
382             "file %s: line %d: should not be reached",  \
383             __FILE__,                                   \
384             __LINE__);          }G_STMT_END
385
386 #endif /* __GNUC__ */
387
388 #endif /* !G_DISABLE_ASSERT */
389
390
391 #ifdef G_DISABLE_CHECKS
392
393 #define g_return_if_fail(expr)
394 #define g_return_val_if_fail(expr,val)
395
396 #else /* !G_DISABLE_CHECKS */
397
398 #ifdef __GNUC__
399
400 #define g_return_if_fail(expr)          G_STMT_START{                   \
401      if (!(expr))                                                       \
402        {                                                                \
403          g_log (G_LOG_DOMAIN,                                           \
404                 G_LOG_LEVEL_CRITICAL,                                   \
405                 "file %s: line %d (%s): assertion `%s' failed.",        \
406                 __FILE__,                                               \
407                 __LINE__,                                               \
408                 __PRETTY_FUNCTION__,                                    \
409                 #expr);                                                 \
410          return;                                                        \
411        };                               }G_STMT_END
412
413 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
414      if (!(expr))                                                       \
415        {                                                                \
416          g_log (G_LOG_DOMAIN,                                           \
417                 G_LOG_LEVEL_CRITICAL,                                   \
418                 "file %s: line %d (%s): assertion `%s' failed.",        \
419                 __FILE__,                                               \
420                 __LINE__,                                               \
421                 __PRETTY_FUNCTION__,                                    \
422                 #expr);                                                 \
423          return val;                                                    \
424        };                               }G_STMT_END
425
426 #else /* !__GNUC__ */
427
428 #define g_return_if_fail(expr)          G_STMT_START{           \
429      if (!(expr))                                               \
430        {                                                        \
431          g_log (G_LOG_DOMAIN,                                   \
432                 G_LOG_LEVEL_CRITICAL,                           \
433                 "file %s: line %d: assertion `%s' failed.",     \
434                 __FILE__,                                       \
435                 __LINE__,                                       \
436                 #expr);                                         \
437          return;                                                \
438        };                               }G_STMT_END
439
440 #define g_return_val_if_fail(expr, val) G_STMT_START{           \
441      if (!(expr))                                               \
442        {                                                        \
443          g_log (G_LOG_DOMAIN,                                   \
444                 G_LOG_LEVEL_CRITICAL,                           \
445                 "file %s: line %d: assertion `%s' failed.",     \
446                 __FILE__,                                       \
447                 __LINE__,                                       \
448                 #expr);                                         \
449          return val;                                            \
450        };                               }G_STMT_END
451
452 #endif /* !__GNUC__ */
453
454 #endif /* !G_DISABLE_CHECKS */
455
456
457 /* Provide type definitions for commonly used types.
458  *  These are useful because a "gint8" can be adjusted
459  *  to be 1 byte (8 bits) on all platforms. Similarly and
460  *  more importantly, "gint32" can be adjusted to be
461  *  4 bytes (32 bits) on all platforms.
462  */
463
464 typedef char   gchar;
465 typedef short  gshort;
466 typedef long   glong;
467 typedef int    gint;
468 typedef gint   gboolean;
469
470 typedef unsigned char   guchar;
471 typedef unsigned short  gushort;
472 typedef unsigned long   gulong;
473 typedef unsigned int    guint;
474
475 typedef float   gfloat;
476 typedef double  gdouble;
477
478 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
479  * Since gldouble isn't used anywhere, just disable it for now */
480
481 #if 0
482 #ifdef HAVE_LONG_DOUBLE
483 typedef long double gldouble;
484 #else /* HAVE_LONG_DOUBLE */
485 typedef double gldouble;
486 #endif /* HAVE_LONG_DOUBLE */
487 #endif /* 0 */
488
489 typedef void* gpointer;
490 typedef const void *gconstpointer;
491
492
493 typedef gint32  gssize;
494 typedef guint32 gsize;
495 typedef guint32 GQuark;
496 typedef gint32  GTime;
497
498
499 /* Portable endian checks and conversions
500  *
501  * glibconfig.h defines G_BYTE_ORDER which expands to one of
502  * the below macros.
503  */
504 #define G_LITTLE_ENDIAN 1234
505 #define G_BIG_ENDIAN    4321
506 #define G_PDP_ENDIAN    3412            /* unused, need specific PDP check */   
507
508
509 /* Basic bit swapping functions
510  */
511 #define GUINT16_SWAP_LE_BE_CONSTANT(val)        ((guint16) ( \
512     (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
513     (((guint16) (val) & (guint16) 0xff00U) >> 8)))
514 #define GUINT32_SWAP_LE_BE_CONSTANT(val)        ((guint32) ( \
515     (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
516     (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) | \
517     (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) | \
518     (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
519
520 /* Intel specific stuff for speed
521  */
522 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
523 #  define GUINT16_SWAP_LE_BE_X86(val) \
524      (__extension__                                     \
525       ({ register guint16 __v;                          \
526          if (__builtin_constant_p (val))                \
527            __v = GUINT16_SWAP_LE_BE_CONSTANT (val);     \
528          else                                           \
529            __asm__ __const__ ("rorw $8, %w0"            \
530                               : "=r" (__v)              \
531                               : "0" ((guint16) (val))); \
532         __v; }))
533 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
534 #  if !defined(__i486__) && !defined(__i586__) \
535       && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
536 #     define GUINT32_SWAP_LE_BE_X86(val) \
537         (__extension__                                          \
538          ({ register guint32 __v;                               \
539             if (__builtin_constant_p (val))                     \
540               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
541           else                                                  \
542             __asm__ __const__ ("rorw $8, %w0\n\t"               \
543                                "rorl $16, %0\n\t"               \
544                                "rorw $8, %w0"                   \
545                                : "=r" (__v)                     \
546                                : "0" ((guint32) (val)));        \
547         __v; }))
548 #  else /* 486 and higher has bswap */
549 #     define GUINT32_SWAP_LE_BE_X86(val) \
550         (__extension__                                          \
551          ({ register guint32 __v;                               \
552             if (__builtin_constant_p (val))                     \
553               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
554           else                                                  \
555             __asm__ __const__ ("bswap %0"                       \
556                                : "=r" (__v)                     \
557                                : "0" ((guint32) (val)));        \
558         __v; }))
559 #  endif /* processor specific 32-bit stuff */
560 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
561 #else /* !__i386__ */
562 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
563 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
564 #endif /* __i386__ */
565
566 #ifdef G_HAVE_GINT64
567 #  define GUINT64_SWAP_LE_BE_CONSTANT(val)      ((guint64) ( \
568       (((guint64) (val) &                                               \
569         (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) |      \
570       (((guint64) (val) &                                               \
571         (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) |      \
572       (((guint64) (val) &                                               \
573         (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) |      \
574       (((guint64) (val) &                                               \
575         (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) <<  8) |      \
576       (((guint64) (val) &                                               \
577         (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >>  8) |      \
578       (((guint64) (val) &                                               \
579         (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) |      \
580       (((guint64) (val) &                                               \
581         (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) |      \
582       (((guint64) (val) &                                               \
583         (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
584 #  if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
585 #    define GUINT64_SWAP_LE_BE_X86(val) \
586         (__extension__                                          \
587          ({ union { guint64 __ll;                               \
588                     guint32 __l[2]; } __r;                      \
589             if (__builtin_constant_p (val))                     \
590               __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val);     \
591             else                                                \
592               {                                                 \
593                 union { guint64 __ll;                           \
594                         guint32 __l[2]; } __w;                  \
595                 __w.__ll = ((guint64) val);                     \
596                 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]);   \
597                 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]);   \
598               }                                                 \
599           __r.__ll; }))
600 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
601 #  else /* !__i386__ */
602 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
603 #  endif
604 #endif
605
606 #define GUINT16_SWAP_LE_PDP(val)        ((guint16) (val))
607 #define GUINT16_SWAP_BE_PDP(val)        (GUINT16_SWAP_LE_BE (val))
608 #define GUINT32_SWAP_LE_PDP(val)        ((guint32) ( \
609     (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
610     (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
611 #define GUINT32_SWAP_BE_PDP(val)        ((guint32) ( \
612     (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
613     (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
614
615 /* The G*_TO_?E() macros are defined in glibconfig.h.
616  * The transformation is symmetric, so the FROM just maps to the TO.
617  */
618 #define GINT16_FROM_LE(val)     (GINT16_TO_LE (val))
619 #define GUINT16_FROM_LE(val)    (GUINT16_TO_LE (val))
620 #define GINT16_FROM_BE(val)     (GINT16_TO_BE (val))
621 #define GUINT16_FROM_BE(val)    (GUINT16_TO_BE (val))
622 #define GINT32_FROM_LE(val)     (GINT32_TO_LE (val))
623 #define GUINT32_FROM_LE(val)    (GUINT32_TO_LE (val))
624 #define GINT32_FROM_BE(val)     (GINT32_TO_BE (val))
625 #define GUINT32_FROM_BE(val)    (GUINT32_TO_BE (val))
626
627 #ifdef G_HAVE_GINT64
628 #define GINT64_FROM_LE(val)     (GINT64_TO_LE (val))
629 #define GUINT64_FROM_LE(val)    (GUINT64_TO_LE (val))
630 #define GINT64_FROM_BE(val)     (GINT64_TO_BE (val))
631 #define GUINT64_FROM_BE(val)    (GUINT64_TO_BE (val))
632 #endif
633
634 #define GLONG_FROM_LE(val)      (GLONG_TO_LE (val))
635 #define GULONG_FROM_LE(val)     (GULONG_TO_LE (val))
636 #define GLONG_FROM_BE(val)      (GLONG_TO_BE (val))
637 #define GULONG_FROM_BE(val)     (GULONG_TO_BE (val))
638
639 #define GINT_FROM_LE(val)       (GINT_TO_LE (val))
640 #define GUINT_FROM_LE(val)      (GUINT_TO_LE (val))
641 #define GINT_FROM_BE(val)       (GINT_TO_BE (val))
642 #define GUINT_FROM_BE(val)      (GUINT_TO_BE (val))
643
644
645 /* Portable versions of host-network order stuff
646  */
647 #define g_ntohl(val) (GUINT32_FROM_BE (val))
648 #define g_ntohs(val) (GUINT16_FROM_BE (val))
649 #define g_htonl(val) (GUINT32_TO_BE (val))
650 #define g_htons(val) (GUINT16_TO_BE (val))
651
652
653 /* Glib version.
654  * we prefix variable declarations so they can
655  * properly get exported in windows dlls.
656  */
657 #ifdef NATIVE_WIN32
658 #  ifdef GLIB_COMPILATION
659 #    define GUTILS_C_VAR __declspec(dllexport)
660 #  else /* !GLIB_COMPILATION */
661 #    define GUTILS_C_VAR extern __declspec(dllimport)
662 #  endif /* !GLIB_COMPILATION */
663 #else /* !NATIVE_WIN32 */
664 #  define GUTILS_C_VAR extern
665 #endif /* !NATIVE_WIN32 */
666
667 GUTILS_C_VAR const guint glib_major_version;
668 GUTILS_C_VAR const guint glib_minor_version;
669 GUTILS_C_VAR const guint glib_micro_version;
670 GUTILS_C_VAR const guint glib_interface_age;
671 GUTILS_C_VAR const guint glib_binary_age;
672
673
674 /* Forward declarations of glib types.
675  */
676 typedef struct _GAllocator      GAllocator;
677 typedef struct _GArray          GArray;
678 typedef struct _GByteArray      GByteArray;
679 typedef struct _GCache          GCache;
680 typedef struct _GCompletion     GCompletion;
681 typedef struct _GData           GData;
682 typedef struct _GDebugKey       GDebugKey;
683 typedef struct _GHashTable      GHashTable;
684 typedef struct _GHook           GHook;
685 typedef struct _GHookList       GHookList;
686 typedef struct _GList           GList;
687 typedef struct _GMemChunk       GMemChunk;
688 typedef struct _GNode           GNode;
689 typedef struct _GPtrArray       GPtrArray;
690 typedef struct _GRelation       GRelation;
691 typedef struct _GScanner        GScanner;
692 typedef struct _GScannerConfig  GScannerConfig;
693 typedef struct _GSList          GSList;
694 typedef struct _GString         GString;
695 typedef struct _GStringChunk    GStringChunk;
696 typedef struct _GTimer          GTimer;
697 typedef struct _GTree           GTree;
698 typedef struct _GTuples         GTuples;
699 typedef union  _GTokenValue     GTokenValue;
700 typedef struct _GIOChannel      GIOChannel;
701
702 typedef enum
703 {
704   G_TRAVERSE_LEAFS      = 1 << 0,
705   G_TRAVERSE_NON_LEAFS  = 1 << 1,
706   G_TRAVERSE_ALL        = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
707   G_TRAVERSE_MASK       = 0x03
708 } GTraverseFlags;
709
710 typedef enum
711 {
712   G_IN_ORDER,
713   G_PRE_ORDER,
714   G_POST_ORDER,
715   G_LEVEL_ORDER
716 } GTraverseType;
717
718 /* Log level shift offset for user defined
719  * log levels (0-7 are used by GLib).
720  */
721 #define G_LOG_LEVEL_USER_SHIFT  (8)
722
723 /* Glib log levels and flags.
724  */
725 typedef enum
726 {
727   /* log flags */
728   G_LOG_FLAG_RECURSION          = 1 << 0,
729   G_LOG_FLAG_FATAL              = 1 << 1,
730   
731   /* GLib log levels */
732   G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
733   G_LOG_LEVEL_CRITICAL          = 1 << 3,
734   G_LOG_LEVEL_WARNING           = 1 << 4,
735   G_LOG_LEVEL_MESSAGE           = 1 << 5,
736   G_LOG_LEVEL_INFO              = 1 << 6,
737   G_LOG_LEVEL_DEBUG             = 1 << 7,
738   
739   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
740 } GLogLevelFlags;
741
742 /* GLib log levels that are considered fatal by default */
743 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
744
745
746 typedef gpointer        (*GCacheNewFunc)        (gpointer       key);
747 typedef gpointer        (*GCacheDupFunc)        (gpointer       value);
748 typedef void            (*GCacheDestroyFunc)    (gpointer       value);
749 typedef gint            (*GCompareFunc)         (gconstpointer  a,
750                                                  gconstpointer  b);
751 typedef gchar*          (*GCompletionFunc)      (gpointer);
752 typedef void            (*GDestroyNotify)       (gpointer       data);
753 typedef void            (*GDataForeachFunc)     (GQuark         key_id,
754                                                  gpointer       data,
755                                                  gpointer       user_data);
756 typedef void            (*GFunc)                (gpointer       data,
757                                                  gpointer       user_data);
758 typedef guint           (*GHashFunc)            (gconstpointer  key);
759 typedef void            (*GFreeFunc)            (gpointer       data);
760 typedef void            (*GHFunc)               (gpointer       key,
761                                                  gpointer       value,
762                                                  gpointer       user_data);
763 typedef gboolean        (*GHRFunc)              (gpointer       key,
764                                                  gpointer       value,
765                                                  gpointer       user_data);
766 typedef gint            (*GHookCompareFunc)     (GHook          *new_hook,
767                                                  GHook          *sibling);
768 typedef gboolean        (*GHookFindFunc)        (GHook          *hook,
769                                                  gpointer        data);
770 typedef void            (*GHookMarshaller)      (GHook          *hook,
771                                                  gpointer        data);
772 typedef gboolean        (*GHookCheckMarshaller) (GHook          *hook,
773                                                  gpointer        data);
774 typedef void            (*GHookFunc)            (gpointer        data);
775 typedef gboolean        (*GHookCheckFunc)       (gpointer        data);
776 typedef void            (*GHookFreeFunc)        (GHookList      *hook_list,
777                                                  GHook          *hook);
778 typedef void            (*GLogFunc)             (const gchar   *log_domain,
779                                                  GLogLevelFlags log_level,
780                                                  const gchar   *message,
781                                                  gpointer       user_data);
782 typedef gboolean        (*GNodeTraverseFunc)    (GNode         *node,
783                                                  gpointer       data);
784 typedef void            (*GNodeForeachFunc)     (GNode         *node,
785                                                  gpointer       data);
786 typedef gint            (*GSearchFunc)          (gpointer       key,
787                                                  gpointer       data);
788 typedef void            (*GScannerMsgFunc)      (GScanner      *scanner,
789                                                  gchar         *message,
790                                                  gint           error);
791 typedef gint            (*GTraverseFunc)        (gpointer       key,
792                                                  gpointer       value,
793                                                  gpointer       data);
794 typedef void            (*GVoidFunc)            (void);
795
796
797 struct _GList
798 {
799   gpointer data;
800   GList *next;
801   GList *prev;
802 };
803
804 struct _GSList
805 {
806   gpointer data;
807   GSList *next;
808 };
809
810 struct _GString
811 {
812   gchar *str;
813   gint len;
814 };
815
816 struct _GArray
817 {
818   gchar *data;
819   guint len;
820 };
821
822 struct _GByteArray
823 {
824   guint8 *data;
825   guint   len;
826 };
827
828 struct _GPtrArray
829 {
830   gpointer *pdata;
831   guint     len;
832 };
833
834 struct _GTuples
835 {
836   guint len;
837 };
838
839 struct _GDebugKey
840 {
841   gchar *key;
842   guint  value;
843 };
844
845
846 /* Doubly linked lists
847  */
848 void   g_list_push_allocator    (GAllocator     *allocator);
849 void   g_list_pop_allocator     (void);
850 GList* g_list_alloc             (void);
851 void   g_list_free              (GList          *list);
852 void   g_list_free_1            (GList          *list);
853 GList* g_list_append            (GList          *list,
854                                  gpointer        data);
855 GList* g_list_prepend           (GList          *list,
856                                  gpointer        data);
857 GList* g_list_insert            (GList          *list,
858                                  gpointer        data,
859                                  gint            position);
860 GList* g_list_insert_sorted     (GList          *list,
861                                  gpointer        data,
862                                  GCompareFunc    func);
863 GList* g_list_concat            (GList          *list1,
864                                  GList          *list2);
865 GList* g_list_remove            (GList          *list,
866                                  gpointer        data);
867 GList* g_list_remove_link       (GList          *list,
868                                  GList          *llink);
869 GList* g_list_reverse           (GList          *list);
870 GList* g_list_copy              (GList          *list);
871 GList* g_list_nth               (GList          *list,
872                                  guint           n);
873 GList* g_list_find              (GList          *list,
874                                  gpointer        data);
875 GList* g_list_find_custom       (GList          *list,
876                                  gpointer        data,
877                                  GCompareFunc    func);
878 gint   g_list_position          (GList          *list,
879                                  GList          *llink);
880 gint   g_list_index             (GList          *list,
881                                  gpointer        data);
882 GList* g_list_last              (GList          *list);
883 GList* g_list_first             (GList          *list);
884 guint  g_list_length            (GList          *list);
885 void   g_list_foreach           (GList          *list,
886                                  GFunc           func,
887                                  gpointer        user_data);
888 GList* g_list_sort              (GList          *list,
889                                  GCompareFunc    compare_func);
890 gpointer g_list_nth_data        (GList          *list,
891                                  guint           n);
892 #define g_list_previous(list)   ((list) ? (((GList *)(list))->prev) : NULL)
893 #define g_list_next(list)       ((list) ? (((GList *)(list))->next) : NULL)
894
895
896 /* Singly linked lists
897  */
898 void    g_slist_push_allocator  (GAllocator     *allocator);
899 void    g_slist_pop_allocator   (void);
900 GSList* g_slist_alloc           (void);
901 void    g_slist_free            (GSList         *list);
902 void    g_slist_free_1          (GSList         *list);
903 GSList* g_slist_append          (GSList         *list,
904                                  gpointer        data);
905 GSList* g_slist_prepend         (GSList         *list,
906                                  gpointer        data);
907 GSList* g_slist_insert          (GSList         *list,
908                                  gpointer        data,
909                                  gint            position);
910 GSList* g_slist_insert_sorted   (GSList         *list,
911                                  gpointer        data,
912                                  GCompareFunc    func);
913 GSList* g_slist_concat          (GSList         *list1,
914                                  GSList         *list2);
915 GSList* g_slist_remove          (GSList         *list,
916                                  gpointer        data);
917 GSList* g_slist_remove_link     (GSList         *list,
918                                  GSList         *llink);
919 GSList* g_slist_reverse         (GSList         *list);
920 GSList* g_slist_copy            (GSList         *list);
921 GSList* g_slist_nth             (GSList         *list,
922                                  guint           n);
923 GSList* g_slist_find            (GSList         *list,
924                                  gpointer        data);
925 GSList* g_slist_find_custom     (GSList         *list,
926                                  gpointer        data,
927                                  GCompareFunc    func);
928 gint    g_slist_position        (GSList         *list,
929                                  GSList         *llink);
930 gint    g_slist_index           (GSList         *list,
931                                  gpointer        data);
932 GSList* g_slist_last            (GSList         *list);
933 guint   g_slist_length          (GSList         *list);
934 void    g_slist_foreach         (GSList         *list,
935                                  GFunc           func,
936                                  gpointer        user_data);
937 GSList*  g_slist_sort           (GSList          *list,
938                                  GCompareFunc    compare_func);
939 gpointer g_slist_nth_data       (GSList         *list,
940                                  guint           n);
941 #define g_slist_next(slist)     ((slist) ? (((GSList *)(slist))->next) : NULL)
942
943
944 /* Hash tables
945  */
946 GHashTable* g_hash_table_new            (GHashFunc       hash_func,
947                                          GCompareFunc    key_compare_func);
948 void        g_hash_table_destroy        (GHashTable     *hash_table);
949 void        g_hash_table_insert         (GHashTable     *hash_table,
950                                          gpointer        key,
951                                          gpointer        value);
952 void        g_hash_table_remove         (GHashTable     *hash_table,
953                                          gconstpointer   key);
954 gpointer    g_hash_table_lookup         (GHashTable     *hash_table,
955                                          gconstpointer   key);
956 gboolean    g_hash_table_lookup_extended(GHashTable     *hash_table,
957                                          gconstpointer   lookup_key,
958                                          gpointer       *orig_key,
959                                          gpointer       *value);
960 void        g_hash_table_freeze         (GHashTable     *hash_table);
961 void        g_hash_table_thaw           (GHashTable     *hash_table);
962 void        g_hash_table_foreach        (GHashTable     *hash_table,
963                                          GHFunc          func,
964                                          gpointer        user_data);
965 gint        g_hash_table_foreach_remove (GHashTable     *hash_table,
966                                          GHRFunc         func,
967                                          gpointer        user_data);
968 gint        g_hash_table_size           (GHashTable     *hash_table);
969
970
971 /* Caches
972  */
973 GCache*  g_cache_new           (GCacheNewFunc      value_new_func,
974                                 GCacheDestroyFunc  value_destroy_func,
975                                 GCacheDupFunc      key_dup_func,
976                                 GCacheDestroyFunc  key_destroy_func,
977                                 GHashFunc          hash_key_func,
978                                 GHashFunc          hash_value_func,
979                                 GCompareFunc       key_compare_func);
980 void     g_cache_destroy       (GCache            *cache);
981 gpointer g_cache_insert        (GCache            *cache,
982                                 gpointer           key);
983 void     g_cache_remove        (GCache            *cache,
984                                 gpointer           value);
985 void     g_cache_key_foreach   (GCache            *cache,
986                                 GHFunc             func,
987                                 gpointer           user_data);
988 void     g_cache_value_foreach (GCache            *cache,
989                                 GHFunc             func,
990                                 gpointer           user_data);
991
992
993 /* Balanced binary trees
994  */
995 GTree*   g_tree_new      (GCompareFunc   key_compare_func);
996 void     g_tree_destroy  (GTree         *tree);
997 void     g_tree_insert   (GTree         *tree,
998                           gpointer       key,
999                           gpointer       value);
1000 void     g_tree_remove   (GTree         *tree,
1001                           gpointer       key);
1002 gpointer g_tree_lookup   (GTree         *tree,
1003                           gpointer       key);
1004 void     g_tree_traverse (GTree         *tree,
1005                           GTraverseFunc  traverse_func,
1006                           GTraverseType  traverse_type,
1007                           gpointer       data);
1008 gpointer g_tree_search   (GTree         *tree,
1009                           GSearchFunc    search_func,
1010                           gpointer       data);
1011 gint     g_tree_height   (GTree         *tree);
1012 gint     g_tree_nnodes   (GTree         *tree);
1013
1014
1015
1016 /* N-way tree implementation
1017  */
1018 struct _GNode
1019 {
1020   gpointer data;
1021   GNode   *next;
1022   GNode   *prev;
1023   GNode   *parent;
1024   GNode   *children;
1025 };
1026
1027 #define  G_NODE_IS_ROOT(node)   (((GNode*) (node))->parent == NULL && \
1028                                  ((GNode*) (node))->prev == NULL && \
1029                                  ((GNode*) (node))->next == NULL)
1030 #define  G_NODE_IS_LEAF(node)   (((GNode*) (node))->children == NULL)
1031
1032 void     g_node_push_allocator  (GAllocator       *allocator);
1033 void     g_node_pop_allocator   (void);
1034 GNode*   g_node_new             (gpointer          data);
1035 void     g_node_destroy         (GNode            *root);
1036 void     g_node_unlink          (GNode            *node);
1037 GNode*   g_node_insert          (GNode            *parent,
1038                                  gint              position,
1039                                  GNode            *node);
1040 GNode*   g_node_insert_before   (GNode            *parent,
1041                                  GNode            *sibling,
1042                                  GNode            *node);
1043 GNode*   g_node_prepend         (GNode            *parent,
1044                                  GNode            *node);
1045 guint    g_node_n_nodes         (GNode            *root,
1046                                  GTraverseFlags    flags);
1047 GNode*   g_node_get_root        (GNode            *node);
1048 gboolean g_node_is_ancestor     (GNode            *node,
1049                                  GNode            *descendant);
1050 guint    g_node_depth           (GNode            *node);
1051 GNode*   g_node_find            (GNode            *root,
1052                                  GTraverseType     order,
1053                                  GTraverseFlags    flags,
1054                                  gpointer          data);
1055
1056 /* convenience macros */
1057 #define g_node_append(parent, node)                             \
1058      g_node_insert_before ((parent), NULL, (node))
1059 #define g_node_insert_data(parent, position, data)              \
1060      g_node_insert ((parent), (position), g_node_new (data))
1061 #define g_node_insert_data_before(parent, sibling, data)        \
1062      g_node_insert_before ((parent), (sibling), g_node_new (data))
1063 #define g_node_prepend_data(parent, data)                       \
1064      g_node_prepend ((parent), g_node_new (data))
1065 #define g_node_append_data(parent, data)                        \
1066      g_node_insert_before ((parent), NULL, g_node_new (data))
1067
1068 /* traversal function, assumes that `node' is root
1069  * (only traverses `node' and its subtree).
1070  * this function is just a high level interface to
1071  * low level traversal functions, optimized for speed.
1072  */
1073 void     g_node_traverse        (GNode            *root,
1074                                  GTraverseType     order,
1075                                  GTraverseFlags    flags,
1076                                  gint              max_depth,
1077                                  GNodeTraverseFunc func,
1078                                  gpointer          data);
1079
1080 /* return the maximum tree height starting with `node', this is an expensive
1081  * operation, since we need to visit all nodes. this could be shortened by
1082  * adding `guint height' to struct _GNode, but then again, this is not very
1083  * often needed, and would make g_node_insert() more time consuming.
1084  */
1085 guint    g_node_max_height       (GNode *root);
1086
1087 void     g_node_children_foreach (GNode           *node,
1088                                   GTraverseFlags   flags,
1089                                   GNodeForeachFunc func,
1090                                   gpointer         data);
1091 void     g_node_reverse_children (GNode           *node);
1092 guint    g_node_n_children       (GNode           *node);
1093 GNode*   g_node_nth_child        (GNode           *node,
1094                                   guint            n);
1095 GNode*   g_node_last_child       (GNode           *node);
1096 GNode*   g_node_find_child       (GNode           *node,
1097                                   GTraverseFlags   flags,
1098                                   gpointer         data);
1099 gint     g_node_child_position   (GNode           *node,
1100                                   GNode           *child);
1101 gint     g_node_child_index      (GNode           *node,
1102                                   gpointer         data);
1103
1104 GNode*   g_node_first_sibling    (GNode           *node);
1105 GNode*   g_node_last_sibling     (GNode           *node);
1106
1107 #define  g_node_prev_sibling(node)      ((node) ? \
1108                                          ((GNode*) (node))->prev : NULL)
1109 #define  g_node_next_sibling(node)      ((node) ? \
1110                                          ((GNode*) (node))->next : NULL)
1111 #define  g_node_first_child(node)       ((node) ? \
1112                                          ((GNode*) (node))->children : NULL)
1113
1114
1115 /* Callback maintenance functions
1116  */
1117 #define G_HOOK_FLAG_USER_SHIFT  (4)
1118 typedef enum
1119 {
1120   G_HOOK_FLAG_ACTIVE    = 1 << 0,
1121   G_HOOK_FLAG_IN_CALL   = 1 << 1,
1122   G_HOOK_FLAG_MASK      = 0x0f
1123 } GHookFlagMask;
1124
1125 struct _GHookList
1126 {
1127   guint          seq_id;
1128   guint          hook_size;
1129   guint          is_setup : 1;
1130   GHook         *hooks;
1131   GMemChunk     *hook_memchunk;
1132   GHookFreeFunc  hook_free; /* virtual function */
1133 };
1134
1135 struct _GHook
1136 {
1137   gpointer       data;
1138   GHook         *next;
1139   GHook         *prev;
1140   guint          ref_count;
1141   guint          hook_id;
1142   guint          flags;
1143   gpointer       func;
1144   GDestroyNotify destroy;
1145 };
1146
1147 #define G_HOOK_ACTIVE(hook)             ((((GHook*) hook)->flags & \
1148                                           G_HOOK_FLAG_ACTIVE) != 0)
1149 #define G_HOOK_IN_CALL(hook)            ((((GHook*) hook)->flags & \
1150                                           G_HOOK_FLAG_IN_CALL) != 0)
1151 #define G_HOOK_IS_VALID(hook)           (((GHook*) hook)->hook_id != 0 && \
1152                                          G_HOOK_ACTIVE (hook))
1153 #define G_HOOK_IS_UNLINKED(hook)        (((GHook*) hook)->next == NULL && \
1154                                          ((GHook*) hook)->prev == NULL && \
1155                                          ((GHook*) hook)->hook_id == 0 && \
1156                                          ((GHook*) hook)->ref_count == 0)
1157
1158 void     g_hook_list_init               (GHookList              *hook_list,
1159                                          guint                   hook_size);
1160 void     g_hook_list_clear              (GHookList              *hook_list);
1161 GHook*   g_hook_alloc                   (GHookList              *hook_list);
1162 void     g_hook_free                    (GHookList              *hook_list,
1163                                          GHook                  *hook);
1164 void     g_hook_ref                     (GHookList              *hook_list,
1165                                          GHook                  *hook);
1166 void     g_hook_unref                   (GHookList              *hook_list,
1167                                          GHook                  *hook);
1168 gboolean g_hook_destroy                 (GHookList              *hook_list,
1169                                          guint                   hook_id);
1170 void     g_hook_destroy_link            (GHookList              *hook_list,
1171                                          GHook                  *hook);
1172 void     g_hook_prepend                 (GHookList              *hook_list,
1173                                          GHook                  *hook);
1174 void     g_hook_insert_before           (GHookList              *hook_list,
1175                                          GHook                  *sibling,
1176                                          GHook                  *hook);
1177 void     g_hook_insert_sorted           (GHookList              *hook_list,
1178                                          GHook                  *hook,
1179                                          GHookCompareFunc        func);
1180 GHook*   g_hook_get                     (GHookList              *hook_list,
1181                                          guint                   hook_id);
1182 GHook*   g_hook_find                    (GHookList              *hook_list,
1183                                          gboolean                need_valids,
1184                                          GHookFindFunc           func,
1185                                          gpointer                data);
1186 GHook*   g_hook_find_data               (GHookList              *hook_list,
1187                                          gboolean                need_valids,
1188                                          gpointer                data);
1189 GHook*   g_hook_find_func               (GHookList              *hook_list,
1190                                          gboolean                need_valids,
1191                                          gpointer                func);
1192 GHook*   g_hook_find_func_data          (GHookList              *hook_list,
1193                                          gboolean                need_valids,
1194                                          gpointer                func,
1195                                          gpointer                data);
1196 /* return the first valid hook, and increment its reference count */
1197 GHook*   g_hook_first_valid             (GHookList              *hook_list,
1198                                          gboolean                may_be_in_call);
1199 /* return the next valid hook with incremented reference count, and
1200  * decrement the reference count of the original hook
1201  */
1202 GHook*   g_hook_next_valid              (GHookList              *hook_list,
1203                                          GHook                  *hook,
1204                                          gboolean                may_be_in_call);
1205
1206 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1207 gint     g_hook_compare_ids             (GHook                  *new_hook,
1208                                          GHook                  *sibling);
1209
1210 /* convenience macros */
1211 #define  g_hook_append( hook_list, hook )  \
1212      g_hook_insert_before ((hook_list), NULL, (hook))
1213
1214 /* invoke all valid hooks with the (*GHookFunc) signature.
1215  */
1216 void     g_hook_list_invoke             (GHookList              *hook_list,
1217                                          gboolean                may_recurse);
1218 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1219  * and destroy the hook if FALSE is returned.
1220  */
1221 void     g_hook_list_invoke_check       (GHookList              *hook_list,
1222                                          gboolean                may_recurse);
1223 /* invoke a marshaller on all valid hooks.
1224  */
1225 void     g_hook_list_marshal            (GHookList              *hook_list,
1226                                          gboolean                may_recurse,
1227                                          GHookMarshaller         marshaller,
1228                                          gpointer                data);
1229 void     g_hook_list_marshal_check      (GHookList              *hook_list,
1230                                          gboolean                may_recurse,
1231                                          GHookCheckMarshaller    marshaller,
1232                                          gpointer                data);
1233
1234
1235 /* Fatal error handlers.
1236  * g_on_error_query() will prompt the user to either
1237  * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1238  * g_on_error_stack_trace() invokes gdb, which attaches to the current
1239  * process and shows a stack trace.
1240  * These function may cause different actions on non-unix platforms.
1241  * The prg_name arg is required by gdb to find the executable, if it is
1242  * passed as NULL, g_on_error_query() will try g_get_prgname().
1243  */
1244 void g_on_error_query (const gchar *prg_name);
1245 void g_on_error_stack_trace (const gchar *prg_name);
1246
1247
1248 /* Logging mechanism
1249  */
1250 extern          const gchar             *g_log_domain_glib;
1251 guint           g_log_set_handler       (const gchar    *log_domain,
1252                                          GLogLevelFlags  log_levels,
1253                                          GLogFunc        log_func,
1254                                          gpointer        user_data);
1255 void            g_log_remove_handler    (const gchar    *log_domain,
1256                                          guint           handler_id);
1257 void            g_log_default_handler   (const gchar    *log_domain,
1258                                          GLogLevelFlags  log_level,
1259                                          const gchar    *message,
1260                                          gpointer        unused_data);
1261 void            g_log                   (const gchar    *log_domain,
1262                                          GLogLevelFlags  log_level,
1263                                          const gchar    *format,
1264                                          ...) G_GNUC_PRINTF (3, 4);
1265 void            g_logv                  (const gchar    *log_domain,
1266                                          GLogLevelFlags  log_level,
1267                                          const gchar    *format,
1268                                          va_list         args);
1269 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
1270                                          GLogLevelFlags  fatal_mask);
1271 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
1272 #ifndef G_LOG_DOMAIN
1273 #define G_LOG_DOMAIN    (NULL)
1274 #endif  /* G_LOG_DOMAIN */
1275 #ifdef  __GNUC__
1276 #define g_error(format, args...)        g_log (G_LOG_DOMAIN, \
1277                                                G_LOG_LEVEL_ERROR, \
1278                                                format, ##args)
1279 #define g_message(format, args...)      g_log (G_LOG_DOMAIN, \
1280                                                G_LOG_LEVEL_MESSAGE, \
1281                                                format, ##args)
1282 #define g_warning(format, args...)      g_log (G_LOG_DOMAIN, \
1283                                                G_LOG_LEVEL_WARNING, \
1284                                                format, ##args)
1285 #else   /* !__GNUC__ */
1286 static inline void
1287 g_error (const gchar *format,
1288          ...)
1289 {
1290   va_list args;
1291   va_start (args, format);
1292   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1293   va_end (args);
1294 }
1295 static inline void
1296 g_message (const gchar *format,
1297            ...)
1298 {
1299   va_list args;
1300   va_start (args, format);
1301   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1302   va_end (args);
1303 }
1304 static inline void
1305 g_warning (const gchar *format,
1306            ...)
1307 {
1308   va_list args;
1309   va_start (args, format);
1310   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1311   va_end (args);
1312 }
1313 #endif  /* !__GNUC__ */
1314
1315 typedef void    (*GPrintFunc)           (const gchar    *string);
1316 void            g_print                 (const gchar    *format,
1317                                          ...) G_GNUC_PRINTF (1, 2);
1318 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
1319 void            g_printerr              (const gchar    *format,
1320                                          ...) G_GNUC_PRINTF (1, 2);
1321 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
1322
1323 /* deprecated compatibility functions, use g_log_set_handler() instead */
1324 typedef void            (*GErrorFunc)           (const gchar *str);
1325 typedef void            (*GWarningFunc)         (const gchar *str);
1326 GErrorFunc   g_set_error_handler   (GErrorFunc   func);
1327 GWarningFunc g_set_warning_handler (GWarningFunc func);
1328 GPrintFunc   g_set_message_handler (GPrintFunc func);
1329
1330
1331 /* Memory allocation and debugging
1332  */
1333 #ifdef USE_DMALLOC
1334
1335 #define g_malloc(size)       ((gpointer) MALLOC (size))
1336 #define g_malloc0(size)      ((gpointer) CALLOC (char, size))
1337 #define g_realloc(mem,size)  ((gpointer) REALLOC (mem, char, size))
1338 #define g_free(mem)          FREE (mem)
1339
1340 #else /* !USE_DMALLOC */
1341
1342 gpointer g_malloc      (gulong    size);
1343 gpointer g_malloc0     (gulong    size);
1344 gpointer g_realloc     (gpointer  mem,
1345                         gulong    size);
1346 void     g_free        (gpointer  mem);
1347
1348 #endif /* !USE_DMALLOC */
1349
1350 void     g_mem_profile (void);
1351 void     g_mem_check   (gpointer  mem);
1352
1353 /* Generic allocators
1354  */
1355 GAllocator* g_allocator_new   (const gchar  *name,
1356                                guint         n_preallocs);
1357 void        g_allocator_free  (GAllocator   *allocator);
1358
1359 #define G_ALLOCATOR_LIST        (1)
1360 #define G_ALLOCATOR_SLIST       (2)
1361 #define G_ALLOCATOR_NODE        (3)
1362
1363
1364 /* "g_mem_chunk_new" creates a new memory chunk.
1365  * Memory chunks are used to allocate pieces of memory which are
1366  *  always the same size. Lists are a good example of such a data type.
1367  * The memory chunk allocates and frees blocks of memory as needed.
1368  *  Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1369  *  allocated in a mem chunk. ("g_free" will most likely cause a seg
1370  *  fault...somewhere).
1371  *
1372  * Oh yeah, GMemChunk is an opaque data type. (You don't really
1373  *  want to know what's going on inside do you?)
1374  */
1375
1376 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1377  *  is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1378  *  atom. (They are also useful for lists which use MemChunk to allocate
1379  *  memory but are also part of the MemChunk implementation).
1380  * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1381  */
1382
1383 #define G_ALLOC_ONLY      1
1384 #define G_ALLOC_AND_FREE  2
1385
1386 GMemChunk* g_mem_chunk_new     (gchar     *name,
1387                                 gint       atom_size,
1388                                 gulong     area_size,
1389                                 gint       type);
1390 void       g_mem_chunk_destroy (GMemChunk *mem_chunk);
1391 gpointer   g_mem_chunk_alloc   (GMemChunk *mem_chunk);
1392 gpointer   g_mem_chunk_alloc0  (GMemChunk *mem_chunk);
1393 void       g_mem_chunk_free    (GMemChunk *mem_chunk,
1394                                 gpointer   mem);
1395 void       g_mem_chunk_clean   (GMemChunk *mem_chunk);
1396 void       g_mem_chunk_reset   (GMemChunk *mem_chunk);
1397 void       g_mem_chunk_print   (GMemChunk *mem_chunk);
1398 void       g_mem_chunk_info    (void);
1399
1400 /* Ah yes...we have a "g_blow_chunks" function.
1401  * "g_blow_chunks" simply compresses all the chunks. This operation
1402  *  consists of freeing every memory area that should be freed (but
1403  *  which we haven't gotten around to doing yet). And, no,
1404  *  "g_blow_chunks" doesn't follow the naming scheme, but it is a
1405  *  much better name than "g_mem_chunk_clean_all" or something
1406  *  similar.
1407  */
1408 void g_blow_chunks (void);
1409
1410
1411 /* Timer
1412  */
1413 GTimer* g_timer_new     (void);
1414 void    g_timer_destroy (GTimer  *timer);
1415 void    g_timer_start   (GTimer  *timer);
1416 void    g_timer_stop    (GTimer  *timer);
1417 void    g_timer_reset   (GTimer  *timer);
1418 gdouble g_timer_elapsed (GTimer  *timer,
1419                          gulong  *microseconds);
1420
1421
1422 /* String utility functions that modify a string argument or
1423  * return a constant string that must not be freed.
1424  */
1425 #define  G_STR_DELIMITERS       "_-|> <."
1426 gchar*   g_strdelimit           (gchar       *string,
1427                                  const gchar *delimiters,
1428                                  gchar        new_delimiter);
1429 gdouble  g_strtod               (const gchar *nptr,
1430                                  gchar      **endptr);
1431 gchar*   g_strerror             (gint         errnum);
1432 gchar*   g_strsignal            (gint         signum);
1433 gint     g_strcasecmp           (const gchar *s1,
1434                                  const gchar *s2);
1435 gint     g_strncasecmp          (const gchar *s1,
1436                                  const gchar *s2,
1437                                  guint        n);
1438 void     g_strdown              (gchar       *string);
1439 void     g_strup                (gchar       *string);
1440 void     g_strreverse           (gchar       *string);
1441 /* removes leading spaces */
1442 gchar*   g_strchug              (gchar        *string);
1443 /* removes trailing spaces */
1444 gchar*  g_strchomp              (gchar        *string);
1445 /* removes leading & trailing spaces */
1446 #define g_strstrip( string )    g_strchomp (g_strchug (string))
1447
1448 /* String utility functions that return a newly allocated string which
1449  * ought to be freed from the caller at some point.
1450  */
1451 gchar*   g_strdup               (const gchar *str);
1452 gchar*   g_strdup_printf        (const gchar *format,
1453                                  ...) G_GNUC_PRINTF (1, 2);
1454 gchar*   g_strdup_vprintf       (const gchar *format,
1455                                  va_list      args);
1456 gchar*   g_strndup              (const gchar *str,
1457                                  guint        n);
1458 gchar*   g_strnfill             (guint        length,
1459                                  gchar        fill_char);
1460 gchar*   g_strconcat            (const gchar *string1,
1461                                  ...); /* NULL terminated */
1462 gchar*   g_strjoin              (const gchar  *separator,
1463                                  ...); /* NULL terminated */
1464 gchar*   g_strescape            (gchar        *string);
1465 gpointer g_memdup               (gconstpointer mem,
1466                                  guint         byte_size);
1467
1468 /* Macros for dynamic strings via fast stack allocation
1469  * All macros take a special first argument: the target gchar* string
1470  */
1471 #if G_HAVE_ALLOCA
1472
1473 #  define g_strdup_a(newstr,str) G_STMT_START { \
1474           if ((str) == NULL) (newstr) = NULL;   \
1475           else {                                \
1476             const char *__old = (str);          \
1477             char *__new;                        \
1478             size_t __len = strlen (__old) + 1;  \
1479             __new = alloca (__len);             \
1480             memcpy (__new, __old, __len);       \
1481             (newstr) = __new;                   \
1482           }                                     \
1483    } G_STMT_END
1484
1485 #  define g_strndup_a(newstr,str,n) G_STMT_START { \
1486           if ((str) == NULL) (newstr) = NULL;   \
1487           else {                                \
1488             const char *__old = (str);          \
1489             char *__new;                        \
1490             size_t __len = strlen (__old);      \
1491             if (__len > (n)) __len = (n);       \
1492             __new = alloca (__len + 1);         \
1493             memcpy (__new, __old, __len);       \
1494             __new[__len] = 0;                   \
1495             (newstr) = __new;                   \
1496           }                                     \
1497    } G_STMT_END
1498
1499 #  define g_strconcat3_a(newstr,str1,str2,str3) G_STMT_START { \
1500           size_t __len1 = ((str1) == (gchar*)NULL) ? 0 : strlen((str1)); \
1501           size_t __len2 = ((str2) == (gchar*)NULL) ? 0 : strlen((str2)); \
1502           size_t __len3 = ((str3) == (gchar*)NULL) ? 0 : strlen((str3)); \
1503           char *__sptr, *__new = \
1504                 alloca (__len1 + __len2 + __len3 + 1); \
1505           __sptr = __new; \
1506           if (__len1){memcpy (__sptr, (str1), __len1); __sptr += __len1;} \
1507           if (__len2){memcpy (__sptr, (str2), __len2); __sptr += __len2;} \
1508           if (__len3){memcpy (__sptr, (str3), __len3); __sptr += __len3;} \
1509           *__sptr = '\0'; \
1510           (newstr) = __new; \
1511    } G_STMT_END
1512
1513 #endif /* G_HAVE_ALLOCA */
1514
1515
1516 /* NULL terminated string arrays.
1517  * g_strsplit() splits up string into max_tokens tokens at delim and
1518  * returns a newly allocated string array.
1519  * g_strjoinv() concatenates all of str_array's strings, sliding in an
1520  * optional separator, the returned string is newly allocated.
1521  * g_strfreev() frees the array itself and all of its strings.
1522  */
1523 gchar**  g_strsplit             (const gchar  *string,
1524                                  const gchar  *delimiter,
1525                                  gint          max_tokens);
1526 gchar*   g_strjoinv             (const gchar  *separator,
1527                                  gchar       **str_array);
1528 void     g_strfreev             (gchar       **str_array);
1529
1530
1531
1532 /* calculate a string size, guarranteed to fit format + args.
1533  */
1534 guint   g_printf_string_upper_bound (const gchar* format,
1535                                      va_list      args);
1536
1537
1538 /* Retrive static string info
1539  */
1540 gchar*  g_get_user_name         (void);
1541 gchar*  g_get_real_name         (void);
1542 gchar*  g_get_home_dir          (void);
1543 gchar*  g_get_tmp_dir           (void);
1544 gchar*  g_get_prgname           (void);
1545 void    g_set_prgname           (const gchar *prgname);
1546
1547
1548 /* Miscellaneous utility functions
1549  */
1550 guint   g_parse_debug_string    (const gchar *string,
1551                                  GDebugKey   *keys,
1552                                  guint        nkeys);
1553 gint    g_snprintf              (gchar       *string,
1554                                  gulong       n,
1555                                  gchar const *format,
1556                                  ...) G_GNUC_PRINTF (3, 4);
1557 gint    g_vsnprintf             (gchar       *string,
1558                                  gulong       n,
1559                                  gchar const *format,
1560                                  va_list      args);
1561 gchar*  g_basename              (const gchar *file_name);
1562 /* Check if a file name is an absolute path */
1563 gboolean g_path_is_absolute     (const gchar *file_name);
1564 /* In case of absolute paths, skip the root part */
1565 gchar*  g_path_skip_root        (gchar       *file_name);
1566
1567 /* strings are newly allocated with g_malloc() */
1568 gchar*  g_dirname               (const gchar *file_name);
1569 gchar*  g_get_current_dir       (void);
1570 gchar*  g_getenv                (const gchar *variable);
1571
1572
1573 /* we use a GLib function as a replacement for ATEXIT, so
1574  * the programmer is not required to check the return value
1575  * (if there is any in the implementation) and doesn't encounter
1576  * missing include files.
1577  */
1578 void    g_atexit                (GVoidFunc    func);
1579
1580
1581 /* Bit tests
1582  */
1583 G_INLINE_FUNC gint      g_bit_nth_lsf (guint32 mask,
1584                                        gint    nth_bit);
1585 #ifdef  G_CAN_INLINE
1586 G_INLINE_FUNC gint
1587 g_bit_nth_lsf (guint32 mask,
1588                gint    nth_bit)
1589 {
1590   do
1591     {
1592       nth_bit++;
1593       if (mask & (1 << (guint) nth_bit))
1594         return nth_bit;
1595     }
1596   while (nth_bit < 32);
1597   return -1;
1598 }
1599 #endif  /* G_CAN_INLINE */
1600
1601 G_INLINE_FUNC gint      g_bit_nth_msf (guint32 mask,
1602                                        gint    nth_bit);
1603 #ifdef G_CAN_INLINE
1604 G_INLINE_FUNC gint
1605 g_bit_nth_msf (guint32 mask,
1606                gint    nth_bit)
1607 {
1608   if (nth_bit < 0)
1609     nth_bit = 32;
1610   do
1611     {
1612       nth_bit--;
1613       if (mask & (1 << (guint) nth_bit))
1614         return nth_bit;
1615     }
1616   while (nth_bit > 0);
1617   return -1;
1618 }
1619 #endif  /* G_CAN_INLINE */
1620
1621 G_INLINE_FUNC guint     g_bit_storage (guint number);
1622 #ifdef G_CAN_INLINE
1623 G_INLINE_FUNC guint
1624 g_bit_storage (guint number)
1625 {
1626   register guint n_bits = 0;
1627   
1628   do
1629     {
1630       n_bits++;
1631       number >>= 1;
1632     }
1633   while (number);
1634   return n_bits;
1635 }
1636 #endif  /* G_CAN_INLINE */
1637
1638 /* String Chunks
1639  */
1640 GStringChunk* g_string_chunk_new           (gint size);
1641 void          g_string_chunk_free          (GStringChunk *chunk);
1642 gchar*        g_string_chunk_insert        (GStringChunk *chunk,
1643                                             const gchar  *string);
1644 gchar*        g_string_chunk_insert_const  (GStringChunk *chunk,
1645                                             const gchar  *string);
1646
1647
1648 /* Strings
1649  */
1650 GString* g_string_new       (const gchar *init);
1651 GString* g_string_sized_new (guint        dfl_size);
1652 void     g_string_free      (GString     *string,
1653                              gint         free_segment);
1654 GString* g_string_assign    (GString     *lval,
1655                              const gchar *rval);
1656 GString* g_string_truncate  (GString     *string,
1657                              gint         len);
1658 GString* g_string_append    (GString     *string,
1659                              const gchar *val);
1660 GString* g_string_append_c  (GString     *string,
1661                              gchar        c);
1662 GString* g_string_prepend   (GString     *string,
1663                              const gchar *val);
1664 GString* g_string_prepend_c (GString     *string,
1665                              gchar        c);
1666 GString* g_string_insert    (GString     *string,
1667                              gint         pos,
1668                              const gchar *val);
1669 GString* g_string_insert_c  (GString     *string,
1670                              gint         pos,
1671                              gchar        c);
1672 GString* g_string_erase     (GString     *string,
1673                              gint         pos,
1674                              gint         len);
1675 GString* g_string_down      (GString     *string);
1676 GString* g_string_up        (GString     *string);
1677 void     g_string_sprintf   (GString     *string,
1678                              const gchar *format,
1679                              ...) G_GNUC_PRINTF (2, 3);
1680 void     g_string_sprintfa  (GString     *string,
1681                              const gchar *format,
1682                              ...) G_GNUC_PRINTF (2, 3);
1683
1684
1685 /* Resizable arrays, remove fills any cleared spot and shortens the
1686  * array, while preserving the order. remove_fast will distort the
1687  * order by moving the last element to the position of the removed 
1688  */
1689
1690 #define g_array_append_val(a,v) g_array_append_vals(a,&v,1)
1691 #define g_array_prepend_val(a,v) g_array_prepend_vals(a,&v,1)
1692 #define g_array_insert_val(a,i,v) g_array_prepend_vals(a,i,&v,1)
1693 #define g_array_index(a,t,i) (((t*)a->data)[i])
1694
1695 GArray* g_array_new               (gboolean         zero_terminated,
1696                                    gboolean         clear,
1697                                    guint            element_size);
1698 void    g_array_free              (GArray          *array,
1699                                    gboolean         free_segment);
1700 GArray* g_array_append_vals       (GArray          *array,
1701                                    gconstpointer    data,
1702                                    guint            len);
1703 GArray* g_array_prepend_vals      (GArray          *array,
1704                                    gconstpointer    data,
1705                                    guint            len);
1706 GArray* g_array_insert_vals       (GArray          *array,
1707                                    guint            index,
1708                                    gconstpointer    data,
1709                                    guint            len);
1710 GArray* g_array_set_size          (GArray          *array,
1711                                    guint            length);
1712 GArray* g_array_remove_index      (GArray          *array,
1713                                    guint            index);
1714 GArray* g_array_remove_index_fast (GArray          *array,
1715                                    guint            index);
1716
1717 /* Resizable pointer array.  This interface is much less complicated
1718  * than the above.  Add appends appends a pointer.  Remove fills any
1719  * cleared spot and shortens the array. remove_fast will again distort
1720  * order.  
1721  */
1722 #define     g_ptr_array_index(array,index) (array->pdata)[index]
1723 GPtrArray*  g_ptr_array_new                (void);
1724 void        g_ptr_array_free               (GPtrArray   *array,
1725                                             gboolean     free_seg);
1726 void        g_ptr_array_set_size           (GPtrArray   *array,
1727                                             gint         length);
1728 gpointer    g_ptr_array_remove_index       (GPtrArray   *array,
1729                                             guint        index);
1730 gpointer    g_ptr_array_remove_index_fast  (GPtrArray   *array,
1731                                             guint        index);
1732 gboolean    g_ptr_array_remove             (GPtrArray   *array,
1733                                             gpointer     data);
1734 gboolean    g_ptr_array_remove_fast        (GPtrArray   *array,
1735                                             gpointer     data);
1736 void        g_ptr_array_add                (GPtrArray   *array,
1737                                             gpointer     data);
1738
1739 /* Byte arrays, an array of guint8.  Implemented as a GArray,
1740  * but type-safe.
1741  */
1742
1743 GByteArray* g_byte_array_new               (void);
1744 void        g_byte_array_free              (GByteArray   *array,
1745                                             gboolean      free_segment);
1746 GByteArray* g_byte_array_append            (GByteArray   *array,
1747                                             const guint8 *data,
1748                                             guint         len);
1749 GByteArray* g_byte_array_prepend           (GByteArray   *array,
1750                                             const guint8 *data,
1751                                             guint         len);
1752 GByteArray* g_byte_array_set_size          (GByteArray   *array,
1753                                             guint         length);
1754 GByteArray* g_byte_array_remove_index      (GByteArray   *array,
1755                                             guint         index);
1756 GByteArray* g_byte_array_remove_index_fast (GByteArray   *array,
1757                                             guint         index);
1758
1759
1760 /* Hash Functions
1761  */
1762 gint  g_str_equal (gconstpointer   v,
1763                    gconstpointer   v2);
1764 guint g_str_hash  (gconstpointer   v);
1765
1766 gint  g_int_equal (gconstpointer   v,
1767                    gconstpointer   v2);
1768 guint g_int_hash  (gconstpointer   v);
1769
1770 /* This "hash" function will just return the key's adress as an
1771  * unsigned integer. Useful for hashing on plain adresses or
1772  * simple integer values.
1773  */
1774 guint g_direct_hash  (gconstpointer v);
1775 gint  g_direct_equal (gconstpointer v,
1776                       gconstpointer v2);
1777
1778
1779 /* Quarks (string<->id association)
1780  */
1781 GQuark    g_quark_try_string            (const gchar    *string);
1782 GQuark    g_quark_from_static_string    (const gchar    *string);
1783 GQuark    g_quark_from_string           (const gchar    *string);
1784 gchar*    g_quark_to_string             (GQuark          quark);
1785
1786
1787 /* Keyed Data List
1788  */
1789 void      g_datalist_init                (GData          **datalist);
1790 void      g_datalist_clear               (GData          **datalist);
1791 gpointer  g_datalist_id_get_data         (GData          **datalist,
1792                                           GQuark           key_id);
1793 void      g_datalist_id_set_data_full    (GData          **datalist,
1794                                           GQuark           key_id,
1795                                           gpointer         data,
1796                                           GDestroyNotify   destroy_func);
1797 void      g_datalist_id_remove_no_notify (GData          **datalist,
1798                                           GQuark           key_id);
1799 void      g_datalist_foreach             (GData          **datalist,
1800                                           GDataForeachFunc func,
1801                                           gpointer         user_data);
1802 #define   g_datalist_id_set_data(dl, q, d)      \
1803      g_datalist_id_set_data_full ((dl), (q), (d), NULL)
1804 #define   g_datalist_id_remove_data(dl, q)      \
1805      g_datalist_id_set_data ((dl), (q), NULL)
1806 #define   g_datalist_get_data(dl, k)            \
1807      (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
1808 #define   g_datalist_set_data_full(dl, k, d, f) \
1809      g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
1810 #define   g_datalist_remove_no_notify(dl, k)    \
1811      g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
1812 #define   g_datalist_set_data(dl, k, d)         \
1813      g_datalist_set_data_full ((dl), (k), (d), NULL)
1814 #define   g_datalist_remove_data(dl, k)         \
1815      g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
1816
1817
1818 /* Location Associated Keyed Data
1819  */
1820 void      g_dataset_destroy             (gconstpointer    dataset_location);
1821 gpointer  g_dataset_id_get_data         (gconstpointer    dataset_location,
1822                                          GQuark           key_id);
1823 void      g_dataset_id_set_data_full    (gconstpointer    dataset_location,
1824                                          GQuark           key_id,
1825                                          gpointer         data,
1826                                          GDestroyNotify   destroy_func);
1827 void      g_dataset_id_remove_no_notify (gconstpointer    dataset_location,
1828                                          GQuark           key_id);
1829 void      g_dataset_foreach             (gconstpointer    dataset_location,
1830                                          GDataForeachFunc func,
1831                                          gpointer         user_data);
1832 #define   g_dataset_id_set_data(l, k, d)        \
1833      g_dataset_id_set_data_full ((l), (k), (d), NULL)
1834 #define   g_dataset_id_remove_data(l, k)        \
1835      g_dataset_id_set_data ((l), (k), NULL)
1836 #define   g_dataset_get_data(l, k)              \
1837      (g_dataset_id_get_data ((l), g_quark_try_string (k)))
1838 #define   g_dataset_set_data_full(l, k, d, f)   \
1839      g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
1840 #define   g_dataset_remove_no_notify(l, k)      \
1841      g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
1842 #define   g_dataset_set_data(l, k, d)           \
1843      g_dataset_set_data_full ((l), (k), (d), NULL)
1844 #define   g_dataset_remove_data(l, k)           \
1845      g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
1846
1847
1848 /* GScanner: Flexible lexical scanner for general purpose.
1849  */
1850
1851 /* Character sets */
1852 #define G_CSET_A_2_Z    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1853 #define G_CSET_a_2_z    "abcdefghijklmnopqrstuvwxyz"
1854 #define G_CSET_LATINC   "\300\301\302\303\304\305\306"\
1855                         "\307\310\311\312\313\314\315\316\317\320"\
1856                         "\321\322\323\324\325\326"\
1857                         "\330\331\332\333\334\335\336"
1858 #define G_CSET_LATINS   "\337\340\341\342\343\344\345\346"\
1859                         "\347\350\351\352\353\354\355\356\357\360"\
1860                         "\361\362\363\364\365\366"\
1861                         "\370\371\372\373\374\375\376\377"
1862
1863 /* Error types */
1864 typedef enum
1865 {
1866   G_ERR_UNKNOWN,
1867   G_ERR_UNEXP_EOF,
1868   G_ERR_UNEXP_EOF_IN_STRING,
1869   G_ERR_UNEXP_EOF_IN_COMMENT,
1870   G_ERR_NON_DIGIT_IN_CONST,
1871   G_ERR_DIGIT_RADIX,
1872   G_ERR_FLOAT_RADIX,
1873   G_ERR_FLOAT_MALFORMED
1874 } GErrorType;
1875
1876 /* Token types */
1877 typedef enum
1878 {
1879   G_TOKEN_EOF                   =   0,
1880   
1881   G_TOKEN_LEFT_PAREN            = '(',
1882   G_TOKEN_RIGHT_PAREN           = ')',
1883   G_TOKEN_LEFT_CURLY            = '{',
1884   G_TOKEN_RIGHT_CURLY           = '}',
1885   G_TOKEN_LEFT_BRACE            = '[',
1886   G_TOKEN_RIGHT_BRACE           = ']',
1887   G_TOKEN_EQUAL_SIGN            = '=',
1888   G_TOKEN_COMMA                 = ',',
1889   
1890   G_TOKEN_NONE                  = 256,
1891   
1892   G_TOKEN_ERROR,
1893   
1894   G_TOKEN_CHAR,
1895   G_TOKEN_BINARY,
1896   G_TOKEN_OCTAL,
1897   G_TOKEN_INT,
1898   G_TOKEN_HEX,
1899   G_TOKEN_FLOAT,
1900   G_TOKEN_STRING,
1901   
1902   G_TOKEN_SYMBOL,
1903   G_TOKEN_IDENTIFIER,
1904   G_TOKEN_IDENTIFIER_NULL,
1905   
1906   G_TOKEN_COMMENT_SINGLE,
1907   G_TOKEN_COMMENT_MULTI,
1908   G_TOKEN_LAST
1909 } GTokenType;
1910
1911 union   _GTokenValue
1912 {
1913   gpointer      v_symbol;
1914   gchar         *v_identifier;
1915   gulong        v_binary;
1916   gulong        v_octal;
1917   gulong        v_int;
1918   gdouble       v_float;
1919   gulong        v_hex;
1920   gchar         *v_string;
1921   gchar         *v_comment;
1922   guchar        v_char;
1923   guint         v_error;
1924 };
1925
1926 struct  _GScannerConfig
1927 {
1928   /* Character sets
1929    */
1930   gchar         *cset_skip_characters;          /* default: " \t\n" */
1931   gchar         *cset_identifier_first;
1932   gchar         *cset_identifier_nth;
1933   gchar         *cpair_comment_single;          /* default: "#\n" */
1934   
1935   /* Should symbol lookup work case sensitive?
1936    */
1937   guint         case_sensitive : 1;
1938   
1939   /* Boolean values to be adjusted "on the fly"
1940    * to configure scanning behaviour.
1941    */
1942   guint         skip_comment_multi : 1;         /* C like comment */
1943   guint         skip_comment_single : 1;        /* single line comment */
1944   guint         scan_comment_multi : 1;         /* scan multi line comments? */
1945   guint         scan_identifier : 1;
1946   guint         scan_identifier_1char : 1;
1947   guint         scan_identifier_NULL : 1;
1948   guint         scan_symbols : 1;
1949   guint         scan_binary : 1;
1950   guint         scan_octal : 1;
1951   guint         scan_float : 1;
1952   guint         scan_hex : 1;                   /* `0x0ff0' */
1953   guint         scan_hex_dollar : 1;            /* `$0ff0' */
1954   guint         scan_string_sq : 1;             /* string: 'anything' */
1955   guint         scan_string_dq : 1;             /* string: "\\-escapes!\n" */
1956   guint         numbers_2_int : 1;              /* bin, octal, hex => int */
1957   guint         int_2_float : 1;                /* int => G_TOKEN_FLOAT? */
1958   guint         identifier_2_string : 1;
1959   guint         char_2_token : 1;               /* return G_TOKEN_CHAR? */
1960   guint         symbol_2_token : 1;
1961   guint         scope_0_fallback : 1;           /* try scope 0 on lookups? */
1962 };
1963
1964 struct  _GScanner
1965 {
1966   /* unused fields */
1967   gpointer              user_data;
1968   guint                 max_parse_errors;
1969   
1970   /* g_scanner_error() increments this field */
1971   guint                 parse_errors;
1972   
1973   /* name of input stream, featured by the default message handler */
1974   const gchar           *input_name;
1975   
1976   /* data pointer for derived structures */
1977   gpointer              derived_data;
1978   
1979   /* link into the scanner configuration */
1980   GScannerConfig        *config;
1981   
1982   /* fields filled in after g_scanner_get_next_token() */
1983   GTokenType            token;
1984   GTokenValue           value;
1985   guint                 line;
1986   guint                 position;
1987   
1988   /* fields filled in after g_scanner_peek_next_token() */
1989   GTokenType            next_token;
1990   GTokenValue           next_value;
1991   guint                 next_line;
1992   guint                 next_position;
1993   
1994   /* to be considered private */
1995   GHashTable            *symbol_table;
1996   gint                  input_fd;
1997   const gchar           *text;
1998   const gchar           *text_end;
1999   gchar                 *buffer;
2000   guint                 scope_id;
2001   
2002   /* handler function for _warn and _error */
2003   GScannerMsgFunc       msg_handler;
2004 };
2005
2006 GScanner*       g_scanner_new                   (GScannerConfig *config_templ);
2007 void            g_scanner_destroy               (GScanner       *scanner);
2008 void            g_scanner_input_file            (GScanner       *scanner,
2009                                                  gint           input_fd);
2010 void            g_scanner_sync_file_offset      (GScanner       *scanner);
2011 void            g_scanner_input_text            (GScanner       *scanner,
2012                                                  const  gchar   *text,
2013                                                  guint          text_len);
2014 GTokenType      g_scanner_get_next_token        (GScanner       *scanner);
2015 GTokenType      g_scanner_peek_next_token       (GScanner       *scanner);
2016 GTokenType      g_scanner_cur_token             (GScanner       *scanner);
2017 GTokenValue     g_scanner_cur_value             (GScanner       *scanner);
2018 guint           g_scanner_cur_line              (GScanner       *scanner);
2019 guint           g_scanner_cur_position          (GScanner       *scanner);
2020 gboolean        g_scanner_eof                   (GScanner       *scanner);
2021 guint           g_scanner_set_scope             (GScanner       *scanner,
2022                                                  guint           scope_id);
2023 void            g_scanner_scope_add_symbol      (GScanner       *scanner,
2024                                                  guint           scope_id,
2025                                                  const gchar    *symbol,
2026                                                  gpointer       value);
2027 void            g_scanner_scope_remove_symbol   (GScanner       *scanner,
2028                                                  guint           scope_id,
2029                                                  const gchar    *symbol);
2030 gpointer        g_scanner_scope_lookup_symbol   (GScanner       *scanner,
2031                                                  guint           scope_id,
2032                                                  const gchar    *symbol);
2033 void            g_scanner_scope_foreach_symbol  (GScanner       *scanner,
2034                                                  guint           scope_id,
2035                                                  GHFunc          func,
2036                                                  gpointer        user_data);
2037 gpointer        g_scanner_lookup_symbol         (GScanner       *scanner,
2038                                                  const gchar    *symbol);
2039 void            g_scanner_freeze_symbol_table   (GScanner       *scanner);
2040 void            g_scanner_thaw_symbol_table     (GScanner       *scanner);
2041 void            g_scanner_unexp_token           (GScanner       *scanner,
2042                                                  GTokenType     expected_token,
2043                                                  const gchar    *identifier_spec,
2044                                                  const gchar    *symbol_spec,
2045                                                  const gchar    *symbol_name,
2046                                                  const gchar    *message,
2047                                                  gint            is_error);
2048 void            g_scanner_error                 (GScanner       *scanner,
2049                                                  const gchar    *format,
2050                                                  ...) G_GNUC_PRINTF (2,3);
2051 void            g_scanner_warn                  (GScanner       *scanner,
2052                                                  const gchar    *format,
2053                                                  ...) G_GNUC_PRINTF (2,3);
2054 gint            g_scanner_stat_mode             (const gchar    *filename);
2055 /* keep downward source compatibility */
2056 #define         g_scanner_add_symbol( scanner, symbol, value )  G_STMT_START { \
2057   g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2058 } G_STMT_END
2059 #define         g_scanner_remove_symbol( scanner, symbol )      G_STMT_START { \
2060   g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2061 } G_STMT_END
2062 #define         g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2063   g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2064 } G_STMT_END
2065
2066
2067 /* GCompletion
2068  */
2069
2070 struct _GCompletion
2071 {
2072   GList* items;
2073   GCompletionFunc func;
2074   
2075   gchar* prefix;
2076   GList* cache;
2077 };
2078
2079 GCompletion* g_completion_new          (GCompletionFunc func);
2080 void         g_completion_add_items    (GCompletion*    cmp,
2081                                         GList*          items);
2082 void         g_completion_remove_items (GCompletion*    cmp,
2083                                         GList*          items);
2084 void         g_completion_clear_items  (GCompletion*    cmp);
2085 GList*       g_completion_complete     (GCompletion*    cmp,
2086                                         gchar*          prefix,
2087                                         gchar**         new_prefix);
2088 void         g_completion_free         (GCompletion*    cmp);
2089
2090
2091 /* GDate
2092  *
2093  * Date calculations (not time for now, to be resolved). These are a
2094  * mutant combination of Steffen Beyer's DateCalc routines
2095  * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2096  * date routines (written for in-house software).  Written by Havoc
2097  * Pennington <hp@pobox.com> 
2098  */
2099
2100 typedef guint16 GDateYear;
2101 typedef guint8  GDateDay;   /* day of the month */
2102 typedef struct _GDate GDate;
2103 /* make struct tm known without having to include time.h */
2104 struct tm;
2105
2106 /* enum used to specify order of appearance in parsed date strings */
2107 typedef enum
2108 {
2109   G_DATE_DAY   = 0,
2110   G_DATE_MONTH = 1,
2111   G_DATE_YEAR  = 2
2112 } GDateDMY;
2113
2114 /* actual week and month values */
2115 typedef enum
2116 {
2117   G_DATE_BAD_WEEKDAY  = 0,
2118   G_DATE_MONDAY       = 1,
2119   G_DATE_TUESDAY      = 2,
2120   G_DATE_WEDNESDAY    = 3,
2121   G_DATE_THURSDAY     = 4,
2122   G_DATE_FRIDAY       = 5,
2123   G_DATE_SATURDAY     = 6,
2124   G_DATE_SUNDAY       = 7
2125 } GDateWeekday;
2126 typedef enum
2127 {
2128   G_DATE_BAD_MONTH = 0,
2129   G_DATE_JANUARY   = 1,
2130   G_DATE_FEBRUARY  = 2,
2131   G_DATE_MARCH     = 3,
2132   G_DATE_APRIL     = 4,
2133   G_DATE_MAY       = 5,
2134   G_DATE_JUNE      = 6,
2135   G_DATE_JULY      = 7,
2136   G_DATE_AUGUST    = 8,
2137   G_DATE_SEPTEMBER = 9,
2138   G_DATE_OCTOBER   = 10,
2139   G_DATE_NOVEMBER  = 11,
2140   G_DATE_DECEMBER  = 12
2141 } GDateMonth;
2142
2143 #define G_DATE_BAD_JULIAN 0U
2144 #define G_DATE_BAD_DAY    0U
2145 #define G_DATE_BAD_YEAR   0U
2146
2147 /* Note: directly manipulating structs is generally a bad idea, but
2148  * in this case it's an *incredibly* bad idea, because all or part
2149  * of this struct can be invalid at any given time. Use the functions,
2150  * or you will get hosed, I promise.
2151  */
2152 struct _GDate
2153
2154   guint julian_days : 32; /* julian days representation - we use a
2155                            *  bitfield hoping that 64 bit platforms
2156                            *  will pack this whole struct in one big
2157                            *  int 
2158                            */
2159
2160   guint julian : 1;    /* julian is valid */
2161   guint dmy    : 1;    /* dmy is valid */
2162
2163   /* DMY representation */
2164   guint day    : 6;  
2165   guint month  : 4; 
2166   guint year   : 16; 
2167 };
2168
2169 /* g_date_new() returns an invalid date, you then have to _set() stuff 
2170  * to get a usable object. You can also allocate a GDate statically,
2171  * then call g_date_clear() to initialize.
2172  */
2173 GDate*       g_date_new                   (void);
2174 GDate*       g_date_new_dmy               (GDateDay     day, 
2175                                            GDateMonth   month, 
2176                                            GDateYear    year);
2177 GDate*       g_date_new_julian            (guint32      julian_day);
2178 void         g_date_free                  (GDate       *date);
2179
2180 /* check g_date_valid() after doing an operation that might fail, like
2181  * _parse.  Almost all g_date operations are undefined on invalid
2182  * dates (the exceptions are the mutators, since you need those to
2183  * return to validity).  
2184  */
2185 gboolean     g_date_valid                 (GDate       *date);
2186 gboolean     g_date_valid_day             (GDateDay     day);
2187 gboolean     g_date_valid_month           (GDateMonth   month);
2188 gboolean     g_date_valid_year            (GDateYear    year);
2189 gboolean     g_date_valid_weekday         (GDateWeekday weekday);
2190 gboolean     g_date_valid_julian          (guint32      julian_date);
2191 gboolean     g_date_valid_dmy             (GDateDay     day,
2192                                            GDateMonth   month,
2193                                            GDateYear    year);
2194
2195 GDateWeekday g_date_weekday               (GDate       *date);
2196 GDateMonth   g_date_month                 (GDate       *date);
2197 GDateYear    g_date_year                  (GDate       *date);
2198 GDateDay     g_date_day                   (GDate       *date);
2199 guint32      g_date_julian                (GDate       *date);
2200 guint        g_date_day_of_year           (GDate       *date);
2201
2202 /* First monday/sunday is the start of week 1; if we haven't reached
2203  * that day, return 0. These are not ISO weeks of the year; that
2204  * routine needs to be added.
2205  * these functions return the number of weeks, starting on the
2206  * corrsponding day
2207  */
2208 guint        g_date_monday_week_of_year   (GDate      *date);
2209 guint        g_date_sunday_week_of_year   (GDate      *date);
2210
2211 /* If you create a static date struct you need to clear it to get it
2212  * in a sane state before use. You can clear a whole array at
2213  * once with the ndates argument.
2214  */
2215 void         g_date_clear                 (GDate       *date, 
2216                                            guint        n_dates);
2217
2218 /* The parse routine is meant for dates typed in by a user, so it
2219  * permits many formats but tries to catch common typos. If your data
2220  * needs to be strictly validated, it is not an appropriate function.
2221  */
2222 void         g_date_set_parse             (GDate       *date,
2223                                            const gchar *str);
2224 void         g_date_set_time              (GDate       *date, 
2225                                            GTime        time);
2226 void         g_date_set_month             (GDate       *date, 
2227                                            GDateMonth   month);
2228 void         g_date_set_day               (GDate       *date, 
2229                                            GDateDay     day);
2230 void         g_date_set_year              (GDate       *date,
2231                                            GDateYear    year);
2232 void         g_date_set_dmy               (GDate       *date,
2233                                            GDateDay     day,
2234                                            GDateMonth   month,
2235                                            GDateYear    y);
2236 void         g_date_set_julian            (GDate       *date,
2237                                            guint32      julian_date);
2238 gboolean     g_date_is_first_of_month     (GDate       *date);
2239 gboolean     g_date_is_last_of_month      (GDate       *date);
2240
2241 /* To go forward by some number of weeks just go forward weeks*7 days */
2242 void         g_date_add_days              (GDate       *date, 
2243                                            guint        n_days);
2244 void         g_date_subtract_days         (GDate       *date, 
2245                                            guint        n_days);
2246
2247 /* If you add/sub months while day > 28, the day might change */
2248 void         g_date_add_months            (GDate       *date,
2249                                            guint        n_months);
2250 void         g_date_subtract_months       (GDate       *date,
2251                                            guint        n_months);
2252
2253 /* If it's feb 29, changing years can move you to the 28th */
2254 void         g_date_add_years             (GDate       *date,
2255                                            guint        n_years);
2256 void         g_date_subtract_years        (GDate       *date,
2257                                            guint        n_years);
2258 gboolean     g_date_is_leap_year          (GDateYear    year);
2259 guint8       g_date_days_in_month         (GDateMonth   month, 
2260                                            GDateYear    year);
2261 guint8       g_date_monday_weeks_in_year  (GDateYear    year);
2262 guint8       g_date_sunday_weeks_in_year  (GDateYear    year);
2263
2264 /* qsort-friendly (with a cast...) */
2265 gint         g_date_compare               (GDate       *lhs,
2266                                            GDate       *rhs);
2267 void         g_date_to_struct_tm          (GDate       *date,
2268                                            struct tm   *tm);
2269
2270 /* Just like strftime() except you can only use date-related formats.
2271  *   Using a time format is undefined.
2272  */
2273 gsize        g_date_strftime              (gchar       *s,
2274                                            gsize        slen,
2275                                            const gchar *format,
2276                                            GDate       *date);
2277
2278
2279 /* GRelation
2280  *
2281  * Indexed Relations.  Imagine a really simple table in a
2282  * database.  Relations are not ordered.  This data type is meant for
2283  * maintaining a N-way mapping.
2284  *
2285  * g_relation_new() creates a relation with FIELDS fields
2286  *
2287  * g_relation_destroy() frees all resources
2288  * g_tuples_destroy() frees the result of g_relation_select()
2289  *
2290  * g_relation_index() indexes relation FIELD with the provided
2291  *   equality and hash functions.  this must be done before any
2292  *   calls to insert are made.
2293  *
2294  * g_relation_insert() inserts a new tuple.  you are expected to
2295  *   provide the right number of fields.
2296  *
2297  * g_relation_delete() deletes all relations with KEY in FIELD
2298  * g_relation_select() returns ...
2299  * g_relation_count() counts ...
2300  */
2301
2302 GRelation* g_relation_new     (gint         fields);
2303 void       g_relation_destroy (GRelation   *relation);
2304 void       g_relation_index   (GRelation   *relation,
2305                                gint         field,
2306                                GHashFunc    hash_func,
2307                                GCompareFunc key_compare_func);
2308 void       g_relation_insert  (GRelation   *relation,
2309                                ...);
2310 gint       g_relation_delete  (GRelation   *relation,
2311                                gconstpointer  key,
2312                                gint         field);
2313 GTuples*   g_relation_select  (GRelation   *relation,
2314                                gconstpointer  key,
2315                                gint         field);
2316 gint       g_relation_count   (GRelation   *relation,
2317                                gconstpointer  key,
2318                                gint         field);
2319 gboolean   g_relation_exists  (GRelation   *relation,
2320                                ...);
2321 void       g_relation_print   (GRelation   *relation);
2322
2323 void       g_tuples_destroy   (GTuples     *tuples);
2324 gpointer   g_tuples_index     (GTuples     *tuples,
2325                                gint         index,
2326                                gint         field);
2327
2328
2329 /* Prime numbers.
2330  */
2331
2332 /* This function returns prime numbers spaced by approximately 1.5-2.0
2333  * and is for use in resizing data structures which prefer
2334  * prime-valued sizes.  The closest spaced prime function returns the
2335  * next largest prime, or the highest it knows about which is about
2336  * MAXINT/4.
2337  */
2338 guint      g_spaced_primes_closest (guint num);
2339
2340
2341 /* GIOChannel
2342  */
2343
2344 typedef struct _GIOFuncs GIOFuncs;
2345 typedef enum
2346 {
2347   G_IO_ERROR_NONE,
2348   G_IO_ERROR_AGAIN,
2349   G_IO_ERROR_INVAL,
2350   G_IO_ERROR_UNKNOWN
2351 } GIOError;
2352 typedef enum
2353 {
2354   G_SEEK_CUR,
2355   G_SEEK_SET,
2356   G_SEEK_END
2357 } GSeekType;
2358 typedef enum
2359 {
2360   G_IO_IN       GLIB_SYSDEF_POLLIN,
2361   G_IO_OUT      GLIB_SYSDEF_POLLOUT,
2362   G_IO_PRI      GLIB_SYSDEF_POLLPRI,
2363   G_IO_ERR      GLIB_SYSDEF_POLLERR,
2364   G_IO_HUP      GLIB_SYSDEF_POLLHUP,
2365   G_IO_NVAL     GLIB_SYSDEF_POLLNVAL
2366 } GIOCondition;
2367
2368 struct _GIOChannel
2369 {
2370   guint channel_flags;
2371   guint ref_count;
2372   GIOFuncs *funcs;
2373 };
2374
2375 typedef gboolean (*GIOFunc) (GIOChannel   *source,
2376                              GIOCondition  condition,
2377                              gpointer      data);
2378 struct _GIOFuncs
2379 {
2380   GIOError (*io_read)   (GIOChannel     *channel, 
2381                          gchar          *buf, 
2382                          guint           count,
2383                          guint          *bytes_read);
2384   GIOError (*io_write)  (GIOChannel     *channel, 
2385                          gchar          *buf, 
2386                          guint           count,
2387                          guint          *bytes_written);
2388   GIOError (*io_seek)   (GIOChannel     *channel, 
2389                          gint            offset, 
2390                          GSeekType       type);
2391   void (*io_close)      (GIOChannel     *channel);
2392   guint (*io_add_watch) (GIOChannel     *channel,
2393                          gint            priority,
2394                          GIOCondition    condition,
2395                          GIOFunc         func,
2396                          gpointer        user_data,
2397                          GDestroyNotify  notify);
2398   void (*io_free)       (GIOChannel     *channel);
2399 };
2400
2401 void        g_io_channel_init   (GIOChannel    *channel);
2402 void        g_io_channel_ref    (GIOChannel    *channel);
2403 void        g_io_channel_unref  (GIOChannel    *channel);
2404 GIOError    g_io_channel_read   (GIOChannel    *channel, 
2405                                  gchar         *buf, 
2406                                  guint          count,
2407                                  guint         *bytes_read);
2408 GIOError  g_io_channel_write    (GIOChannel    *channel, 
2409                                  gchar         *buf, 
2410                                  guint          count,
2411                                  guint         *bytes_written);
2412 GIOError  g_io_channel_seek     (GIOChannel    *channel,
2413                                  gint           offset, 
2414                                  GSeekType      type);
2415 void      g_io_channel_close    (GIOChannel    *channel);
2416 guint     g_io_add_watch_full   (GIOChannel    *channel,
2417                                  gint           priority,
2418                                  GIOCondition   condition,
2419                                  GIOFunc        func,
2420                                  gpointer       user_data,
2421                                  GDestroyNotify notify);
2422 guint    g_io_add_watch         (GIOChannel    *channel,
2423                                  GIOCondition   condition,
2424                                  GIOFunc        func,
2425                                  gpointer       user_data);
2426
2427
2428 /* Main loop
2429  */
2430 typedef struct _GTimeVal        GTimeVal;
2431 typedef struct _GSourceFuncs    GSourceFuncs;
2432 typedef struct _GMainLoop       GMainLoop;      /* Opaque */
2433
2434 struct _GTimeVal
2435 {
2436   glong tv_sec;
2437   glong tv_usec;
2438 };
2439 struct _GSourceFuncs
2440 {
2441   gboolean (*prepare)  (gpointer  source_data, 
2442                         GTimeVal *current_time,
2443                         gint     *timeout);
2444   gboolean (*check)    (gpointer  source_data,
2445                         GTimeVal *current_time);
2446   gboolean (*dispatch) (gpointer  source_data, 
2447                         GTimeVal *current_time,
2448                         gpointer  user_data);
2449   GDestroyNotify destroy;
2450 };
2451
2452 /* Standard priorities */
2453
2454 #define G_PRIORITY_HIGH            -100
2455 #define G_PRIORITY_DEFAULT          0
2456 #define G_PRIORITY_HIGH_IDLE        100
2457 #define G_PRIORITY_DEFAULT_IDLE     200
2458 #define G_PRIORITY_LOW              300
2459
2460 typedef gboolean (*GSourceFunc) (gpointer data);
2461
2462 /* Hooks for adding to the main loop */
2463 guint g_source_add                  (gint           priority, 
2464                                      gboolean       can_recurse,
2465                                      GSourceFuncs  *funcs,
2466                                      gpointer       source_data, 
2467                                      gpointer       user_data,
2468                                      GDestroyNotify notify);
2469 void g_source_remove                (guint          tag);
2470 void g_source_remove_by_user_data   (gpointer       user_data);
2471 void g_source_remove_by_source_data (gpointer       source_data);
2472
2473 void g_get_current_time             (GTimeVal      *result);
2474
2475 /* Running the main loop */
2476 GMainLoop*      g_main_new              (gboolean        is_running);
2477 void            g_main_run              (GMainLoop      *loop);
2478 void            g_main_quit             (GMainLoop      *loop);
2479 void            g_main_destroy          (GMainLoop      *loop);
2480 gboolean        g_main_is_running       (GMainLoop      *loop);
2481
2482 /* Run a single iteration of the mainloop. If block is FALSE,
2483  * will never block
2484  */
2485 gboolean        g_main_iteration        (gboolean       may_block);
2486
2487 /* See if any events are pending */
2488 gboolean        g_main_pending          (void);
2489
2490 /* Idles and timeouts */
2491 guint           g_timeout_add_full      (gint           priority,
2492                                          guint          interval, 
2493                                          GSourceFunc    function,
2494                                          gpointer       data,
2495                                          GDestroyNotify notify);
2496 guint           g_timeout_add           (guint          interval,
2497                                          GSourceFunc    function,
2498                                          gpointer       data);
2499 guint           g_idle_add              (GSourceFunc    function,
2500                                          gpointer       data);
2501 guint           g_idle_add_full         (gint           priority,
2502                                          GSourceFunc    function,
2503                                          gpointer       data,
2504                                          GDestroyNotify destroy);
2505
2506 /* GPollFD
2507  *
2508  * Unix-specific IO and main loop calls
2509  */
2510
2511 typedef struct _GPollFD GPollFD;
2512 typedef gint    (*GPollFunc)    (GPollFD *ufds,
2513                                  guint    nfsd,
2514                                  gint     timeout);
2515 struct _GPollFD
2516 {
2517   gint          fd;
2518   gushort       events;
2519   gushort       revents;
2520 };
2521
2522 void        g_main_add_poll          (GPollFD    *fd,
2523                                       gint        priority);
2524 void        g_main_remove_poll       (GPollFD    *fd);
2525 void        g_main_set_poll_func     (GPollFunc   func);
2526
2527 GIOChannel* g_io_channel_unix_new    (int         fd);
2528 gint        g_io_channel_unix_get_fd (GIOChannel *channel);
2529
2530
2531 /* old IO Channels */
2532 #if 0
2533 /* IO Channels.
2534  * These are used for plug-in communication in the GIMP, for instance.
2535  * On Unix, it's simply an encapsulated file descriptor (a pipe).
2536  * On Windows, it's a handle to an anonymouos pipe, *and* (in the case
2537  * of the writable end) a thread id to post a message to when you have written
2538  * stuff.
2539  */
2540 struct _GIOChannel
2541 {
2542   gint fd;                      /* file handle (pseudo such in Win32) */
2543 #ifdef NATIVE_WIN32
2544   guint peer;                   /* thread to post message to */
2545   guint peer_fd;                /* read handle (in the other process) */
2546   guint offset;                 /* counter of accumulated bytes, to
2547                                  * be included in the message posted
2548                                  * so we keep in sync.
2549                                  */
2550   guint need_wakeups;           /* in output channels whether the reader
2551                                  * needs wakeups
2552                                  */
2553 #endif
2554 };
2555 GIOChannel *g_iochannel_new            (gint        fd);
2556 void        g_iochannel_free           (GIOChannel *channel);
2557 void        g_iochannel_close_and_free (GIOChannel *channel);
2558 void        g_iochannel_wakeup_peer    (GIOChannel *channel);
2559 #ifndef NATIVE_WIN32
2560 #  define   g_iochannel_wakeup_peer(channel) G_STMT_START { } G_STMT_END
2561 #endif
2562 #endif  /* old IO Channels */
2563
2564
2565 /* Windows emulation stubs for common unix functions
2566  */
2567 #ifdef NATIVE_WIN32
2568 #  define MAXPATHLEN 1024
2569 #  ifdef _MSC_VER
2570 typedef int pid_t;
2571
2572 /* These POSIXish functions are available in the Microsoft C library
2573  * prefixed with underscore (which of course technically speaking is
2574  * the Right Thing, as they are non-ANSI. Not that being non-ANSI
2575  * prevents Microsoft from practically requiring you to include
2576  * <windows.h> every now and then...).
2577  *
2578  * You still need to include the appropriate headers to get the
2579  * prototypes, <io.h> or <direct.h>.
2580  *
2581  * For some functions, we provide emulators in glib, which are prefixed
2582  * with gwin_.
2583  */
2584 #    define getcwd              _getcwd
2585 #    define getpid              _getpid
2586 #    define access              _access
2587 #    define open                _open
2588 #    define read                _read
2589 #    define write               _write
2590 #    define lseek               _lseek
2591 #    define close               _close
2592 #    define pipe(phandles)      _pipe (phandles, 4096, _O_BINARY)
2593 #    define popen               _popen
2594 #    define pclose              _pclose
2595 #    define fdopen              _fdopen
2596 #    define ftruncate(fd, size) gwin_ftruncate (fd, size)
2597 #    define opendir             gwin_opendir
2598 #    define readdir             gwin_readdir
2599 #    define rewinddir           gwin_rewinddir
2600 #    define closedir            gwin_closedir
2601 #    define NAME_MAX 255
2602 struct DIR
2603 {
2604   gchar    *dir_name;
2605   gboolean  just_opened;
2606   guint     find_file_handle;
2607   gpointer  find_file_data;
2608 };
2609 typedef struct DIR DIR;
2610 struct dirent
2611 {
2612   gchar  d_name[NAME_MAX + 1];
2613 };
2614 /* emulation functions */
2615 extern int      gwin_ftruncate  (gint            f,
2616                                  guint           size);
2617 DIR*            gwin_opendir    (const gchar    *dirname);
2618 struct dirent*  gwin_readdir    (DIR            *dir);
2619 void            gwin_rewinddir  (DIR            *dir);
2620 gint            gwin_closedir   (DIR            *dir);
2621 #  endif /* _MSC_VER */
2622 #endif   /* NATIVE_WIN32 */
2623
2624
2625 /* GLib Thread support
2626  */
2627 typedef struct _GMutex          GMutex;
2628 typedef struct _GCond           GCond;
2629 typedef struct _GPrivate        GPrivate;
2630 typedef struct _GStaticPrivate  GStaticPrivate;
2631 typedef struct _GThreadFunctions GThreadFunctions;
2632 struct _GThreadFunctions
2633 {
2634   GMutex*  (*mutex_new)       (void);
2635   void     (*mutex_lock)      (GMutex           *mutex);
2636   gboolean (*mutex_trylock)   (GMutex           *mutex);
2637   void     (*mutex_unlock)    (GMutex           *mutex);
2638   void     (*mutex_free)      (GMutex           *mutex);
2639   GCond*   (*cond_new)        (void);
2640   void     (*cond_signal)     (GCond            *cond);
2641   void     (*cond_broadcast)  (GCond            *cond);
2642   void     (*cond_wait)       (GCond            *cond,
2643                                GMutex           *mutex);
2644   gboolean (*cond_timed_wait) (GCond            *cond,
2645                                GMutex           *mutex, 
2646                                GTimeVal         *end_time);
2647   void      (*cond_free)      (GCond            *cond);
2648   GPrivate* (*private_new)    (GDestroyNotify    destructor);
2649   gpointer  (*private_get)    (GPrivate         *private_key);
2650   void      (*private_set)    (GPrivate         *private_key,
2651                                gpointer          data);
2652 };
2653
2654 GUTILS_C_VAR GThreadFunctions   g_thread_functions_for_glib_use;
2655 GUTILS_C_VAR gboolean           g_thread_use_default_impl;
2656 GUTILS_C_VAR gboolean           g_threads_got_initialized;
2657
2658 /* initializes the mutex/cond/private implementation for glib, might
2659  * only be called once, and must not be called directly or indirectly
2660  * from another glib-function, e.g. as a callback.
2661  */
2662 void    g_thread_init   (GThreadFunctions       *vtable);
2663
2664 /* internal function for fallback static mutex implementation */
2665 GMutex* g_static_mutex_get_mutex_impl   (GMutex **mutex);
2666
2667 /* shorthands for conditional and unconditional function calls */
2668 #define G_THREAD_UF(name, arglist) \
2669     (*g_thread_functions_for_glib_use . name) arglist
2670 #define G_THREAD_CF(name, fail, arg) \
2671     (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
2672 /* keep in mind, all those mutexes and static mutexes are not 
2673  * recursive in general, don't rely on that
2674  */
2675 #define g_thread_supported()    (g_threads_got_initialized)
2676 #define g_mutex_new()            G_THREAD_UF (mutex_new,      ())
2677 #define g_mutex_lock(mutex)      G_THREAD_CF (mutex_lock,     (void)0, (mutex))
2678 #define g_mutex_trylock(mutex)   G_THREAD_CF (mutex_trylock,  TRUE,    (mutex))
2679 #define g_mutex_unlock(mutex)    G_THREAD_CF (mutex_unlock,   (void)0, (mutex))
2680 #define g_mutex_free(mutex)      G_THREAD_CF (mutex_free,     (void)0, (mutex))
2681 #define g_cond_new()             G_THREAD_UF (cond_new,       ())
2682 #define g_cond_signal(cond)      G_THREAD_CF (cond_signal,    (void)0, (cond))
2683 #define g_cond_broadcast(cond)   G_THREAD_CF (cond_broadcast, (void)0, (cond))
2684 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait,      (void)0, (cond, \
2685                                                                         mutex))
2686 #define g_cond_free(cond)        G_THREAD_CF (cond_free,      (void)0, (cond))
2687 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
2688                                                               TRUE, \
2689                                                               (cond, mutex, \
2690                                                                abs_time))
2691 #define g_private_new(destructor)         G_THREAD_UF (private_new, (destructor))
2692 #define g_private_get(private_key)        G_THREAD_CF (private_get, \
2693                                                        ((gpointer)private_key), \
2694                                                        (private_key))
2695 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
2696                                                        (void) (private_key = \
2697                                                         (GPrivate*) (value)), \
2698                                                        (private_key, value))
2699 /* GStaticMutexes can be statically initialized with the value
2700  * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
2701  * much easier, than having to explicitly allocate the mutex before
2702  * use
2703  */
2704 #define g_static_mutex_lock(mutex) \
2705     g_mutex_lock (g_static_mutex_get_mutex (mutex))
2706 #define g_static_mutex_trylock(mutex) \
2707     g_mutex_trylock (g_static_mutex_get_mutex (mutex))
2708 #define g_static_mutex_unlock(mutex) \
2709     g_mutex_unlock (g_static_mutex_get_mutex (mutex)) 
2710 struct _GStaticPrivate
2711 {
2712   guint index;
2713 };
2714 #define G_STATIC_PRIVATE_INIT { 0 }
2715 gpointer g_static_private_get (GStaticPrivate   *private_key);
2716 void     g_static_private_set (GStaticPrivate   *private_key, 
2717                                gpointer          data,
2718                                GDestroyNotify    notify);
2719
2720 /* these are some convenience macros that expand to nothing if GLib was
2721  * configured with --deisable-threads. for using StaticMutexes, you
2722  * declare them with G_LOCK_DECLARE_STATIC (name) or G_LOCK_DECLARE (name)
2723  * if you need to export the mutex. name is a unique identifier for the
2724  * protected varibale or code portion. locking, testing and unlocking of
2725  * such mutexes can be done with G_LOCK(), G_UNLOCK() and G_TRYLOCK()
2726  * respectively.
2727  */
2728 extern void glib_dummy_decl (void);
2729 #define G_LOCK_NAME(name)               (g__ ## name ## _lock)
2730 #ifdef  G_THREADS_ENABLED
2731 #  define G_LOCK_DECLARE_STATIC(name)   static G_LOCK_DECLARE (name)
2732 #  define G_LOCK_DECLARE(name)          \
2733     GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT 
2734
2735 #  ifdef G_DEBUG_LOCKS
2736 #    define G_LOCK(name)                G_STMT_START{             \
2737         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
2738                "file %s: line %d (%s): locking: %s ",             \
2739                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
2740                #name);                                            \
2741         g_static_mutex_lock (G_LOCK_NAME (name));                 \
2742      }G_STMT_END
2743 #    define G_UNLOCK(name)              G_STMT_START{             \
2744         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
2745                "file %s: line %d (%s): unlocking: %s ",           \
2746                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
2747                #name);                                            \
2748        g_static_mutex_unlock (G_LOCK_NAME (name));                \
2749      }G_STMT_END
2750 #    define G_TRYLOCK(name)             G_STMT_START{             \
2751         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
2752                "file %s: line %d (%s): try locking: %s ",         \
2753                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
2754                #name);                                            \
2755      }G_STMT_END,       g_static_mutex_trylock (G_LOCK_NAME (name))
2756 #  else  /* !G_DEBUG_LOCKS */
2757 #    define G_LOCK(name) g_static_mutex_lock       (G_LOCK_NAME (name)) 
2758 #    define G_UNLOCK(name) g_static_mutex_unlock   (G_LOCK_NAME (name))
2759 #    define G_TRYLOCK(name) g_static_mutex_trylock (G_LOCK_NAME (name))
2760 #  endif /* !G_DEBUG_LOCKS */
2761 #else   /* !G_THREADS_ENABLED */
2762 #  define G_LOCK_DECLARE_STATIC(name)   extern void glib_dummy_decl (void)
2763 #  define G_LOCK_DECLARE(name)          extern void glib_dummy_decl (void)
2764 #  define G_LOCK(name)
2765 #  define G_UNLOCK(name)
2766 #  define G_TRYLOCK(name)               (FALSE)
2767 #endif  /* !G_THREADS_ENABLED */
2768
2769 #ifdef __cplusplus
2770 }
2771 #endif /* __cplusplus */
2772
2773
2774 #endif /* __G_LIB_H__ */