Use an "inheritance" scheme for IO channel memory allocation.h

Tue Dec 15 17:17:46 1998  Owen Taylor  <otaylor@redhat.com>

	* glib.h giounix.c giochannel.c: Use an "inheritance"
	scheme for IO channel memory allocation.h
This commit is contained in:
Owen Taylor 1998-12-15 22:30:15 +00:00 committed by Owen Taylor
parent 5ccc68d010
commit 05d49ddbd3
14 changed files with 110 additions and 158 deletions

View File

@ -1,3 +1,8 @@
Tue Dec 15 17:17:46 1998 Owen Taylor <otaylor@redhat.com>
* glib.h giounix.c giochannel.c: Use an "inheritance"
scheme for IO channel memory allocation.h
1998-12-15 Havoc Pennington <hp@pobox.com> 1998-12-15 Havoc Pennington <hp@pobox.com>
* gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime

View File

@ -1,3 +1,8 @@
Tue Dec 15 17:17:46 1998 Owen Taylor <otaylor@redhat.com>
* glib.h giounix.c giochannel.c: Use an "inheritance"
scheme for IO channel memory allocation.h
1998-12-15 Havoc Pennington <hp@pobox.com> 1998-12-15 Havoc Pennington <hp@pobox.com>
* gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime

View File

@ -1,3 +1,8 @@
Tue Dec 15 17:17:46 1998 Owen Taylor <otaylor@redhat.com>
* glib.h giounix.c giochannel.c: Use an "inheritance"
scheme for IO channel memory allocation.h
1998-12-15 Havoc Pennington <hp@pobox.com> 1998-12-15 Havoc Pennington <hp@pobox.com>
* gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime

View File

@ -1,3 +1,8 @@
Tue Dec 15 17:17:46 1998 Owen Taylor <otaylor@redhat.com>
* glib.h giounix.c giochannel.c: Use an "inheritance"
scheme for IO channel memory allocation.h
1998-12-15 Havoc Pennington <hp@pobox.com> 1998-12-15 Havoc Pennington <hp@pobox.com>
* gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime

View File

@ -1,3 +1,8 @@
Tue Dec 15 17:17:46 1998 Owen Taylor <otaylor@redhat.com>
* glib.h giounix.c giochannel.c: Use an "inheritance"
scheme for IO channel memory allocation.h
1998-12-15 Havoc Pennington <hp@pobox.com> 1998-12-15 Havoc Pennington <hp@pobox.com>
* gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime

View File

@ -1,3 +1,8 @@
Tue Dec 15 17:17:46 1998 Owen Taylor <otaylor@redhat.com>
* glib.h giounix.c giochannel.c: Use an "inheritance"
scheme for IO channel memory allocation.h
1998-12-15 Havoc Pennington <hp@pobox.com> 1998-12-15 Havoc Pennington <hp@pobox.com>
* gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime

View File

@ -1,3 +1,8 @@
Tue Dec 15 17:17:46 1998 Owen Taylor <otaylor@redhat.com>
* glib.h giounix.c giochannel.c: Use an "inheritance"
scheme for IO channel memory allocation.h
1998-12-15 Havoc Pennington <hp@pobox.com> 1998-12-15 Havoc Pennington <hp@pobox.com>
* gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime

View File

@ -1,3 +1,8 @@
Tue Dec 15 17:17:46 1998 Owen Taylor <otaylor@redhat.com>
* glib.h giounix.c giochannel.c: Use an "inheritance"
scheme for IO channel memory allocation.h
1998-12-15 Havoc Pennington <hp@pobox.com> 1998-12-15 Havoc Pennington <hp@pobox.com>
* gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime * gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime

View File

