26ba25
From 6dc6db930a9906faa26dc8c4e89d4b356dbaaa58 Mon Sep 17 00:00:00 2001
26ba25
From: Markus Armbruster <armbru@redhat.com>
26ba25
Date: Mon, 18 Jun 2018 08:43:12 +0200
26ba25
Subject: [PATCH 014/268] qobject: Modify qobject_ref() to return obj
26ba25
MIME-Version: 1.0
26ba25
Content-Type: text/plain; charset=UTF-8
26ba25
Content-Transfer-Encoding: 8bit
26ba25
26ba25
RH-Author: Markus Armbruster <armbru@redhat.com>
26ba25
Message-id: <20180618084330.30009-6-armbru@redhat.com>
26ba25
Patchwork-id: 80738
26ba25
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 05/23] qobject: Modify qobject_ref() to return obj
26ba25
Bugzilla: 1557995
26ba25
RH-Acked-by: Max Reitz <mreitz@redhat.com>
26ba25
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
26ba25
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
26ba25
26ba25
From: Marc-André Lureau <marcandre.lureau@redhat.com>
26ba25
26ba25
For convenience and clarity, make it possible to call qobject_ref() at
26ba25
the time when the reference is associated with a variable, or
26ba25
argument, by making qobject_ref() return the same pointer as given.
26ba25
Use that to simplify the callers.
26ba25
26ba25
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
26ba25
Reviewed-by: Eric Blake <eblake@redhat.com>
26ba25
Message-Id: <20180419150145.24795-5-marcandre.lureau@redhat.com>
26ba25
Reviewed-by: Markus Armbruster <armbru@redhat.com>
26ba25
[Useless change to qobject_ref_impl() dropped, commit message improved
26ba25
slightly]
26ba25
Signed-off-by: Markus Armbruster <armbru@redhat.com>
26ba25
(cherry picked from commit f5a74a5a50387c6f980b2e2f94f062487a1826da)
26ba25
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 block.c                       |  8 ++++----
26ba25
 block/blkdebug.c              |  7 +++----
26ba25
 block/blkverify.c             |  8 ++++----
26ba25
 block/null.c                  |  3 +--
26ba25
 block/nvme.c                  |  3 +--
26ba25
 block/quorum.c                |  4 ++--
26ba25
 include/qapi/qmp/qnull.h      |  3 +--
26ba25
 include/qapi/qmp/qobject.h    |  9 ++++++++-
26ba25
 monitor.c                     | 20 +++++++-------------
26ba25
 qapi/qobject-input-visitor.c  |  6 ++----
26ba25
 qapi/qobject-output-visitor.c |  7 +++----
26ba25
 qobject/qdict.c               | 33 +++++++++++----------------------
26ba25
 12 files changed, 47 insertions(+), 64 deletions(-)
26ba25
26ba25
diff --git a/block.c b/block.c
26ba25
index 55a7984..676e57f 100644
26ba25
--- a/block.c
26ba25
+++ b/block.c
26ba25
@@ -5134,8 +5134,8 @@ static bool append_open_options(QDict *d, BlockDriverState *bs)
26ba25
             continue;
26ba25
         }
26ba25
 
26ba25
-        qobject_ref(qdict_entry_value(entry));
26ba25
-        qdict_put_obj(d, qdict_entry_key(entry), qdict_entry_value(entry));
26ba25
+        qdict_put_obj(d, qdict_entry_key(entry),
26ba25
+                      qobject_ref(qdict_entry_value(entry)));
26ba25
         found_any = true;
26ba25
     }
26ba25
 
26ba25
@@ -5207,8 +5207,8 @@ void bdrv_refresh_filename(BlockDriverState *bs)
26ba25
          * suffices without querying the (exact_)filename of this BDS. */
