[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 13/49] qom: centralize module-loading functionality
From: |
Paolo Bonzini |
Subject: |
[PULL 13/49] qom: centralize module-loading functionality |
Date: |
Thu, 31 Oct 2024 18:51:37 +0100 |
Put together the common code of object_initialize() and
module_object_class_by_name() into a function that supports
Error **. Rename the existing function type_get_by_name() to
clarify that it will only look at defined types; this is often
okay within object.c to look at the parents, but not outside it.
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
qom/object.c | 72 ++++++++++++++++++++++++++--------------------------
1 file changed, 36 insertions(+), 36 deletions(-)
diff --git a/qom/object.c b/qom/object.c
index 8b269414488..29155c64639 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -195,7 +195,7 @@ void type_register_static_array(const TypeInfo *infos, int
nr_infos)
}
}
-static TypeImpl *type_get_by_name(const char *name)
+static TypeImpl *type_get_by_name_noload(const char *name)
{
if (name == NULL) {
return NULL;
@@ -204,10 +204,32 @@ static TypeImpl *type_get_by_name(const char *name)
return type_table_lookup(name);
}
+static TypeImpl *type_get_or_load_by_name(const char *name, Error **errp)
+{
+ TypeImpl *type = type_get_by_name_noload(name);
+
+#ifdef CONFIG_MODULES
+ if (!type) {
+ int rv = module_load_qom(name, errp);
+ if (rv > 0) {
+ type = type_get_by_name_noload(name);
+ } else {
+ error_prepend(errp, "could not load a module for type '%s'", name);
+ return NULL;
+ }
+ }
+#endif
+ if (!type) {
+ error_setg(errp, "unknown type '%s'", name);
+ }
+
+ return type;
+}
+
static TypeImpl *type_get_parent(TypeImpl *type)
{
if (!type->parent_type && type->parent) {
- type->parent_type = type_get_by_name(type->parent);
+ type->parent_type = type_get_by_name_noload(type->parent);
if (!type->parent_type) {
fprintf(stderr, "Type '%s' is missing its parent '%s'\n",
type->name, type->parent);
@@ -363,7 +385,7 @@ static void type_initialize(TypeImpl *ti)
}
for (i = 0; i < ti->num_interfaces; i++) {
- TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
+ TypeImpl *t = type_get_by_name_noload(ti->interfaces[i].typename);
if (!t) {
error_report("missing interface '%s' for object '%s'",
ti->interfaces[i].typename, parent->name);
@@ -557,23 +579,7 @@ static void object_initialize_with_type(Object *obj,
size_t size, TypeImpl *type
void object_initialize(void *data, size_t size, const char *typename)
{
- TypeImpl *type = type_get_by_name(typename);
-
-#ifdef CONFIG_MODULES
- if (!type) {
- int rv = module_load_qom(typename, &error_fatal);
- if (rv > 0) {
- type = type_get_by_name(typename);
- } else {
- error_report("missing object type '%s'", typename);
- exit(1);
- }
- }
-#endif
- if (!type) {
- error_report("missing object type '%s'", typename);
- abort();
- }
+ TypeImpl *type = type_get_or_load_by_name(typename, &error_fatal);
object_initialize_with_type(data, size, type);
}
@@ -784,7 +790,7 @@ Object *object_new_with_class(ObjectClass *klass)
Object *object_new(const char *typename)
{
- TypeImpl *ti = type_get_by_name(typename);
+ TypeImpl *ti = type_get_by_name_noload(typename);
return object_new_with_type(ti);
}
@@ -957,7 +963,7 @@ ObjectClass *object_class_dynamic_cast(ObjectClass *class,
return class;
}
- target_type = type_get_by_name(typename);
+ target_type = type_get_by_name_noload(typename);
if (!target_type) {
/* target class type unknown, so fail the cast */
return NULL;
@@ -1055,7 +1061,7 @@ const char *object_class_get_name(ObjectClass *klass)
ObjectClass *object_class_by_name(const char *typename)
{
- TypeImpl *type = type_get_by_name(typename);
+ TypeImpl *type = type_get_by_name_noload(typename);
if (!type) {
return NULL;
@@ -1068,21 +1074,15 @@ ObjectClass *object_class_by_name(const char *typename)
ObjectClass *module_object_class_by_name(const char *typename)
{
- ObjectClass *oc;
+ TypeImpl *type = type_get_or_load_by_name(typename, NULL);
- oc = object_class_by_name(typename);
-#ifdef CONFIG_MODULES
- if (!oc) {
- Error *local_err = NULL;
- int rv = module_load_qom(typename, &local_err);
- if (rv > 0) {
- oc = object_class_by_name(typename);
- } else if (rv < 0) {
- error_report_err(local_err);
- }
+ if (!type) {
+ return NULL;
}
-#endif
- return oc;
+
+ type_initialize(type);
+
+ return type->class;
}
ObjectClass *object_class_get_parent(ObjectClass *class)
--
2.47.0
- [PULL 02/49] tests: add 'rust' and 'bindgen' to CI package list, (continued)
- [PULL 02/49] tests: add 'rust' and 'bindgen' to CI package list, Paolo Bonzini, 2024/10/31
- [PULL 04/49] target/i386: fix CPUID check for LFENCE and SFENCE, Paolo Bonzini, 2024/10/31
- [PULL 06/49] configure: detect 64-bit MIPS, Paolo Bonzini, 2024/10/31
- [PULL 05/49] scripts: remove erroneous file that breaks git clone on Windows, Paolo Bonzini, 2024/10/31
- [PULL 07/49] configure, meson: deprecate 32-bit MIPS, Paolo Bonzini, 2024/10/31
- [PULL 08/49] MAINTAINERS: Add myself as a reviewer of x86 general architecture support, Paolo Bonzini, 2024/10/31
- [PULL 09/49] accel: remove dead statement and useless assertion, Paolo Bonzini, 2024/10/31
- [PULL 10/49] i386/cpu: Drop the check of phys_bits in host_cpu_realizefn(), Paolo Bonzini, 2024/10/31
- [PULL 11/49] qom: remove unused function, Paolo Bonzini, 2024/10/31
- [PULL 12/49] qom: use object_new_with_class when possible, Paolo Bonzini, 2024/10/31
- [PULL 13/49] qom: centralize module-loading functionality,
Paolo Bonzini <=
- [PULL 15/49] qom: allow user-creatable classes to be in modules, Paolo Bonzini, 2024/10/31
- [PULL 18/49] target/i386: Expose bits related to SRSO vulnerability, Paolo Bonzini, 2024/10/31
- [PULL 16/49] target/i386: Fix minor typo in NO_NESTED_DATA_BP feature bit, Paolo Bonzini, 2024/10/31
- [PULL 17/49] target/i386: Add PerfMonV2 feature bit, Paolo Bonzini, 2024/10/31
- [PULL 20/49] target/i386/hvf: fix handling of XSAVE-related CPUID bits, Paolo Bonzini, 2024/10/31
- [PULL 19/49] target/i386: Expose new feature bits in CPUID 8000_0021_EAX/EBX, Paolo Bonzini, 2024/10/31
- [PULL 21/49] tests/lcitool: Update libvirt-ci and add libcbor dependency, Paolo Bonzini, 2024/10/31
- [PULL 23/49] hw/core: Add Enclave Image Format (EIF) related helpers, Paolo Bonzini, 2024/10/31
- [PULL 22/49] device/virtio-nsm: Support for Nitro Secure Module device, Paolo Bonzini, 2024/10/31
- [PULL 24/49] core/machine: Make create_default_memdev machine a virtual method, Paolo Bonzini, 2024/10/31