diff mbox series

[RFC,V1,09/14] qemu-option: filtered foreach

Message ID 1729178055-207271-10-git-send-email-steven.sistare@oracle.com (mailing list archive)
State New
Headers show
Series precreate phase | expand

Commit Message

Steven Sistare Oct. 17, 2024, 3:14 p.m. UTC
Define an accessor to loop over an options list and call a function
for each element that passes a filter function.

Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
---
 include/qemu/option.h |  5 +++++
 util/qemu-option.c    | 25 +++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
diff mbox series

Patch

diff --git a/include/qemu/option.h b/include/qemu/option.h
index 01e673a..fe37d4c 100644
--- a/include/qemu/option.h
+++ b/include/qemu/option.h
@@ -138,9 +138,14 @@  QDict *qemu_opts_to_qdict_filtered(QemuOpts *opts, QDict *qdict,
 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict);
 bool qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp);
 
+typedef bool (*qemu_opts_filterfunc)(void *opaque, QemuOpts *opts);
 typedef int (*qemu_opts_loopfunc)(void *opaque, QemuOpts *opts, Error **errp);
 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
                       void *opaque, Error **errp);
+int qemu_opts_filter_foreach(QemuOptsList *list,
+                             qemu_opts_filterfunc filter,
+                             qemu_opts_loopfunc func,
+                             void *opaque, Error **errp);
 void qemu_opts_print(QemuOpts *opts, const char *sep);
 void qemu_opts_print_help(QemuOptsList *list, bool print_caption);
 void qemu_opts_free(QemuOptsList *list);
diff --git a/util/qemu-option.c b/util/qemu-option.c
index 201f7a8..f2f02d6 100644
--- a/util/qemu-option.c
+++ b/util/qemu-option.c
@@ -1142,6 +1142,31 @@  int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
     return rc;
 }
 
+int qemu_opts_filter_foreach(QemuOptsList *list,
+                             qemu_opts_filterfunc filter,
+                             qemu_opts_loopfunc func,
+                             void *opaque, Error **errp)
+{
+    Location loc;
+    QemuOpts *opts, *next;
+    int rc = 0;
+
+    loc_push_none(&loc);
+    QTAILQ_FOREACH_SAFE(opts, &list->head, next, next) {
+        if (!filter(opaque, opts)) {
+            continue;
+        }
+        loc_restore(&opts->loc);
+        rc = func(opaque, opts, errp);
+        if (rc) {
+            break;
+        }
+        assert(!errp || !*errp);
+    }
+    loc_pop(&loc);
+    return rc;
+}
+
 static size_t count_opts_list(QemuOptsList *list)
 {
     QemuOptDesc *desc = NULL;