Introduce 'single_generic_device' boolean for clarity.
Allocate base_name on entry. Replace the g_free() call
by g_autofree qualifier. Use g_strconcat() instead of
g_strdup_printf().

Signed-off-by: Philippe Mathieu-Daudé <phi...@linaro.org>
---
 hw/virtio/virtio-pci.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
index a72f878e949..15383570c91 100644
--- a/hw/virtio/virtio-pci.c
+++ b/hw/virtio/virtio-pci.c
@@ -2469,7 +2469,12 @@ static void 
virtio_pci_non_transitional_instance_init(Object *obj)
 
 void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t)
 {
-    char *base_name = NULL;
+    /* No base type -> register a single generic device type */
+    bool single_generic_device = !t->base_name;
+    /* use intermediate %s-base-type to add generic device props */
+    g_autofree char *base_name = single_generic_device
+                                 ? g_strconcat(t->generic_name, "-base-type", 
NULL)
+                                 : NULL;
     TypeInfo base_type_info = {
         .name          = t->base_name,
         .parent        = t->parent ? t->parent : TYPE_VIRTIO_PCI,
@@ -2492,9 +2497,6 @@ void virtio_pci_types_register(const 
VirtioPCIDeviceTypeInfo *t)
     };
 
     if (!base_type_info.name) {
-        /* No base type -> register a single generic device type */
-        /* use intermediate %s-base-type to add generic device props */
-        base_name = g_strdup_printf("%s-base-type", t->generic_name);
         base_type_info.name = base_name;
         base_type_info.class_init = virtio_pci_generic_class_init;
 
@@ -2544,7 +2546,6 @@ void virtio_pci_types_register(const 
VirtioPCIDeviceTypeInfo *t)
         };
         type_register_static(&transitional_type_info);
     }
-    g_free(base_name);
 }
 
 unsigned virtio_pci_optimal_num_queues(unsigned fixed_queues)
-- 
2.47.1


Reply via email to