qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Anthony Liguori <aliguori@us.ibm.com>
To: qemu-devel@nongnu.org
Cc: Anthony Liguori <aliguori@us.ibm.com>
Subject: [Qemu-devel] [PATCH 18/21] qom-chrdrv: add memory character driver
Date: Sun, 24 Jul 2011 20:44:50 -0500	[thread overview]
Message-ID: <1311558293-5855-19-git-send-email-aliguori@us.ibm.com> (raw)
In-Reply-To: <1311558293-5855-1-git-send-email-aliguori@us.ibm.com>

This is used primarly by QMP.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 chrdrv/Makefile       |    1 +
 chrdrv/Qconfig        |    7 +++++
 chrdrv/memchr.c       |   70 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/qemu/memchr.h |   46 ++++++++++++++++++++++++++++++++
 4 files changed, 124 insertions(+), 0 deletions(-)
 create mode 100644 chrdrv/memchr.c
 create mode 100644 include/qemu/memchr.h

diff --git a/chrdrv/Makefile b/chrdrv/Makefile
index 43a51e7..6cfdd27 100644
--- a/chrdrv/Makefile
+++ b/chrdrv/Makefile
@@ -1 +1,2 @@
 chrdrv-obj-$(CONFIG_CHRDRV) := chrdrv.o
+chrdrv-obj-$(CONFIG_CHRDRV_MEM) += memchr.o
diff --git a/chrdrv/Qconfig b/chrdrv/Qconfig
index 845c205..f3f939e 100644
--- a/chrdrv/Qconfig
+++ b/chrdrv/Qconfig
@@ -3,3 +3,10 @@ config CHRDRV
        default y
        help
          Character layer
+
+config CHRDRV_MEM
+       bool "Memory character device"
+       default y
+       depends on CHRDRV
+       help
+         Character device that stores all written data to a memory buffer.
diff --git a/chrdrv/memchr.c b/chrdrv/memchr.c
new file mode 100644
index 0000000..3046d2e
--- /dev/null
+++ b/chrdrv/memchr.c
@@ -0,0 +1,70 @@
+#include "qemu/memchr.h"
+
+void memory_char_driver_initialize(MemoryCharDriver *d, const char *id)
+{
+    type_initialize(d, TYPE_MEMORY_CHAR_DRIVER, id);
+}
+
+void memory_char_driver_finalize(MemoryCharDriver *d)
+{
+    type_finalize(d);
+}
+
+static int memory_char_driver_write(CharDriver *chr, const uint8_t *buf,
+                                    int len)
+{
+    MemoryCharDriver *d = MEMORY_CHAR_DRIVER(chr);
+
+    /* TODO: the QString implementation has the same code, we should
+     * introduce a generic way to do this in cutils.c */
+    if (d->outbuf_capacity < d->outbuf_size + len) {
+        /* grow outbuf */
+        d->outbuf_capacity += len;
+        d->outbuf_capacity *= 2;
+        d->outbuf = qemu_realloc(d->outbuf, d->outbuf_capacity);
+    }
+
+    memcpy(d->outbuf + d->outbuf_size, buf, len);
+    d->outbuf_size += len;
+
+    return len;
+}
+
+size_t memory_char_driver_get_osize(MemoryCharDriver *d)
+{
+    return d->outbuf_size;
+}
+
+QString *memory_char_driver_get_qs(MemoryCharDriver *d)
+{
+    return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
+}
+
+static void memory_char_driver_fini(TypeInstance *inst)
+{
+    MemoryCharDriver *d = MEMORY_CHAR_DRIVER(inst);
+
+    qemu_free(d->outbuf);
+}
+
+static void memory_char_driver_class_init(TypeClass *class)
+{
+    CharDriverClass *cdc = CHAR_DRIVER_CLASS(class);
+
+    cdc->write = memory_char_driver_write;
+}
+
+static TypeInfo memory_char_driver_type_info = {
+    .name = TYPE_MEMORY_CHAR_DRIVER,
+    .parent = TYPE_CHAR_DRIVER,
+    .instance_size = sizeof(MemoryCharDriver),
+    .class_init = memory_char_driver_class_init,
+    .instance_finalize = memory_char_driver_fini,
+};
+
+static void register_backends(void)
+{
+    type_register_static(&memory_char_driver_type_info);
+}
+
+device_init(register_backends);
diff --git a/include/qemu/memchr.h b/include/qemu/memchr.h
new file mode 100644
index 0000000..0744684
--- /dev/null
+++ b/include/qemu/memchr.h
@@ -0,0 +1,46 @@
+#ifndef CHAR_DRIVER_MEM_H
+#define CHAR_DRIVER_MEM_H
+
+#include "qemu/chrdrv.h"
+
+/**
+ * @MemoryCharDriver:
+ *
+ * A @CharDriver that stores all written data to memory.  This is useful for
+ * interfacing directly with objects that expect to work with a @CharDriver.
+ *
+ * The accumulated data can be obtained as a QString.
+ */
+typedef struct MemoryCharDriver
+{
+    CharDriver parent;
+
+    /* Private */
+    size_t outbuf_size;
+    size_t outbuf_capacity;
+    uint8_t *outbuf;
+} MemoryCharDriver;
+
+#define TYPE_MEMORY_CHAR_DRIVER "memory-char-driver"
+#define MEMORY_CHAR_DRIVER(obj) \
+    TYPE_CHECK(MemoryCharDriver, obj, TYPE_MEMORY_CHAR_DRIVER)
+
+void memory_char_driver_initialize(MemoryCharDriver *d, const char *id);
+void memory_char_driver_finalize(MemoryCharDriver *d);
+
+/**
+ * @memory_char_driver_get_osize:
+ *
+ * Returns:  The size of the output buffer.
+ */
+size_t memory_char_driver_get_osize(MemoryCharDriver *d);
+
+/**
+ * @memory_char_driver_get_qs:
+ *
+ * Returns:  A QString representing the output buffer.  The reference ownership
+ *           is transferred to the caller.
+ */
+QString *memory_char_driver_get_qs(MemoryCharDriver *d);
+
+#endif
-- 
1.7.4.1

  parent reply	other threads:[~2011-07-25  1:45 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-25  1:44 [Qemu-devel] [RFC][PATCH 0/21] QEMU Object Model Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 01/21] qom: add make infrastructure Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 02/21] qom: convert QAPI to use Qconfig build system Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 03/21] qom: Add core type system Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 04/21] qom: add Plug class Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 05/21] plug: add Plug property type Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 06/21] plug: add socket " Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 07/21] plug: add generated property types Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 08/21] qom: add plug_create QMP command Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 09/21] qom: add plug_list " Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 10/21] qom: add plug_get " Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 11/21] qom: add plug_set " Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 12/21] qom: add plug_list_props " Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 13/21] qom: add plug_destroy command Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 14/21] qom: add example qsh command Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 15/21] qom: add Device class Anthony Liguori
