From patchwork Tue Feb 5 16:22:13 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 218459 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 975802C02CB for ; Wed, 6 Feb 2013 09:50:22 +1100 (EST) Received: from localhost ([::1]:60388 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U2rLG-0003ZJ-2F for incoming@patchwork.ozlabs.org; Tue, 05 Feb 2013 17:50:18 -0500 Received: from eggs.gnu.org ([208.118.235.92]:60323) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U2rL2-0003He-Tc for qemu-devel@nongnu.org; Tue, 05 Feb 2013 17:50:08 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1U2lHq-00080p-9d for qemu-devel@nongnu.org; Tue, 05 Feb 2013 11:22:33 -0500 Received: from oxygen.pond.sub.org ([2a01:4f8:121:10e4::3]:52240) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U2lHp-0007zO-U0 for qemu-devel@nongnu.org; Tue, 05 Feb 2013 11:22:22 -0500 Received: from blackfin.pond.sub.org (p57B0FFFA.dip.t-dialin.net [87.176.255.250]) by oxygen.pond.sub.org (Postfix) with ESMTPA id AACD7A43D5; Tue, 5 Feb 2013 17:22:20 +0100 (CET) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 9602B200B1; Tue, 5 Feb 2013 17:22:17 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Tue, 5 Feb 2013 17:22:13 +0100 Message-Id: <1360081335-6594-11-git-send-email-armbru@redhat.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1360081335-6594-1-git-send-email-armbru@redhat.com> References: <1360081335-6594-1-git-send-email-armbru@redhat.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a01:4f8:121:10e4::3 Cc: lilei@linux.vnet.ibm.com, lcapitulino@redhat.com Subject: [Qemu-devel] [PATCH for-1.4 10/12] qemu-char: General chardev "memory" code cleanup X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 Reviewed-by: Eric Blake --- qemu-char.c | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/qemu-char.c b/qemu-char.c index 8972bfd..d92578a 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -2644,7 +2644,7 @@ size_t qemu_chr_mem_osize(const CharDriverState *chr) } /*********************************************************/ -/*CircularMemory chardev*/ +/* CircularMemory chardev */ typedef struct { size_t size; @@ -2653,18 +2653,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) @@ -2677,8 +2670,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; } } @@ -2691,8 +2684,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; @@ -2742,9 +2735,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, @@ -2762,7 +2755,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; } @@ -2801,7 +2794,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; } @@ -2811,7 +2804,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);