From patchwork Fri Sep 4 16:22:57 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 53134 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f200.google.com (mail-wi0-f200.google.com [209.85.212.200]) by patches.linaro.org (Postfix) with ESMTPS id 82C562159E for ; Fri, 4 Sep 2015 16:23:20 +0000 (UTC) Received: by wisv5 with SMTP id v5sf8087009wis.0 for ; Fri, 04 Sep 2015 09:23:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=MXsAu3+kEAR1DL/dJ8uTLO4OHuuVdnvRJEZqri369kk=; b=O/0w78qnY5aDt8N/kkXw4m9U8MObezvxriEPimCEnMZC/yNCClhXNUzaun6oyzsteh NKmuylmuQCfvRFzH7wb6clFwZty7QZScwTlR17n+0JRlFVC3po/A6OV8UN4MJ2ds4h3L KhkPETkgWG6Md+dVAx2vmDYpPxIdX7jtUlCLu2kTuGxYMGQj7a7QHUf6aj/RYK5ZIjaS phKhoadVpYCrjDLpFXjot53pRYkxx6Z3FVIcsYRw6LBiq3bBt9ZJBjrQk2bbmvsss7WF 1Fn6g4KqmTU6A3n4apXlN1ZAgxW/PbxO0njdYsFmGVR/fazZI5SgeDHtPxZk04dQ6/19 S25g== X-Gm-Message-State: ALoCoQlcr8O7NjSkDokGaZyYn3I7iwB/uHQJGK3VbDfGeRI8ENA5ZTxhK00nNYoXMvAXUYt+u68G X-Received: by 10.180.100.71 with SMTP id ew7mr1352618wib.0.1441383799872; Fri, 04 Sep 2015 09:23:19 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.23.166 with SMTP id n6ls324401laf.86.gmail; Fri, 04 Sep 2015 09:23:19 -0700 (PDT) X-Received: by 10.152.26.163 with SMTP id m3mr4451627lag.86.1441383799672; Fri, 04 Sep 2015 09:23:19 -0700 (PDT) Received: from mail-lb0-f180.google.com (mail-lb0-f180.google.com. [209.85.217.180]) by mx.google.com with ESMTPS id g4si2718187laa.21.2015.09.04.09.23.19 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Sep 2015 09:23:19 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.180 as permitted sender) client-ip=209.85.217.180; Received: by lbcjc2 with SMTP id jc2so14146992lbc.0 for ; Fri, 04 Sep 2015 09:23:19 -0700 (PDT) X-Received: by 10.112.141.136 with SMTP id ro8mr4470408lbb.88.1441383799464; Fri, 04 Sep 2015 09:23:19 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.112.164.42 with SMTP id yn10csp14742lbb; Fri, 4 Sep 2015 09:23:18 -0700 (PDT) X-Received: by 10.66.218.42 with SMTP id pd10mr10065853pac.106.1441383788651; Fri, 04 Sep 2015 09:23:08 -0700 (PDT) Received: from mnementh.archaic.org.uk (mnementh.archaic.org.uk. [2001:8b0:1d0::1]) by mx.google.com with ESMTPS id l1si5037120pdi.225.2015.09.04.09.23.07 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Fri, 04 Sep 2015 09:23:08 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 2001:8b0:1d0::1 as permitted sender) client-ip=2001:8b0:1d0::1; Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.80) (envelope-from ) id 1ZXtlW-0006Li-IB; Fri, 04 Sep 2015 17:23:02 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Cc: patches@linaro.org, "Edgar E. Iglesias" Subject: [PATCH v3 1/6] qom: Add recursive version of object_child_for_each Date: Fri, 4 Sep 2015 17:22:57 +0100 Message-Id: <1441383782-24378-2-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1441383782-24378-1-git-send-email-peter.maydell@linaro.org> References: <1441383782-24378-1-git-send-email-peter.maydell@linaro.org> X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: peter.maydell@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.180 as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Peter Crosthwaite Useful for iterating through an entire QOM subtree. Signed-off-by: Peter Crosthwaite Reviewed-by: Peter Maydell Signed-off-by: Peter Maydell --- include/qom/object.h | 15 +++++++++++++++ qom/object.c | 25 ++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/include/qom/object.h b/include/qom/object.h index 807978e..be7280c 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -1494,6 +1494,21 @@ int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), void *opaque); /** + * object_child_foreach_recursive: + * @obj: the object whose children will be navigated + * @fn: the iterator function to be called + * @opaque: an opaque value that will be passed to the iterator + * + * Call @fn passing each child of @obj and @opaque to it, until @fn returns + * non-zero. Calls recursively, all child nodes of @obj will also be passed + * all the way down to the leaf nodes of the tree. Depth first ordering. + * + * Returns: The last value returned by @fn, or 0 if there is no child. + */ +int object_child_foreach_recursive(Object *obj, + int (*fn)(Object *child, void *opaque), + void *opaque); +/** * container_get: * @root: root of the #path, e.g., object_get_root() * @path: path to the container diff --git a/qom/object.c b/qom/object.c index eea8edf..b7b05d3 100644 --- a/qom/object.c +++ b/qom/object.c @@ -775,23 +775,42 @@ void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), enumerating_types = false; } -int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), - void *opaque) +static int do_object_child_foreach(Object *obj, + int (*fn)(Object *child, void *opaque), + void *opaque, bool recurse) { ObjectProperty *prop, *next; int ret = 0; QTAILQ_FOREACH_SAFE(prop, &obj->properties, node, next) { if (object_property_is_child(prop)) { - ret = fn(prop->opaque, opaque); + Object *child = prop->opaque; + + ret = fn(child, opaque); if (ret != 0) { break; } + if (recurse) { + do_object_child_foreach(child, fn, opaque, true); + } } } return ret; } +int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), + void *opaque) +{ + return do_object_child_foreach(obj, fn, opaque, false); +} + +int object_child_foreach_recursive(Object *obj, + int (*fn)(Object *child, void *opaque), + void *opaque) +{ + return do_object_child_foreach(obj, fn, opaque, true); +} + static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) { GSList **list = opaque;