From: Paolo Bonzini <pbonzini@redhat.com>
object_resolve_type_unambiguous provides a useful functionality, that
is currently emulated for example by usb_bus_find(). Move it to core
code and add error reporting for increased generality.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Message-ID: <20240223124406.234509-2-pbonzini@redhat.com>
[PMD: Fixed style]
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
include/qom/object.h | 13 +++++++++++++
hw/i386/acpi-build.c | 20 +++++---------------
qom/object.c | 16 ++++++++++++++++
3 files changed, 34 insertions(+), 15 deletions(-)
diff --git a/include/qom/object.h b/include/qom/object.h
index afccd24ca7..e9ed9550f0 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -1550,6 +1550,19 @@ Object *object_resolve_path(const char *path, bool *ambiguous);
Object *object_resolve_path_type(const char *path, const char *typename,
bool *ambiguous);
+/**
+ * object_resolve_type_unambiguous:
+ * @typename: the type to look for
+ * @errp: pointer to error object
+ *
+ * Return the only object in the QOM tree of type @typename.
+ * If no match or more than one match is found, an error is
+ * returned.
+ *
+ * Returns: The matched object or NULL on path lookup failure.
+ */
+Object *object_resolve_type_unambiguous(const char *typename, Error **errp);
+
/**
* object_resolve_path_at:
* @parent: the object in which to resolve the path
diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
index d3ce96dd9f..896ff87519 100644
--- a/hw/i386/acpi-build.c
+++ b/hw/i386/acpi-build.c
@@ -192,21 +192,10 @@ static void init_common_fadt_data(MachineState *ms, Object *o,
*data = fadt;
}
-static Object *object_resolve_type_unambiguous(const char *typename)
-{
- bool ambig;
- Object *o = object_resolve_path_type("", typename, &ambig);
-
- if (ambig || !o) {
- return NULL;
- }
- return o;
-}
-
static void acpi_get_pm_info(MachineState *machine, AcpiPmInfo *pm)
{
- Object *piix = object_resolve_type_unambiguous(TYPE_PIIX4_PM);
- Object *lpc = object_resolve_type_unambiguous(TYPE_ICH9_LPC_DEVICE);
+ Object *piix = object_resolve_type_unambiguous(TYPE_PIIX4_PM, NULL);
+ Object *lpc = object_resolve_type_unambiguous(TYPE_ICH9_LPC_DEVICE, NULL);
Object *obj = piix ? piix : lpc;
QObject *o;
pm->cpu_hp_io_base = 0;
@@ -1428,8 +1417,9 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
AcpiPmInfo *pm, AcpiMiscInfo *misc,
Range *pci_hole, Range *pci_hole64, MachineState *machine)
{
- Object *i440fx = object_resolve_type_unambiguous(TYPE_I440FX_PCI_HOST_BRIDGE);
- Object *q35 = object_resolve_type_unambiguous(TYPE_Q35_HOST_DEVICE);
+ Object *i440fx = object_resolve_type_unambiguous(TYPE_I440FX_PCI_HOST_BRIDGE,
+ NULL);
+ Object *q35 = object_resolve_type_unambiguous(TYPE_Q35_HOST_DEVICE, NULL);
CrsRangeEntry *entry;
Aml *dsdt, *sb_scope, *scope, *dev, *method, *field, *pkg, *crs;
CrsRangeSet crs_range_set;
diff --git a/qom/object.c b/qom/object.c
index 2c4c64d2b6..d4a001cf41 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -2229,6 +2229,22 @@ Object *object_resolve_path_at(Object *parent, const char *path)
return object_resolve_abs_path(parent, parts, TYPE_OBJECT);
}
+Object *object_resolve_type_unambiguous(const char *typename, Error **errp)
+{
+ bool ambig;
+ Object *o = object_resolve_path_type("", typename, &ambig);
+
+ if (ambig) {
+ error_setg(errp, "More than one object of type %s", typename);
+ return NULL;
+ }
+ if (!o) {
+ error_setg(errp, "No object found of type %s", typename);
+ return NULL;
+ }
+ return o;
+}
+
typedef struct StringProperty
{
char *(*get)(Object *, Error **);
--
2.41.0