Luiz Capitulino <lcapitul...@redhat.com> writes: > 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 <lcapitul...@redhat.com> > --- > qemu-char.c | 66 > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > qemu-char.h | 6 +++++ > 2 files changed, 72 insertions(+), 0 deletions(-) > > diff --git a/qemu-char.c b/qemu-char.c > index 88997f9..896df14 100644 > --- a/qemu-char.c > +++ b/qemu-char.c > @@ -2275,6 +2275,72 @@ static CharDriverState *qemu_chr_open_socket(QemuOpts > *opts) > return NULL; > } > > +/***********************************************************/ > +/* Memory chardev */ > +typedef struct { > + size_t outbuf_size; > + size_t outbuf_capacity; > + uint8_t *outbuf; > +} MemoryDriver; > + > +static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len) > +{ > + MemoryDriver *d = chr->opaque; > + > + /* 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) { > + /* grown outbuf */
Used to say "grow" (sans n) here. Intentional change? > + 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 It's the *initial* buffer size, isn't it? Doubt it's worth a #define (there's just one user), but that's a matter of taste. > + > +void qemu_chr_init_mem(CharDriverState *chr) > +{ > + MemoryDriver *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 = mem_chr_write; > +} > + > +/* assumes the stored data is a string */ What else could it be? Worrying about embedded '\0's? > +QString *qemu_chr_mem_to_qs(CharDriverState *chr) > +{ > + MemoryDriver *d = chr->opaque; > + > + if (d->outbuf_size == 0) { > + return NULL; > + } Did you forget to change this? We agreed to return an empty QString when chr contains an empty string. > + > + return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1); > +} > + > +/* NOTE: this driver can not be closed with qemu_chr_close()! */ > +void qemu_chr_close_mem(CharDriverState *chr) > +{ > + MemoryDriver *d = chr->opaque; > + > + qemu_free(d->outbuf); > + qemu_free(chr->opaque); > + chr->opaque = NULL; > + chr->chr_write = NULL; > +} > + Unlike normal character drivers, this one can't be closed with qemu_chr_close(). It probably explodes if you try. Please add a suitable assertion to qemu_chr_close() to document the fact, and to ensure misuse fails in a controlled, obvious manner. > QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) > { > char host[65], port[33], width[8], height[8]; > diff --git a/qemu-char.h b/qemu-char.h > index 18ad12b..c4e55b4 100644 > --- a/qemu-char.h > +++ b/qemu-char.h > @@ -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; > > +/* memory chardev */ > +void qemu_chr_init_mem(CharDriverState *chr); > +void qemu_chr_close_mem(CharDriverState *chr); > +QString *qemu_chr_mem_to_qs(CharDriverState *chr); > + > /* async I/O support */ > > int qemu_set_fd_handler2(int fd,