diff options
author | Marc-André Lureau <marcandre.lureau@redhat.com> | 2018-08-23 18:39:59 +0200 |
---|---|---|
committer | Markus Armbruster <armbru@redhat.com> | 2018-08-24 20:26:37 +0200 |
commit | e8b19d7d7300366a1dd85273512657bbeab564ab (patch) | |
tree | 26227aef51ef6599071ab19151aafda25bf62fdd /qobject | |
parent | 7c1e1d5481fe8d2e757469179f9ccd14e8838ed1 (diff) | |
download | qemu-e8b19d7d7300366a1dd85273512657bbeab564ab.zip qemu-e8b19d7d7300366a1dd85273512657bbeab564ab.tar.gz qemu-e8b19d7d7300366a1dd85273512657bbeab564ab.tar.bz2 |
json-parser: simplify and avoid JSONParserContext allocation
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é Lureau <marcandre.lureau@redhat.com>
Message-Id: <20180719184111.5129-9-marcandre.lureau@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20180823164025.12553-33-armbru@redhat.com>
Diffstat (limited to 'qobject')
-rw-r--r-- | qobject/json-parser.c | 41 |
1 files changed, 9 insertions, 32 deletions
diff --git a/qobject/json-parser.c b/qobject/json-parser.c index 73e6ad7..7bfa082 100644 --- a/qobject/json-parser.c +++ b/qobject/json-parser.c @@ -237,33 +237,6 @@ static JSONToken *parser_context_peek_token(JSONParserContext *ctxt) return g_queue_peek_head(ctxt->buf); } -static JSONParserContext *parser_context_new(GQueue *tokens) -{ - JSONParserContext *ctxt; - - if (!tokens) { - return NULL; - } - - ctxt = g_malloc0(sizeof(JSONParserContext)); - ctxt->buf = 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 */ @@ -575,18 +548,22 @@ QObject *json_parser_parse(GQueue *tokens, va_list *ap) QObject *json_parser_parse_err(GQueue *tokens, va_list *ap, Error **errp) { - JSONParserContext *ctxt = parser_context_new(tokens); + JSONParserContext ctxt = { .buf = tokens }; QObject *result; - if (!ctxt) { + if (!tokens) { return NULL; } - result = parse_value(ctxt, ap); + result = parse_value(&ctxt, ap); - error_propagate(errp, ctxt->err); + error_propagate(errp, ctxt.err); - 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); return result; } |