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 /* provide a string identifying the current code position */
152 # define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
154 # define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__)
158 /* Count the number of elements in an array. The array must be defined
159 * as such; using this with a dynamically allocated array will give
162 #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
164 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
165 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
167 #if !defined (G_VA_COPY)
168 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
169 # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
170 # elif defined (G_VA_COPY_AS_ARRAY)
171 # define G_VA_COPY(ap1, ap2) g_memmove ((ap1), (ap2), sizeof (va_list))
172 # else /* va_list is a pointer */
173 # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
174 # endif /* va_list is a pointer */
175 #endif /* !G_VA_COPY */
178 /* Provide convenience macros for handling structure
179 * fields through their offsets.
181 #define G_STRUCT_OFFSET(struct_type, member) \
182 ((glong) ((guint8*) &((struct_type*) 0)->member))
183 #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
184 ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
185 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
186 (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
189 /* inlining hassle. for compilers that don't allow the `inline' keyword,
190 * mostly because of strict ANSI C compliance or dumbness, we try to fall
191 * back to either `__inline__' or `__inline'.
192 * we define G_CAN_INLINE, if the compiler seems to be actually
193 * *capable* to do function inlining, in which case inline function bodys
194 * do make sense. we also define G_INLINE_FUNC to properly export the
195 * function prototypes if no inlining can be performed.
196 * we special case most of the stuff, so inline functions can have a normal
197 * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
199 #ifndef G_INLINE_FUNC
200 # define G_CAN_INLINE 1
203 # if defined (__GNUC__) && defined (__STRICT_ANSI__)
205 # define inline __inline__
207 #else /* !G_HAVE_INLINE */
209 # if defined (G_HAVE___INLINE__)
210 # define inline __inline__
211 # else /* !inline && !__inline__ */
212 # if defined (G_HAVE___INLINE)
213 # define inline __inline
214 # else /* !inline && !__inline__ && !__inline */
215 # define inline /* don't inline, then */
216 # ifndef G_INLINE_FUNC
222 #ifndef G_INLINE_FUNC
225 # define G_INLINE_FUNC extern inline
228 # define G_INLINE_FUNC extern
230 # else /* !__GNUC__ */
232 # define G_INLINE_FUNC static inline
234 # define G_INLINE_FUNC extern
236 # endif /* !__GNUC__ */
237 #endif /* !G_INLINE_FUNC */
240 /* Provide simple macro statement wrappers (adapted from Perl):
241 * G_STMT_START { statements; } G_STMT_END;
242 * can be used as a single statement, as in
243 * if (x) G_STMT_START { ... } G_STMT_END; else ...
245 * For gcc we will wrap the statements within `({' and `})' braces.
246 * For SunOS they will be wrapped within `if (1)' and `else (void) 0',
247 * and otherwise within `do' and `while (0)'.
249 #if !(defined (G_STMT_START) && defined (G_STMT_END))
250 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
251 # define G_STMT_START (void)(
252 # define G_STMT_END )
254 # if (defined (sun) || defined (__sun__))
255 # define G_STMT_START if (1)
256 # define G_STMT_END else (void)0
258 # define G_STMT_START do
259 # define G_STMT_END while (0)
265 /* Provide macros to feature the GCC function attribute.
267 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
268 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
269 __attribute__((format (printf, format_idx, arg_idx)))
270 #define G_GNUC_SCANF( format_idx, arg_idx ) \
271 __attribute__((format (scanf, format_idx, arg_idx)))
272 #define G_GNUC_FORMAT( arg_idx ) \
273 __attribute__((format_arg (arg_idx)))
274 #define G_GNUC_NORETURN \
275 __attribute__((noreturn))
276 #define G_GNUC_CONST \
277 __attribute__((const))
278 #define G_GNUC_UNUSED \
279 __attribute__((unused))
280 #else /* !__GNUC__ */
281 #define G_GNUC_PRINTF( format_idx, arg_idx )
282 #define G_GNUC_SCANF( format_idx, arg_idx )
283 #define G_GNUC_FORMAT( arg_idx )
284 #define G_GNUC_NORETURN
286 #define G_GNUC_UNUSED
287 #endif /* !__GNUC__ */
289 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
290 * macros, so we can refer to them as strings unconditionally.
293 #define G_GNUC_FUNCTION __FUNCTION__
294 #define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
295 #else /* !__GNUC__ */
296 #define G_GNUC_FUNCTION ""
297 #define G_GNUC_PRETTY_FUNCTION ""
298 #endif /* !__GNUC__ */
300 /* we try to provide a usefull equivalent for ATEXIT if it is
301 * not defined, but use is actually abandoned. people should
302 * use g_atexit() instead.
305 # define ATEXIT(proc) g_ATEXIT(proc)
307 # define G_NATIVE_ATEXIT
310 /* Hacker macro to place breakpoints for elected machines.
311 * Actual use is strongly deprecated of course ;)
313 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
314 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
315 #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
316 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
317 #else /* !__i386__ && !__alpha__ */
318 #define G_BREAKPOINT()
319 #endif /* __i386__ */
322 /* Provide macros for easily allocating memory. The macros
323 * will cast the allocated memory to the specified type
324 * in order to avoid compiler warnings. (Makes the code neater).
328 # define g_new(type, count) (ALLOC (type, count))
329 # define g_new0(type, count) (CALLOC (type, count))
330 # define g_renew(type, mem, count) (REALLOC (mem, type, count))
331 #else /* __DMALLOC_H__ */
332 # define g_new(type, count) \
333 ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
334 # define g_new0(type, count) \
335 ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
336 # define g_renew(type, mem, count) \
337 ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
338 #endif /* __DMALLOC_H__ */
340 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
341 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
343 sizeof (type) * (pre_alloc), \
346 #define g_chunk_new(type, chunk) ( \
347 (type *) g_mem_chunk_alloc (chunk) \
349 #define g_chunk_new0(type, chunk) ( \
350 (type *) g_mem_chunk_alloc0 (chunk) \
352 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
353 g_mem_chunk_free ((mem_chunk), (mem)); \
357 /* Provide macros for error handling. The "assert" macros will
358 * exit on failure. The "return" macros will exit the current
359 * function. Two different definitions are given for the macros
360 * if G_DISABLE_ASSERT is not defined, in order to support gcc's
361 * __PRETTY_FUNCTION__ capability.
364 #ifdef G_DISABLE_ASSERT
366 #define g_assert(expr)
367 #define g_assert_not_reached()
369 #else /* !G_DISABLE_ASSERT */
373 #define g_assert(expr) G_STMT_START{ \
375 g_log (G_LOG_DOMAIN, \
377 "file %s: line %d (%s): assertion failed: (%s)", \
380 __PRETTY_FUNCTION__, \
383 #define g_assert_not_reached() G_STMT_START{ \
384 g_log (G_LOG_DOMAIN, \
386 "file %s: line %d (%s): should not be reached", \
389 __PRETTY_FUNCTION__); }G_STMT_END
391 #else /* !__GNUC__ */
393 #define g_assert(expr) G_STMT_START{ \
395 g_log (G_LOG_DOMAIN, \
397 "file %s: line %d: assertion failed: (%s)", \
402 #define g_assert_not_reached() G_STMT_START{ \
403 g_log (G_LOG_DOMAIN, \
405 "file %s: line %d: should not be reached", \
407 __LINE__); }G_STMT_END
409 #endif /* __GNUC__ */
411 #endif /* !G_DISABLE_ASSERT */
414 #ifdef G_DISABLE_CHECKS
416 #define g_return_if_fail(expr)
417 #define g_return_val_if_fail(expr,val)
419 #else /* !G_DISABLE_CHECKS */
423 #define g_return_if_fail(expr) G_STMT_START{ \
426 g_log (G_LOG_DOMAIN, \
427 G_LOG_LEVEL_CRITICAL, \
428 "file %s: line %d (%s): assertion `%s' failed.", \
431 __PRETTY_FUNCTION__, \
436 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
439 g_log (G_LOG_DOMAIN, \
440 G_LOG_LEVEL_CRITICAL, \
441 "file %s: line %d (%s): assertion `%s' failed.", \
444 __PRETTY_FUNCTION__, \
449 #else /* !__GNUC__ */
451 #define g_return_if_fail(expr) G_STMT_START{ \
454 g_log (G_LOG_DOMAIN, \
455 G_LOG_LEVEL_CRITICAL, \
456 "file %s: line %d: assertion `%s' failed.", \
463 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
466 g_log (G_LOG_DOMAIN, \
467 G_LOG_LEVEL_CRITICAL, \
468 "file %s: line %d: assertion `%s' failed.", \
475 #endif /* !__GNUC__ */
477 #endif /* !G_DISABLE_CHECKS */
480 /* Provide type definitions for commonly used types.
481 * These are useful because a "gint8" can be adjusted
482 * to be 1 byte (8 bits) on all platforms. Similarly and
483 * more importantly, "gint32" can be adjusted to be
484 * 4 bytes (32 bits) on all platforms.
488 typedef short gshort;
491 typedef gint gboolean;
493 typedef unsigned char guchar;
494 typedef unsigned short gushort;
495 typedef unsigned long gulong;
496 typedef unsigned int guint;
498 #define G_GSHORT_FORMAT "hi"
499 #define G_GUSHORT_FORMAT "hu"
500 #define G_GINT_FORMAT "i"
501 #define G_GUINT_FORMAT "u"
502 #define G_GLONG_FORMAT "li"
503 #define G_GULONG_FORMAT "lu"
505 typedef float gfloat;
506 typedef double gdouble;
508 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
509 * Since gldouble isn't used anywhere, just disable it for now */
512 #ifdef HAVE_LONG_DOUBLE
513 typedef long double gldouble;
514 #else /* HAVE_LONG_DOUBLE */
515 typedef double gldouble;
516 #endif /* HAVE_LONG_DOUBLE */
519 typedef void* gpointer;
520 typedef const void *gconstpointer;
523 typedef gint32 gssize;
524 typedef guint32 gsize;
525 typedef guint32 GQuark;
526 typedef gint32 GTime;
529 /* Portable endian checks and conversions
531 * glibconfig.h defines G_BYTE_ORDER which expands to one of
534 #define G_LITTLE_ENDIAN 1234
535 #define G_BIG_ENDIAN 4321
536 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
539 /* Basic bit swapping functions
541 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
542 (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
543 (((guint16) (val) & (guint16) 0xff00U) >> 8)))
544 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((guint32) ( \
545 (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
546 (((guint32) (val) & (guint32) 0x0000ff00U) << 8) | \
547 (((guint32) (val) & (guint32) 0x00ff0000U) >> 8) | \
548 (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
550 /* Intel specific stuff for speed
552 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
553 # define GUINT16_SWAP_LE_BE_X86(val) \
555 ({ register guint16 __v; \
556 if (__builtin_constant_p (val)) \
557 __v = GUINT16_SWAP_LE_BE_CONSTANT (val); \
559 __asm__ __const__ ("rorw $8, %w0" \
561 : "0" ((guint16) (val))); \
563 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
564 # if !defined(__i486__) && !defined(__i586__) \
565 && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
566 # define GUINT32_SWAP_LE_BE_X86(val) \
568 ({ register guint32 __v; \
569 if (__builtin_constant_p (val)) \
570 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
572 __asm__ __const__ ("rorw $8, %w0\n\t" \
576 : "0" ((guint32) (val))); \
578 # else /* 486 and higher has bswap */
579 # define GUINT32_SWAP_LE_BE_X86(val) \
581 ({ register guint32 __v; \
582 if (__builtin_constant_p (val)) \
583 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
585 __asm__ __const__ ("bswap %0" \
587 : "0" ((guint32) (val))); \
589 # endif /* processor specific 32-bit stuff */
590 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
591 #else /* !__i386__ */
592 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
593 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
594 #endif /* __i386__ */
597 # define GUINT64_SWAP_LE_BE_CONSTANT(val) ((guint64) ( \
598 (((guint64) (val) & \
599 (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) | \
600 (((guint64) (val) & \
601 (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) | \
602 (((guint64) (val) & \
603 (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) | \
604 (((guint64) (val) & \
605 (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) << 8) | \
606 (((guint64) (val) & \
607 (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >> 8) | \
608 (((guint64) (val) & \
609 (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) | \
610 (((guint64) (val) & \
611 (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) | \
612 (((guint64) (val) & \
613 (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
614 # if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
615 # define GUINT64_SWAP_LE_BE_X86(val) \
617 ({ union { guint64 __ll; \
618 guint32 __l[2]; } __r; \
619 if (__builtin_constant_p (val)) \
620 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val); \
623 union { guint64 __ll; \
624 guint32 __l[2]; } __w; \
625 __w.__ll = ((guint64) val); \
626 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
627 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
630 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
631 # else /* !__i386__ */
632 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
636 #define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
637 #define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
638 #define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
639 (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
640 (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
641 #define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
642 (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
643 (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
645 /* The G*_TO_?E() macros are defined in glibconfig.h.
646 * The transformation is symmetric, so the FROM just maps to the TO.
648 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
649 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
650 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
651 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
652 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
653 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
654 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
655 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
658 #define GINT64_FROM_LE(val) (GINT64_TO_LE (val))
659 #define GUINT64_FROM_LE(val) (GUINT64_TO_LE (val))
660 #define GINT64_FROM_BE(val) (GINT64_TO_BE (val))
661 #define GUINT64_FROM_BE(val) (GUINT64_TO_BE (val))
664 #define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
665 #define GULONG_FROM_LE(val) (GULONG_TO_LE (val))
666 #define GLONG_FROM_BE(val) (GLONG_TO_BE (val))
667 #define GULONG_FROM_BE(val) (GULONG_TO_BE (val))
669 #define GINT_FROM_LE(val) (GINT_TO_LE (val))
670 #define GUINT_FROM_LE(val) (GUINT_TO_LE (val))
671 #define GINT_FROM_BE(val) (GINT_TO_BE (val))
672 #define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
675 /* Portable versions of host-network order stuff
677 #define g_ntohl(val) (GUINT32_FROM_BE (val))
678 #define g_ntohs(val) (GUINT16_FROM_BE (val))
679 #define g_htonl(val) (GUINT32_TO_BE (val))
680 #define g_htons(val) (GUINT16_TO_BE (val))
684 * we prefix variable declarations so they can
685 * properly get exported in windows dlls.
688 # ifdef GLIB_COMPILATION
689 # define GUTILS_C_VAR __declspec(dllexport)
690 # else /* !GLIB_COMPILATION */
691 # define GUTILS_C_VAR extern __declspec(dllimport)
692 # endif /* !GLIB_COMPILATION */
693 #else /* !G_OS_WIN32 */
694 # define GUTILS_C_VAR extern
695 #endif /* !G_OS_WIN32 */
697 GUTILS_C_VAR const guint glib_major_version;
698 GUTILS_C_VAR const guint glib_minor_version;
699 GUTILS_C_VAR const guint glib_micro_version;
700 GUTILS_C_VAR const guint glib_interface_age;
701 GUTILS_C_VAR const guint glib_binary_age;
703 #define GLIB_CHECK_VERSION(major,minor,micro) \
704 (GLIB_MAJOR_VERSION > (major) || \
705 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
706 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
707 GLIB_MICRO_VERSION >= (micro)))
709 /* Forward declarations of glib types.
711 typedef struct _GAllocator GAllocator;
712 typedef struct _GArray GArray;
713 typedef struct _GByteArray GByteArray;
714 typedef struct _GCache GCache;
715 typedef struct _GCompletion GCompletion;
716 typedef struct _GData GData;
717 typedef struct _GDebugKey GDebugKey;
718 typedef union _GDoubleIEEE754 GDoubleIEEE754;
719 typedef union _GFloatIEEE754 GFloatIEEE754;
720 typedef struct _GHashTable GHashTable;
721 typedef struct _GHook GHook;
722 typedef struct _GHookList GHookList;
723 typedef struct _GList GList;
724 typedef struct _GMemChunk GMemChunk;
725 typedef struct _GNode GNode;
726 typedef struct _GPtrArray GPtrArray;
727 typedef struct _GQueue GQueue;
728 typedef struct _GRand GRand;
729 typedef struct _GRelation GRelation;
730 typedef struct _GScanner GScanner;
731 typedef struct _GScannerConfig GScannerConfig;
732 typedef struct _GSList GSList;
733 typedef struct _GString GString;
734 typedef struct _GStringChunk GStringChunk;
735 typedef struct _GTimer GTimer;
736 typedef struct _GTrashStack GTrashStack;
737 typedef struct _GTree GTree;
738 typedef struct _GTuples GTuples;
739 typedef union _GTokenValue GTokenValue;
740 typedef struct _GIOChannel GIOChannel;
742 /* Tree traverse flags */
745 G_TRAVERSE_LEAFS = 1 << 0,
746 G_TRAVERSE_NON_LEAFS = 1 << 1,
747 G_TRAVERSE_ALL = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
748 G_TRAVERSE_MASK = 0x03
751 /* Tree traverse orders */
760 /* Log level shift offset for user defined
761 * log levels (0-7 are used by GLib).
763 #define G_LOG_LEVEL_USER_SHIFT (8)
765 /* Glib log levels and flags.
770 G_LOG_FLAG_RECURSION = 1 << 0,
771 G_LOG_FLAG_FATAL = 1 << 1,
773 /* GLib log levels */
774 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
775 G_LOG_LEVEL_CRITICAL = 1 << 3,
776 G_LOG_LEVEL_WARNING = 1 << 4,
777 G_LOG_LEVEL_MESSAGE = 1 << 5,
778 G_LOG_LEVEL_INFO = 1 << 6,
779 G_LOG_LEVEL_DEBUG = 1 << 7,
781 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
784 /* GLib log levels that are considered fatal by default */
785 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
788 typedef gpointer (*GCacheNewFunc) (gpointer key);
789 typedef gpointer (*GCacheDupFunc) (gpointer value);
790 typedef void (*GCacheDestroyFunc) (gpointer value);
791 typedef gint (*GCompareFunc) (gconstpointer a,
793 typedef gchar* (*GCompletionFunc) (gpointer);
794 typedef void (*GDestroyNotify) (gpointer data);
795 typedef void (*GDataForeachFunc) (GQuark key_id,
798 typedef void (*GFunc) (gpointer data,
800 typedef guint (*GHashFunc) (gconstpointer key);
801 typedef void (*GFreeFunc) (gpointer data);
802 typedef void (*GHFunc) (gpointer key,
805 typedef gboolean (*GHRFunc) (gpointer key,
808 typedef gint (*GHookCompareFunc) (GHook *new_hook,
810 typedef gboolean (*GHookFindFunc) (GHook *hook,
812 typedef void (*GHookMarshaller) (GHook *hook,
814 typedef gboolean (*GHookCheckMarshaller) (GHook *hook,
816 typedef void (*GHookFunc) (gpointer data);
817 typedef gboolean (*GHookCheckFunc) (gpointer data);
818 typedef void (*GHookFreeFunc) (GHookList *hook_list,
820 typedef void (*GLogFunc) (const gchar *log_domain,
821 GLogLevelFlags log_level,
822 const gchar *message,
824 typedef gboolean (*GNodeTraverseFunc) (GNode *node,
826 typedef void (*GNodeForeachFunc) (GNode *node,
828 typedef void (*GScannerMsgFunc) (GScanner *scanner,
831 typedef gint (*GTraverseFunc) (gpointer key,
834 typedef void (*GVoidFunc) (void);
898 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
901 * +--------+---------------+---------------+
902 * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
903 * +--------+---------------+---------------+
904 * B0------------------->B1------->B2-->B3-->
906 * IEEE Standard 754 Double Precision Storage Format (gdouble):
908 * 63 62 52 51 32 31 0
909 * +--------+----------------+----------------+ +---------------+
910 * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
911 * +--------+----------------+----------------+ +---------------+
912 * B0--------------->B1---------->B2--->B3----> B4->B5->B6->B7->
914 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
915 #define G_IEEE754_FLOAT_BIAS (127)
916 #define G_IEEE754_DOUBLE_BIAS (1023)
917 /* multiply with base2 exponent to get base10 exponent (nomal numbers) */
918 #define G_LOG_2_BASE_10 (0.30102999566398119521)
919 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
925 guint biased_exponent : 8;
929 union _GDoubleIEEE754
933 guint mantissa_low : 32;
934 guint mantissa_high : 20;
935 guint biased_exponent : 11;
939 #elif G_BYTE_ORDER == G_BIG_ENDIAN
945 guint biased_exponent : 8;
949 union _GDoubleIEEE754
954 guint biased_exponent : 11;
955 guint mantissa_high : 20;
956 guint mantissa_low : 32;
959 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
960 #error unknown ENDIAN type
961 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
964 /* Doubly linked lists
966 void g_list_push_allocator (GAllocator *allocator);
967 void g_list_pop_allocator (void);
968 GList* g_list_alloc (void);
969 void g_list_free (GList *list);
970 void g_list_free_1 (GList *list);
971 GList* g_list_append (GList *list,
973 GList* g_list_prepend (GList *list,
975 GList* g_list_insert (GList *list,
978 GList* g_list_insert_sorted (GList *list,
981 GList* g_list_concat (GList *list1,
983 GList* g_list_remove (GList *list,
985 GList* g_list_remove_link (GList *list,
987 GList* g_list_delete_link (GList *list,
989 GList* g_list_reverse (GList *list);
990 GList* g_list_copy (GList *list);
991 GList* g_list_nth (GList *list,
993 GList* g_list_find (GList *list,
995 GList* g_list_find_custom (GList *list,
998 gint g_list_position (GList *list,
1000 gint g_list_index (GList *list,
1001 gconstpointer data);
1002 GList* g_list_last (GList *list);
1003 GList* g_list_first (GList *list);
1004 guint g_list_length (GList *list);
1005 void g_list_foreach (GList *list,
1007 gpointer user_data);
1008 GList* g_list_sort (GList *list,
1009 GCompareFunc compare_func);
1010 gpointer g_list_nth_data (GList *list,
1012 #define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
1013 #define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
1016 /* Singly linked lists
1018 void g_slist_push_allocator (GAllocator *allocator);
1019 void g_slist_pop_allocator (void);
1020 GSList* g_slist_alloc (void);
1021 void g_slist_free (GSList *list);
1022 void g_slist_free_1 (GSList *list);
1023 GSList* g_slist_append (GSList *list,
1025 GSList* g_slist_prepend (GSList *list,
1027 GSList* g_slist_insert (GSList *list,
1030 GSList* g_slist_insert_sorted (GSList *list,
1033 GSList* g_slist_insert_before (GSList *slist,
1036 GSList* g_slist_concat (GSList *list1,
1038 GSList* g_slist_remove (GSList *list,
1039 gconstpointer data);
1040 GSList* g_slist_remove_link (GSList *list,
1042 GSList* g_slist_delete_link (GSList *list,
1044 GSList* g_slist_reverse (GSList *list);
1045 GSList* g_slist_copy (GSList *list);
1046 GSList* g_slist_nth (GSList *list,
1048 GSList* g_slist_find (GSList *list,
1049 gconstpointer data);
1050 GSList* g_slist_find_custom (GSList *list,
1053 gint g_slist_position (GSList *list,
1055 gint g_slist_index (GSList *list,
1056 gconstpointer data);
1057 GSList* g_slist_last (GSList *list);
1058 guint g_slist_length (GSList *list);
1059 void g_slist_foreach (GSList *list,
1061 gpointer user_data);
1062 GSList* g_slist_sort (GSList *list,
1063 GCompareFunc compare_func);
1064 gpointer g_slist_nth_data (GSList *list,
1066 #define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
1071 GQueue* g_queue_new (void);
1072 void g_queue_free (GQueue *queue);
1073 void g_queue_push_head (GQueue *queue,
1075 void g_queue_push_tail (GQueue *queue,
1077 gpointer g_queue_pop_head (GQueue *queue);
1078 gpointer g_queue_pop_tail (GQueue *queue);
1079 gboolean g_queue_is_empty (GQueue *queue);
1080 gpointer g_queue_peek_head (GQueue *queue);
1081 gpointer g_queue_peek_tail (GQueue *queue);
1082 void g_queue_push_head_link (GQueue *queue,
1084 void g_queue_push_tail_link (GQueue *queue,
1086 GList* g_queue_pop_head_link (GQueue *queue);
1087 GList* g_queue_pop_tail_link (GQueue *queue);
1091 GHashTable* g_hash_table_new (GHashFunc hash_func,
1092 GCompareFunc key_compare_func);
1093 void g_hash_table_destroy (GHashTable *hash_table);
1094 void g_hash_table_insert (GHashTable *hash_table,
1097 void g_hash_table_remove (GHashTable *hash_table,
1099 gpointer g_hash_table_lookup (GHashTable *hash_table,
1101 gboolean g_hash_table_lookup_extended(GHashTable *hash_table,
1102 gconstpointer lookup_key,
1105 void g_hash_table_freeze (GHashTable *hash_table);
1106 void g_hash_table_thaw (GHashTable *hash_table);
1107 void g_hash_table_foreach (GHashTable *hash_table,
1109 gpointer user_data);
1110 guint g_hash_table_foreach_remove (GHashTable *hash_table,
1112 gpointer user_data);
1113 guint g_hash_table_size (GHashTable *hash_table);
1118 GCache* g_cache_new (GCacheNewFunc value_new_func,
1119 GCacheDestroyFunc value_destroy_func,
1120 GCacheDupFunc key_dup_func,
1121 GCacheDestroyFunc key_destroy_func,
1122 GHashFunc hash_key_func,
1123 GHashFunc hash_value_func,
1124 GCompareFunc key_compare_func);
1125 void g_cache_destroy (GCache *cache);
1126 gpointer g_cache_insert (GCache *cache,
1128 void g_cache_remove (GCache *cache,
1129 gconstpointer value);
1130 void g_cache_key_foreach (GCache *cache,
1132 gpointer user_data);
1133 void g_cache_value_foreach (GCache *cache,
1135 gpointer user_data);
1138 /* Balanced binary trees
1140 GTree* g_tree_new (GCompareFunc key_compare_func);
1141 void g_tree_destroy (GTree *tree);
1142 void g_tree_insert (GTree *tree,
1145 void g_tree_remove (GTree *tree,
1147 gpointer g_tree_lookup (GTree *tree,
1149 void g_tree_traverse (GTree *tree,
1150 GTraverseFunc traverse_func,
1151 GTraverseType traverse_type,
1153 gpointer g_tree_search (GTree *tree,
1154 GCompareFunc search_func,
1155 gconstpointer data);
1156 gint g_tree_height (GTree *tree);
1157 gint g_tree_nnodes (GTree *tree);
1161 /* N-way tree implementation
1172 #define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
1173 ((GNode*) (node))->prev == NULL && \
1174 ((GNode*) (node))->next == NULL)
1175 #define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
1177 void g_node_push_allocator (GAllocator *allocator);
1178 void g_node_pop_allocator (void);
1179 GNode* g_node_new (gpointer data);
1180 void g_node_destroy (GNode *root);
1181 void g_node_unlink (GNode *node);
1182 GNode* g_node_copy (GNode *node);
1183 GNode* g_node_insert (GNode *parent,
1186 GNode* g_node_insert_before (GNode *parent,
1189 GNode* g_node_prepend (GNode *parent,
1191 guint g_node_n_nodes (GNode *root,
1192 GTraverseFlags flags);
1193 GNode* g_node_get_root (GNode *node);
1194 gboolean g_node_is_ancestor (GNode *node,
1196 guint g_node_depth (GNode *node);
1197 GNode* g_node_find (GNode *root,
1198 GTraverseType order,
1199 GTraverseFlags flags,
1202 /* convenience macros */
1203 #define g_node_append(parent, node) \
1204 g_node_insert_before ((parent), NULL, (node))
1205 #define g_node_insert_data(parent, position, data) \
1206 g_node_insert ((parent), (position), g_node_new (data))
1207 #define g_node_insert_data_before(parent, sibling, data) \
1208 g_node_insert_before ((parent), (sibling), g_node_new (data))
1209 #define g_node_prepend_data(parent, data) \
1210 g_node_prepend ((parent), g_node_new (data))
1211 #define g_node_append_data(parent, data) \
1212 g_node_insert_before ((parent), NULL, g_node_new (data))
1214 /* traversal function, assumes that `node' is root
1215 * (only traverses `node' and its subtree).
1216 * this function is just a high level interface to
1217 * low level traversal functions, optimized for speed.
1219 void g_node_traverse (GNode *root,
1220 GTraverseType order,
1221 GTraverseFlags flags,
1223 GNodeTraverseFunc func,
1226 /* return the maximum tree height starting with `node', this is an expensive
1227 * operation, since we need to visit all nodes. this could be shortened by
1228 * adding `guint height' to struct _GNode, but then again, this is not very
1229 * often needed, and would make g_node_insert() more time consuming.
1231 guint g_node_max_height (GNode *root);
1233 void g_node_children_foreach (GNode *node,
1234 GTraverseFlags flags,
1235 GNodeForeachFunc func,
1237 void g_node_reverse_children (GNode *node);
1238 guint g_node_n_children (GNode *node);
1239 GNode* g_node_nth_child (GNode *node,
1241 GNode* g_node_last_child (GNode *node);
1242 GNode* g_node_find_child (GNode *node,
1243 GTraverseFlags flags,
1245 gint g_node_child_position (GNode *node,
1247 gint g_node_child_index (GNode *node,
1250 GNode* g_node_first_sibling (GNode *node);
1251 GNode* g_node_last_sibling (GNode *node);
1253 #define g_node_prev_sibling(node) ((node) ? \
1254 ((GNode*) (node))->prev : NULL)
1255 #define g_node_next_sibling(node) ((node) ? \
1256 ((GNode*) (node))->next : NULL)
1257 #define g_node_first_child(node) ((node) ? \
1258 ((GNode*) (node))->children : NULL)
1261 /* Callback maintenance functions
1263 #define G_HOOK_FLAG_USER_SHIFT (4)
1266 G_HOOK_FLAG_ACTIVE = 1 << 0,
1267 G_HOOK_FLAG_IN_CALL = 1 << 1,
1268 G_HOOK_FLAG_MASK = 0x0f
1271 #define G_HOOK_DEFERRED_DESTROY ((GHookFreeFunc) 0x01)
1279 GMemChunk *hook_memchunk;
1280 GHookFreeFunc hook_free; /* virtual function */
1281 GHookFreeFunc hook_destroy; /* virtual function */
1293 GDestroyNotify destroy;
1296 #define G_HOOK_ACTIVE(hook) ((((GHook*) hook)->flags & \
1297 G_HOOK_FLAG_ACTIVE) != 0)
1298 #define G_HOOK_IN_CALL(hook) ((((GHook*) hook)->flags & \
1299 G_HOOK_FLAG_IN_CALL) != 0)
1300 #define G_HOOK_IS_VALID(hook) (((GHook*) hook)->hook_id != 0 && \
1301 G_HOOK_ACTIVE (hook))
1302 #define G_HOOK_IS_UNLINKED(hook) (((GHook*) hook)->next == NULL && \
1303 ((GHook*) hook)->prev == NULL && \
1304 ((GHook*) hook)->hook_id == 0 && \
1305 ((GHook*) hook)->ref_count == 0)
1307 void g_hook_list_init (GHookList *hook_list,
1309 void g_hook_list_clear (GHookList *hook_list);
1310 GHook* g_hook_alloc (GHookList *hook_list);
1311 void g_hook_free (GHookList *hook_list,
1313 void g_hook_ref (GHookList *hook_list,
1315 void g_hook_unref (GHookList *hook_list,
1317 gboolean g_hook_destroy (GHookList *hook_list,
1319 void g_hook_destroy_link (GHookList *hook_list,
1321 void g_hook_prepend (GHookList *hook_list,
1323 void g_hook_insert_before (GHookList *hook_list,
1326 void g_hook_insert_sorted (GHookList *hook_list,
1328 GHookCompareFunc func);
1329 GHook* g_hook_get (GHookList *hook_list,
1331 GHook* g_hook_find (GHookList *hook_list,
1332 gboolean need_valids,
1335 GHook* g_hook_find_data (GHookList *hook_list,
1336 gboolean need_valids,
1338 GHook* g_hook_find_func (GHookList *hook_list,
1339 gboolean need_valids,
1341 GHook* g_hook_find_func_data (GHookList *hook_list,
1342 gboolean need_valids,
1345 /* return the first valid hook, and increment its reference count */
1346 GHook* g_hook_first_valid (GHookList *hook_list,
1347 gboolean may_be_in_call);
1348 /* return the next valid hook with incremented reference count, and
1349 * decrement the reference count of the original hook
1351 GHook* g_hook_next_valid (GHookList *hook_list,
1353 gboolean may_be_in_call);
1355 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1356 gint g_hook_compare_ids (GHook *new_hook,
1359 /* convenience macros */
1360 #define g_hook_append( hook_list, hook ) \
1361 g_hook_insert_before ((hook_list), NULL, (hook))
1363 /* invoke all valid hooks with the (*GHookFunc) signature.
1365 void g_hook_list_invoke (GHookList *hook_list,
1366 gboolean may_recurse);
1367 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1368 * and destroy the hook if FALSE is returned.
1370 void g_hook_list_invoke_check (GHookList *hook_list,
1371 gboolean may_recurse);
1372 /* invoke a marshaller on all valid hooks.
1374 void g_hook_list_marshal (GHookList *hook_list,
1375 gboolean may_recurse,
1376 GHookMarshaller marshaller,
1378 void g_hook_list_marshal_check (GHookList *hook_list,
1379 gboolean may_recurse,
1380 GHookCheckMarshaller marshaller,
1384 /* Fatal error handlers.
1385 * g_on_error_query() will prompt the user to either
1386 * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1387 * g_on_error_stack_trace() invokes gdb, which attaches to the current
1388 * process and shows a stack trace.
1389 * These function may cause different actions on non-unix platforms.
1390 * The prg_name arg is required by gdb to find the executable, if it is
1391 * passed as NULL, g_on_error_query() will try g_get_prgname().
1393 void g_on_error_query (const gchar *prg_name);
1394 void g_on_error_stack_trace (const gchar *prg_name);
1397 /* Logging mechanism
1399 extern const gchar *g_log_domain_glib;
1400 guint g_log_set_handler (const gchar *log_domain,
1401 GLogLevelFlags log_levels,
1403 gpointer user_data);
1404 void g_log_remove_handler (const gchar *log_domain,
1406 void g_log_default_handler (const gchar *log_domain,
1407 GLogLevelFlags log_level,
1408 const gchar *message,
1409 gpointer unused_data);
1410 void g_log (const gchar *log_domain,
1411 GLogLevelFlags log_level,
1412 const gchar *format,
1413 ...) G_GNUC_PRINTF (3, 4);
1414 void g_logv (const gchar *log_domain,
1415 GLogLevelFlags log_level,
1416 const gchar *format,
1418 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
1419 GLogLevelFlags fatal_mask);
1420 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
1421 #ifndef G_LOG_DOMAIN
1422 #define G_LOG_DOMAIN ((gchar*) 0)
1423 #endif /* G_LOG_DOMAIN */
1425 #define g_error(format, args...) g_log (G_LOG_DOMAIN, \
1426 G_LOG_LEVEL_ERROR, \
1428 #define g_message(format, args...) g_log (G_LOG_DOMAIN, \
1429 G_LOG_LEVEL_MESSAGE, \
1431 #define g_warning(format, args...) g_log (G_LOG_DOMAIN, \
1432 G_LOG_LEVEL_WARNING, \
1434 #else /* !__GNUC__ */
1436 g_error (const gchar *format,
1440 va_start (args, format);
1441 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1445 g_message (const gchar *format,
1449 va_start (args, format);
1450 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1454 g_warning (const gchar *format,
1458 va_start (args, format);
1459 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1462 #endif /* !__GNUC__ */
1464 typedef void (*GPrintFunc) (const gchar *string);
1465 void g_print (const gchar *format,
1466 ...) G_GNUC_PRINTF (1, 2);
1467 GPrintFunc g_set_print_handler (GPrintFunc func);
1468 void g_printerr (const gchar *format,
1469 ...) G_GNUC_PRINTF (1, 2);
1470 GPrintFunc g_set_printerr_handler (GPrintFunc func);
1472 /* deprecated compatibility functions, use g_log_set_handler() instead */
1473 typedef void (*GErrorFunc) (const gchar *str);
1474 typedef void (*GWarningFunc) (const gchar *str);
1475 GErrorFunc g_set_error_handler (GErrorFunc func);
1476 GWarningFunc g_set_warning_handler (GWarningFunc func);
1477 GPrintFunc g_set_message_handler (GPrintFunc func);
1480 /* Memory allocation and debugging
1484 #define g_malloc(size) ((gpointer) MALLOC (size))
1485 #define g_malloc0(size) ((gpointer) CALLOC (char, size))
1486 #define g_realloc(mem,size) ((gpointer) REALLOC (mem, char, size))
1487 #define g_free(mem) FREE (mem)
1489 #else /* !USE_DMALLOC */
1491 gpointer g_malloc (gulong size);
1492 gpointer g_malloc0 (gulong size);
1493 gpointer g_realloc (gpointer mem,
1495 void g_free (gpointer mem);
1497 #endif /* !USE_DMALLOC */
1499 void g_mem_profile (void);
1500 void g_mem_check (gpointer mem);
1502 /* Generic allocators
1504 GAllocator* g_allocator_new (const gchar *name,
1506 void g_allocator_free (GAllocator *allocator);
1508 #define G_ALLOCATOR_LIST (1)
1509 #define G_ALLOCATOR_SLIST (2)
1510 #define G_ALLOCATOR_NODE (3)
1513 /* "g_mem_chunk_new" creates a new memory chunk.
1514 * Memory chunks are used to allocate pieces of memory which are
1515 * always the same size. Lists are a good example of such a data type.
1516 * The memory chunk allocates and frees blocks of memory as needed.
1517 * Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1518 * allocated in a mem chunk. ("g_free" will most likely cause a seg
1519 * fault...somewhere).
1521 * Oh yeah, GMemChunk is an opaque data type. (You don't really
1522 * want to know what's going on inside do you?)
1525 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1526 * is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1527 * atom. (They are also useful for lists which use MemChunk to allocate
1528 * memory but are also part of the MemChunk implementation).
1529 * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1532 #define G_ALLOC_ONLY 1
1533 #define G_ALLOC_AND_FREE 2
1535 GMemChunk* g_mem_chunk_new (gchar *name,
1539 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
1540 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
1541 gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
1542 void g_mem_chunk_free (GMemChunk *mem_chunk,
1544 void g_mem_chunk_clean (GMemChunk *mem_chunk);
1545 void g_mem_chunk_reset (GMemChunk *mem_chunk);
1546 void g_mem_chunk_print (GMemChunk *mem_chunk);
1547 void g_mem_chunk_info (void);
1549 /* Ah yes...we have a "g_blow_chunks" function.
1550 * "g_blow_chunks" simply compresses all the chunks. This operation
1551 * consists of freeing every memory area that should be freed (but
1552 * which we haven't gotten around to doing yet). And, no,
1553 * "g_blow_chunks" doesn't follow the naming scheme, but it is a
1554 * much better name than "g_mem_chunk_clean_all" or something
1557 void g_blow_chunks (void);
1563 #define G_MICROSEC 1000000
1565 GTimer* g_timer_new (void);
1566 void g_timer_destroy (GTimer *timer);
1567 void g_timer_start (GTimer *timer);
1568 void g_timer_stop (GTimer *timer);
1569 void g_timer_reset (GTimer *timer);
1570 gdouble g_timer_elapsed (GTimer *timer,
1571 gulong *microseconds);
1572 void g_usleep (gulong microseconds);
1574 /* String utility functions that modify a string argument or
1575 * return a constant string that must not be freed.
1577 #define G_STR_DELIMITERS "_-|> <."
1578 gchar* g_strdelimit (gchar *string,
1579 const gchar *delimiters,
1580 gchar new_delimiter);
1581 gchar* g_strcanon (gchar *string,
1582 const gchar *valid_chars,
1584 gdouble g_strtod (const gchar *nptr,
1586 gchar* g_strerror (gint errnum);
1587 gchar* g_strsignal (gint signum);
1588 gint g_strcasecmp (const gchar *s1,
1590 gint g_strncasecmp (const gchar *s1,
1593 gchar* g_strdown (gchar *string);
1594 gchar* g_strup (gchar *string);
1595 gchar* g_strreverse (gchar *string);
1596 /* removes leading spaces */
1597 gchar* g_strchug (gchar *string);
1598 /* removes trailing spaces */
1599 gchar* g_strchomp (gchar *string);
1600 /* removes leading & trailing spaces */
1601 #define g_strstrip( string ) g_strchomp (g_strchug (string))
1603 /* String utility functions that return a newly allocated string which
1604 * ought to be freed with g_free from the caller at some point.
1606 gchar* g_strdup (const gchar *str);
1607 gchar* g_strdup_printf (const gchar *format,
1608 ...) G_GNUC_PRINTF (1, 2);
1609 gchar* g_strdup_vprintf (const gchar *format,
1611 gchar* g_strndup (const gchar *str,
1613 gchar* g_strnfill (guint length,
1615 gchar* g_strconcat (const gchar *string1,
1616 ...); /* NULL terminated */
1617 gchar* g_strjoin (const gchar *separator,
1618 ...); /* NULL terminated */
1619 /* Make a copy of a string interpreting C string -style escape
1620 * sequences. Inverse of g_strescape. The recognized sequences are \b
1621 * \f \n \r \t \\ \" and the octal format.
1623 gchar* g_strcompress (const gchar *source);
1625 /* Convert between the operating system (or C runtime)
1626 * representation of file names and UTF-8.
1628 gchar* g_filename_to_utf8 (const gchar *opsysstring);
1629 gchar* g_filename_from_utf8 (const gchar *utf8string);
1631 /* Copy a string escaping nonprintable characters like in C strings.
1632 * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
1633 * to a string containing characters that are not to be escaped.
1635 * Deprecated API: gchar* g_strescape (const gchar *source);
1636 * Luckily this function wasn't used much, using NULL as second parameter
1637 * provides mostly identical semantics.
1639 gchar* g_strescape (const gchar *source,
1640 const gchar *exceptions);
1642 gpointer g_memdup (gconstpointer mem,
1645 /* NULL terminated string arrays.
1646 * g_strsplit() splits up string into max_tokens tokens at delim and
1647 * returns a newly allocated string array.
1648 * g_strjoinv() concatenates all of str_array's strings, sliding in an
1649 * optional separator, the returned string is newly allocated.
1650 * g_strfreev() frees the array itself and all of its strings.
1652 gchar** g_strsplit (const gchar *string,
1653 const gchar *delimiter,
1655 gchar* g_strjoinv (const gchar *separator,
1657 void g_strfreev (gchar **str_array);
1661 /* calculate a string size, guarranteed to fit format + args.
1663 guint g_printf_string_upper_bound (const gchar* format,
1667 /* Retrive static string info
1669 gchar* g_get_user_name (void);
1670 gchar* g_get_real_name (void);
1671 gchar* g_get_home_dir (void);
1672 gchar* g_get_tmp_dir (void);
1673 gchar* g_get_prgname (void);
1674 void g_set_prgname (const gchar *prgname);
1677 /* Miscellaneous utility functions
1679 guint g_parse_debug_string (const gchar *string,
1682 gint g_snprintf (gchar *string,
1684 gchar const *format,
1685 ...) G_GNUC_PRINTF (3, 4);
1686 gint g_vsnprintf (gchar *string,
1688 gchar const *format,
1690 gchar* g_basename (const gchar *file_name);
1691 /* Check if a file name is an absolute path */
1692 gboolean g_path_is_absolute (const gchar *file_name);
1693 /* In case of absolute paths, skip the root part */
1694 gchar* g_path_skip_root (gchar *file_name);
1696 /* strings are newly allocated with g_malloc() */
1697 gchar* g_dirname (const gchar *file_name);
1698 gchar* g_get_current_dir (void);
1700 /* return the environment string for the variable. The returned memory
1701 * must not be freed. */
1702 gchar* g_getenv (const gchar *variable);
1704 /* we use a GLib function as a replacement for ATEXIT, so
1705 * the programmer is not required to check the return value
1706 * (if there is any in the implementation) and doesn't encounter
1707 * missing include files.
1709 void g_atexit (GVoidFunc func);
1714 G_INLINE_FUNC gint g_bit_nth_lsf (guint32 mask,
1718 g_bit_nth_lsf (guint32 mask,
1724 if (mask & (1 << (guint) nth_bit))
1727 while (nth_bit < 32);
1730 #endif /* G_CAN_INLINE */
1732 G_INLINE_FUNC gint g_bit_nth_msf (guint32 mask,
1736 g_bit_nth_msf (guint32 mask,
1744 if (mask & (1 << (guint) nth_bit))
1747 while (nth_bit > 0);
1750 #endif /* G_CAN_INLINE */
1752 G_INLINE_FUNC guint g_bit_storage (guint number);
1755 g_bit_storage (guint number)
1757 register guint n_bits = 0;
1767 #endif /* G_CAN_INLINE */
1771 * elements need to be >= sizeof (gpointer)
1773 G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p,
1777 g_trash_stack_push (GTrashStack **stack_p,
1780 GTrashStack *data = (GTrashStack *) data_p;
1782 data->next = *stack_p;
1785 #endif /* G_CAN_INLINE */
1787 G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p);
1789 G_INLINE_FUNC gpointer
1790 g_trash_stack_pop (GTrashStack **stack_p)
1797 *stack_p = data->next;
1798 /* NULLify private pointer here, most platforms store NULL as
1799 * subsequent 0 bytes
1806 #endif /* G_CAN_INLINE */
1808 G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p);
1810 G_INLINE_FUNC gpointer
1811 g_trash_stack_peek (GTrashStack **stack_p)
1819 #endif /* G_CAN_INLINE */
1821 G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p);
1824 g_trash_stack_height (GTrashStack **stack_p)
1829 for (data = *stack_p; data; data = data->next)
1834 #endif /* G_CAN_INLINE */
1839 GStringChunk* g_string_chunk_new (gint size);
1840 void g_string_chunk_free (GStringChunk *chunk);
1841 gchar* g_string_chunk_insert (GStringChunk *chunk,
1842 const gchar *string);
1843 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
1844 const gchar *string);
1849 GString* g_string_new (const gchar *init);
1850 GString* g_string_sized_new (guint dfl_size);
1851 void g_string_free (GString *string,
1852 gboolean free_segment);
1853 GString* g_string_assign (GString *string,
1855 GString* g_string_truncate (GString *string,
1857 GString* g_string_insert_len (GString *string,
1861 GString* g_string_append (GString *string,
1863 GString* g_string_append_len (GString *string,
1866 GString* g_string_append_c (GString *string,
1868 GString* g_string_prepend (GString *string,
1870 GString* g_string_prepend_c (GString *string,
1872 GString* g_string_prepend_len (GString *string,
1875 GString* g_string_insert (GString *string,
1878 GString* g_string_insert_c (GString *string,
1881 GString* g_string_erase (GString *string,
1884 GString* g_string_down (GString *string);
1885 GString* g_string_up (GString *string);
1886 void g_string_sprintf (GString *string,
1887 const gchar *format,
1888 ...) G_GNUC_PRINTF (2, 3);
1889 void g_string_sprintfa (GString *string,
1890 const gchar *format,
1891 ...) G_GNUC_PRINTF (2, 3);
1894 /* Resizable arrays, remove fills any cleared spot and shortens the
1895 * array, while preserving the order. remove_fast will distort the
1896 * order by moving the last element to the position of the removed
1899 #define g_array_append_val(a,v) g_array_append_vals (a, &v, 1)
1900 #define g_array_prepend_val(a,v) g_array_prepend_vals (a, &v, 1)
1901 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
1902 #define g_array_index(a,t,i) (((t*) (a)->data) [(i)])
1904 GArray* g_array_new (gboolean zero_terminated,
1906 guint element_size);
1907 GArray* g_array_sized_new (gboolean zero_terminated,
1910 guint reserved_size);
1911 void g_array_free (GArray *array,
1912 gboolean free_segment);
1913 GArray* g_array_append_vals (GArray *array,
1916 GArray* g_array_prepend_vals (GArray *array,
1919 GArray* g_array_insert_vals (GArray *array,
1923 GArray* g_array_set_size (GArray *array,
1925 GArray* g_array_remove_index (GArray *array,
1927 GArray* g_array_remove_index_fast (GArray *array,
1930 /* Resizable pointer array. This interface is much less complicated
1931 * than the above. Add appends appends a pointer. Remove fills any
1932 * cleared spot and shortens the array. remove_fast will again distort
1935 #define g_ptr_array_index(array,index) (array->pdata)[index]
1936 GPtrArray* g_ptr_array_new (void);
1937 GPtrArray* g_ptr_array_sized_new (guint reserved_size);
1938 void g_ptr_array_free (GPtrArray *array,
1940 void g_ptr_array_set_size (GPtrArray *array,
1942 gpointer g_ptr_array_remove_index (GPtrArray *array,
1944 gpointer g_ptr_array_remove_index_fast (GPtrArray *array,
1946 gboolean g_ptr_array_remove (GPtrArray *array,
1948 gboolean g_ptr_array_remove_fast (GPtrArray *array,
1950 void g_ptr_array_add (GPtrArray *array,
1953 /* Byte arrays, an array of guint8. Implemented as a GArray,
1957 GByteArray* g_byte_array_new (void);
1958 GByteArray* g_byte_array_sized_new (guint reserved_size);
1959 void g_byte_array_free (GByteArray *array,
1960 gboolean free_segment);
1961 GByteArray* g_byte_array_append (GByteArray *array,
1964 GByteArray* g_byte_array_prepend (GByteArray *array,
1967 GByteArray* g_byte_array_set_size (GByteArray *array,
1969 GByteArray* g_byte_array_remove_index (GByteArray *array,
1971 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1977 gint g_str_equal (gconstpointer v,
1979 guint g_str_hash (gconstpointer v);
1981 gint g_int_equal (gconstpointer v,
1983 guint g_int_hash (gconstpointer v);
1985 /* This "hash" function will just return the key's adress as an
1986 * unsigned integer. Useful for hashing on plain adresses or
1987 * simple integer values.
1988 * passing NULL into g_hash_table_new() as GHashFunc has the
1989 * same effect as passing g_direct_hash().
1991 guint g_direct_hash (gconstpointer v);
1992 gint g_direct_equal (gconstpointer v,
1996 /* Quarks (string<->id association)
1998 GQuark g_quark_try_string (const gchar *string);
1999 GQuark g_quark_from_static_string (const gchar *string);
2000 GQuark g_quark_from_string (const gchar *string);
2001 gchar* g_quark_to_string (GQuark quark);
2006 void g_datalist_init (GData **datalist);
2007 void g_datalist_clear (GData **datalist);
2008 gpointer g_datalist_id_get_data (GData **datalist,
2010 void g_datalist_id_set_data_full (GData **datalist,
2013 GDestroyNotify destroy_func);
2014 gpointer g_datalist_id_remove_no_notify (GData **datalist,
2016 void g_datalist_foreach (GData **datalist,
2017 GDataForeachFunc func,
2018 gpointer user_data);
2019 #define g_datalist_id_set_data(dl, q, d) \
2020 g_datalist_id_set_data_full ((dl), (q), (d), NULL)
2021 #define g_datalist_id_remove_data(dl, q) \
2022 g_datalist_id_set_data ((dl), (q), NULL)
2023 #define g_datalist_get_data(dl, k) \
2024 (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
2025 #define g_datalist_set_data_full(dl, k, d, f) \
2026 g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
2027 #define g_datalist_remove_no_notify(dl, k) \
2028 g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
2029 #define g_datalist_set_data(dl, k, d) \
2030 g_datalist_set_data_full ((dl), (k), (d), NULL)
2031 #define g_datalist_remove_data(dl, k) \
2032 g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
2035 /* Location Associated Keyed Data
2037 void g_dataset_destroy (gconstpointer dataset_location);
2038 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
2040 void g_dataset_id_set_data_full (gconstpointer dataset_location,
2043 GDestroyNotify destroy_func);
2044 gpointer g_dataset_id_remove_no_notify (gconstpointer dataset_location,
2046 void g_dataset_foreach (gconstpointer dataset_location,
2047 GDataForeachFunc func,
2048 gpointer user_data);
2049 #define g_dataset_id_set_data(l, k, d) \
2050 g_dataset_id_set_data_full ((l), (k), (d), NULL)
2051 #define g_dataset_id_remove_data(l, k) \
2052 g_dataset_id_set_data ((l), (k), NULL)
2053 #define g_dataset_get_data(l, k) \
2054 (g_dataset_id_get_data ((l), g_quark_try_string (k)))
2055 #define g_dataset_set_data_full(l, k, d, f) \
2056 g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
2057 #define g_dataset_remove_no_notify(l, k) \
2058 g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
2059 #define g_dataset_set_data(l, k, d) \
2060 g_dataset_set_data_full ((l), (k), (d), NULL)
2061 #define g_dataset_remove_data(l, k) \
2062 g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
2065 /* GScanner: Flexible lexical scanner for general purpose.
2068 /* Character sets */
2069 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2070 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
2071 #define G_CSET_DIGITS "0123456789"
2072 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
2073 "\307\310\311\312\313\314\315\316\317\320"\
2074 "\321\322\323\324\325\326"\
2075 "\330\331\332\333\334\335\336"
2076 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
2077 "\347\350\351\352\353\354\355\356\357\360"\
2078 "\361\362\363\364\365\366"\
2079 "\370\371\372\373\374\375\376\377"
2086 G_ERR_UNEXP_EOF_IN_STRING,
2087 G_ERR_UNEXP_EOF_IN_COMMENT,
2088 G_ERR_NON_DIGIT_IN_CONST,
2091 G_ERR_FLOAT_MALFORMED
2099 G_TOKEN_LEFT_PAREN = '(',
2100 G_TOKEN_RIGHT_PAREN = ')',
2101 G_TOKEN_LEFT_CURLY = '{',
2102 G_TOKEN_RIGHT_CURLY = '}',
2103 G_TOKEN_LEFT_BRACE = '[',
2104 G_TOKEN_RIGHT_BRACE = ']',
2105 G_TOKEN_EQUAL_SIGN = '=',
2106 G_TOKEN_COMMA = ',',
2122 G_TOKEN_IDENTIFIER_NULL,
2124 G_TOKEN_COMMENT_SINGLE,
2125 G_TOKEN_COMMENT_MULTI,
2132 gchar *v_identifier;
2144 struct _GScannerConfig
2148 gchar *cset_skip_characters; /* default: " \t\n" */
2149 gchar *cset_identifier_first;
2150 gchar *cset_identifier_nth;
2151 gchar *cpair_comment_single; /* default: "#\n" */
2153 /* Should symbol lookup work case sensitive?
2155 guint case_sensitive : 1;
2157 /* Boolean values to be adjusted "on the fly"
2158 * to configure scanning behaviour.
2160 guint skip_comment_multi : 1; /* C like comment */
2161 guint skip_comment_single : 1; /* single line comment */
2162 guint scan_comment_multi : 1; /* scan multi line comments? */
2163 guint scan_identifier : 1;
2164 guint scan_identifier_1char : 1;
2165 guint scan_identifier_NULL : 1;
2166 guint scan_symbols : 1;
2167 guint scan_binary : 1;
2168 guint scan_octal : 1;
2169 guint scan_float : 1;
2170 guint scan_hex : 1; /* `0x0ff0' */
2171 guint scan_hex_dollar : 1; /* `$0ff0' */
2172 guint scan_string_sq : 1; /* string: 'anything' */
2173 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
2174 guint numbers_2_int : 1; /* bin, octal, hex => int */
2175 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
2176 guint identifier_2_string : 1;
2177 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
2178 guint symbol_2_token : 1;
2179 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
2186 guint max_parse_errors;
2188 /* g_scanner_error() increments this field */
2191 /* name of input stream, featured by the default message handler */
2192 const gchar *input_name;
2194 /* data pointer for derived structures */
2195 gpointer derived_data;
2197 /* link into the scanner configuration */
2198 GScannerConfig *config;
2200 /* fields filled in after g_scanner_get_next_token() */
2206 /* fields filled in after g_scanner_peek_next_token() */
2207 GTokenType next_token;
2208 GTokenValue next_value;
2210 guint next_position;
2212 /* to be considered private */
2213 GHashTable *symbol_table;
2216 const gchar *text_end;
2220 /* handler function for _warn and _error */
2221 GScannerMsgFunc msg_handler;
2224 GScanner* g_scanner_new (GScannerConfig *config_templ);
2225 void g_scanner_destroy (GScanner *scanner);
2226 void g_scanner_input_file (GScanner *scanner,
2228 void g_scanner_sync_file_offset (GScanner *scanner);
2229 void g_scanner_input_text (GScanner *scanner,
2232 GTokenType g_scanner_get_next_token (GScanner *scanner);
2233 GTokenType g_scanner_peek_next_token (GScanner *scanner);
2234 GTokenType g_scanner_cur_token (GScanner *scanner);
2235 GTokenValue g_scanner_cur_value (GScanner *scanner);
2236 guint g_scanner_cur_line (GScanner *scanner);
2237 guint g_scanner_cur_position (GScanner *scanner);
2238 gboolean g_scanner_eof (GScanner *scanner);
2239 guint g_scanner_set_scope (GScanner *scanner,
2241 void g_scanner_scope_add_symbol (GScanner *scanner,
2243 const gchar *symbol,
2245 void g_scanner_scope_remove_symbol (GScanner *scanner,
2247 const gchar *symbol);
2248 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
2250 const gchar *symbol);
2251 void g_scanner_scope_foreach_symbol (GScanner *scanner,
2254 gpointer user_data);
2255 gpointer g_scanner_lookup_symbol (GScanner *scanner,
2256 const gchar *symbol);
2257 void g_scanner_freeze_symbol_table (GScanner *scanner);
2258 void g_scanner_thaw_symbol_table (GScanner *scanner);
2259 void g_scanner_unexp_token (GScanner *scanner,
2260 GTokenType expected_token,
2261 const gchar *identifier_spec,
2262 const gchar *symbol_spec,
2263 const gchar *symbol_name,
2264 const gchar *message,
2266 void g_scanner_error (GScanner *scanner,
2267 const gchar *format,
2268 ...) G_GNUC_PRINTF (2,3);
2269 void g_scanner_warn (GScanner *scanner,
2270 const gchar *format,
2271 ...) G_GNUC_PRINTF (2,3);
2272 gint g_scanner_stat_mode (const gchar *filename);
2273 /* keep downward source compatibility */
2274 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
2275 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2277 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
2278 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2280 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2281 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2291 GCompletionFunc func;
2297 GCompletion* g_completion_new (GCompletionFunc func);
2298 void g_completion_add_items (GCompletion* cmp,
2300 void g_completion_remove_items (GCompletion* cmp,
2302 void g_completion_clear_items (GCompletion* cmp);
2303 GList* g_completion_complete (GCompletion* cmp,
2305 gchar** new_prefix);
2306 void g_completion_free (GCompletion* cmp);
2311 * Date calculations (not time for now, to be resolved). These are a
2312 * mutant combination of Steffen Beyer's DateCalc routines
2313 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2314 * date routines (written for in-house software). Written by Havoc
2315 * Pennington <hp@pobox.com>
2318 typedef guint16 GDateYear;
2319 typedef guint8 GDateDay; /* day of the month */
2320 typedef struct _GDate GDate;
2321 /* make struct tm known without having to include time.h */
2324 /* enum used to specify order of appearance in parsed date strings */
2332 /* actual week and month values */
2335 G_DATE_BAD_WEEKDAY = 0,
2338 G_DATE_WEDNESDAY = 3,
2339 G_DATE_THURSDAY = 4,
2341 G_DATE_SATURDAY = 6,
2346 G_DATE_BAD_MONTH = 0,
2348 G_DATE_FEBRUARY = 2,
2355 G_DATE_SEPTEMBER = 9,
2356 G_DATE_OCTOBER = 10,
2357 G_DATE_NOVEMBER = 11,
2358 G_DATE_DECEMBER = 12
2361 #define G_DATE_BAD_JULIAN 0U
2362 #define G_DATE_BAD_DAY 0U
2363 #define G_DATE_BAD_YEAR 0U
2365 /* Note: directly manipulating structs is generally a bad idea, but
2366 * in this case it's an *incredibly* bad idea, because all or part
2367 * of this struct can be invalid at any given time. Use the functions,
2368 * or you will get hosed, I promise.
2372 guint julian_days : 32; /* julian days representation - we use a
2373 * bitfield hoping that 64 bit platforms
2374 * will pack this whole struct in one big
2378 guint julian : 1; /* julian is valid */
2379 guint dmy : 1; /* dmy is valid */
2381 /* DMY representation */
2387 /* g_date_new() returns an invalid date, you then have to _set() stuff
2388 * to get a usable object. You can also allocate a GDate statically,
2389 * then call g_date_clear() to initialize.
2391 GDate* g_date_new (void);
2392 GDate* g_date_new_dmy (GDateDay day,
2395 GDate* g_date_new_julian (guint32 julian_day);
2396 void g_date_free (GDate *date);
2398 /* check g_date_valid() after doing an operation that might fail, like
2399 * _parse. Almost all g_date operations are undefined on invalid
2400 * dates (the exceptions are the mutators, since you need those to
2401 * return to validity).
2403 gboolean g_date_valid (GDate *date);
2404 gboolean g_date_valid_day (GDateDay day);
2405 gboolean g_date_valid_month (GDateMonth month);
2406 gboolean g_date_valid_year (GDateYear year);
2407 gboolean g_date_valid_weekday (GDateWeekday weekday);
2408 gboolean g_date_valid_julian (guint32 julian_date);
2409 gboolean g_date_valid_dmy (GDateDay day,
2413 GDateWeekday g_date_weekday (GDate *date);
2414 GDateMonth g_date_month (GDate *date);
2415 GDateYear g_date_year (GDate *date);
2416 GDateDay g_date_day (GDate *date);
2417 guint32 g_date_julian (GDate *date);
2418 guint g_date_day_of_year (GDate *date);
2420 /* First monday/sunday is the start of week 1; if we haven't reached
2421 * that day, return 0. These are not ISO weeks of the year; that
2422 * routine needs to be added.
2423 * these functions return the number of weeks, starting on the
2426 guint g_date_monday_week_of_year (GDate *date);
2427 guint g_date_sunday_week_of_year (GDate *date);
2429 /* If you create a static date struct you need to clear it to get it
2430 * in a sane state before use. You can clear a whole array at
2431 * once with the ndates argument.
2433 void g_date_clear (GDate *date,
2436 /* The parse routine is meant for dates typed in by a user, so it
2437 * permits many formats but tries to catch common typos. If your data
2438 * needs to be strictly validated, it is not an appropriate function.
2440 void g_date_set_parse (GDate *date,
2442 void g_date_set_time (GDate *date,
2444 void g_date_set_month (GDate *date,
2446 void g_date_set_day (GDate *date,
2448 void g_date_set_year (GDate *date,
2450 void g_date_set_dmy (GDate *date,
2454 void g_date_set_julian (GDate *date,
2455 guint32 julian_date);
2456 gboolean g_date_is_first_of_month (GDate *date);
2457 gboolean g_date_is_last_of_month (GDate *date);
2459 /* To go forward by some number of weeks just go forward weeks*7 days */
2460 void g_date_add_days (GDate *date,
2462 void g_date_subtract_days (GDate *date,
2465 /* If you add/sub months while day > 28, the day might change */
2466 void g_date_add_months (GDate *date,
2468 void g_date_subtract_months (GDate *date,
2471 /* If it's feb 29, changing years can move you to the 28th */
2472 void g_date_add_years (GDate *date,
2474 void g_date_subtract_years (GDate *date,
2476 gboolean g_date_is_leap_year (GDateYear year);
2477 guint8 g_date_days_in_month (GDateMonth month,
2479 guint8 g_date_monday_weeks_in_year (GDateYear year);
2480 guint8 g_date_sunday_weeks_in_year (GDateYear year);
2482 /* qsort-friendly (with a cast...) */
2483 gint g_date_compare (GDate *lhs,
2485 void g_date_to_struct_tm (GDate *date,
2488 /* Just like strftime() except you can only use date-related formats.
2489 * Using a time format is undefined.
2491 gsize g_date_strftime (gchar *s,
2493 const gchar *format,
2499 * Indexed Relations. Imagine a really simple table in a
2500 * database. Relations are not ordered. This data type is meant for
2501 * maintaining a N-way mapping.
2503 * g_relation_new() creates a relation with FIELDS fields
2505 * g_relation_destroy() frees all resources
2506 * g_tuples_destroy() frees the result of g_relation_select()
2508 * g_relation_index() indexes relation FIELD with the provided
2509 * equality and hash functions. this must be done before any
2510 * calls to insert are made.
2512 * g_relation_insert() inserts a new tuple. you are expected to
2513 * provide the right number of fields.
2515 * g_relation_delete() deletes all relations with KEY in FIELD
2516 * g_relation_select() returns ...
2517 * g_relation_count() counts ...
2520 GRelation* g_relation_new (gint fields);
2521 void g_relation_destroy (GRelation *relation);
2522 void g_relation_index (GRelation *relation,
2524 GHashFunc hash_func,
2525 GCompareFunc key_compare_func);
2526 void g_relation_insert (GRelation *relation,
2528 gint g_relation_delete (GRelation *relation,
2531 GTuples* g_relation_select (GRelation *relation,
2534 gint g_relation_count (GRelation *relation,
2537 gboolean g_relation_exists (GRelation *relation,
2539 void g_relation_print (GRelation *relation);
2541 void g_tuples_destroy (GTuples *tuples);
2542 gpointer g_tuples_index (GTuples *tuples,
2547 /* GRand - a good and fast random number generator: Mersenne Twister
2548 * see http://www.math.keio.ac.jp/~matumoto/emt.html for more info.
2549 * The range functions return a value in the intervall [min,max).
2550 * int -> [0..2^32-1]
2551 * int_range -> [min..max-1]
2553 * double_range -> [min..max)
2556 GRand* g_rand_new_with_seed (guint32 seed);
2557 GRand* g_rand_new (void);
2558 void g_rand_free (GRand *rand);
2560 void g_rand_set_seed (GRand *rand,
2562 guint32 g_rand_int (GRand *rand);
2563 gint32 g_rand_int_range (GRand *rand,
2566 gdouble g_rand_double (GRand *rand);
2567 gdouble g_rand_double_range (GRand *rand,
2571 void g_random_set_seed (guint32 seed);
2572 guint32 g_random_int (void);
2573 gint32 g_random_int_range (gint32 min,
2575 gdouble g_random_double (void);
2576 gdouble g_random_double_range (gdouble min,
2583 /* This function returns prime numbers spaced by approximately 1.5-2.0
2584 * and is for use in resizing data structures which prefer
2585 * prime-valued sizes. The closest spaced prime function returns the
2586 * next largest prime, or the highest it knows about which is about
2589 guint g_spaced_primes_closest (guint num);
2595 typedef struct _GIOFuncs GIOFuncs;
2611 G_IO_IN GLIB_SYSDEF_POLLIN,
2612 G_IO_OUT GLIB_SYSDEF_POLLOUT,
2613 G_IO_PRI GLIB_SYSDEF_POLLPRI,
2614 G_IO_ERR GLIB_SYSDEF_POLLERR,
2615 G_IO_HUP GLIB_SYSDEF_POLLHUP,
2616 G_IO_NVAL GLIB_SYSDEF_POLLNVAL
2621 guint channel_flags;
2626 typedef gboolean (*GIOFunc) (GIOChannel *source,
2627 GIOCondition condition,
2631 GIOError (*io_read) (GIOChannel *channel,
2635 GIOError (*io_write) (GIOChannel *channel,
2638 guint *bytes_written);
2639 GIOError (*io_seek) (GIOChannel *channel,
2642 void (*io_close) (GIOChannel *channel);
2643 guint (*io_add_watch) (GIOChannel *channel,
2645 GIOCondition condition,
2648 GDestroyNotify notify);
2649 void (*io_free) (GIOChannel *channel);
2652 void g_io_channel_init (GIOChannel *channel);
2653 void g_io_channel_ref (GIOChannel *channel);
2654 void g_io_channel_unref (GIOChannel *channel);
2655 GIOError g_io_channel_read (GIOChannel *channel,
2659 GIOError g_io_channel_write (GIOChannel *channel,
2662 guint *bytes_written);
2663 GIOError g_io_channel_seek (GIOChannel *channel,
2666 void g_io_channel_close (GIOChannel *channel);
2667 guint g_io_add_watch_full (GIOChannel *channel,
2669 GIOCondition condition,
2672 GDestroyNotify notify);
2673 guint g_io_add_watch (GIOChannel *channel,
2674 GIOCondition condition,
2676 gpointer user_data);
2681 typedef struct _GTimeVal GTimeVal;
2682 typedef struct _GSourceFuncs GSourceFuncs;
2683 typedef struct _GMainLoop GMainLoop; /* Opaque */
2690 struct _GSourceFuncs
2692 gboolean (*prepare) (gpointer source_data,
2693 GTimeVal *current_time,
2695 gpointer user_data);
2696 gboolean (*check) (gpointer source_data,
2697 GTimeVal *current_time,
2698 gpointer user_data);
2699 gboolean (*dispatch) (gpointer source_data,
2700 GTimeVal *dispatch_time,
2701 gpointer user_data);
2702 GDestroyNotify destroy;
2705 /* Standard priorities */
2707 #define G_PRIORITY_HIGH -100
2708 #define G_PRIORITY_DEFAULT 0
2709 #define G_PRIORITY_HIGH_IDLE 100
2710 #define G_PRIORITY_DEFAULT_IDLE 200
2711 #define G_PRIORITY_LOW 300
2713 typedef gboolean (*GSourceFunc) (gpointer data);
2715 /* Hooks for adding to the main loop */
2716 guint g_source_add (gint priority,
2717 gboolean can_recurse,
2718 GSourceFuncs *funcs,
2719 gpointer source_data,
2721 GDestroyNotify notify);
2722 gboolean g_source_remove (guint tag);
2723 gboolean g_source_remove_by_user_data (gpointer user_data);
2724 gboolean g_source_remove_by_source_data (gpointer source_data);
2725 gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
2726 gpointer user_data);
2728 void g_get_current_time (GTimeVal *result);
2730 /* Running the main loop */
2731 GMainLoop* g_main_new (gboolean is_running);
2732 void g_main_run (GMainLoop *loop);
2733 void g_main_quit (GMainLoop *loop);
2734 void g_main_destroy (GMainLoop *loop);
2735 gboolean g_main_is_running (GMainLoop *loop);
2737 /* Run a single iteration of the mainloop. If block is FALSE,
2740 gboolean g_main_iteration (gboolean may_block);
2742 /* See if any events are pending */
2743 gboolean g_main_pending (void);
2745 /* Idles and timeouts */
2746 guint g_timeout_add_full (gint priority,
2748 GSourceFunc function,
2750 GDestroyNotify notify);
2751 guint g_timeout_add (guint interval,
2752 GSourceFunc function,
2754 guint g_idle_add (GSourceFunc function,
2756 guint g_idle_add_full (gint priority,
2757 GSourceFunc function,
2759 GDestroyNotify destroy);
2760 gboolean g_idle_remove_by_data (gpointer data);
2764 * System-specific IO and main loop calls
2766 * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
2767 * descriptor as provided by the C runtime) that can be used by
2768 * MsgWaitForMultipleObjects. This does *not* include file handles
2769 * from CreateFile, SOCKETs, nor pipe handles. (But you can use
2770 * WSAEventSelect to signal events when a SOCKET is readable).
2772 * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
2773 * indicate polling for messages. These message queue GPollFDs should
2774 * be added with the g_main_poll_win32_msg_add function.
2776 * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
2777 * (GTK) programs, as GDK itself wants to read messages and convert them
2780 * So, unless you really know what you are doing, it's best not to try
2781 * to use the main loop polling stuff for your own needs on
2782 * Win32. It's really only written for the GIMP's needs so
2786 typedef struct _GPollFD GPollFD;
2787 typedef gint (*GPollFunc) (GPollFD *ufds,
2797 void g_main_add_poll (GPollFD *fd,
2799 void g_main_remove_poll (GPollFD *fd);
2800 void g_main_set_poll_func (GPollFunc func);
2802 /* On Unix, IO channels created with this function for any file
2803 * descriptor or socket.
2805 * On Win32, use this only for plain files opened with the MSVCRT (the
2806 * Microsoft run-time C library) _open(), including file descriptors
2807 * 0, 1 and 2 (corresponding to stdin, stdout and stderr).
2808 * Actually, don't do even that, this code isn't done yet.
2810 * The term file descriptor as used in the context of Win32 refers to
2811 * the emulated Unix-like file descriptors MSVCRT provides.
2813 GIOChannel* g_io_channel_unix_new (int fd);
2814 gint g_io_channel_unix_get_fd (GIOChannel *channel);
2818 GUTILS_C_VAR guint g_pipe_readable_msg;
2820 #define G_WIN32_MSG_HANDLE 19981206
2822 /* This is used to add polling for Windows messages. GDK (GTk+) programs
2823 * should *not* use this. (In fact, I can't think of any program that
2824 * would want to use this, but it's here just for completeness's sake.
2826 void g_main_poll_win32_msg_add(gint priority,
2830 /* An IO channel for Windows messages for window handle hwnd. */
2831 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
2833 /* An IO channel for an anonymous pipe as returned from the MSVCRT
2834 * _pipe(), with no mechanism for the writer to tell the reader when
2835 * there is data in the pipe.
2837 * This is not really implemented yet.
2839 GIOChannel *g_io_channel_win32_new_pipe (int fd);
2841 /* An IO channel for a pipe as returned from the MSVCRT _pipe(), with
2842 * Windows user messages used to signal data in the pipe for the
2845 * fd is the file descriptor. For the write end, peer is the thread id
2846 * of the reader, and peer_fd is his file descriptor for the read end
2849 * This is used by the GIMP, and works.
2851 GIOChannel *g_io_channel_win32_new_pipe_with_wakeups (int fd,
2855 void g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
2859 void g_io_channel_win32_pipe_readable (int fd,
2862 /* Get the C runtime file descriptor of a channel. */
2863 gint g_io_channel_win32_get_fd (GIOChannel *channel);
2865 /* An IO channel for a SOCK_STREAM winsock socket. The parameter is
2866 * actually a SOCKET.
2868 GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
2872 /* Windows emulation stubs for common Unix functions
2875 # define MAXPATHLEN 1024
2882 * To get prototypes for the following POSIXish functions, you have to
2883 * include the indicated non-POSIX headers. The functions are defined
2884 * in OLDNAMES.LIB (MSVC) or -lmoldname-msvc (mingw32).
2886 * getcwd: <direct.h> (MSVC), <io.h> (mingw32)
2887 * getpid: <process.h>
2889 * unlink: <stdio.h> or <io.h>
2890 * open, read, write, lseek, close: <io.h>
2895 /* pipe is not in OLDNAMES.LIB or -lmoldname-msvc. */
2896 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
2898 /* For some POSIX functions that are not provided by the MS runtime,
2899 * we provide emulators in glib, which are prefixed with g_win32_.
2901 # define ftruncate(fd, size) g_win32_ftruncate (fd, size)
2903 /* -lmingw32 also has emulations for these, but we need our own
2904 * for MSVC anyhow, so we might aswell use them always.
2906 # define opendir g_win32_opendir
2907 # define readdir g_win32_readdir
2908 # define rewinddir g_win32_rewinddir
2909 # define closedir g_win32_closedir
2910 # define NAME_MAX 255
2915 gboolean just_opened;
2916 guint find_file_handle;
2917 gpointer find_file_data;
2919 typedef struct DIR DIR;
2922 gchar d_name[NAME_MAX + 1];
2924 /* emulation functions */
2925 extern int g_win32_ftruncate (gint f,
2927 DIR* g_win32_opendir (const gchar *dirname);
2928 struct dirent* g_win32_readdir (DIR *dir);
2929 void g_win32_rewinddir (DIR *dir);
2930 gint g_win32_closedir (DIR *dir);
2932 /* The MS setlocale uses locale names of the form "English_United
2933 * States.1252" etc. We want the Unixish standard form "en", "zh_TW"
2934 * etc. This function gets the current thread locale from Windows and
2935 * returns it as a string of the above form for use in forming file
2936 * names etc. The returned string should be deallocated with g_free().
2938 gchar * g_win32_getlocale (void);
2940 /* Translate a Win32 error code (as returned by GetLastError()) into
2941 * the corresponding message. The returned string should be deallocated
2944 gchar * g_win32_error_message (gint error);
2946 #endif /* G_OS_WIN32 */
2949 /* GLib Thread support
2952 typedef void (*GThreadFunc) (gpointer value);
2956 G_THREAD_PRIORITY_LOW,
2957 G_THREAD_PRIORITY_NORMAL,
2958 G_THREAD_PRIORITY_HIGH,
2959 G_THREAD_PRIORITY_URGENT
2962 typedef struct _GThread GThread;
2965 GThreadPriority priority;
2970 typedef struct _GMutex GMutex;
2971 typedef struct _GCond GCond;
2972 typedef struct _GPrivate GPrivate;
2973 typedef struct _GStaticPrivate GStaticPrivate;
2974 typedef struct _GAsyncQueue GAsyncQueue;
2975 typedef struct _GThreadPool GThreadPool;
2977 typedef struct _GThreadFunctions GThreadFunctions;
2978 struct _GThreadFunctions
2980 GMutex* (*mutex_new) (void);
2981 void (*mutex_lock) (GMutex *mutex);
2982 gboolean (*mutex_trylock) (GMutex *mutex);
2983 void (*mutex_unlock) (GMutex *mutex);
2984 void (*mutex_free) (GMutex *mutex);
2985 GCond* (*cond_new) (void);
2986 void (*cond_signal) (GCond *cond);
2987 void (*cond_broadcast) (GCond *cond);
2988 void (*cond_wait) (GCond *cond,
2990 gboolean (*cond_timed_wait) (GCond *cond,
2992 GTimeVal *end_time);
2993 void (*cond_free) (GCond *cond);
2994 GPrivate* (*private_new) (GDestroyNotify destructor);
2995 gpointer (*private_get) (GPrivate *private_key);
2996 void (*private_set) (GPrivate *private_key,
2998 void (*thread_create) (GThreadFunc thread_func,
3003 GThreadPriority priority,
3005 void (*thread_yield) (void);
3006 void (*thread_join) (gpointer thread);
3007 void (*thread_exit) (void);
3008 void (*thread_set_priority)(gpointer thread,
3009 GThreadPriority priority);
3010 void (*thread_self) (gpointer thread);
3013 GUTILS_C_VAR GThreadFunctions g_thread_functions_for_glib_use;
3014 GUTILS_C_VAR gboolean g_thread_use_default_impl;
3015 GUTILS_C_VAR gboolean g_threads_got_initialized;
3017 /* initializes the mutex/cond/private implementation for glib, might
3018 * only be called once, and must not be called directly or indirectly
3019 * from another glib-function, e.g. as a callback.
3021 void g_thread_init (GThreadFunctions *vtable);
3023 /* internal function for fallback static mutex implementation */
3024 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
3026 /* shorthands for conditional and unconditional function calls */
3027 #define G_THREAD_UF(name, arglist) \
3028 (*g_thread_functions_for_glib_use . name) arglist
3029 #define G_THREAD_CF(name, fail, arg) \
3030 (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
3031 /* keep in mind, all those mutexes and static mutexes are not
3032 * recursive in general, don't rely on that
3034 #define g_thread_supported() (g_threads_got_initialized)
3035 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
3036 #define g_mutex_lock(mutex) G_THREAD_CF (mutex_lock, (void)0, (mutex))
3037 #define g_mutex_trylock(mutex) G_THREAD_CF (mutex_trylock, TRUE, (mutex))
3038 #define g_mutex_unlock(mutex) G_THREAD_CF (mutex_unlock, (void)0, (mutex))
3039 #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex))
3040 #define g_cond_new() G_THREAD_UF (cond_new, ())
3041 #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
3042 #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
3043 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait, (void)0, (cond, \
3045 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
3046 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
3050 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
3051 #define g_private_get(private_key) G_THREAD_CF (private_get, \
3052 ((gpointer)private_key), \
3054 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
3055 (void) (private_key = \
3056 (GPrivate*) (value)), \
3057 (private_key, value))
3058 #define g_thread_yield() G_THREAD_CF (thread_yield, (void)0, ())
3059 #define g_thread_exit() G_THREAD_CF (thread_exit, (void)0, ())
3061 GThread* g_thread_create (GThreadFunc thread_func,
3066 GThreadPriority priority);
3067 GThread* g_thread_self ();
3068 void g_thread_join (GThread* thread);
3069 void g_thread_set_priority (GThread* thread,
3070 GThreadPriority priority);
3072 /* GStaticMutexes can be statically initialized with the value
3073 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
3074 * much easier, than having to explicitly allocate the mutex before
3077 #define g_static_mutex_lock(mutex) \
3078 g_mutex_lock (g_static_mutex_get_mutex (mutex))
3079 #define g_static_mutex_trylock(mutex) \
3080 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
3081 #define g_static_mutex_unlock(mutex) \
3082 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
3084 struct _GStaticPrivate
3088 #define G_STATIC_PRIVATE_INIT { 0 }
3089 gpointer g_static_private_get (GStaticPrivate *private_key);
3090 void g_static_private_set (GStaticPrivate *private_key,
3092 GDestroyNotify notify);
3093 gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
3095 void g_static_private_set_for_thread (GStaticPrivate *private_key,
3098 GDestroyNotify notify);
3100 typedef struct _GStaticRecMutex GStaticRecMutex;
3101 struct _GStaticRecMutex
3105 GSystemThread owner;
3108 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT }
3109 void g_static_rec_mutex_lock (GStaticRecMutex *mutex);
3110 gboolean g_static_rec_mutex_trylock (GStaticRecMutex *mutex);
3111 void g_static_rec_mutex_unlock (GStaticRecMutex *mutex);
3112 void g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
3114 guint g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
3116 typedef struct _GStaticRWLock GStaticRWLock;
3117 struct _GStaticRWLock
3124 guint want_to_write;
3127 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
3129 void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
3130 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
3131 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
3132 void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
3133 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
3134 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
3135 void g_static_rw_lock_free (GStaticRWLock* lock);
3137 /* these are some convenience macros that expand to nothing if GLib
3138 * was configured with --disable-threads. for using StaticMutexes,
3139 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
3140 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
3141 * declare such an globally defined lock. name is a unique identifier
3142 * for the protected varibale or code portion. locking, testing and
3143 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
3144 * G_TRYLOCK() respectively.
3146 extern void glib_dummy_decl (void);
3147 #define G_LOCK_NAME(name) g__ ## name ## _lock
3148 #ifdef G_THREADS_ENABLED
3149 # define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
3150 # define G_LOCK_DEFINE(name) \
3151 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
3152 # define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
3154 # ifdef G_DEBUG_LOCKS
3155 # define G_LOCK(name) G_STMT_START{ \
3156 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3157 "file %s: line %d (%s): locking: %s ", \
3158 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3160 g_static_mutex_lock (&G_LOCK_NAME (name)); \
3162 # define G_UNLOCK(name) G_STMT_START{ \
3163 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3164 "file %s: line %d (%s): unlocking: %s ", \
3165 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3167 g_static_mutex_unlock (&G_LOCK_NAME (name)); \
3169 # define G_TRYLOCK(name) \
3170 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3171 "file %s: line %d (%s): try locking: %s ", \
3172 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3173 #name), g_static_mutex_trylock (&G_LOCK_NAME (name)))
3174 # else /* !G_DEBUG_LOCKS */
3175 # define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
3176 # define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
3177 # define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
3178 # endif /* !G_DEBUG_LOCKS */
3179 #else /* !G_THREADS_ENABLED */
3180 # define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
3181 # define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
3182 # define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
3183 # define G_LOCK(name)
3184 # define G_UNLOCK(name)
3185 # define G_TRYLOCK(name) (TRUE)
3186 #endif /* !G_THREADS_ENABLED */
3188 /* Asyncronous Queues, can be used to communicate between threads
3191 /* Get a new GAsyncQueue with the ref_count 1 */
3192 GAsyncQueue* g_async_queue_new (void);
3194 /* Lock and unlock an GAsyncQueue, all functions lock the queue for
3195 * themselves, but in certain cirumstances you want to hold the lock longer,
3196 * thus you lock the queue, call the *_unlocked functions and unlock it again
3198 void g_async_queue_lock (GAsyncQueue *queue);
3199 void g_async_queue_unlock (GAsyncQueue *queue);
3201 /* Ref and unref the GAsyncQueue. g_async_queue_unref_unlocked makes
3202 * no sense, as after the unreffing the Queue might be gone and can't
3203 * be unlocked. So you have a function to call, if you don't hold the
3204 * lock (g_async_queue_unref) and one to call, when you already hold
3205 * the lock (g_async_queue_unref_and_unlock). After that however, you
3206 * don't hold the lock anymore and the Queue might in fact be
3207 * destroyed, if you unrefed to zero */
3208 void g_async_queue_ref (GAsyncQueue *queue);
3209 void g_async_queue_ref_unlocked (GAsyncQueue *queue);
3210 void g_async_queue_unref (GAsyncQueue *queue);
3211 void g_async_queue_unref_and_unlock (GAsyncQueue *queue);
3213 /* Push data into the async queue. Must not be NULL */
3214 void g_async_queue_push (GAsyncQueue *queue,
3216 void g_async_queue_push_unlocked (GAsyncQueue *queue,
3219 /* Pop data from the async queue, when no data is there, the thread is blocked
3220 * until data arrives */
3221 gpointer g_async_queue_pop (GAsyncQueue *queue);
3222 gpointer g_async_queue_pop_unlocked (GAsyncQueue *queue);
3224 /* Try to pop data, NULL is returned in case of empty queue */
3225 gpointer g_async_queue_try_pop (GAsyncQueue *queue);
3226 gpointer g_async_queue_try_pop_unlocked (GAsyncQueue *queue);
3228 /* Wait for data until at maximum until end_time is reached, NULL is returned
3229 * in case of empty queue*/
3230 gpointer g_async_queue_timed_pop (GAsyncQueue *queue,
3231 GTimeVal *end_time);
3232 gpointer g_async_queue_timed_pop_unlocked (GAsyncQueue *queue,
3233 GTimeVal *end_time);
3235 /* Return the length of the queue, negative values mean, that threads
3236 * are waiting, positve values mean, that there are entries in the
3237 * queue. Actually this function returns the length of the queue minus
3238 * the number of waiting threads, g_async_queue_length == 0 could also
3239 * mean 'n' entries in the queue and 'n' thread waiting, such can
3240 * happen due to locking of the queue or due to scheduling. */
3241 gint g_async_queue_length (GAsyncQueue *queue);
3242 gint g_async_queue_length_unlocked (GAsyncQueue *queue);
3247 /* The real GThreadPool is bigger, so you may only create a thread
3248 * pool with the constructor function */
3254 GThreadPriority priority;
3259 /* Get a thread pool with the function thread_func, at most max_threads may
3260 * run at a time (max_threads == -1 means no limit), stack_size, bound,
3261 * priority like in g_thread_create, exclusive == TRUE means, that the threads
3262 * shouldn't be shared and that they will be prestarted (otherwise they are
3263 * started, as needed) user_data is the 2nd argument to the thread_func */
3264 GThreadPool* g_thread_pool_new (GFunc thread_func,
3268 GThreadPriority priority,
3270 gpointer user_data);
3272 /* Push new data into the thread pool. This task is assigned to a thread later
3273 * (when the maximal number of threads is reached for that pool) or now
3274 * (otherwise). If necessary a new thread will be started. The function
3275 * returns immediatly */
3276 void g_thread_pool_push (GThreadPool *pool,
3279 /* Set the number of threads, which can run concurrently for that pool, -1
3280 * means no limit. 0 means has the effect, that the pool won't process
3281 * requests until the limit is set higher again */
3282 void g_thread_pool_set_max_threads (GThreadPool *pool,
3284 gint g_thread_pool_get_max_threads (GThreadPool *pool);
3286 /* Get the number of threads assigned to that pool. This number doesn't
3287 * necessarily represent the number of working threads in that pool */
3288 guint g_thread_pool_get_num_threads (GThreadPool *pool);
3290 /* Get the number of unprocessed items in the pool */
3291 guint g_thread_pool_unprocessed (GThreadPool *pool);
3293 /* Free the pool, immediate means, that all unprocessed items in the queue
3294 * wont be processed, wait means, that the function doesn't return immediatly,
3295 * but after all threads in the pool are ready processing items. immediate
3296 * does however not mean, that threads are killed. */
3297 void g_thread_pool_free (GThreadPool *pool,
3301 /* Set the maximal number of unused threads before threads will be stopped by
3302 * GLib, -1 means no limit */
3303 void g_thread_pool_set_max_unused_threads (gint max_threads);
3304 gint g_thread_pool_get_max_unused_threads (void);
3305 guint g_thread_pool_get_num_unused_threads (void);
3307 /* Stop all currently unused threads, but leave the limit untouched */
3308 void g_thread_pool_stop_unused_threads (void);
3312 #endif /* __cplusplus */
3315 #endif /* __G_LIB_H__ */