cryptospore / rpms / qemu-kvm

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