Received: from eggs.gnu.org ([208.118.235.92]:38619) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T5m5H-0004m5-6P for qemu-devel@nongnu.org; Sun, 26 Aug 2012 19:17:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T5m5G-0003ds-78 for qemu-devel@nongnu.org; Sun, 26 Aug 2012 19:17:35 -0400 Received: from cpe-70-123-140-180.austin.res.rr.com ([70.123.140.180]:50521 helo=localhost6.localdomain6) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T5m5F-0003cZ-Uz for qemu-devel@nongnu.org; Sun, 26 Aug 2012 19:17:34 -0400 From: Anthony Liguori Date: Sun, 26 Aug 2012 10:51:31 -0500 Message-Id: <1345996298-4892-3-git-send-email-aliguori@us.ibm.com> In-Reply-To: <1345996298-4892-1-git-send-email-aliguori@us.ibm.com> References: <1345996298-4892-1-git-send-email-aliguori@us.ibm.com> Subject: [Qemu-devel] [PATCH 2/9] object: automatically free objects based on a release function List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Anthony Liguori , Liu Ping Fan , Andreas Faerber Now object_delete() simply has the semantics of unref'ing an object and unparenting it. Signed-off-by: Anthony Liguori --- include/qemu/object.h | 5 +++++ qom/object.c | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 1 deletions(-) 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) -- 1.7.5.4