Refactor the code to advance the QListEntry pointer at the point where visit_type_FOO() is called, rather than visit_next_list(). This will allow a future patch to move the visit of the list head into visit_start_list(), and get rid of the 'first' flag.
Signed-off-by: Eric Blake <ebl...@redhat.com> --- v13: no change v12: new patch --- qapi/qmp-input-visitor.c | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/qapi/qmp-input-visitor.c b/qapi/qmp-input-visitor.c index 4bb7b21..ed54d4c 100644 --- a/qapi/qmp-input-visitor.c +++ b/qapi/qmp-input-visitor.c @@ -26,9 +26,10 @@ typedef struct StackObject { QObject *obj; /* Object being visited */ - /* If obj is list: NULL if list is at head, otherwise tail of list - * still needing visits */ + /* If obj is list: tail of list still needing visits */ const QListEntry *entry; + /* If obj is list: true if head is not visited yet */ + bool first; GHashTable *h; /* If obj is dict: remaining keys needing visits */ } StackObject; @@ -76,7 +77,12 @@ static QObject *qmp_input_get_object(QmpInputVisitor *qiv, * of the list. */ if (tos->entry) { assert(qobject_type(qobj) == QTYPE_QLIST); - return qlist_entry_obj(tos->entry); + assert(!tos->first); + qobj = qlist_entry_obj(tos->entry); + if (consume) { + tos->entry = qlist_next(tos->entry); + } + return qobj; } /* Otherwise, we are at the root of the visit or the start of a @@ -90,7 +96,8 @@ static void qdict_add_key(const char *key, QObject *obj, void *opaque) g_hash_table_insert(h, (gpointer) key, NULL); } -static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp) +static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, + const QListEntry *entry, Error **errp) { GHashTable *h; StackObject *tos = &qiv->stack[qiv->nb_stack]; @@ -102,7 +109,8 @@ static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp) } tos->obj = obj; - tos->entry = NULL; + tos->entry = entry; + tos->first = true; tos->h = NULL; if (qiv->strict && qobject_type(obj) == QTYPE_QDICT) { @@ -152,7 +160,7 @@ static void qmp_input_start_struct(Visitor *v, const char *name, void **obj, return; } - qmp_input_push(qiv, qobj, &err); + qmp_input_push(qiv, qobj, NULL, &err); if (err) { error_propagate(errp, err); return; @@ -174,6 +182,7 @@ static void qmp_input_start_list(Visitor *v, const char *name, Error **errp) { QmpInputVisitor *qiv = to_qiv(v); QObject *qobj = qmp_input_get_object(qiv, name, true); + const QListEntry *entry; if (!qobj || qobject_type(qobj) != QTYPE_QLIST) { error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", @@ -181,7 +190,8 @@ static void qmp_input_start_list(Visitor *v, const char *name, Error **errp) return; } - qmp_input_push(qiv, qobj, errp); + entry = qlist_first(qobject_to_qlist(qobj)); + qmp_input_push(qiv, qobj, entry, errp); } static GenericList *qmp_input_next_list(Visitor *v, GenericList **list, @@ -190,23 +200,15 @@ static GenericList *qmp_input_next_list(Visitor *v, GenericList **list, QmpInputVisitor *qiv = to_qiv(v); GenericList *entry; StackObject *so = &qiv->stack[qiv->nb_stack - 1]; - bool first; - if (so->entry == NULL) { - so->entry = qlist_first(qobject_to_qlist(so->obj)); - first = true; - } else { - so->entry = qlist_next(so->entry); - first = false; - } - - if (so->entry == NULL) { + if (!so->entry) { return NULL; } entry = g_malloc0(size); - if (first) { + if (so->first) { *list = entry; + so->first = false; } else { (*list)->next = entry; } @@ -381,7 +383,7 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj) v->visitor.type_any = qmp_input_type_any; v->visitor.optional = qmp_input_optional; - qmp_input_push(v, obj, NULL); + qmp_input_push(v, obj, NULL, NULL); qobject_incref(obj); return v; -- 2.5.0