]> git.proxmox.com Git - mirror_qemu.git/commitdiff
qom: make object_delete usable for statically-allocated objects
authorPaolo Bonzini <pbonzini@redhat.com>
Fri, 23 Nov 2012 08:47:14 +0000 (09:47 +0100)
committerAnthony Liguori <aliguori@us.ibm.com>
Mon, 26 Nov 2012 19:41:00 +0000 (13:41 -0600)
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>
include/qemu/object.h
qom/object.c

index 232463b3c7983e7561355ccc3ea6892738dcf008..5ddcb4aa5f33c84845ad2394ed70b3c1a541a18b 100644 (file)
@@ -238,6 +238,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:
  *
@@ -272,6 +280,7 @@ struct Object
 {
     /*< private >*/
     ObjectClass *class;
+    ObjectFree *free;
     QTAILQ_HEAD(, ObjectProperty) properties;
     uint32_t ref;
     Object *parent;
index 07495066d55a5473411de4f59ad18e21d5f1638a..3b502554505c0f4672a81a874319856b54f8da41 100644 (file)
@@ -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)