From patchwork Sat Mar 5 23:50:47 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 592485 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id ABB101402C4 for ; Sun, 6 Mar 2016 10:51:47 +1100 (AEDT) Received: from localhost ([::1]:48462 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1acLz7-0001CR-Rt for incoming@patchwork.ozlabs.org; Sat, 05 Mar 2016 18:51:45 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39826) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1acLyW-0008Pu-SN for qemu-devel@nongnu.org; Sat, 05 Mar 2016 18:51:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1acLyU-0005ks-8n for qemu-devel@nongnu.org; Sat, 05 Mar 2016 18:51:08 -0500 Received: from mx1.redhat.com ([209.132.183.28]:34985) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1acLyU-0005ko-11 for qemu-devel@nongnu.org; Sat, 05 Mar 2016 18:51:06 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id 998C364D14; Sat, 5 Mar 2016 23:51:05 +0000 (UTC) Received: from red.redhat.com (ovpn-113-165.phx2.redhat.com [10.3.113.165]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u25Np3ZD020509; Sat, 5 Mar 2016 18:51:05 -0500 From: Eric Blake To: qemu-devel@nongnu.org Date: Sat, 5 Mar 2016 16:50:47 -0700 Message-Id: <1457221861-18067-5-git-send-email-eblake@redhat.com> In-Reply-To: <1457221861-18067-1-git-send-email-eblake@redhat.com> References: <1457221861-18067-1-git-send-email-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Sat, 05 Mar 2016 23:51:05 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Cc: armbru@redhat.com, Michael Roth Subject: [Qemu-devel] [PATCH v13 04/18] qmp-input: Clean up stack handling X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Management of the top of stack was a bit verbose; creating a temporary variable and adding some comments makes the existing code more legible before the next few patches improve things. No semantic changes other than asserting that we are always visiting a QObject, and not a NULL value. Signed-off-by: Eric Blake --- v13: no change v12: new patch --- qapi/qmp-input-visitor.c | 52 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/qapi/qmp-input-visitor.c b/qapi/qmp-input-visitor.c index 550aed6..7428d15 100644 --- a/qapi/qmp-input-visitor.c +++ b/qapi/qmp-input-visitor.c @@ -24,16 +24,26 @@ typedef struct StackObject { - QObject *obj; + QObject *obj; /* Object being visited */ + + /* If obj is list: NULL if list is at head, otherwise tail of list + * still needing visits */ const QListEntry *entry; - GHashTable *h; + + GHashTable *h; /* If obj is dict: remaining keys needing visits */ } StackObject; struct QmpInputVisitor { Visitor visitor; + + /* Stack of objects being visited. stack[0] is root of visit, + * stack[1] and below correspond to visit_start_struct (nested + * QDict) and visit_start_list (nested QList). */ StackObject stack[QIV_STACK_SIZE]; int nb_stack; + + /* True to track whether all keys in QDict have been parsed. */ bool strict; }; @@ -46,19 +56,29 @@ static QObject *qmp_input_get_object(QmpInputVisitor *qiv, const char *name, bool consume) { - QObject *qobj = qiv->stack[qiv->nb_stack - 1].obj; + StackObject *tos = &qiv->stack[qiv->nb_stack - 1]; + QObject *qobj = tos->obj; - if (qobj) { - if (name && qobject_type(qobj) == QTYPE_QDICT) { - if (qiv->stack[qiv->nb_stack - 1].h && consume) { - g_hash_table_remove(qiv->stack[qiv->nb_stack - 1].h, name); - } - return qdict_get(qobject_to_qdict(qobj), name); - } else if (qiv->stack[qiv->nb_stack - 1].entry) { - return qlist_entry_obj(qiv->stack[qiv->nb_stack - 1].entry); + assert(qobj); + + /* If we have a name, and we're in a dictionary, then return that + * value. */ + if (name && qobject_type(qobj) == QTYPE_QDICT) { + if (tos->h && consume) { + g_hash_table_remove(tos->h, name); } + return qdict_get(qobject_to_qdict(qobj), name); } + /* If we are in the middle of a list, then return the next element + * of the list. */ + if (tos->entry) { + assert(qobject_type(qobj) == QTYPE_QLIST); + return qlist_entry_obj(tos->entry); + } + + /* Otherwise, we are at the root of the visit or the start of a + * list, and return the object as-is. */ return qobj; } @@ -71,20 +91,22 @@ static void qdict_add_key(const char *key, QObject *obj, void *opaque) static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp) { GHashTable *h; + StackObject *tos = &qiv->stack[qiv->nb_stack]; + assert(obj); if (qiv->nb_stack >= QIV_STACK_SIZE) { error_setg(errp, "An internal buffer overran"); return; } - qiv->stack[qiv->nb_stack].obj = obj; - qiv->stack[qiv->nb_stack].entry = NULL; - qiv->stack[qiv->nb_stack].h = NULL; + tos->obj = obj; + tos->entry = NULL; + tos->h = NULL; if (qiv->strict && qobject_type(obj) == QTYPE_QDICT) { h = g_hash_table_new(g_str_hash, g_str_equal); qdict_iter(qobject_to_qdict(obj), qdict_add_key, h); - qiv->stack[qiv->nb_stack].h = h; + tos->h = h; } qiv->nb_stack++;