qom: optimize qdev_get_canonical_path using a parent link
The full tree search was a bit unreasonable.
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
diff --git a/hw/qdev.c b/hw/qdev.c
index 4d491f1..4004860 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -1229,6 +1229,8 @@
NULL, NULL, child, errp);
qdev_ref(child);
+ g_assert(child->parent == NULL);
+ child->parent = dev;
g_free(type);
}
@@ -1307,47 +1309,37 @@
g_free(full_type);
}
-static gchar *qdev_get_path_in(DeviceState *parent, DeviceState *dev)
-{
- DeviceProperty *prop;
-
- if (parent == dev) {
- return g_strdup("");
- }
-
- QTAILQ_FOREACH(prop, &parent->properties, node) {
- gchar *subpath;
-
- if (!strstart(prop->type, "child<", NULL)) {
- continue;
- }
-
- /* Check to see if the device is one of parent's children */
- if (prop->opaque == dev) {
- return g_strdup(prop->name);
- }
-
- /* Check to see if the device is a child of our child */
- subpath = qdev_get_path_in(prop->opaque, dev);
- if (subpath) {
- gchar *path;
-
- path = g_strdup_printf("%s/%s", prop->name, subpath);
- g_free(subpath);
-
- return path;
- }
- }
-
- return NULL;
-}
-
gchar *qdev_get_canonical_path(DeviceState *dev)
{
- gchar *path, *newpath;
+ DeviceState *root = qdev_get_root();
+ char *newpath = NULL, *path = NULL;
- path = qdev_get_path_in(qdev_get_root(), dev);
- g_assert(path != NULL);
+ while (dev != root) {
+ DeviceProperty *prop = NULL;
+
+ g_assert(dev->parent != NULL);
+
+ QTAILQ_FOREACH(prop, &dev->parent->properties, node) {
+ if (!strstart(prop->type, "child<", NULL)) {
+ continue;
+ }
+
+ if (prop->opaque == dev) {
+ if (path) {
+ newpath = g_strdup_printf("%s/%s", prop->name, path);
+ g_free(path);
+ path = newpath;
+ } else {
+ path = g_strdup(prop->name);
+ }
+ break;
+ }
+ }
+
+ g_assert(prop != NULL);
+
+ dev = dev->parent;
+ }
newpath = g_strdup_printf("/%s", path);
g_free(path);