2 * QEMU I/O channels memory buffer driver
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "io/channel-buffer.h"
23 #include "io/channel-watch.h"
24 #include "qemu/sockets.h"
28 qio_channel_buffer_new(size_t capacity
)
30 QIOChannelBuffer
*ioc
;
32 ioc
= QIO_CHANNEL_BUFFER(object_new(TYPE_QIO_CHANNEL_BUFFER
));
35 ioc
->data
= g_new0(uint8_t, capacity
);
36 ioc
->capacity
= capacity
;
43 static void qio_channel_buffer_finalize(Object
*obj
)
45 QIOChannelBuffer
*ioc
= QIO_CHANNEL_BUFFER(obj
);
47 ioc
->capacity
= ioc
->usage
= ioc
->offset
= 0;
51 static ssize_t
qio_channel_buffer_readv(QIOChannel
*ioc
,
52 const struct iovec
*iov
,
58 QIOChannelBuffer
*bioc
= QIO_CHANNEL_BUFFER(ioc
);
62 for (i
= 0; i
< niov
; i
++) {
63 size_t want
= iov
[i
].iov_len
;
64 if (bioc
->offset
>= bioc
->usage
) {
67 if ((bioc
->offset
+ want
) > bioc
->usage
) {
68 want
= bioc
->usage
- bioc
->offset
;
70 memcpy(iov
[i
].iov_base
, bioc
->data
+ bioc
->offset
, want
);
78 static ssize_t
qio_channel_buffer_writev(QIOChannel
*ioc
,
79 const struct iovec
*iov
,
85 QIOChannelBuffer
*bioc
= QIO_CHANNEL_BUFFER(ioc
);
90 for (i
= 0; i
< niov
; i
++) {
91 towrite
+= iov
[i
].iov_len
;
94 if ((bioc
->offset
+ towrite
) > bioc
->capacity
) {
95 bioc
->capacity
= bioc
->offset
+ towrite
;
96 bioc
->data
= g_realloc(bioc
->data
, bioc
->capacity
);
99 if (bioc
->offset
> bioc
->usage
) {
100 memset(bioc
->data
, 0, bioc
->offset
- bioc
->usage
);
101 bioc
->usage
= bioc
->offset
;
104 for (i
= 0; i
< niov
; i
++) {
105 memcpy(bioc
->data
+ bioc
->usage
,
108 bioc
->usage
+= iov
[i
].iov_len
;
109 bioc
->offset
+= iov
[i
].iov_len
;
110 ret
+= iov
[i
].iov_len
;
116 static int qio_channel_buffer_set_blocking(QIOChannel
*ioc G_GNUC_UNUSED
,
117 bool enabled G_GNUC_UNUSED
,
118 Error
**errp G_GNUC_UNUSED
)
124 static off_t
qio_channel_buffer_seek(QIOChannel
*ioc
,
129 QIOChannelBuffer
*bioc
= QIO_CHANNEL_BUFFER(ioc
);
131 bioc
->offset
= offset
;
137 static int qio_channel_buffer_close(QIOChannel
*ioc
,
140 QIOChannelBuffer
*bioc
= QIO_CHANNEL_BUFFER(ioc
);
144 bioc
->capacity
= bioc
->usage
= bioc
->offset
= 0;
150 typedef struct QIOChannelBufferSource QIOChannelBufferSource
;
151 struct QIOChannelBufferSource
{
153 QIOChannelBuffer
*bioc
;
154 GIOCondition condition
;
158 qio_channel_buffer_source_prepare(GSource
*source
,
161 QIOChannelBufferSource
*bsource
= (QIOChannelBufferSource
*)source
;
165 return (G_IO_IN
| G_IO_OUT
) & bsource
->condition
;
169 qio_channel_buffer_source_check(GSource
*source
)
171 QIOChannelBufferSource
*bsource
= (QIOChannelBufferSource
*)source
;
173 return (G_IO_IN
| G_IO_OUT
) & bsource
->condition
;
177 qio_channel_buffer_source_dispatch(GSource
*source
,
178 GSourceFunc callback
,
181 QIOChannelFunc func
= (QIOChannelFunc
)callback
;
182 QIOChannelBufferSource
*bsource
= (QIOChannelBufferSource
*)source
;
184 return (*func
)(QIO_CHANNEL(bsource
->bioc
),
185 ((G_IO_IN
| G_IO_OUT
) & bsource
->condition
),
190 qio_channel_buffer_source_finalize(GSource
*source
)
192 QIOChannelBufferSource
*ssource
= (QIOChannelBufferSource
*)source
;
194 object_unref(OBJECT(ssource
->bioc
));
197 GSourceFuncs qio_channel_buffer_source_funcs
= {
198 qio_channel_buffer_source_prepare
,
199 qio_channel_buffer_source_check
,
200 qio_channel_buffer_source_dispatch
,
201 qio_channel_buffer_source_finalize
204 static GSource
*qio_channel_buffer_create_watch(QIOChannel
*ioc
,
205 GIOCondition condition
)
207 QIOChannelBuffer
*bioc
= QIO_CHANNEL_BUFFER(ioc
);
208 QIOChannelBufferSource
*ssource
;
211 source
= g_source_new(&qio_channel_buffer_source_funcs
,
212 sizeof(QIOChannelBufferSource
));
213 ssource
= (QIOChannelBufferSource
*)source
;
215 ssource
->bioc
= bioc
;
216 object_ref(OBJECT(bioc
));
218 ssource
->condition
= condition
;
224 static void qio_channel_buffer_class_init(ObjectClass
*klass
,
225 void *class_data G_GNUC_UNUSED
)
227 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
229 ioc_klass
->io_writev
= qio_channel_buffer_writev
;
230 ioc_klass
->io_readv
= qio_channel_buffer_readv
;
231 ioc_klass
->io_set_blocking
= qio_channel_buffer_set_blocking
;
232 ioc_klass
->io_seek
= qio_channel_buffer_seek
;
233 ioc_klass
->io_close
= qio_channel_buffer_close
;
234 ioc_klass
->io_create_watch
= qio_channel_buffer_create_watch
;
237 static const TypeInfo qio_channel_buffer_info
= {
238 .parent
= TYPE_QIO_CHANNEL
,
239 .name
= TYPE_QIO_CHANNEL_BUFFER
,
240 .instance_size
= sizeof(QIOChannelBuffer
),
241 .instance_finalize
= qio_channel_buffer_finalize
,
242 .class_init
= qio_channel_buffer_class_init
,
245 static void qio_channel_buffer_register_types(void)
247 type_register_static(&qio_channel_buffer_info
);
250 type_init(qio_channel_buffer_register_types
);