From patchwork Mon Jan 9 11:20:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 640603 Delivered-To: patch@linaro.org Received: by 2002:a17:522:f3c4:b0:4b4:3859:abed with SMTP id in4csp2077787pvb; Mon, 9 Jan 2023 03:22:46 -0800 (PST) X-Google-Smtp-Source: AMrXdXs+r23uzQbMe/6P7g6lwn4ddf7ZEzULQF3iUb1XRbPzF1qnHAJ2jJRZRkySe5mYwhxiW0Bu X-Received: by 2002:ad4:41ce:0:b0:531:bea4:a9f1 with SMTP id a14-20020ad441ce000000b00531bea4a9f1mr40367146qvq.36.1673263366367; Mon, 09 Jan 2023 03:22:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673263366; cv=none; d=google.com; s=arc-20160816; b=swXzO7D1qZmIb9Dj3FiY3hiu6rlpZcBsRCrnJeEp3fjng09J2x+rISS9le8GQVhhUC rNJW1cCaLME7k0Ag+VIKdsaBT1ofOXw1udob4mZVdjZRIsKy8dN2+SDYdUIpIizxwYns 0SBgDQ+IedHXYovHLAK/Nff5ekdyfZQcblRX0FQplfF/FqTRHqkztjAz0cpwt6N9Dnv+ URPPrLViTFe2kedjvoGZlAVPOoBUurf+qNd43GuXdIpGGuFI4zPaNasr8urNYCeNk3yq exeib54xnesQDZJlkyJM7N56MW2gpi7e6Rbnlah9Ggq+YF1lMSwMFgJpg670qbzq+21P Qv1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=HjztAYhnpGeZaH9bHabVbRF7j7Z+gMhwa5mewUvtJyA=; b=bjMtphtsWkFkBbFg6Uy8qbTrUXlv/i9n3hctzYgbMsNj2iMwUw5U0OF4MYhrWuItyA AVryslQgrvOor+MtwUZvJy6lciFROBOTiVM2AV6/Wr1CRgA95VcoEC6nAXg171dnNRxV C1kMVeW4+8+RlSLShH/JyL++Cq0X4UxW5Si0PhX0pzTy8JXQPAmv3XVwMjwTZkoqHQQV QR/E9OW5eX0u+vwBqaV5kQGmUSn7sA7W4dw9wwM55mYfT+2hPKNZhtQEfzM67fUPXgWm gVCCxRYRfyWOFJLUg5fXdNHRTuV6RxjILBInQMosFo1uVzhgXmrbkXIWYaNyAcSDH+zo r0rw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Iy2VCGYU; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id hf8-20020a0562140e8800b00531bc37143asi4301893qvb.419.2023.01.09.03.22.46 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 09 Jan 2023 03:22:46 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Iy2VCGYU; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pEqDD-0006Te-BM; Mon, 09 Jan 2023 06:21:07 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pEqD8-0006Qb-Km for qemu-devel@nongnu.org; Mon, 09 Jan 2023 06:21:02 -0500 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pEqD6-0001Be-K3 for qemu-devel@nongnu.org; Mon, 09 Jan 2023 06:21:02 -0500 Received: by mail-wm1-x336.google.com with SMTP id ay12-20020a05600c1e0c00b003d9ea12bafcso3328637wmb.3 for ; Mon, 09 Jan 2023 03:21:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=HjztAYhnpGeZaH9bHabVbRF7j7Z+gMhwa5mewUvtJyA=; b=Iy2VCGYUkhHBuxlW2PjsE51x0C3kayMvxrP/vzrsMoCpS+wVH/iWdw48n1XehMtjr4 8/nb3A/IQpazu5lbhbjl8dUrxJVHK9BkU8pxVQM3ESaI2ngAmJOkKOWA0tDqnQh+JqzS rXlmQb/P2XHTsH7sTX2sW2fnrJVx+wn/HFHn/FbIuKpE/JP4/GQOrOm9cD9gAUhbBAvh Rq88Sw8QGIYf+EzMiL9csuPShILVefZvt0lyhn5/yNGMmxeVfdxX1G4oIKua1zovv38d cFYaTdylVPiAi7p0O6/7zgY8k3C3QTyPf1193UvMxyRH7wj9hN9I7eml8hyw5i8KcnJC ehiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=HjztAYhnpGeZaH9bHabVbRF7j7Z+gMhwa5mewUvtJyA=; b=tHtTAHQnKPqxGYoQlDHTOGgi99PsUqUE66VBaKc6g8sYP4xisZciS3b3mH1b8NFdGa s2aSitoXVUXRU3+yvI4LjvrslmujNlrKzS9KXOUQ83izv/RPewAC8y4qyjfFuybTqxvh QCFG2fhuUNnblpriUf1nRGateYFPLpk6JofgIFZ8Ef8vAlE4ZhdK+xdh7n1DLjeJzZCj wc9l7VpuXD2OfDp4oS96uKFO+SmJo1zSDBGOBEFjVWD2Ob1Siv0AEMCHB1WKvvFkoXTZ nO/+zxePvJHOPEHY7hY9uVIg/gSwDGALS1dKe7aGrD8HLFl2CNId2+mv1qoz/ckMbZm2 OcIg== X-Gm-Message-State: AFqh2ko6USCStC5iReN/B2D6Kb+z23mlyGHqTb9DT4e/sAlaTYypVbYN Pc1e88iClgnk2IbggqzdKHzZyn7XgrU6w2sq X-Received: by 2002:a1c:4c12:0:b0:3c6:e63e:89a6 with SMTP id z18-20020a1c4c12000000b003c6e63e89a6mr45934917wmf.2.1673263258353; Mon, 09 Jan 2023 03:20:58 -0800 (PST) Received: from localhost.localdomain ([81.0.6.76]) by smtp.gmail.com with ESMTPSA id l6-20020a5d6686000000b002bb3229a03csm7298328wru.34.2023.01.09.03.20.57 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 09 Jan 2023 03:20:57 -0800 (PST) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Markus Armbruster , Claudio Fontana , Eduardo Habkost , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Gerd Hoffmann , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [RFC PATCH] qom: Extract object_try_new() from qdev_new() Date: Mon, 9 Jan 2023 12:20:56 +0100 Message-Id: <20230109112056.94385-1-philmd@linaro.org> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::336; envelope-from=philmd@linaro.org; helo=mail-wm1-x336.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org The module resolving in qdev_new() is specific to QOM, not to QDev. Extract the code as a new QOM object_try_new() helper so it can be reused by non-QDev code. Signed-off-by: Philippe Mathieu-Daudé --- RFC because I'm wonder if we can't find a better name... Also, should we refactor object_initialize() similarly, having object_try_initialize(..., Error *)? --- hw/core/qdev.c | 23 ++--------------------- include/qom/object.h | 12 ++++++++++++ qom/object.c | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index d759c4602c..3a076dcc7f 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -147,31 +147,12 @@ bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp) DeviceState *qdev_new(const char *name) { - ObjectClass *oc = object_class_by_name(name); -#ifdef CONFIG_MODULES - if (!oc) { - int rv = module_load_qom(name, &error_fatal); - if (rv > 0) { - oc = object_class_by_name(name); - } else { - error_report("could not find a module for type '%s'", name); - exit(1); - } - } -#endif - if (!oc) { - error_report("unknown type '%s'", name); - abort(); - } - return DEVICE(object_new(name)); + return DEVICE(object_try_new(name, &error_fatal)); } DeviceState *qdev_try_new(const char *name) { - if (!module_object_class_by_name(name)) { - return NULL; - } - return DEVICE(object_new(name)); + return DEVICE(object_try_new(name, NULL)); } static QTAILQ_HEAD(, DeviceListener) device_listeners diff --git a/include/qom/object.h b/include/qom/object.h index ef7258a5e1..9cc5bf30ec 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -565,6 +565,18 @@ Object *object_new_with_class(ObjectClass *klass); */ Object *object_new(const char *typename); +/** + * object_try_new: Try to create an object on the heap + * @name: The name of the type of the object to instantiate. + * @errp: pointer to Error object. + * + * This is like object_new(), except it returns %NULL when type @name + * does not exist, rather than asserting. + * + * Returns: The newly allocated and instantiated object, or %NULL. + */ +Object *object_try_new(const char *name, Error **errp); + /** * object_new_with_props: * @typename: The name of the type of the object to instantiate. diff --git a/qom/object.c b/qom/object.c index e25f1e96db..6d3faaeb6e 100644 --- a/qom/object.c +++ b/qom/object.c @@ -755,6 +755,29 @@ Object *object_new(const char *typename) } +Object *object_try_new(const char *name, Error **errp) +{ + TypeImpl *ti = type_get_by_name(name); + + if (!ti) { +#ifdef CONFIG_MODULES + int rv = module_load_qom(name, errp); + if (rv <= 0) { + error_report("could not find a module for type '%s'", name); + exit(1); + } + ti = type_get_by_name(name); +#endif + } + if (!ti) { + error_setg(errp, "unknown type '%s'", name); + return NULL; + } + + return object_new_with_type(ti); +} + + Object *object_new_with_props(const char *typename, Object *parent, const char *id,