qerror.h: Remove QERR defines that are only used once
[qemu.git] / qom / object.c
blobe42b25430398920f7b160da0302ad446a889c051
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 "qom/object.h"
14 #include "qemu-common.h"
15 #include "qapi/visitor.h"
16 #include "qapi/string-input-visitor.h"
17 #include "qapi/string-output-visitor.h"
18 #include "qapi/qmp/qerror.h"
19 #include "trace.h"
21 /* TODO: replace QObject with a simpler visitor to avoid a dependency
22 * of the QOM core on QObject? */
23 #include "qom/qom-qobject.h"
24 #include "qapi/qmp/qobject.h"
25 #include "qapi/qmp/qbool.h"
26 #include "qapi/qmp/qint.h"
27 #include "qapi/qmp/qstring.h"
29 #define MAX_INTERFACES 32
31 typedef struct InterfaceImpl InterfaceImpl;
32 typedef struct TypeImpl TypeImpl;
34 struct InterfaceImpl
36 const char *typename;
39 struct TypeImpl
41 const char *name;
43 size_t class_size;
45 size_t instance_size;
47 void (*class_init)(ObjectClass *klass, void *data);
48 void (*class_base_init)(ObjectClass *klass, void *data);
49 void (*class_finalize)(ObjectClass *klass, void *data);
51 void *class_data;
53 void (*instance_init)(Object *obj);
54 void (*instance_post_init)(Object *obj);
55 void (*instance_finalize)(Object *obj);
57 bool abstract;
59 const char *parent;
60 TypeImpl *parent_type;
62 ObjectClass *class;
64 int num_interfaces;
65 InterfaceImpl interfaces[MAX_INTERFACES];
68 static Type type_interface;
70 static GHashTable *type_table_get(void)
72 static GHashTable *type_table;
74 if (type_table == NULL) {
75 type_table = g_hash_table_new(g_str_hash, g_str_equal);
78 return type_table;
81 static bool enumerating_types;
83 static void type_table_add(TypeImpl *ti)
85 assert(!enumerating_types);
86 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
89 static TypeImpl *type_table_lookup(const char *name)
91 return g_hash_table_lookup(type_table_get(), name);
94 static TypeImpl *type_new(const TypeInfo *info)
96 TypeImpl *ti = g_malloc0(sizeof(*ti));
97 int i;
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);
103 abort();
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_base_init = info->class_base_init;
114 ti->class_finalize = info->class_finalize;
115 ti->class_data = info->class_data;
117 ti->instance_init = info->instance_init;
118 ti->instance_post_init = info->instance_post_init;
119 ti->instance_finalize = info->instance_finalize;
121 ti->abstract = info->abstract;
123 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
124 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
126 ti->num_interfaces = i;
128 return ti;
131 static TypeImpl *type_register_internal(const TypeInfo *info)
133 TypeImpl *ti;
134 ti = type_new(info);
136 type_table_add(ti);
137 return ti;
140 TypeImpl *type_register(const TypeInfo *info)
142 assert(info->parent);
143 return type_register_internal(info);
146 TypeImpl *type_register_static(const TypeInfo *info)
148 return type_register(info);
151 static TypeImpl *type_get_by_name(const char *name)
153 if (name == NULL) {
154 return NULL;
157 return type_table_lookup(name);
160 static TypeImpl *type_get_parent(TypeImpl *type)
162 if (!type->parent_type && type->parent) {
163 type->parent_type = type_get_by_name(type->parent);
164 g_assert(type->parent_type != NULL);
167 return type->parent_type;
170 static bool type_has_parent(TypeImpl *type)
172 return (type->parent != NULL);
175 static size_t type_class_get_size(TypeImpl *ti)
177 if (ti->class_size) {
178 return ti->class_size;
181 if (type_has_parent(ti)) {
182 return type_class_get_size(type_get_parent(ti));
185 return sizeof(ObjectClass);
188 static size_t type_object_get_size(TypeImpl *ti)
190 if (ti->instance_size) {
191 return ti->instance_size;
194 if (type_has_parent(ti)) {
195 return type_object_get_size(type_get_parent(ti));
198 return 0;
201 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
203 assert(target_type);
205 /* Check if typename is a direct ancestor of type */
206 while (type) {
207 if (type == target_type) {
208 return true;
211 type = type_get_parent(type);
214 return false;
217 static void type_initialize(TypeImpl *ti);
219 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
220 TypeImpl *parent_type)
222 InterfaceClass *new_iface;
223 TypeInfo info = { };
224 TypeImpl *iface_impl;
226 info.parent = parent_type->name;
227 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
228 info.abstract = true;
230 iface_impl = type_new(&info);
231 iface_impl->parent_type = parent_type;
232 type_initialize(iface_impl);
233 g_free((char *)info.name);
235 new_iface = (InterfaceClass *)iface_impl->class;
236 new_iface->concrete_class = ti->class;
237 new_iface->interface_type = interface_type;
239 ti->class->interfaces = g_slist_append(ti->class->interfaces,
240 iface_impl->class);
243 static void type_initialize(TypeImpl *ti)
245 TypeImpl *parent;
247 if (ti->class) {
248 return;
251 ti->class_size = type_class_get_size(ti);
252 ti->instance_size = type_object_get_size(ti);
254 ti->class = g_malloc0(ti->class_size);
256 parent = type_get_parent(ti);
257 if (parent) {
258 type_initialize(parent);
259 GSList *e;
260 int i;
262 g_assert(parent->class_size <= ti->class_size);
263 memcpy(ti->class, parent->class, parent->class_size);
264 ti->class->interfaces = NULL;
266 for (e = parent->class->interfaces; e; e = e->next) {
267 InterfaceClass *iface = e->data;
268 ObjectClass *klass = OBJECT_CLASS(iface);
270 type_initialize_interface(ti, iface->interface_type, klass->type);
273 for (i = 0; i < ti->num_interfaces; i++) {
274 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
275 for (e = ti->class->interfaces; e; e = e->next) {
276 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
278 if (type_is_ancestor(target_type, t)) {
279 break;
283 if (e) {
284 continue;
287 type_initialize_interface(ti, t, t);
291 ti->class->type = ti;
293 while (parent) {
294 if (parent->class_base_init) {
295 parent->class_base_init(ti->class, ti->class_data);
297 parent = type_get_parent(parent);
300 if (ti->class_init) {
301 ti->class_init(ti->class, ti->class_data);
305 static void object_init_with_type(Object *obj, TypeImpl *ti)
307 if (type_has_parent(ti)) {
308 object_init_with_type(obj, type_get_parent(ti));
311 if (ti->instance_init) {
312 ti->instance_init(obj);
316 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
318 if (ti->instance_post_init) {
319 ti->instance_post_init(obj);
322 if (type_has_parent(ti)) {
323 object_post_init_with_type(obj, type_get_parent(ti));
327 void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
329 Object *obj = data;
331 g_assert(type != NULL);
332 type_initialize(type);
334 g_assert(type->instance_size >= sizeof(Object));
335 g_assert(type->abstract == false);
336 g_assert(size >= type->instance_size);
338 memset(obj, 0, type->instance_size);
339 obj->class = type->class;
340 object_ref(obj);
341 QTAILQ_INIT(&obj->properties);
342 object_init_with_type(obj, type);
343 object_post_init_with_type(obj, type);
346 void object_initialize(void *data, size_t size, const char *typename)
348 TypeImpl *type = type_get_by_name(typename);
350 object_initialize_with_type(data, size, type);
353 static inline bool object_property_is_child(ObjectProperty *prop)
355 return strstart(prop->type, "child<", NULL);
358 static inline bool object_property_is_link(ObjectProperty *prop)
360 return strstart(prop->type, "link<", NULL);
363 static void object_property_del_all(Object *obj)
365 while (!QTAILQ_EMPTY(&obj->properties)) {
366 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
368 QTAILQ_REMOVE(&obj->properties, prop, node);
370 if (prop->release) {
371 prop->release(obj, prop->name, prop->opaque);
374 g_free(prop->name);
375 g_free(prop->type);
376 g_free(prop);
380 static void object_property_del_child(Object *obj, Object *child, Error **errp)
382 ObjectProperty *prop;
384 QTAILQ_FOREACH(prop, &obj->properties, node) {
385 if (object_property_is_child(prop) && prop->opaque == child) {
386 object_property_del(obj, prop->name, errp);
387 break;
392 void object_unparent(Object *obj)
394 if (!obj->parent) {
395 return;
398 object_ref(obj);
399 if (obj->class->unparent) {
400 (obj->class->unparent)(obj);
402 if (obj->parent) {
403 object_property_del_child(obj->parent, obj, NULL);
405 object_unref(obj);
408 static void object_deinit(Object *obj, TypeImpl *type)
410 if (type->instance_finalize) {
411 type->instance_finalize(obj);
414 if (type_has_parent(type)) {
415 object_deinit(obj, type_get_parent(type));
419 static void object_finalize(void *data)
421 Object *obj = data;
422 TypeImpl *ti = obj->class->type;
424 object_deinit(obj, ti);
425 object_property_del_all(obj);
427 g_assert(obj->ref == 0);
428 if (obj->free) {
429 obj->free(obj);
433 Object *object_new_with_type(Type type)
435 Object *obj;
437 g_assert(type != NULL);
438 type_initialize(type);
440 obj = g_malloc(type->instance_size);
441 object_initialize_with_type(obj, type->instance_size, type);
442 obj->free = g_free;
444 return obj;
447 Object *object_new(const char *typename)
449 TypeImpl *ti = type_get_by_name(typename);
451 return object_new_with_type(ti);
454 Object *object_dynamic_cast(Object *obj, const char *typename)
456 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
457 return obj;
460 return NULL;
463 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
464 const char *file, int line, const char *func)
466 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
467 typename, file, line, func);
469 #ifdef CONFIG_QOM_CAST_DEBUG
470 int i;
471 Object *inst;
473 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
474 if (obj->class->object_cast_cache[i] == typename) {
475 goto out;
479 inst = object_dynamic_cast(obj, typename);
481 if (!inst && obj) {
482 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
483 file, line, func, obj, typename);
484 abort();
487 assert(obj == inst);
489 if (obj && obj == inst) {
490 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
491 obj->class->object_cast_cache[i - 1] =
492 obj->class->object_cast_cache[i];
494 obj->class->object_cast_cache[i - 1] = typename;
497 out:
498 #endif
499 return obj;
502 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
503 const char *typename)
505 ObjectClass *ret = NULL;
506 TypeImpl *target_type;
507 TypeImpl *type;
509 if (!class) {
510 return NULL;
513 /* A simple fast path that can trigger a lot for leaf classes. */
514 type = class->type;
515 if (type->name == typename) {
516 return class;
519 target_type = type_get_by_name(typename);
520 if (!target_type) {
521 /* target class type unknown, so fail the cast */
522 return NULL;
525 if (type->class->interfaces &&
526 type_is_ancestor(target_type, type_interface)) {
527 int found = 0;
528 GSList *i;
530 for (i = class->interfaces; i; i = i->next) {
531 ObjectClass *target_class = i->data;
533 if (type_is_ancestor(target_class->type, target_type)) {
534 ret = target_class;
535 found++;
539 /* The match was ambiguous, don't allow a cast */
540 if (found > 1) {
541 ret = NULL;
543 } else if (type_is_ancestor(type, target_type)) {
544 ret = class;
547 return ret;
550 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
551 const char *typename,
552 const char *file, int line,
553 const char *func)
555 ObjectClass *ret;
557 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
558 typename, file, line, func);
560 #ifdef CONFIG_QOM_CAST_DEBUG
561 int i;
563 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
564 if (class->class_cast_cache[i] == typename) {
565 ret = class;
566 goto out;
569 #else
570 if (!class || !class->interfaces) {
571 return class;
573 #endif
575 ret = object_class_dynamic_cast(class, typename);
576 if (!ret && class) {
577 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
578 file, line, func, class, typename);
579 abort();
582 #ifdef CONFIG_QOM_CAST_DEBUG
583 if (class && ret == class) {
584 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
585 class->class_cast_cache[i - 1] = class->class_cast_cache[i];
587 class->class_cast_cache[i - 1] = typename;
589 out:
590 #endif
591 return ret;
594 const char *object_get_typename(Object *obj)
596 return obj->class->type->name;
599 ObjectClass *object_get_class(Object *obj)
601 return obj->class;
604 bool object_class_is_abstract(ObjectClass *klass)
606 return klass->type->abstract;
609 const char *object_class_get_name(ObjectClass *klass)
611 return klass->type->name;
614 ObjectClass *object_class_by_name(const char *typename)
616 TypeImpl *type = type_get_by_name(typename);
618 if (!type) {
619 return NULL;
622 type_initialize(type);
624 return type->class;
627 ObjectClass *object_class_get_parent(ObjectClass *class)
629 TypeImpl *type = type_get_parent(class->type);
631 if (!type) {
632 return NULL;
635 type_initialize(type);
637 return type->class;
640 typedef struct OCFData
642 void (*fn)(ObjectClass *klass, void *opaque);
643 const char *implements_type;
644 bool include_abstract;
645 void *opaque;
646 } OCFData;
648 static void object_class_foreach_tramp(gpointer key, gpointer value,
649 gpointer opaque)
651 OCFData *data = opaque;
652 TypeImpl *type = value;
653 ObjectClass *k;
655 type_initialize(type);
656 k = type->class;
658 if (!data->include_abstract && type->abstract) {
659 return;
662 if (data->implements_type &&
663 !object_class_dynamic_cast(k, data->implements_type)) {
664 return;
667 data->fn(k, data->opaque);
670 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
671 const char *implements_type, bool include_abstract,
672 void *opaque)
674 OCFData data = { fn, implements_type, include_abstract, opaque };
676 enumerating_types = true;
677 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
678 enumerating_types = false;
681 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
682 void *opaque)
684 ObjectProperty *prop;
685 int ret = 0;
687 QTAILQ_FOREACH(prop, &obj->properties, node) {
688 if (object_property_is_child(prop)) {
689 ret = fn(prop->opaque, opaque);
690 if (ret != 0) {
691 break;
695 return ret;
698 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
700 GSList **list = opaque;
702 *list = g_slist_prepend(*list, klass);
705 GSList *object_class_get_list(const char *implements_type,
706 bool include_abstract)
708 GSList *list = NULL;
710 object_class_foreach(object_class_get_list_tramp,
711 implements_type, include_abstract, &list);
712 return list;
715 void object_ref(Object *obj)
717 atomic_inc(&obj->ref);
720 void object_unref(Object *obj)
722 g_assert(obj->ref > 0);
724 /* parent always holds a reference to its children */
725 if (atomic_fetch_dec(&obj->ref) == 1) {
726 object_finalize(obj);
730 void object_property_add(Object *obj, const char *name, const char *type,
731 ObjectPropertyAccessor *get,
732 ObjectPropertyAccessor *set,
733 ObjectPropertyRelease *release,
734 void *opaque, Error **errp)
736 ObjectProperty *prop;
738 QTAILQ_FOREACH(prop, &obj->properties, node) {
739 if (strcmp(prop->name, name) == 0) {
740 error_setg(errp, "attempt to add duplicate property '%s'"
741 " to object (type '%s')", name,
742 object_get_typename(obj));
743 return;
747 prop = g_malloc0(sizeof(*prop));
749 prop->name = g_strdup(name);
750 prop->type = g_strdup(type);
752 prop->get = get;
753 prop->set = set;
754 prop->release = release;
755 prop->opaque = opaque;
757 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
760 ObjectProperty *object_property_find(Object *obj, const char *name,
761 Error **errp)
763 ObjectProperty *prop;
765 QTAILQ_FOREACH(prop, &obj->properties, node) {
766 if (strcmp(prop->name, name) == 0) {
767 return prop;
771 error_setg(errp, "Property '.%s' not found", name);
772 return NULL;
775 void object_property_del(Object *obj, const char *name, Error **errp)
777 ObjectProperty *prop = object_property_find(obj, name, errp);
778 if (prop == NULL) {
779 return;
782 if (prop->release) {
783 prop->release(obj, name, prop->opaque);
786 QTAILQ_REMOVE(&obj->properties, prop, node);
788 g_free(prop->name);
789 g_free(prop->type);
790 g_free(prop);
793 void object_property_get(Object *obj, Visitor *v, const char *name,
794 Error **errp)
796 ObjectProperty *prop = object_property_find(obj, name, errp);
797 if (prop == NULL) {
798 return;
801 if (!prop->get) {
802 error_set(errp, QERR_PERMISSION_DENIED);
803 } else {
804 prop->get(obj, v, prop->opaque, name, errp);
808 void object_property_set(Object *obj, Visitor *v, const char *name,
809 Error **errp)
811 ObjectProperty *prop = object_property_find(obj, name, errp);
812 if (prop == NULL) {
813 return;
816 if (!prop->set) {
817 error_set(errp, QERR_PERMISSION_DENIED);
818 } else {
819 prop->set(obj, v, prop->opaque, name, errp);
823 void object_property_set_str(Object *obj, const char *value,
824 const char *name, Error **errp)
826 QString *qstr = qstring_from_str(value);
827 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
829 QDECREF(qstr);
832 char *object_property_get_str(Object *obj, const char *name,
833 Error **errp)
835 QObject *ret = object_property_get_qobject(obj, name, errp);
836 QString *qstring;
837 char *retval;
839 if (!ret) {
840 return NULL;
842 qstring = qobject_to_qstring(ret);
843 if (!qstring) {
844 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
845 retval = NULL;
846 } else {
847 retval = g_strdup(qstring_get_str(qstring));
850 QDECREF(qstring);
851 return retval;
854 void object_property_set_link(Object *obj, Object *value,
855 const char *name, Error **errp)
857 gchar *path = object_get_canonical_path(value);
858 object_property_set_str(obj, path, name, errp);
859 g_free(path);
862 Object *object_property_get_link(Object *obj, const char *name,
863 Error **errp)
865 char *str = object_property_get_str(obj, name, errp);
866 Object *target = NULL;
868 if (str && *str) {
869 target = object_resolve_path(str, NULL);
870 if (!target) {
871 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
875 g_free(str);
876 return target;
879 void object_property_set_bool(Object *obj, bool value,
880 const char *name, Error **errp)
882 QBool *qbool = qbool_from_int(value);
883 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
885 QDECREF(qbool);
888 bool object_property_get_bool(Object *obj, const char *name,
889 Error **errp)
891 QObject *ret = object_property_get_qobject(obj, name, errp);
892 QBool *qbool;
893 bool retval;
895 if (!ret) {
896 return false;
898 qbool = qobject_to_qbool(ret);
899 if (!qbool) {
900 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
901 retval = false;
902 } else {
903 retval = qbool_get_int(qbool);
906 QDECREF(qbool);
907 return retval;
910 void object_property_set_int(Object *obj, int64_t value,
911 const char *name, Error **errp)
913 QInt *qint = qint_from_int(value);
914 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
916 QDECREF(qint);
919 int64_t object_property_get_int(Object *obj, const char *name,
920 Error **errp)
922 QObject *ret = object_property_get_qobject(obj, name, errp);
923 QInt *qint;
924 int64_t retval;
926 if (!ret) {
927 return -1;
929 qint = qobject_to_qint(ret);
930 if (!qint) {
931 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
932 retval = -1;
933 } else {
934 retval = qint_get_int(qint);
937 QDECREF(qint);
938 return retval;
941 void object_property_parse(Object *obj, const char *string,
942 const char *name, Error **errp)
944 StringInputVisitor *mi;
945 mi = string_input_visitor_new(string);
946 object_property_set(obj, string_input_get_visitor(mi), name, errp);
948 string_input_visitor_cleanup(mi);
951 char *object_property_print(Object *obj, const char *name, bool human,
952 Error **errp)
954 StringOutputVisitor *mo;
955 char *string;
957 mo = string_output_visitor_new(human);
958 object_property_get(obj, string_output_get_visitor(mo), name, errp);
959 string = string_output_get_string(mo);
960 string_output_visitor_cleanup(mo);
961 return string;
964 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
966 ObjectProperty *prop = object_property_find(obj, name, errp);
967 if (prop == NULL) {
968 return NULL;
971 return prop->type;
974 Object *object_get_root(void)
976 static Object *root;
978 if (!root) {
979 root = object_new("container");
982 return root;
985 static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
986 const char *name, Error **errp)
988 Object *child = opaque;
989 gchar *path;
991 path = object_get_canonical_path(child);
992 visit_type_str(v, &path, name, errp);
993 g_free(path);
996 static void object_finalize_child_property(Object *obj, const char *name,
997 void *opaque)
999 Object *child = opaque;
1001 object_unref(child);
1004 void object_property_add_child(Object *obj, const char *name,
1005 Object *child, Error **errp)
1007 Error *local_err = NULL;
1008 gchar *type;
1010 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1012 object_property_add(obj, name, type, object_get_child_property, NULL,
1013 object_finalize_child_property, child, &local_err);
1014 if (local_err) {
1015 error_propagate(errp, local_err);
1016 goto out;
1018 object_ref(child);
1019 g_assert(child->parent == NULL);
1020 child->parent = obj;
1022 out:
1023 g_free(type);
1026 void object_property_allow_set_link(Object *obj, const char *name,
1027 Object *val, Error **errp)
1029 /* Allow the link to be set, always */
1032 typedef struct {
1033 Object **child;
1034 void (*check)(Object *, const char *, Object *, Error **);
1035 ObjectPropertyLinkFlags flags;
1036 } LinkProperty;
1038 static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
1039 const char *name, Error **errp)
1041 LinkProperty *lprop = opaque;
1042 Object **child = lprop->child;
1043 gchar *path;
1045 if (*child) {
1046 path = object_get_canonical_path(*child);
1047 visit_type_str(v, &path, name, errp);
1048 g_free(path);
1049 } else {
1050 path = (gchar *)"";
1051 visit_type_str(v, &path, name, errp);
1056 * object_resolve_link:
1058 * Lookup an object and ensure its type matches the link property type. This
1059 * is similar to object_resolve_path() except type verification against the
1060 * link property is performed.
1062 * Returns: The matched object or NULL on path lookup failures.
1064 static Object *object_resolve_link(Object *obj, const char *name,
1065 const char *path, Error **errp)
1067 const char *type;
1068 gchar *target_type;
1069 bool ambiguous = false;
1070 Object *target;
1072 /* Go from link<FOO> to FOO. */
1073 type = object_property_get_type(obj, name, NULL);
1074 target_type = g_strndup(&type[5], strlen(type) - 6);
1075 target = object_resolve_path_type(path, target_type, &ambiguous);
1077 if (ambiguous) {
1078 error_set(errp, ERROR_CLASS_GENERIC_ERROR,
1079 "Path '%s' does not uniquely identify an object", path);
1080 } else if (!target) {
1081 target = object_resolve_path(path, &ambiguous);
1082 if (target || ambiguous) {
1083 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1084 } else {
1085 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
1087 target = NULL;
1089 g_free(target_type);
1091 return target;
1094 static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
1095 const char *name, Error **errp)
1097 Error *local_err = NULL;
1098 LinkProperty *prop = opaque;
1099 Object **child = prop->child;
1100 Object *old_target = *child;
1101 Object *new_target = NULL;
1102 char *path = NULL;
1104 visit_type_str(v, &path, name, &local_err);
1106 if (!local_err && strcmp(path, "") != 0) {
1107 new_target = object_resolve_link(obj, name, path, &local_err);
1110 g_free(path);
1111 if (local_err) {
1112 error_propagate(errp, local_err);
1113 return;
1116 prop->check(obj, name, new_target, &local_err);
1117 if (local_err) {
1118 error_propagate(errp, local_err);
1119 return;
1122 if (new_target) {
1123 object_ref(new_target);
1125 *child = new_target;
1126 if (old_target != NULL) {
1127 object_unref(old_target);
1131 static void object_release_link_property(Object *obj, const char *name,
1132 void *opaque)
1134 LinkProperty *prop = opaque;
1136 if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) {
1137 object_unref(*prop->child);
1139 g_free(prop);
1142 void object_property_add_link(Object *obj, const char *name,
1143 const char *type, Object **child,
1144 void (*check)(Object *, const char *,
1145 Object *, Error **),
1146 ObjectPropertyLinkFlags flags,
1147 Error **errp)
1149 Error *local_err = NULL;
1150 LinkProperty *prop = g_malloc(sizeof(*prop));
1151 gchar *full_type;
1153 prop->child = child;
1154 prop->check = check;
1155 prop->flags = flags;
1157 full_type = g_strdup_printf("link<%s>", type);
1159 object_property_add(obj, name, full_type,
1160 object_get_link_property,
1161 check ? object_set_link_property : NULL,
1162 object_release_link_property,
1163 prop,
1164 &local_err);
1165 if (local_err) {
1166 error_propagate(errp, local_err);
1167 g_free(prop);
1170 g_free(full_type);
1173 gchar *object_get_canonical_path_component(Object *obj)
1175 ObjectProperty *prop = NULL;
1177 g_assert(obj);
1178 g_assert(obj->parent != NULL);
1180 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
1181 if (!object_property_is_child(prop)) {
1182 continue;
1185 if (prop->opaque == obj) {
1186 return g_strdup(prop->name);
1190 /* obj had a parent but was not a child, should never happen */
1191 g_assert_not_reached();
1192 return NULL;
1195 gchar *object_get_canonical_path(Object *obj)
1197 Object *root = object_get_root();
1198 char *newpath, *path = NULL;
1200 while (obj != root) {
1201 char *component = object_get_canonical_path_component(obj);
1203 if (path) {
1204 newpath = g_strdup_printf("%s/%s", component, path);
1205 g_free(component);
1206 g_free(path);
1207 path = newpath;
1208 } else {
1209 path = component;
1212 obj = obj->parent;
1215 newpath = g_strdup_printf("/%s", path ? path : "");
1216 g_free(path);
1218 return newpath;
1221 Object *object_resolve_path_component(Object *parent, const gchar *part)
1223 ObjectProperty *prop = object_property_find(parent, part, NULL);
1224 if (prop == NULL) {
1225 return NULL;
1228 if (object_property_is_link(prop)) {
1229 LinkProperty *lprop = prop->opaque;
1230 return *lprop->child;
1231 } else if (object_property_is_child(prop)) {
1232 return prop->opaque;
1233 } else {
1234 return NULL;
1238 static Object *object_resolve_abs_path(Object *parent,
1239 gchar **parts,
1240 const char *typename,
1241 int index)
1243 Object *child;
1245 if (parts[index] == NULL) {
1246 return object_dynamic_cast(parent, typename);
1249 if (strcmp(parts[index], "") == 0) {
1250 return object_resolve_abs_path(parent, parts, typename, index + 1);
1253 child = object_resolve_path_component(parent, parts[index]);
1254 if (!child) {
1255 return NULL;
1258 return object_resolve_abs_path(child, parts, typename, index + 1);
1261 static Object *object_resolve_partial_path(Object *parent,
1262 gchar **parts,
1263 const char *typename,
1264 bool *ambiguous)
1266 Object *obj;
1267 ObjectProperty *prop;
1269 obj = object_resolve_abs_path(parent, parts, typename, 0);
1271 QTAILQ_FOREACH(prop, &parent->properties, node) {
1272 Object *found;
1274 if (!object_property_is_child(prop)) {
1275 continue;
1278 found = object_resolve_partial_path(prop->opaque, parts,
1279 typename, ambiguous);
1280 if (found) {
1281 if (obj) {
1282 if (ambiguous) {
1283 *ambiguous = true;
1285 return NULL;
1287 obj = found;
1290 if (ambiguous && *ambiguous) {
1291 return NULL;
1295 return obj;
1298 Object *object_resolve_path_type(const char *path, const char *typename,
1299 bool *ambiguous)
1301 Object *obj;
1302 gchar **parts;
1304 parts = g_strsplit(path, "/", 0);
1305 assert(parts);
1307 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
1308 if (ambiguous) {
1309 *ambiguous = false;
1311 obj = object_resolve_partial_path(object_get_root(), parts,
1312 typename, ambiguous);
1313 } else {
1314 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
1317 g_strfreev(parts);
1319 return obj;
1322 Object *object_resolve_path(const char *path, bool *ambiguous)
1324 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1327 typedef struct StringProperty
1329 char *(*get)(Object *, Error **);
1330 void (*set)(Object *, const char *, Error **);
1331 } StringProperty;
1333 static void property_get_str(Object *obj, Visitor *v, void *opaque,
1334 const char *name, Error **errp)
1336 StringProperty *prop = opaque;
1337 char *value;
1339 value = prop->get(obj, errp);
1340 if (value) {
1341 visit_type_str(v, &value, name, errp);
1342 g_free(value);
1346 static void property_set_str(Object *obj, Visitor *v, void *opaque,
1347 const char *name, Error **errp)
1349 StringProperty *prop = opaque;
1350 char *value;
1351 Error *local_err = NULL;
1353 visit_type_str(v, &value, name, &local_err);
1354 if (local_err) {
1355 error_propagate(errp, local_err);
1356 return;
1359 prop->set(obj, value, errp);
1360 g_free(value);
1363 static void property_release_str(Object *obj, const char *name,
1364 void *opaque)
1366 StringProperty *prop = opaque;
1367 g_free(prop);
1370 void object_property_add_str(Object *obj, const char *name,
1371 char *(*get)(Object *, Error **),
1372 void (*set)(Object *, const char *, Error **),
1373 Error **errp)
1375 Error *local_err = NULL;
1376 StringProperty *prop = g_malloc0(sizeof(*prop));
1378 prop->get = get;
1379 prop->set = set;
1381 object_property_add(obj, name, "string",
1382 get ? property_get_str : NULL,
1383 set ? property_set_str : NULL,
1384 property_release_str,
1385 prop, &local_err);
1386 if (local_err) {
1387 error_propagate(errp, local_err);
1388 g_free(prop);
1392 typedef struct BoolProperty
1394 bool (*get)(Object *, Error **);
1395 void (*set)(Object *, bool, Error **);
1396 } BoolProperty;
1398 static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1399 const char *name, Error **errp)
1401 BoolProperty *prop = opaque;
1402 bool value;
1404 value = prop->get(obj, errp);
1405 visit_type_bool(v, &value, name, errp);
1408 static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1409 const char *name, Error **errp)
1411 BoolProperty *prop = opaque;
1412 bool value;
1413 Error *local_err = NULL;
1415 visit_type_bool(v, &value, name, &local_err);
1416 if (local_err) {
1417 error_propagate(errp, local_err);
1418 return;
1421 prop->set(obj, value, errp);
1424 static void property_release_bool(Object *obj, const char *name,
1425 void *opaque)
1427 BoolProperty *prop = opaque;
1428 g_free(prop);
1431 void object_property_add_bool(Object *obj, const char *name,
1432 bool (*get)(Object *, Error **),
1433 void (*set)(Object *, bool, Error **),
1434 Error **errp)
1436 Error *local_err = NULL;
1437 BoolProperty *prop = g_malloc0(sizeof(*prop));
1439 prop->get = get;
1440 prop->set = set;
1442 object_property_add(obj, name, "bool",
1443 get ? property_get_bool : NULL,
1444 set ? property_set_bool : NULL,
1445 property_release_bool,
1446 prop, &local_err);
1447 if (local_err) {
1448 error_propagate(errp, local_err);
1449 g_free(prop);
1453 static char *qdev_get_type(Object *obj, Error **errp)
1455 return g_strdup(object_get_typename(obj));
1458 static void property_get_uint8_ptr(Object *obj, Visitor *v,
1459 void *opaque, const char *name,
1460 Error **errp)
1462 uint8_t value = *(uint8_t *)opaque;
1463 visit_type_uint8(v, &value, name, errp);
1466 static void property_get_uint16_ptr(Object *obj, Visitor *v,
1467 void *opaque, const char *name,
1468 Error **errp)
1470 uint16_t value = *(uint16_t *)opaque;
1471 visit_type_uint16(v, &value, name, errp);
1474 static void property_get_uint32_ptr(Object *obj, Visitor *v,
1475 void *opaque, const char *name,
1476 Error **errp)
1478 uint32_t value = *(uint32_t *)opaque;
1479 visit_type_uint32(v, &value, name, errp);
1482 static void property_get_uint64_ptr(Object *obj, Visitor *v,
1483 void *opaque, const char *name,
1484 Error **errp)
1486 uint64_t value = *(uint64_t *)opaque;
1487 visit_type_uint64(v, &value, name, errp);
1490 void object_property_add_uint8_ptr(Object *obj, const char *name,
1491 const uint8_t *v, Error **errp)
1493 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
1494 NULL, NULL, (void *)v, errp);
1497 void object_property_add_uint16_ptr(Object *obj, const char *name,
1498 const uint16_t *v, Error **errp)
1500 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
1501 NULL, NULL, (void *)v, errp);
1504 void object_property_add_uint32_ptr(Object *obj, const char *name,
1505 const uint32_t *v, Error **errp)
1507 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
1508 NULL, NULL, (void *)v, errp);
1511 void object_property_add_uint64_ptr(Object *obj, const char *name,
1512 const uint64_t *v, Error **errp)
1514 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
1515 NULL, NULL, (void *)v, errp);
1518 static void object_instance_init(Object *obj)
1520 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1523 static void register_types(void)
1525 static TypeInfo interface_info = {
1526 .name = TYPE_INTERFACE,
1527 .class_size = sizeof(InterfaceClass),
1528 .abstract = true,
1531 static TypeInfo object_info = {
1532 .name = TYPE_OBJECT,
1533 .instance_size = sizeof(Object),
1534 .instance_init = object_instance_init,
1535 .abstract = true,
1538 type_interface = type_register_internal(&interface_info);
1539 type_register_internal(&object_info);
1542 type_init(register_types)