diff mbox

[2/9] object: automatically free objects based on a release function

Message ID 1345996298-4892-3-git-send-email-aliguori@us.ibm.com
State New
Headers show

Commit Message

Anthony Liguori Aug. 26, 2012, 3:51 p.m. UTC
Now object_delete() simply has the semantics of unref'ing an object and
unparenting it.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 include/qemu/object.h |    5 +++++
 qom/object.c          |   16 +++++++++++++++-
 2 files changed, 20 insertions(+), 1 deletions(-)

Comments

Andreas Färber Aug. 27, 2012, 1:31 p.m. UTC | #1
Am 26.08.2012 17:51, schrieb Anthony Liguori:
> Now object_delete() simply has the semantics of unref'ing an object and
> unparenting it.
> 
> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

Acked-by: Andreas Färber <afaerber@suse.de>

/-F
diff mbox

Patch

diff --git a/include/qemu/object.h b/include/qemu/object.h
index cc75fee..487adcd 100644
--- a/include/qemu/object.h
+++ b/include/qemu/object.h
@@ -242,6 +242,8 @@  struct ObjectClass
     GSList *interfaces;
 };
 
+typedef void (ObjectReleaseFunc)(Object *obj);
+
 /**
  * Object:
  *
@@ -264,6 +266,7 @@  struct Object
     QTAILQ_HEAD(, ObjectProperty) properties;
     uint32_t ref;
     Object *parent;
+    ObjectReleaseFunc *release;
 };
 
 /**
@@ -464,6 +467,8 @@  Object *object_new_with_type(Type type);
  */
 void object_delete(Object *obj);
 
+void object_set_release_func(Object *obj, ObjectReleaseFunc *func);
+
 /**
  * object_initialize_with_type:
  * @obj: A pointer to the memory to be used for the object.
diff --git a/qom/object.c b/qom/object.c
index e3e9242..44135c3 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -384,6 +384,20 @@  void object_finalize(void *data)
     object_property_del_all(obj);
 
     g_assert(obj->ref == 0);
+
+    if (obj->release) {
+        obj->release(obj);
+    }
+}
+
+void object_set_release_func(Object *obj, ObjectReleaseFunc *func)
+{
+    obj->release = func;
+}
+
+static void object_release_func(Object *obj)
+{
+    g_free(obj);
 }
 
 Object *object_new_with_type(Type type)
@@ -395,6 +409,7 @@  Object *object_new_with_type(Type type)
 
     obj = g_malloc(type->instance_size);
     object_initialize_with_type(obj, type);
+    object_set_release_func(obj, object_release_func);
     object_ref(obj);
 
     return obj;
@@ -412,7 +427,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)