diff mbox series

[v2,08/18] json-parser: simplify and avoid JSONParserContext allocation

Message ID 20180719184111.5129-9-marcandre.lureau@redhat.com
State New
Headers show
Series monitor: various code simplification and fixes | expand

Commit Message

Marc-André Lureau July 19, 2018, 6:41 p.m. UTC
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>
---
 qobject/json-parser.c | 41 +++++++++--------------------------------
 1 file changed, 9 insertions(+), 32 deletions(-)

Comments

Markus Armbruster July 20, 2018, 6:28 a.m. UTC | #1
Marc-André Lureau <marcandre.lureau@redhat.com> writes:

> 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>
> ---
>  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 9a7004e680..6baf73b4b9 100644
> --- a/qobject/json-parser.c
> +++ b/qobject/json-parser.c
> @@ -244,33 +244,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
>   */
> @@ -577,18 +550,22 @@ static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
>  
>  QObject *json_parser_parse(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;
>  }

A lovely simplification.

Reviewed-by: Markus Armbruster <armbru@redhat.com>
diff mbox series

Patch

diff --git a/qobject/json-parser.c b/qobject/json-parser.c
index 9a7004e680..6baf73b4b9 100644
--- a/qobject/json-parser.c
+++ b/qobject/json-parser.c
@@ -244,33 +244,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
  */
@@ -577,18 +550,22 @@  static QObject *parse_value(JSONParserContext *ctxt, va_list *ap)
 
 QObject *json_parser_parse(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;
 }