diff mbox series

[2/9] qdev: Add qdev_create_simple() helper

Message ID 20230109092617.80224-3-philmd@linaro.org (mailing list archive)
State New, archived
Headers show
Series hw: Convert simple devices from SysBus to QDev | expand

Commit Message

Philippe Mathieu-Daudé Jan. 9, 2023, 9:26 a.m. UTC
Add a helper to easily create a qdev with no (or default) property.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/core/qdev.c         |  9 +++++++++
 include/hw/qdev-core.h | 11 +++++++++++
 2 files changed, 20 insertions(+)
diff mbox series

Patch

diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index d759c4602c..4afed2a24c 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -174,6 +174,15 @@  DeviceState *qdev_try_new(const char *name)
     return DEVICE(object_new(name));
 }
 
+DeviceState *qdev_create_simple(const char *name, Error **errp)
+{
+    DeviceState *dev = qdev_new(name);
+
+    qdev_realize_and_unref(dev, NULL, errp);
+
+    return dev;
+}
+
 static QTAILQ_HEAD(, DeviceListener) device_listeners
     = QTAILQ_HEAD_INITIALIZER(device_listeners);
 
diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index 35fddb19a6..ca6b2b8458 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -397,6 +397,17 @@  bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp);
  * the life of the simulation and should not be unrealized and freed.
  */
 void qdev_unrealize(DeviceState *dev);
+
+/**
+ * Create and realize a device on the heap.
+ * @name: device type to create (we assert() that this type exists)
+ * @errp: pointer to error object
+ *
+ * Create the device state structure, initialize it, and drop the
+ * reference to it (the device is realized).
+ */
+DeviceState *qdev_create_simple(const char *name, Error **errp);
+
 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
                                  int required_for_version);
 HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev);