From patchwork Thu May 12 07:58:51 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 9077441 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 5D71C9F1D3 for ; Thu, 12 May 2016 08:08:58 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id AA39B201EC for ; Thu, 12 May 2016 08:08:57 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D5B4920155 for ; Thu, 12 May 2016 08:08:56 +0000 (UTC) Received: from localhost ([::1]:55922 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b0lfz-0000l7-RS for patchwork-qemu-devel@patchwork.kernel.org; Thu, 12 May 2016 04:08:55 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54132) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b0lWj-0000tv-5O for qemu-devel@nongnu.org; Thu, 12 May 2016 03:59:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b0lWc-0006JE-F2 for qemu-devel@nongnu.org; Thu, 12 May 2016 03:59:20 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49549) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b0lWc-0006Iw-4X for qemu-devel@nongnu.org; Thu, 12 May 2016 03:59:14 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 97DEC83F44 for ; Thu, 12 May 2016 07:59:13 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-35.ams2.redhat.com [10.36.116.35]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u4C7xACg028658 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 12 May 2016 03:59:12 -0400 Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 72FA91132BD9; Thu, 12 May 2016 09:59:10 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Thu, 12 May 2016 09:58:51 +0200 Message-Id: <1463039950-4021-5-git-send-email-armbru@redhat.com> In-Reply-To: <1463039950-4021-1-git-send-email-armbru@redhat.com> References: <1463039950-4021-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 12 May 2016 07:59:13 +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 Subject: [Qemu-devel] [PULL 04/23] qmp-input: Clean up stack handling X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Blake 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. In particular, the check for 'name && qobject_type(qobj) == QTYPE_QDICT)' is a bit overkill (a dict visit should always have a name); a later patch revisits that, while this patch is only changing one layer of indentation due to dropping 'if (qobj)'. Signed-off-by: Eric Blake Message-Id: <1461879932-9020-5-git-send-email-eblake@redhat.com> Signed-off-by: Markus Armbruster --- qapi/qmp-input-visitor.c | 51 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/qapi/qmp-input-visitor.c b/qapi/qmp-input-visitor.c index 77cce8b..8550bc7 100644 --- a/qapi/qmp-input-visitor.c +++ b/qapi/qmp-input-visitor.c @@ -25,16 +25,23 @@ typedef struct StackObject { - QObject *obj; - const QListEntry *entry; - GHashTable *h; + QObject *obj; /* Object being visited */ + + GHashTable *h; /* If obj is dict: unvisited keys */ + const QListEntry *entry; /* If obj is list: unvisited tail */ } StackObject; struct QmpInputVisitor { Visitor visitor; + + /* Stack of objects being visited. stack[0] is root of visit, + * stack[1..] records the nesting of start_struct()/end_struct() + * and start_list()/end_list() pairs. */ StackObject stack[QIV_STACK_SIZE]; int nb_stack; + + /* True to reject parse in visit_end_struct() if unvisited keys remain. */ bool strict; }; @@ -47,19 +54,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; } @@ -72,20 +89,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++;