From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52610) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fnNBq-0006lc-Lk for qemu-devel@nongnu.org; Wed, 08 Aug 2018 08:03:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fnNBk-0008VT-4p for qemu-devel@nongnu.org; Wed, 08 Aug 2018 08:03:46 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:45394 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fnNBj-0008UI-Qv for qemu-devel@nongnu.org; Wed, 08 Aug 2018 08:03:39 -0400 From: Markus Armbruster Date: Wed, 8 Aug 2018 14:03:09 +0200 Message-Id: <20180808120334.10970-32-armbru@redhat.com> In-Reply-To: <20180808120334.10970-1-armbru@redhat.com> References: <20180808120334.10970-1-armbru@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH 31/56] json-parser: simplify and avoid JSONParserContext allocation List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: marcandre.lureau@redhat.com, mdroth@linux.vnet.ibm.com, eblake@redhat.com From: Marc-Andr=C3=A9 Lureau parser_context_new/free() are only used from json_parser_parse(). We can fold the code there and avoid an allocation altogether. Signed-off-by: Marc-Andr=C3=A9 Lureau Message-Id: <20180719184111.5129-9-marcandre.lureau@redhat.com> Reviewed-by: Markus Armbruster --- qobject/json-parser.c | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/qobject/json-parser.c b/qobject/json-parser.c index 703065fa2b..b14336e653 100644 --- a/qobject/json-parser.c +++ b/qobject/json-parser.c @@ -232,33 +232,6 @@ static JSONToken *parser_context_peek_token(JSONPars= erContext *ctxt) return g_queue_peek_head(ctxt->buf); } =20 -static JSONParserContext *parser_context_new(GQueue *tokens) -{ - JSONParserContext *ctxt; - - if (!tokens) { - return NULL; - } - - ctxt =3D g_malloc0(sizeof(JSONParserContext)); - ctxt->buf =3D tokens; - - return ctxt; -} - -/* to support error propagation, ctxt->err must be freed separately */ -static void parser_context_free(JSONParserContext *ctxt) -{ - if (ctxt) { - while (!g_queue_is_empty(ctxt->buf)) { - parser_context_pop_token(ctxt); - } - g_free(ctxt->current); - g_queue_free(ctxt->buf); - g_free(ctxt); - } -} - /** * Parsing rules */ @@ -570,18 +543,22 @@ QObject *json_parser_parse(GQueue *tokens, va_list = *ap) =20 QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp= ) { - JSONParserContext *ctxt =3D parser_context_new(tokens); + JSONParserContext ctxt =3D { .buf =3D tokens }; QObject *result; =20 - if (!ctxt) { + if (!tokens) { return NULL; } =20 - result =3D parse_value(ctxt, ap); + result =3D parse_value(&ctxt, ap); =20 - error_propagate(errp, ctxt->err); + error_propagate(errp, ctxt.err); =20 - parser_context_free(ctxt); + while (!g_queue_is_empty(ctxt.buf)) { + parser_context_pop_token(&ctxt); + } + g_free(ctxt.current); + g_queue_free(ctxt.buf); =20 return result; } --=20 2.17.1