26ba25
         if (bs->file->bs->full_open_options) {
26ba25
             qdict_put_str(opts, "driver", drv->format_name);
26ba25
-            qobject_ref(bs->file->bs->full_open_options);
26ba25
-            qdict_put(opts, "file", bs->file->bs->full_open_options);
26ba25
+            qdict_put(opts, "file",
26ba25
+                      qobject_ref(bs->file->bs->full_open_options));
26ba25
 
26ba25
             bs->full_open_options = opts;
26ba25
         } else {
26ba25
diff --git a/block/blkdebug.c b/block/blkdebug.c
26ba25
index 689703d..053372c 100644
26ba25
--- a/block/blkdebug.c
26ba25
+++ b/block/blkdebug.c
26ba25
@@ -845,13 +845,12 @@ static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options)
26ba25
     opts = qdict_new();
26ba25
     qdict_put_str(opts, "driver", "blkdebug");
26ba25
 
26ba25
-    qobject_ref(bs->file->bs->full_open_options);
26ba25
-    qdict_put(opts, "image", bs->file->bs->full_open_options);
26ba25
+    qdict_put(opts, "image", qobject_ref(bs->file->bs->full_open_options));
26ba25
 
26ba25
     for (e = qdict_first(options); e; e = qdict_next(options, e)) {
26ba25
         if (strcmp(qdict_entry_key(e), "x-image")) {
26ba25
-            qobject_ref(qdict_entry_value(e));
26ba25
-            qdict_put_obj(opts, qdict_entry_key(e), qdict_entry_value(e));
26ba25
+            qdict_put_obj(opts, qdict_entry_key(e),
26ba25
+                          qobject_ref(qdict_entry_value(e)));
26ba25
         }
26ba25
     }
26ba25
 
26ba25
diff --git a/block/blkverify.c b/block/blkverify.c
26ba25
index 3cffcb1..754cc9e 100644
26ba25
--- a/block/blkverify.c
26ba25
+++ b/block/blkverify.c
26ba25
@@ -291,10 +291,10 @@ static void blkverify_refresh_filename(BlockDriverState *bs, QDict *options)
26ba25
         QDict *opts = qdict_new();
26ba25
         qdict_put_str(opts, "driver", "blkverify");
26ba25
 
26ba25
-        qobject_ref(bs->file->bs->full_open_options);
26ba25
-        qdict_put(opts, "raw", bs->file->bs->full_open_options);
26ba25
-        qobject_ref(s->test_file->bs->full_open_options);
26ba25
-        qdict_put(opts, "test", s->test_file->bs->full_open_options);
26ba25
+        qdict_put(opts, "raw",
26ba25
+                  qobject_ref(bs->file->bs->full_open_options));
26ba25
+        qdict_put(opts, "test",
26ba25
+                  qobject_ref(s->test_file->bs->full_open_options));
26ba25
 
26ba25
         bs->full_open_options = opts;
26ba25
     }
