9ae3a8
From 39da81cf143ccc400263362a5319803063ad14cf Mon Sep 17 00:00:00 2001
9ae3a8
From: Markus Armbruster <armbru@redhat.com>
9ae3a8
Date: Wed, 27 Jul 2016 07:35:07 +0200
9ae3a8
Subject: [PATCH 09/16] qjson: replace QString in JSONLexer with GString
9ae3a8
9ae3a8
RH-Author: Markus Armbruster <armbru@redhat.com>
9ae3a8
Message-id: <1469604913-12442-11-git-send-email-armbru@redhat.com>
9ae3a8
Patchwork-id: 71480
9ae3a8
O-Subject: [RHEL-7.3 qemu-kvm PATCH v2 09/15] qjson: replace QString in JSONLexer with GString
9ae3a8
Bugzilla: 1276036
9ae3a8
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
RH-Acked-by: John Snow <jsnow@redhat.com>
9ae3a8
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
9ae3a8
From: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
9ae3a8
JSONLexer only needs a simple resizable buffer.  json-streamer.c
9ae3a8
can allocate memory for each token instead of relying on reference
9ae3a8
counting of QStrings.
9ae3a8
9ae3a8
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
Message-Id: <1448300659-23559-2-git-send-email-pbonzini@redhat.com>
9ae3a8
[Straightforwardly rebased on my patches, checkpatch made happy]
9ae3a8
Signed-off-by: Markus Armbruster <armbru@redhat.com>
9ae3a8
Reviewed-by: Eric Blake <eblake@redhat.com>
9ae3a8
(cherry picked from commit d2ca7c0b0d876cf0e219ae7a92252626b0913a28)
9ae3a8
Signed-off-by: Markus Armbruster <armbru@redhat.com>
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 include/qapi/qmp/json-lexer.h    |  8 ++++----
9ae3a8
 include/qapi/qmp/json-streamer.h |  1 +
9ae3a8
 qobject/json-lexer.c             | 22 ++++++++--------------
9ae3a8
 qobject/json-streamer.c          |  9 +++++----
9ae3a8
 4 files changed, 18 insertions(+), 22 deletions(-)
9ae3a8
9ae3a8
diff --git a/include/qapi/qmp/json-lexer.h b/include/qapi/qmp/json-lexer.h
9ae3a8
index f3e8dc7..cb456d5 100644
9ae3a8
--- a/include/qapi/qmp/json-lexer.h
9ae3a8
+++ b/include/qapi/qmp/json-lexer.h
9ae3a8
@@ -14,8 +14,7 @@
9ae3a8
 #ifndef QEMU_JSON_LEXER_H
9ae3a8
 #define QEMU_JSON_LEXER_H
9ae3a8
 
9ae3a8
-#include "qapi/qmp/qstring.h"
9ae3a8
-#include "qapi/qmp/qlist.h"
9ae3a8
+#include "glib-compat.h"
9ae3a8
 
