[PATCH 08/14] qdev: Add qdev_device_new_from_qdict()

Akihiko Odaki posted 14 patches 12 months ago
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Paolo Bonzini <pbonzini@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Eduardo Habkost <eduardo@habkost.net>, Akihiko Odaki <akihiko.odaki@daynix.com>, Sriram Yagnaraman <sriram.yagnaraman@est.tech>, Jason Wang <jasowang@redhat.com>, Keith Busch <kbusch@kernel.org>, Klaus Jensen <its@irrelevant.dk>, Alex Williamson <alex.williamson@redhat.com>, "Cédric Le Goater" <clg@redhat.com>
There is a newer version of this series
[PATCH 08/14] qdev: Add qdev_device_new_from_qdict()
Posted by Akihiko Odaki 12 months ago
qdev_device_new_from_qdict() can be used to create a device from QDict
without realizing it.

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
 include/monitor/qdev.h |  2 ++
 system/qdev-monitor.c  | 23 ++++++++++++++++++++---
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/include/monitor/qdev.h b/include/monitor/qdev.h
index 1d57bf6577..013108a7dd 100644
--- a/include/monitor/qdev.h
+++ b/include/monitor/qdev.h
@@ -11,6 +11,8 @@ int qdev_device_help(QemuOpts *opts);
 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp);
 DeviceState *qdev_device_add_from_qdict(const QDict *opts,
                                         bool from_json, Error **errp);
+DeviceState *qdev_device_new_from_qdict(const QDict *opts, bool from_json,
+                                        BusState **busp, Error **errp);
 
 /**
  * qdev_set_id: parent the device and set its id if provided.
diff --git a/system/qdev-monitor.c b/system/qdev-monitor.c
index 42aac94b8c..028b97f2b5 100644
--- a/system/qdev-monitor.c
+++ b/system/qdev-monitor.c
@@ -618,6 +618,25 @@ const char *qdev_set_id(DeviceState *dev, char *id, Error **errp)
 
 DeviceState *qdev_device_add_from_qdict(const QDict *opts,
                                         bool from_json, Error **errp)
+{
+    DeviceState *dev;
+    BusState *bus;
+
+    dev = qdev_device_new_from_qdict(opts, from_json, &bus, errp);
+    if (!dev) {
+        return NULL;
+    }
+
+    if (!qdev_realize(dev, bus, errp)) {
+        object_unparent(OBJECT(dev));
+        object_unref(OBJECT(dev));
+    }
+
+    return dev;
+}
+
+DeviceState *qdev_device_new_from_qdict(const QDict *opts, bool from_json,
+                                        BusState **busp, Error **errp)
 {
     ERRP_GUARD();
     DeviceClass *dc;
@@ -720,9 +739,7 @@ DeviceState *qdev_device_add_from_qdict(const QDict *opts,
         goto err_del_dev;
     }
 
-    if (!qdev_realize(dev, bus, errp)) {
-        goto err_del_dev;
-    }
+    *busp = bus;
     return dev;
 
 err_del_dev:

-- 
2.43.0