From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:52512) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TgLpz-0008Th-SD for qemu-devel@nongnu.org; Wed, 05 Dec 2012 15:45:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TgLpy-0001Cm-Jc for qemu-devel@nongnu.org; Wed, 05 Dec 2012 15:44:59 -0500 Received: from mail-ee0-f45.google.com ([74.125.83.45]:57746) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TgLpy-0001AN-DA for qemu-devel@nongnu.org; Wed, 05 Dec 2012 15:44:58 -0500 Received: by mail-ee0-f45.google.com with SMTP id d49so3380495eek.4 for ; Wed, 05 Dec 2012 12:44:58 -0800 (PST) Sender: Paolo Bonzini From: Paolo Bonzini Date: Wed, 5 Dec 2012 21:44:36 +0100 Message-Id: <1354740282-20679-6-git-send-email-pbonzini@redhat.com> In-Reply-To: <1354740282-20679-1-git-send-email-pbonzini@redhat.com> References: <1354740282-20679-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PATCH 05/11] qdev: move deletion of children from finalize to unparent List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: aliguori@us.ibm.com, afaerber@suse.de A device will never be finalized as long as it has a reference from other devices that sit on its buses. To ensure that the references go away, deassociate a bus from its children in the unparent callback for the bus. Signed-off-by: Paolo Bonzini --- hw/qdev.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/hw/qdev.c b/hw/qdev.c index 87dfcb5..12b1529 100644 --- a/hw/qdev.c +++ b/hw/qdev.c @@ -446,6 +446,25 @@ void qbus_init(BusState *bus, DeviceState *parent, const char *name) } } +static void qbus_remove_children(Object *obj) +{ + BusState *bus = BUS(obj); + BusChild *kid; + + while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) { + DeviceState *dev = kid->child; + qdev_free(dev); + } + if (bus->parent) { + QLIST_REMOVE(bus, sibling); + bus->parent->num_child_bus--; + bus->parent = NULL; + } else { + assert(bus != sysbus_get_default()); /* main_system_bus is never freed */ + qemu_unregister_reset(qbus_reset_all_fn, bus); + } +} + void qbus_create_inplace(BusState *bus, const char *typename, DeviceState *parent, const char *name) { @@ -761,22 +780,15 @@ static void qbus_initfn(Object *obj) QTAILQ_INIT(&bus->children); } +static void bus_class_init(ObjectClass *class, void *data) +{ + class->unparent = qbus_remove_children; +} + static void qbus_finalize(Object *obj) { BusState *bus = BUS(obj); - BusChild *kid; - while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) { - DeviceState *dev = kid->child; - qdev_free(dev); - } - if (bus->parent) { - QLIST_REMOVE(bus, sibling); - bus->parent->num_child_bus--; - } else { - assert(bus != sysbus_get_default()); /* main_system_bus is never freed */ - qemu_unregister_reset(qbus_reset_all_fn, bus); - } g_free((char *)bus->name); } @@ -788,6 +800,7 @@ static const TypeInfo bus_info = { .class_size = sizeof(BusClass), .instance_init = qbus_initfn, .instance_finalize = qbus_finalize, + .class_init = bus_class_init, }; static void qdev_register_types(void) -- 1.8.0.1