From patchwork Wed Nov 17 04:50:21 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 71512 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 9AA8FB7181 for ; Wed, 17 Nov 2010 15:53:40 +1100 (EST) Received: from localhost ([127.0.0.1]:35774 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PIa1Y-0005co-Nr for incoming@patchwork.ozlabs.org; Tue, 16 Nov 2010 23:53:36 -0500 Received: from [140.186.70.92] (port=34631 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PIZyc-0004Ta-1S for qemu-devel@nongnu.org; Tue, 16 Nov 2010 23:50:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PIZya-0000lV-9B for qemu-devel@nongnu.org; Tue, 16 Nov 2010 23:50:33 -0500 Received: from mail.valinux.co.jp ([210.128.90.3]:55515) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PIZyZ-0000kV-KH for qemu-devel@nongnu.org; Tue, 16 Nov 2010 23:50:32 -0500 Received: from ps.local.valinux.co.jp (vagw.valinux.co.jp [210.128.90.14]) by mail.valinux.co.jp (Postfix) with SMTP id D003727E5E; Wed, 17 Nov 2010 13:50:28 +0900 (JST) Received: (nullmailer pid 25292 invoked by uid 1000); Wed, 17 Nov 2010 04:50:27 -0000 From: Isaku Yamahata To: qemu-devel@nongnu.org Date: Wed, 17 Nov 2010 13:50:21 +0900 Message-Id: <61e9609833bd34a1e1766d7b7b9faae6a86503cb.1289969012.git.yamahata@valinux.co.jp> X-Mailer: git-send-email 1.7.1.1 In-Reply-To: References: In-Reply-To: References: X-Virus-Scanned: clamav-milter 0.95.2 at va-mail.local.valinux.co.jp X-Virus-Status: Clean X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) Cc: skandasa@cisco.com, Anthony Liguori , etmartin@cisco.com, wexu2@cisco.com, mst@redhat.com, yamahata@valinux.co.jp Subject: [Qemu-devel] [PATCH 1/7] qbus: add functions to walk both devices and busses 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 From: Anthony Liguori There are some cases where you want to walk the busses, in particular, when searching for a bus either by name or DeviceInfo. Paolo suggested that we model the return values on how GCC's walkers work which allows an actor to skip child transversal, or terminate walking with a positive value that's returned as the qbus_walk_children's result. Signed-off-by: Isaku Yamahata Signed-off-by: Anthony Liguori --- hw/qdev.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ hw/qdev.h | 9 +++++++++ 2 files changed, 55 insertions(+), 0 deletions(-) diff --git a/hw/qdev.c b/hw/qdev.c index 35858cb..11d845a 100644 --- a/hw/qdev.c +++ b/hw/qdev.c @@ -449,6 +449,52 @@ BusState *qdev_get_child_bus(DeviceState *dev, const char *name) return NULL; } +int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn, + qbus_walkerfn *busfn, void *opaque) +{ + DeviceState *dev; + int err; + + if (busfn) { + err = busfn(bus, opaque); + if (err) { + return err; + } + } + + QLIST_FOREACH(dev, &bus->children, sibling) { + err = qdev_walk_children(dev, devfn, busfn, opaque); + if (err < 0) { + return err; + } + } + + return 0; +} + +int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn, + qbus_walkerfn *busfn, void *opaque) +{ + BusState *bus; + int err; + + if (devfn) { + err = devfn(dev, opaque); + if (err) { + return err; + } + } + + QLIST_FOREACH(bus, &dev->child_bus, sibling) { + err = qbus_walk_children(bus, devfn, busfn, opaque); + if (err < 0) { + return err; + } + } + + return 0; +} + static BusState *qbus_find_recursive(BusState *bus, const char *name, const BusInfo *info) { diff --git a/hw/qdev.h b/hw/qdev.h index 579328a..0cf50b1 100644 --- a/hw/qdev.h +++ b/hw/qdev.h @@ -173,9 +173,18 @@ BusState *qdev_get_parent_bus(DeviceState *dev); /*** BUS API. ***/ +/* Returns 0 to walk children, > 0 to terminate walk, < 0 to skip walk. */ +typedef int (qbus_walkerfn)(BusState *bus, void *opaque); +typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque); + void qbus_create_inplace(BusState *bus, BusInfo *info, DeviceState *parent, const char *name); BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name); +/* Returns > 0 if either devfn or busfn terminate walk, 0 otherwise. */ +int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn, + qbus_walkerfn *busfn, void *opaque); +int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn, + qbus_walkerfn *busfn, void *opaque); void qbus_free(BusState *bus); #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)