a19a21
From 34f664093db2a6275fcddd768684c7319cfc01b4 Mon Sep 17 00:00:00 2001
709dde
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
a19a21
Date: Wed, 16 Dec 2020 16:06:06 -0500
a19a21
Subject: [PATCH 05/14] qapi: enable use of g_autoptr with QAPI types
709dde
MIME-Version: 1.0
709dde
Content-Type: text/plain; charset=UTF-8
709dde
Content-Transfer-Encoding: 8bit
709dde
709dde
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
a19a21
Message-id: <20201216160615.324213-2-marcandre.lureau@redhat.com>
a19a21
Patchwork-id: 100472
a19a21
O-Subject: [RHEL-8.4.0 qemu-kvm PATCH v2 01/10] qapi: enable use of g_autoptr with QAPI types
a19a21
Bugzilla: 1859494
a19a21
RH-Acked-by: Danilo de Paula <ddepaula@redhat.com>
a19a21
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
a19a21
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
709dde
709dde
From: Daniel P. Berrangé <berrange@redhat.com>
709dde
709dde
Currently QAPI generates a type and function for free'ing it:
709dde
709dde
  typedef struct QCryptoBlockCreateOptions QCryptoBlockCreateOptions;
709dde
  void qapi_free_QCryptoBlockCreateOptions(QCryptoBlockCreateOptions *obj);
709dde
709dde
This is used in the traditional manner:
709dde
709dde
  QCryptoBlockCreateOptions *opts = NULL;
709dde
709dde
  opts = g_new0(QCryptoBlockCreateOptions, 1);
709dde
709dde
  ....do stuff with opts...
709dde
709dde
  qapi_free_QCryptoBlockCreateOptions(opts);
709dde
709dde
Since bumping the min glib to 2.48, QEMU has incrementally adopted the
709dde
use of g_auto/g_autoptr. This allows the compiler to run a function to
709dde
free a variable when it goes out of scope, the benefit being the
709dde
compiler can guarantee it is freed in all possible code ptahs.
709dde
709dde
This benefit is applicable to QAPI types too, and given the seriously
709dde
long method names for some qapi_free_XXXX() functions, is much less
709dde
typing. This change thus makes the code generator emit:
709dde
709dde
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoBlockCreateOptions,
709dde
                              qapi_free_QCryptoBlockCreateOptions)
709dde
709dde
The above code example now becomes
709dde
709dde
  g_autoptr(QCryptoBlockCreateOptions) opts = NULL;
709dde
709dde
  opts = g_new0(QCryptoBlockCreateOptions, 1);
709dde
709dde
  ....do stuff with opts...
709dde
709dde
Note, if the local pointer needs to live beyond the scope holding the
709dde
variable, then g_steal_pointer can be used. This is useful to return the
709dde
pointer to the caller in the success codepath, while letting it be freed
709dde
in all error codepaths.
709dde
709dde
  return g_steal_pointer(&opts);
709dde
709dde
The crypto/block.h header needs updating to avoid symbol clash now that
709dde
the g_autoptr support is a standard QAPI feature.
709dde
709dde
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
709dde
Message-Id: <20200723153845.2934357-1-berrange@redhat.com>
709dde
Reviewed-by: Markus Armbruster <armbru@redhat.com>
709dde
Reviewed-by: Eric Blake <eblake@redhat.com>
709dde
Signed-off-by: Markus Armbruster <armbru@redhat.com>
709dde
709dde
(cherry picked from commit 221db5daf6b3666f1c8e4ca06ae45892e99a112f)
709dde
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
709dde
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
709dde
---
709dde
 docs/devel/qapi-code-gen.txt       |  2 ++
709dde
 scripts/qapi/types.py              |  1 +
709dde
 tests/test-qobject-input-visitor.c | 23 +++++++----------------
709dde
 3 files changed, 10 insertions(+), 16 deletions(-)
709dde
709dde
diff --git a/docs/devel/qapi-code-gen.txt b/docs/devel/qapi-code-gen.txt
709dde
index 45c93a43cc3..ca59c695fac 100644
709dde
--- a/docs/devel/qapi-code-gen.txt
709dde
+++ b/docs/devel/qapi-code-gen.txt
709dde
@@ -1278,6 +1278,7 @@ Example:
709dde
     };
