diff mbox series

[RFC,v3,15/27] monitor: introduce monitor_qmp_respond()

Message ID 20171106094643.14881-16-peterx@redhat.com
State New
Headers show
Series [RFC,v3,01/27] char-io: fix possible race on IOWatchPoll | expand

Commit Message

Peter Xu Nov. 6, 2017, 9:46 a.m. UTC
A tiny refactoring, preparing to split the QMP dispatcher away.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 monitor.c | 48 +++++++++++++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 17 deletions(-)

Comments

Fam Zheng Nov. 7, 2017, 7:24 a.m. UTC | #1
On Mon, 11/06 17:46, Peter Xu wrote:
> A tiny refactoring, preparing to split the QMP dispatcher away.
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  monitor.c | 48 +++++++++++++++++++++++++++++++-----------------
>  1 file changed, 31 insertions(+), 17 deletions(-)
> 
> diff --git a/monitor.c b/monitor.c
> index 442d711d5d..1e87de87f8 100644
> --- a/monitor.c
> +++ b/monitor.c
> @@ -3877,6 +3877,36 @@ static int monitor_can_read(void *opaque)
>      return (mon->suspend_cnt == 0) ? 1 : 0;
>  }
>  
> +/*
> + * When rsp/err/id is passed in, the function will be responsible for
> + * the cleanup.
> + */
> +static void monitor_qmp_respond(Monitor *mon, QObject *rsp,
> +                                Error *err, QObject *id)
> +{
> +    QDict *qdict = NULL;
> +
> +    if (err) {
> +        qdict = qdict_new();
> +        qdict_put_obj(qdict, "error", qmp_build_error_object(err));
> +        error_free(err);
> +        rsp = QOBJECT(qdict);
> +    }
> +
> +    if (rsp) {
> +        if (id) {
> +            /* This is for the qdict below. */
> +            qobject_incref(id);

A small change from the old code (that does no incref, and sets id to NULL
afterwards to the final qobject_decref(id) is no-op), but looks correct.

Reviewed-by: Fam Zheng <famz@redhat.com>

> +            qdict_put_obj(qobject_to_qdict(rsp), "id", id);
> +        }
> +
> +        monitor_json_emitter(mon, rsp);
> +    }
> +
> +    qobject_decref(id);
> +    qobject_decref(rsp);
> +}
> +
>  static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens,
>                                 void *opaque)
>  {
> @@ -3927,24 +3957,8 @@ static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens,
>      }
>  
>  err_out:
> -    if (err) {
> -        qdict = qdict_new();
> -        qdict_put_obj(qdict, "error", qmp_build_error_object(err));
> -        error_free(err);
> -        rsp = QOBJECT(qdict);
> -    }
> +    monitor_qmp_respond(mon, rsp, err, id);
>  
> -    if (rsp) {
> -        if (id) {
> -            qdict_put_obj(qobject_to_qdict(rsp), "id", id);
> -            id = NULL;
> -        }
> -
> -        monitor_json_emitter(mon, rsp);
> -    }
> -
> -    qobject_decref(id);
> -    qobject_decref(rsp);
>      qobject_decref(req);
>  }
>  
> -- 
> 2.13.5
>
diff mbox series

Patch

diff --git a/monitor.c b/monitor.c
index 442d711d5d..1e87de87f8 100644
--- a/monitor.c
+++ b/monitor.c
@@ -3877,6 +3877,36 @@  static int monitor_can_read(void *opaque)
     return (mon->suspend_cnt == 0) ? 1 : 0;
 }
 
+/*
+ * When rsp/err/id is passed in, the function will be responsible for
+ * the cleanup.
+ */
+static void monitor_qmp_respond(Monitor *mon, QObject *rsp,
+                                Error *err, QObject *id)
+{
+    QDict *qdict = NULL;
+
+    if (err) {
+        qdict = qdict_new();
+        qdict_put_obj(qdict, "error", qmp_build_error_object(err));
+        error_free(err);
+        rsp = QOBJECT(qdict);
+    }
+
+    if (rsp) {
+        if (id) {
+            /* This is for the qdict below. */
+            qobject_incref(id);
+            qdict_put_obj(qobject_to_qdict(rsp), "id", id);
+        }
+
+        monitor_json_emitter(mon, rsp);
+    }
+
+    qobject_decref(id);
+    qobject_decref(rsp);
+}
+
 static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens,
                                void *opaque)
 {
@@ -3927,24 +3957,8 @@  static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens,
     }
 
 err_out:
-    if (err) {
-        qdict = qdict_new();
-        qdict_put_obj(qdict, "error", qmp_build_error_object(err));
-        error_free(err);
-        rsp = QOBJECT(qdict);
-    }
+    monitor_qmp_respond(mon, rsp, err, id);
 
-    if (rsp) {
-        if (id) {
-            qdict_put_obj(qobject_to_qdict(rsp), "id", id);
-            id = NULL;
-        }
-
-        monitor_json_emitter(mon, rsp);
-    }
-
-    qobject_decref(id);
-    qobject_decref(rsp);
     qobject_decref(req);
 }