From patchwork Tue Mar 1 05:14:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 8461871 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 9C6E6C0553 for ; Tue, 1 Mar 2016 05:16:59 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E91DE2024F for ; Tue, 1 Mar 2016 05:16:58 +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 2706620221 for ; Tue, 1 Mar 2016 05:16:58 +0000 (UTC) Received: from localhost ([::1]:40890 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aacg5-0003nz-D5 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 01 Mar 2016 00:16:57 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59673) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aacdt-0000HA-HT for qemu-devel@nongnu.org; Tue, 01 Mar 2016 00:14:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aacdr-0006Ou-4t for qemu-devel@nongnu.org; Tue, 01 Mar 2016 00:14:41 -0500 Received: from mx1.redhat.com ([209.132.183.28]:38343) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aacdq-0006Om-TT for qemu-devel@nongnu.org; Tue, 01 Mar 2016 00:14:39 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 9D9E2486A0; Tue, 1 Mar 2016 05:14:38 +0000 (UTC) Received: from red.redhat.com (ovpn-113-165.phx2.redhat.com [10.3.113.165]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u215EZ9O018132; Tue, 1 Mar 2016 00:14:38 -0500 From: Eric Blake To: qemu-devel@nongnu.org Date: Mon, 29 Feb 2016 22:14:18 -0700 Message-Id: <1456809272-14184-5-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> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: armbru@redhat.com, Michael Roth Subject: [Qemu-devel] [PATCH v12 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org 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 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 --- 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++;