@@ -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));
}
@@ -154,7 +155,8 @@ DeviceState *qdev_new_dynamic(const char *name, Error **errp)
return DEVICE(object_new_dynamic(name, errp));
}
-DeviceState *qdev_try_new(const char *name)
+/* Only to be called via the 'qdev_try_new' macro */
+DeviceState *qdev_try_new_internal(const char *name)
{
ObjectClass *oc = module_object_class_by_name(name);
if (!oc) {
@@ -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
@@ -487,7 +497,17 @@ DeviceState *qdev_new_dynamic(const char *name, Error **errp);
* Return: a derived DeviceState object with a reference count of 1 or
* NULL if type @name does not exist.
*/
-DeviceState *qdev_try_new(const char *name);
+
+/*
+ * NB, qdev_try_new_internal is just an internal helper, wrapped by
+ * the qdev_try_new() macro which prevents invokation unless given
+ * a static, const string.
+ *
+ * Code should call qdev_try_new(), or qdev_try_new_dynamic(), not
+ * qdev_try_new_internal().
+ */
+DeviceState *qdev_try_new_internal(const char *name);
+#define qdev_try_new(name) qdev_try_new_internal(name "")
/**
* qdev_try_new_dynamic: Try to create a device on the heap
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 | 6 ++++-- include/hw/qdev-core.h | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-)