@ -27,65 +27,30 @@
#include "glib.h" #include "glib.h"
#include <unistd.h> #include <unistd.h>
typedef struct _GIOChannelPrivate GIOChannelPrivate; void
g_io_channel_init (GIOChannel *channel)
struct _GIOChannelPrivate {
GIOChannel channel;
GIOFuncs *funcs;
guint ref_count;
gboolean closed;
};
GIOChannel *
g_io_channel_new (GIOFuncs *funcs,
gpointer channel_data)
{ {
GIOChannelPrivate *result; channel->channel_flags = 0;
GIOChannel *channel; channel->ref_count = 1;
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;
} }
void void
g_io_channel_ref (GIOChannel *channel) g_io_channel_ref (GIOChannel *channel)
{ {
GIOChannelPrivate *private;
g_return_if_fail (channel != NULL); g_return_if_fail (channel != NULL);
private = (GIOChannelPrivate *)channel; channel->ref_count++;
private->ref_count++;
} }
void void
g_io_channel_unref (GIOChannel *channel) g_io_channel_unref (GIOChannel *channel)
{ {
GIOChannelPrivate *private;
g_return_if_fail (channel != NULL); g_return_if_fail (channel != NULL);
private = (GIOChannelPrivate *)channel; channel->ref_count--;
if (channel->ref_count == 0)
private->ref_count--; channel->funcs->io_free (channel);
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);
}
} }
GIOError GIOError
@ -94,12 +59,9 @@ g_io_channel_read (GIOChannel *channel,
guint count, guint count,
guint *bytes_read) guint *bytes_read)
{ {
GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN); 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 GIOError
@ -108,12 +70,9 @@ g_io_channel_write (GIOChannel *channel,
guint count, guint count,
guint *bytes_written) guint *bytes_written)
{ {
GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN); 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 GIOError
@ -121,24 +80,17 @@ g_io_channel_seek (GIOChannel *channel,
gint offset, gint offset,
GSeekType type) GSeekType type)
{ {
GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN); 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 void
g_io_channel_close (GIOChannel *channel) g_io_channel_close (GIOChannel *channel)
{ {
GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
g_return_if_fail (channel != NULL); g_return_if_fail (channel != NULL);
g_return_if_fail (!private->closed);
private->closed = TRUE; channel->funcs->io_close (channel);
private->funcs->io_close (channel);
} }
guint guint
@ -149,12 +101,9 @@ g_io_add_watch_full (GIOChannel *channel,
gpointer user_data, gpointer user_data,
GDestroyNotify notify) GDestroyNotify notify)
{ {
GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
g_return_val_if_fail (channel != NULL, 0); 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); func, user_data, notify);
} }

View File

