Made the debugging G_TRYLOCK call also work for compilers with funnt
[glib.git] / glib.h
blob49c8fb69e8c0c4be61524f52138f50d4345ff8da
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/.
27 #ifndef __G_LIB_H__
28 #define __G_LIB_H__
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__
39 #else
40 # define G_GNUC_EXTENSION
41 #endif
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
47 * G_MININT, G_MAXINT
48 * G_MINLONG, G_MAXLONG
49 * G_MINFLOAT, G_MAXFLOAT
50 * G_MINDOUBLE, G_MAXDOUBLE
52 * It also provides the following typedefs:
54 * gint8, guint8
55 * gint16, guint16
56 * gint32, guint32
57 * gint64, guint64
59 * It defines the G_BYTE_ORDER symbol to one of G_*_ENDIAN (see later in
60 * this file).
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,
73 * or size_t.
75 #include <glibconfig.h>
77 /* include varargs functions for assertment macros
79 #include <stdarg.h>
81 /* optionally feature DMALLOC memory allocation debugger
83 #ifdef USE_DMALLOC
84 #include "dmalloc.h"
85 #endif
88 #ifdef G_OS_WIN32
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 */
100 /* Unix */
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 */
109 #ifdef __cplusplus
110 extern "C" {
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.
119 #ifndef NULL
120 # ifdef __cplusplus
121 # define NULL (0L)
122 # else /* !__cplusplus */
123 # define NULL ((void*) 0)
124 # endif /* !__cplusplus */
125 #endif
127 #ifndef FALSE
128 #define FALSE (0)
129 #endif
131 #ifndef TRUE
132 #define TRUE (!FALSE)
133 #endif
135 #undef MAX
136 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
138 #undef MIN
139 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
141 #undef ABS
142 #define ABS(a) (((a) < 0) ? -(a) : (a))
144 #undef CLAMP
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
152 * incorrect results.
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
193 #endif
194 #ifdef G_HAVE_INLINE
195 # if defined (__GNUC__) && defined (__STRICT_ANSI__)
196 # undef inline
197 # define inline __inline__
198 # endif
199 #else /* !G_HAVE_INLINE */
200 # undef 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
209 # undef G_CAN_INLINE
210 # endif
211 # endif
212 # endif
213 #endif
214 #ifndef G_INLINE_FUNC
215 # ifdef __GNUC__
216 # ifdef __OPTIMIZE__
217 # define G_INLINE_FUNC extern inline
218 # else
219 # undef G_CAN_INLINE
220 # define G_INLINE_FUNC extern
221 # endif
222 # else /* !__GNUC__ */
223 # ifdef G_CAN_INLINE
224 # define G_INLINE_FUNC static inline
225 # else
226 # define G_INLINE_FUNC extern
227 # endif
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 )
245 # else
246 # if (defined (sun) || defined (__sun__))
247 # define G_STMT_START if (1)
248 # define G_STMT_END else (void)0
249 # else
250 # define G_STMT_START do
251 # define G_STMT_END while (0)
252 # endif
253 # endif
254 #endif
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
277 #define G_GNUC_CONST
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.
284 #ifdef __GNUC__
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.
296 #ifndef ATEXIT
297 # define ATEXIT(proc) g_ATEXIT(proc)
298 #else
299 # define G_NATIVE_ATEXIT
300 #endif /* 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).
319 #ifdef __DMALLOC_H__
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 ")", \
334 sizeof (type), \
335 sizeof (type) * (pre_alloc), \
336 (alloc_type)) \
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)); \
346 } G_STMT_END
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 */
363 #ifdef __GNUC__
365 #define g_assert(expr) G_STMT_START{ \
366 if (!(expr)) \
367 g_log (G_LOG_DOMAIN, \
368 G_LOG_LEVEL_ERROR, \
369 "file %s: line %d (%s): assertion failed: (%s)", \
370 __FILE__, \
371 __LINE__, \
372 __PRETTY_FUNCTION__, \
373 #expr); }G_STMT_END
375 #define g_assert_not_reached() G_STMT_START{ \
376 g_log (G_LOG_DOMAIN, \
377 G_LOG_LEVEL_ERROR, \
378 "file %s: line %d (%s): should not be reached", \
379 __FILE__, \
380 __LINE__, \
381 __PRETTY_FUNCTION__); }G_STMT_END
383 #else /* !__GNUC__ */
385 #define g_assert(expr) G_STMT_START{ \
386 if (!(expr)) \
387 g_log (G_LOG_DOMAIN, \
388 G_LOG_LEVEL_ERROR, \
389 "file %s: line %d: assertion failed: (%s)", \
390 __FILE__, \
391 __LINE__, \
392 #expr); }G_STMT_END
394 #define g_assert_not_reached() G_STMT_START{ \
395 g_log (G_LOG_DOMAIN, \
396 G_LOG_LEVEL_ERROR, \
397 "file %s: line %d: should not be reached", \
398 __FILE__, \
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 */
413 #ifdef __GNUC__
415 #define g_return_if_fail(expr) G_STMT_START{ \
416 if (!(expr)) \
418 g_log (G_LOG_DOMAIN, \
419 G_LOG_LEVEL_CRITICAL, \
420 "file %s: line %d (%s): assertion `%s' failed.", \
421 __FILE__, \
422 __LINE__, \
423 __PRETTY_FUNCTION__, \
424 #expr); \
425 return; \
426 }; }G_STMT_END
428 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
429 if (!(expr)) \
431 g_log (G_LOG_DOMAIN, \
432 G_LOG_LEVEL_CRITICAL, \
433 "file %s: line %d (%s): assertion `%s' failed.", \
434 __FILE__, \
435 __LINE__, \
436 __PRETTY_FUNCTION__, \
437 #expr); \
438 return val; \
439 }; }G_STMT_END
441 #else /* !__GNUC__ */
443 #define g_return_if_fail(expr) G_STMT_START{ \
444 if (!(expr)) \
446 g_log (G_LOG_DOMAIN, \
447 G_LOG_LEVEL_CRITICAL, \
448 "file %s: line %d: assertion `%s' failed.", \
449 __FILE__, \
450 __LINE__, \
451 #expr); \
452 return; \
453 }; }G_STMT_END
455 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
456 if (!(expr)) \
458 g_log (G_LOG_DOMAIN, \
459 G_LOG_LEVEL_CRITICAL, \
460 "file %s: line %d: assertion `%s' failed.", \
461 __FILE__, \
462 __LINE__, \
463 #expr); \
464 return val; \
465 }; }G_STMT_END
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.
479 typedef char gchar;
480 typedef short gshort;
481 typedef long glong;
482 typedef int gint;
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 */
503 #if 0
504 #ifdef HAVE_LONG_DOUBLE
505 typedef long double gldouble;
506 #else /* HAVE_LONG_DOUBLE */
507 typedef double gldouble;
508 #endif /* HAVE_LONG_DOUBLE */
509 #endif /* 0 */
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
524 * the below macros.
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) \
546 (__extension__ \
547 ({ register guint16 __v; \
548 if (__builtin_constant_p (val)) \
549 __v = GUINT16_SWAP_LE_BE_CONSTANT (val); \
550 else \
551 __asm__ __const__ ("rorw $8, %w0" \
552 : "=r" (__v) \
553 : "0" ((guint16) (val))); \
554 __v; }))
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) \
559 (__extension__ \
560 ({ register guint32 __v; \
561 if (__builtin_constant_p (val)) \
562 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
563 else \
564 __asm__ __const__ ("rorw $8, %w0\n\t" \
565 "rorl $16, %0\n\t" \
566 "rorw $8, %w0" \
567 : "=r" (__v) \
568 : "0" ((guint32) (val))); \
569 __v; }))
570 # else /* 486 and higher has bswap */
571 # define GUINT32_SWAP_LE_BE_X86(val) \
572 (__extension__ \
573 ({ register guint32 __v; \
574 if (__builtin_constant_p (val)) \
575 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
576 else \
577 __asm__ __const__ ("bswap %0" \
578 : "=r" (__v) \
579 : "0" ((guint32) (val))); \
580 __v; }))
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__ */
588 #ifdef G_HAVE_GINT64
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) \
608 (__extension__ \
609 ({ union { guint64 __ll; \
610 guint32 __l[2]; } __r; \
611 if (__builtin_constant_p (val)) \
612 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val); \
613 else \
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]); \
621 __r.__ll; }))
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))
625 # endif
626 #endif
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))
649 #ifdef G_HAVE_GINT64
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))
654 #endif
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))
675 /* Glib version.
676 * we prefix variable declarations so they can
677 * properly get exported in windows dlls.
679 #ifdef G_OS_WIN32
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 */
735 typedef enum
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
741 } GTraverseFlags;
743 /* Tree traverse orders */
744 typedef enum
746 G_IN_ORDER,
747 G_PRE_ORDER,
748 G_POST_ORDER,
749 G_LEVEL_ORDER
750 } GTraverseType;
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.
759 typedef enum
761 /* log 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)
774 } GLogLevelFlags;
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,
784 gconstpointer b);
785 typedef gchar* (*GCompletionFunc) (gpointer);
786 typedef void (*GDestroyNotify) (gpointer data);
787 typedef void (*GDataForeachFunc) (GQuark key_id,
788 gpointer data,
789 gpointer user_data);
790 typedef void (*GFunc) (gpointer data,
791 gpointer user_data);
792 typedef guint (*GHashFunc) (gconstpointer key);
793 typedef void (*GFreeFunc) (gpointer data);
794 typedef void (*GHFunc) (gpointer key,
795 gpointer value,
796 gpointer user_data);
797 typedef gboolean (*GHRFunc) (gpointer key,
798 gpointer value,
799 gpointer user_data);
800 typedef gint (*GHookCompareFunc) (GHook *new_hook,
801 GHook *sibling);
802 typedef gboolean (*GHookFindFunc) (GHook *hook,
803 gpointer data);
804 typedef void (*GHookMarshaller) (GHook *hook,
805 gpointer data);
806 typedef gboolean (*GHookCheckMarshaller) (GHook *hook,
807 gpointer data);
808 typedef void (*GHookFunc) (gpointer data);
809 typedef gboolean (*GHookCheckFunc) (gpointer data);
810 typedef void (*GHookFreeFunc) (GHookList *hook_list,
811 GHook *hook);
812 typedef void (*GLogFunc) (const gchar *log_domain,
813 GLogLevelFlags log_level,
814 const gchar *message,
815 gpointer user_data);
816 typedef gboolean (*GNodeTraverseFunc) (GNode *node,
817 gpointer data);
818 typedef void (*GNodeForeachFunc) (GNode *node,
819 gpointer data);
820 typedef gint (*GSearchFunc) (gpointer key,
821 gpointer data);
822 typedef void (*GScannerMsgFunc) (GScanner *scanner,
823 gchar *message,
824 gint error);
825 typedef gint (*GTraverseFunc) (gpointer key,
826 gpointer value,
827 gpointer data);
828 typedef void (*GVoidFunc) (void);
831 struct _GArray
833 gchar *data;
834 guint len;
837 struct _GByteArray
839 guint8 *data;
840 guint len;
843 struct _GDebugKey
845 gchar *key;
846 guint value;
849 struct _GList
851 gpointer data;
852 GList *next;
853 GList *prev;
856 struct _GPtrArray
858 gpointer *pdata;
859 guint len;
862 struct _GQueue
864 GList *head;
865 GList *tail;
866 guint length;
869 struct _GSList
871 gpointer data;
872 GSList *next;
875 struct _GString
877 gchar *str;
878 gint len;
881 struct _GTrashStack
883 GTrashStack *next;
886 struct _GTuples
888 guint len;
892 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
894 * 31 30 23 22 0
895 * +--------+---------------+---------------+
896 * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
897 * +--------+---------------+---------------+
898 * B0------------------->B1------->B2-->B3-->
900 * IEEE Standard 754 Double Precision Storage Format (gdouble):
902 * 63 62 52 51 32 31 0
903 * +--------+----------------+----------------+ +---------------+
904 * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
905 * +--------+----------------+----------------+ +---------------+
906 * B0--------------->B1---------->B2--->B3----> B4->B5->B6->B7->
908 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
909 #define G_IEEE754_FLOAT_BIAS (127)
910 #define G_IEEE754_DOUBLE_BIAS (1023)
911 /* multiply with base2 exponent to get base10 exponent (nomal numbers) */
912 #define G_LOG_2_BASE_10 (0.30102999566398119521)
913 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
914 union _GFloatIEEE754
916 gfloat v_float;
917 struct {
918 guint mantissa : 23;
919 guint biased_exponent : 8;
920 guint sign : 1;
921 } mpn;
923 union _GDoubleIEEE754
925 gdouble v_double;
926 struct {
927 guint mantissa_low : 32;
928 guint mantissa_high : 20;
929 guint biased_exponent : 11;
930 guint sign : 1;
931 } mpn;
933 #elif G_BYTE_ORDER == G_BIG_ENDIAN
934 union _GFloatIEEE754
936 gfloat v_float;
937 struct {
938 guint sign : 1;
939 guint biased_exponent : 8;
940 guint mantissa : 23;
941 } mpn;
943 union _GDoubleIEEE754
945 gdouble v_double;
946 struct {
947 guint sign : 1;
948 guint biased_exponent : 11;
949 guint mantissa_high : 20;
950 guint mantissa_low : 32;
951 } mpn;
953 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
954 #error unknown ENDIAN type
955 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
958 /* Doubly linked lists
960 void g_list_push_allocator (GAllocator *allocator);
961 void g_list_pop_allocator (void);
962 GList* g_list_alloc (void);
963 void g_list_free (GList *list);
964 void g_list_free_1 (GList *list);
965 GList* g_list_append (GList *list,
966 gpointer data);
967 GList* g_list_prepend (GList *list,
968 gpointer data);
969 GList* g_list_insert (GList *list,
970 gpointer data,
971 gint position);
972 GList* g_list_insert_sorted (GList *list,
973 gpointer data,
974 GCompareFunc func);
975 GList* g_list_concat (GList *list1,
976 GList *list2);
977 GList* g_list_remove (GList *list,
978 gconstpointer data);
979 GList* g_list_remove_link (GList *list,
980 GList *llink);
981 GList* g_list_delete_link (GList *list,
982 GList *link);
983 GList* g_list_reverse (GList *list);
984 GList* g_list_copy (GList *list);
985 GList* g_list_nth (GList *list,
986 guint n);
987 GList* g_list_find (GList *list,
988 gconstpointer data);
989 GList* g_list_find_custom (GList *list,
990 gconstpointer data,
991 GCompareFunc func);
992 gint g_list_position (GList *list,
993 GList *llink);
994 gint g_list_index (GList *list,
995 gconstpointer data);
996 GList* g_list_last (GList *list);
997 GList* g_list_first (GList *list);
998 guint g_list_length (GList *list);
999 void g_list_foreach (GList *list,
1000 GFunc func,
1001 gpointer user_data);
1002 GList* g_list_sort (GList *list,
1003 GCompareFunc compare_func);
1004 gpointer g_list_nth_data (GList *list,
1005 guint n);
1006 #define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
1007 #define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
1010 /* Singly linked lists
1012 void g_slist_push_allocator (GAllocator *allocator);
1013 void g_slist_pop_allocator (void);
1014 GSList* g_slist_alloc (void);
1015 void g_slist_free (GSList *list);
1016 void g_slist_free_1 (GSList *list);
1017 GSList* g_slist_append (GSList *list,
1018 gpointer data);
1019 GSList* g_slist_prepend (GSList *list,
1020 gpointer data);
1021 GSList* g_slist_insert (GSList *list,
1022 gpointer data,
1023 gint position);
1024 GSList* g_slist_insert_sorted (GSList *list,
1025 gpointer data,
1026 GCompareFunc func);
1027 GSList* g_slist_concat (GSList *list1,
1028 GSList *list2);
1029 GSList* g_slist_remove (GSList *list,
1030 gconstpointer data);
1031 GSList* g_slist_remove_link (GSList *list,
1032 GSList *link);
1033 GSList* g_slist_delete_link (GSList *list,
1034 GSList *link);
1035 GSList* g_slist_reverse (GSList *list);
1036 GSList* g_slist_copy (GSList *list);
1037 GSList* g_slist_nth (GSList *list,
1038 guint n);
1039 GSList* g_slist_find (GSList *list,
1040 gconstpointer data);
1041 GSList* g_slist_find_custom (GSList *list,
1042 gconstpointer data,
1043 GCompareFunc func);
1044 gint g_slist_position (GSList *list,
1045 GSList *llink);
1046 gint g_slist_index (GSList *list,
1047 gconstpointer data);
1048 GSList* g_slist_last (GSList *list);
1049 guint g_slist_length (GSList *list);
1050 void g_slist_foreach (GSList *list,
1051 GFunc func,
1052 gpointer user_data);
1053 GSList* g_slist_sort (GSList *list,
1054 GCompareFunc compare_func);
1055 gpointer g_slist_nth_data (GSList *list,
1056 guint n);
1057 #define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
1060 /* Queues
1062 GQueue* g_queue_new (void);
1063 void g_queue_free (GQueue *queue);
1064 void g_queue_push_head (GQueue *queue,
1065 gpointer data);
1066 void g_queue_push_tail (GQueue *queue,
1067 gpointer data);
1068 gpointer g_queue_pop_head (GQueue *queue);
1069 gpointer g_queue_pop_tail (GQueue *queue);
1070 gboolean g_queue_is_empty (GQueue *queue);
1071 gpointer g_queue_peek_head (GQueue *queue);
1072 gpointer g_queue_peek_tail (GQueue *queue);
1073 void g_queue_push_head_link (GQueue *queue,
1074 GList *link);
1075 void g_queue_push_tail_link (GQueue *queue,
1076 GList *link);
1077 GList* g_queue_pop_head_link (GQueue *queue);
1078 GList* g_queue_pop_tail_link (GQueue *queue);
1081 /* Hash tables
1083 GHashTable* g_hash_table_new (GHashFunc hash_func,
1084 GCompareFunc key_compare_func);
1085 void g_hash_table_destroy (GHashTable *hash_table);
1086 void g_hash_table_insert (GHashTable *hash_table,
1087 gpointer key,
1088 gpointer value);
1089 void g_hash_table_remove (GHashTable *hash_table,
1090 gconstpointer key);
1091 gpointer g_hash_table_lookup (GHashTable *hash_table,
1092 gconstpointer key);
1093 gboolean g_hash_table_lookup_extended(GHashTable *hash_table,
1094 gconstpointer lookup_key,
1095 gpointer *orig_key,
1096 gpointer *value);
1097 void g_hash_table_freeze (GHashTable *hash_table);
1098 void g_hash_table_thaw (GHashTable *hash_table);
1099 void g_hash_table_foreach (GHashTable *hash_table,
1100 GHFunc func,
1101 gpointer user_data);
1102 guint g_hash_table_foreach_remove (GHashTable *hash_table,
1103 GHRFunc func,
1104 gpointer user_data);
1105 guint g_hash_table_size (GHashTable *hash_table);
1108 /* Caches
1110 GCache* g_cache_new (GCacheNewFunc value_new_func,
1111 GCacheDestroyFunc value_destroy_func,
1112 GCacheDupFunc key_dup_func,
1113 GCacheDestroyFunc key_destroy_func,
1114 GHashFunc hash_key_func,
1115 GHashFunc hash_value_func,
1116 GCompareFunc key_compare_func);
1117 void g_cache_destroy (GCache *cache);
1118 gpointer g_cache_insert (GCache *cache,
1119 gpointer key);
1120 void g_cache_remove (GCache *cache,
1121 gpointer value);
1122 void g_cache_key_foreach (GCache *cache,
1123 GHFunc func,
1124 gpointer user_data);
1125 void g_cache_value_foreach (GCache *cache,
1126 GHFunc func,
1127 gpointer user_data);
1130 /* Balanced binary trees
1132 GTree* g_tree_new (GCompareFunc key_compare_func);
1133 void g_tree_destroy (GTree *tree);
1134 void g_tree_insert (GTree *tree,
1135 gpointer key,
1136 gpointer value);
1137 void g_tree_remove (GTree *tree,
1138 gpointer key);
1139 gpointer g_tree_lookup (GTree *tree,
1140 gpointer key);
1141 void g_tree_traverse (GTree *tree,
1142 GTraverseFunc traverse_func,
1143 GTraverseType traverse_type,
1144 gpointer data);
1145 gpointer g_tree_search (GTree *tree,
1146 GSearchFunc search_func,
1147 gpointer data);
1148 gint g_tree_height (GTree *tree);
1149 gint g_tree_nnodes (GTree *tree);
1153 /* N-way tree implementation
1155 struct _GNode
1157 gpointer data;
1158 GNode *next;
1159 GNode *prev;
1160 GNode *parent;
1161 GNode *children;
1164 #define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
1165 ((GNode*) (node))->prev == NULL && \
1166 ((GNode*) (node))->next == NULL)
1167 #define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
1169 void g_node_push_allocator (GAllocator *allocator);
1170 void g_node_pop_allocator (void);
1171 GNode* g_node_new (gpointer data);
1172 void g_node_destroy (GNode *root);
1173 void g_node_unlink (GNode *node);
1174 GNode* g_node_copy (GNode *node);
1175 GNode* g_node_insert (GNode *parent,
1176 gint position,
1177 GNode *node);
1178 GNode* g_node_insert_before (GNode *parent,
1179 GNode *sibling,
1180 GNode *node);
1181 GNode* g_node_prepend (GNode *parent,
1182 GNode *node);
1183 guint g_node_n_nodes (GNode *root,
1184 GTraverseFlags flags);
1185 GNode* g_node_get_root (GNode *node);
1186 gboolean g_node_is_ancestor (GNode *node,
1187 GNode *descendant);
1188 guint g_node_depth (GNode *node);
1189 GNode* g_node_find (GNode *root,
1190 GTraverseType order,
1191 GTraverseFlags flags,
1192 gpointer data);
1194 /* convenience macros */
1195 #define g_node_append(parent, node) \
1196 g_node_insert_before ((parent), NULL, (node))
1197 #define g_node_insert_data(parent, position, data) \
1198 g_node_insert ((parent), (position), g_node_new (data))
1199 #define g_node_insert_data_before(parent, sibling, data) \
1200 g_node_insert_before ((parent), (sibling), g_node_new (data))
1201 #define g_node_prepend_data(parent, data) \
1202 g_node_prepend ((parent), g_node_new (data))
1203 #define g_node_append_data(parent, data) \
1204 g_node_insert_before ((parent), NULL, g_node_new (data))
1206 /* traversal function, assumes that `node' is root
1207 * (only traverses `node' and its subtree).
1208 * this function is just a high level interface to
1209 * low level traversal functions, optimized for speed.
1211 void g_node_traverse (GNode *root,
1212 GTraverseType order,
1213 GTraverseFlags flags,
1214 gint max_depth,
1215 GNodeTraverseFunc func,
1216 gpointer data);
1218 /* return the maximum tree height starting with `node', this is an expensive
1219 * operation, since we need to visit all nodes. this could be shortened by
1220 * adding `guint height' to struct _GNode, but then again, this is not very
1221 * often needed, and would make g_node_insert() more time consuming.
1223 guint g_node_max_height (GNode *root);
1225 void g_node_children_foreach (GNode *node,
1226 GTraverseFlags flags,
1227 GNodeForeachFunc func,
1228 gpointer data);
1229 void g_node_reverse_children (GNode *node);
1230 guint g_node_n_children (GNode *node);
1231 GNode* g_node_nth_child (GNode *node,
1232 guint n);
1233 GNode* g_node_last_child (GNode *node);
1234 GNode* g_node_find_child (GNode *node,
1235 GTraverseFlags flags,
1236 gpointer data);
1237 gint g_node_child_position (GNode *node,
1238 GNode *child);
1239 gint g_node_child_index (GNode *node,
1240 gpointer data);
1242 GNode* g_node_first_sibling (GNode *node);
1243 GNode* g_node_last_sibling (GNode *node);
1245 #define g_node_prev_sibling(node) ((node) ? \
1246 ((GNode*) (node))->prev : NULL)
1247 #define g_node_next_sibling(node) ((node) ? \
1248 ((GNode*) (node))->next : NULL)
1249 #define g_node_first_child(node) ((node) ? \
1250 ((GNode*) (node))->children : NULL)
1253 /* Callback maintenance functions
1255 #define G_HOOK_FLAG_USER_SHIFT (4)
1256 typedef enum
1258 G_HOOK_FLAG_ACTIVE = 1 << 0,
1259 G_HOOK_FLAG_IN_CALL = 1 << 1,
1260 G_HOOK_FLAG_MASK = 0x0f
1261 } GHookFlagMask;
1263 #define G_HOOK_DEFERRED_DESTROY ((GHookFreeFunc) 0x01)
1265 struct _GHookList
1267 guint seq_id;
1268 guint hook_size;
1269 guint is_setup : 1;
1270 GHook *hooks;
1271 GMemChunk *hook_memchunk;
1272 GHookFreeFunc hook_free; /* virtual function */
1273 GHookFreeFunc hook_destroy; /* virtual function */
1276 struct _GHook
1278 gpointer data;
1279 GHook *next;
1280 GHook *prev;
1281 guint ref_count;
1282 guint hook_id;
1283 guint flags;
1284 gpointer func;
1285 GDestroyNotify destroy;
1288 #define G_HOOK_ACTIVE(hook) ((((GHook*) hook)->flags & \
1289 G_HOOK_FLAG_ACTIVE) != 0)
1290 #define G_HOOK_IN_CALL(hook) ((((GHook*) hook)->flags & \
1291 G_HOOK_FLAG_IN_CALL) != 0)
1292 #define G_HOOK_IS_VALID(hook) (((GHook*) hook)->hook_id != 0 && \
1293 G_HOOK_ACTIVE (hook))
1294 #define G_HOOK_IS_UNLINKED(hook) (((GHook*) hook)->next == NULL && \
1295 ((GHook*) hook)->prev == NULL && \
1296 ((GHook*) hook)->hook_id == 0 && \
1297 ((GHook*) hook)->ref_count == 0)
1299 void g_hook_list_init (GHookList *hook_list,
1300 guint hook_size);
1301 void g_hook_list_clear (GHookList *hook_list);
1302 GHook* g_hook_alloc (GHookList *hook_list);
1303 void g_hook_free (GHookList *hook_list,
1304 GHook *hook);
1305 void g_hook_ref (GHookList *hook_list,
1306 GHook *hook);
1307 void g_hook_unref (GHookList *hook_list,
1308 GHook *hook);
1309 gboolean g_hook_destroy (GHookList *hook_list,
1310 guint hook_id);
1311 void g_hook_destroy_link (GHookList *hook_list,
1312 GHook *hook);
1313 void g_hook_prepend (GHookList *hook_list,
1314 GHook *hook);
1315 void g_hook_insert_before (GHookList *hook_list,
1316 GHook *sibling,
1317 GHook *hook);
1318 void g_hook_insert_sorted (GHookList *hook_list,
1319 GHook *hook,
1320 GHookCompareFunc func);
1321 GHook* g_hook_get (GHookList *hook_list,
1322 guint hook_id);
1323 GHook* g_hook_find (GHookList *hook_list,
1324 gboolean need_valids,
1325 GHookFindFunc func,
1326 gpointer data);
1327 GHook* g_hook_find_data (GHookList *hook_list,
1328 gboolean need_valids,
1329 gpointer data);
1330 GHook* g_hook_find_func (GHookList *hook_list,
1331 gboolean need_valids,
1332 gpointer func);
1333 GHook* g_hook_find_func_data (GHookList *hook_list,
1334 gboolean need_valids,
1335 gpointer func,
1336 gpointer data);
1337 /* return the first valid hook, and increment its reference count */
1338 GHook* g_hook_first_valid (GHookList *hook_list,
1339 gboolean may_be_in_call);
1340 /* return the next valid hook with incremented reference count, and
1341 * decrement the reference count of the original hook
1343 GHook* g_hook_next_valid (GHookList *hook_list,
1344 GHook *hook,
1345 gboolean may_be_in_call);
1347 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1348 gint g_hook_compare_ids (GHook *new_hook,
1349 GHook *sibling);
1351 /* convenience macros */
1352 #define g_hook_append( hook_list, hook ) \
1353 g_hook_insert_before ((hook_list), NULL, (hook))
1355 /* invoke all valid hooks with the (*GHookFunc) signature.
1357 void g_hook_list_invoke (GHookList *hook_list,
1358 gboolean may_recurse);
1359 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1360 * and destroy the hook if FALSE is returned.
1362 void g_hook_list_invoke_check (GHookList *hook_list,
1363 gboolean may_recurse);
1364 /* invoke a marshaller on all valid hooks.
1366 void g_hook_list_marshal (GHookList *hook_list,
1367 gboolean may_recurse,
1368 GHookMarshaller marshaller,
1369 gpointer data);
1370 void g_hook_list_marshal_check (GHookList *hook_list,
1371 gboolean may_recurse,
1372 GHookCheckMarshaller marshaller,
1373 gpointer data);
1376 /* Fatal error handlers.
1377 * g_on_error_query() will prompt the user to either
1378 * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1379 * g_on_error_stack_trace() invokes gdb, which attaches to the current
1380 * process and shows a stack trace.
1381 * These function may cause different actions on non-unix platforms.
1382 * The prg_name arg is required by gdb to find the executable, if it is
1383 * passed as NULL, g_on_error_query() will try g_get_prgname().
1385 void g_on_error_query (const gchar *prg_name);
1386 void g_on_error_stack_trace (const gchar *prg_name);
1389 /* Logging mechanism
1391 extern const gchar *g_log_domain_glib;
1392 guint g_log_set_handler (const gchar *log_domain,
1393 GLogLevelFlags log_levels,
1394 GLogFunc log_func,
1395 gpointer user_data);
1396 void g_log_remove_handler (const gchar *log_domain,
1397 guint handler_id);
1398 void g_log_default_handler (const gchar *log_domain,
1399 GLogLevelFlags log_level,
1400 const gchar *message,
1401 gpointer unused_data);
1402 void g_log (const gchar *log_domain,
1403 GLogLevelFlags log_level,
1404 const gchar *format,
1405 ...) G_GNUC_PRINTF (3, 4);
1406 void g_logv (const gchar *log_domain,
1407 GLogLevelFlags log_level,
1408 const gchar *format,
1409 va_list args);
1410 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
1411 GLogLevelFlags fatal_mask);
1412 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
1413 #ifndef G_LOG_DOMAIN
1414 #define G_LOG_DOMAIN ((gchar*) 0)
1415 #endif /* G_LOG_DOMAIN */
1416 #ifdef __GNUC__
1417 #define g_error(format, args...) g_log (G_LOG_DOMAIN, \
1418 G_LOG_LEVEL_ERROR, \
1419 format, ##args)
1420 #define g_message(format, args...) g_log (G_LOG_DOMAIN, \
1421 G_LOG_LEVEL_MESSAGE, \
1422 format, ##args)
1423 #define g_warning(format, args...) g_log (G_LOG_DOMAIN, \
1424 G_LOG_LEVEL_WARNING, \
1425 format, ##args)
1426 #else /* !__GNUC__ */
1427 static void
1428 g_error (const gchar *format,
1429 ...)
1431 va_list args;
1432 va_start (args, format);
1433 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1434 va_end (args);
1436 static void
1437 g_message (const gchar *format,
1438 ...)
1440 va_list args;
1441 va_start (args, format);
1442 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1443 va_end (args);
1445 static void
1446 g_warning (const gchar *format,
1447 ...)
1449 va_list args;
1450 va_start (args, format);
1451 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1452 va_end (args);
1454 #endif /* !__GNUC__ */
1456 typedef void (*GPrintFunc) (const gchar *string);
1457 void g_print (const gchar *format,
1458 ...) G_GNUC_PRINTF (1, 2);
1459 GPrintFunc g_set_print_handler (GPrintFunc func);
1460 void g_printerr (const gchar *format,
1461 ...) G_GNUC_PRINTF (1, 2);
1462 GPrintFunc g_set_printerr_handler (GPrintFunc func);
1464 /* deprecated compatibility functions, use g_log_set_handler() instead */
1465 typedef void (*GErrorFunc) (const gchar *str);
1466 typedef void (*GWarningFunc) (const gchar *str);
1467 GErrorFunc g_set_error_handler (GErrorFunc func);
1468 GWarningFunc g_set_warning_handler (GWarningFunc func);
1469 GPrintFunc g_set_message_handler (GPrintFunc func);
1472 /* Memory allocation and debugging
1474 #ifdef USE_DMALLOC
1476 #define g_malloc(size) ((gpointer) MALLOC (size))
1477 #define g_malloc0(size) ((gpointer) CALLOC (char, size))
1478 #define g_realloc(mem,size) ((gpointer) REALLOC (mem, char, size))
1479 #define g_free(mem) FREE (mem)
1481 #else /* !USE_DMALLOC */
1483 gpointer g_malloc (gulong size);
1484 gpointer g_malloc0 (gulong size);
1485 gpointer g_realloc (gpointer mem,
1486 gulong size);
1487 void g_free (gpointer mem);
1489 #endif /* !USE_DMALLOC */
1491 void g_mem_profile (void);
1492 void g_mem_check (gpointer mem);
1494 /* Generic allocators
1496 GAllocator* g_allocator_new (const gchar *name,
1497 guint n_preallocs);
1498 void g_allocator_free (GAllocator *allocator);
1500 #define G_ALLOCATOR_LIST (1)
1501 #define G_ALLOCATOR_SLIST (2)
1502 #define G_ALLOCATOR_NODE (3)
1505 /* "g_mem_chunk_new" creates a new memory chunk.
1506 * Memory chunks are used to allocate pieces of memory which are
1507 * always the same size. Lists are a good example of such a data type.
1508 * The memory chunk allocates and frees blocks of memory as needed.
1509 * Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1510 * allocated in a mem chunk. ("g_free" will most likely cause a seg
1511 * fault...somewhere).
1513 * Oh yeah, GMemChunk is an opaque data type. (You don't really
1514 * want to know what's going on inside do you?)
1517 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1518 * is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1519 * atom. (They are also useful for lists which use MemChunk to allocate
1520 * memory but are also part of the MemChunk implementation).
1521 * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1524 #define G_ALLOC_ONLY 1
1525 #define G_ALLOC_AND_FREE 2
1527 GMemChunk* g_mem_chunk_new (gchar *name,
1528 gint atom_size,
1529 gulong area_size,
1530 gint type);
1531 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
1532 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
1533 gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
1534 void g_mem_chunk_free (GMemChunk *mem_chunk,
1535 gpointer mem);
1536 void g_mem_chunk_clean (GMemChunk *mem_chunk);
1537 void g_mem_chunk_reset (GMemChunk *mem_chunk);
1538 void g_mem_chunk_print (GMemChunk *mem_chunk);
1539 void g_mem_chunk_info (void);
1541 /* Ah yes...we have a "g_blow_chunks" function.
1542 * "g_blow_chunks" simply compresses all the chunks. This operation
1543 * consists of freeing every memory area that should be freed (but
1544 * which we haven't gotten around to doing yet). And, no,
1545 * "g_blow_chunks" doesn't follow the naming scheme, but it is a
1546 * much better name than "g_mem_chunk_clean_all" or something
1547 * similar.
1549 void g_blow_chunks (void);
1552 /* Timer
1555 #define G_MICROSEC 1000000
1557 GTimer* g_timer_new (void);
1558 void g_timer_destroy (GTimer *timer);
1559 void g_timer_start (GTimer *timer);
1560 void g_timer_stop (GTimer *timer);
1561 void g_timer_reset (GTimer *timer);
1562 gdouble g_timer_elapsed (GTimer *timer,
1563 gulong *microseconds);
1564 void g_usleep (gulong microseconds);
1566 /* String utility functions that modify a string argument or
1567 * return a constant string that must not be freed.
1569 #define G_STR_DELIMITERS "_-|> <."
1570 gchar* g_strdelimit (gchar *string,
1571 const gchar *delimiters,
1572 gchar new_delimiter);
1573 gdouble g_strtod (const gchar *nptr,
1574 gchar **endptr);
1575 gchar* g_strerror (gint errnum);
1576 gchar* g_strsignal (gint signum);
1577 gint g_strcasecmp (const gchar *s1,
1578 const gchar *s2);
1579 gint g_strncasecmp (const gchar *s1,
1580 const gchar *s2,
1581 guint n);
1582 void g_strdown (gchar *string);
1583 void g_strup (gchar *string);
1584 void g_strreverse (gchar *string);
1585 /* removes leading spaces */
1586 gchar* g_strchug (gchar *string);
1587 /* removes trailing spaces */
1588 gchar* g_strchomp (gchar *string);
1589 /* removes leading & trailing spaces */
1590 #define g_strstrip( string ) g_strchomp (g_strchug (string))
1592 /* String utility functions that return a newly allocated string which
1593 * ought to be freed with g_free from the caller at some point.
1595 gchar* g_strdup (const gchar *str);
1596 gchar* g_strdup_printf (const gchar *format,
1597 ...) G_GNUC_PRINTF (1, 2);
1598 gchar* g_strdup_vprintf (const gchar *format,
1599 va_list args);
1600 gchar* g_strndup (const gchar *str,
1601 guint n);
1602 gchar* g_strnfill (guint length,
1603 gchar fill_char);
1604 gchar* g_strconcat (const gchar *string1,
1605 ...); /* NULL terminated */
1606 gchar* g_strjoin (const gchar *separator,
1607 ...); /* NULL terminated */
1608 /* Make a copy of a string interpreting C string -style escape
1609 * sequences. Inverse of g_strescape. The recognized sequences are \b
1610 * \f \n \r \t \\ \" and the octal format.
1612 gchar* g_strcompress (const gchar *source);
1614 /* Convert between the operating system (or C runtime)
1615 * representation of file names and UTF-8.
1617 gchar* g_filename_to_utf8 (const gchar *opsysstring);
1618 gchar* g_filename_from_utf8 (const gchar *utf8string);
1620 /* Copy a string escaping nonprintable characters like in C strings.
1621 * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
1622 * to a string containing characters that are not to be escaped.
1624 * Deprecated API: gchar* g_strescape (const gchar *source);
1625 * Luckily this function wasn't used much, using NULL as second parameter
1626 * provides mostly identical semantics.
1628 gchar* g_strescape (const gchar *source,
1629 const gchar *exceptions);
1631 gpointer g_memdup (gconstpointer mem,
1632 guint byte_size);
1634 /* NULL terminated string arrays.
1635 * g_strsplit() splits up string into max_tokens tokens at delim and
1636 * returns a newly allocated string array.
1637 * g_strjoinv() concatenates all of str_array's strings, sliding in an
1638 * optional separator, the returned string is newly allocated.
1639 * g_strfreev() frees the array itself and all of its strings.
1641 gchar** g_strsplit (const gchar *string,
1642 const gchar *delimiter,
1643 gint max_tokens);
1644 gchar* g_strjoinv (const gchar *separator,
1645 gchar **str_array);
1646 void g_strfreev (gchar **str_array);
1650 /* calculate a string size, guarranteed to fit format + args.
1652 guint g_printf_string_upper_bound (const gchar* format,
1653 va_list args);
1656 /* Retrive static string info
1658 gchar* g_get_user_name (void);
1659 gchar* g_get_real_name (void);
1660 gchar* g_get_home_dir (void);
1661 gchar* g_get_tmp_dir (void);
1662 gchar* g_get_prgname (void);
1663 void g_set_prgname (const gchar *prgname);
1666 /* Miscellaneous utility functions
1668 guint g_parse_debug_string (const gchar *string,
1669 GDebugKey *keys,
1670 guint nkeys);
1671 gint g_snprintf (gchar *string,
1672 gulong n,
1673 gchar const *format,
1674 ...) G_GNUC_PRINTF (3, 4);
1675 gint g_vsnprintf (gchar *string,
1676 gulong n,
1677 gchar const *format,
1678 va_list args);
1679 gchar* g_basename (const gchar *file_name);
1680 /* Check if a file name is an absolute path */
1681 gboolean g_path_is_absolute (const gchar *file_name);
1682 /* In case of absolute paths, skip the root part */
1683 gchar* g_path_skip_root (gchar *file_name);
1685 /* strings are newly allocated with g_malloc() */
1686 gchar* g_dirname (const gchar *file_name);
1687 gchar* g_get_current_dir (void);
1689 /* return the environment string for the variable. The returned memory
1690 * must not be freed. */
1691 gchar* g_getenv (const gchar *variable);
1693 /* we use a GLib function as a replacement for ATEXIT, so
1694 * the programmer is not required to check the return value
1695 * (if there is any in the implementation) and doesn't encounter
1696 * missing include files.
1698 void g_atexit (GVoidFunc func);
1701 /* Bit tests
1703 G_INLINE_FUNC gint g_bit_nth_lsf (guint32 mask,
1704 gint nth_bit);
1705 #ifdef G_CAN_INLINE
1706 G_INLINE_FUNC gint
1707 g_bit_nth_lsf (guint32 mask,
1708 gint nth_bit)
1712 nth_bit++;
1713 if (mask & (1 << (guint) nth_bit))
1714 return nth_bit;
1716 while (nth_bit < 32);
1717 return -1;
1719 #endif /* G_CAN_INLINE */
1721 G_INLINE_FUNC gint g_bit_nth_msf (guint32 mask,
1722 gint nth_bit);
1723 #ifdef G_CAN_INLINE
1724 G_INLINE_FUNC gint
1725 g_bit_nth_msf (guint32 mask,
1726 gint nth_bit)
1728 if (nth_bit < 0)
1729 nth_bit = 32;
1732 nth_bit--;
1733 if (mask & (1 << (guint) nth_bit))
1734 return nth_bit;
1736 while (nth_bit > 0);
1737 return -1;
1739 #endif /* G_CAN_INLINE */
1741 G_INLINE_FUNC guint g_bit_storage (guint number);
1742 #ifdef G_CAN_INLINE
1743 G_INLINE_FUNC guint
1744 g_bit_storage (guint number)
1746 register guint n_bits = 0;
1750 n_bits++;
1751 number >>= 1;
1753 while (number);
1754 return n_bits;
1756 #endif /* G_CAN_INLINE */
1759 /* Trash Stacks
1760 * elements need to be >= sizeof (gpointer)
1762 G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p,
1763 gpointer data_p);
1764 #ifdef G_CAN_INLINE
1765 G_INLINE_FUNC void
1766 g_trash_stack_push (GTrashStack **stack_p,
1767 gpointer data_p)
1769 GTrashStack *data = (GTrashStack *) data_p;
1771 data->next = *stack_p;
1772 *stack_p = data;
1774 #endif /* G_CAN_INLINE */
1776 G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p);
1777 #ifdef G_CAN_INLINE
1778 G_INLINE_FUNC gpointer
1779 g_trash_stack_pop (GTrashStack **stack_p)
1781 GTrashStack *data;
1783 data = *stack_p;
1784 if (data)
1786 *stack_p = data->next;
1787 /* NULLify private pointer here, most platforms store NULL as
1788 * subsequent 0 bytes
1790 data->next = NULL;
1793 return data;
1795 #endif /* G_CAN_INLINE */
1797 G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p);
1798 #ifdef G_CAN_INLINE
1799 G_INLINE_FUNC gpointer
1800 g_trash_stack_peek (GTrashStack **stack_p)
1802 GTrashStack *data;
1804 data = *stack_p;
1806 return data;
1808 #endif /* G_CAN_INLINE */
1810 G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p);
1811 #ifdef G_CAN_INLINE
1812 G_INLINE_FUNC guint
1813 g_trash_stack_height (GTrashStack **stack_p)
1815 GTrashStack *data;
1816 guint i = 0;
1818 for (data = *stack_p; data; data = data->next)
1819 i++;
1821 return i;
1823 #endif /* G_CAN_INLINE */
1826 /* String Chunks
1828 GStringChunk* g_string_chunk_new (gint size);
1829 void g_string_chunk_free (GStringChunk *chunk);
1830 gchar* g_string_chunk_insert (GStringChunk *chunk,
1831 const gchar *string);
1832 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
1833 const gchar *string);
1836 /* Strings
1838 GString* g_string_new (const gchar *init);
1839 GString* g_string_sized_new (guint dfl_size);
1840 void g_string_free (GString *string,
1841 gboolean free_segment);
1842 GString* g_string_assign (GString *string,
1843 const gchar *rval);
1844 GString* g_string_truncate (GString *string,
1845 gint len);
1846 GString* g_string_insert_len (GString *string,
1847 gint pos,
1848 const gchar *val,
1849 gint len);
1850 GString* g_string_append (GString *string,
1851 const gchar *val);
1852 GString* g_string_append_len (GString *string,
1853 const gchar *val,
1854 gint len);
1855 GString* g_string_append_c (GString *string,
1856 gchar c);
1857 GString* g_string_prepend (GString *string,
1858 const gchar *val);
1859 GString* g_string_prepend_c (GString *string,
1860 gchar c);
1861 GString* g_string_prepend_len (GString *string,
1862 const gchar *val,
1863 gint len);
1864 GString* g_string_insert (GString *string,
1865 gint pos,
1866 const gchar *val);
1867 GString* g_string_insert_c (GString *string,
1868 gint pos,
1869 gchar c);
1870 GString* g_string_erase (GString *string,
1871 gint pos,
1872 gint len);
1873 GString* g_string_down (GString *string);
1874 GString* g_string_up (GString *string);
1875 void g_string_sprintf (GString *string,
1876 const gchar *format,
1877 ...) G_GNUC_PRINTF (2, 3);
1878 void g_string_sprintfa (GString *string,
1879 const gchar *format,
1880 ...) G_GNUC_PRINTF (2, 3);
1883 /* Resizable arrays, remove fills any cleared spot and shortens the
1884 * array, while preserving the order. remove_fast will distort the
1885 * order by moving the last element to the position of the removed
1888 #define g_array_append_val(a,v) g_array_append_vals (a, &v, 1)
1889 #define g_array_prepend_val(a,v) g_array_prepend_vals (a, &v, 1)
1890 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
1891 #define g_array_index(a,t,i) (((t*) (a)->data) [(i)])
1893 GArray* g_array_new (gboolean zero_terminated,
1894 gboolean clear,
1895 guint element_size);
1896 GArray* g_array_sized_new (gboolean zero_terminated,
1897 gboolean clear,
1898 guint element_size,
1899 guint reserved_size);
1900 void g_array_free (GArray *array,
1901 gboolean free_segment);
1902 GArray* g_array_append_vals (GArray *array,
1903 gconstpointer data,
1904 guint len);
1905 GArray* g_array_prepend_vals (GArray *array,
1906 gconstpointer data,
1907 guint len);
1908 GArray* g_array_insert_vals (GArray *array,
1909 guint index,
1910 gconstpointer data,
1911 guint len);
1912 GArray* g_array_set_size (GArray *array,
1913 guint length);
1914 GArray* g_array_remove_index (GArray *array,
1915 guint index);
1916 GArray* g_array_remove_index_fast (GArray *array,
1917 guint index);
1919 /* Resizable pointer array. This interface is much less complicated
1920 * than the above. Add appends appends a pointer. Remove fills any
1921 * cleared spot and shortens the array. remove_fast will again distort
1922 * order.
1924 #define g_ptr_array_index(array,index) (array->pdata)[index]
1925 GPtrArray* g_ptr_array_new (void);
1926 GPtrArray* g_ptr_array_sized_new (guint reserved_size);
1927 void g_ptr_array_free (GPtrArray *array,
1928 gboolean free_seg);
1929 void g_ptr_array_set_size (GPtrArray *array,
1930 gint length);
1931 gpointer g_ptr_array_remove_index (GPtrArray *array,
1932 guint index);
1933 gpointer g_ptr_array_remove_index_fast (GPtrArray *array,
1934 guint index);
1935 gboolean g_ptr_array_remove (GPtrArray *array,
1936 gpointer data);
1937 gboolean g_ptr_array_remove_fast (GPtrArray *array,
1938 gpointer data);
1939 void g_ptr_array_add (GPtrArray *array,
1940 gpointer data);
1942 /* Byte arrays, an array of guint8. Implemented as a GArray,
1943 * but type-safe.
1946 GByteArray* g_byte_array_new (void);
1947 GByteArray* g_byte_array_sized_new (guint reserved_size);
1948 void g_byte_array_free (GByteArray *array,
1949 gboolean free_segment);
1950 GByteArray* g_byte_array_append (GByteArray *array,
1951 const guint8 *data,
1952 guint len);
1953 GByteArray* g_byte_array_prepend (GByteArray *array,
1954 const guint8 *data,
1955 guint len);
1956 GByteArray* g_byte_array_set_size (GByteArray *array,
1957 guint length);
1958 GByteArray* g_byte_array_remove_index (GByteArray *array,
1959 guint index);
1960 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1961 guint index);
1964 /* Hash Functions
1966 gint g_str_equal (gconstpointer v,
1967 gconstpointer v2);
1968 guint g_str_hash (gconstpointer v);
1970 gint g_int_equal (gconstpointer v,
1971 gconstpointer v2);
1972 guint g_int_hash (gconstpointer v);
1974 /* This "hash" function will just return the key's adress as an
1975 * unsigned integer. Useful for hashing on plain adresses or
1976 * simple integer values.
1977 * passing NULL into g_hash_table_new() as GHashFunc has the
1978 * same effect as passing g_direct_hash().
1980 guint g_direct_hash (gconstpointer v);
1981 gint g_direct_equal (gconstpointer v,
1982 gconstpointer v2);
1985 /* Quarks (string<->id association)
1987 GQuark g_quark_try_string (const gchar *string);
1988 GQuark g_quark_from_static_string (const gchar *string);
1989 GQuark g_quark_from_string (const gchar *string);
1990 gchar* g_quark_to_string (GQuark quark);
1993 /* Keyed Data List
1995 void g_datalist_init (GData **datalist);
1996 void g_datalist_clear (GData **datalist);
1997 gpointer g_datalist_id_get_data (GData **datalist,
1998 GQuark key_id);
1999 void g_datalist_id_set_data_full (GData **datalist,
2000 GQuark key_id,
2001 gpointer data,
2002 GDestroyNotify destroy_func);
2003 gpointer g_datalist_id_remove_no_notify (GData **datalist,
2004 GQuark key_id);
2005 void g_datalist_foreach (GData **datalist,
2006 GDataForeachFunc func,
2007 gpointer user_data);
2008 #define g_datalist_id_set_data(dl, q, d) \
2009 g_datalist_id_set_data_full ((dl), (q), (d), NULL)
2010 #define g_datalist_id_remove_data(dl, q) \
2011 g_datalist_id_set_data ((dl), (q), NULL)
2012 #define g_datalist_get_data(dl, k) \
2013 (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
2014 #define g_datalist_set_data_full(dl, k, d, f) \
2015 g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
2016 #define g_datalist_remove_no_notify(dl, k) \
2017 g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
2018 #define g_datalist_set_data(dl, k, d) \
2019 g_datalist_set_data_full ((dl), (k), (d), NULL)
2020 #define g_datalist_remove_data(dl, k) \
2021 g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
2024 /* Location Associated Keyed Data
2026 void g_dataset_destroy (gconstpointer dataset_location);
2027 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
2028 GQuark key_id);
2029 void g_dataset_id_set_data_full (gconstpointer dataset_location,
2030 GQuark key_id,
2031 gpointer data,
2032 GDestroyNotify destroy_func);
2033 gpointer g_dataset_id_remove_no_notify (gconstpointer dataset_location,
2034 GQuark key_id);
2035 void g_dataset_foreach (gconstpointer dataset_location,
2036 GDataForeachFunc func,
2037 gpointer user_data);
2038 #define g_dataset_id_set_data(l, k, d) \
2039 g_dataset_id_set_data_full ((l), (k), (d), NULL)
2040 #define g_dataset_id_remove_data(l, k) \
2041 g_dataset_id_set_data ((l), (k), NULL)
2042 #define g_dataset_get_data(l, k) \
2043 (g_dataset_id_get_data ((l), g_quark_try_string (k)))
2044 #define g_dataset_set_data_full(l, k, d, f) \
2045 g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
2046 #define g_dataset_remove_no_notify(l, k) \
2047 g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
2048 #define g_dataset_set_data(l, k, d) \
2049 g_dataset_set_data_full ((l), (k), (d), NULL)
2050 #define g_dataset_remove_data(l, k) \
2051 g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
2054 /* GScanner: Flexible lexical scanner for general purpose.
2057 /* Character sets */
2058 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2059 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
2060 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
2061 "\307\310\311\312\313\314\315\316\317\320"\
2062 "\321\322\323\324\325\326"\
2063 "\330\331\332\333\334\335\336"
2064 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
2065 "\347\350\351\352\353\354\355\356\357\360"\
2066 "\361\362\363\364\365\366"\
2067 "\370\371\372\373\374\375\376\377"
2069 /* Error types */
2070 typedef enum
2072 G_ERR_UNKNOWN,
2073 G_ERR_UNEXP_EOF,
2074 G_ERR_UNEXP_EOF_IN_STRING,
2075 G_ERR_UNEXP_EOF_IN_COMMENT,
2076 G_ERR_NON_DIGIT_IN_CONST,
2077 G_ERR_DIGIT_RADIX,
2078 G_ERR_FLOAT_RADIX,
2079 G_ERR_FLOAT_MALFORMED
2080 } GErrorType;
2082 /* Token types */
2083 typedef enum
2085 G_TOKEN_EOF = 0,
2087 G_TOKEN_LEFT_PAREN = '(',
2088 G_TOKEN_RIGHT_PAREN = ')',
2089 G_TOKEN_LEFT_CURLY = '{',
2090 G_TOKEN_RIGHT_CURLY = '}',
2091 G_TOKEN_LEFT_BRACE = '[',
2092 G_TOKEN_RIGHT_BRACE = ']',
2093 G_TOKEN_EQUAL_SIGN = '=',
2094 G_TOKEN_COMMA = ',',
2096 G_TOKEN_NONE = 256,
2098 G_TOKEN_ERROR,
2100 G_TOKEN_CHAR,
2101 G_TOKEN_BINARY,
2102 G_TOKEN_OCTAL,
2103 G_TOKEN_INT,
2104 G_TOKEN_HEX,
2105 G_TOKEN_FLOAT,
2106 G_TOKEN_STRING,
2108 G_TOKEN_SYMBOL,
2109 G_TOKEN_IDENTIFIER,
2110 G_TOKEN_IDENTIFIER_NULL,
2112 G_TOKEN_COMMENT_SINGLE,
2113 G_TOKEN_COMMENT_MULTI,
2114 G_TOKEN_LAST
2115 } GTokenType;
2117 union _GTokenValue
2119 gpointer v_symbol;
2120 gchar *v_identifier;
2121 gulong v_binary;
2122 gulong v_octal;
2123 gulong v_int;
2124 gdouble v_float;
2125 gulong v_hex;
2126 gchar *v_string;
2127 gchar *v_comment;
2128 guchar v_char;
2129 guint v_error;
2132 struct _GScannerConfig
2134 /* Character sets
2136 gchar *cset_skip_characters; /* default: " \t\n" */
2137 gchar *cset_identifier_first;
2138 gchar *cset_identifier_nth;
2139 gchar *cpair_comment_single; /* default: "#\n" */
2141 /* Should symbol lookup work case sensitive?
2143 guint case_sensitive : 1;
2145 /* Boolean values to be adjusted "on the fly"
2146 * to configure scanning behaviour.
2148 guint skip_comment_multi : 1; /* C like comment */
2149 guint skip_comment_single : 1; /* single line comment */
2150 guint scan_comment_multi : 1; /* scan multi line comments? */
2151 guint scan_identifier : 1;
2152 guint scan_identifier_1char : 1;
2153 guint scan_identifier_NULL : 1;
2154 guint scan_symbols : 1;
2155 guint scan_binary : 1;
2156 guint scan_octal : 1;
2157 guint scan_float : 1;
2158 guint scan_hex : 1; /* `0x0ff0' */
2159 guint scan_hex_dollar : 1; /* `$0ff0' */
2160 guint scan_string_sq : 1; /* string: 'anything' */
2161 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
2162 guint numbers_2_int : 1; /* bin, octal, hex => int */
2163 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
2164 guint identifier_2_string : 1;
2165 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
2166 guint symbol_2_token : 1;
2167 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
2170 struct _GScanner
2172 /* unused fields */
2173 gpointer user_data;
2174 guint max_parse_errors;
2176 /* g_scanner_error() increments this field */
2177 guint parse_errors;
2179 /* name of input stream, featured by the default message handler */
2180 const gchar *input_name;
2182 /* data pointer for derived structures */
2183 gpointer derived_data;
2185 /* link into the scanner configuration */
2186 GScannerConfig *config;
2188 /* fields filled in after g_scanner_get_next_token() */
2189 GTokenType token;
2190 GTokenValue value;
2191 guint line;
2192 guint position;
2194 /* fields filled in after g_scanner_peek_next_token() */
2195 GTokenType next_token;
2196 GTokenValue next_value;
2197 guint next_line;
2198 guint next_position;
2200 /* to be considered private */
2201 GHashTable *symbol_table;
2202 gint input_fd;
2203 const gchar *text;
2204 const gchar *text_end;
2205 gchar *buffer;
2206 guint scope_id;
2208 /* handler function for _warn and _error */
2209 GScannerMsgFunc msg_handler;
2212 GScanner* g_scanner_new (GScannerConfig *config_templ);
2213 void g_scanner_destroy (GScanner *scanner);
2214 void g_scanner_input_file (GScanner *scanner,
2215 gint input_fd);
2216 void g_scanner_sync_file_offset (GScanner *scanner);
2217 void g_scanner_input_text (GScanner *scanner,
2218 const gchar *text,
2219 guint text_len);
2220 GTokenType g_scanner_get_next_token (GScanner *scanner);
2221 GTokenType g_scanner_peek_next_token (GScanner *scanner);
2222 GTokenType g_scanner_cur_token (GScanner *scanner);
2223 GTokenValue g_scanner_cur_value (GScanner *scanner);
2224 guint g_scanner_cur_line (GScanner *scanner);
2225 guint g_scanner_cur_position (GScanner *scanner);
2226 gboolean g_scanner_eof (GScanner *scanner);
2227 guint g_scanner_set_scope (GScanner *scanner,
2228 guint scope_id);
2229 void g_scanner_scope_add_symbol (GScanner *scanner,
2230 guint scope_id,
2231 const gchar *symbol,
2232 gpointer value);
2233 void g_scanner_scope_remove_symbol (GScanner *scanner,
2234 guint scope_id,
2235 const gchar *symbol);
2236 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
2237 guint scope_id,
2238 const gchar *symbol);
2239 void g_scanner_scope_foreach_symbol (GScanner *scanner,
2240 guint scope_id,
2241 GHFunc func,
2242 gpointer user_data);
2243 gpointer g_scanner_lookup_symbol (GScanner *scanner,
2244 const gchar *symbol);
2245 void g_scanner_freeze_symbol_table (GScanner *scanner);
2246 void g_scanner_thaw_symbol_table (GScanner *scanner);
2247 void g_scanner_unexp_token (GScanner *scanner,
2248 GTokenType expected_token,
2249 const gchar *identifier_spec,
2250 const gchar *symbol_spec,
2251 const gchar *symbol_name,
2252 const gchar *message,
2253 gint is_error);
2254 void g_scanner_error (GScanner *scanner,
2255 const gchar *format,
2256 ...) G_GNUC_PRINTF (2,3);
2257 void g_scanner_warn (GScanner *scanner,
2258 const gchar *format,
2259 ...) G_GNUC_PRINTF (2,3);
2260 gint g_scanner_stat_mode (const gchar *filename);
2261 /* keep downward source compatibility */
2262 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
2263 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2264 } G_STMT_END
2265 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
2266 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2267 } G_STMT_END
2268 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2269 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2270 } G_STMT_END
2273 /* GCompletion
2276 struct _GCompletion
2278 GList* items;
2279 GCompletionFunc func;
2281 gchar* prefix;
2282 GList* cache;
2285 GCompletion* g_completion_new (GCompletionFunc func);
2286 void g_completion_add_items (GCompletion* cmp,
2287 GList* items);
2288 void g_completion_remove_items (GCompletion* cmp,
2289 GList* items);
2290 void g_completion_clear_items (GCompletion* cmp);
2291 GList* g_completion_complete (GCompletion* cmp,
2292 gchar* prefix,
2293 gchar** new_prefix);
2294 void g_completion_free (GCompletion* cmp);
2297 /* GDate
2299 * Date calculations (not time for now, to be resolved). These are a
2300 * mutant combination of Steffen Beyer's DateCalc routines
2301 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2302 * date routines (written for in-house software). Written by Havoc
2303 * Pennington <hp@pobox.com>
2306 typedef guint16 GDateYear;
2307 typedef guint8 GDateDay; /* day of the month */
2308 typedef struct _GDate GDate;
2309 /* make struct tm known without having to include time.h */
2310 struct tm;
2312 /* enum used to specify order of appearance in parsed date strings */
2313 typedef enum
2315 G_DATE_DAY = 0,
2316 G_DATE_MONTH = 1,
2317 G_DATE_YEAR = 2
2318 } GDateDMY;
2320 /* actual week and month values */
2321 typedef enum
2323 G_DATE_BAD_WEEKDAY = 0,
2324 G_DATE_MONDAY = 1,
2325 G_DATE_TUESDAY = 2,
2326 G_DATE_WEDNESDAY = 3,
2327 G_DATE_THURSDAY = 4,
2328 G_DATE_FRIDAY = 5,
2329 G_DATE_SATURDAY = 6,
2330 G_DATE_SUNDAY = 7
2331 } GDateWeekday;
2332 typedef enum
2334 G_DATE_BAD_MONTH = 0,
2335 G_DATE_JANUARY = 1,
2336 G_DATE_FEBRUARY = 2,
2337 G_DATE_MARCH = 3,
2338 G_DATE_APRIL = 4,
2339 G_DATE_MAY = 5,
2340 G_DATE_JUNE = 6,
2341 G_DATE_JULY = 7,
2342 G_DATE_AUGUST = 8,
2343 G_DATE_SEPTEMBER = 9,
2344 G_DATE_OCTOBER = 10,
2345 G_DATE_NOVEMBER = 11,
2346 G_DATE_DECEMBER = 12
2347 } GDateMonth;
2349 #define G_DATE_BAD_JULIAN 0U
2350 #define G_DATE_BAD_DAY 0U
2351 #define G_DATE_BAD_YEAR 0U
2353 /* Note: directly manipulating structs is generally a bad idea, but
2354 * in this case it's an *incredibly* bad idea, because all or part
2355 * of this struct can be invalid at any given time. Use the functions,
2356 * or you will get hosed, I promise.
2358 struct _GDate
2360 guint julian_days : 32; /* julian days representation - we use a
2361 * bitfield hoping that 64 bit platforms
2362 * will pack this whole struct in one big
2363 * int
2366 guint julian : 1; /* julian is valid */
2367 guint dmy : 1; /* dmy is valid */
2369 /* DMY representation */
2370 guint day : 6;
2371 guint month : 4;
2372 guint year : 16;
2375 /* g_date_new() returns an invalid date, you then have to _set() stuff
2376 * to get a usable object. You can also allocate a GDate statically,
2377 * then call g_date_clear() to initialize.
2379 GDate* g_date_new (void);
2380 GDate* g_date_new_dmy (GDateDay day,
2381 GDateMonth month,
2382 GDateYear year);
2383 GDate* g_date_new_julian (guint32 julian_day);
2384 void g_date_free (GDate *date);
2386 /* check g_date_valid() after doing an operation that might fail, like
2387 * _parse. Almost all g_date operations are undefined on invalid
2388 * dates (the exceptions are the mutators, since you need those to
2389 * return to validity).
2391 gboolean g_date_valid (GDate *date);
2392 gboolean g_date_valid_day (GDateDay day);
2393 gboolean g_date_valid_month (GDateMonth month);
2394 gboolean g_date_valid_year (GDateYear year);
2395 gboolean g_date_valid_weekday (GDateWeekday weekday);
2396 gboolean g_date_valid_julian (guint32 julian_date);
2397 gboolean g_date_valid_dmy (GDateDay day,
2398 GDateMonth month,
2399 GDateYear year);
2401 GDateWeekday g_date_weekday (GDate *date);
2402 GDateMonth g_date_month (GDate *date);
2403 GDateYear g_date_year (GDate *date);
2404 GDateDay g_date_day (GDate *date);
2405 guint32 g_date_julian (GDate *date);
2406 guint g_date_day_of_year (GDate *date);
2408 /* First monday/sunday is the start of week 1; if we haven't reached
2409 * that day, return 0. These are not ISO weeks of the year; that
2410 * routine needs to be added.
2411 * these functions return the number of weeks, starting on the
2412 * corrsponding day
2414 guint g_date_monday_week_of_year (GDate *date);
2415 guint g_date_sunday_week_of_year (GDate *date);
2417 /* If you create a static date struct you need to clear it to get it
2418 * in a sane state before use. You can clear a whole array at
2419 * once with the ndates argument.
2421 void g_date_clear (GDate *date,
2422 guint n_dates);
2424 /* The parse routine is meant for dates typed in by a user, so it
2425 * permits many formats but tries to catch common typos. If your data
2426 * needs to be strictly validated, it is not an appropriate function.
2428 void g_date_set_parse (GDate *date,
2429 const gchar *str);
2430 void g_date_set_time (GDate *date,
2431 GTime time);
2432 void g_date_set_month (GDate *date,
2433 GDateMonth month);
2434 void g_date_set_day (GDate *date,
2435 GDateDay day);
2436 void g_date_set_year (GDate *date,
2437 GDateYear year);
2438 void g_date_set_dmy (GDate *date,
2439 GDateDay day,
2440 GDateMonth month,
2441 GDateYear y);
2442 void g_date_set_julian (GDate *date,
2443 guint32 julian_date);
2444 gboolean g_date_is_first_of_month (GDate *date);
2445 gboolean g_date_is_last_of_month (GDate *date);
2447 /* To go forward by some number of weeks just go forward weeks*7 days */
2448 void g_date_add_days (GDate *date,
2449 guint n_days);
2450 void g_date_subtract_days (GDate *date,
2451 guint n_days);
2453 /* If you add/sub months while day > 28, the day might change */
2454 void g_date_add_months (GDate *date,
2455 guint n_months);
2456 void g_date_subtract_months (GDate *date,
2457 guint n_months);
2459 /* If it's feb 29, changing years can move you to the 28th */
2460 void g_date_add_years (GDate *date,
2461 guint n_years);
2462 void g_date_subtract_years (GDate *date,
2463 guint n_years);
2464 gboolean g_date_is_leap_year (GDateYear year);
2465 guint8 g_date_days_in_month (GDateMonth month,
2466 GDateYear year);
2467 guint8 g_date_monday_weeks_in_year (GDateYear year);
2468 guint8 g_date_sunday_weeks_in_year (GDateYear year);
2470 /* qsort-friendly (with a cast...) */
2471 gint g_date_compare (GDate *lhs,
2472 GDate *rhs);
2473 void g_date_to_struct_tm (GDate *date,
2474 struct tm *tm);
2476 /* Just like strftime() except you can only use date-related formats.
2477 * Using a time format is undefined.
2479 gsize g_date_strftime (gchar *s,
2480 gsize slen,
2481 const gchar *format,
2482 GDate *date);
2485 /* GRelation
2487 * Indexed Relations. Imagine a really simple table in a
2488 * database. Relations are not ordered. This data type is meant for
2489 * maintaining a N-way mapping.
2491 * g_relation_new() creates a relation with FIELDS fields
2493 * g_relation_destroy() frees all resources
2494 * g_tuples_destroy() frees the result of g_relation_select()
2496 * g_relation_index() indexes relation FIELD with the provided
2497 * equality and hash functions. this must be done before any
2498 * calls to insert are made.
2500 * g_relation_insert() inserts a new tuple. you are expected to
2501 * provide the right number of fields.
2503 * g_relation_delete() deletes all relations with KEY in FIELD
2504 * g_relation_select() returns ...
2505 * g_relation_count() counts ...
2508 GRelation* g_relation_new (gint fields);
2509 void g_relation_destroy (GRelation *relation);
2510 void g_relation_index (GRelation *relation,
2511 gint field,
2512 GHashFunc hash_func,
2513 GCompareFunc key_compare_func);
2514 void g_relation_insert (GRelation *relation,
2515 ...);
2516 gint g_relation_delete (GRelation *relation,
2517 gconstpointer key,
2518 gint field);
2519 GTuples* g_relation_select (GRelation *relation,
2520 gconstpointer key,
2521 gint field);
2522 gint g_relation_count (GRelation *relation,
2523 gconstpointer key,
2524 gint field);
2525 gboolean g_relation_exists (GRelation *relation,
2526 ...);
2527 void g_relation_print (GRelation *relation);
2529 void g_tuples_destroy (GTuples *tuples);
2530 gpointer g_tuples_index (GTuples *tuples,
2531 gint index,
2532 gint field);
2535 /* GRand - a good and fast random number generator: Mersenne Twister
2536 * see http://www.math.keio.ac.jp/~matumoto/emt.html for more info.
2537 * The range functions return a value in the intervall [min,max).
2538 * int -> [0..2^32-1]
2539 * int_range -> [min..max-1]
2540 * double -> [0..1)
2541 * double_range -> [min..max)
2544 GRand* g_rand_new_with_seed (guint32 seed);
2545 GRand* g_rand_new (void);
2546 void g_rand_free (GRand *rand);
2548 void g_rand_set_seed (GRand *rand,
2549 guint32 seed);
2550 guint32 g_rand_int (GRand *rand);
2551 gint32 g_rand_int_range (GRand *rand,
2552 gint32 min,
2553 gint32 max);
2554 gdouble g_rand_double (GRand *rand);
2555 gdouble g_rand_double_range (GRand *rand,
2556 gdouble min,
2557 gdouble max);
2559 void g_random_set_seed (guint32 seed);
2560 guint32 g_random_int (void);
2561 gint32 g_random_int_range (gint32 min,
2562 gint32 max);
2563 gdouble g_random_double (void);
2564 gdouble g_random_double_range (gdouble min,
2565 gdouble max);
2568 /* Prime numbers.
2571 /* This function returns prime numbers spaced by approximately 1.5-2.0
2572 * and is for use in resizing data structures which prefer
2573 * prime-valued sizes. The closest spaced prime function returns the
2574 * next largest prime, or the highest it knows about which is about
2575 * MAXINT/4.
2577 guint g_spaced_primes_closest (guint num);
2580 /* GIOChannel
2583 typedef struct _GIOFuncs GIOFuncs;
2584 typedef enum
2586 G_IO_ERROR_NONE,
2587 G_IO_ERROR_AGAIN,
2588 G_IO_ERROR_INVAL,
2589 G_IO_ERROR_UNKNOWN
2590 } GIOError;
2591 typedef enum
2593 G_SEEK_CUR,
2594 G_SEEK_SET,
2595 G_SEEK_END
2596 } GSeekType;
2597 typedef enum
2599 G_IO_IN GLIB_SYSDEF_POLLIN,
2600 G_IO_OUT GLIB_SYSDEF_POLLOUT,
2601 G_IO_PRI GLIB_SYSDEF_POLLPRI,
2602 G_IO_ERR GLIB_SYSDEF_POLLERR,
2603 G_IO_HUP GLIB_SYSDEF_POLLHUP,
2604 G_IO_NVAL GLIB_SYSDEF_POLLNVAL
2605 } GIOCondition;
2607 struct _GIOChannel
2609 guint channel_flags;
2610 guint ref_count;
2611 GIOFuncs *funcs;
2614 typedef gboolean (*GIOFunc) (GIOChannel *source,
2615 GIOCondition condition,
2616 gpointer data);
2617 struct _GIOFuncs
2619 GIOError (*io_read) (GIOChannel *channel,
2620 gchar *buf,
2621 guint count,
2622 guint *bytes_read);
2623 GIOError (*io_write) (GIOChannel *channel,
2624 gchar *buf,
2625 guint count,
2626 guint *bytes_written);
2627 GIOError (*io_seek) (GIOChannel *channel,
2628 gint offset,
2629 GSeekType type);
2630 void (*io_close) (GIOChannel *channel);
2631 guint (*io_add_watch) (GIOChannel *channel,
2632 gint priority,
2633 GIOCondition condition,
2634 GIOFunc func,
2635 gpointer user_data,
2636 GDestroyNotify notify);
2637 void (*io_free) (GIOChannel *channel);
2640 void g_io_channel_init (GIOChannel *channel);
2641 void g_io_channel_ref (GIOChannel *channel);
2642 void g_io_channel_unref (GIOChannel *channel);
2643 GIOError g_io_channel_read (GIOChannel *channel,
2644 gchar *buf,
2645 guint count,
2646 guint *bytes_read);
2647 GIOError g_io_channel_write (GIOChannel *channel,
2648 gchar *buf,
2649 guint count,
2650 guint *bytes_written);
2651 GIOError g_io_channel_seek (GIOChannel *channel,
2652 gint offset,
2653 GSeekType type);
2654 void g_io_channel_close (GIOChannel *channel);
2655 guint g_io_add_watch_full (GIOChannel *channel,
2656 gint priority,
2657 GIOCondition condition,
2658 GIOFunc func,
2659 gpointer user_data,
2660 GDestroyNotify notify);
2661 guint g_io_add_watch (GIOChannel *channel,
2662 GIOCondition condition,
2663 GIOFunc func,
2664 gpointer user_data);
2667 /* Main loop
2669 typedef struct _GTimeVal GTimeVal;
2670 typedef struct _GSourceFuncs GSourceFuncs;
2671 typedef struct _GMainLoop GMainLoop; /* Opaque */
2673 struct _GTimeVal
2675 glong tv_sec;
2676 glong tv_usec;
2678 struct _GSourceFuncs
2680 gboolean (*prepare) (gpointer source_data,
2681 GTimeVal *current_time,
2682 gint *timeout,
2683 gpointer user_data);
2684 gboolean (*check) (gpointer source_data,
2685 GTimeVal *current_time,
2686 gpointer user_data);
2687 gboolean (*dispatch) (gpointer source_data,
2688 GTimeVal *dispatch_time,
2689 gpointer user_data);
2690 GDestroyNotify destroy;
2693 /* Standard priorities */
2695 #define G_PRIORITY_HIGH -100
2696 #define G_PRIORITY_DEFAULT 0
2697 #define G_PRIORITY_HIGH_IDLE 100
2698 #define G_PRIORITY_DEFAULT_IDLE 200
2699 #define G_PRIORITY_LOW 300
2701 typedef gboolean (*GSourceFunc) (gpointer data);
2703 /* Hooks for adding to the main loop */
2704 guint g_source_add (gint priority,
2705 gboolean can_recurse,
2706 GSourceFuncs *funcs,
2707 gpointer source_data,
2708 gpointer user_data,
2709 GDestroyNotify notify);
2710 gboolean g_source_remove (guint tag);
2711 gboolean g_source_remove_by_user_data (gpointer user_data);
2712 gboolean g_source_remove_by_source_data (gpointer source_data);
2713 gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
2714 gpointer user_data);
2716 void g_get_current_time (GTimeVal *result);
2718 /* Running the main loop */
2719 GMainLoop* g_main_new (gboolean is_running);
2720 void g_main_run (GMainLoop *loop);
2721 void g_main_quit (GMainLoop *loop);
2722 void g_main_destroy (GMainLoop *loop);
2723 gboolean g_main_is_running (GMainLoop *loop);
2725 /* Run a single iteration of the mainloop. If block is FALSE,
2726 * will never block
2728 gboolean g_main_iteration (gboolean may_block);
2730 /* See if any events are pending */
2731 gboolean g_main_pending (void);
2733 /* Idles and timeouts */
2734 guint g_timeout_add_full (gint priority,
2735 guint interval,
2736 GSourceFunc function,
2737 gpointer data,
2738 GDestroyNotify notify);
2739 guint g_timeout_add (guint interval,
2740 GSourceFunc function,
2741 gpointer data);
2742 guint g_idle_add (GSourceFunc function,
2743 gpointer data);
2744 guint g_idle_add_full (gint priority,
2745 GSourceFunc function,
2746 gpointer data,
2747 GDestroyNotify destroy);
2748 gboolean g_idle_remove_by_data (gpointer data);
2750 /* GPollFD
2752 * System-specific IO and main loop calls
2754 * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
2755 * descriptor as provided by the C runtime) that can be used by
2756 * MsgWaitForMultipleObjects. This does *not* include file handles
2757 * from CreateFile, SOCKETs, nor pipe handles. (But you can use
2758 * WSAEventSelect to signal events when a SOCKET is readable).
2760 * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
2761 * indicate polling for messages. These message queue GPollFDs should
2762 * be added with the g_main_poll_win32_msg_add function.
2764 * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
2765 * (GTK) programs, as GDK itself wants to read messages and convert them
2766 * to GDK events.
2768 * So, unless you really know what you are doing, it's best not to try
2769 * to use the main loop polling stuff for your own needs on
2770 * Win32. It's really only written for the GIMP's needs so
2771 * far.
2774 typedef struct _GPollFD GPollFD;
2775 typedef gint (*GPollFunc) (GPollFD *ufds,
2776 guint nfsd,
2777 gint timeout);
2778 struct _GPollFD
2780 gint fd;
2781 gushort events;
2782 gushort revents;
2785 void g_main_add_poll (GPollFD *fd,
2786 gint priority);
2787 void g_main_remove_poll (GPollFD *fd);
2788 void g_main_set_poll_func (GPollFunc func);
2790 /* On Unix, IO channels created with this function for any file
2791 * descriptor or socket.
2793 * On Win32, use this only for plain files opened with the MSVCRT (the
2794 * Microsoft run-time C library) _open(), including file descriptors
2795 * 0, 1 and 2 (corresponding to stdin, stdout and stderr).
2796 * Actually, don't do even that, this code isn't done yet.
2798 * The term file descriptor as used in the context of Win32 refers to
2799 * the emulated Unix-like file descriptors MSVCRT provides.
2801 GIOChannel* g_io_channel_unix_new (int fd);
2802 gint g_io_channel_unix_get_fd (GIOChannel *channel);
2804 #ifdef G_OS_WIN32
2806 GUTILS_C_VAR guint g_pipe_readable_msg;
2808 #define G_WIN32_MSG_HANDLE 19981206
2810 /* This is used to add polling for Windows messages. GDK (GTk+) programs
2811 * should *not* use this. (In fact, I can't think of any program that
2812 * would want to use this, but it's here just for completeness's sake.
2814 void g_main_poll_win32_msg_add(gint priority,
2815 GPollFD *fd,
2816 guint hwnd);
2818 /* An IO channel for Windows messages for window handle hwnd. */
2819 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
2821 /* An IO channel for an anonymous pipe as returned from the MSVCRT
2822 * _pipe(), with no mechanism for the writer to tell the reader when
2823 * there is data in the pipe.
2825 * This is not really implemented yet.
2827 GIOChannel *g_io_channel_win32_new_pipe (int fd);
2829 /* An IO channel for a pipe as returned from the MSVCRT _pipe(), with
2830 * Windows user messages used to signal data in the pipe for the
2831 * reader.
2833 * fd is the file descriptor. For the write end, peer is the thread id
2834 * of the reader, and peer_fd is his file descriptor for the read end
2835 * of the pipe.
2837 * This is used by the GIMP, and works.
2839 GIOChannel *g_io_channel_win32_new_pipe_with_wakeups (int fd,
2840 guint peer,
2841 int peer_fd);
2843 void g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
2844 guint peer,
2845 int peer_fd);
2847 void g_io_channel_win32_pipe_readable (int fd,
2848 guint offset);
2850 /* Get the C runtime file descriptor of a channel. */
2851 gint g_io_channel_win32_get_fd (GIOChannel *channel);
2853 /* An IO channel for a SOCK_STREAM winsock socket. The parameter is
2854 * actually a SOCKET.
2856 GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
2858 #endif
2860 /* Windows emulation stubs for common Unix functions
2862 #ifdef G_OS_WIN32
2863 # define MAXPATHLEN 1024
2865 #ifdef _MSC_VER
2866 typedef int pid_t;
2867 #endif
2870 * To get prototypes for the following POSIXish functions, you have to
2871 * include the indicated non-POSIX headers. The functions are defined
2872 * in OLDNAMES.LIB (MSVC) or -lmoldname-msvc (mingw32).
2874 * getcwd: <direct.h> (MSVC), <io.h> (mingw32)
2875 * getpid: <process.h>
2876 * access: <io.h>
2877 * unlink: <stdio.h> or <io.h>
2878 * open, read, write, lseek, close: <io.h>
2879 * rmdir: <direct.h>
2880 * pipe: <direct.h>
2883 /* pipe is not in OLDNAMES.LIB or -lmoldname-msvc. */
2884 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
2886 /* For some POSIX functions that are not provided by the MS runtime,
2887 * we provide emulators in glib, which are prefixed with g_win32_.
2889 # define ftruncate(fd, size) g_win32_ftruncate (fd, size)
2891 /* -lmingw32 also has emulations for these, but we need our own
2892 * for MSVC anyhow, so we might aswell use them always.
2894 # define opendir g_win32_opendir
2895 # define readdir g_win32_readdir
2896 # define rewinddir g_win32_rewinddir
2897 # define closedir g_win32_closedir
2898 # define NAME_MAX 255
2900 struct DIR
2902 gchar *dir_name;
2903 gboolean just_opened;
2904 guint find_file_handle;
2905 gpointer find_file_data;
2907 typedef struct DIR DIR;
2908 struct dirent
2910 gchar d_name[NAME_MAX + 1];
2912 /* emulation functions */
2913 extern int g_win32_ftruncate (gint f,
2914 guint size);
2915 DIR* g_win32_opendir (const gchar *dirname);
2916 struct dirent* g_win32_readdir (DIR *dir);
2917 void g_win32_rewinddir (DIR *dir);
2918 gint g_win32_closedir (DIR *dir);
2920 /* The MS setlocale uses locale names of the form "English_United
2921 * States.1252" etc. We want the Unixish standard form "en", "zh_TW"
2922 * etc. This function gets the current thread locale from Windows and
2923 * returns it as a string of the above form for use in forming file
2924 * names etc. The returned string should be deallocated with g_free().
2926 gchar * g_win32_getlocale (void);
2928 /* Translate a Win32 error code (as returned by GetLastError()) into
2929 * the corresponding message. The returned string should be deallocated
2930 * with g_free().
2932 gchar * g_win32_error_message (gint error);
2934 #endif /* G_OS_WIN32 */
2937 /* GLib Thread support
2940 typedef void (*GThreadFunc) (gpointer value);
2942 typedef enum
2944 G_THREAD_PRIORITY_LOW,
2945 G_THREAD_PRIORITY_NORMAL,
2946 G_THREAD_PRIORITY_HIGH,
2947 G_THREAD_PRIORITY_URGENT
2948 } GThreadPriority;
2950 typedef struct _GThread GThread;
2951 struct _GThread
2953 GThreadPriority priority;
2954 gboolean bound;
2955 gboolean joinable;
2958 typedef struct _GMutex GMutex;
2959 typedef struct _GCond GCond;
2960 typedef struct _GPrivate GPrivate;
2961 typedef struct _GStaticPrivate GStaticPrivate;
2963 typedef struct _GThreadFunctions GThreadFunctions;
2964 struct _GThreadFunctions
2966 GMutex* (*mutex_new) (void);
2967 void (*mutex_lock) (GMutex *mutex);
2968 gboolean (*mutex_trylock) (GMutex *mutex);
2969 void (*mutex_unlock) (GMutex *mutex);
2970 void (*mutex_free) (GMutex *mutex);
2971 GCond* (*cond_new) (void);
2972 void (*cond_signal) (GCond *cond);
2973 void (*cond_broadcast) (GCond *cond);
2974 void (*cond_wait) (GCond *cond,
2975 GMutex *mutex);
2976 gboolean (*cond_timed_wait) (GCond *cond,
2977 GMutex *mutex,
2978 GTimeVal *end_time);
2979 void (*cond_free) (GCond *cond);
2980 GPrivate* (*private_new) (GDestroyNotify destructor);
2981 gpointer (*private_get) (GPrivate *private_key);
2982 void (*private_set) (GPrivate *private_key,
2983 gpointer data);
2984 void (*thread_create) (GThreadFunc thread_func,
2985 gpointer arg,
2986 gulong stack_size,
2987 gboolean joinable,
2988 gboolean bound,
2989 GThreadPriority priority,
2990 gpointer thread);
2991 void (*thread_yield) (void);
2992 void (*thread_join) (gpointer thread);
2993 void (*thread_exit) (void);
2994 void (*thread_set_priority)(gpointer thread,
2995 GThreadPriority priority);
2996 void (*thread_self) (gpointer thread);
2999 GUTILS_C_VAR GThreadFunctions g_thread_functions_for_glib_use;
3000 GUTILS_C_VAR gboolean g_thread_use_default_impl;
3001 GUTILS_C_VAR gboolean g_threads_got_initialized;
3003 /* initializes the mutex/cond/private implementation for glib, might
3004 * only be called once, and must not be called directly or indirectly
3005 * from another glib-function, e.g. as a callback.
3007 void g_thread_init (GThreadFunctions *vtable);
3009 /* internal function for fallback static mutex implementation */
3010 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
3012 /* shorthands for conditional and unconditional function calls */
3013 #define G_THREAD_UF(name, arglist) \
3014 (*g_thread_functions_for_glib_use . name) arglist
3015 #define G_THREAD_CF(name, fail, arg) \
3016 (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
3017 /* keep in mind, all those mutexes and static mutexes are not
3018 * recursive in general, don't rely on that
3020 #define g_thread_supported() (g_threads_got_initialized)
3021 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
3022 #define g_mutex_lock(mutex) G_THREAD_CF (mutex_lock, (void)0, (mutex))
3023 #define g_mutex_trylock(mutex) G_THREAD_CF (mutex_trylock, TRUE, (mutex))
3024 #define g_mutex_unlock(mutex) G_THREAD_CF (mutex_unlock, (void)0, (mutex))
3025 #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex))
3026 #define g_cond_new() G_THREAD_UF (cond_new, ())
3027 #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
3028 #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
3029 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait, (void)0, (cond, \
3030 mutex))
3031 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
3032 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
3033 TRUE, \
3034 (cond, mutex, \
3035 abs_time))
3036 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
3037 #define g_private_get(private_key) G_THREAD_CF (private_get, \
3038 ((gpointer)private_key), \
3039 (private_key))
3040 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
3041 (void) (private_key = \
3042 (GPrivate*) (value)), \
3043 (private_key, value))
3044 #define g_thread_yield() G_THREAD_CF (thread_yield, (void)0, ())
3045 #define g_thread_exit() G_THREAD_CF (thread_exit, (void)0, ())
3047 GThread* g_thread_create (GThreadFunc thread_func,
3048 gpointer arg,
3049 gulong stack_size,
3050 gboolean joinable,
3051 gboolean bound,
3052 GThreadPriority priority);
3053 GThread* g_thread_self ();
3054 void g_thread_join (GThread* thread);
3055 void g_thread_set_priority (GThread* thread,
3056 GThreadPriority priority);
3058 /* GStaticMutexes can be statically initialized with the value
3059 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
3060 * much easier, than having to explicitly allocate the mutex before
3061 * use
3063 #define g_static_mutex_lock(mutex) \
3064 g_mutex_lock (g_static_mutex_get_mutex (mutex))
3065 #define g_static_mutex_trylock(mutex) \
3066 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
3067 #define g_static_mutex_unlock(mutex) \
3068 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
3070 struct _GStaticPrivate
3072 guint index;
3074 #define G_STATIC_PRIVATE_INIT { 0 }
3075 gpointer g_static_private_get (GStaticPrivate *private_key);
3076 void g_static_private_set (GStaticPrivate *private_key,
3077 gpointer data,
3078 GDestroyNotify notify);
3079 gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
3080 GThread *thread);
3081 void g_static_private_set_for_thread (GStaticPrivate *private_key,
3082 GThread *thread,
3083 gpointer data,
3084 GDestroyNotify notify);
3086 typedef struct _GStaticRecMutex GStaticRecMutex;
3087 struct _GStaticRecMutex
3089 GStaticMutex mutex;
3090 unsigned int depth;
3091 GSystemThread owner;
3094 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT }
3095 void g_static_rec_mutex_lock (GStaticRecMutex *mutex);
3096 gboolean g_static_rec_mutex_trylock (GStaticRecMutex *mutex);
3097 void g_static_rec_mutex_unlock (GStaticRecMutex *mutex);
3098 void g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
3099 guint depth);
3100 guint g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
3102 typedef struct _GStaticRWLock GStaticRWLock;
3103 struct _GStaticRWLock
3105 GStaticMutex mutex;
3106 GCond *read_cond;
3107 GCond *write_cond;
3108 guint read_counter;
3109 gboolean write;
3110 guint want_to_write;
3113 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
3115 void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
3116 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
3117 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
3118 void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
3119 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
3120 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
3121 void g_static_rw_lock_free (GStaticRWLock* lock);
3123 /* these are some convenience macros that expand to nothing if GLib
3124 * was configured with --disable-threads. for using StaticMutexes,
3125 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
3126 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
3127 * declare such an globally defined lock. name is a unique identifier
3128 * for the protected varibale or code portion. locking, testing and
3129 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
3130 * G_TRYLOCK() respectively.
3132 extern void glib_dummy_decl (void);
3133 #define G_LOCK_NAME(name) g__ ## name ## _lock
3134 #ifdef G_THREADS_ENABLED
3135 # define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
3136 # define G_LOCK_DEFINE(name) \
3137 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
3138 # define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
3140 # ifdef G_DEBUG_LOCKS
3141 # define G_LOCK(name) G_STMT_START{ \
3142 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3143 "file %s: line %d (%s): locking: %s ", \
3144 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3145 #name); \
3146 g_static_mutex_lock (&G_LOCK_NAME (name)); \
3147 }G_STMT_END
3148 # define G_UNLOCK(name) G_STMT_START{ \
3149 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3150 "file %s: line %d (%s): unlocking: %s ", \
3151 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3152 #name); \
3153 g_static_mutex_unlock (&G_LOCK_NAME (name)); \
3154 }G_STMT_END
3155 # define G_TRYLOCK(name) \
3156 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3157 "file %s: line %d (%s): try locking: %s ", \
3158 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3159 #name), g_static_mutex_trylock (&G_LOCK_NAME (name)))
3160 # else /* !G_DEBUG_LOCKS */
3161 # define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
3162 # define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
3163 # define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
3164 # endif /* !G_DEBUG_LOCKS */
3165 #else /* !G_THREADS_ENABLED */
3166 # define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
3167 # define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
3168 # define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
3169 # define G_LOCK(name)
3170 # define G_UNLOCK(name)
3171 # define G_TRYLOCK(name) (TRUE)
3172 #endif /* !G_THREADS_ENABLED */
3174 #ifdef __cplusplus
3176 #endif /* __cplusplus */
3179 #endif /* __G_LIB_H__ */