qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Peter Maydell" <peter.maydell@linaro.org>,
	"Daniel P . Berrange" <berrange@redhat.com>,
	"Eduardo Habkost" <ehabkost@redhat.com>,
	"David Hildenbrand" <david@redhat.com>,
	"Jason Wang" <jasowang@redhat.com>,
	"Michael S . Tsirkin" <mst@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>,
	peterx@redhat.com, "Eric Auger" <eric.auger@redhat.com>,
	"Alex Williamson" <alex.williamson@redhat.com>,
	"Igor Mammedov" <imammedo@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@redhat.com>,
	"David Gibson" <david@gibson.dropbear.id.au>
Subject: [PATCH v2 3/5] qom: object_child_foreach_recursive_type()
Date: Thu, 28 Oct 2021 12:31:27 +0800	[thread overview]
Message-ID: <20211028043129.38871-4-peterx@redhat.com> (raw)
In-Reply-To: <20211028043129.38871-1-peterx@redhat.com>

Add this sister helper besides object_child_foreach_recursive() to loop over
child objects only if the object can be casted to a specific type.

Suggested-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 include/qom/object.h | 20 ++++++++++++++++++++
 qom/object.c         | 27 +++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/include/qom/object.h b/include/qom/object.h
index faae0d841f..355277db40 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -1926,6 +1926,26 @@ int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
 int object_child_foreach_recursive(Object *obj,
                                    int (*fn)(Object *child, void *opaque),
                                    void *opaque);
+
+/**
+ * object_child_foreach_recursive_type:
+ * @obj: the object whose children will be navigated
+ * @type: the typename string to scan
+ * @fn: the iterator function to be called
+ * @opaque: an opaque value that will be passed to the iterator
+ *
+ * This is a special version of object_child_foreach_recursive() so that we
+ * only call the fn() if the child can be casted to the @typename specified.
+ * Please refer to the comments above object_child_foreach_recursive() for
+ * more details.
+ *
+ * Returns: The last value returned by @fn, or 0 if there is no child.
+ */
+int object_child_foreach_recursive_type(Object *obj,
+                                        const char *typename,
+                                        int (*fn)(Object *child, void *opaque),
+                                        void *opaque);
+
 /**
  * container_get:
  * @root: root of the #path, e.g., object_get_root()
diff --git a/qom/object.c b/qom/object.c
index 6be710bc40..d25ca09b1d 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -1134,6 +1134,33 @@ int object_child_foreach_recursive(Object *obj,
     return do_object_child_foreach(obj, fn, opaque, true);
 }
 
+typedef struct {
+    const char *typename;
+    int (*fn)(Object *child, void *opaque);
+    void *opaque;
+} ObjectTypeArgs;
+
+static int object_child_hook(Object *child, void *opaque)
+{
+    ObjectTypeArgs *args = opaque;
+
+    if (object_dynamic_cast(child, args->typename)) {
+        return args->fn(child, args->opaque);
+    }
+
+    return 0;
+}
+
+int object_child_foreach_recursive_type(Object *obj,
+                                        const char *typename,
+                                        int (*fn)(Object *child, void *opaque),
+                                        void *opaque)
+{
+    ObjectTypeArgs args = { .typename = typename, .fn = fn, .opaque = opaque };
+
+    return object_child_foreach_recursive(obj, object_child_hook, &args);
+}
+
 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
 {
     GSList **list = opaque;
-- 
2.32.0



  parent reply	other threads:[~2021-10-28  4:37 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-28  4:31 [PATCH v2 0/5] pci/iommu: Fail early if vfio-pci detected before vIOMMU Peter Xu
2021-10-28  4:31 ` [PATCH v2 1/5] pci: Define pci_bus_dev_fn/pci_bus_fn/pci_bus_ret_fn Peter Xu
2021-10-28  4:31 ` [PATCH v2 2/5] pci: Export pci_for_each_device_under_bus*() Peter Xu
2021-10-29  0:06   ` David Gibson
2021-10-28  4:31 ` Peter Xu [this message]
2021-10-28  7:06   ` [PATCH v2 3/5] qom: object_child_foreach_recursive_type() David Hildenbrand
2021-10-28  4:31 ` [PATCH v2 4/5] pci: Add pci_for_each_root_bus() Peter Xu
2021-10-28  7:09   ` David Hildenbrand
2021-10-28  4:31 ` [PATCH v2 5/5] pc/q35: Add pre-plug hook for x86-iommu Peter Xu
2021-10-28  7:17   ` David Hildenbrand
2021-10-28  8:16     ` Peter Xu
2021-10-28 14:52   ` Alex Williamson
2021-10-28 15:36     ` Peter Xu
2021-10-28 16:11       ` Alex Williamson
2021-10-29  2:53         ` Peter Xu
2021-10-29 15:31           ` Alex Williamson
2021-11-02  1:15 ` [PATCH v2 0/5] pci/iommu: Fail early if vfio-pci detected before vIOMMU Peter Xu
2021-11-02  6:40   ` Michael S. Tsirkin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211028043129.38871-4-peterx@redhat.com \
    --to=peterx@redhat.com \
    --cc=alex.williamson@redhat.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=david@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=eric.auger@redhat.com \
    --cc=imammedo@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).