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"
16 #include "qapi/string-input-visitor.h"
17 #include "qapi/string-output-visitor.h"
19 /* TODO: replace QObject with a simpler visitor to avoid a dependency
20 * of the QOM core on QObject? */
21 #include "qemu/qom-qobject.h"
27 #define MAX_INTERFACES 32
29 typedef struct InterfaceImpl InterfaceImpl;
30 typedef struct TypeImpl TypeImpl;
35 void (*interface_initfn)(ObjectClass *class, void *data);
47 void (*class_init)(ObjectClass *klass, void *data);
48 void (*class_finalize)(ObjectClass *klass, void *data);
52 void (*instance_init)(Object *obj);
53 void (*instance_finalize)(Object *obj);
58 TypeImpl *parent_type;
63 InterfaceImpl interfaces[MAX_INTERFACES];
66 typedef struct Interface
72 #define INTERFACE(obj) OBJECT_CHECK(Interface, obj, TYPE_INTERFACE)
74 static Type type_interface;
76 static GHashTable *type_table_get(void)
78 static GHashTable *type_table;
80 if (type_table == NULL) {
81 type_table = g_hash_table_new(g_str_hash, g_str_equal);
87 static void type_table_add(TypeImpl *ti)
89 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
92 static TypeImpl *type_table_lookup(const char *name)
94 return g_hash_table_lookup(type_table_get(), name);
97 TypeImpl *type_register(const TypeInfo *info)
99 TypeImpl *ti = g_malloc0(sizeof(*ti));
101 g_assert(info->name != NULL);
103 if (type_table_lookup(info->name) != NULL) {
104 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
108 ti->name = g_strdup(info->name);
109 ti->parent = g_strdup(info->parent);
111 ti->class_size = info->class_size;
112 ti->instance_size = info->instance_size;
114 ti->class_init = info->class_init;
115 ti->class_finalize = info->class_finalize;
116 ti->class_data = info->class_data;
118 ti->instance_init = info->instance_init;
119 ti->instance_finalize = info->instance_finalize;
121 ti->abstract = info->abstract;
123 if (info->interfaces) {
126 for (i = 0; info->interfaces[i].type; i++) {
127 ti->interfaces[i].parent = info->interfaces[i].type;
128 ti->interfaces[i].interface_initfn = info->interfaces[i].interface_initfn;
129 ti->num_interfaces++;
138 TypeImpl *type_register_static(const TypeInfo *info)
140 return type_register(info);
143 static TypeImpl *type_get_by_name(const char *name)
149 return type_table_lookup(name);
152 static TypeImpl *type_get_parent(TypeImpl *type)
154 if (!type->parent_type && type->parent) {
155 type->parent_type = type_get_by_name(type->parent);
156 g_assert(type->parent_type != NULL);
159 return type->parent_type;
162 static bool type_has_parent(TypeImpl *type)
164 return (type->parent != NULL);
167 static size_t type_class_get_size(TypeImpl *ti)
169 if (ti->class_size) {
170 return ti->class_size;
173 if (type_has_parent(ti)) {
174 return type_class_get_size(type_get_parent(ti));
177 return sizeof(ObjectClass);
180 static void type_class_interface_init(TypeImpl *ti, InterfaceImpl *iface)
183 .instance_size = sizeof(Interface),
184 .parent = iface->parent,
185 .class_size = sizeof(InterfaceClass),
186 .class_init = iface->interface_initfn,
189 char *name = g_strdup_printf("<%s::%s>", ti->name, iface->parent);
192 iface->type = type_register(&info);
196 static void type_class_init(TypeImpl *ti)
198 size_t class_size = sizeof(ObjectClass);
205 ti->class_size = type_class_get_size(ti);
207 ti->class = g_malloc0(ti->class_size);
208 ti->class->type = ti;
210 if (type_has_parent(ti)) {
211 TypeImpl *parent = type_get_parent(ti);
213 type_class_init(parent);
215 class_size = parent->class_size;
216 g_assert(parent->class_size <= ti->class_size);
218 memcpy((void *)ti->class + sizeof(ObjectClass),
219 (void *)parent->class + sizeof(ObjectClass),
220 parent->class_size - sizeof(ObjectClass));
223 memset((void *)ti->class + class_size, 0, ti->class_size - class_size);
225 for (i = 0; i < ti->num_interfaces; i++) {
226 type_class_interface_init(ti, &ti->interfaces[i]);
229 if (ti->class_init) {
230 ti->class_init(ti->class, ti->class_data);
234 static void object_interface_init(Object *obj, InterfaceImpl *iface)
236 TypeImpl *ti = iface->type;
237 Interface *iface_obj;
239 iface_obj = INTERFACE(object_new(ti->name));
240 iface_obj->obj = obj;
242 obj->interfaces = g_slist_prepend(obj->interfaces, iface_obj);
245 static void object_init_with_type(Object *obj, TypeImpl *ti)
249 if (type_has_parent(ti)) {
250 object_init_with_type(obj, type_get_parent(ti));
253 for (i = 0; i < ti->num_interfaces; i++) {
254 object_interface_init(obj, &ti->interfaces[i]);
257 if (ti->instance_init) {
258 ti->instance_init(obj);
262 void object_initialize_with_type(void *data, TypeImpl *type)
266 g_assert(type != NULL);
267 g_assert(type->instance_size >= sizeof(Object));
269 type_class_init(type);
270 g_assert(type->abstract == false);
272 memset(obj, 0, type->instance_size);
273 obj->class = type->class;
274 QTAILQ_INIT(&obj->properties);
275 object_init_with_type(obj, type);
278 void object_initialize(void *data, const char *typename)
280 TypeImpl *type = type_get_by_name(typename);
282 object_initialize_with_type(data, type);
285 static void object_property_del_all(Object *obj)
287 while (!QTAILQ_EMPTY(&obj->properties)) {
288 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
290 QTAILQ_REMOVE(&obj->properties, prop, node);
293 prop->release(obj, prop->name, prop->opaque);
302 static void object_property_del_child(Object *obj, Object *child, Error **errp)
304 ObjectProperty *prop;
306 QTAILQ_FOREACH(prop, &obj->properties, node) {
307 if (strstart(prop->type, "child<", NULL) && prop->opaque == child) {
308 object_property_del(obj, prop->name, errp);
314 void object_unparent(Object *obj)
317 object_property_del_child(obj->parent, obj, NULL);
321 static void object_deinit(Object *obj, TypeImpl *type)
323 if (type->instance_finalize) {
324 type->instance_finalize(obj);
327 while (obj->interfaces) {
328 Interface *iface_obj = obj->interfaces->data;
329 obj->interfaces = g_slist_delete_link(obj->interfaces, obj->interfaces);
330 object_delete(OBJECT(iface_obj));
333 if (type_has_parent(type)) {
334 object_deinit(obj, type_get_parent(type));
337 object_unparent(obj);
340 void object_finalize(void *data)
343 TypeImpl *ti = obj->class->type;
345 object_deinit(obj, ti);
346 object_property_del_all(obj);
348 g_assert(obj->ref == 0);
351 Object *object_new_with_type(Type type)
355 g_assert(type != NULL);
357 obj = g_malloc(type->instance_size);
358 object_initialize_with_type(obj, type);
364 Object *object_new(const char *typename)
366 TypeImpl *ti = type_get_by_name(typename);
368 return object_new_with_type(ti);
371 void object_delete(Object *obj)
374 g_assert(obj->ref == 0);
378 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
382 /* Check if typename is a direct ancestor of type */
384 if (type == target_type) {
388 type = type_get_parent(type);
394 static bool object_is_type(Object *obj, TypeImpl *target_type)
396 return !target_type || type_is_ancestor(obj->class->type, target_type);
399 Object *object_dynamic_cast(Object *obj, const char *typename)
401 TypeImpl *target_type = type_get_by_name(typename);
404 /* Check if typename is a direct ancestor. Special-case TYPE_OBJECT,
405 * we want to go back from interfaces to the parent.
407 if (target_type && object_is_type(obj, target_type)) {
411 /* Check if obj is an interface and its containing object is a direct
412 * ancestor of typename. In principle we could do this test at the very
413 * beginning of object_dynamic_cast, avoiding a second call to
414 * object_is_type. However, casting between interfaces is relatively
415 * rare, and object_is_type(obj, type_interface) would fail almost always.
417 * Perhaps we could add a magic value to the object header for increased
418 * (run-time) type safety and to speed up tests like this one. If we ever
419 * do that we can revisit the order here.
421 if (object_is_type(obj, type_interface)) {
422 assert(!obj->interfaces);
423 obj = INTERFACE(obj)->obj;
424 if (object_is_type(obj, target_type)) {
433 /* Check if obj has an interface of typename */
434 for (i = obj->interfaces; i; i = i->next) {
435 Interface *iface = i->data;
437 if (object_is_type(OBJECT(iface), target_type)) {
438 return OBJECT(iface);
446 static void register_types(void)
448 static TypeInfo interface_info = {
449 .name = TYPE_INTERFACE,
450 .instance_size = sizeof(Interface),
454 type_interface = type_register_static(&interface_info);
457 type_init(register_types)
459 Object *object_dynamic_cast_assert(Object *obj, const char *typename)
463 inst = object_dynamic_cast(obj, typename);
466 fprintf(stderr, "Object %p is not an instance of type %s\n",
474 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
475 const char *typename)
477 TypeImpl *target_type = type_get_by_name(typename);
478 TypeImpl *type = class->type;
481 if (type == target_type) {
485 type = type_get_parent(type);
491 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
492 const char *typename)
494 ObjectClass *ret = object_class_dynamic_cast(class, typename);
497 fprintf(stderr, "Object %p is not an instance of type %s\n",
505 const char *object_get_typename(Object *obj)
507 return obj->class->type->name;
510 ObjectClass *object_get_class(Object *obj)
515 const char *object_class_get_name(ObjectClass *klass)
517 return klass->type->name;
520 ObjectClass *object_class_by_name(const char *typename)
522 TypeImpl *type = type_get_by_name(typename);
528 type_class_init(type);
533 typedef struct OCFData
535 void (*fn)(ObjectClass *klass, void *opaque);
536 const char *implements_type;
537 bool include_abstract;
541 static void object_class_foreach_tramp(gpointer key, gpointer value,
544 OCFData *data = opaque;
545 TypeImpl *type = value;
548 type_class_init(type);
551 if (!data->include_abstract && type->abstract) {
555 if (data->implements_type &&
556 !object_class_dynamic_cast(k, data->implements_type)) {
560 data->fn(k, data->opaque);
563 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
564 const char *implements_type, bool include_abstract,
567 OCFData data = { fn, implements_type, include_abstract, opaque };
569 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
572 void object_ref(Object *obj)
577 void object_unref(Object *obj)
579 g_assert(obj->ref > 0);
582 /* parent always holds a reference to its children */
584 object_finalize(obj);
588 void object_property_add(Object *obj, const char *name, const char *type,
589 ObjectPropertyAccessor *get,
590 ObjectPropertyAccessor *set,
591 ObjectPropertyRelease *release,
592 void *opaque, Error **errp)
594 ObjectProperty *prop = g_malloc0(sizeof(*prop));
596 prop->name = g_strdup(name);
597 prop->type = g_strdup(type);
601 prop->release = release;
602 prop->opaque = opaque;
604 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
607 static ObjectProperty *object_property_find(Object *obj, const char *name)
609 ObjectProperty *prop;
611 QTAILQ_FOREACH(prop, &obj->properties, node) {
612 if (strcmp(prop->name, name) == 0) {
620 void object_property_del(Object *obj, const char *name, Error **errp)
622 ObjectProperty *prop = object_property_find(obj, name);
624 QTAILQ_REMOVE(&obj->properties, prop, node);
626 prop->release(obj, prop->name, prop->opaque);
633 void object_property_get(Object *obj, Visitor *v, const char *name,
636 ObjectProperty *prop = object_property_find(obj, name);
639 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
644 error_set(errp, QERR_PERMISSION_DENIED);
646 prop->get(obj, v, prop->opaque, name, errp);
650 void object_property_set(Object *obj, Visitor *v, const char *name,
653 ObjectProperty *prop = object_property_find(obj, name);
656 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
661 error_set(errp, QERR_PERMISSION_DENIED);
663 prop->set(obj, v, prop->opaque, name, errp);
667 void object_property_set_str(Object *obj, const char *value,
668 const char *name, Error **errp)
670 QString *qstr = qstring_from_str(value);
671 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
676 char *object_property_get_str(Object *obj, const char *name,
679 QObject *ret = object_property_get_qobject(obj, name, errp);
686 qstring = qobject_to_qstring(ret);
688 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
691 retval = g_strdup(qstring_get_str(qstring));
698 void object_property_set_link(Object *obj, Object *value,
699 const char *name, Error **errp)
701 object_property_set_str(obj, object_get_canonical_path(value),
705 Object *object_property_get_link(Object *obj, const char *name,
708 char *str = object_property_get_str(obj, name, errp);
709 Object *target = NULL;
712 target = object_resolve_path(str, NULL);
714 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
722 void object_property_set_bool(Object *obj, bool value,
723 const char *name, Error **errp)
725 QBool *qbool = qbool_from_int(value);
726 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
731 bool object_property_get_bool(Object *obj, const char *name,
734 QObject *ret = object_property_get_qobject(obj, name, errp);
741 qbool = qobject_to_qbool(ret);
743 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
746 retval = qbool_get_int(qbool);
753 void object_property_set_int(Object *obj, int64_t value,
754 const char *name, Error **errp)
756 QInt *qint = qint_from_int(value);
757 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
762 int64_t object_property_get_int(Object *obj, const char *name,
765 QObject *ret = object_property_get_qobject(obj, name, errp);
772 qint = qobject_to_qint(ret);
774 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
777 retval = qint_get_int(qint);
784 void object_property_parse(Object *obj, const char *string,
785 const char *name, Error **errp)
787 StringInputVisitor *mi;
788 mi = string_input_visitor_new(string);
789 object_property_set(obj, string_input_get_visitor(mi), name, errp);
791 string_input_visitor_cleanup(mi);
794 char *object_property_print(Object *obj, const char *name,
797 StringOutputVisitor *mo;
800 mo = string_output_visitor_new();
801 object_property_get(obj, string_output_get_visitor(mo), name, NULL);
802 string = string_output_get_string(mo);
803 string_output_visitor_cleanup(mo);
807 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
809 ObjectProperty *prop = object_property_find(obj, name);
812 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
819 Object *object_get_root(void)
824 root = object_new("container");
830 static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
831 const char *name, Error **errp)
833 Object *child = opaque;
836 path = object_get_canonical_path(child);
837 visit_type_str(v, &path, name, errp);
841 static void object_finalize_child_property(Object *obj, const char *name,
844 Object *child = opaque;
849 void object_property_add_child(Object *obj, const char *name,
850 Object *child, Error **errp)
854 /* Registering an interface object in the composition tree will mightily
855 * confuse object_get_canonical_path (which, on the other hand, knows how
856 * to get the canonical path of an interface object).
858 assert(!object_is_type(obj, type_interface));
860 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
862 object_property_add(obj, name, type, object_get_child_property,
863 NULL, object_finalize_child_property, child, errp);
866 g_assert(child->parent == NULL);
872 static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
873 const char *name, Error **errp)
875 Object **child = opaque;
879 path = object_get_canonical_path(*child);
880 visit_type_str(v, &path, name, errp);
884 visit_type_str(v, &path, name, errp);
888 static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
889 const char *name, Error **errp)
891 Object **child = opaque;
893 bool ambiguous = false;
898 type = object_property_get_type(obj, name, NULL);
900 visit_type_str(v, &path, name, errp);
905 if (strcmp(path, "") != 0) {
908 /* Go from link<FOO> to FOO. */
909 target_type = g_strndup(&type[5], strlen(type) - 6);
910 target = object_resolve_path_type(path, target_type, &ambiguous);
913 error_set(errp, QERR_AMBIGUOUS_PATH, path);
918 target = object_resolve_path(path, &ambiguous);
919 if (target || ambiguous) {
920 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
922 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
930 if (old_target != NULL) {
931 object_unref(old_target);
935 void object_property_add_link(Object *obj, const char *name,
936 const char *type, Object **child,
941 full_type = g_strdup_printf("link<%s>", type);
943 object_property_add(obj, name, full_type,
944 object_get_link_property,
945 object_set_link_property,
951 gchar *object_get_canonical_path(Object *obj)
953 Object *root = object_get_root();
954 char *newpath = NULL, *path = NULL;
956 if (object_is_type(obj, type_interface)) {
957 obj = INTERFACE(obj)->obj;
960 while (obj != root) {
961 ObjectProperty *prop = NULL;
963 g_assert(obj->parent != NULL);
965 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
966 if (!strstart(prop->type, "child<", NULL)) {
970 if (prop->opaque == obj) {
972 newpath = g_strdup_printf("%s/%s", prop->name, path);
976 path = g_strdup(prop->name);
982 g_assert(prop != NULL);
987 newpath = g_strdup_printf("/%s", path);
993 static Object *object_resolve_abs_path(Object *parent,
995 const char *typename,
998 ObjectProperty *prop;
1001 if (parts[index] == NULL) {
1002 return object_dynamic_cast(parent, typename);
1005 if (strcmp(parts[index], "") == 0) {
1006 return object_resolve_abs_path(parent, parts, typename, index + 1);
1009 prop = object_property_find(parent, parts[index]);
1015 if (strstart(prop->type, "link<", NULL)) {
1016 Object **pchild = prop->opaque;
1020 } else if (strstart(prop->type, "child<", NULL)) {
1021 child = prop->opaque;
1028 return object_resolve_abs_path(child, parts, typename, index + 1);
1031 static Object *object_resolve_partial_path(Object *parent,
1033 const char *typename,
1037 ObjectProperty *prop;
1039 obj = object_resolve_abs_path(parent, parts, typename, 0);
1041 QTAILQ_FOREACH(prop, &parent->properties, node) {
1044 if (!strstart(prop->type, "child<", NULL)) {
1048 found = object_resolve_partial_path(prop->opaque, parts,
1049 typename, ambiguous);
1060 if (ambiguous && *ambiguous) {
1068 Object *object_resolve_path_type(const char *path, const char *typename,
1071 bool partial_path = true;
1075 parts = g_strsplit(path, "/", 0);
1076 if (parts == NULL || parts[0] == NULL) {
1078 return object_get_root();
1081 if (strcmp(parts[0], "") == 0) {
1082 partial_path = false;
1089 obj = object_resolve_partial_path(object_get_root(), parts,
1090 typename, ambiguous);
1092 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
1100 Object *object_resolve_path(const char *path, bool *ambiguous)
1102 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1105 typedef struct StringProperty
1107 char *(*get)(Object *, Error **);
1108 void (*set)(Object *, const char *, Error **);
1111 static void property_get_str(Object *obj, Visitor *v, void *opaque,
1112 const char *name, Error **errp)
1114 StringProperty *prop = opaque;
1117 value = prop->get(obj, errp);
1119 visit_type_str(v, &value, name, errp);
1124 static void property_set_str(Object *obj, Visitor *v, void *opaque,
1125 const char *name, Error **errp)
1127 StringProperty *prop = opaque;
1129 Error *local_err = NULL;
1131 visit_type_str(v, &value, name, &local_err);
1133 error_propagate(errp, local_err);
1137 prop->set(obj, value, errp);
1141 static void property_release_str(Object *obj, const char *name,
1144 StringProperty *prop = opaque;
1148 void object_property_add_str(Object *obj, const char *name,
1149 char *(*get)(Object *, Error **),
1150 void (*set)(Object *, const char *, Error **),
1153 StringProperty *prop = g_malloc0(sizeof(*prop));
1158 object_property_add(obj, name, "string",
1159 get ? property_get_str : NULL,
1160 set ? property_set_str : NULL,
1161 property_release_str,