From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Cc: mdroth@linux.vnet.ibm.com
Subject: [PATCH for-5.1 3/5] qobject: Eliminate qlist_iter(), use QLIST_FOREACH_ENTRY() instead
Date: Wed, 15 Apr 2020 10:30:46 +0200 [thread overview]
Message-ID: <20200415083048.14339-4-armbru@redhat.com> (raw)
In-Reply-To: <20200415083048.14339-1-armbru@redhat.com>
qlist_iter() has just three uses outside tests/. Replace by
QLIST_FOREACH_ENTRY() for more concise code and less type punning.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
include/qapi/qmp/qlist.h | 2 --
qobject/qjson.c | 31 ++++++++++------------------
qobject/qlist.c | 44 +++++++++++-----------------------------
tests/check-qlist.c | 37 +++++++++++++--------------------
4 files changed, 37 insertions(+), 77 deletions(-)
diff --git a/include/qapi/qmp/qlist.h b/include/qapi/qmp/qlist.h
index 07ecae81e4..595b7943e1 100644
--- a/include/qapi/qmp/qlist.h
+++ b/include/qapi/qmp/qlist.h
@@ -47,8 +47,6 @@ static inline QObject *qlist_entry_obj(const QListEntry *entry)
QList *qlist_new(void);
QList *qlist_copy(QList *src);
void qlist_append_obj(QList *qlist, QObject *obj);
-void qlist_iter(const QList *qlist,
- void (*iter)(QObject *obj, void *opaque), void *opaque);
QObject *qlist_pop(QList *qlist);
QObject *qlist_peek(QList *qlist);
int qlist_empty(const QList *qlist);
diff --git a/qobject/qjson.c b/qobject/qjson.c
index f3c62711b9..10050354a0 100644
--- a/qobject/qjson.c
+++ b/qobject/qjson.c
@@ -191,20 +191,6 @@ static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
s->count++;
}
-static void to_json_list_iter(QObject *obj, void *opaque)
-{
- ToJsonIterState *s = opaque;
-
- if (s->count) {
- qstring_append(s->str, s->pretty ? "," : ", ");
- }
-
- json_pretty_newline(s->str, s->pretty, s->indent);
-
- to_json(obj, s->str, s->pretty, s->indent);
- s->count++;
-}
-
static void to_json(const QObject *obj, QString *str, int pretty, int indent)
{
switch (qobject_type(obj)) {
@@ -289,15 +275,20 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
break;
}
case QTYPE_QLIST: {
- ToJsonIterState s;
QList *val = qobject_to(QList, obj);
+ const char *comma = pretty ? "," : ", ";
+ const char *sep = "";
+ QListEntry *entry;
- s.count = 0;
- s.str = str;
- s.indent = indent + 1;
- s.pretty = pretty;
qstring_append(str, "[");
- qlist_iter(val, (void *)to_json_list_iter, &s);
+
+ QLIST_FOREACH_ENTRY(val, entry) {
+ qstring_append(str, sep);
+ json_pretty_newline(str, pretty, indent + 1);
+ to_json(qlist_entry_obj(entry), str, pretty, indent + 1);
+ sep = comma;
+ }
+
json_pretty_newline(str, pretty, indent);
qstring_append(str, "]");
break;
diff --git a/qobject/qlist.c b/qobject/qlist.c
index b3274af88b..1be95367d1 100644
--- a/qobject/qlist.c
+++ b/qobject/qlist.c
@@ -34,20 +34,17 @@ QList *qlist_new(void)
return qlist;
}
-static void qlist_copy_elem(QObject *obj, void *opaque)
-{
- QList *dst = opaque;
-
- qobject_ref(obj);
- qlist_append_obj(dst, obj);
-}
-
QList *qlist_copy(QList *src)
{
QList *dst = qlist_new();
+ QListEntry *entry;
+ QObject *elt;
- qlist_iter(src, qlist_copy_elem, dst);
-
+ QLIST_FOREACH_ENTRY(src, entry) {
+ elt = qlist_entry_obj(entry);
+ qobject_ref(elt);
+ qlist_append_obj(dst, elt);
+ }
return dst;
}
@@ -86,21 +83,6 @@ void qlist_append_null(QList *qlist)
qlist_append(qlist, qnull());
}
-/**
- * qlist_iter(): Iterate over all the list's stored values.
- *
- * This function allows the user to provide an iterator, which will be
- * called for each stored value in the list.
- */
-void qlist_iter(const QList *qlist,
- void (*iter)(QObject *obj, void *opaque), void *opaque)
-{
- QListEntry *entry;
-
- QTAILQ_FOREACH(entry, &qlist->head, next)
- iter(entry->value, opaque);
-}
-
QObject *qlist_pop(QList *qlist)
{
QListEntry *entry;
@@ -137,16 +119,14 @@ int qlist_empty(const QList *qlist)
return QTAILQ_EMPTY(&qlist->head);
}
-static void qlist_size_iter(QObject *obj, void *opaque)
-{
- size_t *count = opaque;
- (*count)++;
-}
-
size_t qlist_size(const QList *qlist)
{
size_t count = 0;
- qlist_iter(qlist, qlist_size_iter, &count);
+ QListEntry *entry;
+
+ QLIST_FOREACH_ENTRY(qlist, entry) {
+ count++;
+ }
return count;
}
diff --git a/tests/check-qlist.c b/tests/check-qlist.c
index ece83e293d..3cd0ccbf19 100644
--- a/tests/check-qlist.c
+++ b/tests/check-qlist.c
@@ -61,40 +61,31 @@ static void qobject_to_qlist_test(void)
qobject_unref(qlist);
}
-static int iter_called;
-static const int iter_max = 42;
-
-static void iter_func(QObject *obj, void *opaque)
-{
- QNum *qi;
- int64_t val;
-
- g_assert(opaque == NULL);
-
- qi = qobject_to(QNum, obj);
- g_assert(qi != NULL);
-
- g_assert(qnum_get_try_int(qi, &val));
- g_assert_cmpint(val, >=, 0);
- g_assert_cmpint(val, <=, iter_max);
-
- iter_called++;
-}
-
static void qlist_iter_test(void)
{
+ const int iter_max = 42;
int i;
QList *qlist;
+ QListEntry *entry;
+ QNum *qi;
+ int64_t val;
qlist = qlist_new();
for (i = 0; i < iter_max; i++)
qlist_append_int(qlist, i);
- iter_called = 0;
- qlist_iter(qlist, iter_func, NULL);
+ i = 0;
+ QLIST_FOREACH_ENTRY(qlist, entry) {
+ qi = qobject_to(QNum, qlist_entry_obj(entry));
+ g_assert(qi != NULL);
- g_assert(iter_called == iter_max);
+ g_assert(qnum_get_try_int(qi, &val));
+ g_assert_cmpint(val, ==, i);
+ i++;
+ }
+
+ g_assert(i == iter_max);
qobject_unref(qlist);
}
--
2.21.1
next prev parent reply other threads:[~2020-04-15 8:34 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-15 8:30 [PATCH for-5.1 0/5] qobject: Minor spring cleaning Markus Armbruster
2020-04-15 8:30 ` [PATCH for-5.1 1/5] qobject: Clean up QLIST_FOREACH_ENTRY() Markus Armbruster
2020-04-15 12:25 ` Eric Blake
2020-04-15 8:30 ` [PATCH for-5.1 2/5] qobject: Factor out helper json_pretty_newline() Markus Armbruster
2020-04-15 12:28 ` Eric Blake
2020-04-15 14:46 ` Markus Armbruster
2020-04-15 8:30 ` Markus Armbruster [this message]
2020-04-15 12:31 ` [PATCH for-5.1 3/5] qobject: Eliminate qlist_iter(), use QLIST_FOREACH_ENTRY() instead Eric Blake
2020-04-15 8:30 ` [PATCH for-5.1 4/5] qobject: Eliminate qdict_iter(), use qdict_first(), qdict_next() Markus Armbruster
2020-04-15 12:34 ` Eric Blake
2020-04-15 14:48 ` Markus Armbruster
2020-04-15 8:30 ` [PATCH for-5.1 5/5] qemu-option: Clean up after the previous commit Markus Armbruster
2020-04-15 12:35 ` Eric Blake
2020-04-29 7:46 ` [PATCH for-5.1 0/5] qobject: Minor spring cleaning Markus Armbruster
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=20200415083048.14339-4-armbru@redhat.com \
--to=armbru@redhat.com \
--cc=mdroth@linux.vnet.ibm.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).