26ba25
diff --git a/block/null.c b/block/null.c
26ba25
index 700a2d0..3944550 100644
26ba25
--- a/block/null.c
26ba25
+++ b/block/null.c
26ba25
@@ -244,7 +244,6 @@ static int coroutine_fn null_co_block_status(BlockDriverState *bs,
26ba25
 
26ba25
 static void null_refresh_filename(BlockDriverState *bs, QDict *opts)
26ba25
 {
26ba25
-    qobject_ref(opts);
26ba25
     qdict_del(opts, "filename");
26ba25
 
26ba25
     if (!qdict_size(opts)) {
26ba25
@@ -253,7 +252,7 @@ static void null_refresh_filename(BlockDriverState *bs, QDict *opts)
26ba25
     }
26ba25
 
26ba25
     qdict_put_str(opts, "driver", bs->drv->format_name);
26ba25
-    bs->full_open_options = opts;
26ba25
+    bs->full_open_options = qobject_ref(opts);
26ba25
 }
26ba25
 
26ba25
 static BlockDriver bdrv_null_co = {
26ba25
diff --git a/block/nvme.c b/block/nvme.c
26ba25
index e192da9..6f71122 100644
26ba25
--- a/block/nvme.c
26ba25
+++ b/block/nvme.c
26ba25
@@ -1073,7 +1073,6 @@ static int nvme_reopen_prepare(BDRVReopenState *reopen_state,
26ba25
 
26ba25
 static void nvme_refresh_filename(BlockDriverState *bs, QDict *opts)
26ba25
 {
26ba25
-    qobject_ref(opts);
26ba25
     qdict_del(opts, "filename");
26ba25
 
26ba25
     if (!qdict_size(opts)) {
26ba25
@@ -1082,7 +1081,7 @@ static void nvme_refresh_filename(BlockDriverState *bs, QDict *opts)
26ba25
     }
26ba25
 
26ba25
     qdict_put_str(opts, "driver", bs->drv->format_name);
26ba25
-    bs->full_open_options = opts;
26ba25
+    bs->full_open_options = qobject_ref(opts);
26ba25
 }
26ba25
 
26ba25
 static void nvme_refresh_limits(BlockDriverState *bs, Error **errp)
26ba25
diff --git a/block/quorum.c b/block/quorum.c
26ba25
index 862cea3..a5051da 100644
26ba25
--- a/block/quorum.c
26ba25
+++ b/block/quorum.c
26ba25
@@ -1082,8 +1082,8 @@ static void quorum_refresh_filename(BlockDriverState *bs, QDict *options)
26ba25
 
26ba25
     children = qlist_new();
26ba25
     for (i = 0; i < s->num_children; i++) {
26ba25
-        qobject_ref(s->children[i]->bs->full_open_options);
26ba25
-        qlist_append(children, s->children[i]->bs->full_open_options);
26ba25
+        qlist_append(children,
26ba25
+                     qobject_ref(s->children[i]->bs->full_open_options));
26ba25
     }
26ba25
 
26ba25
     opts = qdict_new();
26ba25
diff --git a/include/qapi/qmp/qnull.h b/include/qapi/qmp/qnull.h
26ba25
index 75b29c6..c142688 100644
26ba25
--- a/include/qapi/qmp/qnull.h
26ba25
+++ b/include/qapi/qmp/qnull.h
26ba25
@@ -23,8 +23,7 @@ extern QNull qnull_;
26ba25
 
26ba25
 static inline QNull *qnull(void)
26ba25
 {
26ba25
-    qobject_ref(&qnull_);
26ba25
-    return &qnull_;
26ba25
+    return qobject_ref(&qnull_);
26ba25
 }
26ba25
 
26ba25
 bool qnull_is_equal(const QObject *x, const QObject *y);
26ba25
diff --git a/include/qapi/qmp/qobject.h b/include/qapi/qmp/qobject.h
26ba25
index e20006f..fcfd549 100644
26ba25
--- a/include/qapi/qmp/qobject.h
26ba25
+++ b/include/qapi/qmp/qobject.h
26ba25
@@ -103,8 +103,15 @@ static inline void qobject_unref_impl(QObject *obj)
26ba25
 
26ba25
 /**
26ba25
  * qobject_ref(): Increment QObject's reference count
26ba25
+ *
26ba25
+ * Returns: the same @obj. The type of @obj will be propagated to the
26ba25
+ * return type.
26ba25
  */
26ba25
-#define qobject_ref(obj) qobject_ref_impl(QOBJECT(obj))
26ba25
+#define qobject_ref(obj) ({                     \
26ba25
+    typeof(obj) _o = (obj);                     \
26ba25
+    qobject_ref_impl(QOBJECT(_o));              \
26ba25
+    _o;                                         \
26ba25
+})
26ba25
 
26ba25
 /**
26ba25
  * qobject_unref(): Decrement QObject's reference count, deallocate
26ba25
diff --git a/monitor.c b/monitor.c
26ba25
index 4f43eee..46814af 100644
26ba25
--- a/monitor.c
26ba25
+++ b/monitor.c
26ba25
@@ -494,9 +494,8 @@ static void monitor_json_emitter(Monitor *mon, QObject *data)
26ba25
          * caller won't free the data (which will be finally freed in
26ba25
          * responder thread).
26ba25
          */
26ba25
-        qobject_ref(data);
26ba25
         qemu_mutex_lock(&mon->qmp.qmp_queue_lock);
26ba25
-        g_queue_push_tail(mon->qmp.qmp_responses, data);
26ba25
+        g_queue_push_tail(mon->qmp.qmp_responses, qobject_ref(data));
26ba25
         qemu_mutex_unlock(&mon->qmp.qmp_queue_lock);
26ba25
         qemu_bh_schedule(mon_global.qmp_respond_bh);
26ba25
     } else {
26ba25
@@ -614,8 +613,7 @@ monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
26ba25
              * replacing a prior stored event if any.
26ba25
              */
26ba25
             qobject_unref(evstate->qdict);
26ba25
-            evstate->qdict = qdict;
26ba25
-            qobject_ref(evstate->qdict);
26ba25
+            evstate->qdict = qobject_ref(qdict);
26ba25
         } else {
26ba25
             /*
26ba25
              * Last send was (at least) evconf->rate ns ago.
26ba25
@@ -629,8 +627,7 @@ monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
26ba25
 
26ba25
             evstate = g_new(MonitorQAPIEventState, 1);
26ba25
             evstate->event = event;
26ba25
-            evstate->data = data;
26ba25
-            qobject_ref(evstate->data);
26ba25
+            evstate->data = qobject_ref(data);
26ba25
             evstate->qdict = NULL;
26ba25
             evstate->timer = timer_new_ns(event_clock_type,
26ba25
                                           monitor_qapi_event_handler,
26ba25
@@ -4048,9 +4045,7 @@ static void monitor_qmp_respond(Monitor *mon, QObject *rsp,
26ba25
 
26ba25
     if (rsp) {
26ba25
         if (id) {
26ba25
-            /* This is for the qdict below. */
26ba25
-            qobject_ref(id);
26ba25
-            qdict_put_obj(qobject_to(QDict, rsp), "id", id);
26ba25
+            qdict_put_obj(qobject_to(QDict, rsp), "id", qobject_ref(id));
26ba25
         }