@ -37,6 +37,7 @@ typedef struct _GIOUnixChannel GIOUnixChannel;
typedef struct _GIOUnixWatch GIOUnixWatch; typedef struct _GIOUnixWatch GIOUnixWatch;
struct _GIOUnixChannel { struct _GIOUnixChannel {
GIOChannel channel;
gint fd; gint fd;
}; };
@ -141,7 +142,7 @@ g_io_unix_read (GIOChannel *channel,
guint count, guint count,
guint *bytes_read) guint *bytes_read)
{ {
GIOUnixChannel *unix_channel = channel->channel_data; GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
gint result; gint result;
result = read (unix_channel->fd, buf, count); result = read (unix_channel->fd, buf, count);
@ -172,7 +173,7 @@ g_io_unix_write(GIOChannel *channel,
guint count, guint count,
guint *bytes_written) guint *bytes_written)
{ {
GIOUnixChannel *unix_channel = channel->channel_data; GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
gint result; gint result;
result = write (unix_channel->fd, buf, count); result = write (unix_channel->fd, buf, count);
@ -202,7 +203,7 @@ g_io_unix_seek (GIOChannel *channel,
gint offset, gint offset,
GSeekType type) GSeekType type)
{ {
GIOUnixChannel *unix_channel = channel->channel_data; GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
int whence; int whence;
off_t result; off_t result;
@ -242,7 +243,7 @@ g_io_unix_seek (GIOChannel *channel,
static void static void
g_io_unix_close (GIOChannel *channel) g_io_unix_close (GIOChannel *channel)
{ {
GIOUnixChannel *unix_channel = channel->channel_data; GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
close (unix_channel->fd); close (unix_channel->fd);
} }
@ -250,7 +251,7 @@ g_io_unix_close (GIOChannel *channel)
static void static void
g_io_unix_free (GIOChannel *channel) g_io_unix_free (GIOChannel *channel)
{ {
GIOUnixChannel *unix_channel = channel->channel_data; GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
g_free (unix_channel); g_free (unix_channel);
} }
@ -264,7 +265,7 @@ g_io_unix_add_watch (GIOChannel *channel,
GDestroyNotify notify) GDestroyNotify notify)
{ {
GIOUnixWatch *watch = g_new (GIOUnixWatch, 1); GIOUnixWatch *watch = g_new (GIOUnixWatch, 1);
GIOUnixChannel *unix_channel = channel->channel_data; GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
watch->channel = channel; watch->channel = channel;
g_io_channel_ref (channel); g_io_channel_ref (channel);
@ -284,14 +285,18 @@ GIOChannel *
g_io_channel_unix_new (gint fd) g_io_channel_unix_new (gint fd)
{ {
GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1); 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; unix_channel->fd = fd;
return g_io_channel_new (&unix_channel_funcs, unix_channel); return channel;
} }
gint gint
g_io_channel_unix_get_fd (GIOChannel *channel) 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; return unix_channel->fd;
} }

10
glib.h
View File

@ -2341,8 +2341,11 @@ typedef enum {
#endif #endif
} GIOCondition; } GIOCondition;
struct _GIOChannel { struct _GIOChannel
gpointer channel_data; {
guint channel_flags;
guint ref_count;
GIOFuncs *funcs;
}; };
typedef gboolean (*GIOFunc) (GIOChannel *source, typedef gboolean (*GIOFunc) (GIOChannel *source,
@ -2371,8 +2374,7 @@ struct _GIOFuncs {
void (*io_free) (GIOChannel *channel); void (*io_free) (GIOChannel *channel);
}; };
GIOChannel *g_io_channel_new (GIOFuncs *funcs, void g_io_channel_init (GIOChannel *channel);
gpointer channel_data);
void g_io_channel_ref (GIOChannel *channel); void g_io_channel_ref (GIOChannel *channel);
void g_io_channel_unref (GIOChannel *channel); void g_io_channel_unref (GIOChannel *channel);
GIOError g_io_channel_read (GIOChannel *channel, GIOError g_io_channel_read (GIOChannel *channel,

View File

@ -27,65 +27,30 @@
#include "glib.h" #include "glib.h"
#include <unistd.h> #include <unistd.h>
typedef struct _GIOChannelPrivate GIOChannelPrivate; void
g_io_channel_init (GIOChannel *channel)
struct _GIOChannelPrivate {
GIOChannel channel;
GIOFuncs *funcs;
guint ref_count;
gboolean closed;
};
GIOChannel *
g_io_channel_new (GIOFuncs *funcs,
gpointer channel_data)
{ {
GIOChannelPrivate *result; channel->channel_flags = 0;
GIOChannel *channel; channel->ref_count = 1;
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;
} }
void void
g_io_channel_ref (GIOChannel *channel) g_io_channel_ref (GIOChannel *channel)
{ {
GIOChannelPrivate *private;
g_return_if_fail (channel != NULL); g_return_if_fail (channel != NULL);
private = (GIOChannelPrivate *)channel; channel->ref_count++;
private->ref_count++;
} }
void void
g_io_channel_unref (GIOChannel *channel) g_io_channel_unref (GIOChannel *channel)
{ {
GIOChannelPrivate *private;
g_return_if_fail (channel != NULL); g_return_if_fail (channel != NULL);
private = (GIOChannelPrivate *)channel; channel->ref_count--;
if (channel->ref_count == 0)
private->ref_count--; channel->funcs->io_free (channel);
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);
}
} }
GIOError GIOError
@ -94,12 +59,9 @@ g_io_channel_read (GIOChannel *channel,
guint count, guint count,
guint *bytes_read) guint *bytes_read)
{ {
GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN); 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 GIOError
@ -108,12 +70,9 @@ g_io_channel_write (GIOChannel *channel,
guint count, guint count,
guint *bytes_written) guint *bytes_written)
{ {
GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN); 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 GIOError
@ -121,24 +80,17 @@ g_io_channel_seek (GIOChannel *channel,
gint offset, gint offset,
GSeekType type) GSeekType type)
{ {
GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN); 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 void
g_io_channel_close (GIOChannel *channel) g_io_channel_close (GIOChannel *channel)
{ {
GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
g_return_if_fail (channel != NULL); g_return_if_fail (channel != NULL);
g_return_if_fail (!private->closed);
private->closed = TRUE; channel->funcs->io_close (channel);
private->funcs->io_close (channel);
} }
guint guint
@ -149,12 +101,9 @@ g_io_add_watch_full (GIOChannel *channel,
gpointer user_data, gpointer user_data,
GDestroyNotify notify) GDestroyNotify notify)
{ {
GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
g_return_val_if_fail (channel != NULL, 0); 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); func, user_data, notify);
} }

