1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-1999. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
30 /* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
31 * where this is valid. This allows for warningless compilation of
32 * "long long" types even in the presence of '-ansi -pedantic'. This
33 * of course should be with the other GCC-isms below, but then
34 * glibconfig.h wouldn't load cleanly and it is better to have that
35 * here, than in glibconfig.h.
37 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
38 # define G_GNUC_EXTENSION __extension__
40 # define G_GNUC_EXTENSION
43 /* system specific config file glibconfig.h provides definitions for
44 * the extrema of many of the standard types. These are:
46 * G_MINSHORT, G_MAXSHORT
48 * G_MINLONG, G_MAXLONG
49 * G_MINFLOAT, G_MAXFLOAT
50 * G_MINDOUBLE, G_MAXDOUBLE
52 * It also provides the following typedefs:
59 * It defines the G_BYTE_ORDER symbol to one of G_*_ENDIAN (see later in
62 * And it provides a way to store and retrieve a `gint' in/from a `gpointer'.
63 * This is useful to pass an integer instead of a pointer to a callback.
65 * GINT_TO_POINTER (i), GUINT_TO_POINTER (i)
66 * GPOINTER_TO_INT (p), GPOINTER_TO_UINT (p)
68 * Finally, it provides the following wrappers to STDC functions:
70 * void g_memmove (gpointer dest, gconstpointer void *src, gulong count);
71 * A wrapper for STDC memmove, or an implementation, if memmove doesn't
72 * exist. The prototype looks like the above, give or take a const,
75 #include <glibconfig.h>
77 /* include varargs functions for assertment macros
81 /* optionally feature DMALLOC memory allocation debugger
90 /* On native Win32, directory separator is the backslash, and search path
91 * separator is the semicolon.
93 #define G_DIR_SEPARATOR '\\'
94 #define G_DIR_SEPARATOR_S "\\"
95 #define G_SEARCHPATH_SEPARATOR ';'
96 #define G_SEARCHPATH_SEPARATOR_S ";"
98 #else /* !G_OS_WIN32 */
102 #define G_DIR_SEPARATOR '/'
103 #define G_DIR_SEPARATOR_S "/"
104 #define G_SEARCHPATH_SEPARATOR ':'
105 #define G_SEARCHPATH_SEPARATOR_S ":"
107 #endif /* !G_OS_WIN32 */
111 #endif /* __cplusplus */
114 /* Provide definitions for some commonly used macros.
115 * Some of them are only provided if they haven't already
116 * been defined. It is assumed that if they are already
117 * defined then the current definition is correct.
122 # else /* !__cplusplus */
123 # define NULL ((void*) 0)
124 # endif /* !__cplusplus */
132 #define TRUE (!FALSE)
136 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
139 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
142 #define ABS(a) (((a) < 0) ? -(a) : (a))
145 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
147 #define G_STRINGIFY(macro_or_string) G_STRINGIFY_ARG (macro_or_string)
148 #define G_STRINGIFY_ARG(contents) #contents
150 /* Count the number of elements in an array. The array must be defined
151 * as such; using this with a dynamically allocated array will give
154 #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
156 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
157 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
159 #if !defined (G_VA_COPY)
160 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
161 # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
162 # elif defined (G_VA_COPY_AS_ARRAY)
163 # define G_VA_COPY(ap1, ap2) g_memmove ((ap1), (ap2), sizeof (va_list))
164 # else /* va_list is a pointer */
165 # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
166 # endif /* va_list is a pointer */
167 #endif /* !G_VA_COPY */
170 /* Provide convenience macros for handling structure
171 * fields through their offsets.
173 #define G_STRUCT_OFFSET(struct_type, member) \
174 ((gulong) ((gchar*) &((struct_type*) 0)->member))
175 #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
176 ((gpointer) ((gchar*) (struct_p) + (gulong) (struct_offset)))
177 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
178 (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
181 /* inlining hassle. for compilers that don't allow the `inline' keyword,
182 * mostly because of strict ANSI C compliance or dumbness, we try to fall
183 * back to either `__inline__' or `__inline'.
184 * we define G_CAN_INLINE, if the compiler seems to be actually
185 * *capable* to do function inlining, in which case inline function bodys
186 * do make sense. we also define G_INLINE_FUNC to properly export the
187 * function prototypes if no inlining can be performed.
188 * we special case most of the stuff, so inline functions can have a normal
189 * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
191 #ifndef G_INLINE_FUNC
192 # define G_CAN_INLINE 1
195 # if defined (__GNUC__) && defined (__STRICT_ANSI__)
197 # define inline __inline__
199 #else /* !G_HAVE_INLINE */
201 # if defined (G_HAVE___INLINE__)
202 # define inline __inline__
203 # else /* !inline && !__inline__ */
204 # if defined (G_HAVE___INLINE)
205 # define inline __inline
206 # else /* !inline && !__inline__ && !__inline */
207 # define inline /* don't inline, then */
208 # ifndef G_INLINE_FUNC
214 #ifndef G_INLINE_FUNC
217 # define G_INLINE_FUNC extern inline
220 # define G_INLINE_FUNC extern
222 # else /* !__GNUC__ */
224 # define G_INLINE_FUNC static inline
226 # define G_INLINE_FUNC extern
228 # endif /* !__GNUC__ */
229 #endif /* !G_INLINE_FUNC */
232 /* Provide simple macro statement wrappers (adapted from Perl):
233 * G_STMT_START { statements; } G_STMT_END;
234 * can be used as a single statement, as in
235 * if (x) G_STMT_START { ... } G_STMT_END; else ...
237 * For gcc we will wrap the statements within `({' and `})' braces.
238 * For SunOS they will be wrapped within `if (1)' and `else (void) 0',
239 * and otherwise within `do' and `while (0)'.
241 #if !(defined (G_STMT_START) && defined (G_STMT_END))
242 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
243 # define G_STMT_START (void)(
244 # define G_STMT_END )
246 # if (defined (sun) || defined (__sun__))
247 # define G_STMT_START if (1)
248 # define G_STMT_END else (void)0
250 # define G_STMT_START do
251 # define G_STMT_END while (0)
257 /* Provide macros to feature the GCC function attribute.
259 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
260 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
261 __attribute__((format (printf, format_idx, arg_idx)))
262 #define G_GNUC_SCANF( format_idx, arg_idx ) \
263 __attribute__((format (scanf, format_idx, arg_idx)))
264 #define G_GNUC_FORMAT( arg_idx ) \
265 __attribute__((format_arg (arg_idx)))
266 #define G_GNUC_NORETURN \
267 __attribute__((noreturn))
268 #define G_GNUC_CONST \
269 __attribute__((const))
270 #define G_GNUC_UNUSED \
271 __attribute__((unused))
272 #else /* !__GNUC__ */
273 #define G_GNUC_PRINTF( format_idx, arg_idx )
274 #define G_GNUC_SCANF( format_idx, arg_idx )
275 #define G_GNUC_FORMAT( arg_idx )
276 #define G_GNUC_NORETURN
278 #define G_GNUC_UNUSED
279 #endif /* !__GNUC__ */
281 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
282 * macros, so we can refer to them as strings unconditionally.
285 #define G_GNUC_FUNCTION __FUNCTION__
286 #define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
287 #else /* !__GNUC__ */
288 #define G_GNUC_FUNCTION ""
289 #define G_GNUC_PRETTY_FUNCTION ""
290 #endif /* !__GNUC__ */
292 /* we try to provide a usefull equivalent for ATEXIT if it is
293 * not defined, but use is actually abandoned. people should
294 * use g_atexit() instead.
297 # define ATEXIT(proc) g_ATEXIT(proc)
299 # define G_NATIVE_ATEXIT
302 /* Hacker macro to place breakpoints for elected machines.
303 * Actual use is strongly deprecated of course ;)
305 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
306 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
307 #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
308 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
309 #else /* !__i386__ && !__alpha__ */
310 #define G_BREAKPOINT()
311 #endif /* __i386__ */
314 /* Provide macros for easily allocating memory. The macros
315 * will cast the allocated memory to the specified type
316 * in order to avoid compiler warnings. (Makes the code neater).
320 # define g_new(type, count) (ALLOC (type, count))
321 # define g_new0(type, count) (CALLOC (type, count))
322 # define g_renew(type, mem, count) (REALLOC (mem, type, count))
323 #else /* __DMALLOC_H__ */
324 # define g_new(type, count) \
325 ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
326 # define g_new0(type, count) \
327 ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
328 # define g_renew(type, mem, count) \
329 ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
330 #endif /* __DMALLOC_H__ */
332 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
333 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
335 sizeof (type) * (pre_alloc), \
338 #define g_chunk_new(type, chunk) ( \
339 (type *) g_mem_chunk_alloc (chunk) \
341 #define g_chunk_new0(type, chunk) ( \
342 (type *) g_mem_chunk_alloc0 (chunk) \
344 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
345 g_mem_chunk_free ((mem_chunk), (mem)); \
349 /* Provide macros for error handling. The "assert" macros will
350 * exit on failure. The "return" macros will exit the current
351 * function. Two different definitions are given for the macros
352 * if G_DISABLE_ASSERT is not defined, in order to support gcc's
353 * __PRETTY_FUNCTION__ capability.
356 #ifdef G_DISABLE_ASSERT
358 #define g_assert(expr)
359 #define g_assert_not_reached()
361 #else /* !G_DISABLE_ASSERT */
365 #define g_assert(expr) G_STMT_START{ \
367 g_log (G_LOG_DOMAIN, \
369 "file %s: line %d (%s): assertion failed: (%s)", \
372 __PRETTY_FUNCTION__, \
375 #define g_assert_not_reached() G_STMT_START{ \
376 g_log (G_LOG_DOMAIN, \
378 "file %s: line %d (%s): should not be reached", \
381 __PRETTY_FUNCTION__); }G_STMT_END
383 #else /* !__GNUC__ */
385 #define g_assert(expr) G_STMT_START{ \
387 g_log (G_LOG_DOMAIN, \
389 "file %s: line %d: assertion failed: (%s)", \
394 #define g_assert_not_reached() G_STMT_START{ \
395 g_log (G_LOG_DOMAIN, \
397 "file %s: line %d: should not be reached", \
399 __LINE__); }G_STMT_END
401 #endif /* __GNUC__ */
403 #endif /* !G_DISABLE_ASSERT */
406 #ifdef G_DISABLE_CHECKS
408 #define g_return_if_fail(expr)
409 #define g_return_val_if_fail(expr,val)
411 #else /* !G_DISABLE_CHECKS */
415 #define g_return_if_fail(expr) G_STMT_START{ \
418 g_log (G_LOG_DOMAIN, \
419 G_LOG_LEVEL_CRITICAL, \
420 "file %s: line %d (%s): assertion `%s' failed.", \
423 __PRETTY_FUNCTION__, \
428 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
431 g_log (G_LOG_DOMAIN, \
432 G_LOG_LEVEL_CRITICAL, \
433 "file %s: line %d (%s): assertion `%s' failed.", \
436 __PRETTY_FUNCTION__, \
441 #else /* !__GNUC__ */
443 #define g_return_if_fail(expr) G_STMT_START{ \
446 g_log (G_LOG_DOMAIN, \
447 G_LOG_LEVEL_CRITICAL, \
448 "file %s: line %d: assertion `%s' failed.", \
455 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
458 g_log (G_LOG_DOMAIN, \
459 G_LOG_LEVEL_CRITICAL, \
460 "file %s: line %d: assertion `%s' failed.", \
467 #endif /* !__GNUC__ */
469 #endif /* !G_DISABLE_CHECKS */
472 /* Provide type definitions for commonly used types.
473 * These are useful because a "gint8" can be adjusted
474 * to be 1 byte (8 bits) on all platforms. Similarly and
475 * more importantly, "gint32" can be adjusted to be
476 * 4 bytes (32 bits) on all platforms.
480 typedef short gshort;
483 typedef gint gboolean;
485 typedef unsigned char guchar;
486 typedef unsigned short gushort;
487 typedef unsigned long gulong;
488 typedef unsigned int guint;
490 #define G_GSHORT_FORMAT "hi"
491 #define G_GUSHORT_FORMAT "hu"
492 #define G_GINT_FORMAT "i"
493 #define G_GUINT_FORMAT "u"
494 #define G_GLONG_FORMAT "li"
495 #define G_GULONG_FORMAT "lu"
497 typedef float gfloat;
498 typedef double gdouble;
500 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
501 * Since gldouble isn't used anywhere, just disable it for now */
504 #ifdef HAVE_LONG_DOUBLE
505 typedef long double gldouble;
506 #else /* HAVE_LONG_DOUBLE */
507 typedef double gldouble;
508 #endif /* HAVE_LONG_DOUBLE */
511 typedef void* gpointer;
512 typedef const void *gconstpointer;
515 typedef gint32 gssize;
516 typedef guint32 gsize;
517 typedef guint32 GQuark;
518 typedef gint32 GTime;
521 /* Portable endian checks and conversions
523 * glibconfig.h defines G_BYTE_ORDER which expands to one of
526 #define G_LITTLE_ENDIAN 1234
527 #define G_BIG_ENDIAN 4321
528 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
531 /* Basic bit swapping functions
533 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
534 (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
535 (((guint16) (val) & (guint16) 0xff00U) >> 8)))
536 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((guint32) ( \
537 (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
538 (((guint32) (val) & (guint32) 0x0000ff00U) << 8) | \
539 (((guint32) (val) & (guint32) 0x00ff0000U) >> 8) | \
540 (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
542 /* Intel specific stuff for speed
544 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
545 # define GUINT16_SWAP_LE_BE_X86(val) \
547 ({ register guint16 __v; \
548 if (__builtin_constant_p (val)) \
549 __v = GUINT16_SWAP_LE_BE_CONSTANT (val); \
551 __asm__ __const__ ("rorw $8, %w0" \
553 : "0" ((guint16) (val))); \
555 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
556 # if !defined(__i486__) && !defined(__i586__) \
557 && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
558 # define GUINT32_SWAP_LE_BE_X86(val) \
560 ({ register guint32 __v; \
561 if (__builtin_constant_p (val)) \
562 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
564 __asm__ __const__ ("rorw $8, %w0\n\t" \
568 : "0" ((guint32) (val))); \
570 # else /* 486 and higher has bswap */
571 # define GUINT32_SWAP_LE_BE_X86(val) \
573 ({ register guint32 __v; \
574 if (__builtin_constant_p (val)) \
575 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
577 __asm__ __const__ ("bswap %0" \
579 : "0" ((guint32) (val))); \
581 # endif /* processor specific 32-bit stuff */
582 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
583 #else /* !__i386__ */
584 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
585 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
586 #endif /* __i386__ */
589 # define GUINT64_SWAP_LE_BE_CONSTANT(val) ((guint64) ( \
590 (((guint64) (val) & \
591 (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) | \
592 (((guint64) (val) & \
593 (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) | \
594 (((guint64) (val) & \
595 (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) | \
596 (((guint64) (val) & \
597 (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) << 8) | \
598 (((guint64) (val) & \
599 (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >> 8) | \
600 (((guint64) (val) & \
601 (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) | \
602 (((guint64) (val) & \
603 (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) | \
604 (((guint64) (val) & \
605 (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
606 # if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
607 # define GUINT64_SWAP_LE_BE_X86(val) \
609 ({ union { guint64 __ll; \
610 guint32 __l[2]; } __r; \
611 if (__builtin_constant_p (val)) \
612 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val); \
615 union { guint64 __ll; \
616 guint32 __l[2]; } __w; \
617 __w.__ll = ((guint64) val); \
618 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
619 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
622 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
623 # else /* !__i386__ */
624 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
628 #define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
629 #define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
630 #define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
631 (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
632 (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
633 #define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
634 (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
635 (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
637 /* The G*_TO_?E() macros are defined in glibconfig.h.
638 * The transformation is symmetric, so the FROM just maps to the TO.
640 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
641 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
642 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
643 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
644 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
645 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
646 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
647 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
650 #define GINT64_FROM_LE(val) (GINT64_TO_LE (val))
651 #define GUINT64_FROM_LE(val) (GUINT64_TO_LE (val))
652 #define GINT64_FROM_BE(val) (GINT64_TO_BE (val))
653 #define GUINT64_FROM_BE(val) (GUINT64_TO_BE (val))
656 #define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
657 #define GULONG_FROM_LE(val) (GULONG_TO_LE (val))
658 #define GLONG_FROM_BE(val) (GLONG_TO_BE (val))
659 #define GULONG_FROM_BE(val) (GULONG_TO_BE (val))
661 #define GINT_FROM_LE(val) (GINT_TO_LE (val))
662 #define GUINT_FROM_LE(val) (GUINT_TO_LE (val))
663 #define GINT_FROM_BE(val) (GINT_TO_BE (val))
664 #define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
667 /* Portable versions of host-network order stuff
669 #define g_ntohl(val) (GUINT32_FROM_BE (val))
670 #define g_ntohs(val) (GUINT16_FROM_BE (val))
671 #define g_htonl(val) (GUINT32_TO_BE (val))
672 #define g_htons(val) (GUINT16_TO_BE (val))
676 * we prefix variable declarations so they can
677 * properly get exported in windows dlls.
680 # ifdef GLIB_COMPILATION
681 # define GUTILS_C_VAR __declspec(dllexport)
682 # else /* !GLIB_COMPILATION */
683 # define GUTILS_C_VAR extern __declspec(dllimport)
684 # endif /* !GLIB_COMPILATION */
685 #else /* !G_OS_WIN32 */
686 # define GUTILS_C_VAR extern
687 #endif /* !G_OS_WIN32 */
689 GUTILS_C_VAR const guint glib_major_version;
690 GUTILS_C_VAR const guint glib_minor_version;
691 GUTILS_C_VAR const guint glib_micro_version;
692 GUTILS_C_VAR const guint glib_interface_age;
693 GUTILS_C_VAR const guint glib_binary_age;
695 #define GLIB_CHECK_VERSION(major,minor,micro) \
696 (GLIB_MAJOR_VERSION > (major) || \
697 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
698 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
699 GLIB_MICRO_VERSION >= (micro)))
701 /* Forward declarations of glib types.
703 typedef struct _GAllocator GAllocator;
704 typedef struct _GArray GArray;
705 typedef struct _GByteArray GByteArray;
706 typedef struct _GCache GCache;
707 typedef struct _GCompletion GCompletion;
708 typedef struct _GData GData;
709 typedef struct _GDebugKey GDebugKey;
710 typedef union _GDoubleIEEE754 GDoubleIEEE754;
711 typedef union _GFloatIEEE754 GFloatIEEE754;
712 typedef struct _GHashTable GHashTable;
713 typedef struct _GHook GHook;
714 typedef struct _GHookList GHookList;
715 typedef struct _GList GList;
716 typedef struct _GMemChunk GMemChunk;
717 typedef struct _GNode GNode;
718 typedef struct _GPtrArray GPtrArray;
719 typedef struct _GQueue GQueue;
720 typedef struct _GRand GRand;
721 typedef struct _GRelation GRelation;
722 typedef struct _GScanner GScanner;
723 typedef struct _GScannerConfig GScannerConfig;
724 typedef struct _GSList GSList;
725 typedef struct _GString GString;
726 typedef struct _GStringChunk GStringChunk;
727 typedef struct _GTimer GTimer;
728 typedef struct _GTrashStack GTrashStack;
729 typedef struct _GTree GTree;
730 typedef struct _GTuples GTuples;
731 typedef union _GTokenValue GTokenValue;
732 typedef struct _GIOChannel GIOChannel;
734 /* Tree traverse flags */
737 G_TRAVERSE_LEAFS = 1 << 0,
738 G_TRAVERSE_NON_LEAFS = 1 << 1,
739 G_TRAVERSE_ALL = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
740 G_TRAVERSE_MASK = 0x03
743 /* Tree traverse orders */
752 /* Log level shift offset for user defined
753 * log levels (0-7 are used by GLib).
755 #define G_LOG_LEVEL_USER_SHIFT (8)
757 /* Glib log levels and flags.
762 G_LOG_FLAG_RECURSION = 1 << 0,
763 G_LOG_FLAG_FATAL = 1 << 1,
765 /* GLib log levels */
766 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
767 G_LOG_LEVEL_CRITICAL = 1 << 3,
768 G_LOG_LEVEL_WARNING = 1 << 4,
769 G_LOG_LEVEL_MESSAGE = 1 << 5,
770 G_LOG_LEVEL_INFO = 1 << 6,
771 G_LOG_LEVEL_DEBUG = 1 << 7,
773 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
776 /* GLib log levels that are considered fatal by default */
777 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
780 typedef gpointer (*GCacheNewFunc) (gpointer key);
781 typedef gpointer (*GCacheDupFunc) (gpointer value);
782 typedef void (*GCacheDestroyFunc) (gpointer value);
783 typedef gint (*GCompareFunc) (gconstpointer a,
785 typedef gchar* (*GCompletionFunc) (gpointer);
786 typedef void (*GDestroyNotify) (gpointer data);
787 typedef void (*GDataForeachFunc) (GQuark key_id,
790 typedef void (*GFunc) (gpointer data,
792 typedef guint (*GHashFunc) (gconstpointer key);
793 typedef void (*GFreeFunc) (gpointer data);
794 typedef void (*GHFunc) (gpointer key,
797 typedef gboolean (*GHRFunc) (gpointer key,
800 typedef gint (*GHookCompareFunc) (GHook *new_hook,
802 typedef gboolean (*GHookFindFunc) (GHook *hook,
804 typedef void (*GHookMarshaller) (GHook *hook,
806 typedef gboolean (*GHookCheckMarshaller) (GHook *hook,
808 typedef void (*GHookFunc) (gpointer data);
809 typedef gboolean (*GHookCheckFunc) (gpointer data);
810 typedef void (*GHookFreeFunc) (GHookList *hook_list,
812 typedef void (*GLogFunc) (const gchar *log_domain,
813 GLogLevelFlags log_level,
814 const gchar *message,
816 typedef gboolean (*GNodeTraverseFunc) (GNode *node,
818 typedef void (*GNodeForeachFunc) (GNode *node,
820 typedef void (*GScannerMsgFunc) (GScanner *scanner,
823 typedef gint (*GTraverseFunc) (gpointer key,
826 typedef void (*GVoidFunc) (void);
890 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
893 * +--------+---------------+---------------+
894 * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
895 * +--------+---------------+---------------+
896 * B0------------------->B1------->B2-->B3-->
898 * IEEE Standard 754 Double Precision Storage Format (gdouble):
900 * 63 62 52 51 32 31 0
901 * +--------+----------------+----------------+ +---------------+
902 * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
903 * +--------+----------------+----------------+ +---------------+
904 * B0--------------->B1---------->B2--->B3----> B4->B5->B6->B7->
906 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
907 #define G_IEEE754_FLOAT_BIAS (127)
908 #define G_IEEE754_DOUBLE_BIAS (1023)
909 /* multiply with base2 exponent to get base10 exponent (nomal numbers) */
910 #define G_LOG_2_BASE_10 (0.30102999566398119521)
911 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
917 guint biased_exponent : 8;
921 union _GDoubleIEEE754
925 guint mantissa_low : 32;
926 guint mantissa_high : 20;
927 guint biased_exponent : 11;
931 #elif G_BYTE_ORDER == G_BIG_ENDIAN
937 guint biased_exponent : 8;
941 union _GDoubleIEEE754
946 guint biased_exponent : 11;
947 guint mantissa_high : 20;
948 guint mantissa_low : 32;
951 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
952 #error unknown ENDIAN type
953 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
956 /* Doubly linked lists
958 void g_list_push_allocator (GAllocator *allocator);
959 void g_list_pop_allocator (void);
960 GList* g_list_alloc (void);
961 void g_list_free (GList *list);
962 void g_list_free_1 (GList *list);
963 GList* g_list_append (GList *list,
965 GList* g_list_prepend (GList *list,
967 GList* g_list_insert (GList *list,
970 GList* g_list_insert_sorted (GList *list,
973 GList* g_list_concat (GList *list1,
975 GList* g_list_remove (GList *list,
977 GList* g_list_remove_link (GList *list,
979 GList* g_list_delete_link (GList *list,
981 GList* g_list_reverse (GList *list);
982 GList* g_list_copy (GList *list);
983 GList* g_list_nth (GList *list,
985 GList* g_list_find (GList *list,
987 GList* g_list_find_custom (GList *list,
990 gint g_list_position (GList *list,
992 gint g_list_index (GList *list,
994 GList* g_list_last (GList *list);
995 GList* g_list_first (GList *list);
996 guint g_list_length (GList *list);
997 void g_list_foreach (GList *list,
1000 GList* g_list_sort (GList *list,
1001 GCompareFunc compare_func);
1002 gpointer g_list_nth_data (GList *list,
1004 #define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
1005 #define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
1008 /* Singly linked lists
1010 void g_slist_push_allocator (GAllocator *allocator);
1011 void g_slist_pop_allocator (void);
1012 GSList* g_slist_alloc (void);
1013 void g_slist_free (GSList *list);
1014 void g_slist_free_1 (GSList *list);
1015 GSList* g_slist_append (GSList *list,
1017 GSList* g_slist_prepend (GSList *list,
1019 GSList* g_slist_insert (GSList *list,
1022 GSList* g_slist_insert_sorted (GSList *list,
1025 GSList* g_slist_concat (GSList *list1,
1027 GSList* g_slist_remove (GSList *list,
1028 gconstpointer data);
1029 GSList* g_slist_remove_link (GSList *list,
1031 GSList* g_slist_delete_link (GSList *list,
1033 GSList* g_slist_reverse (GSList *list);
1034 GSList* g_slist_copy (GSList *list);
1035 GSList* g_slist_nth (GSList *list,
1037 GSList* g_slist_find (GSList *list,
1038 gconstpointer data);
1039 GSList* g_slist_find_custom (GSList *list,
1042 gint g_slist_position (GSList *list,
1044 gint g_slist_index (GSList *list,
1045 gconstpointer data);
1046 GSList* g_slist_last (GSList *list);
1047 guint g_slist_length (GSList *list);
1048 void g_slist_foreach (GSList *list,
1050 gpointer user_data);
1051 GSList* g_slist_sort (GSList *list,
1052 GCompareFunc compare_func);
1053 gpointer g_slist_nth_data (GSList *list,
1055 #define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
1060 GQueue* g_queue_new (void);
1061 void g_queue_free (GQueue *queue);
1062 void g_queue_push_head (GQueue *queue,
1064 void g_queue_push_tail (GQueue *queue,
1066 gpointer g_queue_pop_head (GQueue *queue);
1067 gpointer g_queue_pop_tail (GQueue *queue);
1068 gboolean g_queue_is_empty (GQueue *queue);
1069 gpointer g_queue_peek_head (GQueue *queue);
1070 gpointer g_queue_peek_tail (GQueue *queue);
1071 void g_queue_push_head_link (GQueue *queue,
1073 void g_queue_push_tail_link (GQueue *queue,
1075 GList* g_queue_pop_head_link (GQueue *queue);
1076 GList* g_queue_pop_tail_link (GQueue *queue);
1080 GHashTable* g_hash_table_new (GHashFunc hash_func,
1081 GCompareFunc key_compare_func);
1082 void g_hash_table_destroy (GHashTable *hash_table);
1083 void g_hash_table_insert (GHashTable *hash_table,
1086 void g_hash_table_remove (GHashTable *hash_table,
1088 gpointer g_hash_table_lookup (GHashTable *hash_table,
1090 gboolean g_hash_table_lookup_extended(GHashTable *hash_table,
1091 gconstpointer lookup_key,
1094 void g_hash_table_freeze (GHashTable *hash_table);
1095 void g_hash_table_thaw (GHashTable *hash_table);
1096 void g_hash_table_foreach (GHashTable *hash_table,
1098 gpointer user_data);
1099 guint g_hash_table_foreach_remove (GHashTable *hash_table,
1101 gpointer user_data);
1102 guint g_hash_table_size (GHashTable *hash_table);
1107 GCache* g_cache_new (GCacheNewFunc value_new_func,
1108 GCacheDestroyFunc value_destroy_func,
1109 GCacheDupFunc key_dup_func,
1110 GCacheDestroyFunc key_destroy_func,
1111 GHashFunc hash_key_func,
1112 GHashFunc hash_value_func,
1113 GCompareFunc key_compare_func);
1114 void g_cache_destroy (GCache *cache);
1115 gpointer g_cache_insert (GCache *cache,
1117 void g_cache_remove (GCache *cache,
1118 gconstpointer value);
1119 void g_cache_key_foreach (GCache *cache,
1121 gpointer user_data);
1122 void g_cache_value_foreach (GCache *cache,
1124 gpointer user_data);
1127 /* Balanced binary trees
1129 GTree* g_tree_new (GCompareFunc key_compare_func);
1130 void g_tree_destroy (GTree *tree);
1131 void g_tree_insert (GTree *tree,
1134 void g_tree_remove (GTree *tree,
1136 gpointer g_tree_lookup (GTree *tree,
1138 void g_tree_traverse (GTree *tree,
1139 GTraverseFunc traverse_func,
1140 GTraverseType traverse_type,
1142 gpointer g_tree_search (GTree *tree,
1143 GCompareFunc search_func,
1144 gconstpointer data);
1145 gint g_tree_height (GTree *tree);
1146 gint g_tree_nnodes (GTree *tree);
1150 /* N-way tree implementation
1161 #define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
1162 ((GNode*) (node))->prev == NULL && \
1163 ((GNode*) (node))->next == NULL)
1164 #define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
1166 void g_node_push_allocator (GAllocator *allocator);
1167 void g_node_pop_allocator (void);
1168 GNode* g_node_new (gpointer data);
1169 void g_node_destroy (GNode *root);
1170 void g_node_unlink (GNode *node);
1171 GNode* g_node_copy (GNode *node);
1172 GNode* g_node_insert (GNode *parent,
1175 GNode* g_node_insert_before (GNode *parent,
1178 GNode* g_node_prepend (GNode *parent,
1180 guint g_node_n_nodes (GNode *root,
1181 GTraverseFlags flags);
1182 GNode* g_node_get_root (GNode *node);
1183 gboolean g_node_is_ancestor (GNode *node,
1185 guint g_node_depth (GNode *node);
1186 GNode* g_node_find (GNode *root,
1187 GTraverseType order,
1188 GTraverseFlags flags,
1191 /* convenience macros */
1192 #define g_node_append(parent, node) \
1193 g_node_insert_before ((parent), NULL, (node))
1194 #define g_node_insert_data(parent, position, data) \
1195 g_node_insert ((parent), (position), g_node_new (data))
1196 #define g_node_insert_data_before(parent, sibling, data) \
1197 g_node_insert_before ((parent), (sibling), g_node_new (data))
1198 #define g_node_prepend_data(parent, data) \
1199 g_node_prepend ((parent), g_node_new (data))
1200 #define g_node_append_data(parent, data) \
1201 g_node_insert_before ((parent), NULL, g_node_new (data))
1203 /* traversal function, assumes that `node' is root
1204 * (only traverses `node' and its subtree).
1205 * this function is just a high level interface to
1206 * low level traversal functions, optimized for speed.
1208 void g_node_traverse (GNode *root,
1209 GTraverseType order,
1210 GTraverseFlags flags,
1212 GNodeTraverseFunc func,
1215 /* return the maximum tree height starting with `node', this is an expensive
1216 * operation, since we need to visit all nodes. this could be shortened by
1217 * adding `guint height' to struct _GNode, but then again, this is not very
1218 * often needed, and would make g_node_insert() more time consuming.
1220 guint g_node_max_height (GNode *root);
1222 void g_node_children_foreach (GNode *node,
1223 GTraverseFlags flags,
1224 GNodeForeachFunc func,
1226 void g_node_reverse_children (GNode *node);
1227 guint g_node_n_children (GNode *node);
1228 GNode* g_node_nth_child (GNode *node,
1230 GNode* g_node_last_child (GNode *node);
1231 GNode* g_node_find_child (GNode *node,
1232 GTraverseFlags flags,
1234 gint g_node_child_position (GNode *node,
1236 gint g_node_child_index (GNode *node,
1239 GNode* g_node_first_sibling (GNode *node);
1240 GNode* g_node_last_sibling (GNode *node);
1242 #define g_node_prev_sibling(node) ((node) ? \
1243 ((GNode*) (node))->prev : NULL)
1244 #define g_node_next_sibling(node) ((node) ? \
1245 ((GNode*) (node))->next : NULL)
1246 #define g_node_first_child(node) ((node) ? \
1247 ((GNode*) (node))->children : NULL)
1250 /* Callback maintenance functions
1252 #define G_HOOK_FLAG_USER_SHIFT (4)
1255 G_HOOK_FLAG_ACTIVE = 1 << 0,
1256 G_HOOK_FLAG_IN_CALL = 1 << 1,
1257 G_HOOK_FLAG_MASK = 0x0f
1260 #define G_HOOK_DEFERRED_DESTROY ((GHookFreeFunc) 0x01)
1268 GMemChunk *hook_memchunk;
1269 GHookFreeFunc hook_free; /* virtual function */
1270 GHookFreeFunc hook_destroy; /* virtual function */
1282 GDestroyNotify destroy;
1285 #define G_HOOK_ACTIVE(hook) ((((GHook*) hook)->flags & \
1286 G_HOOK_FLAG_ACTIVE) != 0)
1287 #define G_HOOK_IN_CALL(hook) ((((GHook*) hook)->flags & \
1288 G_HOOK_FLAG_IN_CALL) != 0)
1289 #define G_HOOK_IS_VALID(hook) (((GHook*) hook)->hook_id != 0 && \
1290 G_HOOK_ACTIVE (hook))
1291 #define G_HOOK_IS_UNLINKED(hook) (((GHook*) hook)->next == NULL && \
1292 ((GHook*) hook)->prev == NULL && \
1293 ((GHook*) hook)->hook_id == 0 && \
1294 ((GHook*) hook)->ref_count == 0)
1296 void g_hook_list_init (GHookList *hook_list,
1298 void g_hook_list_clear (GHookList *hook_list);
1299 GHook* g_hook_alloc (GHookList *hook_list);
1300 void g_hook_free (GHookList *hook_list,
1302 void g_hook_ref (GHookList *hook_list,
1304 void g_hook_unref (GHookList *hook_list,
1306 gboolean g_hook_destroy (GHookList *hook_list,
1308 void g_hook_destroy_link (GHookList *hook_list,
1310 void g_hook_prepend (GHookList *hook_list,
1312 void g_hook_insert_before (GHookList *hook_list,
1315 void g_hook_insert_sorted (GHookList *hook_list,
1317 GHookCompareFunc func);
1318 GHook* g_hook_get (GHookList *hook_list,
1320 GHook* g_hook_find (GHookList *hook_list,
1321 gboolean need_valids,
1324 GHook* g_hook_find_data (GHookList *hook_list,
1325 gboolean need_valids,
1327 GHook* g_hook_find_func (GHookList *hook_list,
1328 gboolean need_valids,
1330 GHook* g_hook_find_func_data (GHookList *hook_list,
1331 gboolean need_valids,
1334 /* return the first valid hook, and increment its reference count */
1335 GHook* g_hook_first_valid (GHookList *hook_list,
1336 gboolean may_be_in_call);
1337 /* return the next valid hook with incremented reference count, and
1338 * decrement the reference count of the original hook
1340 GHook* g_hook_next_valid (GHookList *hook_list,
1342 gboolean may_be_in_call);
1344 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1345 gint g_hook_compare_ids (GHook *new_hook,
1348 /* convenience macros */
1349 #define g_hook_append( hook_list, hook ) \
1350 g_hook_insert_before ((hook_list), NULL, (hook))
1352 /* invoke all valid hooks with the (*GHookFunc) signature.
1354 void g_hook_list_invoke (GHookList *hook_list,
1355 gboolean may_recurse);
1356 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1357 * and destroy the hook if FALSE is returned.
1359 void g_hook_list_invoke_check (GHookList *hook_list,
1360 gboolean may_recurse);
1361 /* invoke a marshaller on all valid hooks.
1363 void g_hook_list_marshal (GHookList *hook_list,
1364 gboolean may_recurse,
1365 GHookMarshaller marshaller,
1367 void g_hook_list_marshal_check (GHookList *hook_list,
1368 gboolean may_recurse,
1369 GHookCheckMarshaller marshaller,
1373 /* Fatal error handlers.
1374 * g_on_error_query() will prompt the user to either
1375 * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1376 * g_on_error_stack_trace() invokes gdb, which attaches to the current
1377 * process and shows a stack trace.
1378 * These function may cause different actions on non-unix platforms.
1379 * The prg_name arg is required by gdb to find the executable, if it is
1380 * passed as NULL, g_on_error_query() will try g_get_prgname().
1382 void g_on_error_query (const gchar *prg_name);
1383 void g_on_error_stack_trace (const gchar *prg_name);
1386 /* Logging mechanism
1388 extern const gchar *g_log_domain_glib;
1389 guint g_log_set_handler (const gchar *log_domain,
1390 GLogLevelFlags log_levels,
1392 gpointer user_data);
1393 void g_log_remove_handler (const gchar *log_domain,
1395 void g_log_default_handler (const gchar *log_domain,
1396 GLogLevelFlags log_level,
1397 const gchar *message,
1398 gpointer unused_data);
1399 void g_log (const gchar *log_domain,
1400 GLogLevelFlags log_level,
1401 const gchar *format,
1402 ...) G_GNUC_PRINTF (3, 4);
1403 void g_logv (const gchar *log_domain,
1404 GLogLevelFlags log_level,
1405 const gchar *format,
1407 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
1408 GLogLevelFlags fatal_mask);
1409 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
1410 #ifndef G_LOG_DOMAIN
1411 #define G_LOG_DOMAIN ((gchar*) 0)
1412 #endif /* G_LOG_DOMAIN */
1414 #define g_error(format, args...) g_log (G_LOG_DOMAIN, \
1415 G_LOG_LEVEL_ERROR, \
1417 #define g_message(format, args...) g_log (G_LOG_DOMAIN, \
1418 G_LOG_LEVEL_MESSAGE, \
1420 #define g_warning(format, args...) g_log (G_LOG_DOMAIN, \
1421 G_LOG_LEVEL_WARNING, \
1423 #else /* !__GNUC__ */
1425 g_error (const gchar *format,
1429 va_start (args, format);
1430 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1434 g_message (const gchar *format,
1438 va_start (args, format);
1439 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1443 g_warning (const gchar *format,
1447 va_start (args, format);
1448 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1451 #endif /* !__GNUC__ */
1453 typedef void (*GPrintFunc) (const gchar *string);
1454 void g_print (const gchar *format,
1455 ...) G_GNUC_PRINTF (1, 2);
1456 GPrintFunc g_set_print_handler (GPrintFunc func);
1457 void g_printerr (const gchar *format,
1458 ...) G_GNUC_PRINTF (1, 2);
1459 GPrintFunc g_set_printerr_handler (GPrintFunc func);
1461 /* deprecated compatibility functions, use g_log_set_handler() instead */
1462 typedef void (*GErrorFunc) (const gchar *str);
1463 typedef void (*GWarningFunc) (const gchar *str);
1464 GErrorFunc g_set_error_handler (GErrorFunc func);
1465 GWarningFunc g_set_warning_handler (GWarningFunc func);
1466 GPrintFunc g_set_message_handler (GPrintFunc func);
1469 /* Memory allocation and debugging
1473 #define g_malloc(size) ((gpointer) MALLOC (size))
1474 #define g_malloc0(size) ((gpointer) CALLOC (char, size))
1475 #define g_realloc(mem,size) ((gpointer) REALLOC (mem, char, size))
1476 #define g_free(mem) FREE (mem)
1478 #else /* !USE_DMALLOC */
1480 gpointer g_malloc (gulong size);
1481 gpointer g_malloc0 (gulong size);
1482 gpointer g_realloc (gpointer mem,
1484 void g_free (gpointer mem);
1486 #endif /* !USE_DMALLOC */
1488 void g_mem_profile (void);
1489 void g_mem_check (gpointer mem);
1491 /* Generic allocators
1493 GAllocator* g_allocator_new (const gchar *name,
1495 void g_allocator_free (GAllocator *allocator);
1497 #define G_ALLOCATOR_LIST (1)
1498 #define G_ALLOCATOR_SLIST (2)
1499 #define G_ALLOCATOR_NODE (3)
1502 /* "g_mem_chunk_new" creates a new memory chunk.
1503 * Memory chunks are used to allocate pieces of memory which are
1504 * always the same size. Lists are a good example of such a data type.
1505 * The memory chunk allocates and frees blocks of memory as needed.
1506 * Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1507 * allocated in a mem chunk. ("g_free" will most likely cause a seg
1508 * fault...somewhere).
1510 * Oh yeah, GMemChunk is an opaque data type. (You don't really
1511 * want to know what's going on inside do you?)
1514 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1515 * is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1516 * atom. (They are also useful for lists which use MemChunk to allocate
1517 * memory but are also part of the MemChunk implementation).
1518 * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1521 #define G_ALLOC_ONLY 1
1522 #define G_ALLOC_AND_FREE 2
1524 GMemChunk* g_mem_chunk_new (gchar *name,
1528 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
1529 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
1530 gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
1531 void g_mem_chunk_free (GMemChunk *mem_chunk,
1533 void g_mem_chunk_clean (GMemChunk *mem_chunk);
1534 void g_mem_chunk_reset (GMemChunk *mem_chunk);
1535 void g_mem_chunk_print (GMemChunk *mem_chunk);
1536 void g_mem_chunk_info (void);
1538 /* Ah yes...we have a "g_blow_chunks" function.
1539 * "g_blow_chunks" simply compresses all the chunks. This operation
1540 * consists of freeing every memory area that should be freed (but
1541 * which we haven't gotten around to doing yet). And, no,
1542 * "g_blow_chunks" doesn't follow the naming scheme, but it is a
1543 * much better name than "g_mem_chunk_clean_all" or something
1546 void g_blow_chunks (void);
1552 #define G_MICROSEC 1000000
1554 GTimer* g_timer_new (void);
1555 void g_timer_destroy (GTimer *timer);
1556 void g_timer_start (GTimer *timer);
1557 void g_timer_stop (GTimer *timer);
1558 void g_timer_reset (GTimer *timer);
1559 gdouble g_timer_elapsed (GTimer *timer,
1560 gulong *microseconds);
1561 void g_usleep (gulong microseconds);
1563 /* String utility functions that modify a string argument or
1564 * return a constant string that must not be freed.
1566 #define G_STR_DELIMITERS "_-|> <."
1567 gchar* g_strdelimit (gchar *string,
1568 const gchar *delimiters,
1569 gchar new_delimiter);
1570 gdouble g_strtod (const gchar *nptr,
1572 gchar* g_strerror (gint errnum);
1573 gchar* g_strsignal (gint signum);
1574 gint g_strcasecmp (const gchar *s1,
1576 gint g_strncasecmp (const gchar *s1,
1579 void g_strdown (gchar *string);
1580 void g_strup (gchar *string);
1581 void g_strreverse (gchar *string);
1582 /* removes leading spaces */
1583 gchar* g_strchug (gchar *string);
1584 /* removes trailing spaces */
1585 gchar* g_strchomp (gchar *string);
1586 /* removes leading & trailing spaces */
1587 #define g_strstrip( string ) g_strchomp (g_strchug (string))
1589 /* String utility functions that return a newly allocated string which
1590 * ought to be freed with g_free from the caller at some point.
1592 gchar* g_strdup (const gchar *str);
1593 gchar* g_strdup_printf (const gchar *format,
1594 ...) G_GNUC_PRINTF (1, 2);
1595 gchar* g_strdup_vprintf (const gchar *format,
1597 gchar* g_strndup (const gchar *str,
1599 gchar* g_strnfill (guint length,
1601 gchar* g_strconcat (const gchar *string1,
1602 ...); /* NULL terminated */
1603 gchar* g_strjoin (const gchar *separator,
1604 ...); /* NULL terminated */
1605 /* Make a copy of a string interpreting C string -style escape
1606 * sequences. Inverse of g_strescape. The recognized sequences are \b
1607 * \f \n \r \t \\ \" and the octal format.
1609 gchar* g_strcompress (const gchar *source);
1611 /* Convert between the operating system (or C runtime)
1612 * representation of file names and UTF-8.
1614 gchar* g_filename_to_utf8 (const gchar *opsysstring);
1615 gchar* g_filename_from_utf8 (const gchar *utf8string);
1617 /* Copy a string escaping nonprintable characters like in C strings.
1618 * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
1619 * to a string containing characters that are not to be escaped.
1621 * Deprecated API: gchar* g_strescape (const gchar *source);
1622 * Luckily this function wasn't used much, using NULL as second parameter
1623 * provides mostly identical semantics.
1625 gchar* g_strescape (const gchar *source,
1626 const gchar *exceptions);
1628 gpointer g_memdup (gconstpointer mem,
1631 /* NULL terminated string arrays.
1632 * g_strsplit() splits up string into max_tokens tokens at delim and
1633 * returns a newly allocated string array.
1634 * g_strjoinv() concatenates all of str_array's strings, sliding in an
1635 * optional separator, the returned string is newly allocated.
1636 * g_strfreev() frees the array itself and all of its strings.
1638 gchar** g_strsplit (const gchar *string,
1639 const gchar *delimiter,
1641 gchar* g_strjoinv (const gchar *separator,
1643 void g_strfreev (gchar **str_array);
1647 /* calculate a string size, guarranteed to fit format + args.
1649 guint g_printf_string_upper_bound (const gchar* format,
1653 /* Retrive static string info
1655 gchar* g_get_user_name (void);
1656 gchar* g_get_real_name (void);
1657 gchar* g_get_home_dir (void);
1658 gchar* g_get_tmp_dir (void);
1659 gchar* g_get_prgname (void);
1660 void g_set_prgname (const gchar *prgname);
1663 /* Miscellaneous utility functions
1665 guint g_parse_debug_string (const gchar *string,
1668 gint g_snprintf (gchar *string,
1670 gchar const *format,
1671 ...) G_GNUC_PRINTF (3, 4);
1672 gint g_vsnprintf (gchar *string,
1674 gchar const *format,
1676 gchar* g_basename (const gchar *file_name);
1677 /* Check if a file name is an absolute path */
1678 gboolean g_path_is_absolute (const gchar *file_name);
1679 /* In case of absolute paths, skip the root part */
1680 gchar* g_path_skip_root (gchar *file_name);
1682 /* strings are newly allocated with g_malloc() */
1683 gchar* g_dirname (const gchar *file_name);
1684 gchar* g_get_current_dir (void);
1686 /* return the environment string for the variable. The returned memory
1687 * must not be freed. */
1688 gchar* g_getenv (const gchar *variable);
1690 /* we use a GLib function as a replacement for ATEXIT, so
1691 * the programmer is not required to check the return value
1692 * (if there is any in the implementation) and doesn't encounter
1693 * missing include files.
1695 void g_atexit (GVoidFunc func);
1700 G_INLINE_FUNC gint g_bit_nth_lsf (guint32 mask,
1704 g_bit_nth_lsf (guint32 mask,
1710 if (mask & (1 << (guint) nth_bit))
1713 while (nth_bit < 32);
1716 #endif /* G_CAN_INLINE */
1718 G_INLINE_FUNC gint g_bit_nth_msf (guint32 mask,
1722 g_bit_nth_msf (guint32 mask,
1730 if (mask & (1 << (guint) nth_bit))
1733 while (nth_bit > 0);
1736 #endif /* G_CAN_INLINE */
1738 G_INLINE_FUNC guint g_bit_storage (guint number);
1741 g_bit_storage (guint number)
1743 register guint n_bits = 0;
1753 #endif /* G_CAN_INLINE */
1757 * elements need to be >= sizeof (gpointer)
1759 G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p,
1763 g_trash_stack_push (GTrashStack **stack_p,
1766 GTrashStack *data = (GTrashStack *) data_p;
1768 data->next = *stack_p;
1771 #endif /* G_CAN_INLINE */
1773 G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p);
1775 G_INLINE_FUNC gpointer
1776 g_trash_stack_pop (GTrashStack **stack_p)
1783 *stack_p = data->next;
1784 /* NULLify private pointer here, most platforms store NULL as
1785 * subsequent 0 bytes
1792 #endif /* G_CAN_INLINE */
1794 G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p);
1796 G_INLINE_FUNC gpointer
1797 g_trash_stack_peek (GTrashStack **stack_p)
1805 #endif /* G_CAN_INLINE */
1807 G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p);
1810 g_trash_stack_height (GTrashStack **stack_p)
1815 for (data = *stack_p; data; data = data->next)
1820 #endif /* G_CAN_INLINE */
1825 GStringChunk* g_string_chunk_new (gint size);
1826 void g_string_chunk_free (GStringChunk *chunk);
1827 gchar* g_string_chunk_insert (GStringChunk *chunk,
1828 const gchar *string);
1829 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
1830 const gchar *string);
1835 GString* g_string_new (const gchar *init);
1836 GString* g_string_sized_new (guint dfl_size);
1837 void g_string_free (GString *string,
1838 gboolean free_segment);
1839 GString* g_string_assign (GString *string,
1841 GString* g_string_truncate (GString *string,
1843 GString* g_string_insert_len (GString *string,
1847 GString* g_string_append (GString *string,
1849 GString* g_string_append_len (GString *string,
1852 GString* g_string_append_c (GString *string,
1854 GString* g_string_prepend (GString *string,
1856 GString* g_string_prepend_c (GString *string,
1858 GString* g_string_prepend_len (GString *string,
1861 GString* g_string_insert (GString *string,
1864 GString* g_string_insert_c (GString *string,
1867 GString* g_string_erase (GString *string,
1870 GString* g_string_down (GString *string);
1871 GString* g_string_up (GString *string);
1872 void g_string_sprintf (GString *string,
1873 const gchar *format,
1874 ...) G_GNUC_PRINTF (2, 3);
1875 void g_string_sprintfa (GString *string,
1876 const gchar *format,
1877 ...) G_GNUC_PRINTF (2, 3);
1880 /* Resizable arrays, remove fills any cleared spot and shortens the
1881 * array, while preserving the order. remove_fast will distort the
1882 * order by moving the last element to the position of the removed
1885 #define g_array_append_val(a,v) g_array_append_vals (a, &v, 1)
1886 #define g_array_prepend_val(a,v) g_array_prepend_vals (a, &v, 1)
1887 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
1888 #define g_array_index(a,t,i) (((t*) (a)->data) [(i)])
1890 GArray* g_array_new (gboolean zero_terminated,
1892 guint element_size);
1893 GArray* g_array_sized_new (gboolean zero_terminated,
1896 guint reserved_size);
1897 void g_array_free (GArray *array,
1898 gboolean free_segment);
1899 GArray* g_array_append_vals (GArray *array,
1902 GArray* g_array_prepend_vals (GArray *array,
1905 GArray* g_array_insert_vals (GArray *array,
1909 GArray* g_array_set_size (GArray *array,
1911 GArray* g_array_remove_index (GArray *array,
1913 GArray* g_array_remove_index_fast (GArray *array,
1916 /* Resizable pointer array. This interface is much less complicated
1917 * than the above. Add appends appends a pointer. Remove fills any
1918 * cleared spot and shortens the array. remove_fast will again distort
1921 #define g_ptr_array_index(array,index) (array->pdata)[index]
1922 GPtrArray* g_ptr_array_new (void);
1923 GPtrArray* g_ptr_array_sized_new (guint reserved_size);
1924 void g_ptr_array_free (GPtrArray *array,
1926 void g_ptr_array_set_size (GPtrArray *array,
1928 gpointer g_ptr_array_remove_index (GPtrArray *array,
1930 gpointer g_ptr_array_remove_index_fast (GPtrArray *array,
1932 gboolean g_ptr_array_remove (GPtrArray *array,
1934 gboolean g_ptr_array_remove_fast (GPtrArray *array,
1936 void g_ptr_array_add (GPtrArray *array,
1939 /* Byte arrays, an array of guint8. Implemented as a GArray,
1943 GByteArray* g_byte_array_new (void);
1944 GByteArray* g_byte_array_sized_new (guint reserved_size);
1945 void g_byte_array_free (GByteArray *array,
1946 gboolean free_segment);
1947 GByteArray* g_byte_array_append (GByteArray *array,
1950 GByteArray* g_byte_array_prepend (GByteArray *array,
1953 GByteArray* g_byte_array_set_size (GByteArray *array,
1955 GByteArray* g_byte_array_remove_index (GByteArray *array,
1957 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1963 gint g_str_equal (gconstpointer v,
1965 guint g_str_hash (gconstpointer v);
1967 gint g_int_equal (gconstpointer v,
1969 guint g_int_hash (gconstpointer v);
1971 /* This "hash" function will just return the key's adress as an
1972 * unsigned integer. Useful for hashing on plain adresses or
1973 * simple integer values.
1974 * passing NULL into g_hash_table_new() as GHashFunc has the
1975 * same effect as passing g_direct_hash().
1977 guint g_direct_hash (gconstpointer v);
1978 gint g_direct_equal (gconstpointer v,
1982 /* Quarks (string<->id association)
1984 GQuark g_quark_try_string (const gchar *string);
1985 GQuark g_quark_from_static_string (const gchar *string);
1986 GQuark g_quark_from_string (const gchar *string);
1987 gchar* g_quark_to_string (GQuark quark);
1992 void g_datalist_init (GData **datalist);
1993 void g_datalist_clear (GData **datalist);
1994 gpointer g_datalist_id_get_data (GData **datalist,
1996 void g_datalist_id_set_data_full (GData **datalist,
1999 GDestroyNotify destroy_func);
2000 gpointer g_datalist_id_remove_no_notify (GData **datalist,
2002 void g_datalist_foreach (GData **datalist,
2003 GDataForeachFunc func,
2004 gpointer user_data);
2005 #define g_datalist_id_set_data(dl, q, d) \
2006 g_datalist_id_set_data_full ((dl), (q), (d), NULL)
2007 #define g_datalist_id_remove_data(dl, q) \
2008 g_datalist_id_set_data ((dl), (q), NULL)
2009 #define g_datalist_get_data(dl, k) \
2010 (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
2011 #define g_datalist_set_data_full(dl, k, d, f) \
2012 g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
2013 #define g_datalist_remove_no_notify(dl, k) \
2014 g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
2015 #define g_datalist_set_data(dl, k, d) \
2016 g_datalist_set_data_full ((dl), (k), (d), NULL)
2017 #define g_datalist_remove_data(dl, k) \
2018 g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
2021 /* Location Associated Keyed Data
2023 void g_dataset_destroy (gconstpointer dataset_location);
2024 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
2026 void g_dataset_id_set_data_full (gconstpointer dataset_location,
2029 GDestroyNotify destroy_func);
2030 gpointer g_dataset_id_remove_no_notify (gconstpointer dataset_location,
2032 void g_dataset_foreach (gconstpointer dataset_location,
2033 GDataForeachFunc func,
2034 gpointer user_data);
2035 #define g_dataset_id_set_data(l, k, d) \
2036 g_dataset_id_set_data_full ((l), (k), (d), NULL)
2037 #define g_dataset_id_remove_data(l, k) \
2038 g_dataset_id_set_data ((l), (k), NULL)
2039 #define g_dataset_get_data(l, k) \
2040 (g_dataset_id_get_data ((l), g_quark_try_string (k)))
2041 #define g_dataset_set_data_full(l, k, d, f) \
2042 g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
2043 #define g_dataset_remove_no_notify(l, k) \
2044 g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
2045 #define g_dataset_set_data(l, k, d) \
2046 g_dataset_set_data_full ((l), (k), (d), NULL)
2047 #define g_dataset_remove_data(l, k) \
2048 g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
2051 /* GScanner: Flexible lexical scanner for general purpose.
2054 /* Character sets */
2055 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2056 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
2057 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
2058 "\307\310\311\312\313\314\315\316\317\320"\
2059 "\321\322\323\324\325\326"\
2060 "\330\331\332\333\334\335\336"
2061 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
2062 "\347\350\351\352\353\354\355\356\357\360"\
2063 "\361\362\363\364\365\366"\
2064 "\370\371\372\373\374\375\376\377"
2071 G_ERR_UNEXP_EOF_IN_STRING,
2072 G_ERR_UNEXP_EOF_IN_COMMENT,
2073 G_ERR_NON_DIGIT_IN_CONST,
2076 G_ERR_FLOAT_MALFORMED
2084 G_TOKEN_LEFT_PAREN = '(',
2085 G_TOKEN_RIGHT_PAREN = ')',
2086 G_TOKEN_LEFT_CURLY = '{',
2087 G_TOKEN_RIGHT_CURLY = '}',
2088 G_TOKEN_LEFT_BRACE = '[',
2089 G_TOKEN_RIGHT_BRACE = ']',
2090 G_TOKEN_EQUAL_SIGN = '=',
2091 G_TOKEN_COMMA = ',',
2107 G_TOKEN_IDENTIFIER_NULL,
2109 G_TOKEN_COMMENT_SINGLE,
2110 G_TOKEN_COMMENT_MULTI,
2117 gchar *v_identifier;
2129 struct _GScannerConfig
2133 gchar *cset_skip_characters; /* default: " \t\n" */
2134 gchar *cset_identifier_first;
2135 gchar *cset_identifier_nth;
2136 gchar *cpair_comment_single; /* default: "#\n" */
2138 /* Should symbol lookup work case sensitive?
2140 guint case_sensitive : 1;
2142 /* Boolean values to be adjusted "on the fly"
2143 * to configure scanning behaviour.
2145 guint skip_comment_multi : 1; /* C like comment */
2146 guint skip_comment_single : 1; /* single line comment */
2147 guint scan_comment_multi : 1; /* scan multi line comments? */
2148 guint scan_identifier : 1;
2149 guint scan_identifier_1char : 1;
2150 guint scan_identifier_NULL : 1;
2151 guint scan_symbols : 1;
2152 guint scan_binary : 1;
2153 guint scan_octal : 1;
2154 guint scan_float : 1;
2155 guint scan_hex : 1; /* `0x0ff0' */
2156 guint scan_hex_dollar : 1; /* `$0ff0' */
2157 guint scan_string_sq : 1; /* string: 'anything' */
2158 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
2159 guint numbers_2_int : 1; /* bin, octal, hex => int */
2160 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
2161 guint identifier_2_string : 1;
2162 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
2163 guint symbol_2_token : 1;
2164 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
2171 guint max_parse_errors;
2173 /* g_scanner_error() increments this field */
2176 /* name of input stream, featured by the default message handler */
2177 const gchar *input_name;
2179 /* data pointer for derived structures */
2180 gpointer derived_data;
2182 /* link into the scanner configuration */
2183 GScannerConfig *config;
2185 /* fields filled in after g_scanner_get_next_token() */
2191 /* fields filled in after g_scanner_peek_next_token() */
2192 GTokenType next_token;
2193 GTokenValue next_value;
2195 guint next_position;
2197 /* to be considered private */
2198 GHashTable *symbol_table;
2201 const gchar *text_end;
2205 /* handler function for _warn and _error */
2206 GScannerMsgFunc msg_handler;
2209 GScanner* g_scanner_new (GScannerConfig *config_templ);
2210 void g_scanner_destroy (GScanner *scanner);
2211 void g_scanner_input_file (GScanner *scanner,
2213 void g_scanner_sync_file_offset (GScanner *scanner);
2214 void g_scanner_input_text (GScanner *scanner,
2217 GTokenType g_scanner_get_next_token (GScanner *scanner);
2218 GTokenType g_scanner_peek_next_token (GScanner *scanner);
2219 GTokenType g_scanner_cur_token (GScanner *scanner);
2220 GTokenValue g_scanner_cur_value (GScanner *scanner);
2221 guint g_scanner_cur_line (GScanner *scanner);
2222 guint g_scanner_cur_position (GScanner *scanner);
2223 gboolean g_scanner_eof (GScanner *scanner);
2224 guint g_scanner_set_scope (GScanner *scanner,
2226 void g_scanner_scope_add_symbol (GScanner *scanner,
2228 const gchar *symbol,
2230 void g_scanner_scope_remove_symbol (GScanner *scanner,
2232 const gchar *symbol);
2233 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
2235 const gchar *symbol);
2236 void g_scanner_scope_foreach_symbol (GScanner *scanner,
2239 gpointer user_data);
2240 gpointer g_scanner_lookup_symbol (GScanner *scanner,
2241 const gchar *symbol);
2242 void g_scanner_freeze_symbol_table (GScanner *scanner);
2243 void g_scanner_thaw_symbol_table (GScanner *scanner);
2244 void g_scanner_unexp_token (GScanner *scanner,
2245 GTokenType expected_token,
2246 const gchar *identifier_spec,
2247 const gchar *symbol_spec,
2248 const gchar *symbol_name,
2249 const gchar *message,
2251 void g_scanner_error (GScanner *scanner,
2252 const gchar *format,
2253 ...) G_GNUC_PRINTF (2,3);
2254 void g_scanner_warn (GScanner *scanner,
2255 const gchar *format,
2256 ...) G_GNUC_PRINTF (2,3);
2257 gint g_scanner_stat_mode (const gchar *filename);
2258 /* keep downward source compatibility */
2259 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
2260 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2262 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
2263 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2265 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2266 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2276 GCompletionFunc func;
2282 GCompletion* g_completion_new (GCompletionFunc func);
2283 void g_completion_add_items (GCompletion* cmp,
2285 void g_completion_remove_items (GCompletion* cmp,
2287 void g_completion_clear_items (GCompletion* cmp);
2288 GList* g_completion_complete (GCompletion* cmp,
2290 gchar** new_prefix);
2291 void g_completion_free (GCompletion* cmp);
2296 * Date calculations (not time for now, to be resolved). These are a
2297 * mutant combination of Steffen Beyer's DateCalc routines
2298 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2299 * date routines (written for in-house software). Written by Havoc
2300 * Pennington <hp@pobox.com>
2303 typedef guint16 GDateYear;
2304 typedef guint8 GDateDay; /* day of the month */
2305 typedef struct _GDate GDate;
2306 /* make struct tm known without having to include time.h */
2309 /* enum used to specify order of appearance in parsed date strings */
2317 /* actual week and month values */
2320 G_DATE_BAD_WEEKDAY = 0,
2323 G_DATE_WEDNESDAY = 3,
2324 G_DATE_THURSDAY = 4,
2326 G_DATE_SATURDAY = 6,
2331 G_DATE_BAD_MONTH = 0,
2333 G_DATE_FEBRUARY = 2,
2340 G_DATE_SEPTEMBER = 9,
2341 G_DATE_OCTOBER = 10,
2342 G_DATE_NOVEMBER = 11,
2343 G_DATE_DECEMBER = 12
2346 #define G_DATE_BAD_JULIAN 0U
2347 #define G_DATE_BAD_DAY 0U
2348 #define G_DATE_BAD_YEAR 0U
2350 /* Note: directly manipulating structs is generally a bad idea, but
2351 * in this case it's an *incredibly* bad idea, because all or part
2352 * of this struct can be invalid at any given time. Use the functions,
2353 * or you will get hosed, I promise.
2357 guint julian_days : 32; /* julian days representation - we use a
2358 * bitfield hoping that 64 bit platforms
2359 * will pack this whole struct in one big
2363 guint julian : 1; /* julian is valid */
2364 guint dmy : 1; /* dmy is valid */
2366 /* DMY representation */
2372 /* g_date_new() returns an invalid date, you then have to _set() stuff
2373 * to get a usable object. You can also allocate a GDate statically,
2374 * then call g_date_clear() to initialize.
2376 GDate* g_date_new (void);
2377 GDate* g_date_new_dmy (GDateDay day,
2380 GDate* g_date_new_julian (guint32 julian_day);
2381 void g_date_free (GDate *date);
2383 /* check g_date_valid() after doing an operation that might fail, like
2384 * _parse. Almost all g_date operations are undefined on invalid
2385 * dates (the exceptions are the mutators, since you need those to
2386 * return to validity).
2388 gboolean g_date_valid (GDate *date);
2389 gboolean g_date_valid_day (GDateDay day);
2390 gboolean g_date_valid_month (GDateMonth month);
2391 gboolean g_date_valid_year (GDateYear year);
2392 gboolean g_date_valid_weekday (GDateWeekday weekday);
2393 gboolean g_date_valid_julian (guint32 julian_date);
2394 gboolean g_date_valid_dmy (GDateDay day,
2398 GDateWeekday g_date_weekday (GDate *date);
2399 GDateMonth g_date_month (GDate *date);
2400 GDateYear g_date_year (GDate *date);
2401 GDateDay g_date_day (GDate *date);
2402 guint32 g_date_julian (GDate *date);
2403 guint g_date_day_of_year (GDate *date);
2405 /* First monday/sunday is the start of week 1; if we haven't reached
2406 * that day, return 0. These are not ISO weeks of the year; that
2407 * routine needs to be added.
2408 * these functions return the number of weeks, starting on the
2411 guint g_date_monday_week_of_year (GDate *date);
2412 guint g_date_sunday_week_of_year (GDate *date);
2414 /* If you create a static date struct you need to clear it to get it
2415 * in a sane state before use. You can clear a whole array at
2416 * once with the ndates argument.
2418 void g_date_clear (GDate *date,
2421 /* The parse routine is meant for dates typed in by a user, so it
2422 * permits many formats but tries to catch common typos. If your data
2423 * needs to be strictly validated, it is not an appropriate function.
2425 void g_date_set_parse (GDate *date,
2427 void g_date_set_time (GDate *date,
2429 void g_date_set_month (GDate *date,
2431 void g_date_set_day (GDate *date,
2433 void g_date_set_year (GDate *date,
2435 void g_date_set_dmy (GDate *date,
2439 void g_date_set_julian (GDate *date,
2440 guint32 julian_date);
2441 gboolean g_date_is_first_of_month (GDate *date);
2442 gboolean g_date_is_last_of_month (GDate *date);
2444 /* To go forward by some number of weeks just go forward weeks*7 days */
2445 void g_date_add_days (GDate *date,
2447 void g_date_subtract_days (GDate *date,
2450 /* If you add/sub months while day > 28, the day might change */
2451 void g_date_add_months (GDate *date,
2453 void g_date_subtract_months (GDate *date,
2456 /* If it's feb 29, changing years can move you to the 28th */
2457 void g_date_add_years (GDate *date,
2459 void g_date_subtract_years (GDate *date,
2461 gboolean g_date_is_leap_year (GDateYear year);
2462 guint8 g_date_days_in_month (GDateMonth month,
2464 guint8 g_date_monday_weeks_in_year (GDateYear year);
2465 guint8 g_date_sunday_weeks_in_year (GDateYear year);
2467 /* qsort-friendly (with a cast...) */
2468 gint g_date_compare (GDate *lhs,
2470 void g_date_to_struct_tm (GDate *date,
2473 /* Just like strftime() except you can only use date-related formats.
2474 * Using a time format is undefined.
2476 gsize g_date_strftime (gchar *s,
2478 const gchar *format,
2484 * Indexed Relations. Imagine a really simple table in a
2485 * database. Relations are not ordered. This data type is meant for
2486 * maintaining a N-way mapping.
2488 * g_relation_new() creates a relation with FIELDS fields
2490 * g_relation_destroy() frees all resources
2491 * g_tuples_destroy() frees the result of g_relation_select()
2493 * g_relation_index() indexes relation FIELD with the provided
2494 * equality and hash functions. this must be done before any
2495 * calls to insert are made.
2497 * g_relation_insert() inserts a new tuple. you are expected to
2498 * provide the right number of fields.
2500 * g_relation_delete() deletes all relations with KEY in FIELD
2501 * g_relation_select() returns ...
2502 * g_relation_count() counts ...
2505 GRelation* g_relation_new (gint fields);
2506 void g_relation_destroy (GRelation *relation);
2507 void g_relation_index (GRelation *relation,
2509 GHashFunc hash_func,
2510 GCompareFunc key_compare_func);
2511 void g_relation_insert (GRelation *relation,
2513 gint g_relation_delete (GRelation *relation,
2516 GTuples* g_relation_select (GRelation *relation,
2519 gint g_relation_count (GRelation *relation,
2522 gboolean g_relation_exists (GRelation *relation,
2524 void g_relation_print (GRelation *relation);
2526 void g_tuples_destroy (GTuples *tuples);
2527 gpointer g_tuples_index (GTuples *tuples,
2532 /* GRand - a good and fast random number generator: Mersenne Twister
2533 * see http://www.math.keio.ac.jp/~matumoto/emt.html for more info.
2534 * The range functions return a value in the intervall [min,max).
2535 * int -> [0..2^32-1]
2536 * int_range -> [min..max-1]
2538 * double_range -> [min..max)
2541 GRand* g_rand_new_with_seed (guint32 seed);
2542 GRand* g_rand_new (void);
2543 void g_rand_free (GRand *rand);
2545 void g_rand_set_seed (GRand *rand,
2547 guint32 g_rand_int (GRand *rand);
2548 gint32 g_rand_int_range (GRand *rand,
2551 gdouble g_rand_double (GRand *rand);
2552 gdouble g_rand_double_range (GRand *rand,
2556 void g_random_set_seed (guint32 seed);
2557 guint32 g_random_int (void);
2558 gint32 g_random_int_range (gint32 min,
2560 gdouble g_random_double (void);
2561 gdouble g_random_double_range (gdouble min,
2568 /* This function returns prime numbers spaced by approximately 1.5-2.0
2569 * and is for use in resizing data structures which prefer
2570 * prime-valued sizes. The closest spaced prime function returns the
2571 * next largest prime, or the highest it knows about which is about
2574 guint g_spaced_primes_closest (guint num);
2580 typedef struct _GIOFuncs GIOFuncs;
2596 G_IO_IN GLIB_SYSDEF_POLLIN,
2597 G_IO_OUT GLIB_SYSDEF_POLLOUT,
2598 G_IO_PRI GLIB_SYSDEF_POLLPRI,
2599 G_IO_ERR GLIB_SYSDEF_POLLERR,
2600 G_IO_HUP GLIB_SYSDEF_POLLHUP,
2601 G_IO_NVAL GLIB_SYSDEF_POLLNVAL
2606 guint channel_flags;
2611 typedef gboolean (*GIOFunc) (GIOChannel *source,
2612 GIOCondition condition,
2616 GIOError (*io_read) (GIOChannel *channel,
2620 GIOError (*io_write) (GIOChannel *channel,
2623 guint *bytes_written);
2624 GIOError (*io_seek) (GIOChannel *channel,
2627 void (*io_close) (GIOChannel *channel);
2628 guint (*io_add_watch) (GIOChannel *channel,
2630 GIOCondition condition,
2633 GDestroyNotify notify);
2634 void (*io_free) (GIOChannel *channel);
2637 void g_io_channel_init (GIOChannel *channel);
2638 void g_io_channel_ref (GIOChannel *channel);
2639 void g_io_channel_unref (GIOChannel *channel);
2640 GIOError g_io_channel_read (GIOChannel *channel,
2644 GIOError g_io_channel_write (GIOChannel *channel,
2647 guint *bytes_written);
2648 GIOError g_io_channel_seek (GIOChannel *channel,
2651 void g_io_channel_close (GIOChannel *channel);
2652 guint g_io_add_watch_full (GIOChannel *channel,
2654 GIOCondition condition,
2657 GDestroyNotify notify);
2658 guint g_io_add_watch (GIOChannel *channel,
2659 GIOCondition condition,
2661 gpointer user_data);
2666 typedef struct _GTimeVal GTimeVal;
2667 typedef struct _GSourceFuncs GSourceFuncs;
2668 typedef struct _GMainLoop GMainLoop; /* Opaque */
2675 struct _GSourceFuncs
2677 gboolean (*prepare) (gpointer source_data,
2678 GTimeVal *current_time,
2680 gpointer user_data);
2681 gboolean (*check) (gpointer source_data,
2682 GTimeVal *current_time,
2683 gpointer user_data);
2684 gboolean (*dispatch) (gpointer source_data,
2685 GTimeVal *dispatch_time,
2686 gpointer user_data);
2687 GDestroyNotify destroy;
2690 /* Standard priorities */
2692 #define G_PRIORITY_HIGH -100
2693 #define G_PRIORITY_DEFAULT 0
2694 #define G_PRIORITY_HIGH_IDLE 100
2695 #define G_PRIORITY_DEFAULT_IDLE 200
2696 #define G_PRIORITY_LOW 300
2698 typedef gboolean (*GSourceFunc) (gpointer data);
2700 /* Hooks for adding to the main loop */
2701 guint g_source_add (gint priority,
2702 gboolean can_recurse,
2703 GSourceFuncs *funcs,
2704 gpointer source_data,
2706 GDestroyNotify notify);
2707 gboolean g_source_remove (guint tag);
2708 gboolean g_source_remove_by_user_data (gpointer user_data);
2709 gboolean g_source_remove_by_source_data (gpointer source_data);
2710 gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
2711 gpointer user_data);
2713 void g_get_current_time (GTimeVal *result);
2715 /* Running the main loop */
2716 GMainLoop* g_main_new (gboolean is_running);
2717 void g_main_run (GMainLoop *loop);
2718 void g_main_quit (GMainLoop *loop);
2719 void g_main_destroy (GMainLoop *loop);
2720 gboolean g_main_is_running (GMainLoop *loop);
2722 /* Run a single iteration of the mainloop. If block is FALSE,
2725 gboolean g_main_iteration (gboolean may_block);
2727 /* See if any events are pending */
2728 gboolean g_main_pending (void);
2730 /* Idles and timeouts */
2731 guint g_timeout_add_full (gint priority,
2733 GSourceFunc function,
2735 GDestroyNotify notify);
2736 guint g_timeout_add (guint interval,
2737 GSourceFunc function,
2739 guint g_idle_add (GSourceFunc function,
2741 guint g_idle_add_full (gint priority,
2742 GSourceFunc function,
2744 GDestroyNotify destroy);
2745 gboolean g_idle_remove_by_data (gpointer data);
2749 * System-specific IO and main loop calls
2751 * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
2752 * descriptor as provided by the C runtime) that can be used by
2753 * MsgWaitForMultipleObjects. This does *not* include file handles
2754 * from CreateFile, SOCKETs, nor pipe handles. (But you can use
2755 * WSAEventSelect to signal events when a SOCKET is readable).
2757 * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
2758 * indicate polling for messages. These message queue GPollFDs should
2759 * be added with the g_main_poll_win32_msg_add function.
2761 * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
2762 * (GTK) programs, as GDK itself wants to read messages and convert them
2765 * So, unless you really know what you are doing, it's best not to try
2766 * to use the main loop polling stuff for your own needs on
2767 * Win32. It's really only written for the GIMP's needs so
2771 typedef struct _GPollFD GPollFD;
2772 typedef gint (*GPollFunc) (GPollFD *ufds,
2782 void g_main_add_poll (GPollFD *fd,
2784 void g_main_remove_poll (GPollFD *fd);
2785 void g_main_set_poll_func (GPollFunc func);
2787 /* On Unix, IO channels created with this function for any file
2788 * descriptor or socket.
2790 * On Win32, use this only for plain files opened with the MSVCRT (the
2791 * Microsoft run-time C library) _open(), including file descriptors
2792 * 0, 1 and 2 (corresponding to stdin, stdout and stderr).
2793 * Actually, don't do even that, this code isn't done yet.
2795 * The term file descriptor as used in the context of Win32 refers to
2796 * the emulated Unix-like file descriptors MSVCRT provides.
2798 GIOChannel* g_io_channel_unix_new (int fd);
2799 gint g_io_channel_unix_get_fd (GIOChannel *channel);
2803 GUTILS_C_VAR guint g_pipe_readable_msg;
2805 #define G_WIN32_MSG_HANDLE 19981206
2807 /* This is used to add polling for Windows messages. GDK (GTk+) programs
2808 * should *not* use this. (In fact, I can't think of any program that
2809 * would want to use this, but it's here just for completeness's sake.
2811 void g_main_poll_win32_msg_add(gint priority,
2815 /* An IO channel for Windows messages for window handle hwnd. */
2816 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
2818 /* An IO channel for an anonymous pipe as returned from the MSVCRT
2819 * _pipe(), with no mechanism for the writer to tell the reader when
2820 * there is data in the pipe.
2822 * This is not really implemented yet.
2824 GIOChannel *g_io_channel_win32_new_pipe (int fd);
2826 /* An IO channel for a pipe as returned from the MSVCRT _pipe(), with
2827 * Windows user messages used to signal data in the pipe for the
2830 * fd is the file descriptor. For the write end, peer is the thread id
2831 * of the reader, and peer_fd is his file descriptor for the read end
2834 * This is used by the GIMP, and works.
2836 GIOChannel *g_io_channel_win32_new_pipe_with_wakeups (int fd,
2840 void g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
2844 void g_io_channel_win32_pipe_readable (int fd,
2847 /* Get the C runtime file descriptor of a channel. */
2848 gint g_io_channel_win32_get_fd (GIOChannel *channel);
2850 /* An IO channel for a SOCK_STREAM winsock socket. The parameter is
2851 * actually a SOCKET.
2853 GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
2857 /* Windows emulation stubs for common Unix functions
2860 # define MAXPATHLEN 1024
2867 * To get prototypes for the following POSIXish functions, you have to
2868 * include the indicated non-POSIX headers. The functions are defined
2869 * in OLDNAMES.LIB (MSVC) or -lmoldname-msvc (mingw32).
2871 * getcwd: <direct.h> (MSVC), <io.h> (mingw32)
2872 * getpid: <process.h>
2874 * unlink: <stdio.h> or <io.h>
2875 * open, read, write, lseek, close: <io.h>
2880 /* pipe is not in OLDNAMES.LIB or -lmoldname-msvc. */
2881 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
2883 /* For some POSIX functions that are not provided by the MS runtime,
2884 * we provide emulators in glib, which are prefixed with g_win32_.
2886 # define ftruncate(fd, size) g_win32_ftruncate (fd, size)
2888 /* -lmingw32 also has emulations for these, but we need our own
2889 * for MSVC anyhow, so we might aswell use them always.
2891 # define opendir g_win32_opendir
2892 # define readdir g_win32_readdir
2893 # define rewinddir g_win32_rewinddir
2894 # define closedir g_win32_closedir
2895 # define NAME_MAX 255
2900 gboolean just_opened;
2901 guint find_file_handle;
2902 gpointer find_file_data;
2904 typedef struct DIR DIR;
2907 gchar d_name[NAME_MAX + 1];
2909 /* emulation functions */
2910 extern int g_win32_ftruncate (gint f,
2912 DIR* g_win32_opendir (const gchar *dirname);
2913 struct dirent* g_win32_readdir (DIR *dir);
2914 void g_win32_rewinddir (DIR *dir);
2915 gint g_win32_closedir (DIR *dir);
2917 /* The MS setlocale uses locale names of the form "English_United
2918 * States.1252" etc. We want the Unixish standard form "en", "zh_TW"
2919 * etc. This function gets the current thread locale from Windows and
2920 * returns it as a string of the above form for use in forming file
2921 * names etc. The returned string should be deallocated with g_free().
2923 gchar * g_win32_getlocale (void);
2925 /* Translate a Win32 error code (as returned by GetLastError()) into
2926 * the corresponding message. The returned string should be deallocated
2929 gchar * g_win32_error_message (gint error);
2931 #endif /* G_OS_WIN32 */
2934 /* GLib Thread support
2937 typedef void (*GThreadFunc) (gpointer value);
2941 G_THREAD_PRIORITY_LOW,
2942 G_THREAD_PRIORITY_NORMAL,
2943 G_THREAD_PRIORITY_HIGH,
2944 G_THREAD_PRIORITY_URGENT
2947 typedef struct _GThread GThread;
2950 GThreadPriority priority;
2955 typedef struct _GMutex GMutex;
2956 typedef struct _GCond GCond;
2957 typedef struct _GPrivate GPrivate;
2958 typedef struct _GStaticPrivate GStaticPrivate;
2959 typedef struct _GAsyncQueue GAsyncQueue;
2960 typedef struct _GThreadPool GThreadPool;
2962 typedef struct _GThreadFunctions GThreadFunctions;
2963 struct _GThreadFunctions
2965 GMutex* (*mutex_new) (void);
2966 void (*mutex_lock) (GMutex *mutex);
2967 gboolean (*mutex_trylock) (GMutex *mutex);
2968 void (*mutex_unlock) (GMutex *mutex);
2969 void (*mutex_free) (GMutex *mutex);
2970 GCond* (*cond_new) (void);
2971 void (*cond_signal) (GCond *cond);
2972 void (*cond_broadcast) (GCond *cond);
2973 void (*cond_wait) (GCond *cond,
2975 gboolean (*cond_timed_wait) (GCond *cond,
2977 GTimeVal *end_time);
2978 void (*cond_free) (GCond *cond);
2979 GPrivate* (*private_new) (GDestroyNotify destructor);
2980 gpointer (*private_get) (GPrivate *private_key);
2981 void (*private_set) (GPrivate *private_key,
2983 void (*thread_create) (GThreadFunc thread_func,
2988 GThreadPriority priority,
2990 void (*thread_yield) (void);
2991 void (*thread_join) (gpointer thread);
2992 void (*thread_exit) (void);
2993 void (*thread_set_priority)(gpointer thread,
2994 GThreadPriority priority);
2995 void (*thread_self) (gpointer thread);
2998 GUTILS_C_VAR GThreadFunctions g_thread_functions_for_glib_use;
2999 GUTILS_C_VAR gboolean g_thread_use_default_impl;
3000 GUTILS_C_VAR gboolean g_threads_got_initialized;
3002 /* initializes the mutex/cond/private implementation for glib, might
3003 * only be called once, and must not be called directly or indirectly
3004 * from another glib-function, e.g. as a callback.
3006 void g_thread_init (GThreadFunctions *vtable);
3008 /* internal function for fallback static mutex implementation */
3009 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
3011 /* shorthands for conditional and unconditional function calls */
3012 #define G_THREAD_UF(name, arglist) \
3013 (*g_thread_functions_for_glib_use . name) arglist
3014 #define G_THREAD_CF(name, fail, arg) \
3015 (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
3016 /* keep in mind, all those mutexes and static mutexes are not
3017 * recursive in general, don't rely on that
3019 #define g_thread_supported() (g_threads_got_initialized)
3020 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
3021 #define g_mutex_lock(mutex) G_THREAD_CF (mutex_lock, (void)0, (mutex))
3022 #define g_mutex_trylock(mutex) G_THREAD_CF (mutex_trylock, TRUE, (mutex))
3023 #define g_mutex_unlock(mutex) G_THREAD_CF (mutex_unlock, (void)0, (mutex))
3024 #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex))
3025 #define g_cond_new() G_THREAD_UF (cond_new, ())
3026 #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
3027 #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
3028 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait, (void)0, (cond, \
3030 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
3031 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
3035 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
3036 #define g_private_get(private_key) G_THREAD_CF (private_get, \
3037 ((gpointer)private_key), \
3039 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
3040 (void) (private_key = \
3041 (GPrivate*) (value)), \
3042 (private_key, value))
3043 #define g_thread_yield() G_THREAD_CF (thread_yield, (void)0, ())
3044 #define g_thread_exit() G_THREAD_CF (thread_exit, (void)0, ())
3046 GThread* g_thread_create (GThreadFunc thread_func,
3051 GThreadPriority priority);
3052 GThread* g_thread_self ();
3053 void g_thread_join (GThread* thread);
3054 void g_thread_set_priority (GThread* thread,
3055 GThreadPriority priority);
3057 /* GStaticMutexes can be statically initialized with the value
3058 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
3059 * much easier, than having to explicitly allocate the mutex before
3062 #define g_static_mutex_lock(mutex) \
3063 g_mutex_lock (g_static_mutex_get_mutex (mutex))
3064 #define g_static_mutex_trylock(mutex) \
3065 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
3066 #define g_static_mutex_unlock(mutex) \
3067 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
3069 struct _GStaticPrivate
3073 #define G_STATIC_PRIVATE_INIT { 0 }
3074 gpointer g_static_private_get (GStaticPrivate *private_key);
3075 void g_static_private_set (GStaticPrivate *private_key,
3077 GDestroyNotify notify);
3078 gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
3080 void g_static_private_set_for_thread (GStaticPrivate *private_key,
3083 GDestroyNotify notify);
3085 typedef struct _GStaticRecMutex GStaticRecMutex;
3086 struct _GStaticRecMutex
3090 GSystemThread owner;
3093 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT }
3094 void g_static_rec_mutex_lock (GStaticRecMutex *mutex);
3095 gboolean g_static_rec_mutex_trylock (GStaticRecMutex *mutex);
3096 void g_static_rec_mutex_unlock (GStaticRecMutex *mutex);
3097 void g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
3099 guint g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
3101 typedef struct _GStaticRWLock GStaticRWLock;
3102 struct _GStaticRWLock
3109 guint want_to_write;
3112 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
3114 void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
3115 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
3116 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
3117 void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
3118 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
3119 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
3120 void g_static_rw_lock_free (GStaticRWLock* lock);
3122 /* these are some convenience macros that expand to nothing if GLib
3123 * was configured with --disable-threads. for using StaticMutexes,
3124 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
3125 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
3126 * declare such an globally defined lock. name is a unique identifier
3127 * for the protected varibale or code portion. locking, testing and
3128 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
3129 * G_TRYLOCK() respectively.
3131 extern void glib_dummy_decl (void);
3132 #define G_LOCK_NAME(name) g__ ## name ## _lock
3133 #ifdef G_THREADS_ENABLED
3134 # define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
3135 # define G_LOCK_DEFINE(name) \
3136 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
3137 # define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
3139 # ifdef G_DEBUG_LOCKS
3140 # define G_LOCK(name) G_STMT_START{ \
3141 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3142 "file %s: line %d (%s): locking: %s ", \
3143 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3145 g_static_mutex_lock (&G_LOCK_NAME (name)); \
3147 # define G_UNLOCK(name) G_STMT_START{ \
3148 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3149 "file %s: line %d (%s): unlocking: %s ", \
3150 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3152 g_static_mutex_unlock (&G_LOCK_NAME (name)); \
3154 # define G_TRYLOCK(name) \
3155 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3156 "file %s: line %d (%s): try locking: %s ", \
3157 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3158 #name), g_static_mutex_trylock (&G_LOCK_NAME (name)))
3159 # else /* !G_DEBUG_LOCKS */
3160 # define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
3161 # define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
3162 # define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
3163 # endif /* !G_DEBUG_LOCKS */
3164 #else /* !G_THREADS_ENABLED */
3165 # define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
3166 # define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
3167 # define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
3168 # define G_LOCK(name)
3169 # define G_UNLOCK(name)
3170 # define G_TRYLOCK(name) (TRUE)
3171 #endif /* !G_THREADS_ENABLED */
3173 /* Asyncronous Queues, can be used to communicate between threads
3176 /* Get a new GAsyncQueue with the ref_count 1 */
3177 GAsyncQueue* g_async_queue_new (void);
3179 /* Lock and unlock an GAsyncQueue, all functions lock the queue for
3180 * themselves, but in certain cirumstances you want to hold the lock longer,
3181 * thus you lock the queue, call the *_unlocked functions and unlock it again
3183 void g_async_queue_lock (GAsyncQueue *queue);
3184 void g_async_queue_unlock (GAsyncQueue *queue);
3186 /* Ref and unref the GAsyncQueue. g_async_queue_unref_unlocked makes
3187 * no sense, as after the unreffing the Queue might be gone and can't
3188 * be unlocked. So you have a function to call, if you don't hold the
3189 * lock (g_async_queue_unref) and one to call, when you already hold
3190 * the lock (g_async_queue_unref_and_unlock). After that however, you
3191 * don't hold the lock anymore and the Queue might in fact be
3192 * destroyed, if you unrefed to zero */
3193 void g_async_queue_ref (GAsyncQueue *queue);
3194 void g_async_queue_ref_unlocked (GAsyncQueue *queue);
3195 void g_async_queue_unref (GAsyncQueue *queue);
3196 void g_async_queue_unref_and_unlock (GAsyncQueue *queue);
3198 /* Push data into the async queue. Must not be NULL */
3199 void g_async_queue_push (GAsyncQueue *queue,
3201 void g_async_queue_push_unlocked (GAsyncQueue *queue,
3204 /* Pop data from the async queue, when no data is there, the thread is blocked
3205 * until data arrives */
3206 gpointer g_async_queue_pop (GAsyncQueue *queue);
3207 gpointer g_async_queue_pop_unlocked (GAsyncQueue *queue);
3209 /* Try to pop data, NULL is returned in case of empty queue */
3210 gpointer g_async_queue_try_pop (GAsyncQueue *queue);
3211 gpointer g_async_queue_try_pop_unlocked (GAsyncQueue *queue);
3213 /* Wait for data until at maximum until end_time is reached, NULL is returned
3214 * in case of empty queue*/
3215 gpointer g_async_queue_timed_pop (GAsyncQueue *queue,
3216 GTimeVal *end_time);
3217 gpointer g_async_queue_timed_pop_unlocked (GAsyncQueue *queue,
3218 GTimeVal *end_time);
3220 /* Return the length of the queue, negative values mean, that threads
3221 * are waiting, positve values mean, that there are entries in the
3222 * queue. Actually this function returns the length of the queue minus
3223 * the number of waiting threads, g_async_queue_length == 0 could also
3224 * mean 'n' entries in the queue and 'n' thread waiting, such can
3225 * happen due to locking of the queue or due to scheduling. */
3226 gint g_async_queue_length (GAsyncQueue *queue);
3227 gint g_async_queue_length_unlocked (GAsyncQueue *queue);
3232 /* The real GThreadPool is bigger, so you may only create a thread
3233 * pool with the constructor function */
3239 GThreadPriority priority;
3244 /* Get a thread pool with the function thread_func, at most max_threads may
3245 * run at a time (max_threads == -1 means no limit), stack_size, bound,
3246 * priority like in g_thread_create, exclusive == TRUE means, that the threads
3247 * shouldn't be shared and that they will be prestarted (otherwise they are
3248 * started, as needed) user_data is the 2nd argument to the thread_func */
3249 GThreadPool* g_thread_pool_new (GFunc thread_func,
3253 GThreadPriority priority,
3255 gpointer user_data);
3257 /* Push new data into the thread pool. This task is assigned to a thread later
3258 * (when the maximal number of threads is reached for that pool) or now
3259 * (otherwise). If necessary a new thread will be started. The function
3260 * returns immediatly */
3261 void g_thread_pool_push (GThreadPool *pool,
3264 /* Set the number of threads, which can run concurrently for that pool, -1
3265 * means no limit. 0 means has the effect, that the pool won't process
3266 * requests until the limit is set higher again */
3267 void g_thread_pool_set_max_threads (GThreadPool *pool,
3269 gint g_thread_pool_get_max_threads (GThreadPool *pool);
3271 /* Get the number of threads assigned to that pool. This number doesn't
3272 * necessarily represent the number of working threads in that pool */
3273 guint g_thread_pool_get_num_threads (GThreadPool *pool);
3275 /* Get the number of unprocessed items in the pool */
3276 guint g_thread_pool_unprocessed (GThreadPool *pool);
3278 /* Free the pool, immediate means, that all unprocessed items in the queue
3279 * wont be processed, wait means, that the function doesn't return immediatly,
3280 * but after all threads in the pool are ready processing items. immediate
3281 * does however not mean, that threads are killed. */
3282 void g_thread_pool_free (GThreadPool *pool,
3286 /* Set the maximal number of unused threads before threads will be stopped by
3287 * GLib, -1 means no limit */
3288 void g_thread_pool_set_max_unused_threads (gint max_threads);
3289 gint g_thread_pool_get_max_unused_threads (void);
3290 guint g_thread_pool_get_num_unused_threads (void);
3292 /* Stop all currently unused threads, but leave the limit untouched */
3293 void g_thread_pool_stop_unused_threads (void);
3297 #endif /* __cplusplus */
3300 #endif /* __G_LIB_H__ */