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>
* 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>
* 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>
* 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>
* 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>
* 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>
* 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>
* 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>
* gdate.c (g_date_prepare_to_parse): Solaris has a broken strftime

View File

@ -27,65 +27,30 @@
#include "glib.h"
#include <unistd.h>
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);
}

View File

@ -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;
}

16
glib.h
View File

@ -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);

View File

@ -27,65 +27,30 @@
#include "glib.h"
#include <unistd.h>
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);
}

View File

@ -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;
}

View File

@ -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);