summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus Armbruster <armbru@redhat.com>2013-02-06 21:27:23 +0100
committerAnthony Liguori <aliguori@us.ibm.com>2013-02-06 16:35:19 -0600
commit5c230105cdea8ac9338bd5b4485c6ae80ec1fa18 (patch)
treedd97fb0858594f31c59623c46f84a0f032271d0d
parent6fd5b66950fc5551d371ba5017d0e0858b7c800b (diff)
downloadqemu-5c230105cdea8ac9338bd5b4485c6ae80ec1fa18.tar.gz
qemu-char: General chardev "memory" code cleanup
Inline trivial cirmem_chr_is_empty() into its only caller. Rename qemu_chr_cirmem_count() to cirmem_count(). Fast ring buffer index wraparound. Without this, there's no point in restricting size to a power two. qemu_is_chr(chr, "memory") returns *zero* when chr is a memory character device, which isn't what I'd expect. Replace it by the saner and more obviously correct chr_is_cirmem(). Also avoids encouraging testing for specific character devices elsewhere. Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
-rw-r--r--qemu-char.c31
1 files changed, 12 insertions, 19 deletions
diff --git a/qemu-char.c b/qemu-char.c
index cdbbafe4f1..831d564a8b 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -2645,7 +2645,7 @@ size_t qemu_chr_mem_osize(const CharDriverState *chr)
}
/*********************************************************/
-/*CircularMemory chardev*/
+/* CircularMemory chardev */
typedef struct {
size_t size;
@@ -2654,18 +2654,11 @@ typedef struct {
uint8_t *cbuf;
} CirMemCharDriver;
-static bool cirmem_chr_is_empty(const CharDriverState *chr)
+static size_t cirmem_count(const CharDriverState *chr)
{
const CirMemCharDriver *d = chr->opaque;
- return d->cons == d->prod;
-}
-
-static size_t qemu_chr_cirmem_count(const CharDriverState *chr)
-{
- const CirMemCharDriver *d = chr->opaque;
-
- return (d->prod - d->cons);
+ return d->prod - d->cons;
}
static int cirmem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
@@ -2678,8 +2671,8 @@ static int cirmem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
}
for (i = 0; i < len; i++ ) {
- d->cbuf[d->prod++ % d->size] = buf[i];
- if ((d->prod - d->cons) > d->size) {
+ d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
+ if (d->prod - d->cons > d->size) {
d->cons = d->prod - d->size;
}
}
@@ -2692,8 +2685,8 @@ static int cirmem_chr_read(CharDriverState *chr, uint8_t *buf, int len)
CirMemCharDriver *d = chr->opaque;
int i;
- for (i = 0; i < len && !cirmem_chr_is_empty(chr); i++) {
- buf[i] = d->cbuf[d->cons++ % d->size];
+ for (i = 0; i < len && d->cons != d->prod; i++) {
+ buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
}
return i;
@@ -2743,9 +2736,9 @@ fail:
return NULL;
}
-static bool qemu_is_chr(const CharDriverState *chr, const char *filename)
+static bool chr_is_cirmem(const CharDriverState *chr)
{
- return strcmp(chr->filename, filename);
+ return chr->chr_write == cirmem_chr_write;
}
void qmp_memchar_write(const char *device, const char *data,
@@ -2763,7 +2756,7 @@ void qmp_memchar_write(const char *device, const char *data,
return;
}
- if (qemu_is_chr(chr, "memory")) {
+ if (!chr_is_cirmem(chr)) {
error_setg(errp,"%s is not memory char device", device);
return;
}
@@ -2802,7 +2795,7 @@ char *qmp_memchar_read(const char *device, int64_t size,
return NULL;
}
- if (qemu_is_chr(chr, "memory")) {
+ if (!chr_is_cirmem(chr)) {
error_setg(errp,"%s is not memory char device", device);
return NULL;
}
@@ -2812,7 +2805,7 @@ char *qmp_memchar_read(const char *device, int64_t size,
return NULL;
}
- count = qemu_chr_cirmem_count(chr);
+ count = cirmem_count(chr);
size = size > count ? count : size;
read_data = g_malloc(size + 1);