[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 9/9] hw: enforce use of static, const string with qdev_new()
From: |
Daniel P . Berrangé |
Subject: |
[PATCH v3 9/9] hw: enforce use of static, const string with qdev_new() |
Date: |
Fri, 15 Nov 2024 17:25:21 +0000 |
Since qdev_new() will assert(), it should only be used in scenarios
where the caller knows exactly what type it is asking to be created,
and can thus be confident in avoiding abstract types.
Enforce this by using a macro wrapper which types to paste "" to the
type name. This will generate a compile error if not passed a static
const string, forcing callers to use qdev_new_dynamic() instead.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
hw/core/qdev.c | 3 ++-
include/hw/qdev-core.h | 12 +++++++++++-
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index eceba33222..968fa33a95 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -144,7 +144,8 @@ bool qdev_set_parent_bus(DeviceState *dev, BusState *bus,
Error **errp)
return true;
}
-DeviceState *qdev_new(const char *name)
+/* Only to be called via the 'qdev_new' macro */
+DeviceState *qdev_new_internal(const char *name)
{
return DEVICE(object_new_dynamic(name, &error_abort));
}
diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index 566c5ef277..335dcd31b0 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -448,7 +448,17 @@ compat_props_add(GPtrArray *arr,
*
* Return: a derived DeviceState object with a reference count of 1.
*/
-DeviceState *qdev_new(const char *name);
+
+/*
+ * NB, qdev_new_internal is just an internal helper, wrapped by
+ * the qdev_new() macro which prevents invokation unless given
+ * a static, const string.
+ *
+ * Code should call qdev_new(), or qdev_new_dynamic(), not
+ * qdev_new_internal().
+ */
+DeviceState *qdev_new_internal(const char *name);
+#define qdev_new(name) qdev_new_internal(name "")
/**
* qdev_new_dynamic: Create a device on the heap
--
2.46.0
- Re: [PATCH v3 1/9] hw: eliminate qdev_try_new, isa_try_new & usb_try_new, (continued)
- [PATCH v3 2/9] qom: refactor checking abstract property when creating instances, Daniel P . Berrangé, 2024/11/15
- [PATCH v3 3/9] qom: allow failure of object_new_with_class, Daniel P . Berrangé, 2024/11/15
- [PATCH v3 4/9] qom: introduce object_new_dynamic(), Daniel P . Berrangé, 2024/11/15
- [PATCH v3 5/9] convert code to object_new_dynamic() where appropriate, Daniel P . Berrangé, 2024/11/15
- [PATCH v3 6/9] qom: enforce use of static, const string with object_new(), Daniel P . Berrangé, 2024/11/15
- [PATCH v3 8/9] convert code to qdev_new_dynamic() where appropriate, Daniel P . Berrangé, 2024/11/15
- [PATCH v3 7/9] qom: introduce qdev_new_dynamic(), Daniel P . Berrangé, 2024/11/15
- [PATCH v3 9/9] hw: enforce use of static, const string with qdev_new(),
Daniel P . Berrangé <=