From patchwork Thu Jan 13 13:00:06 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amit Shah X-Patchwork-Id: 78749 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id C3E74B6F14 for ; Fri, 14 Jan 2011 00:07:48 +1100 (EST) Received: from localhost ([127.0.0.1]:55840 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PdMu1-0001HG-AY for incoming@patchwork.ozlabs.org; Thu, 13 Jan 2011 08:07:45 -0500 Received: from [140.186.70.92] (port=42155 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PdMmy-0005oI-Hy for qemu-devel@nongnu.org; Thu, 13 Jan 2011 08:00:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PdMmx-0004WK-D7 for qemu-devel@nongnu.org; Thu, 13 Jan 2011 08:00:28 -0500 Received: from mx1.redhat.com ([209.132.183.28]:20758) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PdMmx-0004WE-3s for qemu-devel@nongnu.org; Thu, 13 Jan 2011 08:00:27 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id p0DD0Inq032703 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 13 Jan 2011 08:00:18 -0500 Received: from localhost (dhcp193-64.pnq.redhat.com [10.65.193.64]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p0DD0Gc9022740; Thu, 13 Jan 2011 08:00:17 -0500 From: Amit Shah To: qemu list Date: Thu, 13 Jan 2011 18:30:06 +0530 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: Amit Shah , Gerd Hoffmann , Paul Brook Subject: [Qemu-devel] [PATCH 1/5] iohandlers: Avoid code duplication X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Add a get_iohandler() function instead of looking up the ioh twice in qemu_set_fd_handler2(). Signed-off-by: Amit Shah --- vl.c | 44 ++++++++++++++++++++++++++------------------ 1 files changed, 26 insertions(+), 18 deletions(-) diff --git a/vl.c b/vl.c index 0292184..9e365f6 100644 --- a/vl.c +++ b/vl.c @@ -1022,6 +1022,17 @@ typedef struct IOHandlerRecord { static QLIST_HEAD(, IOHandlerRecord) io_handlers = QLIST_HEAD_INITIALIZER(io_handlers); +static IOHandlerRecord *get_iohandler(int fd) +{ + IOHandlerRecord *ioh; + + QLIST_FOREACH(ioh, &io_handlers, next) { + if (ioh->fd == fd) { + return ioh; + } + } + return NULL; +} /* XXX: fd_read_poll should be suppressed, but an API change is necessary in the character devices to suppress fd_can_read(). */ @@ -1033,28 +1044,25 @@ int qemu_set_fd_handler2(int fd, { IOHandlerRecord *ioh; - if (!fd_read && !fd_write) { - QLIST_FOREACH(ioh, &io_handlers, next) { - if (ioh->fd == fd) { - ioh->deleted = 1; - break; - } - } - } else { - QLIST_FOREACH(ioh, &io_handlers, next) { - if (ioh->fd == fd) - goto found; - } + ioh = get_iohandler(fd); + + if (ioh && !fd_read && !fd_write) { + ioh->deleted = 1; + return 0; + } + + if (!ioh) { ioh = qemu_mallocz(sizeof(IOHandlerRecord)); QLIST_INSERT_HEAD(&io_handlers, ioh, next); - found: + ioh->fd = fd; - ioh->fd_read_poll = fd_read_poll; - ioh->fd_read = fd_read; - ioh->fd_write = fd_write; - ioh->opaque = opaque; - ioh->deleted = 0; } + ioh->fd_read_poll = fd_read_poll; + ioh->fd_read = fd_read; + ioh->fd_write = fd_write; + ioh->opaque = opaque; + ioh->deleted = 0; + return 0; }