qom: accept any compatible type when setting a link property
[qemu/kevin.git] / qom / object.c
blob49addefd62fcc00d306156e4eeada908a97e0161
1 /*
2 * QEMU Object Model
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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 "hw/qdev.h"
17 // FIXME remove above
19 #define MAX_INTERFACES 32
21 typedef struct InterfaceImpl InterfaceImpl;
22 typedef struct TypeImpl TypeImpl;
24 struct InterfaceImpl
26 const char *parent;
27 void (*interface_initfn)(ObjectClass *class, void *data);
28 TypeImpl *type;
31 struct TypeImpl
33 const char *name;
35 size_t class_size;
37 size_t instance_size;
39 void (*class_init)(ObjectClass *klass, void *data);
40 void (*class_finalize)(ObjectClass *klass, void *data);
42 void *class_data;
44 void (*instance_init)(Object *obj);
45 void (*instance_finalize)(Object *obj);
47 bool abstract;
49 const char *parent;
50 TypeImpl *parent_type;
52 ObjectClass *class;
54 int num_interfaces;
55 InterfaceImpl interfaces[MAX_INTERFACES];
58 typedef struct Interface
60 Object parent;
61 Object *obj;
62 } Interface;
64 #define INTERFACE(obj) OBJECT_CHECK(Interface, obj, TYPE_INTERFACE)
66 static GHashTable *type_table_get(void)
68 static GHashTable *type_table;
70 if (type_table == NULL) {
71 type_table = g_hash_table_new(g_str_hash, g_str_equal);
74 return type_table;
77 static void type_table_add(TypeImpl *ti)
79 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
82 static TypeImpl *type_table_lookup(const char *name)
84 return g_hash_table_lookup(type_table_get(), name);
87 TypeImpl *type_register(const TypeInfo *info)
89 TypeImpl *ti = g_malloc0(sizeof(*ti));
91 g_assert(info->name != NULL);
93 if (type_table_lookup(info->name) != NULL) {
94 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
95 abort();
98 ti->name = g_strdup(info->name);
99 ti->parent = g_strdup(info->parent);
101 ti->class_size = info->class_size;
102 ti->instance_size = info->instance_size;
104 ti->class_init = info->class_init;
105 ti->class_finalize = info->class_finalize;
106 ti->class_data = info->class_data;
108 ti->instance_init = info->instance_init;
109 ti->instance_finalize = info->instance_finalize;
111 ti->abstract = info->abstract;
113 if (info->interfaces) {
114 int i;
116 for (i = 0; info->interfaces[i].type; i++) {
117 ti->interfaces[i].parent = info->interfaces[i].type;
118 ti->interfaces[i].interface_initfn = info->interfaces[i].interface_initfn;
119 ti->num_interfaces++;
123 type_table_add(ti);
125 return ti;
128 TypeImpl *type_register_static(const TypeInfo *info)
130 return type_register(info);
133 static TypeImpl *type_get_by_name(const char *name)
135 if (name == NULL) {
136 return NULL;
139 return type_table_lookup(name);
142 static TypeImpl *type_get_parent(TypeImpl *type)
144 if (!type->parent_type && type->parent) {
145 type->parent_type = type_get_by_name(type->parent);
146 g_assert(type->parent_type != NULL);
149 return type->parent_type;
152 static bool type_has_parent(TypeImpl *type)
154 return (type->parent != NULL);
157 static size_t type_class_get_size(TypeImpl *ti)
159 if (ti->class_size) {
160 return ti->class_size;
163 if (type_has_parent(ti)) {
164 return type_class_get_size(type_get_parent(ti));
167 return sizeof(ObjectClass);
170 static void type_class_interface_init(TypeImpl *ti, InterfaceImpl *iface)
172 TypeInfo info = {
173 .instance_size = sizeof(Interface),
174 .parent = iface->parent,
175 .class_size = sizeof(InterfaceClass),
176 .class_init = iface->interface_initfn,
177 .abstract = true,
179 char *name = g_strdup_printf("<%s::%s>", ti->name, iface->parent);
181 info.name = name;
182 iface->type = type_register(&info);
183 g_free(name);
186 static void type_class_init(TypeImpl *ti)
188 size_t class_size = sizeof(ObjectClass);
189 int i;
191 if (ti->class) {
192 return;
195 ti->class_size = type_class_get_size(ti);
197 ti->class = g_malloc0(ti->class_size);
198 ti->class->type = ti;
200 if (type_has_parent(ti)) {
201 TypeImpl *parent = type_get_parent(ti);
203 type_class_init(parent);
205 class_size = parent->class_size;
206 g_assert(parent->class_size <= ti->class_size);
208 memcpy((void *)ti->class + sizeof(ObjectClass),
209 (void *)parent->class + sizeof(ObjectClass),
210 parent->class_size - sizeof(ObjectClass));
213 memset((void *)ti->class + class_size, 0, ti->class_size - class_size);
215 for (i = 0; i < ti->num_interfaces; i++) {
216 type_class_interface_init(ti, &ti->interfaces[i]);
219 if (ti->class_init) {
220 ti->class_init(ti->class, ti->class_data);
224 static void object_interface_init(Object *obj, InterfaceImpl *iface)
226 TypeImpl *ti = iface->type;
227 Interface *iface_obj;
229 iface_obj = INTERFACE(object_new(ti->name));
230 iface_obj->obj = obj;
232 obj->interfaces = g_slist_prepend(obj->interfaces, iface_obj);
235 static void object_init_with_type(Object *obj, TypeImpl *ti)
237 int i;
239 if (type_has_parent(ti)) {
240 object_init_with_type(obj, type_get_parent(ti));
243 for (i = 0; i < ti->num_interfaces; i++) {
244 object_interface_init(obj, &ti->interfaces[i]);
247 if (ti->instance_init) {
248 ti->instance_init(obj);
252 void object_initialize_with_type(void *data, TypeImpl *type)
254 Object *obj = data;
256 g_assert(type != NULL);
257 g_assert(type->instance_size >= sizeof(ObjectClass));
259 type_class_init(type);
260 g_assert(type->abstract == false);
262 memset(obj, 0, type->instance_size);
263 obj->class = type->class;
264 QTAILQ_INIT(&obj->properties);
265 object_init_with_type(obj, type);
268 void object_initialize(void *data, const char *typename)
270 TypeImpl *type = type_get_by_name(typename);
272 object_initialize_with_type(data, type);
275 static void object_property_del_all(Object *obj)
277 while (!QTAILQ_EMPTY(&obj->properties)) {
278 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
280 QTAILQ_REMOVE(&obj->properties, prop, node);
282 if (prop->release) {
283 prop->release(obj, prop->name, prop->opaque);
286 g_free(prop->name);
287 g_free(prop->type);
288 g_free(prop);
292 static void object_property_del_child(Object *obj, Object *child, Error **errp)
294 ObjectProperty *prop;
296 QTAILQ_FOREACH(prop, &obj->properties, node) {
297 if (!strstart(prop->type, "child<", NULL)) {
298 continue;
301 if (prop->opaque == child) {
302 object_property_del(obj, prop->name, errp);
307 void object_unparent(Object *obj)
309 if (obj->parent) {
310 object_property_del_child(obj->parent, obj, NULL);
314 static void object_deinit(Object *obj, TypeImpl *type)
316 if (type->instance_finalize) {
317 type->instance_finalize(obj);
320 while (obj->interfaces) {
321 Interface *iface_obj = obj->interfaces->data;
322 obj->interfaces = g_slist_delete_link(obj->interfaces, obj->interfaces);
323 object_delete(OBJECT(iface_obj));
326 if (type_has_parent(type)) {
327 object_deinit(obj, type_get_parent(type));
330 object_unparent(obj);
333 void object_finalize(void *data)
335 Object *obj = data;
336 TypeImpl *ti = obj->class->type;
338 object_deinit(obj, ti);
339 object_property_del_all(obj);
342 Object *object_new_with_type(Type type)
344 Object *obj;
346 g_assert(type != NULL);
348 obj = g_malloc(type->instance_size);
349 object_initialize_with_type(obj, type);
351 return obj;
354 Object *object_new(const char *typename)
356 TypeImpl *ti = type_get_by_name(typename);
358 return object_new_with_type(ti);
361 void object_delete(Object *obj)
363 object_finalize(obj);
364 g_free(obj);
367 static bool object_is_type(Object *obj, const char *typename)
369 TypeImpl *target_type = type_get_by_name(typename);
370 TypeImpl *type = obj->class->type;
371 GSList *i;
373 /* Check if typename is a direct ancestor of type */
374 while (type) {
375 if (type == target_type) {
376 return true;
379 type = type_get_parent(type);
382 /* Check if obj has an interface of typename */
383 for (i = obj->interfaces; i; i = i->next) {
384 Interface *iface = i->data;
386 if (object_is_type(OBJECT(iface), typename)) {
387 return true;
391 return false;
394 Object *object_dynamic_cast(Object *obj, const char *typename)
396 GSList *i;
398 /* Check if typename is a direct ancestor */
399 if (object_is_type(obj, typename)) {
400 return obj;
403 /* Check if obj has an interface of typename */
404 for (i = obj->interfaces; i; i = i->next) {
405 Interface *iface = i->data;
407 if (object_is_type(OBJECT(iface), typename)) {
408 return OBJECT(iface);
412 /* Check if obj is an interface and its containing object is a direct
413 * ancestor of typename */
414 if (object_is_type(obj, TYPE_INTERFACE)) {
415 Interface *iface = INTERFACE(obj);
417 if (object_is_type(iface->obj, typename)) {
418 return iface->obj;
422 return NULL;
426 static void register_interface(void)
428 static TypeInfo interface_info = {
429 .name = TYPE_INTERFACE,
430 .instance_size = sizeof(Interface),
431 .abstract = true,
434 type_register_static(&interface_info);
437 device_init(register_interface);
439 Object *object_dynamic_cast_assert(Object *obj, const char *typename)
441 Object *inst;
443 inst = object_dynamic_cast(obj, typename);
445 if (!inst) {
446 fprintf(stderr, "Object %p is not an instance of type %s\n",
447 obj, typename);
448 abort();
451 return inst;
454 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
455 const char *typename)
457 TypeImpl *target_type = type_get_by_name(typename);
458 TypeImpl *type = class->type;
460 while (type) {
461 if (type == target_type) {
462 return class;
465 type = type_get_parent(type);
468 return NULL;
471 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
472 const char *typename)
474 ObjectClass *ret = object_class_dynamic_cast(class, typename);
476 if (!ret) {
477 fprintf(stderr, "Object %p is not an instance of type %s\n",
478 class, typename);
479 abort();
482 return ret;
485 const char *object_get_typename(Object *obj)
487 return obj->class->type->name;
490 ObjectClass *object_get_class(Object *obj)
492 return obj->class;
495 const char *object_class_get_name(ObjectClass *klass)
497 return klass->type->name;
500 ObjectClass *object_class_by_name(const char *typename)
502 TypeImpl *type = type_get_by_name(typename);
504 if (!type) {
505 return NULL;
508 type_class_init(type);
510 return type->class;
513 typedef struct OCFData
515 void (*fn)(ObjectClass *klass, void *opaque);
516 const char *implements_type;
517 bool include_abstract;
518 void *opaque;
519 } OCFData;
521 static void object_class_foreach_tramp(gpointer key, gpointer value,
522 gpointer opaque)
524 OCFData *data = opaque;
525 TypeImpl *type = value;
526 ObjectClass *k;
528 type_class_init(type);
529 k = type->class;
531 if (!data->include_abstract && type->abstract) {
532 return;
535 if (data->implements_type &&
536 !object_class_dynamic_cast(k, data->implements_type)) {
537 return;
540 data->fn(k, data->opaque);
543 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
544 const char *implements_type, bool include_abstract,
545 void *opaque)
547 OCFData data = { fn, implements_type, include_abstract, opaque };
549 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
552 void object_ref(Object *obj)
554 obj->ref++;
557 void object_unref(Object *obj)
559 g_assert(obj->ref > 0);
560 obj->ref--;
562 /* parent always holds a reference to its children */
563 if (obj->ref == 0) {
564 object_finalize(obj);
568 void object_property_add(Object *obj, const char *name, const char *type,
569 ObjectPropertyAccessor *get,
570 ObjectPropertyAccessor *set,
571 ObjectPropertyRelease *release,
572 void *opaque, Error **errp)
574 ObjectProperty *prop = g_malloc0(sizeof(*prop));
576 prop->name = g_strdup(name);
577 prop->type = g_strdup(type);
579 prop->get = get;
580 prop->set = set;
581 prop->release = release;
582 prop->opaque = opaque;
584 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
587 static ObjectProperty *object_property_find(Object *obj, const char *name)
589 ObjectProperty *prop;
591 QTAILQ_FOREACH(prop, &obj->properties, node) {
592 if (strcmp(prop->name, name) == 0) {
593 return prop;
597 return NULL;
600 void object_property_del(Object *obj, const char *name, Error **errp)
602 ObjectProperty *prop = object_property_find(obj, name);
604 QTAILQ_REMOVE(&obj->properties, prop, node);
606 prop->release(obj, prop->name, prop->opaque);
608 g_free(prop->name);
609 g_free(prop->type);
610 g_free(prop);
613 void object_property_get(Object *obj, Visitor *v, const char *name,
614 Error **errp)
616 ObjectProperty *prop = object_property_find(obj, name);
618 if (prop == NULL) {
619 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
620 return;
623 if (!prop->get) {
624 error_set(errp, QERR_PERMISSION_DENIED);
625 } else {
626 prop->get(obj, v, prop->opaque, name, errp);
630 void object_property_set(Object *obj, Visitor *v, const char *name,
631 Error **errp)
633 ObjectProperty *prop = object_property_find(obj, name);
635 if (prop == NULL) {
636 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
637 return;
640 if (!prop->set) {
641 error_set(errp, QERR_PERMISSION_DENIED);
642 } else {
643 prop->set(obj, v, prop->opaque, name, errp);
647 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
649 ObjectProperty *prop = object_property_find(obj, name);
651 if (prop == NULL) {
652 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
653 return NULL;
656 return prop->type;
659 Object *object_get_root(void)
661 static DeviceState *object_root;
663 if (!object_root) {
664 object_root = qdev_create(NULL, "container");
665 qdev_init_nofail(object_root);
668 return OBJECT(object_root);
671 static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
672 const char *name, Error **errp)
674 Object *child = opaque;
675 gchar *path;
677 path = object_get_canonical_path(child);
678 visit_type_str(v, &path, name, errp);
679 g_free(path);
682 void object_property_add_child(Object *obj, const char *name,
683 Object *child, Error **errp)
685 gchar *type;
687 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
689 object_property_add(obj, name, type, object_get_child_property,
690 NULL, NULL, child, errp);
692 object_ref(child);
693 g_assert(child->parent == NULL);
694 child->parent = obj;
696 g_free(type);
699 static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
700 const char *name, Error **errp)
702 Object **child = opaque;
703 gchar *path;
705 if (*child) {
706 path = object_get_canonical_path(*child);
707 visit_type_str(v, &path, name, errp);
708 g_free(path);
709 } else {
710 path = (gchar *)"";
711 visit_type_str(v, &path, name, errp);
715 static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
716 const char *name, Error **errp)
718 Object **child = opaque;
719 bool ambiguous = false;
720 const char *type;
721 char *path;
723 type = object_property_get_type(obj, name, NULL);
725 visit_type_str(v, &path, name, errp);
727 if (*child) {
728 object_unref(*child);
731 if (strcmp(path, "") != 0) {
732 Object *target;
734 target = object_resolve_path(path, &ambiguous);
735 if (target) {
736 gchar *target_type;
738 target_type = g_strdup(&type[5]);
739 target_type[strlen(target_type) - 2] = 0;
741 if (object_dynamic_cast(target, target_type)) {
742 object_ref(target);
743 *child = target;
744 } else {
745 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, type);
748 g_free(target_type);
749 } else {
750 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
752 } else {
753 *child = NULL;
756 g_free(path);
759 void object_property_add_link(Object *obj, const char *name,
760 const char *type, Object **child,
761 Error **errp)
763 gchar *full_type;
765 full_type = g_strdup_printf("link<%s>", type);
767 object_property_add(obj, name, full_type,
768 object_get_link_property,
769 object_set_link_property,
770 NULL, child, errp);
772 g_free(full_type);
775 gchar *object_get_canonical_path(Object *obj)
777 Object *root = object_get_root();
778 char *newpath = NULL, *path = NULL;
780 while (obj != root) {
781 ObjectProperty *prop = NULL;
783 g_assert(obj->parent != NULL);
785 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
786 if (!strstart(prop->type, "child<", NULL)) {
787 continue;
790 if (prop->opaque == obj) {
791 if (path) {
792 newpath = g_strdup_printf("%s/%s", prop->name, path);
793 g_free(path);
794 path = newpath;
795 } else {
796 path = g_strdup(prop->name);
798 break;
802 g_assert(prop != NULL);
804 obj = obj->parent;
807 newpath = g_strdup_printf("/%s", path);
808 g_free(path);
810 return newpath;
813 static Object *object_resolve_abs_path(Object *parent,
814 gchar **parts,
815 int index)
817 ObjectProperty *prop;
818 Object *child;
820 if (parts[index] == NULL) {
821 return parent;
824 if (strcmp(parts[index], "") == 0) {
825 return object_resolve_abs_path(parent, parts, index + 1);
828 prop = object_property_find(parent, parts[index]);
829 if (prop == NULL) {
830 return NULL;
833 child = NULL;
834 if (strstart(prop->type, "link<", NULL)) {
835 Object **pchild = prop->opaque;
836 if (*pchild) {
837 child = *pchild;
839 } else if (strstart(prop->type, "child<", NULL)) {
840 child = prop->opaque;
843 if (!child) {
844 return NULL;
847 return object_resolve_abs_path(child, parts, index + 1);
850 static Object *object_resolve_partial_path(Object *parent,
851 gchar **parts,
852 bool *ambiguous)
854 Object *obj;
855 ObjectProperty *prop;
857 obj = object_resolve_abs_path(parent, parts, 0);
859 QTAILQ_FOREACH(prop, &parent->properties, node) {
860 Object *found;
862 if (!strstart(prop->type, "child<", NULL)) {
863 continue;
866 found = object_resolve_partial_path(prop->opaque, parts, ambiguous);
867 if (found) {
868 if (obj) {
869 if (ambiguous) {
870 *ambiguous = true;
872 return NULL;
874 obj = found;
877 if (ambiguous && *ambiguous) {
878 return NULL;
882 return obj;
885 Object *object_resolve_path(const char *path, bool *ambiguous)
887 bool partial_path = true;
888 Object *obj;
889 gchar **parts;
891 parts = g_strsplit(path, "/", 0);
892 if (parts == NULL || parts[0] == NULL) {
893 g_strfreev(parts);
894 return object_get_root();
897 if (strcmp(parts[0], "") == 0) {
898 partial_path = false;
901 if (partial_path) {
902 if (ambiguous) {
903 *ambiguous = false;
905 obj = object_resolve_partial_path(object_get_root(), parts, ambiguous);
906 } else {
907 obj = object_resolve_abs_path(object_get_root(), parts, 1);
910 g_strfreev(parts);
912 return obj;
915 typedef struct StringProperty
917 char *(*get)(Object *, Error **);
918 void (*set)(Object *, const char *, Error **);
919 } StringProperty;
921 static void object_property_get_str(Object *obj, Visitor *v, void *opaque,
922 const char *name, Error **errp)
924 StringProperty *prop = opaque;
925 char *value;
927 value = prop->get(obj, errp);
928 if (value) {
929 visit_type_str(v, &value, name, errp);
930 g_free(value);
934 static void object_property_set_str(Object *obj, Visitor *v, void *opaque,
935 const char *name, Error **errp)
937 StringProperty *prop = opaque;
938 char *value;
939 Error *local_err = NULL;
941 visit_type_str(v, &value, name, &local_err);
942 if (local_err) {
943 error_propagate(errp, local_err);
944 return;
947 prop->set(obj, value, errp);
948 g_free(value);
951 static void object_property_release_str(Object *obj, const char *name,
952 void *opaque)
954 StringProperty *prop = opaque;
955 g_free(prop);
958 void object_property_add_str(Object *obj, const char *name,
959 char *(*get)(Object *, Error **),
960 void (*set)(Object *, const char *, Error **),
961 Error **errp)
963 StringProperty *prop = g_malloc0(sizeof(*prop));
965 prop->get = get;
966 prop->set = set;
968 object_property_add(obj, name, "string",
969 get ? object_property_get_str : NULL,
970 set ? object_property_set_str : NULL,
971 object_property_release_str,
972 prop, errp);