4 * Copyright IBM, Corp. 2011
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/object.h"
14 #include "qemu-common.h"
15 #include "qapi/qapi-visit-core.h"
17 /* TODO: replace QObject with a simpler visitor to avoid a dependency
18 * of the QOM core on QObject? */
19 #include "qemu/qom-qobject.h"
25 #define MAX_INTERFACES 32
27 typedef struct InterfaceImpl InterfaceImpl;
28 typedef struct TypeImpl TypeImpl;
33 void (*interface_initfn)(ObjectClass *class, void *data);
45 void (*class_init)(ObjectClass *klass, void *data);
46 void (*class_finalize)(ObjectClass *klass, void *data);
50 void (*instance_init)(Object *obj);
51 void (*instance_finalize)(Object *obj);
56 TypeImpl *parent_type;
61 InterfaceImpl interfaces[MAX_INTERFACES];
64 typedef struct Interface
70 #define INTERFACE(obj) OBJECT_CHECK(Interface, obj, TYPE_INTERFACE)
72 static Type type_interface;
74 static GHashTable *type_table_get(void)
76 static GHashTable *type_table;
78 if (type_table == NULL) {
79 type_table = g_hash_table_new(g_str_hash, g_str_equal);
85 static void type_table_add(TypeImpl *ti)
87 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
90 static TypeImpl *type_table_lookup(const char *name)
92 return g_hash_table_lookup(type_table_get(), name);
95 TypeImpl *type_register(const TypeInfo *info)
97 TypeImpl *ti = g_malloc0(sizeof(*ti));
99 g_assert(info->name != NULL);
101 if (type_table_lookup(info->name) != NULL) {
102 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
106 ti->name = g_strdup(info->name);
107 ti->parent = g_strdup(info->parent);
109 ti->class_size = info->class_size;
110 ti->instance_size = info->instance_size;
112 ti->class_init = info->class_init;
113 ti->class_finalize = info->class_finalize;
114 ti->class_data = info->class_data;
116 ti->instance_init = info->instance_init;
117 ti->instance_finalize = info->instance_finalize;
119 ti->abstract = info->abstract;
121 if (info->interfaces) {
124 for (i = 0; info->interfaces[i].type; i++) {
125 ti->interfaces[i].parent = info->interfaces[i].type;
126 ti->interfaces[i].interface_initfn = info->interfaces[i].interface_initfn;
127 ti->num_interfaces++;
136 TypeImpl *type_register_static(const TypeInfo *info)
138 return type_register(info);
141 static TypeImpl *type_get_by_name(const char *name)
147 return type_table_lookup(name);
150 static TypeImpl *type_get_parent(TypeImpl *type)
152 if (!type->parent_type && type->parent) {
153 type->parent_type = type_get_by_name(type->parent);
154 g_assert(type->parent_type != NULL);
157 return type->parent_type;
160 static bool type_has_parent(TypeImpl *type)
162 return (type->parent != NULL);
165 static size_t type_class_get_size(TypeImpl *ti)
167 if (ti->class_size) {
168 return ti->class_size;
171 if (type_has_parent(ti)) {
172 return type_class_get_size(type_get_parent(ti));
175 return sizeof(ObjectClass);
178 static void type_class_interface_init(TypeImpl *ti, InterfaceImpl *iface)
181 .instance_size = sizeof(Interface),
182 .parent = iface->parent,
183 .class_size = sizeof(InterfaceClass),
184 .class_init = iface->interface_initfn,
187 char *name = g_strdup_printf("<%s::%s>", ti->name, iface->parent);
190 iface->type = type_register(&info);
194 static void type_class_init(TypeImpl *ti)
196 size_t class_size = sizeof(ObjectClass);
203 ti->class_size = type_class_get_size(ti);
205 ti->class = g_malloc0(ti->class_size);
206 ti->class->type = ti;
208 if (type_has_parent(ti)) {
209 TypeImpl *parent = type_get_parent(ti);
211 type_class_init(parent);
213 class_size = parent->class_size;
214 g_assert(parent->class_size <= ti->class_size);
216 memcpy((void *)ti->class + sizeof(ObjectClass),
217 (void *)parent->class + sizeof(ObjectClass),
218 parent->class_size - sizeof(ObjectClass));
221 memset((void *)ti->class + class_size, 0, ti->class_size - class_size);
223 for (i = 0; i < ti->num_interfaces; i++) {
224 type_class_interface_init(ti, &ti->interfaces[i]);
227 if (ti->class_init) {
228 ti->class_init(ti->class, ti->class_data);
232 static void object_interface_init(Object *obj, InterfaceImpl *iface)
234 TypeImpl *ti = iface->type;
235 Interface *iface_obj;
237 iface_obj = INTERFACE(object_new(ti->name));
238 iface_obj->obj = obj;
240 obj->interfaces = g_slist_prepend(obj->interfaces, iface_obj);
243 static void object_init_with_type(Object *obj, TypeImpl *ti)
247 if (type_has_parent(ti)) {
248 object_init_with_type(obj, type_get_parent(ti));
251 for (i = 0; i < ti->num_interfaces; i++) {
252 object_interface_init(obj, &ti->interfaces[i]);
255 if (ti->instance_init) {
256 ti->instance_init(obj);
260 void object_initialize_with_type(void *data, TypeImpl *type)
264 g_assert(type != NULL);
265 g_assert(type->instance_size >= sizeof(ObjectClass));
267 type_class_init(type);
268 g_assert(type->abstract == false);
270 memset(obj, 0, type->instance_size);
271 obj->class = type->class;
272 QTAILQ_INIT(&obj->properties);
273 object_init_with_type(obj, type);
276 void object_initialize(void *data, const char *typename)
278 TypeImpl *type = type_get_by_name(typename);
280 object_initialize_with_type(data, type);
283 static void object_property_del_all(Object *obj)
285 while (!QTAILQ_EMPTY(&obj->properties)) {
286 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
288 QTAILQ_REMOVE(&obj->properties, prop, node);
291 prop->release(obj, prop->name, prop->opaque);
300 static void object_property_del_child(Object *obj, Object *child, Error **errp)
302 ObjectProperty *prop;
304 QTAILQ_FOREACH(prop, &obj->properties, node) {
305 if (!strstart(prop->type, "child<", NULL)) {
309 if (prop->opaque == child) {
310 object_property_del(obj, prop->name, errp);
315 void object_unparent(Object *obj)
318 object_property_del_child(obj->parent, obj, NULL);
322 static void object_deinit(Object *obj, TypeImpl *type)
324 if (type->instance_finalize) {
325 type->instance_finalize(obj);
328 while (obj->interfaces) {
329 Interface *iface_obj = obj->interfaces->data;
330 obj->interfaces = g_slist_delete_link(obj->interfaces, obj->interfaces);
331 object_delete(OBJECT(iface_obj));
334 if (type_has_parent(type)) {
335 object_deinit(obj, type_get_parent(type));
338 object_unparent(obj);
341 void object_finalize(void *data)
344 TypeImpl *ti = obj->class->type;
346 object_deinit(obj, ti);
347 object_property_del_all(obj);
349 g_assert(obj->ref == 0);
352 Object *object_new_with_type(Type type)
356 g_assert(type != NULL);
358 obj = g_malloc(type->instance_size);
359 object_initialize_with_type(obj, type);
365 Object *object_new(const char *typename)
367 TypeImpl *ti = type_get_by_name(typename);
369 return object_new_with_type(ti);
372 void object_delete(Object *obj)
375 g_assert(obj->ref == 0);
379 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
383 /* Check if typename is a direct ancestor of type */
385 if (type == target_type) {
389 type = type_get_parent(type);
395 static bool object_is_type(Object *obj, TypeImpl *target_type)
397 return !target_type || type_is_ancestor(obj->class->type, target_type);
400 Object *object_dynamic_cast(Object *obj, const char *typename)
402 TypeImpl *target_type = type_get_by_name(typename);
405 /* Check if typename is a direct ancestor. Special-case TYPE_OBJECT,
406 * we want to go back from interfaces to the parent.
408 if (target_type && object_is_type(obj, target_type)) {
412 /* Check if obj is an interface and its containing object is a direct
413 * ancestor of typename. In principle we could do this test at the very
414 * beginning of object_dynamic_cast, avoiding a second call to
415 * object_is_type. However, casting between interfaces is relatively
416 * rare, and object_is_type(obj, type_interface) would fail almost always.
418 * Perhaps we could add a magic value to the object header for increased
419 * (run-time) type safety and to speed up tests like this one. If we ever
420 * do that we can revisit the order here.
422 if (object_is_type(obj, type_interface)) {
423 assert(!obj->interfaces);
424 obj = INTERFACE(obj)->obj;
425 if (object_is_type(obj, target_type)) {
434 /* Check if obj has an interface of typename */
435 for (i = obj->interfaces; i; i = i->next) {
436 Interface *iface = i->data;
438 if (object_is_type(OBJECT(iface), target_type)) {
439 return OBJECT(iface);
447 static void register_types(void)
449 static TypeInfo interface_info = {
450 .name = TYPE_INTERFACE,
451 .instance_size = sizeof(Interface),
455 type_interface = type_register_static(&interface_info);
458 type_init(register_types)
460 Object *object_dynamic_cast_assert(Object *obj, const char *typename)
464 inst = object_dynamic_cast(obj, typename);
467 fprintf(stderr, "Object %p is not an instance of type %s\n",
475 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
476 const char *typename)
478 TypeImpl *target_type = type_get_by_name(typename);
479 TypeImpl *type = class->type;
482 if (type == target_type) {
486 type = type_get_parent(type);
492 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
493 const char *typename)
495 ObjectClass *ret = object_class_dynamic_cast(class, typename);
498 fprintf(stderr, "Object %p is not an instance of type %s\n",
506 const char *object_get_typename(Object *obj)
508 return obj->class->type->name;
511 ObjectClass *object_get_class(Object *obj)
516 const char *object_class_get_name(ObjectClass *klass)
518 return klass->type->name;
521 ObjectClass *object_class_by_name(const char *typename)
523 TypeImpl *type = type_get_by_name(typename);
529 type_class_init(type);
534 typedef struct OCFData
536 void (*fn)(ObjectClass *klass, void *opaque);
537 const char *implements_type;
538 bool include_abstract;
542 static void object_class_foreach_tramp(gpointer key, gpointer value,
545 OCFData *data = opaque;
546 TypeImpl *type = value;
549 type_class_init(type);
552 if (!data->include_abstract && type->abstract) {
556 if (data->implements_type &&
557 !object_class_dynamic_cast(k, data->implements_type)) {
561 data->fn(k, data->opaque);
564 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
565 const char *implements_type, bool include_abstract,
568 OCFData data = { fn, implements_type, include_abstract, opaque };
570 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
573 void object_ref(Object *obj)
578 void object_unref(Object *obj)
580 g_assert(obj->ref > 0);
583 /* parent always holds a reference to its children */
585 object_finalize(obj);
589 void object_property_add(Object *obj, const char *name, const char *type,
590 ObjectPropertyAccessor *get,
591 ObjectPropertyAccessor *set,
592 ObjectPropertyRelease *release,
593 void *opaque, Error **errp)
595 ObjectProperty *prop = g_malloc0(sizeof(*prop));
597 prop->name = g_strdup(name);
598 prop->type = g_strdup(type);
602 prop->release = release;
603 prop->opaque = opaque;
605 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
608 static ObjectProperty *object_property_find(Object *obj, const char *name)
610 ObjectProperty *prop;
612 QTAILQ_FOREACH(prop, &obj->properties, node) {
613 if (strcmp(prop->name, name) == 0) {
621 void object_property_del(Object *obj, const char *name, Error **errp)
623 ObjectProperty *prop = object_property_find(obj, name);
625 QTAILQ_REMOVE(&obj->properties, prop, node);
627 prop->release(obj, prop->name, prop->opaque);
634 void object_property_get(Object *obj, Visitor *v, const char *name,
637 ObjectProperty *prop = object_property_find(obj, name);
640 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
645 error_set(errp, QERR_PERMISSION_DENIED);
647 prop->get(obj, v, prop->opaque, name, errp);
651 void object_property_set(Object *obj, Visitor *v, const char *name,
654 ObjectProperty *prop = object_property_find(obj, name);
657 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
662 error_set(errp, QERR_PERMISSION_DENIED);
664 prop->set(obj, v, prop->opaque, name, errp);
668 void object_property_set_str(Object *obj, const char *value,
669 const char *name, Error **errp)
671 QString *qstr = qstring_from_str(value);
672 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
677 char *object_property_get_str(Object *obj, const char *name,
680 QObject *ret = object_property_get_qobject(obj, name, errp);
687 qstring = qobject_to_qstring(ret);
689 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
692 retval = g_strdup(qstring_get_str(qstring));
699 void object_property_set_link(Object *obj, Object *value,
700 const char *name, Error **errp)
702 object_property_set_str(obj, object_get_canonical_path(value),
706 Object *object_property_get_link(Object *obj, const char *name,
709 char *str = object_property_get_str(obj, name, errp);
710 Object *target = NULL;
713 target = object_resolve_path(str, NULL);
715 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
723 void object_property_set_bool(Object *obj, bool value,
724 const char *name, Error **errp)
726 QBool *qbool = qbool_from_int(value);
727 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
732 bool object_property_get_bool(Object *obj, const char *name,
735 QObject *ret = object_property_get_qobject(obj, name, errp);
742 qbool = qobject_to_qbool(ret);
744 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
747 retval = qbool_get_int(qbool);
754 void object_property_set_int(Object *obj, int64_t value,
755 const char *name, Error **errp)
757 QInt *qint = qint_from_int(value);
758 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
763 int64_t object_property_get_int(Object *obj, const char *name,
766 QObject *ret = object_property_get_qobject(obj, name, errp);
773 qint = qobject_to_qint(ret);
775 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
778 retval = qint_get_int(qint);
785 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
787 ObjectProperty *prop = object_property_find(obj, name);
790 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
797 Object *object_get_root(void)
802 root = object_new("container");
808 static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
809 const char *name, Error **errp)
811 Object *child = opaque;
814 path = object_get_canonical_path(child);
815 visit_type_str(v, &path, name, errp);
819 static void object_finalize_child_property(Object *obj, const char *name,
822 Object *child = opaque;
827 void object_property_add_child(Object *obj, const char *name,
828 Object *child, Error **errp)
832 /* Registering an interface object in the composition tree will mightily
833 * confuse object_get_canonical_path (which, on the other hand, knows how
834 * to get the canonical path of an interface object).
836 assert(!object_is_type(obj, type_interface));
838 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
840 object_property_add(obj, name, type, object_get_child_property,
841 NULL, object_finalize_child_property, child, errp);
844 g_assert(child->parent == NULL);
850 static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
851 const char *name, Error **errp)
853 Object **child = opaque;
857 path = object_get_canonical_path(*child);
858 visit_type_str(v, &path, name, errp);
862 visit_type_str(v, &path, name, errp);
866 static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
867 const char *name, Error **errp)
869 Object **child = opaque;
870 bool ambiguous = false;
875 type = object_property_get_type(obj, name, NULL);
877 visit_type_str(v, &path, name, errp);
880 object_unref(*child);
884 if (strcmp(path, "") != 0) {
887 /* Go from link<FOO> to FOO. */
888 target_type = g_strndup(&type[5], strlen(type) - 6);
889 target = object_resolve_path_type(path, target_type, &ambiguous);
892 error_set(errp, QERR_AMBIGUOUS_PATH, path);
897 target = object_resolve_path(path, &ambiguous);
898 if (target || ambiguous) {
899 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
901 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
910 void object_property_add_link(Object *obj, const char *name,
911 const char *type, Object **child,
916 full_type = g_strdup_printf("link<%s>", type);
918 object_property_add(obj, name, full_type,
919 object_get_link_property,
920 object_set_link_property,
926 gchar *object_get_canonical_path(Object *obj)
928 Object *root = object_get_root();
929 char *newpath = NULL, *path = NULL;
931 if (object_is_type(obj, type_interface)) {
932 obj = INTERFACE(obj)->obj;
935 while (obj != root) {
936 ObjectProperty *prop = NULL;
938 g_assert(obj->parent != NULL);
940 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
941 if (!strstart(prop->type, "child<", NULL)) {
945 if (prop->opaque == obj) {
947 newpath = g_strdup_printf("%s/%s", prop->name, path);
951 path = g_strdup(prop->name);
957 g_assert(prop != NULL);
962 newpath = g_strdup_printf("/%s", path);
968 static Object *object_resolve_abs_path(Object *parent,
970 const char *typename,
973 ObjectProperty *prop;
976 if (parts[index] == NULL) {
977 return object_dynamic_cast(parent, typename);
980 if (strcmp(parts[index], "") == 0) {
981 return object_resolve_abs_path(parent, parts, typename, index + 1);
984 prop = object_property_find(parent, parts[index]);
990 if (strstart(prop->type, "link<", NULL)) {
991 Object **pchild = prop->opaque;
995 } else if (strstart(prop->type, "child<", NULL)) {
996 child = prop->opaque;
1003 return object_resolve_abs_path(child, parts, typename, index + 1);
1006 static Object *object_resolve_partial_path(Object *parent,
1008 const char *typename,
1012 ObjectProperty *prop;
1014 obj = object_resolve_abs_path(parent, parts, typename, 0);
1016 QTAILQ_FOREACH(prop, &parent->properties, node) {
1019 if (!strstart(prop->type, "child<", NULL)) {
1023 found = object_resolve_partial_path(prop->opaque, parts,
1024 typename, ambiguous);
1035 if (ambiguous && *ambiguous) {
1043 Object *object_resolve_path_type(const char *path, const char *typename,
1046 bool partial_path = true;
1050 parts = g_strsplit(path, "/", 0);
1051 if (parts == NULL || parts[0] == NULL) {
1053 return object_get_root();
1056 if (strcmp(parts[0], "") == 0) {
1057 partial_path = false;
1064 obj = object_resolve_partial_path(object_get_root(), parts,
1065 typename, ambiguous);
1067 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
1075 Object *object_resolve_path(const char *path, bool *ambiguous)
1077 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1080 typedef struct StringProperty
1082 char *(*get)(Object *, Error **);
1083 void (*set)(Object *, const char *, Error **);
1086 static void property_get_str(Object *obj, Visitor *v, void *opaque,
1087 const char *name, Error **errp)
1089 StringProperty *prop = opaque;
1092 value = prop->get(obj, errp);
1094 visit_type_str(v, &value, name, errp);
1099 static void property_set_str(Object *obj, Visitor *v, void *opaque,
1100 const char *name, Error **errp)
1102 StringProperty *prop = opaque;
1104 Error *local_err = NULL;
1106 visit_type_str(v, &value, name, &local_err);
1108 error_propagate(errp, local_err);
1112 prop->set(obj, value, errp);
1116 static void property_release_str(Object *obj, const char *name,
1119 StringProperty *prop = opaque;
1123 void object_property_add_str(Object *obj, const char *name,
1124 char *(*get)(Object *, Error **),
1125 void (*set)(Object *, const char *, Error **),
1128 StringProperty *prop = g_malloc0(sizeof(*prop));
1133 object_property_add(obj, name, "string",
1134 get ? property_get_str : NULL,
1135 set ? property_set_str : NULL,
1136 property_release_str,