2011-07-27 15:10   ` Peter Maydell
2011-07-27 16:07     ` Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 16/21] qom-devices: add a Pin class Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 17/21] qom: add CharDriver class Anthony Liguori
2011-07-25  1:44 ` Anthony Liguori [this message]
2011-07-25  1:44 ` [Qemu-devel] [PATCH 19/21] qom-chrdrv: add Socket base class Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 20/21] qom-chrdrv: add TCPServer class Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 21/21] qom-chrdrv: add UnixServer Anthony Liguori
2011-07-25 11:21 ` [Qemu-devel] [RFC][PATCH 0/21] QEMU Object Model Kevin Wolf
2011-07-25 12:45   ` Anthony Liguori
2011-07-25 13:08     ` Kevin Wolf
2011-07-25 13:10       ` Anthony Liguori
2011-07-26 12:59 ` Paolo Bonzini
2011-07-26 14:02   ` Anthony Liguori
2011-07-26 14:35     ` Paolo Bonzini
2011-07-26 15:34       ` Anthony Liguori
2011-07-26 18:26         ` Paolo Bonzini
2011-07-26 19:23           ` Anthony Liguori
2011-07-27  8:55             ` Paolo Bonzini
2011-07-27 12:48               ` Anthony Liguori
2011-07-27 15:33                 ` Paolo Bonzini
2011-07-27 16:19                   ` Anthony Liguori
2011-07-27 16:28                   ` Anthony Liguori
2011-07-27 18:51                     ` Paolo Bonzini
2011-07-27 20:01                       ` Anthony Liguori
2011-07-28  7:36                         ` Paolo Bonzini
2011-07-28 12:46                           ` Anthony Liguori
2011-07-28 13:50                             ` Paolo Bonzini
2011-07-28 14:03                               ` Anthony Liguori
2011-07-28 14:41                                 ` Paolo Bonzini
2011-07-28 15:04                                   ` Anthony Liguori
2011-07-28 15:47                                     ` Paolo Bonzini
2011-07-28 17:59                                       ` Anthony Liguori
2011-07-29  7:19                                         ` Paolo Bonzini
2011-07-27 21:33     ` Peter Maydell
2011-07-27 22:31       ` Anthony Liguori

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=1311558293-5855-19-git-send-email-aliguori@us.ibm.com \
    --to=aliguori@us.ibm.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).