From: "Daniel P. Berrange" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
Gerd Hoffmann <kraxel@redhat.com>
Subject: [Qemu-devel] [PATCH v1 16/16] io: add QIOChannelBuffer class
Date: Fri, 18 Sep 2015 14:19:10 +0100 [thread overview]
Message-ID: <1442582350-9179-17-git-send-email-berrange@redhat.com> (raw)
In-Reply-To: <1442582350-9179-1-git-send-email-berrange@redhat.com>
Add a QIOChannel subclass that is capable of performing I/O
to/from a memory buffer. This implementation does not attempt
to support concurrent readers & writers. It is designed for
serialized access where by a single thread at a time may write
data, seek and then read data back out.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
include/io/channel-buffer.h | 59 ++++++++++
io/Makefile.objs | 1 +
io/channel-buffer.c | 255 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 315 insertions(+)
create mode 100644 include/io/channel-buffer.h
create mode 100644 io/channel-buffer.c
diff --git a/include/io/channel-buffer.h b/include/io/channel-buffer.h
new file mode 100644
index 0000000..1e0f6c2
--- /dev/null
+++ b/include/io/channel-buffer.h
@@ -0,0 +1,59 @@
+/*
+ * QEMU I/O channels memory buffer driver
+ *
+ * Copyright (c) 2015 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef QIO_CHANNEL_BUFFER_H__
+#define QIO_CHANNEL_BUFFER_H__
+
+#include "io/channel.h"
+
+#define TYPE_QIO_CHANNEL_BUFFER "qio-channel-buffer"
+#define QIO_CHANNEL_BUFFER(obj) \
+ OBJECT_CHECK(QIOChannelBuffer, (obj), TYPE_QIO_CHANNEL_BUFFER)
+
+typedef struct QIOChannelBuffer QIOChannelBuffer;
+
+/**
+ * QIOChannelBuffer:
+ *
+ * The QIOChannelBuffer object provides a channel implementation
+ * that is able to perform I/O to/from a memory buffer.
+ *
+ */
+
+struct QIOChannelBuffer {
+ QIOChannel parent;
+ size_t capacity; /* Total allocated memory */
+ size_t usage; /* Current size of data */
+ size_t offset; /* Offset for future I/O ops */
+ char *data;
+};
+
+
+/**
+ * qio_channel_buffer_new:
+ *
+ * Allocate a new buffer which is initially empty
+ *
+ * Returns: the new channel object
+ */
+QIOChannelBuffer *
+qio_channel_buffer_new(void);
+
+#endif /* QIO_CHANNEL_BUFFER_H__ */
diff --git a/io/Makefile.objs b/io/Makefile.objs
index 9e28ee5..5e47e6c 100644
--- a/io/Makefile.objs
+++ b/io/Makefile.objs
@@ -7,3 +7,4 @@ io-obj-y += channel-file.o
io-obj-y += channel-tls.o
io-obj-y += channel-websock.o
io-obj-y += channel-command.o
+io-obj-y += channel-buffer.o
diff --git a/io/channel-buffer.c b/io/channel-buffer.c
new file mode 100644
index 0000000..8d85032
--- /dev/null
+++ b/io/channel-buffer.c
@@ -0,0 +1,255 @@
+/*
+ * QEMU I/O channels memory buffer driver
+ *
+ * Copyright (c) 2015 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "io/channel-buffer.h"
+#include "io/channel-watch.h"
+#include "qemu/sockets.h"
+#include "trace.h"
+
+QIOChannelBuffer *
+qio_channel_buffer_new(void)
+{
+ QIOChannelBuffer *ioc;
+
+ ioc = QIO_CHANNEL_BUFFER(object_new(TYPE_QIO_CHANNEL_BUFFER));
+
+ return ioc;
+}
+
+
+static void qio_channel_buffer_finalize(Object *obj)
+{
+ QIOChannelBuffer *ioc = QIO_CHANNEL_BUFFER(obj);
+ g_free(ioc->data);
+ ioc->capacity = ioc->usage = ioc->offset = 0;
+}
+
+
+static ssize_t qio_channel_buffer_readv(QIOChannel *ioc,
+ const struct iovec *iov,
+ size_t niov,
+ int **fds,
+ size_t *nfds,
+ Error **errp)
+{
+ QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
+ ssize_t ret = 0;
+ size_t i;
+
+ if (fds || nfds) {
+ error_setg_errno(errp, EINVAL,
+ "Channel does not support buffer descriptor passing");
+ return -1;
+ }
+
+ for (i = 0; i < niov; i++) {
+ size_t want = iov[i].iov_len;
+ if (bioc->offset >= bioc->usage) {
+ break;
+ }
+ if ((bioc->offset + want) > bioc->usage) {
+ want = bioc->usage - bioc->offset;
+ }
+ memcpy(iov[i].iov_base, bioc->data + bioc->offset, want);
+ ret += want;
+ }
+
+ return ret;
+}
+
+static ssize_t qio_channel_buffer_writev(QIOChannel *ioc,
+ const struct iovec *iov,
+ size_t niov,
+ int *fds,
+ size_t nfds,
+ Error **errp)
+{
+ QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
+ ssize_t ret = 0;
+ size_t i;
+ size_t towrite = 0;
+
+ if (fds || nfds) {
+ error_setg_errno(errp, EINVAL,
+ "Channel does not support buffer descriptor passing");
+ return -1;
+ }
+
+ for (i = 0; i < niov; i++) {
+ towrite += iov[i].iov_len;
+ }
+
+ if ((bioc->offset + towrite) > bioc->capacity) {
+ bioc->capacity = bioc->offset + towrite;
+ bioc->data = g_realloc(bioc->data, bioc->capacity);
+ }
+
+ if (bioc->offset > bioc->usage) {
+ memset(bioc->data, 0, bioc->offset - bioc->usage);
+ bioc->usage = bioc->offset;
+ }
+
+ for (i = 0; i < niov; i++) {
+ memcpy(bioc->data + bioc->usage,
+ iov[i].iov_base,
+ iov[i].iov_len);
+ bioc->usage += iov[i].iov_len;
+ bioc->offset += iov[i].iov_len;
+ ret += iov[i].iov_len;
+ }
+
+ return ret;
+}
+
+static int qio_channel_buffer_set_blocking(QIOChannel *ioc G_GNUC_UNUSED,
+ bool enabled G_GNUC_UNUSED,
+ Error **errp G_GNUC_UNUSED)
+{
+ return 0;
+}
+
+
+static off64_t qio_channel_buffer_seek(QIOChannel *ioc,
+ off64_t offset,
+ int whence,
+ Error **errp)
+{
+ QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
+
+ bioc->offset = offset;
+
+ return offset;
+}
+
+
+static int qio_channel_buffer_close(QIOChannel *ioc,
+ Error **errp)
+{
+ QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
+
+ g_free(bioc->data);
+ bioc->capacity = bioc->usage = bioc->offset = 0;
+
+ return 0;
+}
+
+
+typedef struct QIOChannelBufferSource QIOChannelBufferSource;
+struct QIOChannelBufferSource {
+ GSource parent;
+ QIOChannelBuffer *bioc;
+ GIOCondition condition;
+};
+
+static gboolean
+qio_channel_buffer_source_prepare(GSource *source,
+ gint *timeout)
+{
+ QIOChannelBufferSource *bsource = (QIOChannelBufferSource *)source;
+
+ *timeout = -1;
+
+ return (G_IO_IN | G_IO_OUT) & bsource->condition;
+}
+
+static gboolean
+qio_channel_buffer_source_check(GSource *source)
+{
+ QIOChannelBufferSource *bsource = (QIOChannelBufferSource *)source;
+
+ return (G_IO_IN | G_IO_OUT) & bsource->condition;
+}
+
+static gboolean
+qio_channel_buffer_source_dispatch(GSource *source,
+ GSourceFunc callback,
+ gpointer user_data)
+{
+ QIOChannelFunc func = (QIOChannelFunc)callback;
+ QIOChannelBufferSource *bsource = (QIOChannelBufferSource *)source;
+
+ return (*func)(QIO_CHANNEL(bsource->bioc),
+ ((G_IO_IN | G_IO_OUT) & bsource->condition),
+ user_data);
+}
+
+static void
+qio_channel_buffer_source_finalize(GSource *source)
+{
+ QIOChannelBufferSource *ssource = (QIOChannelBufferSource *)source;
+
+ object_unref(OBJECT(ssource->bioc));
+}
+
+GSourceFuncs qio_channel_buffer_source_funcs = {
+ qio_channel_buffer_source_prepare,
+ qio_channel_buffer_source_check,
+ qio_channel_buffer_source_dispatch,
+ qio_channel_buffer_source_finalize
+};
+
+static GSource *qio_channel_buffer_create_watch(QIOChannel *ioc,
+ GIOCondition condition)
+{
+ QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
+ QIOChannelBufferSource *ssource;
+ GSource *source;
+
+ source = g_source_new(&qio_channel_buffer_source_funcs,
+ sizeof(QIOChannelBufferSource));
+ g_source_set_name(source, "QIOChannelBuffer");
+ ssource = (QIOChannelBufferSource *)source;
+
+ ssource->bioc = bioc;
+ object_ref(OBJECT(bioc));
+
+ ssource->condition = condition;
+
+ return source;
+}
+
+
+static void qio_channel_buffer_class_init(ObjectClass *klass,
+ void *class_data G_GNUC_UNUSED)
+{
+ QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
+
+ ioc_klass->io_writev = qio_channel_buffer_writev;
+ ioc_klass->io_readv = qio_channel_buffer_readv;
+ ioc_klass->io_set_blocking = qio_channel_buffer_set_blocking;
+ ioc_klass->io_seek = qio_channel_buffer_seek;
+ ioc_klass->io_close = qio_channel_buffer_close;
+ ioc_klass->io_create_watch = qio_channel_buffer_create_watch;
+}
+
+static const TypeInfo qio_channel_buffer_info = {
+ .parent = TYPE_QIO_CHANNEL,
+ .name = TYPE_QIO_CHANNEL_BUFFER,
+ .instance_size = sizeof(QIOChannelBuffer),
+ .instance_finalize = qio_channel_buffer_finalize,
+ .class_init = qio_channel_buffer_class_init,
+};
+
+static void qio_channel_buffer_register_types(void)
+{
+ type_register_static(&qio_channel_buffer_info);
+}
+
+type_init(qio_channel_buffer_register_types);
--
2.4.3
next prev parent reply other threads:[~2015-09-18 13:36 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-18 13:18 [Qemu-devel] [PATCH v1 00/16] Introduce I/O channels framework Daniel P. Berrange
2015-09-18 13:18 ` [Qemu-devel] [PATCH v1 01/16] sockets: add helpers for creating SocketAddress from a socket Daniel P. Berrange
2015-09-18 13:18 ` [Qemu-devel] [PATCH v1 02/16] sockets: move qapi_copy_SocketAddress into qemu-sockets.c Daniel P. Berrange
2015-09-18 13:18 ` [Qemu-devel] [PATCH v1 03/16] sockets: allow port to be NULL when listening on IP address Daniel P. Berrange
2015-09-18 13:18 ` [Qemu-devel] [PATCH v1 04/16] ui: convert VNC startup code to use SocketAddress Daniel P. Berrange
2015-09-18 13:18 ` [Qemu-devel] [PATCH v1 05/16] osdep: add qemu_fork() wrapper for safely handling signals Daniel P. Berrange
2015-09-18 13:19 ` [Qemu-devel] [PATCH v1 06/16] coroutine: move into libqemuutil.a library Daniel P. Berrange
2015-09-22 12:07 ` Paolo Bonzini
2015-09-24 14:46 ` Daniel P. Berrange
2015-09-18 13:19 ` [Qemu-devel] [PATCH v1 07/16] io: add abstract QIOChannel classes Daniel P. Berrange
2015-09-22 12:19 ` Paolo Bonzini
2015-09-22 12:24 ` Daniel P. Berrange
2015-09-22 12:28 ` Paolo Bonzini
2015-09-24 14:47 ` Daniel P. Berrange
2015-09-18 13:19 ` [Qemu-devel] [PATCH v1 08/16] io: add helper module for creating watches on FDs Daniel P. Berrange
2015-09-18 13:19 ` [Qemu-devel] [PATCH v1 09/16] io: pull Buffer code out of VNC module Daniel P. Berrange
2015-09-22 12:04 ` Paolo Bonzini
2015-09-22 12:20 ` Daniel P. Berrange
2015-09-22 12:21 ` Paolo Bonzini
2015-09-18 13:19 ` [Qemu-devel] [PATCH v1 10/16] io: add QIOTask class for async operations Daniel P. Berrange
2015-09-18 13:19 ` [Qemu-devel] [PATCH v1 11/16] io: add QIOChannelSocket class Daniel P. Berrange
2015-09-18 13:19 ` [Qemu-devel] [PATCH v1 12/16] io: add QIOChannelFile class Daniel P. Berrange
2015-09-18 13:19 ` [Qemu-devel] [PATCH v1 13/16] io: add QIOChannelTLS class Daniel P. Berrange
2015-09-18 13:19 ` [Qemu-devel] [PATCH v1 14/16] io: add QIOChannelWebsock class Daniel P. Berrange
2015-09-18 13:19 ` [Qemu-devel] [PATCH v1 15/16] io: add QIOChannelCommand class Daniel P. Berrange
2015-09-18 13:19 ` Daniel P. Berrange [this message]
2015-09-25 10:15 ` [Qemu-devel] [PATCH v1 16/16] io: add QIOChannelBuffer class Paolo Bonzini
2015-09-25 10:21 ` Daniel P. Berrange
2015-09-22 12:23 ` [Qemu-devel] [PATCH v1 00/16] Introduce I/O channels framework Paolo Bonzini
2015-09-24 14:51 ` Daniel P. Berrange
2015-09-25 10:18 ` Paolo Bonzini
2015-09-25 10:20 ` Daniel P. Berrange
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1442582350-9179-17-git-send-email-berrange@redhat.com \
--to=berrange@redhat.com \
--cc=dgilbert@redhat.com \
--cc=kraxel@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.