From patchwork Wed Dec 18 13:42:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 851681 Delivered-To: patch@linaro.org Received: by 2002:a5d:4888:0:b0:385:e875:8a9e with SMTP id g8csp922230wrq; Wed, 18 Dec 2024 05:44:13 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXyKTTMMsLRmr6Z27gnN0LLlQ4RGLKEAIKSuP7kQHLSKaGCTO/D+UYwsaWxo8XrsFmx4zBAmA==@linaro.org X-Google-Smtp-Source: AGHT+IH9RBJ8vYd90UfIgypLAWqPf/HfsxpUoyZh4reo7qtarW8tRV7rltzcFjweeMWH8KfEpAgi X-Received: by 2002:a05:620a:450d:b0:7b6:6765:4ca6 with SMTP id af79cd13be357-7b86375acb5mr287882585a.34.1734529453175; Wed, 18 Dec 2024 05:44:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1734529453; cv=none; d=google.com; s=arc-20240605; b=JHtsZFHlymBxkWLNUiM83sfQ+7WUG2GQyYYAz0pSY6T0eGyY397Wz/YiEYTBE0nUBT GzaK2afcbOIKnKihhwgKDgfqJn4K7CKRBHnk5qC/9pHClsRRGOJRdRzISIquWzzKWzRI axMBr27SCg9hvfs+eXL1Pdc2+MKgyRlEjH1W80NbdeyzI88gsC8UM2i5N3/AlTgN3ltE 9jxLHf279LJ/i2O4fDKUqtq8Xtfye5RRDGY4AlVoYHGIusvUWbXjwbn9jfv1t8s/wq1L kaIMo5kmRoUOWuBvNwq5K8FcQSXKf5WmV+RxtypxI2D/C4a0/kndtzvVcxmDPQlL5WYr pCGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=V9uuK3Bi0zd4Ff4jmnl20vwDce29dx6gKCtTc6/W2eI=; fh=3mEp7FusppN5Drq2PN08R8D/9xXM9YdbtG5TkJxJBiE=; b=KIIf/i09ziyzSE/7mVnmDOueGrXkyiEEjXIbWo46pvHy+A9QGthSeuMWircimgeTF+ VwkryQG4Dv3nJo8uZv5SLSe81juIRSKGAG55u/9udcIRHdmY8NyRFfbDeGXVq+YHAFod UeEMfghwa3771aSlj78godYjR534GUbAYYQuFoE9Ot8KU2WSIyjrBj2hspFOw9Q10TL2 CkUNmC38lWW0kwJ3rRGvwejSHfX27csVvlrbq+WBBYNGoScJwznzgf6CJO2gMchk+Moa BOqLZ3bQ6qXwlfGCzP9+rKbVjBNPAvDZsYza9ZX+OJHWl1/8w+uSzyEEFOtilnNvQ2eM ENdg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=nrndXKIt; 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; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-7b7048e2448si1176827685a.493.2024.12.18.05.44.13 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 18 Dec 2024 05:44:13 -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=nrndXKIt; 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; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tNuLJ-0002NY-Ej; Wed, 18 Dec 2024 08:44:05 -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 1tNuKb-0001L6-60 for qemu-devel@nongnu.org; Wed, 18 Dec 2024 08:43:20 -0500 Received: from mail-oi1-x229.google.com ([2607:f8b0:4864:20::229]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tNuKX-0005gO-HQ for qemu-devel@nongnu.org; Wed, 18 Dec 2024 08:43:16 -0500 Received: by mail-oi1-x229.google.com with SMTP id 5614622812f47-3eba7784112so2534797b6e.2 for ; Wed, 18 Dec 2024 05:43:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1734529392; x=1735134192; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=V9uuK3Bi0zd4Ff4jmnl20vwDce29dx6gKCtTc6/W2eI=; b=nrndXKItF/MXHbV2uQeq8FXyG4BoQE+jFmPd8P9qhdYnHwVSa51MNRWgYJhD0P1AVi YF5ypy0uzbI3TQBhkOtEC8JBj265ktgtY2ogM3sGSa1pg59UgO2pIlDWgUGyFuYAEc6O bo/9jLxFa99HPUaAT+jG/vJ0HGutzIJce2Sz9PeqecryB60z7Qyrmllo/y0wAPCb6eYb M53iV7e+KaCTw0ljW9HiTt993jPwq5vJ5x5VlPiGFY01F55AvugjmMwrR8ViWckuKxvC RPx0BlIf+Uoo2nqdhGfJYmTsIwqAZ8HvehkfdhA7pJqI3fzNJy+L6dCACWCfUSUG1BlS HsfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734529392; x=1735134192; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=V9uuK3Bi0zd4Ff4jmnl20vwDce29dx6gKCtTc6/W2eI=; b=pgOplSqnIofjsvhEXIs8+7/bAYBGR7Bwgt5+svZz91OOHpppOpXT57/Iuq+MMPwbhD XY4YJFgcb0+XmP3YMTy2ujUwgJBNm2Z+QOat6zVrOD88VGkeczbgDu9qYsMnqeIvGgjG 1l2wn665WVfZMYAS3FfzJ5NoECbtAuvvGUrMKcZekJeza552i3Nwdo3hWdox48gkab/A f4g2kU4Lnhb1PPVApShfPQ/mRzL8+djYIEDqJ/IFhGr2G1Sfuy3wlNgGWw5y3TpzyigB T2wau1vxBxeTREwr9daR+AHQ4wBjP9qMxfIFbnly+db7HNCwGpVD1wCNfd29ys/Sw90t y1mQ== X-Gm-Message-State: AOJu0Yzs0gV5josniNKyNJXFgD0UvddZH+FE3b+9iWEXyhFfC68TNOxS UsGe/BU4qx647Il59/w48hHeYxiGN9wZLt/SW4JJKZYEs2pbuEi3dhYL8a4pgaTNRPfH05NHKQq +zx19RLov X-Gm-Gg: ASbGncvjyKpw/g7NPR89HBnFHNCSeZw4tAn3I3oh71Pm8Tne1t2rJsqkg5J0BvTbSKk dzTTNr8vxms9QMjOfLlXAGCLMXMo7Dx1c9hQHXH5koPI4m4rsUnO7HAGSLUISIo7aSNa8XwGv9g Fd7WVTBaQYN54ZdGFgf/Ri9M27uHov5mQX9J2lsCMMObSoTiwmbLi46jmnne34O6IINZLpx+WcI q8V1a17PtfdTGcfLCPmY1ezCXs3YFXtpli6xJvNY0uwj4VQEHtdmq8uPol6+96x X-Received: by 2002:a05:6808:1383:b0:3eb:394b:83f5 with SMTP id 5614622812f47-3eccc0b9fc9mr1598569b6e.16.1734529392181; Wed, 18 Dec 2024 05:43:12 -0800 (PST) Received: from stoup.. ([187.217.227.247]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3ebb478a502sm2870951b6e.9.2024.12.18.05.43.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Dec 2024 05:43:11 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com Subject: [PATCH v2 13/24] hw/core: Introduce device_class_set_props_n Date: Wed, 18 Dec 2024 07:42:40 -0600 Message-ID: <20241218134251.4724-14-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241218134251.4724-1-richard.henderson@linaro.org> References: <20241218134251.4724-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::229; envelope-from=richard.henderson@linaro.org; helo=mail-oi1-x229.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 Record the size of the array in DeviceClass.props_count_. Iterate with known count in qdev_prop_walk. Signed-off-by: Richard Henderson --- include/hw/qdev-core.h | 18 ++++++++++++++++++ hw/core/qdev-properties.c | 39 +++++++++++++++++++++++++++++---------- hw/core/qdev.c | 1 + system/qdev-monitor.c | 15 +++++++-------- 4 files changed, 55 insertions(+), 18 deletions(-) diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index cbce3cf0b4..e9b4891f55 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -138,6 +138,12 @@ struct DeviceClass { */ const Property *props_; + /** + * @props_count_: number of elements in @props_; should only be + * assigned by using device_class_set_props(). + */ + uint16_t props_count_; + /** * @user_creatable: Can user instantiate with -device / device_add? * @@ -960,6 +966,18 @@ void device_class_set_props(DeviceClass *dc, const Property *props); (device_class_set_props)((dc), (props)); \ } while (0) +/** + * device_class_set_props_n(): add a set of properties to an device + * @dc: the parent DeviceClass all devices inherit + * @props: an array of properties, not terminated by DEFINE_PROP_END_OF_LIST. + * @n: ARRAY_SIZE(@props) + * + * This will add a set of properties to the object. It will fault if + * you attempt to add an existing property defined by a parent class. + * To modify an inherited property you need to use???? + */ +void device_class_set_props_n(DeviceClass *dc, const Property *props, size_t n); + /** * device_class_set_parent_realize() - set up for chaining realize fns * @dc: The device class diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c index de618a964a..31e3072b55 100644 --- a/hw/core/qdev-properties.c +++ b/hw/core/qdev-properties.c @@ -749,16 +749,13 @@ const PropertyInfo qdev_prop_array = { /* --- public helpers --- */ -static const Property *qdev_prop_walk(const Property *props, const char *name) +static const Property *qdev_prop_walk(DeviceClass *cls, const char *name) { - if (!props) { - return NULL; - } - while (props->name) { - if (strcmp(props->name, name) == 0) { - return props; + for (int i = 0, n = cls->props_count_; i < n; ++i) { + const Property *prop = &cls->props_[i]; + if (strcmp(prop->name, name) == 0) { + return prop; } - props++; } return NULL; } @@ -771,7 +768,7 @@ static const Property *qdev_prop_find(DeviceState *dev, const char *name) /* device properties */ class = object_get_class(OBJECT(dev)); do { - prop = qdev_prop_walk(DEVICE_CLASS(class)->props_, name); + prop = qdev_prop_walk(DEVICE_CLASS(class), name); if (prop) { return prop; } @@ -1064,9 +1061,31 @@ static void qdev_class_add_legacy_property(DeviceClass *dc, const Property *prop void (device_class_set_props)(DeviceClass *dc, const Property *props) { const Property *prop; + size_t n; dc->props_ = props; - for (prop = props; prop && prop->name; prop++) { + for (prop = props, n = 0; prop && prop->name; prop++, n++) { + qdev_class_add_legacy_property(dc, prop); + qdev_class_add_property(dc, prop->name, prop); + } + + /* We used a hole in DeviceClass because that's still a lot. */ + assert(n <= UINT16_MAX); + dc->props_count_ = n; +} + +void device_class_set_props_n(DeviceClass *dc, const Property *props, size_t n) +{ + /* We used a hole in DeviceClass because that's still a lot. */ + assert(n <= UINT16_MAX); + assert(n != 0); + + dc->props_ = props; + dc->props_count_ = n; + + for (size_t i = 0; i < n; ++i) { + const Property *prop = &props[i]; + assert(prop->name); qdev_class_add_legacy_property(dc, prop); qdev_class_add_property(dc, prop->name, prop); } diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 5f13111b77..57c1d9df3a 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -703,6 +703,7 @@ static void device_class_base_init(ObjectClass *class, void *data) * so do not propagate them to the subclasses. */ klass->props_ = NULL; + klass->props_count_ = 0; } static void device_unparent(Object *obj) diff --git a/system/qdev-monitor.c b/system/qdev-monitor.c index 03ae610649..6831fffd9d 100644 --- a/system/qdev-monitor.c +++ b/system/qdev-monitor.c @@ -752,19 +752,18 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__) -static void qdev_print_props(Monitor *mon, DeviceState *dev, const Property *props, +static void qdev_print_props(Monitor *mon, DeviceState *dev, DeviceClass *dc, int indent) { - if (!props) - return; - for (; props->name; props++) { + for (int i = 0, n = dc->props_count_; i < n; ++i) { + const Property *prop = &dc->props_[i]; char *value; - char *legacy_name = g_strdup_printf("legacy-%s", props->name); + char *legacy_name = g_strdup_printf("legacy-%s", prop->name); if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) { value = object_property_get_str(OBJECT(dev), legacy_name, NULL); } else { - value = object_property_print(OBJECT(dev), props->name, true, + value = object_property_print(OBJECT(dev), prop->name, true, NULL); } g_free(legacy_name); @@ -772,7 +771,7 @@ static void qdev_print_props(Monitor *mon, DeviceState *dev, const Property *pro if (!value) { continue; } - qdev_printf("%s = %s\n", props->name, + qdev_printf("%s = %s\n", prop->name, *value ? value : ""); g_free(value); } @@ -812,7 +811,7 @@ static void qdev_print(Monitor *mon, DeviceState *dev, int indent) } class = object_get_class(OBJECT(dev)); do { - qdev_print_props(mon, dev, DEVICE_CLASS(class)->props_, indent); + qdev_print_props(mon, dev, DEVICE_CLASS(class), indent); class = object_class_get_parent(class); } while (class != object_class_by_name(TYPE_DEVICE)); bus_print_dev(dev->parent_bus, mon, dev, indent);