[Qemu-devel] [PATCH 07/11] qdev: simplify the SysBusDeviceClass::init path

Philippe Mathieu-Daudé posted 11 patches 7 years, 9 months ago
There is a newer version of this series
[Qemu-devel] [PATCH 07/11] qdev: simplify the SysBusDeviceClass::init path
Posted by Philippe Mathieu-Daudé 7 years, 9 months ago
The SysBusDevice is the last DeviceClass::init user.

Instead of using
  SysBusDeviceClass::realize
   -> DeviceClass::realize
       -> DeviceClass::init
           -> sysbus_device_init
              -> SysBusDeviceClass::init

Simplify the path by directly calling SysBusDeviceClass::init
in SysBusDeviceClass::realize:

  SysBusDeviceClass::realize
   -> SysBusDeviceClass::init

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/core/qdev.c   |  9 ---------
 hw/core/sysbus.c | 21 +++++++++------------
 2 files changed, 9 insertions(+), 21 deletions(-)

diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index 11112951a5..a4f76c8f5d 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -221,15 +221,6 @@ void device_listener_unregister(DeviceListener *listener)
 
 static void device_realize(DeviceState *dev, Error **errp)
 {
-    DeviceClass *dc = DEVICE_GET_CLASS(dev);
-
-    if (dc->init) {
-        int rc = dc->init(dev);
-        if (rc < 0) {
-            error_setg(errp, "Device initialization failed.");
-            return;
-        }
-    }
 }
 
 static void device_unrealize(DeviceState *dev, Error **errp)
diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c
index 04d6061f76..9edf43bc27 100644
--- a/hw/core/sysbus.c
+++ b/hw/core/sysbus.c
@@ -18,6 +18,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qapi/error.h"
 #include "hw/sysbus.h"
 #include "monitor/monitor.h"
 #include "exec/address-spaces.h"
@@ -200,22 +201,19 @@ void sysbus_init_ioports(SysBusDevice *dev, uint32_t ioport, uint32_t size)
     }
 }
 
-/* TODO remove, once users are converted to realize */
-static int sysbus_device_init(DeviceState *dev)
+static void sysbus_realize(DeviceState *dev, Error **errp)
 {
     SysBusDevice *sd = SYS_BUS_DEVICE(dev);
     SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
 
-    if (!sbc->init) {
-        return 0;
+    /* TODO remove, once users are converted to realize */
+    if (sbc->init) {
+        int rc = sbc->init(sd);
+        if (rc < 0) {
+            error_setg(errp, "Device initialization failed.");
+            return;
+        }
     }
-    return sbc->init(sd);
-}
-
-static void sysbus_realize(DeviceState *dev, Error **errp)
-{
-    SysBusDevice *sd = SYS_BUS_DEVICE(dev);
-    SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
 
     if (sbc->realize) {
         sbc->realize(sd, errp);
@@ -345,7 +343,6 @@ MemoryRegion *sysbus_address_space(SysBusDevice *dev)
 static void sysbus_device_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *k = DEVICE_CLASS(klass);
-    k->init = sysbus_device_init;
     k->realize = sysbus_realize;
     k->unrealize = sysbus_unrealize;
     k->bus_type = TYPE_SYSTEM_BUS;
-- 
2.15.1


Re: [Qemu-devel] [PATCH 07/11] qdev: simplify the SysBusDeviceClass::init path
Posted by Eduardo Habkost 7 years, 9 months ago
On Tue, Jan 16, 2018 at 10:15:51AM -0300, Philippe Mathieu-Daudé wrote:
> The SysBusDevice is the last DeviceClass::init user.
> 
> Instead of using
>   SysBusDeviceClass::realize
>    -> DeviceClass::realize
>        -> DeviceClass::init
>            -> sysbus_device_init
>               -> SysBusDeviceClass::init
> 
> Simplify the path by directly calling SysBusDeviceClass::init
> in SysBusDeviceClass::realize:
> 
>   SysBusDeviceClass::realize
>    -> SysBusDeviceClass::init
> 
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
>  hw/core/qdev.c   |  9 ---------
>  hw/core/sysbus.c | 21 +++++++++------------
>  2 files changed, 9 insertions(+), 21 deletions(-)
> 
> diff --git a/hw/core/qdev.c b/hw/core/qdev.c
> index 11112951a5..a4f76c8f5d 100644
> --- a/hw/core/qdev.c
> +++ b/hw/core/qdev.c
> @@ -221,15 +221,6 @@ void device_listener_unregister(DeviceListener *listener)
>  
>  static void device_realize(DeviceState *dev, Error **errp)
>  {
> -    DeviceClass *dc = DEVICE_GET_CLASS(dev);
> -
> -    if (dc->init) {
> -        int rc = dc->init(dev);
> -        if (rc < 0) {
> -            error_setg(errp, "Device initialization failed.");
> -            return;
> -        }
> -    }
>  }

If you are removing the code that makes DeviceClass::init work,
you could remove the struct field as well.  I suggest either
squashing patches 07/11 and 08/11 together, or moving this hunk
to patch 08/11.

>  
>  static void device_unrealize(DeviceState *dev, Error **errp)
> diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c
> index 04d6061f76..9edf43bc27 100644
> --- a/hw/core/sysbus.c
> +++ b/hw/core/sysbus.c
> @@ -18,6 +18,7 @@
>   */
>  
>  #include "qemu/osdep.h"
> +#include "qapi/error.h"
>  #include "hw/sysbus.h"
>  #include "monitor/monitor.h"
>  #include "exec/address-spaces.h"
> @@ -200,22 +201,19 @@ void sysbus_init_ioports(SysBusDevice *dev, uint32_t ioport, uint32_t size)
>      }
>  }
>  
> -/* TODO remove, once users are converted to realize */
> -static int sysbus_device_init(DeviceState *dev)
> +static void sysbus_realize(DeviceState *dev, Error **errp)
>  {
>      SysBusDevice *sd = SYS_BUS_DEVICE(dev);
>      SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
>  
> -    if (!sbc->init) {
> -        return 0;
> +    /* TODO remove, once users are converted to realize */
> +    if (sbc->init) {
> +        int rc = sbc->init(sd);
> +        if (rc < 0) {
> +            error_setg(errp, "Device initialization failed.");
> +            return;
> +        }
>      }
> -    return sbc->init(sd);
> -}
> -
> -static void sysbus_realize(DeviceState *dev, Error **errp)
> -{
> -    SysBusDevice *sd = SYS_BUS_DEVICE(dev);
> -    SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
>  
>      if (sbc->realize) {
>          sbc->realize(sd, errp);
> @@ -345,7 +343,6 @@ MemoryRegion *sysbus_address_space(SysBusDevice *dev)
>  static void sysbus_device_class_init(ObjectClass *klass, void *data)
>  {
>      DeviceClass *k = DEVICE_CLASS(klass);
> -    k->init = sysbus_device_init;
>      k->realize = sysbus_realize;
>      k->unrealize = sysbus_unrealize;
>      k->bus_type = TYPE_SYSTEM_BUS;
> -- 
> 2.15.1
> 
> 

-- 
Eduardo