From patchwork Sat Jun 16 10:39:03 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Peter A. G. Crosthwaite" X-Patchwork-Id: 165286 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 3F585B70FC for ; Sat, 16 Jun 2012 20:30:04 +1000 (EST) Received: from localhost ([::1]:34623 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SfqGX-0007eW-Ty for incoming@patchwork.ozlabs.org; Sat, 16 Jun 2012 06:30:01 -0400 Received: from eggs.gnu.org ([208.118.235.92]:38169) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SfqGM-0007VB-AC for qemu-devel@nongnu.org; Sat, 16 Jun 2012 06:29:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SfqGK-0005UL-6Z for qemu-devel@nongnu.org; Sat, 16 Jun 2012 06:29:49 -0400 Received: from mail-pz0-f45.google.com ([209.85.210.45]:36717) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SfqGK-0005T7-08 for qemu-devel@nongnu.org; Sat, 16 Jun 2012 06:29:48 -0400 Received: by mail-pz0-f45.google.com with SMTP id n2so4209466dad.4 for ; Sat, 16 Jun 2012 03:29:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :in-reply-to:references:x-gm-message-state; bh=jR2O3DpmkK5H27BbUHWfJZvDUKkC/mvMrKDiVWRFda0=; b=MB7Oh0KduBcG/ekHE5IbaK4CuAwT4rjwzKWMAYuwlfB2yMzJzsmyzAnW7D6fN1cJRl mdVseDe6dmESdVm2GGSuWJpkKEiSIvEuwj2SOJSJ7caQfS70wsc/1HeZaA4WVgPowrym MbRSWfhFT+9WdU2T+cosI0IMRCjp1iviJnaBPOrSGppP/FG9FD5XAkytRUIavYzVZ4C0 ZOW+Ei9+0SyMIpsQStOHT9npO+5hkS2w3CEjJrtH/I6gHCSC5dvS4LaMPuk4ObpUPOQ0 Xn+3L2SaMOyQwiTN0okFWW+e5idggswncwj550xqpJ8jXXx5WVngajdKwMSIQrZ4oYEk qZ6A== Received: by 10.68.217.166 with SMTP id oz6mr29381413pbc.136.1339842586972; Sat, 16 Jun 2012 03:29:46 -0700 (PDT) Received: from localhost ([124.148.20.9]) by mx.google.com with ESMTPS id pg3sm16444966pbc.2.2012.06.16.03.29.42 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 16 Jun 2012 03:29:46 -0700 (PDT) From: "Peter A. G. Crosthwaite" To: qemu-devel@nongnu.org, edgar.iglesias@gmail.com, aliguori@us.ibm.com, pbonzini@redhat.com Date: Sat, 16 Jun 2012 20:39:03 +1000 Message-Id: <8fe64df91fc46d552142d2cae5101654755627f5.1339843070.git.peter.crosthwaite@petalogix.com> X-Mailer: git-send-email 1.7.3.2 In-Reply-To: References: In-Reply-To: References: X-Gm-Message-State: ALoCoQk+bQ0hJDnzG2Y6F50xDrhLLD7CJLyjAUzxwf7r30x7aqe+jDnx2tcobMW5cZdW9AItBpId X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.210.45 Cc: peter.maydell@linaro.org, peter.crosthwaite@petalogix.com, paul@codesourcery.com, afaerber@suse.de, john.williams@petalogix.com, avi@redhat.com Subject: [Qemu-devel] [PATCH v1 3/3] qom: Converged dynamic cast for interfaces & objs 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 Interfaces and Object for some reason have seperate code paths for dynamic casting. AFAICT, Anthonys new interface specific cast is a functional superset of the object cast, so this patch renames the interface cast to object cast and blows away the original object cast. Anyone want to shed any light on why the casts need to be different? The problem (and the reason for this patch) is its not always possible for user of interface to control how their object are going to be casted. Where this broke down big-time is with links. The link setter code path explictly uses object_dynamic_cast(), when link are set, which pretty much makes linking to interfaces impossible. So either: 1: This patch 2: Explictly test objects/classes for interfacage in the link setting code 3: Ban linking to interfaces Signed-off-by: Peter A. G. Crosthwaite --- include/qemu/object.h | 20 +------------------- qom/object.c | 49 +++++++++---------------------------------------- 2 files changed, 10 insertions(+), 59 deletions(-) diff --git a/include/qemu/object.h b/include/qemu/object.h index 72cb290..a8916ce 100644 --- a/include/qemu/object.h +++ b/include/qemu/object.h @@ -425,7 +425,7 @@ struct InterfaceClass * Returns: @obj casted to @interface if cast is valid, otherwise raise error. */ #define INTERFACE_CHECK(interface, obj, name) \ - ((interface *)interface_dynamic_cast_assert(OBJECT((obj)), (name))) + ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name))) /** * object_new: @@ -564,24 +564,6 @@ ObjectClass *object_class_dynamic_cast(ObjectClass *klass, const char *typename); /** - * interface_dynamic_cast_assert: - * @obj: the object to cast to an interface type - * @typename: the type name of the interface - * - * Returns: @obj if @obj implements @typename, otherwise raise an error - */ -Object *interface_dynamic_cast_assert(Object *obj, const char *typename); - -/** - * interface_dynamic_cast_assert: - * @obj: the object to cast to an interface type - * @typename: the type name of the interface - * - * Returns: @obj if @obj implements @typename, otherwise %NULL - */ -Object *interface_dynamic_cast(Object *obj, const char *typename); - -/** * object_class_get_name: * @klass: The class to obtain the QOM typename for. * diff --git a/qom/object.c b/qom/object.c index aa26693..7574666 100644 --- a/qom/object.c +++ b/qom/object.c @@ -405,24 +405,6 @@ static bool object_is_type(Object *obj, TypeImpl *target_type) return !target_type || type_is_ancestor(obj->class->type, target_type); } -Object *object_dynamic_cast(Object *obj, const char *typename) -{ - TypeImpl *target_type = type_get_by_name(typename); - - /* Check if typename is a direct ancestor. Special-case TYPE_OBJECT, - * we want to go back from interfaces to the parent. - */ - if (target_type && object_is_type(obj, target_type)) { - return obj; - } - - if (!target_type) { - return obj; - } - - return NULL; -} - static void register_types(void) { static TypeInfo interface_info = { @@ -436,6 +418,15 @@ static void register_types(void) type_init(register_types) +Object *object_dynamic_cast(Object *obj, const char *typename) +{ + if (object_class_dynamic_cast(object_get_class(obj), typename)) { + return obj; + } + + return NULL; +} + Object *object_dynamic_cast_assert(Object *obj, const char *typename) { Object *inst; @@ -496,28 +487,6 @@ ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, return ret; } -Object *interface_dynamic_cast(Object *obj, const char *typename) -{ - if (object_class_dynamic_cast(object_get_class(obj), typename)) { - return obj; - } - - return NULL; -} - -Object *interface_dynamic_cast_assert(Object *obj, const char *typename) -{ - Object *ret = interface_dynamic_cast(obj, typename); - - if (!ret) { - fprintf(stderr, "Object %p is not an instance of type %s\n", - obj, typename); - abort(); - } - - return ret; -} - const char *object_get_typename(Object *obj) { return obj->class->type->name;