From patchwork Thu Jul 7 11:27:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 9218787 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9A43360574 for ; Thu, 7 Jul 2016 11:28:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8471C28893 for ; Thu, 7 Jul 2016 11:28:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 790AC28895; Thu, 7 Jul 2016 11:28:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 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.wl.linuxfoundation.org (Postfix) with ESMTPS id D8C8D28893 for ; Thu, 7 Jul 2016 11:28:19 +0000 (UTC) Received: from localhost ([::1]:38973 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bL7Te-0002UJ-AN for patchwork-qemu-devel@patchwork.kernel.org; Thu, 07 Jul 2016 07:28:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60021) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bL7TL-0002UC-74 for qemu-devel@nongnu.org; Thu, 07 Jul 2016 07:28:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bL7TH-0003I0-FZ for qemu-devel@nongnu.org; Thu, 07 Jul 2016 07:27:58 -0400 Received: from mail-wm0-x242.google.com ([2a00:1450:400c:c09::242]:35574) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bL7TH-0003Hw-3X for qemu-devel@nongnu.org; Thu, 07 Jul 2016 07:27:55 -0400 Received: by mail-wm0-x242.google.com with SMTP id a66so4738694wme.2 for ; Thu, 07 Jul 2016 04:27:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id; bh=j+Oud95iht6G0kkUxjEgljcFBLtSpVZw2ksdz26j7y4=; b=ZFAE6ARIUwcrv0v6dORZg5wlVZrd75LXV+SEa5YXd51ABBWzsg8+T/EK4k8AzlWh33 3QmNt7FQGUHcgrvohUT2MWRUe3p/kMr/QP9h2Ui2IHvywktwwhbzIC3uysp5FQUlQGm1 fckXB0a4dQf19uwK68m3CYLnMBzNEJYZXtaYO1bQGdktUT31eUaJ/vxTTTnluR8Y60sZ 0nui+B17RpQN/uzU8TCLh9qINUXRafduGHHVd6Dz2eE5IpEftNumfvnDk3IOFVzBBJnW WOF/baKZBK7+SxvmJvU4tY3zVLShJuX1F2lrKoz3z06dr8SowvFE+qtmyZdw63scQahd +rPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=j+Oud95iht6G0kkUxjEgljcFBLtSpVZw2ksdz26j7y4=; b=lnrk3d3+otObsrYTHS2hI3m8vBB2Dwf/dVtjiTQYjUzr7thugG7S7jt8Ju4WtONT4e hHmHs4dodG9qAl8Vk4hzBm+fo/RQ+f/SbSbMuL9zkXi/xs/ihKz9LKLyJKaVrhoo5TSa XmuZ9oDv4yBoIwFUs5PiQSVQ5JwLjO8o9EpYdx8+XzkxZmSRlgdeswkYVK3JckLpTGqA uaw7G0WpUzMB6LBm069giSXlkMY01+87IO3QH9NoqCA+dqhYYe7IbAAtU7Z7zdaJe8Rf Arl9vjpSkJnGQB6SfnQTp2goUDbRJVomlA4zRn1i7xjr8A6b5wtFyj/KFGby3XpvrQ60 KTEA== X-Gm-Message-State: ALyK8tJF9sSRDaW89SKaOxNAqhZTir5/rleanXZjona7aE8yoQLl00XS2u50Y508uNlNZA== X-Received: by 10.28.163.70 with SMTP id m67mr25314897wme.38.1467890874283; Thu, 07 Jul 2016 04:27:54 -0700 (PDT) Received: from donizetti.lan (94-39-188-118.adsl-ull.clienti.tiscali.it. [94.39.188.118]) by smtp.gmail.com with ESMTPSA id bf5sm1673824wjc.12.2016.07.07.04.27.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Jul 2016 04:27:52 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 7 Jul 2016 13:27:45 +0200 Message-Id: <1467890865-8553-1-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 2.7.4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::242 Subject: [Qemu-devel] [PATCH v2] qapi: change QmpInputVisitor to QSLIST 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: , Cc: armbru@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This saves a lot of memory compared to a statically-sized array, or at least 24kb could be considered a lot on an Atari ST. It also makes the code more similar to QmpOutputVisitor. Signed-off-by: Paolo Bonzini Reviewed-by: Eric Blake --- v1->v2: leave assert in check_struct [Eric, Markus] qapi/qmp-input-visitor.c | 54 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/qapi/qmp-input-visitor.c b/qapi/qmp-input-visitor.c index aea90a1..ad33a7b 100644 --- a/qapi/qmp-input-visitor.c +++ b/qapi/qmp-input-visitor.c @@ -29,6 +29,8 @@ typedef struct StackObject GHashTable *h; /* If obj is dict: unvisited keys */ const QListEntry *entry; /* If obj is list: unvisited tail */ + + QSLIST_ENTRY(StackObject) node; } StackObject; struct QmpInputVisitor @@ -40,8 +42,7 @@ struct QmpInputVisitor /* Stack of objects being visited (all entries will be either * QDict or QList). */ - StackObject stack[QIV_STACK_SIZE]; - int nb_stack; + QSLIST_HEAD(, StackObject) stack; /* True to reject parse in visit_end_struct() if unvisited keys remain. */ bool strict; @@ -60,13 +61,13 @@ static QObject *qmp_input_get_object(QmpInputVisitor *qiv, QObject *qobj; QObject *ret; - if (!qiv->nb_stack) { + if (QSLIST_EMPTY(&qiv->stack)) { /* Starting at root, name is ignored. */ return qiv->root; } /* We are in a container; find the next element. */ - tos = &qiv->stack[qiv->nb_stack - 1]; + tos = QSLIST_FIRST(&qiv->stack); qobj = tos->obj; assert(qobj); @@ -99,17 +100,10 @@ static const QListEntry *qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp) { GHashTable *h; - StackObject *tos = &qiv->stack[qiv->nb_stack]; + StackObject *tos = g_new0(StackObject, 1); assert(obj); - if (qiv->nb_stack >= QIV_STACK_SIZE) { - error_setg(errp, "An internal buffer overran"); - return NULL; - } - tos->obj = obj; - assert(!tos->h); - assert(!tos->entry); if (qiv->strict && qobject_type(obj) == QTYPE_QDICT) { h = g_hash_table_new(g_str_hash, g_str_equal); @@ -119,7 +113,7 @@ static const QListEntry *qmp_input_push(QmpInputVisitor *qiv, QObject *obj, tos->entry = qlist_first(qobject_to_qlist(obj)); } - qiv->nb_stack++; + QSLIST_INSERT_HEAD(&qiv->stack, tos, node); return tos->entry; } @@ -127,10 +121,9 @@ static const QListEntry *qmp_input_push(QmpInputVisitor *qiv, QObject *obj, static void qmp_input_check_struct(Visitor *v, Error **errp) { QmpInputVisitor *qiv = to_qiv(v); - StackObject *tos = &qiv->stack[qiv->nb_stack - 1]; - - assert(qiv->nb_stack > 0); + StackObject *tos = QSLIST_FIRST(&qiv->stack); + assert(tos && !tos->entry); if (qiv->strict) { GHashTable *const top_ht = tos->h; if (top_ht) { @@ -145,22 +138,25 @@ static void qmp_input_check_struct(Visitor *v, Error **errp) } } -static void qmp_input_pop(Visitor *v) +static void qmp_input_stack_pop(QmpInputVisitor *qiv) { - QmpInputVisitor *qiv = to_qiv(v); - StackObject *tos = &qiv->stack[qiv->nb_stack - 1]; - - assert(qiv->nb_stack > 0); + StackObject *tos = QSLIST_FIRST(&qiv->stack); if (qiv->strict) { - GHashTable * const top_ht = qiv->stack[qiv->nb_stack - 1].h; - if (top_ht) { - g_hash_table_unref(top_ht); + if (tos->h) { + g_hash_table_unref(tos->h); } - tos->h = NULL; } - qiv->nb_stack--; + QSLIST_REMOVE_HEAD(&qiv->stack, node); + g_free(tos); +} + +static void qmp_input_pop(Visitor *v) +{ + QmpInputVisitor *qiv = to_qiv(v); + + qmp_input_stack_pop(qiv); } static void qmp_input_start_struct(Visitor *v, const char *name, void **obj, @@ -221,7 +217,7 @@ static GenericList *qmp_input_next_list(Visitor *v, GenericList *tail, size_t size) { QmpInputVisitor *qiv = to_qiv(v); - StackObject *so = &qiv->stack[qiv->nb_stack - 1]; + StackObject *so = QSLIST_FIRST(&qiv->stack); if (!so->entry) { return NULL; @@ -377,6 +373,10 @@ Visitor *qmp_input_get_visitor(QmpInputVisitor *v) void qmp_input_visitor_cleanup(QmpInputVisitor *v) { + while (!QSLIST_EMPTY(&v->stack)) { + qmp_input_stack_pop(v); + } + qobject_decref(v->root); g_free(v); }