Signed-off-by: Lei Li <li...@linux.vnet.ibm.com> --- qemu-char.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 84 insertions(+), 0 deletions(-)
diff --git a/qemu-char.c b/qemu-char.c index 767da93..0470085 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -2591,6 +2591,90 @@ size_t qemu_chr_mem_osize(const CharDriverState *chr) return d->outbuf_size; } +/*********************************************************/ +/*CircularMemory chardev*/ + +typedef struct { + size_t cbuf_capacity; + size_t cbuf_in; + size_t cbuf_out; + size_t cbuf_count; + uint8_t *cbuf; +} CircMemCharDriver; + +static int cirmem_chr_is_empty(CharDriverState *chr) +{ + CircMemCharDriver *d = chr->opaque; + + return d->cbuf_count == 0; +} + +static int cirmem_chr_is_full(CharDriverState *chr) +{ + CircMemCharDriver *d = chr->opaque; + + return d->cbuf_count == d->cbuf_capacity; +} + +static int cirmem_chr_write(CharDriverState *chr, const uint8_t *buf, int len) +{ + CircMemCharDriver *d = chr->opaque; + int left; + + if (d->cbuf_capacity < len) { + return -1; + } + + left = d->cbuf_capacity - d->cbuf_count % d->cbuf_capacity; + + /* Some of cbuf need to be overwrited */ + if (left < len) { + memcpy(d->cbuf + d->cbuf_in, buf, left); + memcpy(d->cbuf + d->cbuf_out, buf + left, len - left); + d->cbuf_out = (d->cbuf_out + len - left) % d->cbuf_capacity; + d->cbuf_count = d->cbuf_count + left; + } else { + /* Completely overwrite */ + if (cirmem_chr_is_full(chr)) { + d->cbuf_out = (d->cbuf_out + len) % d->cbuf_capacity; + } else { + /* Enough cbuf to write */ + d->cbuf_count += len; + } + memcpy(d->cbuf + d->cbuf_in, buf, len); + } + + d->cbuf_in = (d->cbuf_in + len) % d->cbuf_capacity; + + return len; +} + +static void cirmem_chr_read(CharDriverState *chr, uint8_t *buf, int len) +{ + CircMemCharDriver *d = chr->opaque; + int left; + + if (cirmem_chr_is_empty(chr)) { + return; + } + + left = d->cbuf_capacity - d->cbuf_count % d->cbuf_capacity; + + if (d->cbuf_capacity < len) { + len = d->cbuf_capacity; + } + + if (left < len) { + memcpy(buf, d->cbuf + d->cbuf_out, left); + memcpy(buf + left, d->cbuf + d->cbuf_out + left, len - left); + } else { + memcpy(buf, d->cbuf + d->cbuf_out, len); + } + + d->cbuf_out = (d->cbuf_out + len) % d->cbuf_capacity; + d->cbuf_count -= len; +} + QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) { char host[65], port[33], width[8], height[8]; -- 1.7.7.6