cryptospore / rpms / qemu-kvm

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