From patchwork Fri Nov 23 08:47:14 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 201271 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9A8502C066F for ; Fri, 23 Nov 2012 20:06:12 +1100 (EST) Received: from localhost ([::1]:37306 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tbovu-0006Ql-JV for incoming@patchwork.ozlabs.org; Fri, 23 Nov 2012 03:48:22 -0500 Received: from eggs.gnu.org ([208.118.235.92]:54740) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tbov7-0004Ix-F1 for qemu-devel@nongnu.org; Fri, 23 Nov 2012 03:47:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tbov1-0002l9-9K for qemu-devel@nongnu.org; Fri, 23 Nov 2012 03:47:33 -0500 Received: from mail-wi0-f171.google.com ([209.85.212.171]:51929) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tbov1-0002jm-2c for qemu-devel@nongnu.org; Fri, 23 Nov 2012 03:47:27 -0500 Received: by mail-wi0-f171.google.com with SMTP id hn14so1064948wib.10 for ; Fri, 23 Nov 2012 00:47:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=7agwhFa2BomUNdC0FebJQHMxGV1KNrZmRthWQ6XAn4k=; b=QTe3ZSFLwSn0+X9rC1Wj8/sgCeaBq9W/vAHzeAnh6Ha1r1aZLJJ4taElapjDV0an0s Oo1vK0N3E8zLPAiKRLZoVMIz2dWLUd2U5bUW4O/cyJCbyP63LK+Ne+/9XtAeTjOzYuCG wzTg16FQZGxqg0BGeD8XIBdEyi9TC2BTc4yJmPokWT08q7xhw6Jka9y+DeKhB9FRy5Fb Wb5+BgseGys1JRqfWJS0cvOcD2fU9zVpHZQzJB3+XEYPOz7aMAk/Wr3WVQpnac6unx2Z IiArjcmh+gQAAU/z4fMR2yl8zZb2pxjn+Zt1QXZJsQN9Qv3f1yXbsulIijTuqRia4EM/ P1AQ== Received: by 10.180.14.162 with SMTP id q2mr8299866wic.21.1353660446643; Fri, 23 Nov 2012 00:47:26 -0800 (PST) Received: from yakj.lan (93-34-169-1.ip50.fastwebnet.it. [93.34.169.1]) by mx.google.com with ESMTPS id p3sm7581141wic.8.2012.11.23.00.47.25 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 23 Nov 2012 00:47:25 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 23 Nov 2012 09:47:14 +0100 Message-Id: <1353660436-8897-4-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.0 In-Reply-To: <1353660436-8897-1-git-send-email-pbonzini@redhat.com> References: <1353660436-8897-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.212.171 Cc: peter.maydell@linaro.org, aliguori@us.ibm.com, Liu Ping Fan Subject: [Qemu-devel] [PATCH 1.3 3/5] qom: make object_delete usable for statically-allocated objects X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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. Signed-off-by: Paolo Bonzini Reviewed-by: Andreas Färber --- Ping Fan, this is the patch I mentioned at http://permalink.gmane.org/gmane.comp.emulators.qemu/180054. With this patch, your call of object_unref for qdev_unplug_complete will not leak the device anymore (object_finalize will free it when the last reference is dropped with object_unref). include/qemu/object.h | 9 +++++++++ qom/object.c | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/qemu/object.h b/include/qemu/object.h index 232463b..5ddcb4a 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 f4747d0..f3e9517 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)