Use an "inheritance" scheme for IO channel memory allocation.h
[platform/upstream/glib.git] / glib / giochannel.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * giochannel.c: IO Channel abstraction
5  * Copyright 1998 Owen Taylor
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /* 
24  * MT safe
25  */
26
27 #include "glib.h"
28 #include <unistd.h>
29
30 void
31 g_io_channel_init (GIOChannel *channel)
32 {
33   channel->channel_flags = 0;
34   channel->ref_count = 1;
35 }
36
37
38 void 
39 g_io_channel_ref (GIOChannel *channel)
40 {
41   g_return_if_fail (channel != NULL);
42
43   channel->ref_count++;
44 }
45
46 void 
47 g_io_channel_unref (GIOChannel *channel)
48 {
49   g_return_if_fail (channel != NULL);
50
51   channel->ref_count--;
52   if (channel->ref_count == 0)
53     channel->funcs->io_free (channel);
54 }
55
56 GIOError 
57 g_io_channel_read (GIOChannel *channel, 
58                    gchar      *buf, 
59                    guint       count,
60                    guint      *bytes_read)
61 {
62   g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
63
64   return channel->funcs->io_read (channel, buf, count, bytes_read);
65 }
66
67 GIOError 
68 g_io_channel_write (GIOChannel *channel, 
69                     gchar      *buf, 
70                     guint       count,
71                     guint      *bytes_written)
72 {
73   g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
74
75   return channel->funcs->io_write (channel, buf, count, bytes_written);
76 }
77
78 GIOError 
79 g_io_channel_seek  (GIOChannel   *channel,
80                     gint        offset, 
81                     GSeekType   type)
82 {
83   g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
84
85   return channel->funcs->io_seek (channel, offset, type);
86 }
87      
88 void
89 g_io_channel_close (GIOChannel *channel)
90 {
91   g_return_if_fail (channel != NULL);
92
93   channel->funcs->io_close (channel);
94 }
95
96 guint 
97 g_io_add_watch_full (GIOChannel      *channel,
98                      gint           priority,
99                      GIOCondition   condition,
100                      GIOFunc        func,
101                      gpointer       user_data,
102                      GDestroyNotify notify)
103 {
104   g_return_val_if_fail (channel != NULL, 0);
105
106   return channel->funcs->io_add_watch (channel, priority, condition,
107                                        func, user_data, notify);
108 }
109
110 guint 
111 g_io_add_watch (GIOChannel      *channel,
112                 GIOCondition   condition,
113                 GIOFunc        func,
114                 gpointer       user_data)
115 {
116   return g_io_add_watch_full (channel, 0, condition, func, user_data, NULL);
117 }