qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] qdev: Assign a default device ID when none is provided.
@ 2014-01-07 19:07 Hani Benhabiles
  2014-01-08  7:36 ` Markus Armbruster
  0 siblings, 1 reply; 9+ messages in thread
From: Hani Benhabiles @ 2014-01-07 19:07 UTC (permalink / raw)
  To: qemu-devel
  Cc: stefanha, Hani Benhabiles, imammedo, pbonzini, afaerber, aliguori

This would allow a user to be able to refer to the device when using commands
like device_del.

Signed-off-by: Hani Benhabiles <kroosec@gmail.com>
---
 qdev-monitor.c | 64 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 50 insertions(+), 14 deletions(-)

diff --git a/qdev-monitor.c b/qdev-monitor.c
index dc37a43..64b2b22 100644
--- a/qdev-monitor.c
+++ b/qdev-monitor.c
@@ -451,11 +451,54 @@ static BusState *qbus_find(const char *path)
     }
 }
 
+static int qbus_device_count(const BusState *bus, const char *typename)
+{
+    BusChild *kid;
+    int count = 0;
+
+    QTAILQ_FOREACH(kid, &bus->children, sibling) {
+        DeviceState *dev = kid->child;
+        BusState *dev_child;
+
+        if (!strcmp(typename, object_get_typename(OBJECT(dev)))) {
+            count++;
+        }
+
+        QLIST_FOREACH(dev_child, &dev->child_bus, sibling) {
+            count += qbus_device_count(dev_child, typename);
+        }
+    }
+
+    return count;
+}
+
+static gchar *assign_device_name(const char *typename, const DeviceState *dev)
+{
+    BusState *bus;
+    gchar *new_id;
+    int count = 0;
+
+    bus = sysbus_get_default();
+    count += qbus_device_count(bus, typename);
+
+    while (1) {
+        new_id = g_strdup_printf("%s.%d", typename, count - 1);
+        /* To not use an existing ID, if the user previously specified it. */
+        if (!qdev_find_recursive(sysbus_get_default(), new_id)) {
+            break;
+        }
+        count++;
+        g_free(new_id);
+    }
+
+    return new_id;
+}
+
 DeviceState *qdev_device_add(QemuOpts *opts)
 {
     ObjectClass *oc;
     DeviceClass *dc;
-    const char *driver, *path, *id;
+    const char *driver, *path;
     DeviceState *dev;
     BusState *bus = NULL;
     Error *err = NULL;
@@ -522,25 +565,18 @@ DeviceState *qdev_device_add(QemuOpts *opts)
         qdev_set_parent_bus(dev, bus);
     }
 
-    id = qemu_opts_id(opts);
-    if (id) {
-        dev->id = id;
+    dev->id = qemu_opts_id(opts);
+    if (!dev->id) {
+        qemu_opts_set_id(opts, assign_device_name(driver, dev));
+        dev->id = qemu_opts_id(opts);
     }
     if (qemu_opt_foreach(opts, set_property, dev, 1) != 0) {
         object_unparent(OBJECT(dev));
         object_unref(OBJECT(dev));
         return NULL;
     }
-    if (dev->id) {
-        object_property_add_child(qdev_get_peripheral(), dev->id,
-                                  OBJECT(dev), NULL);
-    } else {
-        static int anon_count;
-        gchar *name = g_strdup_printf("device[%d]", anon_count++);
-        object_property_add_child(qdev_get_peripheral_anon(), name,
-                                  OBJECT(dev), NULL);
-        g_free(name);
-    }
+    object_property_add_child(qdev_get_peripheral(), dev->id, OBJECT(dev),
+                              NULL);
     object_property_set_bool(OBJECT(dev), true, "realized", &err);
     if (err != NULL) {
         qerror_report_err(err);
-- 
1.8.3.2

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2014-01-10 11:54 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-01-07 19:07 [Qemu-devel] [PATCH] qdev: Assign a default device ID when none is provided Hani Benhabiles
2014-01-08  7:36 ` Markus Armbruster
2014-01-08 17:17   ` Hani Benhabiles
2014-01-08 17:34     ` Paolo Bonzini
2014-01-09 18:18       ` Hani Benhabiles
2014-01-09 18:33         ` Paolo Bonzini
2014-01-10  9:09           ` Markus Armbruster
2014-01-10  9:42             ` Andreas Färber
2014-01-10 11:54               ` Markus Armbruster

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).