}
/*********************************************************/
-/*CircularMemory chardev*/
+/* CircularMemory chardev */
typedef struct {
size_t size;
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)
}
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;
}
}
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;
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,
return;
}
- if (qemu_is_chr(chr, "memory")) {
+ if (!chr_is_cirmem(chr)) {
error_setg(errp,"%s is not memory char device", device);
return;
}
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;
}
return NULL;
}
- count = qemu_chr_cirmem_count(chr);
+ count = cirmem_count(chr);
size = size > count ? count : size;
read_data = g_malloc(size + 1);