From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43879) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNgQ6-0005aY-L9 for qemu-devel@nongnu.org; Wed, 12 Mar 2014 06:29:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WNgQ1-0003Px-M7 for qemu-devel@nongnu.org; Wed, 12 Mar 2014 06:29:54 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50490) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WNgQ1-0003Ps-EW for qemu-devel@nongnu.org; Wed, 12 Mar 2014 06:29:49 -0400 From: Stefan Hajnoczi Date: Wed, 12 Mar 2014 11:29:15 +0100 Message-Id: <1394620172-25071-8-git-send-email-stefanha@redhat.com> In-Reply-To: <1394620172-25071-1-git-send-email-stefanha@redhat.com> References: <1394620172-25071-1-git-send-email-stefanha@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 07/24] object: add object_get_canonical_path_component() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Peter Maydell , Anthony Liguori It is often useful to find an object's child property name. Also use this new function to simplify the implementation of object_get_canonical_path(). Reviewed-by: Andreas F=C3=A4rber Signed-off-by: Stefan Hajnoczi --- include/qom/object.h | 8 ++++++++ qom/object.c | 54 +++++++++++++++++++++++++++++++---------------= ------ 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/include/qom/object.h b/include/qom/object.h index 9c7c361..4cd7704 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -974,6 +974,14 @@ const char *object_property_get_type(Object *obj, co= nst char *name, Object *object_get_root(void); =20 /** + * object_get_canonical_path_component: + * + * Returns: The final component in the object's canonical path. The can= onical + * path is the path within the composition tree starting from the root. + */ +gchar *object_get_canonical_path_component(Object *obj); + +/** * object_get_canonical_path: * * Returns: The canonical path for a object. This is the path within th= e diff --git a/qom/object.c b/qom/object.c index 660859c..3290375 100644 --- a/qom/object.c +++ b/qom/object.c @@ -1102,39 +1102,49 @@ void object_property_add_link(Object *obj, const = char *name, g_free(full_type); } =20 +gchar *object_get_canonical_path_component(Object *obj) +{ + ObjectProperty *prop =3D NULL; + + g_assert(obj); + g_assert(obj->parent !=3D NULL); + + QTAILQ_FOREACH(prop, &obj->parent->properties, node) { + if (!object_property_is_child(prop)) { + continue; + } + + if (prop->opaque =3D=3D obj) { + return g_strdup(prop->name); + } + } + + /* obj had a parent but was not a child, should never happen */ + g_assert_not_reached(); + return NULL; +} + gchar *object_get_canonical_path(Object *obj) { Object *root =3D object_get_root(); - char *newpath =3D NULL, *path =3D NULL; + char *newpath, *path =3D NULL; =20 while (obj !=3D root) { - ObjectProperty *prop =3D NULL; - - g_assert(obj->parent !=3D NULL); - - QTAILQ_FOREACH(prop, &obj->parent->properties, node) { - if (!object_property_is_child(prop)) { - continue; - } + char *component =3D object_get_canonical_path_component(obj); =20 - if (prop->opaque =3D=3D obj) { - if (path) { - newpath =3D g_strdup_printf("%s/%s", prop->name, pat= h); - g_free(path); - path =3D newpath; - } else { - path =3D g_strdup(prop->name); - } - break; - } + if (path) { + newpath =3D g_strdup_printf("%s/%s", component, path); + g_free(component); + g_free(path); + path =3D newpath; + } else { + path =3D component; } =20 - g_assert(prop !=3D NULL); - obj =3D obj->parent; } =20 - newpath =3D g_strdup_printf("/%s", path); + newpath =3D g_strdup_printf("/%s", path ? path : ""); g_free(path); =20 return newpath; --=20 1.8.5.3