summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2012-11-23 09:47:14 +0100
committerAnthony Liguori <aliguori@us.ibm.com>2012-11-26 13:41:00 -0600
commitfde9bf4470d4a3b6ee1da0dee2370ab028b6314a (patch)
tree7bf9d19324d25310090bd09b28cb3683cadc2251
parent667d22d1ae59da46b4c1fbd094ca61145f19b8c3 (diff)
qom: make object_delete usable for statically-allocated objects
Store in the object the freeing function that will be used at deletion time. This makes it possible to use object_delete on statically-allocated (embedded) objects. Dually, it makes it possible to use object_unparent and object_unref without leaking memory, when the lifetime of object might extend until after the call to object_delete. Reviewed-by: Andreas Färber <afaerber@suse.de> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
-rw-r--r--include/qemu/object.h9
-rw-r--r--qom/object.c5
2 files changed, 13 insertions, 1 deletions
diff --git a/include/qemu/object.h b/include/qemu/object.h
index 232463b3c..5ddcb4aa5 100644
--- a/include/qemu/object.h
+++ b/include/qemu/object.h
@@ -239,6 +239,14 @@ typedef struct ObjectProperty
typedef void (ObjectUnparent)(Object *obj);
/**
+ * ObjectFree:
+ * @obj: the object being freed
+ *
+ * Called when an object's last reference is removed.
+ */
+typedef void (ObjectFree)(void *obj);
+
+/**
* ObjectClass:
*
* The base for all classes. The only thing that #ObjectClass contains is an
@@ -272,6 +280,7 @@ struct Object
{
/*< private >*/
ObjectClass *class;
+ ObjectFree *free;
QTAILQ_HEAD(, ObjectProperty) properties;
uint32_t ref;
Object *parent;
diff --git a/qom/object.c b/qom/object.c
index 07495066d..3b5025545 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -388,6 +388,9 @@ void object_finalize(void *data)
object_property_del_all(obj);
g_assert(obj->ref == 0);
+ if (obj->free) {
+ obj->free(obj);
+ }
}
Object *object_new_with_type(Type type)
@@ -399,6 +402,7 @@ Object *object_new_with_type(Type type)
obj = g_malloc(type->instance_size);
object_initialize_with_type(obj, type);
+ obj->free = g_free;
return obj;
}
@@ -415,7 +419,6 @@ void object_delete(Object *obj)
object_unparent(obj);
g_assert(obj->ref == 1);
object_unref(obj);
- g_free(obj);
}
Object *object_dynamic_cast(Object *obj, const char *typename)