From 05d49ddbd36b285afda7dddb54532c20728e9112 Mon Sep 17 00:00:00 2001 From: Owen Taylor Date: Tue, 15 Dec 1998 22:30:15 +0000 Subject: [PATCH] Use an "inheritance" scheme for IO channel memory allocation.h Tue Dec 15 17:17:46 1998 Owen Taylor * glib.h giounix.c giochannel.c: Use an "inheritance" scheme for IO channel memory allocation.h --- ChangeLog | 5 +++ ChangeLog.pre-2-0 | 5 +++ ChangeLog.pre-2-10 | 5 +++ ChangeLog.pre-2-12 | 5 +++ ChangeLog.pre-2-2 | 5 +++ ChangeLog.pre-2-4 | 5 +++ ChangeLog.pre-2-6 | 5 +++ ChangeLog.pre-2-8 | 5 +++ giochannel.c | 77 ++++++++-------------------------------------- giounix.c | 21 ++++++++----- glib.h | 16 +++++----- glib/giochannel.c | 77 ++++++++-------------------------------------- glib/giounix.c | 21 ++++++++----- glib/glib.h | 16 +++++----- 14 files changed, 110 insertions(+), 158 deletions(-) diff --git a/ChangeLog b/ChangeLog index 05f3caf13..48223a772 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Dec 15 17:17:46 1998 Owen Taylor + + * glib.h giounix.c giochannel.c: Use an "inheritance" + scheme for IO channel memory allocation.h + 1998-12-15 Havoc Pennington * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index 05f3caf13..48223a772 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,8 @@ +Tue Dec 15 17:17:46 1998 Owen Taylor + + * glib.h giounix.c giochannel.c: Use an "inheritance" + scheme for IO channel memory allocation.h + 1998-12-15 Havoc Pennington * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 05f3caf13..48223a772 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,8 @@ +Tue Dec 15 17:17:46 1998 Owen Taylor + + * glib.h giounix.c giochannel.c: Use an "inheritance" + scheme for IO channel memory allocation.h + 1998-12-15 Havoc Pennington * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 05f3caf13..48223a772 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,8 @@ +Tue Dec 15 17:17:46 1998 Owen Taylor + + * glib.h giounix.c giochannel.c: Use an "inheritance" + scheme for IO channel memory allocation.h + 1998-12-15 Havoc Pennington * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 05f3caf13..48223a772 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,8 @@ +Tue Dec 15 17:17:46 1998 Owen Taylor + + * glib.h giounix.c giochannel.c: Use an "inheritance" + scheme for IO channel memory allocation.h + 1998-12-15 Havoc Pennington * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 05f3caf13..48223a772 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,8 @@ +Tue Dec 15 17:17:46 1998 Owen Taylor + + * glib.h giounix.c giochannel.c: Use an "inheritance" + scheme for IO channel memory allocation.h + 1998-12-15 Havoc Pennington * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 05f3caf13..48223a772 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,8 @@ +Tue Dec 15 17:17:46 1998 Owen Taylor + + * glib.h giounix.c giochannel.c: Use an "inheritance" + scheme for IO channel memory allocation.h + 1998-12-15 Havoc Pennington * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 05f3caf13..48223a772 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,8 @@ +Tue Dec 15 17:17:46 1998 Owen Taylor + + * glib.h giounix.c giochannel.c: Use an "inheritance" + scheme for IO channel memory allocation.h + 1998-12-15 Havoc Pennington * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime diff --git a/giochannel.c b/giochannel.c index 0693a084b..a9bf10a8a 100644 --- a/giochannel.c +++ b/giochannel.c @@ -27,65 +27,30 @@ #include "glib.h" #include -typedef struct _GIOChannelPrivate GIOChannelPrivate; - -struct _GIOChannelPrivate { - GIOChannel channel; - GIOFuncs *funcs; - guint ref_count; - gboolean closed; -}; - -GIOChannel * -g_io_channel_new (GIOFuncs *funcs, - gpointer channel_data) +void +g_io_channel_init (GIOChannel *channel) { - GIOChannelPrivate *result; - GIOChannel *channel; - - g_return_val_if_fail (funcs != NULL, NULL); - - result = g_new (GIOChannelPrivate, 1); - channel = (GIOChannel *)result; - - result->funcs = funcs; - result->ref_count = 1; - result->closed = FALSE; - - channel->channel_data = channel_data; - return channel; + channel->channel_flags = 0; + channel->ref_count = 1; } void g_io_channel_ref (GIOChannel *channel) { - GIOChannelPrivate *private; g_return_if_fail (channel != NULL); - private = (GIOChannelPrivate *)channel; - - private->ref_count++; + channel->ref_count++; } void g_io_channel_unref (GIOChannel *channel) { - GIOChannelPrivate *private; g_return_if_fail (channel != NULL); - private = (GIOChannelPrivate *)channel; - - private->ref_count--; - if (private->ref_count == 0) - { - /* We don't want to close the channel here, because - * the channel may just be wrapping a file or socket - * that the app is independently manipulating. - */ - private->funcs->io_free (channel); - g_free (private); - } + channel->ref_count--; + if (channel->ref_count == 0) + channel->funcs->io_free (channel); } GIOError @@ -94,12 +59,9 @@ g_io_channel_read (GIOChannel *channel, guint count, guint *bytes_read) { - GIOChannelPrivate *private = (GIOChannelPrivate *)channel; - g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN); - g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN); - return private->funcs->io_read (channel, buf, count, bytes_read); + return channel->funcs->io_read (channel, buf, count, bytes_read); } GIOError @@ -108,12 +70,9 @@ g_io_channel_write (GIOChannel *channel, guint count, guint *bytes_written) { - GIOChannelPrivate *private = (GIOChannelPrivate *)channel; - g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN); - g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN); - return private->funcs->io_write (channel, buf, count, bytes_written); + return channel->funcs->io_write (channel, buf, count, bytes_written); } GIOError @@ -121,24 +80,17 @@ g_io_channel_seek (GIOChannel *channel, gint offset, GSeekType type) { - GIOChannelPrivate *private = (GIOChannelPrivate *)channel; - g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN); - g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN); - return private->funcs->io_seek (channel, offset, type); + return channel->funcs->io_seek (channel, offset, type); } void g_io_channel_close (GIOChannel *channel) { - GIOChannelPrivate *private = (GIOChannelPrivate *)channel; - g_return_if_fail (channel != NULL); - g_return_if_fail (!private->closed); - private->closed = TRUE; - private->funcs->io_close (channel); + channel->funcs->io_close (channel); } guint @@ -149,12 +101,9 @@ g_io_add_watch_full (GIOChannel *channel, gpointer user_data, GDestroyNotify notify) { - GIOChannelPrivate *private = (GIOChannelPrivate *)channel; - g_return_val_if_fail (channel != NULL, 0); - g_return_val_if_fail (!private->closed, 0); - return private->funcs->io_add_watch (channel, priority, condition, + return channel->funcs->io_add_watch (channel, priority, condition, func, user_data, notify); } diff --git a/giounix.c b/giounix.c index 82944be89..23fc69f0e 100644 --- a/giounix.c +++ b/giounix.c @@ -37,6 +37,7 @@ typedef struct _GIOUnixChannel GIOUnixChannel; typedef struct _GIOUnixWatch GIOUnixWatch; struct _GIOUnixChannel { + GIOChannel channel; gint fd; }; @@ -141,7 +142,7 @@ g_io_unix_read (GIOChannel *channel, guint count, guint *bytes_read) { - GIOUnixChannel *unix_channel = channel->channel_data; + GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel; gint result; result = read (unix_channel->fd, buf, count); @@ -172,7 +173,7 @@ g_io_unix_write(GIOChannel *channel, guint count, guint *bytes_written) { - GIOUnixChannel *unix_channel = channel->channel_data; + GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel; gint result; result = write (unix_channel->fd, buf, count); @@ -202,7 +203,7 @@ g_io_unix_seek (GIOChannel *channel, gint offset, GSeekType type) { - GIOUnixChannel *unix_channel = channel->channel_data; + GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel; int whence; off_t result; @@ -242,7 +243,7 @@ g_io_unix_seek (GIOChannel *channel, static void g_io_unix_close (GIOChannel *channel) { - GIOUnixChannel *unix_channel = channel->channel_data; + GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel; close (unix_channel->fd); } @@ -250,7 +251,7 @@ g_io_unix_close (GIOChannel *channel) static void g_io_unix_free (GIOChannel *channel) { - GIOUnixChannel *unix_channel = channel->channel_data; + GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel; g_free (unix_channel); } @@ -264,7 +265,7 @@ g_io_unix_add_watch (GIOChannel *channel, GDestroyNotify notify) { GIOUnixWatch *watch = g_new (GIOUnixWatch, 1); - GIOUnixChannel *unix_channel = channel->channel_data; + GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel; watch->channel = channel; g_io_channel_ref (channel); @@ -284,14 +285,18 @@ GIOChannel * g_io_channel_unix_new (gint fd) { GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1); + GIOChannel *channel = (GIOChannel *)unix_channel; + + g_io_channel_init (channel); + channel->funcs = &unix_channel_funcs; unix_channel->fd = fd; - return g_io_channel_new (&unix_channel_funcs, unix_channel); + return channel; } gint g_io_channel_unix_get_fd (GIOChannel *channel) { - GIOUnixChannel *unix_channel = channel->channel_data; + GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1); return unix_channel->fd; } diff --git a/glib.h b/glib.h index 5a3020b9e..4ea30970a 100644 --- a/glib.h +++ b/glib.h @@ -2341,8 +2341,11 @@ typedef enum { #endif } GIOCondition; -struct _GIOChannel { - gpointer channel_data; +struct _GIOChannel +{ + guint channel_flags; + guint ref_count; + GIOFuncs *funcs; }; typedef gboolean (*GIOFunc) (GIOChannel *source, @@ -2371,11 +2374,10 @@ struct _GIOFuncs { void (*io_free) (GIOChannel *channel); }; -GIOChannel *g_io_channel_new (GIOFuncs *funcs, - gpointer channel_data); -void g_io_channel_ref (GIOChannel *channel); -void g_io_channel_unref (GIOChannel *channel); -GIOError g_io_channel_read (GIOChannel *channel, +void g_io_channel_init (GIOChannel *channel); +void g_io_channel_ref (GIOChannel *channel); +void g_io_channel_unref (GIOChannel *channel); +GIOError g_io_channel_read (GIOChannel *channel, gchar *buf, guint count, guint *bytes_read); diff --git a/glib/giochannel.c b/glib/giochannel.c index 0693a084b..a9bf10a8a 100644 --- a/glib/giochannel.c +++ b/glib/giochannel.c @@ -27,65 +27,30 @@ #include "glib.h" #include -typedef struct _GIOChannelPrivate GIOChannelPrivate; - -struct _GIOChannelPrivate { - GIOChannel channel; - GIOFuncs *funcs; - guint ref_count; - gboolean closed; -}; - -GIOChannel * -g_io_channel_new (GIOFuncs *funcs, - gpointer channel_data) +void +g_io_channel_init (GIOChannel *channel) { - GIOChannelPrivate *result; - GIOChannel *channel; - - g_return_val_if_fail (funcs != NULL, NULL); - - result = g_new (GIOChannelPrivate, 1); - channel = (GIOChannel *)result; - - result->funcs = funcs; - result->ref_count = 1; - result->closed = FALSE; - - channel->channel_data = channel_data; - return channel; + channel->channel_flags = 0; + channel->ref_count = 1; } void g_io_channel_ref (GIOChannel *channel) { - GIOChannelPrivate *private; g_return_if_fail (channel != NULL); - private = (GIOChannelPrivate *)channel; - - private->ref_count++; + channel->ref_count++; } void g_io_channel_unref (GIOChannel *channel) { - GIOChannelPrivate *private; g_return_if_fail (channel != NULL); - private = (GIOChannelPrivate *)channel; - - private->ref_count--; - if (private->ref_count == 0) - { - /* We don't want to close the channel here, because - * the channel may just be wrapping a file or socket - * that the app is independently manipulating. - */ - private->funcs->io_free (channel); - g_free (private); - } + channel->ref_count--; + if (channel->ref_count == 0) + channel->funcs->io_free (channel); } GIOError @@ -94,12 +59,9 @@ g_io_channel_read (GIOChannel *channel, guint count, guint *bytes_read) { - GIOChannelPrivate *private = (GIOChannelPrivate *)channel; - g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN); - g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN); - return private->funcs->io_read (channel, buf, count, bytes_read); + return channel->funcs->io_read (channel, buf, count, bytes_read); } GIOError @@ -108,12 +70,9 @@ g_io_channel_write (GIOChannel *channel, guint count, guint *bytes_written) { - GIOChannelPrivate *private = (GIOChannelPrivate *)channel; - g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN); - g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN); - return private->funcs->io_write (channel, buf, count, bytes_written); + return channel->funcs->io_write (channel, buf, count, bytes_written); } GIOError @@ -121,24 +80,17 @@ g_io_channel_seek (GIOChannel *channel, gint offset, GSeekType type) { - GIOChannelPrivate *private = (GIOChannelPrivate *)channel; - g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN); - g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN); - return private->funcs->io_seek (channel, offset, type); + return channel->funcs->io_seek (channel, offset, type); } void g_io_channel_close (GIOChannel *channel) { - GIOChannelPrivate *private = (GIOChannelPrivate *)channel; - g_return_if_fail (channel != NULL); - g_return_if_fail (!private->closed); - private->closed = TRUE; - private->funcs->io_close (channel); + channel->funcs->io_close (channel); } guint @@ -149,12 +101,9 @@ g_io_add_watch_full (GIOChannel *channel, gpointer user_data, GDestroyNotify notify) { - GIOChannelPrivate *private = (GIOChannelPrivate *)channel; - g_return_val_if_fail (channel != NULL, 0); - g_return_val_if_fail (!private->closed, 0); - return private->funcs->io_add_watch (channel, priority, condition, + return channel->funcs->io_add_watch (channel, priority, condition, func, user_data, notify); } diff --git a/glib/giounix.c b/glib/giounix.c index 82944be89..23fc69f0e 100644 --- a/glib/giounix.c +++ b/glib/giounix.c @@ -37,6 +37,7 @@ typedef struct _GIOUnixChannel GIOUnixChannel; typedef struct _GIOUnixWatch GIOUnixWatch; struct _GIOUnixChannel { + GIOChannel channel; gint fd; }; @@ -141,7 +142,7 @@ g_io_unix_read (GIOChannel *channel, guint count, guint *bytes_read) { - GIOUnixChannel *unix_channel = channel->channel_data; + GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel; gint result; result = read (unix_channel->fd, buf, count); @@ -172,7 +173,7 @@ g_io_unix_write(GIOChannel *channel, guint count, guint *bytes_written) { - GIOUnixChannel *unix_channel = channel->channel_data; + GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel; gint result; result = write (unix_channel->fd, buf, count); @@ -202,7 +203,7 @@ g_io_unix_seek (GIOChannel *channel, gint offset, GSeekType type) { - GIOUnixChannel *unix_channel = channel->channel_data; + GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel; int whence; off_t result; @@ -242,7 +243,7 @@ g_io_unix_seek (GIOChannel *channel, static void g_io_unix_close (GIOChannel *channel) { - GIOUnixChannel *unix_channel = channel->channel_data; + GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel; close (unix_channel->fd); } @@ -250,7 +251,7 @@ g_io_unix_close (GIOChannel *channel) static void g_io_unix_free (GIOChannel *channel) { - GIOUnixChannel *unix_channel = channel->channel_data; + GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel; g_free (unix_channel); } @@ -264,7 +265,7 @@ g_io_unix_add_watch (GIOChannel *channel, GDestroyNotify notify) { GIOUnixWatch *watch = g_new (GIOUnixWatch, 1); - GIOUnixChannel *unix_channel = channel->channel_data; + GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel; watch->channel = channel; g_io_channel_ref (channel); @@ -284,14 +285,18 @@ GIOChannel * g_io_channel_unix_new (gint fd) { GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1); + GIOChannel *channel = (GIOChannel *)unix_channel; + + g_io_channel_init (channel); + channel->funcs = &unix_channel_funcs; unix_channel->fd = fd; - return g_io_channel_new (&unix_channel_funcs, unix_channel); + return channel; } gint g_io_channel_unix_get_fd (GIOChannel *channel) { - GIOUnixChannel *unix_channel = channel->channel_data; + GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1); return unix_channel->fd; } diff --git a/glib/glib.h b/glib/glib.h index 5a3020b9e..4ea30970a 100644 --- a/glib/glib.h +++ b/glib/glib.h @@ -2341,8 +2341,11 @@ typedef enum { #endif } GIOCondition; -struct _GIOChannel { - gpointer channel_data; +struct _GIOChannel +{ + guint channel_flags; + guint ref_count; + GIOFuncs *funcs; }; typedef gboolean (*GIOFunc) (GIOChannel *source, @@ -2371,11 +2374,10 @@ struct _GIOFuncs { void (*io_free) (GIOChannel *channel); }; -GIOChannel *g_io_channel_new (GIOFuncs *funcs, - gpointer channel_data); -void g_io_channel_ref (GIOChannel *channel); -void g_io_channel_unref (GIOChannel *channel); -GIOError g_io_channel_read (GIOChannel *channel, +void g_io_channel_init (GIOChannel *channel); +void g_io_channel_ref (GIOChannel *channel); +void g_io_channel_unref (GIOChannel *channel); +GIOError g_io_channel_read (GIOChannel *channel, gchar *buf, guint count, guint *bytes_read);