View File

@ -37,6 +37,7 @@ typedef struct _GIOUnixChannel GIOUnixChannel;
typedef struct _GIOUnixWatch GIOUnixWatch; typedef struct _GIOUnixWatch GIOUnixWatch;
struct _GIOUnixChannel { struct _GIOUnixChannel {
GIOChannel channel;
gint fd; gint fd;
}; };
@ -141,7 +142,7 @@ g_io_unix_read (GIOChannel *channel,
guint count, guint count,
guint *bytes_read) guint *bytes_read)
{ {
GIOUnixChannel *unix_channel = channel->channel_data; GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
gint result; gint result;
result = read (unix_channel->fd, buf, count); result = read (unix_channel->fd, buf, count);
@ -172,7 +173,7 @@ g_io_unix_write(GIOChannel *channel,
guint count, guint count,
guint *bytes_written) guint *bytes_written)
{ {
GIOUnixChannel *unix_channel = channel->channel_data; GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
gint result; gint result;
result = write (unix_channel->fd, buf, count); result = write (unix_channel->fd, buf, count);
@ -202,7 +203,7 @@ g_io_unix_seek (GIOChannel *channel,
gint offset, gint offset,
GSeekType type) GSeekType type)
{ {
GIOUnixChannel *unix_channel = channel->channel_data; GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
int whence; int whence;
off_t result; off_t result;
@ -242,7 +243,7 @@ g_io_unix_seek (GIOChannel *channel,
static void static void
g_io_unix_close (GIOChannel *channel) g_io_unix_close (GIOChannel *channel)
{ {
GIOUnixChannel *unix_channel = channel->channel_data; GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
close (unix_channel->fd); close (unix_channel->fd);
} }
@ -250,7 +251,7 @@ g_io_unix_close (GIOChannel *channel)
static void static void
g_io_unix_free (GIOChannel *channel) g_io_unix_free (GIOChannel *channel)
{ {
GIOUnixChannel *unix_channel = channel->channel_data; GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
g_free (unix_channel); g_free (unix_channel);
} }
@ -264,7 +265,7 @@ g_io_unix_add_watch (GIOChannel *channel,
GDestroyNotify notify) GDestroyNotify notify)
{ {
GIOUnixWatch *watch = g_new (GIOUnixWatch, 1); GIOUnixWatch *watch = g_new (GIOUnixWatch, 1);
GIOUnixChannel *unix_channel = channel->channel_data; GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
watch->channel = channel; watch->channel = channel;
g_io_channel_ref (channel); g_io_channel_ref (channel);
@ -284,14 +285,18 @@ GIOChannel *
g_io_channel_unix_new (gint fd) g_io_channel_unix_new (gint fd)
{ {
GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1); 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; unix_channel->fd = fd;
return g_io_channel_new (&unix_channel_funcs, unix_channel); return channel;
} }
gint gint
g_io_channel_unix_get_fd (GIOChannel *channel) 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; return unix_channel->fd;
} }

View File

@ -2341,8 +2341,11 @@ typedef enum {
#endif #endif
} GIOCondition; } GIOCondition;
struct _GIOChannel { struct _GIOChannel
gpointer channel_data; {
guint channel_flags;
guint ref_count;
GIOFuncs *funcs;
}; };
typedef gboolean (*GIOFunc) (GIOChannel *source, typedef gboolean (*GIOFunc) (GIOChannel *source,
@ -2371,8 +2374,7 @@ struct _GIOFuncs {
void (*io_free) (GIOChannel *channel); void (*io_free) (GIOChannel *channel);
}; };
GIOChannel *g_io_channel_new (GIOFuncs *funcs, void g_io_channel_init (GIOChannel *channel);
gpointer channel_data);
void g_io_channel_ref (GIOChannel *channel); void g_io_channel_ref (GIOChannel *channel);
void g_io_channel_unref (GIOChannel *channel); void g_io_channel_unref (GIOChannel *channel);
GIOError g_io_channel_read (GIOChannel *channel, GIOError g_io_channel_read (GIOChannel *channel,