709dde
 
709dde
     void qapi_free_UserDefOne(UserDefOne *obj);
709dde
+    G_DEFINE_AUTOPTR_CLEANUP_FUNC(UserDefOne, qapi_free_UserDefOne)
709dde
 
709dde
     struct UserDefOneList {
709dde
         UserDefOneList *next;
709dde
@@ -1285,6 +1286,7 @@ Example:
709dde
     };
709dde
 
709dde
     void qapi_free_UserDefOneList(UserDefOneList *obj);
709dde
+    G_DEFINE_AUTOPTR_CLEANUP_FUNC(UserDefOneList, qapi_free_UserDefOneList)
709dde
 
709dde
     struct q_obj_my_command_arg {
709dde
         UserDefOneList *arg1;
709dde
diff --git a/scripts/qapi/types.py b/scripts/qapi/types.py
709dde
index d8751daa049..c3be141dc90 100644
709dde
--- a/scripts/qapi/types.py
709dde
+++ b/scripts/qapi/types.py
709dde
@@ -213,6 +213,7 @@ def gen_type_cleanup_decl(name):
709dde
     ret = mcgen('''
709dde
 
709dde
 void qapi_free_%(c_name)s(%(c_name)s *obj);
709dde
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(%(c_name)s, qapi_free_%(c_name)s)
709dde
 ''',
709dde
                 c_name=c_name(name))
709dde
     return ret
709dde
diff --git a/tests/test-qobject-input-visitor.c b/tests/test-qobject-input-visitor.c
709dde
index 6bacabf0632..e41b91a2a6f 100644
709dde
--- a/tests/test-qobject-input-visitor.c
709dde
+++ b/tests/test-qobject-input-visitor.c
709dde
@@ -417,7 +417,7 @@ static void test_visitor_in_struct(TestInputVisitorData *data,
709dde
 static void test_visitor_in_struct_nested(TestInputVisitorData *data,
709dde
                                           const void *unused)
709dde
 {
709dde
-    UserDefTwo *udp = NULL;
709dde
+    g_autoptr(UserDefTwo) udp = NULL;
709dde
     Visitor *v;
709dde
 
709dde
     v = visitor_input_test_init(data, "{ 'string0': 'string0', "
709dde
@@ -433,8 +433,6 @@ static void test_visitor_in_struct_nested(TestInputVisitorData *data,
709dde
     g_assert_cmpstr(udp->dict1->dict2->userdef->string, ==, "string");
709dde
     g_assert_cmpstr(udp->dict1->dict2->string, ==, "string2");
709dde
     g_assert(udp->dict1->has_dict3 == false);
709dde
-
709dde
-    qapi_free_UserDefTwo(udp);
709dde
 }
709dde
 
709dde
 static void test_visitor_in_list(TestInputVisitorData *data,
709dde
@@ -546,7 +544,7 @@ static void test_visitor_in_union_flat(TestInputVisitorData *data,
709dde
                                        const void *unused)
709dde
 {
709dde
     Visitor *v;
709dde
-    UserDefFlatUnion *tmp;
709dde
+    g_autoptr(UserDefFlatUnion) tmp = NULL;
709dde
     UserDefUnionBase *base;
709dde
 
709dde
     v = visitor_input_test_init(data,
709dde
@@ -563,8 +561,6 @@ static void test_visitor_in_union_flat(TestInputVisitorData *data,
709dde
 
709dde
     base = qapi_UserDefFlatUnion_base(tmp);
709dde
     g_assert(&base->enum1 == &tmp->enum1);
709dde
-
709dde
-    qapi_free_UserDefFlatUnion(tmp);
709dde
 }
709dde
 
709dde
 static void test_visitor_in_alternate(TestInputVisitorData *data,
709dde
@@ -690,7 +686,7 @@ static void test_list_union_integer_helper(TestInputVisitorData *data,
709dde
                                            const void *unused,
709dde
                                            UserDefListUnionKind kind)
709dde
 {
709dde
-    UserDefListUnion *cvalue = NULL;
709dde
+    g_autoptr(UserDefListUnion) cvalue = NULL;
709dde
     Visitor *v;
709dde
     GString *gstr_list = g_string_new("");
709dde
     GString *gstr_union = g_string_new("");
709dde
@@ -782,7 +778,6 @@ static void test_list_union_integer_helper(TestInputVisitorData *data,
709dde
 
709dde
     g_string_free(gstr_union, true);
709dde
     g_string_free(gstr_list, true);
709dde
-    qapi_free_UserDefListUnion(cvalue);
709dde
 }
709dde
 
709dde
 static void test_visitor_in_list_union_int(TestInputVisitorData *data,
709dde
@@ -851,7 +846,7 @@ static void test_visitor_in_list_union_uint64(TestInputVisitorData *data,
709dde
 static void test_visitor_in_list_union_bool(TestInputVisitorData *data,
709dde
                                             const void *unused)
709dde
 {
709dde
-    UserDefListUnion *cvalue = NULL;
709dde
+    g_autoptr(UserDefListUnion) cvalue = NULL;
709dde
     boolList *elem = NULL;
709dde
     Visitor *v;
709dde
     GString *gstr_list = g_string_new("");
709dde
@@ -879,13 +874,12 @@ static void test_visitor_in_list_union_bool(TestInputVisitorData *data,
709dde
 
709dde
     g_string_free(gstr_union, true);
709dde
     g_string_free(gstr_list, true);
709dde
-    qapi_free_UserDefListUnion(cvalue);
709dde
 }
709dde
 
709dde
 static void test_visitor_in_list_union_string(TestInputVisitorData *data,
709dde
                                               const void *unused)
709dde
 {
709dde
-    UserDefListUnion *cvalue = NULL;
709dde
+    g_autoptr(UserDefListUnion) cvalue = NULL;
709dde
     strList *elem = NULL;
709dde
     Visitor *v;
709dde
     GString *gstr_list = g_string_new("");
709dde
@@ -914,7 +908,6 @@ static void test_visitor_in_list_union_string(TestInputVisitorData *data,
709dde
 
709dde
     g_string_free(gstr_union, true);
709dde
     g_string_free(gstr_list, true);
709dde
-    qapi_free_UserDefListUnion(cvalue);
709dde
 }
709dde
 
709dde
 #define DOUBLE_STR_MAX 16
709dde
@@ -922,7 +915,7 @@ static void test_visitor_in_list_union_string(TestInputVisitorData *data,
709dde
 static void test_visitor_in_list_union_number(TestInputVisitorData *data,
709dde
                                               const void *unused)
709dde
 {
709dde
-    UserDefListUnion *cvalue = NULL;
709dde
+    g_autoptr(UserDefListUnion) cvalue = NULL;
709dde
     numberList *elem = NULL;
709dde
     Visitor *v;
709dde
     GString *gstr_list = g_string_new("");
709dde
@@ -957,7 +950,6 @@ static void test_visitor_in_list_union_number(TestInputVisitorData *data,
709dde
 
709dde
     g_string_free(gstr_union, true);
709dde
     g_string_free(gstr_list, true);
709dde
-    qapi_free_UserDefListUnion(cvalue);
709dde
 }
709dde
 
709dde
 static void input_visitor_test_add(const char *testpath,
709dde
@@ -1253,7 +1245,7 @@ static void test_visitor_in_fail_alternate(TestInputVisitorData *data,
709dde
 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData *data,
709dde
                                               const QLitObject *qlit)
709dde
 {
709dde
-    SchemaInfoList *schema = NULL;
709dde
+    g_autoptr(SchemaInfoList) schema = NULL;
709dde
     QObject *obj = qobject_from_qlit(qlit);
709dde
     Visitor *v;
709dde
 
709dde
@@ -1262,7 +1254,6 @@ static void do_test_visitor_in_qmp_introspect(TestInputVisitorData *data,
709dde
     visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
709dde
     g_assert(schema);
709dde
 
709dde
-    qapi_free_SchemaInfoList(schema);
709dde
     qobject_unref(obj);
709dde
     visit_free(v);
709dde
 }
709dde
-- 
709dde
2.27.0
709dde