It can be used in simpler cases where a read-only property is needed. Signed-off-by: Eduardo Habkost <ehabk...@redhat.com> --- include/qom/object.h | 23 +++++++++++++++++++++++ qom/object.c | 15 +++++++++++++++ 2 files changed, 38 insertions(+)
diff --git a/include/qom/object.h b/include/qom/object.h index d2d7748..78bb941 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -1123,6 +1123,8 @@ void object_property_add_child(Object *obj, const char *name, typedef enum { /* Unref the link pointer when the property is deleted */ OBJ_PROP_LINK_UNREF_ON_RELEASE = 0x1, + /* Free the child pointer passed to object_property_add_link() */ + OBJ_PROP_LINK_FREE_CHILD_POINTER = 0x2, } ObjectPropertyLinkFlags; /** @@ -1162,6 +1164,8 @@ void object_property_allow_set_link(Object *, const char *, * property is deleted with object_property_del(). If the * <code>@flags</code> <code>OBJ_PROP_LINK_UNREF_ON_RELEASE</code> bit is set, * the reference count is decremented when the property is deleted. + * If the <code>OBJ_PROP_LINK_FREE_CHILD_POINTER</code> bit is set, + * @child is freed when the property is deleted. */ void object_property_add_link(Object *obj, const char *name, const char *type, Object **child, @@ -1170,6 +1174,25 @@ void object_property_add_link(Object *obj, const char *name, ObjectPropertyLinkFlags flags, Error **errp); + +/** + * object_property_add_const_link: + * @obj: the object to add a property to + * @name: the name of the property + * @type: the qobj type of the link + * @child: pointer to the linked object + * @flags: additional options for the link + * @errp: if an error occurs, a pointer to an area to store the error + * + * Like object_property_add_link(), but adds a link property that will + * always point to the same object. + */ +void object_property_add_const_link(Object *obj, const char *name, + const char *type, Object *child, + ObjectPropertyLinkFlags flags, + Error **errp); + + /** * object_property_add_str: * @obj: the object to add a property to diff --git a/qom/object.c b/qom/object.c index b8dff43..7d4a46d 100644 --- a/qom/object.c +++ b/qom/object.c @@ -1227,6 +1227,9 @@ static void object_release_link_property(Object *obj, const char *name, if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) { object_unref(*prop->child); } + if (prop->flags & OBJ_PROP_LINK_FREE_CHILD_POINTER) { + g_free(prop->child); + } g_free(prop); } @@ -1266,6 +1269,18 @@ out: g_free(full_type); } +void object_property_add_const_link(Object *obj, const char *name, + const char *type, Object *child, + ObjectPropertyLinkFlags flags, + Error **errp) +{ + Object **childp = g_new0(Object*, 1); + + *childp = child; + object_property_add_link(obj, name, type, childp, NULL, + flags | OBJ_PROP_LINK_FREE_CHILD_POINTER, errp); +} + gchar *object_get_canonical_path_component(Object *obj) { ObjectProperty *prop = NULL; -- 2.1.0