26ba25
 
26ba25
         monitor_json_emitter(mon, rsp);
26ba25
@@ -4190,15 +4185,14 @@ static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
26ba25
         goto err;
26ba25
     }
26ba25
 
26ba25
-    qobject_ref(id);
26ba25
-    qdict_del(qdict, "id");
26ba25
-
26ba25
     req_obj = g_new0(QMPRequest, 1);
26ba25
     req_obj->mon = mon;
26ba25
-    req_obj->id = id;
26ba25
+    req_obj->id = qobject_ref(id);
26ba25
     req_obj->req = req;
26ba25
     req_obj->need_resume = false;
26ba25
 
26ba25
+    qdict_del(qdict, "id");
26ba25
+
26ba25
     if (qmp_is_oob(qdict)) {
26ba25
         /* Out-Of-Band (OOB) requests are executed directly in parser. */
26ba25
         trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(req_obj->id)
26ba25
diff --git a/qapi/qobject-input-visitor.c b/qapi/qobject-input-visitor.c
26ba25
index 7a290c4..da57f4c 100644
26ba25
--- a/qapi/qobject-input-visitor.c
26ba25
+++ b/qapi/qobject-input-visitor.c
26ba25
@@ -588,8 +588,7 @@ static void qobject_input_type_any(Visitor *v, const char *name, QObject **obj,
26ba25
         return;
26ba25
     }
26ba25
 
26ba25
-    qobject_ref(qobj);
26ba25
-    *obj = qobj;
26ba25
+    *obj = qobject_ref(qobj);
26ba25
 }
26ba25
 
