* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
+
+/*
+ * Modified by the GLib Team and others 1997-1999. See the AUTHORS
+ * file for a list of people on the GLib Team. See the ChangeLog
+ * files for a list of changes. These files are distributed with
+ * GLib at ftp://ftp.gtk.org/pub/gtk/.
+ */
+
#ifndef __G_LIB_H__
#define __G_LIB_H__
* To register hooks which are executed on exit().
* Usually a wrapper for STDC atexit.
*
- * void *g_memmove(void *dest, const void *src, guint count);
+ * void g_memmove(gpointer dest, gconstpointer void *src, gulong count);
* A wrapper for STDC memmove, or an implementation, if memmove doesn't
* exist. The prototype looks like the above, give or take a const,
* or size_t.
typedef unsigned long gulong;
typedef unsigned int guint;
+#define G_GSHORT_FORMAT "hi"
+#define G_GUSHORT_FORMAT "hu"
+#define G_GINT_FORMAT "i"
+#define G_GUINT_FORMAT "u"
+#define G_GLONG_FORMAT "li"
+#define G_GULONG_FORMAT "lu"
+
typedef float gfloat;
typedef double gdouble;
/* Portable endian checks and conversions
+ *
+ * glibconfig.h defines G_BYTE_ORDER which expands to one of
+ * the below macros.
*/
-
#define G_LITTLE_ENDIAN 1234
#define G_BIG_ENDIAN 4321
#define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
+
/* Basic bit swapping functions
*/
#define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
/* Intel specific stuff for speed
*/
#if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
-
# define GUINT16_SWAP_LE_BE_X86(val) \
(__extension__ \
({ register guint16 __v; \
: "=r" (__v) \
: "0" ((guint16) (val))); \
__v; }))
-
# define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
-
# if !defined(__i486__) && !defined(__i586__) \
&& !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
# define GUINT32_SWAP_LE_BE_X86(val) \
: "=r" (__v) \
: "0" ((guint32) (val))); \
__v; }))
-
# else /* 486 and higher has bswap */
# define GUINT32_SWAP_LE_BE_X86(val) \
(__extension__ \
: "0" ((guint32) (val))); \
__v; }))
# endif /* processor specific 32-bit stuff */
-
# define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
-
#else /* !__i386__ */
# define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
# define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
(guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) | \
(((guint64) (val) & \
(guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
-
# if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
# define GUINT64_SWAP_LE_BE_X86(val) \
(__extension__ \
__r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
} \
__r.__ll; }))
-
# define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
-
# else /* !__i386__ */
# define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
# endif
(((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
(((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
-/* The TO_?E stuff is defined in glibconfig.h. The transformation is symmetric,
- so the FROM just maps to the TO.
+/* The G*_TO_?E() macros are defined in glibconfig.h.
+ * The transformation is symmetric, so the FROM just maps to the TO.
*/
#define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
#define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
#define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
#ifdef G_HAVE_GINT64
-#define GINT64_FROM_LE(val) (GINT32_TO_LE (val))
-#define GUINT64_FROM_LE(val) (GUINT32_TO_LE (val))
-#define GINT64_FROM_BE(val) (GINT32_TO_BE (val))
-#define GUINT64_FROM_BE(val) (GUINT32_TO_BE (val))
+#define GINT64_FROM_LE(val) (GINT64_TO_LE (val))
+#define GUINT64_FROM_LE(val) (GUINT64_TO_LE (val))
+#define GINT64_FROM_BE(val) (GINT64_TO_BE (val))
+#define GUINT64_FROM_BE(val) (GUINT64_TO_BE (val))
#endif
#define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
#define GINT_FROM_BE(val) (GINT_TO_BE (val))
#define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
+
/* Portable versions of host-network order stuff
*/
#define g_ntohl(val) (GUINT32_FROM_BE (val))
GUTILS_C_VAR const guint glib_interface_age;
GUTILS_C_VAR const guint glib_binary_age;
+#define GLIB_CHECK_VERSION(major,minor,micro) \
+ (GLIB_MAJOR_VERSION > (major) || \
+ (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
+ (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
+ GLIB_MICRO_VERSION >= (micro)))
+
/* Forward declarations of glib types.
*/
-
typedef struct _GAllocator GAllocator;
typedef struct _GArray GArray;
typedef struct _GByteArray GByteArray;
typedef struct _GMemChunk GMemChunk;
typedef struct _GNode GNode;
typedef struct _GPtrArray GPtrArray;
+typedef struct _GQueue GQueue;
+typedef struct _GRand GRand;
typedef struct _GRelation GRelation;
typedef struct _GScanner GScanner;
typedef struct _GScannerConfig GScannerConfig;
typedef struct _GSList GSList;
+typedef struct _GStack GStack;
typedef struct _GString GString;
typedef struct _GStringChunk GStringChunk;
typedef struct _GTimer GTimer;
typedef union _GTokenValue GTokenValue;
typedef struct _GIOChannel GIOChannel;
-
typedef enum
{
G_TRAVERSE_LEAFS = 1 << 0,
GSList *next;
};
+struct _GStack
+{
+ GList *list;
+};
+
+struct _GQueue
+{
+ GList *list;
+ GList *list_end;
+ guint list_size;
+};
+
struct _GString
{
gchar *str;
GCompareFunc func);
GList* g_list_concat (GList *list1,
GList *list2);
+GList* g_list_delete (GList *list,
+ GList *link);
GList* g_list_remove (GList *list,
gpointer data);
GList* g_list_remove_link (GList *list,
#define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
+/* Stacks
+ */
+
+GStack * g_stack_new (void);
+void g_stack_free (GStack *stack);
+gpointer g_stack_pop (GStack *stack);
+
+#define g_stack_empty(stack) \
+ ((((GStack *)(stack)) && ((GStack *)(stack))->list) ? FALSE : TRUE)
+
+#define g_stack_peek(stack) \
+ ((((GStack *)(stack)) && ((GStack *)(stack))->list) ? \
+ ((GStack *)(stack))->list->data : NULL)
+
+#define g_stack_index(stack,ptr) \
+ ((((GStack *)(stack)) && ((GStack *)(stack))->list) ? \
+ g_list_index (((GStack *)(stack))->list, (ptr)) : -1)
+
+#define g_stack_push(stack,data) G_STMT_START { \
+ if ((GStack *)(stack)) \
+ ((GStack *)(stack))->list = \
+ g_list_prepend (((GStack *)(stack))->list, (data)); \
+ } G_STMT_END
+
+
+
+/* Queues
+ */
+
+GQueue * g_queue_new (void);
+void g_queue_free (GQueue *q);
+guint g_queue_get_size (GQueue *q);
+void g_queue_push_front (GQueue *q, gpointer data);
+void g_queue_push_back (GQueue *q, gpointer data);
+gpointer g_queue_pop_front (GQueue *q);
+gpointer g_queue_pop_back (GQueue *q);
+
+#define g_queue_empty(queue) \
+ ((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? FALSE : TRUE)
+
+#define g_queue_peek_front(queue) \
+ ((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? \
+ ((GQueue *)(queue))->list->data : NULL)
+
+#define g_queue_peek_back(queue) \
+ ((((GQueue *)(queue)) && ((GQueue *)(queue))->list_end) ? \
+ ((GQueue *)(queue))->list_end->data : NULL)
+
+#define g_queue_index(queue,ptr) \
+ ((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? \
+ g_list_index (((GQueue *)(queue))->list, (ptr)) : -1)
+
+#define g_queue_push g_queue_push_back
+#define g_queue_pop g_queue_pop_front
+#define g_queue_peek g_queue_peek_front
+
+
+
+
+
/* Hash tables
*/
GHashTable* g_hash_table_new (GHashFunc hash_func,
void g_hash_table_foreach (GHashTable *hash_table,
GHFunc func,
gpointer user_data);
-gint g_hash_table_foreach_remove (GHashTable *hash_table,
+guint g_hash_table_foreach_remove (GHashTable *hash_table,
GHRFunc func,
gpointer user_data);
-gint g_hash_table_size (GHashTable *hash_table);
+guint g_hash_table_size (GHashTable *hash_table);
/* Caches
G_HOOK_FLAG_MASK = 0x0f
} GHookFlagMask;
+#define G_HOOK_DEFERRED_DESTROY ((GHookFreeFunc) 0x01)
+
struct _GHookList
{
guint seq_id;
GHook *hooks;
GMemChunk *hook_memchunk;
GHookFreeFunc hook_free; /* virtual function */
+ GHookFreeFunc hook_destroy; /* virtual function */
};
struct _GHook
gboolean need_valids,
gpointer func,
gpointer data);
+/* return the first valid hook, and increment its reference count */
GHook* g_hook_first_valid (GHookList *hook_list,
gboolean may_be_in_call);
-GHook* g_hook_next_valid (GHook *hook,
+/* return the next valid hook with incremented reference count, and
+ * decrement the reference count of the original hook
+ */
+GHook* g_hook_next_valid (GHookList *hook_list,
+ GHook *hook,
gboolean may_be_in_call);
/* GHookCompareFunc implementation to insert hooks sorted by their id */
GLogLevelFlags fatal_mask);
GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
#ifndef G_LOG_DOMAIN
-#define G_LOG_DOMAIN (NULL)
+#define G_LOG_DOMAIN ((gchar*) 0)
#endif /* G_LOG_DOMAIN */
#ifdef __GNUC__
#define g_error(format, args...) g_log (G_LOG_DOMAIN, \
G_LOG_LEVEL_WARNING, \
format, ##args)
#else /* !__GNUC__ */
-static inline void
+static void
g_error (const gchar *format,
...)
{
g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
va_end (args);
}
-static inline void
+static void
g_message (const gchar *format,
...)
{
g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
va_end (args);
}
-static inline void
+static void
g_warning (const gchar *format,
...)
{
/* Timer
*/
+
+#define G_MICROSEC 1000000
+
GTimer* g_timer_new (void);
void g_timer_destroy (GTimer *timer);
void g_timer_start (GTimer *timer);
void g_timer_reset (GTimer *timer);
gdouble g_timer_elapsed (GTimer *timer,
gulong *microseconds);
-
+void g_usleep (gulong microseconds);
/* String utility functions that modify a string argument or
* return a constant string that must not be freed.
...); /* NULL terminated */
gchar* g_strjoin (const gchar *separator,
...); /* NULL terminated */
+/* Return a duplicate of the string with \ and " characters escaped by
+ * a \. The returned string should be freed with g_free().
+ */
gchar* g_strescape (gchar *string);
+
gpointer g_memdup (gconstpointer mem,
guint byte_size);
/* Strings
*/
-GString* g_string_new (const gchar *init);
-GString* g_string_sized_new (guint dfl_size);
-void g_string_free (GString *string,
- gint free_segment);
-GString* g_string_assign (GString *lval,
- const gchar *rval);
-GString* g_string_truncate (GString *string,
- gint len);
-GString* g_string_append (GString *string,
- const gchar *val);
-GString* g_string_append_c (GString *string,
- gchar c);
-GString* g_string_prepend (GString *string,
- const gchar *val);
-GString* g_string_prepend_c (GString *string,
- gchar c);
-GString* g_string_insert (GString *string,
- gint pos,
- const gchar *val);
-GString* g_string_insert_c (GString *string,
- gint pos,
- gchar c);
-GString* g_string_erase (GString *string,
- gint pos,
- gint len);
-GString* g_string_down (GString *string);
-GString* g_string_up (GString *string);
-void g_string_sprintf (GString *string,
- const gchar *format,
- ...) G_GNUC_PRINTF (2, 3);
-void g_string_sprintfa (GString *string,
- const gchar *format,
- ...) G_GNUC_PRINTF (2, 3);
+typedef enum
+{
+ G_STRING_ERROR_NONE, /* No error occurred */
+ G_STRING_ERROR_INVAL, /* Invalid input value to function */
+ G_STRING_ERROR_READ, /* read() returned an error - check errno */
+ G_STRING_ERROR_NODATA, /* No more input data - result string may contain data */
+ G_STRING_ERROR_LENGTH /* max_length reached */
+} GStringError;
+
+#define g_string_length(fstring) (fstring ? fstring->len : 0)
+#define g_string_str(fstring) (fstring ? fstring->str : NULL)
+#define g_string_char(fstring, n) (fstring->str[n])
+
+#define g_string_copy(a,b) (g_string_assign(a, b->str))
+#define g_string_dup(fstring) (fstring ? g_string_new(fstring->str) :\
+ g_string_new(NULL))
+
+#define g_string_cmp(a,b) (strcmp(g_string_str(a), \
+ g_string_str(b)))
+#define g_string_ncmp(a,b,n) (strncmp(g_string_str(a), \
+ g_string_str(b), n))
+#define g_string_casecmp(a,b) (g_strcasecmp(g_string_str(a), \
+ g_string_str(b)))
+#define g_string_ncasecmp(a,b,n) (g_strncasecmp(g_string_str(a), \
+ g_string_str(b), n))
+
+#define g_string_strcmp(a,b) (strcmp(g_string_str(a), b))
+#define g_string_strcasecmp(a,b) (g_strcasecmp(g_string_str(a), b))
+#define g_string_strncasecmp(a,b,n) (g_strncasecmp(g_string_str(a), b, n))
+
+GString* g_string_new (const gchar *init);
+GString* g_string_sized_new (guint dfl_size);
+void g_string_free (GString *string,
+ gint free_segment);
+GString* g_string_assign (GString *lval,
+ const gchar *rval);
+GString* g_string_truncate (GString *string,
+ gint len);
+GString* g_string_append (GString *string,
+ const gchar *val);
+GString* g_string_append_c (GString *string,
+ gchar c);
+GString* g_string_prepend (GString *string,
+ const gchar *val);
+GString* g_string_prepend_c (GString *string,
+ gchar c);
+GString* g_string_insert (GString *string,
+ gint pos,
+ const gchar *val);
+GString* g_string_insert_c (GString *string,
+ gint pos,
+ gchar c);
+GString* g_string_erase (GString *string,
+ gint pos,
+ gint len);
+GString* g_string_down (GString *string);
+GString* g_string_up (GString *string);
+void g_string_sprintf (GString *string,
+ const gchar *format,
+ ...) G_GNUC_PRINTF (2, 3);
+void g_string_sprintfa (GString *string,
+ const gchar *format,
+ ...) G_GNUC_PRINTF (2, 3);
+GStringError g_string_readline (GString *dest_str,
+ gint max_length,
+ gint fd);
+GStringError g_string_readline_buffered (GString *dest_str,
+ GString *buff_str,
+ gint max_length,
+ gint fd,
+ gint match_bare_cr);
+GList* g_string_tokenise (GString *string,
+ gchar *delims,
+ gint max_tokens,
+ gint allow_empty);
+void g_string_tokenise_free (GList *tokens,
+ gint free_token);
/* Resizable arrays, remove fills any cleared spot and shortens the
* order by moving the last element to the position of the removed
*/
-#define g_array_append_val(a,v) g_array_append_vals(a,&v,1)
-#define g_array_prepend_val(a,v) g_array_prepend_vals(a,&v,1)
-#define g_array_insert_val(a,i,v) g_array_prepend_vals(a,i,&v,1)
-#define g_array_index(a,t,i) (((t*)a->data)[i])
+#define g_array_append_val(a,v) g_array_append_vals (a, &v, 1)
+#define g_array_prepend_val(a,v) g_array_prepend_vals (a, &v, 1)
+#define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
+#define g_array_index(a,t,i) (((t*) (a)->data) [(i)])
GArray* g_array_new (gboolean zero_terminated,
gboolean clear,
/* This "hash" function will just return the key's adress as an
* unsigned integer. Useful for hashing on plain adresses or
* simple integer values.
+ * passing NULL into g_hash_table_new() as GHashFunc has the
+ * same effect as passing g_direct_hash().
*/
guint g_direct_hash (gconstpointer v);
gint g_direct_equal (gconstpointer v,
const gchar *format,
GDate *date);
-
/* GRelation
*
* Indexed Relations. Imagine a really simple table in a
gint field);
+/* GRand - a good and fast random number generator: Mersenne Twister
+ * see http://www.math.keio.ac.jp/~matumoto/emt.html for more info.
+ * The range functions return a value in the intervall [min,max).
+ * int -> [0..2^32-1]
+ * int_range -> [min..max-1]
+ * double -> [0..1)
+ * double_range -> [min..max)
+ */
+
+GRand* g_rand_new_with_seed (guint32 seed);
+GRand* g_rand_new (void);
+void g_rand_free (GRand *rand);
+
+void g_rand_set_seed (GRand *rand,
+ guint32 seed);
+guint32 g_rand_int (GRand *rand);
+gint32 g_rand_int_range (GRand *rand,
+ gint32 min,
+ gint32 max);
+gdouble g_rand_double (GRand *rand);
+gdouble g_rand_double_range (GRand *rand,
+ gdouble min,
+ gdouble max);
+
+void g_random_set_seed (guint32 seed);
+guint32 g_random_int (void);
+gint32 g_random_int_range (gint32 min,
+ gint32 max);
+gdouble g_random_double (void);
+gdouble g_random_double_range (gdouble min,
+ gdouble max);
+
+
/* Prime numbers.
*/
} GSeekType;
typedef enum
{
- G_IO_IN
-#ifdef POLLIN
- = POLLIN
-#endif
- , G_IO_OUT
-#ifdef POLLOUT
- = POLLOUT
-#endif
- , G_IO_PRI
-#ifdef POLLPRI
- = POLLPRI
-#endif
-
- , G_IO_ERR
-#ifdef POLLERR
- = POLLERR
-#endif
- , G_IO_HUP
-#ifdef POLLHUP
- = POLLHUP
-#endif
- , G_IO_NVAL
-#ifdef POLLNVAL
- = POLLNVAL
-#endif
+ G_IO_IN GLIB_SYSDEF_POLLIN,
+ G_IO_OUT GLIB_SYSDEF_POLLOUT,
+ G_IO_PRI GLIB_SYSDEF_POLLPRI,
+ G_IO_ERR GLIB_SYSDEF_POLLERR,
+ G_IO_HUP GLIB_SYSDEF_POLLHUP,
+ G_IO_NVAL GLIB_SYSDEF_POLLNVAL
} GIOCondition;
struct _GIOChannel
GDestroyNotify destroy;
};
+/* Standard priorities */
+
+#define G_PRIORITY_HIGH -100
+#define G_PRIORITY_DEFAULT 0
+#define G_PRIORITY_HIGH_IDLE 100
+#define G_PRIORITY_DEFAULT_IDLE 200
+#define G_PRIORITY_LOW 300
+
typedef gboolean (*GSourceFunc) (gpointer data);
/* Hooks for adding to the main loop */
-guint g_source_add (gint priority,
- gboolean can_recurse,
- GSourceFuncs *funcs,
- gpointer source_data,
- gpointer user_data,
- GDestroyNotify notify);
-void g_source_remove (guint tag);
-void g_source_remove_by_user_data (gpointer user_data);
-void g_source_remove_by_source_data (gpointer source_data);
+guint g_source_add (gint priority,
+ gboolean can_recurse,
+ GSourceFuncs *funcs,
+ gpointer source_data,
+ gpointer user_data,
+ GDestroyNotify notify);
+gboolean g_source_remove (guint tag);
+gboolean g_source_remove_by_user_data (gpointer user_data);
+gboolean g_source_remove_by_source_data (gpointer source_data);
+gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
+ gpointer user_data);
void g_get_current_time (GTimeVal *result);
/* Running the main loop */
-GMainLoop* g_main_new (void);
+GMainLoop* g_main_new (gboolean is_running);
void g_main_run (GMainLoop *loop);
void g_main_quit (GMainLoop *loop);
void g_main_destroy (GMainLoop *loop);
+gboolean g_main_is_running (GMainLoop *loop);
/* Run a single iteration of the mainloop. If block is FALSE,
* will never block
GSourceFunc function,
gpointer data,
GDestroyNotify destroy);
+gboolean g_idle_remove_by_data (gpointer data);
/* GPollFD
*
- * Unix-specific IO and main loop calls
+ * System-specific IO and main loop calls
+ *
+ * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
+ * descriptor as provided by the C runtime) that can be used by
+ * MsgWaitForMultipleObjects. This does *not* include file handles
+ * from CreateFile, SOCKETs, nor pipe handles. (But you can use
+ * WSAEventSelect to signal events when a SOCKET is readable).
+ *
+ * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
+ * indicate polling for messages. These message queue GPollFDs should
+ * be added with the g_main_poll_win32_msg_add function.
+ *
+ * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
+ * (GTK) programs, as GDK itself wants to read messages and convert them
+ * to GDK events.
+ *
+ * So, unless you really know what you are doing, it's best not to try
+ * to use the main loop polling stuff for your own needs on
+ * Win32. It's really only written for the GIMP's needs so
+ * far.
*/
typedef struct _GPollFD GPollFD;
gushort revents;
};
-void g_main_poll_add (gint priority,
- GPollFD *fd);
-void g_main_poll_remove (GPollFD *fd);
+void g_main_add_poll (GPollFD *fd,
+ gint priority);
+void g_main_remove_poll (GPollFD *fd);
void g_main_set_poll_func (GPollFunc func);
+/* On Unix, IO channels created with this function for any file
+ * descriptor or socket.
+ *
+ * On Win32, use this only for plain files opened with the MSVCRT (the
+ * Microsoft run-time C library) _open(), including file descriptors
+ * 0, 1 and 2 (corresponding to stdin, stdout and stderr).
+ * Actually, don't do even that, this code isn't done yet.
+ *
+ * The term file descriptor as used in the context of Win32 refers to
+ * the emulated Unix-like file descriptors MSVCRT provides.
+ */
GIOChannel* g_io_channel_unix_new (int fd);
gint g_io_channel_unix_get_fd (GIOChannel *channel);
+#ifdef NATIVE_WIN32
+
+GUTILS_C_VAR guint g_pipe_readable_msg;
-/* old IO Channels */
-#if 0
-/* IO Channels.
- * These are used for plug-in communication in the GIMP, for instance.
- * On Unix, it's simply an encapsulated file descriptor (a pipe).
- * On Windows, it's a handle to an anonymouos pipe, *and* (in the case
- * of the writable end) a thread id to post a message to when you have written
- * stuff.
+#define G_WIN32_MSG_HANDLE 19981206
+
+/* This is used to add polling for Windows messages. GDK (GTk+) programs
+ * should *not* use this. (In fact, I can't think of any program that
+ * would want to use this, but it's here just for completeness's sake.
*/
-struct _GIOChannel
-{
- gint fd; /* file handle (pseudo such in Win32) */
-#ifdef NATIVE_WIN32
- guint peer; /* thread to post message to */
- guint peer_fd; /* read handle (in the other process) */
- guint offset; /* counter of accumulated bytes, to
- * be included in the message posted
- * so we keep in sync.
- */
- guint need_wakeups; /* in output channels whether the reader
- * needs wakeups
- */
-#endif
-};
-GIOChannel *g_iochannel_new (gint fd);
-void g_iochannel_free (GIOChannel *channel);
-void g_iochannel_close_and_free (GIOChannel *channel);
-void g_iochannel_wakeup_peer (GIOChannel *channel);
-#ifndef NATIVE_WIN32
-# define g_iochannel_wakeup_peer(channel) G_STMT_START { } G_STMT_END
-#endif
-#endif /* old IO Channels */
+void g_main_poll_win32_msg_add(gint priority,
+ GPollFD *fd,
+ guint hwnd);
+/* An IO channel for Windows messages for window handle hwnd. */
+GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
-/* Windows emulation stubs for common unix functions
+/* An IO channel for an anonymous pipe as returned from the MSVCRT
+ * _pipe(), with no mechanism for the writer to tell the reader when
+ * there is data in the pipe.
+ *
+ * This is not really implemented yet.
+ */
+GIOChannel *g_io_channel_win32_new_pipe (int fd);
+
+/* An IO channel for a pipe as returned from the MSVCRT _pipe(), with
+ * Windows user messages used to signal data in the pipe for the
+ * reader.
+ *
+ * fd is the file descriptor. For the write end, peer is the thread id
+ * of the reader, and peer_fd is his file descriptor for the read end
+ * of the pipe.
+ *
+ * This is used by the GIMP, and works.
+ */
+GIOChannel *g_io_channel_win32_new_pipe_with_wakeups (int fd,
+ guint peer,
+ int peer_fd);
+
+void g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
+ guint peer,
+ int peer_fd);
+
+void g_io_channel_win32_pipe_readable (int fd,
+ guint offset);
+
+/* Get the C runtime file descriptor of a channel. */
+gint g_io_channel_win32_get_fd (GIOChannel *channel);
+
+/* An IO channel for a SOCK_STREAM winsock socket. The parameter is
+ * actually a SOCKET.
+ */
+GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
+
+#endif
+
+/* Windows emulation stubs for common Unix functions
*/
#ifdef NATIVE_WIN32
# define MAXPATHLEN 1024
-# ifdef _MSC_VER
+
+#ifdef _MSC_VER
typedef int pid_t;
+#endif
/* These POSIXish functions are available in the Microsoft C library
* prefixed with underscore (which of course technically speaking is
* <windows.h> every now and then...).
*
* You still need to include the appropriate headers to get the
- * prototypes, <io.h> or <direct.h>.
+ * prototypes, like <stdio.h>, <io.h>, <direct.h> or <process.h>.
*
* For some functions, we provide emulators in glib, which are prefixed
* with gwin_.
# define getcwd _getcwd
# define getpid _getpid
# define access _access
+#ifdef __GNUC__
+# define stat _stat
+# define fileno _fileno
+#endif
+# define fstat _fstat
+# define unlink _unlink
# define open _open
# define read _read
# define write _write
# define lseek _lseek
# define close _close
+# define rmdir _rmdir
# define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
# define popen _popen
# define pclose _pclose
# define fdopen _fdopen
+# define hypot _hypot
# define ftruncate(fd, size) gwin_ftruncate (fd, size)
# define opendir gwin_opendir
# define readdir gwin_readdir
struct dirent* gwin_readdir (DIR *dir);
void gwin_rewinddir (DIR *dir);
gint gwin_closedir (DIR *dir);
-# endif /* _MSC_VER */
#endif /* NATIVE_WIN32 */
/* GLib Thread support
*/
+
+typedef void (*GThreadFunc) (gpointer value);
+
+typedef enum
+{
+ G_THREAD_PRIORITY_LOW,
+ G_THREAD_PRIORITY_NORMAL,
+ G_THREAD_PRIORITY_HIGH,
+ G_THREAD_PRIORITY_URGENT,
+} GThreadPriority;
+
+typedef struct _GThread GThread;
+struct _GThread
+{
+ GThreadPriority priority;
+ gboolean bound;
+ gboolean joinable;
+};
+
typedef struct _GMutex GMutex;
typedef struct _GCond GCond;
typedef struct _GPrivate GPrivate;
typedef struct _GStaticPrivate GStaticPrivate;
+
typedef struct _GThreadFunctions GThreadFunctions;
struct _GThreadFunctions
{
- GMutex* (*mutex_new) (void);
- void (*mutex_lock) (GMutex *mutex);
- gboolean (*mutex_trylock) (GMutex *mutex);
- void (*mutex_unlock) (GMutex *mutex);
- void (*mutex_free) (GMutex *mutex);
- GCond* (*cond_new) (void);
- void (*cond_signal) (GCond *cond);
- void (*cond_broadcast) (GCond *cond);
- void (*cond_wait) (GCond *cond,
- GMutex *mutex);
- gboolean (*cond_timed_wait) (GCond *cond,
- GMutex *mutex,
- GTimeVal *end_time);
- void (*cond_free) (GCond *cond);
- GPrivate* (*private_new) (GDestroyNotify destructor);
- gpointer (*private_get) (GPrivate *private_key);
- void (*private_set) (GPrivate *private_key,
- gpointer data);
+ GMutex* (*mutex_new) (void);
+ void (*mutex_lock) (GMutex *mutex);
+ gboolean (*mutex_trylock) (GMutex *mutex);
+ void (*mutex_unlock) (GMutex *mutex);
+ void (*mutex_free) (GMutex *mutex);
+ GCond* (*cond_new) (void);
+ void (*cond_signal) (GCond *cond);
+ void (*cond_broadcast) (GCond *cond);
+ void (*cond_wait) (GCond *cond,
+ GMutex *mutex);
+ gboolean (*cond_timed_wait) (GCond *cond,
+ GMutex *mutex,
+ GTimeVal *end_time);
+ void (*cond_free) (GCond *cond);
+ GPrivate* (*private_new) (GDestroyNotify destructor);
+ gpointer (*private_get) (GPrivate *private_key);
+ void (*private_set) (GPrivate *private_key,
+ gpointer data);
+ gpointer (*thread_create) (GThreadFunc thread_func,
+ gpointer arg,
+ gulong stack_size,
+ gboolean joinable,
+ gboolean bound,
+ GThreadPriority priority);
+ void (*thread_yield) (void);
+ void (*thread_join) (gpointer thread);
+ void (*thread_exit) (void);
+ void (*thread_set_priority)(gpointer thread,
+ GThreadPriority priority);
+ gpointer (*thread_self) (void);
};
GUTILS_C_VAR GThreadFunctions g_thread_functions_for_glib_use;
(void) (private_key = \
(GPrivate*) (value)), \
(private_key, value))
+#define g_thread_yield() G_THREAD_CF (thread_yield, (void)0, ())
+#define g_thread_exit() G_THREAD_CF (thread_exit, (void)0, ())
+
+GThread* g_thread_create (GThreadFunc thread_func,
+ gpointer arg,
+ gulong stack_size,
+ gboolean joinable,
+ gboolean bound,
+ GThreadPriority priority);
+GThread* g_thread_self ();
+void g_thread_join (GThread* thread);
+void g_thread_set_priority (GThread* thread,
+ GThreadPriority priority);
+
/* GStaticMutexes can be statically initialized with the value
* G_STATIC_MUTEX_INIT, and then they can directly be used, that is
* much easier, than having to explicitly allocate the mutex before
g_mutex_trylock (g_static_mutex_get_mutex (mutex))
#define g_static_mutex_unlock(mutex) \
g_mutex_unlock (g_static_mutex_get_mutex (mutex))
+
struct _GStaticPrivate
{
guint index;
void g_static_private_set (GStaticPrivate *private_key,
gpointer data,
GDestroyNotify notify);
+gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
+ GThread *thread);
+void g_static_private_set_for_thread (GStaticPrivate *private_key,
+ GThread *thread,
+ gpointer data,
+ GDestroyNotify notify);
+#ifndef G_STATIC_REC_MUTEX_INIT
+/* if GStaticRecMutex is not just a differently initialized GStaticMutex,
+ * the following is done:
+ * This can't be done in glibconfig.h, as GStaticPrivate and gboolean
+ * are not yet known there
+ */
+typedef struct _GStaticRecMutex GStaticRecMutex;
+struct _GStaticRecMutex
+{
+ GStaticMutex mutex;
+ GStaticPrivate counter;
+};
+#define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT, G_STATIC_PRIVATE_INIT }
+void g_static_rec_mutex_lock (GStaticRecMutex* mutex);
+gboolean g_static_rec_mutex_trylock (GStaticRecMutex* mutex);
+void g_static_rec_mutex_unlock (GStaticRecMutex* mutex);
+#define g_static_rec_mutex_get_mutex(mutex) ((mutex)->mutex)
+#endif /* G_STATIC_REC_MUTEX_INIT */
+
+typedef struct _GStaticRWLock GStaticRWLock;
+struct _GStaticRWLock
+{
+ GStaticMutex mutex;
+ GCond *read_cond;
+ GCond *write_cond;
+ guint read_counter;
+ gboolean write;
+ guint want_to_write;
+};
-/* these are some convenience macros that expand to nothing if GLib was
- * configured with --deisable-threads. for using StaticMutexes, you
- * declare them with G_LOCK_DECLARE_STATIC (name) or G_LOCK_DECLARE (name)
- * if you need to export the mutex. name is a unique identifier for the
- * protected varibale or code portion. locking, testing and unlocking of
- * such mutexes can be done with G_LOCK(), G_UNLOCK() and G_TRYLOCK()
- * respectively.
- */
-extern gboolean glib_dummy_decl;
+#define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
+
+void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
+gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
+void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
+void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
+gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
+void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
+void g_static_rw_lock_free (GStaticRWLock* lock);
+
+/* these are some convenience macros that expand to nothing if GLib
+ * was configured with --disable-threads. for using StaticMutexes,
+ * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
+ * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
+ * declare such an globally defined lock. name is a unique identifier
+ * for the protected varibale or code portion. locking, testing and
+ * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
+ * G_TRYLOCK() respectively.
+ */
+extern void glib_dummy_decl (void);
#define G_LOCK_NAME(name) (g__ ## name ## _lock)
#ifdef G_THREADS_ENABLED
-# define G_LOCK_DECLARE_STATIC(name) static G_LOCK_DECLARE (name)
-# define G_LOCK_DECLARE(name) \
+# define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
+# define G_LOCK_DEFINE(name) \
GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
+# define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
# ifdef G_DEBUG_LOCKS
# define G_LOCK(name) G_STMT_START{ \
"file %s: line %d (%s): locking: %s ", \
__FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
#name); \
- g_static_mutex_lock (G_LOCK_NAME (name)); \
+ g_static_mutex_lock (&G_LOCK_NAME (name)); \
}G_STMT_END
# define G_UNLOCK(name) G_STMT_START{ \
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
"file %s: line %d (%s): unlocking: %s ", \
__FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
#name); \
- g_static_mutex_unlock (G_LOCK_NAME (name)); \
+ g_static_mutex_unlock (&G_LOCK_NAME (name)); \
}G_STMT_END
# define G_TRYLOCK(name) G_STMT_START{ \
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
"file %s: line %d (%s): try locking: %s ", \
__FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
#name); \
- }G_STMT_END, g_static_mutex_trylock (G_LOCK_NAME (name))
+ }G_STMT_END, g_static_mutex_trylock (&G_LOCK_NAME (name))
# else /* !G_DEBUG_LOCKS */
-# define G_LOCK(name) g_static_mutex_lock (G_LOCK_NAME (name))
-# define G_UNLOCK(name) g_static_mutex_unlock (G_LOCK_NAME (name))
-# define G_TRYLOCK(name) g_static_mutex_trylock (G_LOCK_NAME (name))
+# define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
+# define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
+# define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
# endif /* !G_DEBUG_LOCKS */
#else /* !G_THREADS_ENABLED */
-# define G_LOCK_DECLARE_STATIC(name) extern gboolean glib_dummy_decl
-# define G_LOCK_DECLARE(name) extern gboolean glib_dummy_decl
+# define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
+# define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
+# define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
# define G_LOCK(name)
# define G_UNLOCK(name)
# define G_TRYLOCK(name) (FALSE)