From 0087625b7eb3a7e289dbdcce4a0cd1b844045edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Fri, 4 May 2018 10:18:28 -0400 Subject: [PATCH] qobject: Modify qobject_ref() to return obj For convenience and clarity, make it possible to call qobject_ref() at the time when the reference is associated with a variable, or argument, by making qobject_ref() return the same pointer as given. Use that to simplify the callers. Backports commit f5a74a5a50387c6f980b2e2f94f062487a1826da from qemu --- qemu/include/qapi/qmp/qnull.h | 3 +-- qemu/include/qapi/qmp/qobject.h | 9 ++++++++- qemu/qapi/qobject-input-visitor.c | 6 ++---- qemu/qapi/qobject-output-visitor.c | 3 +-- qemu/qobject/qdict.c | 27 +++++++++------------------ 5 files changed, 21 insertions(+), 27 deletions(-) diff --git a/qemu/include/qapi/qmp/qnull.h b/qemu/include/qapi/qmp/qnull.h index 75b29c6a..c1426882 100644 --- a/qemu/include/qapi/qmp/qnull.h +++ b/qemu/include/qapi/qmp/qnull.h @@ -23,8 +23,7 @@ extern QNull qnull_; static inline QNull *qnull(void) { - qobject_ref(&qnull_); - return &qnull_; + return qobject_ref(&qnull_); } bool qnull_is_equal(const QObject *x, const QObject *y); diff --git a/qemu/include/qapi/qmp/qobject.h b/qemu/include/qapi/qmp/qobject.h index e20006fa..fcfd5492 100644 --- a/qemu/include/qapi/qmp/qobject.h +++ b/qemu/include/qapi/qmp/qobject.h @@ -103,8 +103,15 @@ static inline void qobject_unref_impl(QObject *obj) /** * qobject_ref(): Increment QObject's reference count + * + * Returns: the same @obj. The type of @obj will be propagated to the + * return type. */ -#define qobject_ref(obj) qobject_ref_impl(QOBJECT(obj)) +#define qobject_ref(obj) ({ \ + typeof(obj) _o = (obj); \ + qobject_ref_impl(QOBJECT(_o)); \ + _o; \ +}) /** * qobject_unref(): Decrement QObject's reference count, deallocate diff --git a/qemu/qapi/qobject-input-visitor.c b/qemu/qapi/qobject-input-visitor.c index d86dfbbd..dbea2c89 100644 --- a/qemu/qapi/qobject-input-visitor.c +++ b/qemu/qapi/qobject-input-visitor.c @@ -463,8 +463,7 @@ static void qobject_input_type_any(Visitor *v, const char *name, QObject **obj, return; } - qobject_ref(qobj); - *obj = qobj; + *obj = qobject_ref(qobj); } static void qobject_input_type_null(Visitor *v, const char *name, @@ -536,8 +535,7 @@ static QObjectInputVisitor *qobject_input_visitor_base_new(QObject *obj) v->visitor.optional = qobject_input_optional; v->visitor.free = qobject_input_free; - v->root = obj; - qobject_ref(obj); + v->root = qobject_ref(obj); return v; } diff --git a/qemu/qapi/qobject-output-visitor.c b/qemu/qapi/qobject-output-visitor.c index fd893ae3..066a409f 100644 --- a/qemu/qapi/qobject-output-visitor.c +++ b/qemu/qapi/qobject-output-visitor.c @@ -191,8 +191,7 @@ static void qobject_output_type_any(Visitor *v, const char *name, QObject **obj, Error **errp) { QObjectOutputVisitor *qov = to_qov(v); - qobject_ref(*obj); - qobject_output_add_obj(qov, name, *obj); + qobject_output_add_obj(qov, name, qobject_ref(*obj)); } static void qobject_output_type_null(Visitor *v, const char *name, diff --git a/qemu/qobject/qdict.c b/qemu/qobject/qdict.c index 25c8ac95..7e222f93 100644 --- a/qemu/qobject/qdict.c +++ b/qemu/qobject/qdict.c @@ -493,8 +493,7 @@ static void qdict_flatten_qlist(QList *qlist, QDict *target, const char *prefix) qdict_flatten_qlist(qobject_to(QList, value), target, new_key); } else { /* All other types are moved to the target unchanged. */ - qobject_ref(value); - qdict_put_obj(target, new_key, value); + qdict_put_obj(target, new_key, qobject_ref(value)); } g_free(new_key); @@ -533,8 +532,7 @@ static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix) delete = true; } else if (prefix) { /* All other objects are moved to the target unchanged. */ - qobject_ref(value); - qdict_put_obj(target, new_key, value); + qdict_put_obj(target, new_key, qobject_ref(value)); delete = true; } @@ -576,8 +574,7 @@ bool qdict_rename_keys(QDict *qdict, const QDictRenames *renames, Error **errp) } qobj = qdict_get(qdict, renames->from); - qobject_ref(qobj); - qdict_put_obj(qdict, renames->to, qobj); + qdict_put_obj(qdict, renames->to, qobject_ref(qobj)); qdict_del(qdict, renames->from); } @@ -611,8 +608,7 @@ void qdict_extract_subqdict(QDict *src, QDict **dst, const char *start) while (entry != NULL) { next = qdict_next(src, entry); if (strstart(entry->key, start, &p)) { - qobject_ref(entry->value); - qdict_put_obj(*dst, p, entry->value); + qdict_put_obj(*dst, p, qobject_ref(entry->value)); qdict_del(src, entry->key); } entry = next; @@ -890,16 +886,14 @@ QObject *qdict_crumple(const QDict *src, Error **errp) qdict_put_obj(two_level, prefix, QOBJECT(child_dict)); } - qobject_ref(ent->value); - qdict_put_obj(child_dict, suffix, ent->value); + qdict_put_obj(child_dict, suffix, qobject_ref(ent->value)); } else { if (child) { error_setg(errp, "Key %s prefix is already set as a dict", prefix); goto error; } - qobject_ref(ent->value); - qdict_put_obj(two_level, prefix, ent->value); + qdict_put_obj(two_level, prefix, qobject_ref(ent->value)); } g_free(prefix); @@ -921,8 +915,7 @@ QObject *qdict_crumple(const QDict *src, Error **errp) qdict_put_obj(multi_level, ent->key, child); } else { - qobject_ref(ent->value); - qdict_put_obj(multi_level, ent->key, ent->value); + qdict_put_obj(multi_level, ent->key, qobject_ref(ent->value)); } } qobject_unref(two_level); @@ -948,8 +941,7 @@ QObject *qdict_crumple(const QDict *src, Error **errp) goto error; } - qobject_ref(child); - qlist_append_obj(qobject_to(QList, dst), child); + qlist_append_obj(qobject_to(QList, dst), qobject_ref(child)); } qobject_unref(multi_level); multi_level = NULL; @@ -990,8 +982,7 @@ void qdict_join(QDict *dest, QDict *src, bool overwrite) next = qdict_next(src, entry); if (overwrite || !qdict_haskey(dest, entry->key)) { - qobject_ref(entry->value); - qdict_put_obj(dest, entry->key, entry->value); + qdict_put_obj(dest, entry->key, qobject_ref(entry->value)); qdict_del(src, entry->key); }