Create the devices by their DeviceCreationPriority order instead
of the input order, however devices with the same priority will
be created in the same order as before.

Signed-off-by: Marcel Apfelbaum <mar...@redhat.com>
---
 vl.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 46 insertions(+), 9 deletions(-)

diff --git a/vl.c b/vl.c
index 3629336..1c650b9 100644
--- a/vl.c
+++ b/vl.c
@@ -2341,20 +2341,58 @@ static int device_help_func(void *opaque, QemuOpts 
*opts, Error **errp)
     return qdev_device_help(opts);
 }
 
-static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
+static int device_insert_by_priority_func(void *opaque, QemuOpts *opts,
+                                          Error **errp)
 {
-    Error *err = NULL;
-    DeviceState *dev;
+    GList **dev_list = opaque;
+    GList *l;
+    int dev_prio = qdev_device_get_priority(opts, errp);
 
-    dev = qdev_device_add(opts, &err);
-    if (!dev) {
-        error_report_err(err);
+    if (dev_prio < 0) {
         return -1;
     }
-    object_unref(OBJECT(dev));
+
+    for (l = *dev_list; l != NULL; l = g_list_next(l)) {
+        int prio = qdev_device_get_priority((QemuOpts *)l->data, errp);
+        if (prio >= dev_prio) {
+            continue;
+        }
+        break;
+    }
+
+    *dev_list = g_list_insert_before(*dev_list, l, opts);
+
     return 0;
 }
 
+static int devices_init(void)
+{
+    GList *dev_list, *l;
+    Error *err = NULL;
+
+    if (qemu_opts_foreach(qemu_find_opts("device"),
+                          device_insert_by_priority_func, &dev_list, &err)) {
+        goto out_err;
+    }
+
+    for (l = dev_list; l != NULL; l = g_list_next(l)) {
+        DeviceState *dev = qdev_device_add((QemuOpts *)l->data, &err);
+        if (!dev) {
+            goto out_err;
+        }
+
+        object_unref(OBJECT(dev));
+    }
+
+    g_list_free(dev_list);
+    return 0;
+
+out_err:
+    g_list_free(dev_list);
+    error_report_err(err);
+    return -1;
+}
+
 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
 {
     Error *local_err = NULL;
@@ -4535,8 +4573,7 @@ int main(int argc, char **argv, char **envp)
     igd_gfx_passthru();
 
     /* init generic devices */
-    if (qemu_opts_foreach(qemu_find_opts("device"),
-                          device_init_func, NULL, NULL)) {
+    if (devices_init()) {
         exit(1);
     }
 
-- 
2.4.3


Reply via email to