From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59683) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aacdt-0000HG-J9 for qemu-devel@nongnu.org; Tue, 01 Mar 2016 00:14:43 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aacdr-0006PB-Qa for qemu-devel@nongnu.org; Tue, 01 Mar 2016 00:14:41 -0500 Received: from mx1.redhat.com ([209.132.183.28]:38345) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aacdr-0006P7-JH for qemu-devel@nongnu.org; Tue, 01 Mar 2016 00:14:39 -0500 From: Eric Blake Date: Mon, 29 Feb 2016 22:14:20 -0700 Message-Id: <1456809272-14184-7-git-send-email-eblake@redhat.com> In-Reply-To: <1456809272-14184-1-git-send-email-eblake@redhat.com> References: <1456809272-14184-1-git-send-email-eblake@redhat.com> Subject: [Qemu-devel] [PATCH v12 06/18] qmp-input: Refactor when list is advanced List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: armbru@redhat.com, Michael Roth 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 --- 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