26ba25
 static void qobject_input_type_null(Visitor *v, const char *name,
26ba25
@@ -677,8 +676,7 @@ static QObjectInputVisitor *qobject_input_visitor_base_new(QObject *obj)
26ba25
     v->visitor.optional = qobject_input_optional;
26ba25
     v->visitor.free = qobject_input_free;
26ba25
 
26ba25
-    v->root = obj;
26ba25
-    qobject_ref(obj);
26ba25
+    v->root = qobject_ref(obj);
26ba25
 
26ba25
     return v;
26ba25
 }
26ba25
diff --git a/qapi/qobject-output-visitor.c b/qapi/qobject-output-visitor.c
26ba25
index 3a933b4..89ffd8a 100644
26ba25
--- a/qapi/qobject-output-visitor.c
26ba25
+++ b/qapi/qobject-output-visitor.c
26ba25
@@ -188,8 +188,8 @@ static void qobject_output_type_any(Visitor *v, const char *name,
26ba25
                                     QObject **obj, Error **errp)
26ba25
 {
26ba25
     QObjectOutputVisitor *qov = to_qov(v);
26ba25
-    qobject_ref(*obj);
26ba25
-    qobject_output_add_obj(qov, name, *obj);
26ba25
+
26ba25
+    qobject_output_add_obj(qov, name, qobject_ref(*obj));
26ba25
 }
26ba25
 
26ba25
 static void qobject_output_type_null(Visitor *v, const char *name,
26ba25
@@ -210,8 +210,7 @@ static void qobject_output_complete(Visitor *v, void *opaque)
26ba25
     assert(qov->root && QSLIST_EMPTY(&qov->stack));
26ba25
     assert(opaque == qov->result);
26ba25
 
26ba25
-    qobject_ref(qov->root);
26ba25
-    *qov->result = qov->root;
26ba25
+    *qov->result = qobject_ref(qov->root);
26ba25
     qov->result = NULL;
26ba25
 }
26ba25
 
26ba25
diff --git a/qobject/qdict.c b/qobject/qdict.c
26ba25
index 2e9bd53..22800ee 100644
26ba25
--- a/qobject/qdict.c
26ba25
+++ b/qobject/qdict.c
26ba25
@@ -373,8 +373,7 @@ QDict *qdict_clone_shallow(const QDict *src)
26ba25
 
26ba25
     for (i = 0; i < QDICT_BUCKET_MAX; i++) {
26ba25
         QLIST_FOREACH(entry, &src->table[i], next) {
26ba25
-            qobject_ref(entry->value);
26ba25
-            qdict_put_obj(dest, entry->key, entry->value);
26ba25
+            qdict_put_obj(dest, entry->key, qobject_ref(entry->value));
26ba25
         }
26ba25
     }
26ba25
 
26ba25
@@ -480,8 +479,7 @@ void qdict_copy_default(QDict *dst, QDict *src, const char *key)
26ba25
 
26ba25
     val = qdict_get(src, key);
26ba25
     if (val) {
26ba25
-        qobject_ref(val);
26ba25
-        qdict_put_obj(dst, key, val);
26ba25
+        qdict_put_obj(dst, key, qobject_ref(val));
26ba25
     }
26ba25
 }
26ba25
 
26ba25
@@ -526,8 +524,7 @@ static void qdict_flatten_qlist(QList *qlist, QDict *target, const char *prefix)
26ba25
             qdict_flatten_qlist(qobject_to(QList, value), target, new_key);
26ba25
         } else {
26ba25
             /* All other types are moved to the target unchanged. */
26ba25
-            qobject_ref(value);
26ba25
-            qdict_put_obj(target, new_key, value);
26ba25
+            qdict_put_obj(target, new_key, qobject_ref(value));
26ba25
         }
26ba25
 
26ba25
         g_free(new_key);
26ba25
@@ -566,8 +563,7 @@ static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix)
26ba25
             delete = true;
26ba25
         } else if (prefix) {
26ba25
             /* All other objects are moved to the target unchanged. */
26ba25
-            qobject_ref(value);
26ba25
-            qdict_put_obj(target, new_key, value);
26ba25
+            qdict_put_obj(target, new_key, qobject_ref(value));
26ba25
             delete = true;
26ba25
         }
