qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Hani Benhabiles <kroosec@gmail.com>
To: qemu-devel@nongnu.org
Cc: stefanha@redhat.com, Hani Benhabiles <kroosec@gmail.com>,
	imammedo@redhat.com, pbonzini@redhat.com, afaerber@suse.de,
	aliguori@amazon.com
Subject: [Qemu-devel] [PATCH] qdev: Assign a default device ID when none is provided.
Date: Tue,  7 Jan 2014 20:07:19 +0100	[thread overview]
Message-ID: <1389121639-17657-1-git-send-email-kroosec@gmail.com> (raw)

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

             reply	other threads:[~2014-01-07 19:11 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-07 19:07 Hani Benhabiles [this message]
2014-01-08  7:36 ` [Qemu-devel] [PATCH] qdev: Assign a default device ID when none is provided 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1389121639-17657-1-git-send-email-kroosec@gmail.com \
    --to=kroosec@gmail.com \
    --cc=afaerber@suse.de \
    --cc=aliguori@amazon.com \
    --cc=imammedo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).