9ae3a8
 typedef enum json_token_type {
9ae3a8
     JSON_MIN = 100,
9ae3a8
@@ -36,13 +35,14 @@ typedef enum json_token_type {
9ae3a8
 
9ae3a8
 typedef struct JSONLexer JSONLexer;
9ae3a8
 
9ae3a8
-typedef void (JSONLexerEmitter)(JSONLexer *, QString *, JSONTokenType, int x, int y);
9ae3a8
+typedef void (JSONLexerEmitter)(JSONLexer *, GString *,
9ae3a8
+                                JSONTokenType, int x, int y);
9ae3a8
 
9ae3a8
 struct JSONLexer
9ae3a8
 {
9ae3a8
     JSONLexerEmitter *emit;
9ae3a8
     int state;
9ae3a8
-    QString *token;
9ae3a8
+    GString *token;
9ae3a8
     int x, y;
9ae3a8
 };
9ae3a8
 
9ae3a8
diff --git a/include/qapi/qmp/json-streamer.h b/include/qapi/qmp/json-streamer.h
9ae3a8
index 823f7d7..e901144 100644
9ae3a8
--- a/include/qapi/qmp/json-streamer.h
9ae3a8
+++ b/include/qapi/qmp/json-streamer.h
9ae3a8
@@ -14,6 +14,7 @@
9ae3a8
 #ifndef QEMU_JSON_STREAMER_H
9ae3a8
 #define QEMU_JSON_STREAMER_H
9ae3a8
 
9ae3a8
+#include <stdint.h>
9ae3a8
 #include "qapi/qmp/qlist.h"
9ae3a8
 #include "qapi/qmp/json-lexer.h"
9ae3a8
 
9ae3a8
diff --git a/qobject/json-lexer.c b/qobject/json-lexer.c
9ae3a8
index 1bfff02..b89e24f 100644
9ae3a8
--- a/qobject/json-lexer.c
9ae3a8
+++ b/qobject/json-lexer.c
9ae3a8
@@ -11,12 +11,9 @@
9ae3a8
  *
9ae3a8
  */
9ae3a8
 
9ae3a8
-#include "qapi/qmp/qstring.h"
9ae3a8
-#include "qapi/qmp/qlist.h"
9ae3a8
-#include "qapi/qmp/qdict.h"
9ae3a8
-#include "qapi/qmp/qint.h"
9ae3a8
 #include "qemu-common.h"
9ae3a8
 #include "qapi/qmp/json-lexer.h"
9ae3a8
+#include <stdint.h>
9ae3a8
 
9ae3a8
 #define MAX_TOKEN_SIZE (64ULL << 20)
9ae3a8
 
9ae3a8
@@ -276,7 +273,7 @@ void json_lexer_init(JSONLexer *lexer, JSONLexerEmitter func)
9ae3a8
 {
9ae3a8
     lexer->emit = func;
9ae3a8
     lexer->state = IN_START;
9ae3a8
-    lexer->token = qstring_new();
9ae3a8
+    lexer->token = g_string_sized_new(3);
9ae3a8
     lexer->x = lexer->y = 0;
9ae3a8
 }
9ae3a8
 
9ae3a8
@@ -295,7 +292,7 @@ static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
9ae3a8
         new_state = json_lexer[lexer->state][(uint8_t)ch];
9ae3a8
         char_consumed = !TERMINAL_NEEDED_LOOKAHEAD(lexer->state, new_state);
9ae3a8
         if (char_consumed) {
9ae3a8
-            qstring_append_chr(lexer->token, ch);
9ae3a8
+            g_string_append_c(lexer->token, ch);
9ae3a8
         }
9ae3a8
 
9ae3a8
         switch (new_state) {
9ae3a8
@@ -313,8 +310,7 @@ static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
9ae3a8
             lexer->emit(lexer, lexer->token, new_state, lexer->x, lexer->y);
9ae3a8
             /* fall through */
9ae3a8
         case JSON_SKIP:
9ae3a8
-            QDECREF(lexer->token);
9ae3a8
-            lexer->token = qstring_new();
9ae3a8
+            g_string_truncate(lexer->token, 0);
9ae3a8
             new_state = IN_START;
9ae3a8
             break;
9ae3a8
         case IN_ERROR:
9ae3a8
@@ -332,8 +328,7 @@ static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
9ae3a8
              * induce an error/flush state.
9ae3a8
              */
9ae3a8
             lexer->emit(lexer, lexer->token, JSON_ERROR, lexer->x, lexer->y);
9ae3a8
-            QDECREF(lexer->token);
9ae3a8
-            lexer->token = qstring_new();
9ae3a8
+            g_string_truncate(lexer->token, 0);
9ae3a8
             new_state = IN_START;
9ae3a8
             lexer->state = new_state;
9ae3a8
             return 0;
9ae3a8
@@ -346,10 +341,9 @@ static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
9ae3a8
     /* Do not let a single token grow to an arbitrarily large size,
9ae3a8
      * this is a security consideration.
9ae3a8
      */
9ae3a8
-    if (lexer->token->length > MAX_TOKEN_SIZE) {
9ae3a8
+    if (lexer->token->len > MAX_TOKEN_SIZE) {
9ae3a8
         lexer->emit(lexer, lexer->token, lexer->state, lexer->x, lexer->y);
9ae3a8
-        QDECREF(lexer->token);
9ae3a8
-        lexer->token = qstring_new();
9ae3a8
+        g_string_truncate(lexer->token, 0);
9ae3a8
         lexer->state = IN_START;
9ae3a8
     }
9ae3a8
 
9ae3a8
@@ -379,5 +373,5 @@ int json_lexer_flush(JSONLexer *lexer)
9ae3a8
 
9ae3a8
 void json_lexer_destroy(JSONLexer *lexer)
9ae3a8
 {
9ae3a8
-    QDECREF(lexer->token);
9ae3a8
+    g_string_free(lexer->token, true);
9ae3a8
 }
9ae3a8
diff --git a/qobject/json-streamer.c b/qobject/json-streamer.c
9ae3a8
index 4a161a1..7292f3a 100644
9ae3a8
--- a/qobject/json-streamer.c
9ae3a8
+++ b/qobject/json-streamer.c
9ae3a8
@@ -12,6 +12,7 @@
9ae3a8
  */
9ae3a8
 
9ae3a8
 #include "qapi/qmp/qlist.h"
9ae3a8
+#include "qapi/qmp/qstring.h"
9ae3a8
 #include "qapi/qmp/qint.h"
9ae3a8
 #include "qapi/qmp/qdict.h"
9ae3a8
 #include "qemu-common.h"
9ae3a8
@@ -21,7 +22,8 @@
9ae3a8
 #define MAX_TOKEN_SIZE (64ULL << 20)
9ae3a8
 #define MAX_NESTING (1ULL << 10)
9ae3a8
 
9ae3a8
-static void json_message_process_token(JSONLexer *lexer, QString *token, JSONTokenType type, int x, int y)
9ae3a8
+static void json_message_process_token(JSONLexer *lexer, GString *input,
9ae3a8
+                                       JSONTokenType type, int x, int y)
9ae3a8
 {
9ae3a8
     JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
9ae3a8
     QDict *dict;
9ae3a8
@@ -45,12 +47,11 @@ static void json_message_process_token(JSONLexer *lexer, QString *token, JSONTok
9ae3a8
 
9ae3a8
     dict = qdict_new();
9ae3a8
     qdict_put(dict, "type", qint_from_int(type));
9ae3a8
-    QINCREF(token);
9ae3a8
-    qdict_put(dict, "token", token);
9ae3a8
+    qdict_put(dict, "token", qstring_from_str(input->str));
9ae3a8
     qdict_put(dict, "x", qint_from_int(x));
9ae3a8
     qdict_put(dict, "y", qint_from_int(y));
9ae3a8
 
9ae3a8
-    parser->token_size += token->length;
9ae3a8
+    parser->token_size += input->len;
9ae3a8
 
9ae3a8
     qlist_append(parser->tokens, dict);
9ae3a8
 
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8