26ba25
 
26ba25
@@ -610,8 +606,7 @@ void qdict_extract_subqdict(QDict *src, QDict **dst, const char *start)
26ba25
     while (entry != NULL) {
26ba25
         next = qdict_next(src, entry);
26ba25
         if (strstart(entry->key, start, &p)) {
26ba25
-            qobject_ref(entry->value);
26ba25
-            qdict_put_obj(*dst, p, entry->value);
26ba25
+            qdict_put_obj(*dst, p, qobject_ref(entry->value));
26ba25
             qdict_del(src, entry->key);
26ba25
         }
26ba25
         entry = next;
26ba25
@@ -894,16 +889,14 @@ QObject *qdict_crumple(const QDict *src, Error **errp)
26ba25
                 qdict_put_obj(two_level, prefix, QOBJECT(child_dict));
26ba25
             }
26ba25
 
26ba25
-            qobject_ref(ent->value);
26ba25
-            qdict_put_obj(child_dict, suffix, ent->value);
26ba25
+            qdict_put_obj(child_dict, suffix, qobject_ref(ent->value));
26ba25
         } else {
26ba25
             if (child) {
26ba25
                 error_setg(errp, "Key %s prefix is already set as a dict",
26ba25
                            prefix);
26ba25
                 goto error;
26ba25
             }
26ba25
-            qobject_ref(ent->value);
26ba25
-            qdict_put_obj(two_level, prefix, ent->value);
26ba25
+            qdict_put_obj(two_level, prefix, qobject_ref(ent->value));
26ba25
         }
26ba25
 
26ba25
         g_free(prefix);
26ba25
@@ -924,8 +917,7 @@ QObject *qdict_crumple(const QDict *src, Error **errp)
26ba25
 
26ba25
             qdict_put_obj(multi_level, ent->key, child);
26ba25
         } else {
26ba25
-            qobject_ref(ent->value);
26ba25
-            qdict_put_obj(multi_level, ent->key, ent->value);
26ba25
+            qdict_put_obj(multi_level, ent->key, qobject_ref(ent->value));
26ba25
         }
26ba25
     }
26ba25
     qobject_unref(two_level);
26ba25
@@ -951,8 +943,7 @@ QObject *qdict_crumple(const QDict *src, Error **errp)
26ba25
                 goto error;
26ba25
             }
26ba25
 
26ba25
-            qobject_ref(child);
26ba25
-            qlist_append_obj(qobject_to(QList, dst), child);
26ba25
+            qlist_append_obj(qobject_to(QList, dst), qobject_ref(child));
26ba25
         }
26ba25
         qobject_unref(multi_level);
26ba25
         multi_level = NULL;
26ba25
@@ -1055,8 +1046,7 @@ void qdict_join(QDict *dest, QDict *src, bool overwrite)
26ba25
         next = qdict_next(src, entry);
26ba25
 
26ba25
         if (overwrite || !qdict_haskey(dest, entry->key)) {
26ba25
-            qobject_ref(entry->value);
26ba25
-            qdict_put_obj(dest, entry->key, entry->value);
26ba25
+            qdict_put_obj(dest, entry->key, qobject_ref(entry->value));
26ba25
             qdict_del(src, entry->key);
26ba25
         }
26ba25
 
26ba25
@@ -1088,8 +1078,7 @@ bool qdict_rename_keys(QDict *qdict, const QDictRenames *renames, Error **errp)
26ba25
             }
26ba25
 
26ba25
             qobj = qdict_get(qdict, renames->from);
26ba25
-            qobject_ref(qobj);
26ba25
-            qdict_put_obj(qdict, renames->to, qobj);
26ba25
+            qdict_put_obj(qdict, renames->to, qobject_ref(qobj));
26ba25
             qdict_del(qdict, renames->from);
26ba25
         }
26ba25
 
26ba25
-- 
26ba25
1.8.3.1
26ba25