@@ -2279,6 +2279,69 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
return NULL;
}
+/***********************************************************/
+/* Buffered chardev */
+typedef struct {
+ size_t outbuf_size;
+ size_t outbuf_capacity;
+ uint8_t *outbuf;
+} BufferedDriver;
+
+static int buffered_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
+{
+ BufferedDriver *d = chr->opaque;
+
+ if (d->outbuf_capacity < (d->outbuf_size + len)) {
+ /* grown 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;
+}
+
+#define DEFAULT_BUF_SIZE 4096
+
+void qemu_chr_init_buffered(CharDriverState *chr)
+{
+ BufferedDriver *d;
+
+ d = qemu_malloc(sizeof(*d));
+ d->outbuf_size = 0;
+ d->outbuf_capacity = DEFAULT_BUF_SIZE;
+ d->outbuf = qemu_mallocz(d->outbuf_capacity);
+
+ memset(chr, 0, sizeof(*chr));
+ chr->opaque = d;
+ chr->chr_write = buffered_chr_write;
+}
+
+/* assumes the stored data is a string */
+QString *qemu_chr_buffered_to_qs(CharDriverState *chr)
+{
+ BufferedDriver *d = chr->opaque;
+
+ if (d->outbuf_size == 0) {
+ return NULL;
+ }
+
+ return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
+}
+
+void qemu_chr_close_buffered(CharDriverState *chr)
+{
+ BufferedDriver *d = chr->opaque;
+
+ qemu_free(d->outbuf);
+ qemu_free(chr->opaque);
+ chr->opaque = NULL;
+ chr->chr_write = NULL;
+}
+
QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
{
char host[65], port[33], width[8], height[8];
@@ -6,6 +6,7 @@
#include "qemu-option.h"
#include "qemu-config.h"
#include "qobject.h"
+#include "qstring.h"
/* character device */
@@ -100,6 +101,11 @@ CharDriverState *qemu_chr_open_eventfd(int eventfd);
extern int term_escape_char;
+/* buffered chardev */
+void qemu_chr_init_buffered(CharDriverState *chr);
+void qemu_chr_close_buffered(CharDriverState *chr);
+QString *qemu_chr_buffered_to_qs(CharDriverState *chr);
+
/* async I/O support */
int qemu_set_fd_handler2(int fd,
This driver handles in-memory chardev operations. That's, all writes to this driver are stored in an internal buffer and it doesn't talk to the external world in any way. Right now it's very simple: it supports only writes. But it can be easily extended to support more operations. This is going to be used by the monitor's "HMP passthrough via QMP" feature, which needs to run monitor handlers without a backing device. Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com> --- qemu-char.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-char.h | 6 +++++ 2 files changed, 69 insertions(+), 0 deletions(-)