diff mbox series

[v3,12/19] hw/virtio/virtio-pci: Always allocate QOM type base_name

Message ID 20250212213249.45574-13-philmd@linaro.org
State New
Headers show
Series qom: Constify class_data | expand

Commit Message

Philippe Mathieu-Daudé Feb. 12, 2025, 9:32 p.m. UTC
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é <philmd@linaro.org>
---
 hw/virtio/virtio-pci.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Comments

Richard Henderson Feb. 12, 2025, 10:06 p.m. UTC | #1
On 2/12/25 13:32, Philippe Mathieu-Daudé wrote:
> 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é <philmd@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;

If you're going to introduce this name, you should use it beyond just the next line.
Most of the rest of the function could use this instead of repeated checks against 
t->base_name.


r~

> +    /* 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)
diff mbox series

Patch

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)