From patchwork Fri Dec 2 20:20:39 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 128954 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1E03DB6F64 for ; Sat, 3 Dec 2011 07:21:21 +1100 (EST) Received: from localhost ([::1]:35739 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RWZbh-0000Rv-Hy for incoming@patchwork.ozlabs.org; Fri, 02 Dec 2011 15:21:17 -0500 Received: from eggs.gnu.org ([140.186.70.92]:60997) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RWZbc-0000QG-8F for qemu-devel@nongnu.org; Fri, 02 Dec 2011 15:21:13 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RWZbb-0007hu-CG for qemu-devel@nongnu.org; Fri, 02 Dec 2011 15:21:12 -0500 Received: from cpe-70-123-132-139.austin.res.rr.com ([70.123.132.139]:49905 helo=localhost6.localdomain6) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RWZba-0007ho-Uz for qemu-devel@nongnu.org; Fri, 02 Dec 2011 15:21:11 -0500 Received: from localhost6.localdomain6 (localhost.localdomain [127.0.0.1]) by localhost6.localdomain6 (8.14.4/8.14.4/Debian-2ubuntu1) with ESMTP id pB2KL4tP014997; Fri, 2 Dec 2011 14:21:04 -0600 Received: (from anthony@localhost) by localhost6.localdomain6 (8.14.4/8.14.4/Submit) id pB2KL1RX014996; Fri, 2 Dec 2011 14:21:01 -0600 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Fri, 2 Dec 2011 14:20:39 -0600 Message-Id: <1322857256-14951-2-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1322857256-14951-1-git-send-email-aliguori@us.ibm.com> References: <1322857256-14951-1-git-send-email-aliguori@us.ibm.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 70.123.132.139 Cc: Kevin Wolf , Peter Maydell , Anthony Liguori , Stefan Hajnoczi , Jan Kiszka , Markus Armbruster , Luiz Capitulino , Gerd Hoffman Subject: [Qemu-devel] [PATCH v2 01/18] qom: add a reference count to qdev 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 To ensure that a device isn't removed from the graph until all of its links are broken. Signed-off-by: Anthony Liguori --- hw/qdev.c | 16 ++++++++++++++++ hw/qdev.h | 26 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 0 deletions(-) diff --git a/hw/qdev.c b/hw/qdev.c index 106407f..fdc9843 100644 --- a/hw/qdev.c +++ b/hw/qdev.c @@ -323,6 +323,11 @@ int qdev_unplug(DeviceState *dev) } assert(dev->info->unplug != NULL); + if (dev->ref != 0) { + qerror_report(QERR_DEVICE_IN_USE, dev->id?:""); + return -1; + } + qdev_hot_removed = true; return dev->info->unplug(dev); @@ -962,3 +967,14 @@ char* qdev_get_fw_dev_path(DeviceState *dev) return strdup(path); } + +void qdev_ref(DeviceState *dev) +{ + dev->ref++; +} + +void qdev_unref(DeviceState *dev) +{ + g_assert(dev->ref > 0); + dev->ref--; +} diff --git a/hw/qdev.h b/hw/qdev.h index 36a4198..2397b4e 100644 --- a/hw/qdev.h +++ b/hw/qdev.h @@ -45,6 +45,12 @@ struct DeviceState { QTAILQ_ENTRY(DeviceState) sibling; int instance_id_alias; int alias_required_for_version; + + /** + * This tracks the number of references between devices. See @qdev_ref for + * more information. + */ + uint32_t ref; }; typedef void (*bus_dev_printfn)(Monitor *mon, DeviceState *dev, int indent); @@ -329,4 +335,24 @@ char *qdev_get_fw_dev_path(DeviceState *dev); /* This is a nasty hack to allow passing a NULL bus to qdev_create. */ extern struct BusInfo system_bus_info; +/** + * @qdev_ref + * + * Increase the reference count of a device. A device cannot be freed as long + * as its reference count is greater than zero. + * + * @dev - the device + */ +void qdev_ref(DeviceState *dev); + +/** + * @qdef_unref + * + * Decrease the reference count of a device. A device cannot be freed as long + * as its reference count is greater than zero. + * + * @dev - the device + */ +void qdev_unref(DeviceState *dev); + #endif