@@ -408,7 +408,7 @@ void qdev_init_nofail(DeviceState *dev)
/*
* Realize @dev.
* @dev must not be plugged into a bus.
- * Plug @dev into @bus. This takes a reference to @dev.
+ * If @bus, plug @dev into @bus. This takes a reference to @dev.
* If @dev has no QOM parent, make one up, taking another reference.
* On success, return true.
* On failure, store an error through @errp and return false.
@@ -418,9 +418,12 @@ bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp)
Error *err = NULL;
assert(!dev->realized && !dev->parent_bus);
- assert(bus);
- qdev_set_parent_bus(dev, bus);
+ if (bus) {
+ qdev_set_parent_bus(dev, bus);
+ } else {
+ assert(!DEVICE_GET_CLASS(dev)->bus_type);
+ }
object_ref(OBJECT(dev));
object_property_set_bool(OBJECT(dev), true, "realized", &err);
@@ -442,7 +445,7 @@ void qdev_unrealize(DeviceState *dev)
* Realize @dev and drop a reference.
* This is like qdev_realize(), except it steals a reference rather
* than take one to plug @dev into @bus. On failure, it drops that
- * reference instead. Intended use:
+ * reference instead. @bus must not be null. Intended use:
* dev = qdev_new();
* [...]
* qdev_realize_and_unref(dev, bus, errp);
@@ -452,6 +455,8 @@ bool qdev_realize_and_unref(DeviceState *dev, BusState *bus, Error **errp)
{
bool ret;
+ assert(bus);
+
ret = qdev_realize(dev, bus, errp);
object_unref(OBJECT(dev));
return ret;
So far, qdev_realize() supports only devices that plug into a bus: argument @bus cannot be null. Extend it to support bus-less devices, too. qdev_realize_and_unref() remains restricted, because its reference counting would become rather confusing for bus-less devices. Signed-off-by: Markus Armbruster <armbru@redhat.com> --- hw/core/qdev.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)