spapr: Add "memop" hypercall
[qemu/kevin.git] / qom / object.c
blob00bb3b029cb800b95ca6e0c4a706c72c5bf6135b
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 "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"
22 #include "qobject.h"
23 #include "qbool.h"
24 #include "qint.h"
25 #include "qstring.h"
27 #define MAX_INTERFACES 32
29 typedef struct InterfaceImpl InterfaceImpl;
30 typedef struct TypeImpl TypeImpl;
32 struct InterfaceImpl
34 const char *parent;
35 void (*interface_initfn)(ObjectClass *class, void *data);
36 TypeImpl *type;
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_finalize)(Object *obj);
56 bool abstract;
58 const char *parent;
59 TypeImpl *parent_type;
61 ObjectClass *class;
63 int num_interfaces;
64 InterfaceImpl interfaces[MAX_INTERFACES];
67 typedef struct Interface
69 Object parent;
70 Object *obj;
71 } Interface;
73 #define INTERFACE(obj) OBJECT_CHECK(Interface, obj, TYPE_INTERFACE)
75 static Type type_interface;
77 static GHashTable *type_table_get(void)
79 static GHashTable *type_table;
81 if (type_table == NULL) {
82 type_table = g_hash_table_new(g_str_hash, g_str_equal);
85 return type_table;
88 static void type_table_add(TypeImpl *ti)
90 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
93 static TypeImpl *type_table_lookup(const char *name)
95 return g_hash_table_lookup(type_table_get(), name);
98 static TypeImpl *type_register_internal(const TypeInfo *info)
100 TypeImpl *ti = g_malloc0(sizeof(*ti));
102 g_assert(info->name != NULL);
104 if (type_table_lookup(info->name) != NULL) {
105 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
106 abort();
109 ti->name = g_strdup(info->name);
110 ti->parent = g_strdup(info->parent);
112 ti->class_size = info->class_size;
113 ti->instance_size = info->instance_size;
115 ti->class_init = info->class_init;
116 ti->class_base_init = info->class_base_init;
117 ti->class_finalize = info->class_finalize;
118 ti->class_data = info->class_data;
120 ti->instance_init = info->instance_init;
121 ti->instance_finalize = info->instance_finalize;
123 ti->abstract = info->abstract;
125 if (info->interfaces) {
126 int i;
128 for (i = 0; info->interfaces[i].type; i++) {
129 ti->interfaces[i].parent = info->interfaces[i].type;
130 ti->interfaces[i].interface_initfn = info->interfaces[i].interface_initfn;
131 ti->num_interfaces++;
135 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 void type_class_interface_init(TypeImpl *ti, InterfaceImpl *iface)
203 TypeInfo info = {
204 .instance_size = sizeof(Interface),
205 .parent = iface->parent,
206 .class_size = sizeof(InterfaceClass),
207 .class_init = iface->interface_initfn,
208 .abstract = true,
210 char *name = g_strdup_printf("<%s::%s>", ti->name, iface->parent);
212 info.name = name;
213 iface->type = type_register_internal(&info);
214 g_free(name);
217 static void type_initialize(TypeImpl *ti)
219 TypeImpl *parent;
220 int i;
222 if (ti->class) {
223 return;
226 ti->class_size = type_class_get_size(ti);
227 ti->instance_size = type_object_get_size(ti);
229 ti->class = g_malloc0(ti->class_size);
231 parent = type_get_parent(ti);
232 if (parent) {
233 type_initialize(parent);
235 g_assert(parent->class_size <= ti->class_size);
236 memcpy(ti->class, parent->class, parent->class_size);
239 ti->class->type = ti;
241 while (parent) {
242 if (parent->class_base_init) {
243 parent->class_base_init(ti->class, ti->class_data);
245 parent = type_get_parent(parent);
248 for (i = 0; i < ti->num_interfaces; i++) {
249 type_class_interface_init(ti, &ti->interfaces[i]);
252 if (ti->class_init) {
253 ti->class_init(ti->class, ti->class_data);
257 static void object_interface_init(Object *obj, InterfaceImpl *iface)
259 TypeImpl *ti = iface->type;
260 Interface *iface_obj;
262 iface_obj = INTERFACE(object_new(ti->name));
263 iface_obj->obj = obj;
265 obj->interfaces = g_slist_prepend(obj->interfaces, iface_obj);
268 static void object_init_with_type(Object *obj, TypeImpl *ti)
270 int i;
272 if (type_has_parent(ti)) {
273 object_init_with_type(obj, type_get_parent(ti));
276 for (i = 0; i < ti->num_interfaces; i++) {
277 object_interface_init(obj, &ti->interfaces[i]);
280 if (ti->instance_init) {
281 ti->instance_init(obj);
285 void object_initialize_with_type(void *data, TypeImpl *type)
287 Object *obj = data;
289 g_assert(type != NULL);
290 type_initialize(type);
292 g_assert(type->instance_size >= sizeof(Object));
293 g_assert(type->abstract == false);
295 memset(obj, 0, type->instance_size);
296 obj->class = type->class;
297 QTAILQ_INIT(&obj->properties);
298 object_init_with_type(obj, type);
301 void object_initialize(void *data, const char *typename)
303 TypeImpl *type = type_get_by_name(typename);
305 object_initialize_with_type(data, type);
308 static inline bool object_property_is_child(ObjectProperty *prop)
310 return strstart(prop->type, "child<", NULL);
313 static inline bool object_property_is_link(ObjectProperty *prop)
315 return strstart(prop->type, "link<", NULL);
318 static void object_property_del_all(Object *obj)
320 while (!QTAILQ_EMPTY(&obj->properties)) {
321 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
323 QTAILQ_REMOVE(&obj->properties, prop, node);
325 if (prop->release) {
326 prop->release(obj, prop->name, prop->opaque);
329 g_free(prop->name);
330 g_free(prop->type);
331 g_free(prop);
335 static void object_property_del_child(Object *obj, Object *child, Error **errp)
337 ObjectProperty *prop;
339 QTAILQ_FOREACH(prop, &obj->properties, node) {
340 if (object_property_is_child(prop) && prop->opaque == child) {
341 object_property_del(obj, prop->name, errp);
342 break;
347 void object_unparent(Object *obj)
349 if (obj->parent) {
350 object_property_del_child(obj->parent, obj, NULL);
354 static void object_deinit(Object *obj, TypeImpl *type)
356 if (type->instance_finalize) {
357 type->instance_finalize(obj);
360 while (obj->interfaces) {
361 Interface *iface_obj = obj->interfaces->data;
362 obj->interfaces = g_slist_delete_link(obj->interfaces, obj->interfaces);
363 object_delete(OBJECT(iface_obj));
366 if (type_has_parent(type)) {
367 object_deinit(obj, type_get_parent(type));
370 object_unparent(obj);
373 void object_finalize(void *data)
375 Object *obj = data;
376 TypeImpl *ti = obj->class->type;
378 object_deinit(obj, ti);
379 object_property_del_all(obj);
381 g_assert(obj->ref == 0);
384 Object *object_new_with_type(Type type)
386 Object *obj;
388 g_assert(type != NULL);
389 type_initialize(type);
391 obj = g_malloc(type->instance_size);
392 object_initialize_with_type(obj, type);
393 object_ref(obj);
395 return obj;
398 Object *object_new(const char *typename)
400 TypeImpl *ti = type_get_by_name(typename);
402 return object_new_with_type(ti);
405 void object_delete(Object *obj)
407 object_unref(obj);
408 g_assert(obj->ref == 0);
409 g_free(obj);
412 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
414 assert(target_type);
416 /* Check if typename is a direct ancestor of type */
417 while (type) {
418 if (type == target_type) {
419 return true;
422 type = type_get_parent(type);
425 return false;
428 static bool object_is_type(Object *obj, TypeImpl *target_type)
430 return !target_type || type_is_ancestor(obj->class->type, target_type);
433 Object *object_dynamic_cast(Object *obj, const char *typename)
435 TypeImpl *target_type = type_get_by_name(typename);
436 GSList *i;
438 /* Check if typename is a direct ancestor. Special-case TYPE_OBJECT,
439 * we want to go back from interfaces to the parent.
441 if (target_type && object_is_type(obj, target_type)) {
442 return obj;
445 /* Check if obj is an interface and its containing object is a direct
446 * ancestor of typename. In principle we could do this test at the very
447 * beginning of object_dynamic_cast, avoiding a second call to
448 * object_is_type. However, casting between interfaces is relatively
449 * rare, and object_is_type(obj, type_interface) would fail almost always.
451 * Perhaps we could add a magic value to the object header for increased
452 * (run-time) type safety and to speed up tests like this one. If we ever
453 * do that we can revisit the order here.
455 if (object_is_type(obj, type_interface)) {
456 assert(!obj->interfaces);
457 obj = INTERFACE(obj)->obj;
458 if (object_is_type(obj, target_type)) {
459 return obj;
463 if (!target_type) {
464 return obj;
467 /* Check if obj has an interface of typename */
468 for (i = obj->interfaces; i; i = i->next) {
469 Interface *iface = i->data;
471 if (object_is_type(OBJECT(iface), target_type)) {
472 return OBJECT(iface);
476 return NULL;
480 Object *object_dynamic_cast_assert(Object *obj, const char *typename)
482 Object *inst;
484 inst = object_dynamic_cast(obj, typename);
486 if (!inst) {
487 fprintf(stderr, "Object %p is not an instance of type %s\n",
488 obj, typename);
489 abort();
492 return inst;
495 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
496 const char *typename)
498 TypeImpl *target_type = type_get_by_name(typename);
499 TypeImpl *type = class->type;
501 while (type) {
502 if (type == target_type) {
503 return class;
506 type = type_get_parent(type);
509 return NULL;
512 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
513 const char *typename)
515 ObjectClass *ret = object_class_dynamic_cast(class, typename);
517 if (!ret) {
518 fprintf(stderr, "Object %p is not an instance of type %s\n",
519 class, typename);
520 abort();
523 return ret;
526 const char *object_get_typename(Object *obj)
528 return obj->class->type->name;
531 ObjectClass *object_get_class(Object *obj)
533 return obj->class;
536 const char *object_class_get_name(ObjectClass *klass)
538 return klass->type->name;
541 ObjectClass *object_class_by_name(const char *typename)
543 TypeImpl *type = type_get_by_name(typename);
545 if (!type) {
546 return NULL;
549 type_initialize(type);
551 return type->class;
554 ObjectClass *object_class_get_parent(ObjectClass *class)
556 TypeImpl *type = type_get_parent(class->type);
558 if (!type) {
559 return NULL;
562 type_initialize(type);
564 return type->class;
567 typedef struct OCFData
569 void (*fn)(ObjectClass *klass, void *opaque);
570 const char *implements_type;
571 bool include_abstract;
572 void *opaque;
573 } OCFData;
575 static void object_class_foreach_tramp(gpointer key, gpointer value,
576 gpointer opaque)
578 OCFData *data = opaque;
579 TypeImpl *type = value;
580 ObjectClass *k;
582 type_initialize(type);
583 k = type->class;
585 if (!data->include_abstract && type->abstract) {
586 return;
589 if (data->implements_type &&
590 !object_class_dynamic_cast(k, data->implements_type)) {
591 return;
594 data->fn(k, data->opaque);
597 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
598 const char *implements_type, bool include_abstract,
599 void *opaque)
601 OCFData data = { fn, implements_type, include_abstract, opaque };
603 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
606 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
607 void *opaque)
609 ObjectProperty *prop;
610 int ret = 0;
612 QTAILQ_FOREACH(prop, &obj->properties, node) {
613 if (object_property_is_child(prop)) {
614 ret = fn(prop->opaque, opaque);
615 if (ret != 0) {
616 break;
620 return ret;
623 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
625 GSList **list = opaque;
627 *list = g_slist_prepend(*list, klass);
630 GSList *object_class_get_list(const char *implements_type,
631 bool include_abstract)
633 GSList *list = NULL;
635 object_class_foreach(object_class_get_list_tramp,
636 implements_type, include_abstract, &list);
637 return list;
640 void object_ref(Object *obj)
642 obj->ref++;
645 void object_unref(Object *obj)
647 g_assert(obj->ref > 0);
648 obj->ref--;
650 /* parent always holds a reference to its children */
651 if (obj->ref == 0) {
652 object_finalize(obj);
656 void object_property_add(Object *obj, const char *name, const char *type,
657 ObjectPropertyAccessor *get,
658 ObjectPropertyAccessor *set,
659 ObjectPropertyRelease *release,
660 void *opaque, Error **errp)
662 ObjectProperty *prop = g_malloc0(sizeof(*prop));
664 prop->name = g_strdup(name);
665 prop->type = g_strdup(type);
667 prop->get = get;
668 prop->set = set;
669 prop->release = release;
670 prop->opaque = opaque;
672 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
675 ObjectProperty *object_property_find(Object *obj, const char *name,
676 Error **errp)
678 ObjectProperty *prop;
680 QTAILQ_FOREACH(prop, &obj->properties, node) {
681 if (strcmp(prop->name, name) == 0) {
682 return prop;
686 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
687 return NULL;
690 void object_property_del(Object *obj, const char *name, Error **errp)
692 ObjectProperty *prop = object_property_find(obj, name, errp);
693 if (prop == NULL) {
694 return;
697 if (prop->release) {
698 prop->release(obj, name, prop->opaque);
701 QTAILQ_REMOVE(&obj->properties, prop, node);
703 g_free(prop->name);
704 g_free(prop->type);
705 g_free(prop);
708 void object_property_get(Object *obj, Visitor *v, const char *name,
709 Error **errp)
711 ObjectProperty *prop = object_property_find(obj, name, errp);
712 if (prop == NULL) {
713 return;
716 if (!prop->get) {
717 error_set(errp, QERR_PERMISSION_DENIED);
718 } else {
719 prop->get(obj, v, prop->opaque, name, errp);
723 void object_property_set(Object *obj, Visitor *v, const char *name,
724 Error **errp)
726 ObjectProperty *prop = object_property_find(obj, name, errp);
727 if (prop == NULL) {
728 return;
731 if (!prop->set) {
732 error_set(errp, QERR_PERMISSION_DENIED);
733 } else {
734 prop->set(obj, v, prop->opaque, name, errp);
738 void object_property_set_str(Object *obj, const char *value,
739 const char *name, Error **errp)
741 QString *qstr = qstring_from_str(value);
742 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
744 QDECREF(qstr);
747 char *object_property_get_str(Object *obj, const char *name,
748 Error **errp)
750 QObject *ret = object_property_get_qobject(obj, name, errp);
751 QString *qstring;
752 char *retval;
754 if (!ret) {
755 return NULL;
757 qstring = qobject_to_qstring(ret);
758 if (!qstring) {
759 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
760 retval = NULL;
761 } else {
762 retval = g_strdup(qstring_get_str(qstring));
765 QDECREF(qstring);
766 return retval;
769 void object_property_set_link(Object *obj, Object *value,
770 const char *name, Error **errp)
772 object_property_set_str(obj, object_get_canonical_path(value),
773 name, errp);
776 Object *object_property_get_link(Object *obj, const char *name,
777 Error **errp)
779 char *str = object_property_get_str(obj, name, errp);
780 Object *target = NULL;
782 if (str && *str) {
783 target = object_resolve_path(str, NULL);
784 if (!target) {
785 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
789 g_free(str);
790 return target;
793 void object_property_set_bool(Object *obj, bool value,
794 const char *name, Error **errp)
796 QBool *qbool = qbool_from_int(value);
797 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
799 QDECREF(qbool);
802 bool object_property_get_bool(Object *obj, const char *name,
803 Error **errp)
805 QObject *ret = object_property_get_qobject(obj, name, errp);
806 QBool *qbool;
807 bool retval;
809 if (!ret) {
810 return false;
812 qbool = qobject_to_qbool(ret);
813 if (!qbool) {
814 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
815 retval = false;
816 } else {
817 retval = qbool_get_int(qbool);
820 QDECREF(qbool);
821 return retval;
824 void object_property_set_int(Object *obj, int64_t value,
825 const char *name, Error **errp)
827 QInt *qint = qint_from_int(value);
828 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
830 QDECREF(qint);
833 int64_t object_property_get_int(Object *obj, const char *name,
834 Error **errp)
836 QObject *ret = object_property_get_qobject(obj, name, errp);
837 QInt *qint;
838 int64_t retval;
840 if (!ret) {
841 return -1;
843 qint = qobject_to_qint(ret);
844 if (!qint) {
845 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
846 retval = -1;
847 } else {
848 retval = qint_get_int(qint);
851 QDECREF(qint);
852 return retval;
855 void object_property_parse(Object *obj, const char *string,
856 const char *name, Error **errp)
858 StringInputVisitor *mi;
859 mi = string_input_visitor_new(string);
860 object_property_set(obj, string_input_get_visitor(mi), name, errp);
862 string_input_visitor_cleanup(mi);
865 char *object_property_print(Object *obj, const char *name,
866 Error **errp)
868 StringOutputVisitor *mo;
869 char *string;
871 mo = string_output_visitor_new();
872 object_property_get(obj, string_output_get_visitor(mo), name, errp);
873 string = string_output_get_string(mo);
874 string_output_visitor_cleanup(mo);
875 return string;
878 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
880 ObjectProperty *prop = object_property_find(obj, name, errp);
881 if (prop == NULL) {
882 return NULL;
885 return prop->type;
888 Object *object_get_root(void)
890 static Object *root;
892 if (!root) {
893 root = object_new("container");
896 return root;
899 static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
900 const char *name, Error **errp)
902 Object *child = opaque;
903 gchar *path;
905 path = object_get_canonical_path(child);
906 visit_type_str(v, &path, name, errp);
907 g_free(path);
910 static void object_finalize_child_property(Object *obj, const char *name,
911 void *opaque)
913 Object *child = opaque;
915 object_unref(child);
918 void object_property_add_child(Object *obj, const char *name,
919 Object *child, Error **errp)
921 gchar *type;
923 /* Registering an interface object in the composition tree will mightily
924 * confuse object_get_canonical_path (which, on the other hand, knows how
925 * to get the canonical path of an interface object).
927 assert(!object_is_type(obj, type_interface));
929 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
931 object_property_add(obj, name, type, object_get_child_property,
932 NULL, object_finalize_child_property, child, errp);
934 object_ref(child);
935 g_assert(child->parent == NULL);
936 child->parent = obj;
938 g_free(type);
941 static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
942 const char *name, Error **errp)
944 Object **child = opaque;
945 gchar *path;
947 if (*child) {
948 path = object_get_canonical_path(*child);
949 visit_type_str(v, &path, name, errp);
950 g_free(path);
951 } else {
952 path = (gchar *)"";
953 visit_type_str(v, &path, name, errp);
957 static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
958 const char *name, Error **errp)
960 Object **child = opaque;
961 Object *old_target;
962 bool ambiguous = false;
963 const char *type;
964 char *path;
965 gchar *target_type;
967 type = object_property_get_type(obj, name, NULL);
969 visit_type_str(v, &path, name, errp);
971 old_target = *child;
972 *child = NULL;
974 if (strcmp(path, "") != 0) {
975 Object *target;
977 /* Go from link<FOO> to FOO. */
978 target_type = g_strndup(&type[5], strlen(type) - 6);
979 target = object_resolve_path_type(path, target_type, &ambiguous);
981 if (ambiguous) {
982 error_set(errp, QERR_AMBIGUOUS_PATH, path);
983 } else if (target) {
984 object_ref(target);
985 *child = target;
986 } else {
987 target = object_resolve_path(path, &ambiguous);
988 if (target || ambiguous) {
989 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
990 } else {
991 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
994 g_free(target_type);
997 g_free(path);
999 if (old_target != NULL) {
1000 object_unref(old_target);
1004 void object_property_add_link(Object *obj, const char *name,
1005 const char *type, Object **child,
1006 Error **errp)
1008 gchar *full_type;
1010 full_type = g_strdup_printf("link<%s>", type);
1012 object_property_add(obj, name, full_type,
1013 object_get_link_property,
1014 object_set_link_property,
1015 NULL, child, errp);
1017 g_free(full_type);
1020 gchar *object_get_canonical_path(Object *obj)
1022 Object *root = object_get_root();
1023 char *newpath = NULL, *path = NULL;
1025 if (object_is_type(obj, type_interface)) {
1026 obj = INTERFACE(obj)->obj;
1029 while (obj != root) {
1030 ObjectProperty *prop = NULL;
1032 g_assert(obj->parent != NULL);
1034 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
1035 if (!object_property_is_child(prop)) {
1036 continue;
1039 if (prop->opaque == obj) {
1040 if (path) {
1041 newpath = g_strdup_printf("%s/%s", prop->name, path);
1042 g_free(path);
1043 path = newpath;
1044 } else {
1045 path = g_strdup(prop->name);
1047 break;
1051 g_assert(prop != NULL);
1053 obj = obj->parent;
1056 newpath = g_strdup_printf("/%s", path);
1057 g_free(path);
1059 return newpath;
1062 Object *object_resolve_path_component(Object *parent, gchar *part)
1064 ObjectProperty *prop = object_property_find(parent, part, NULL);
1065 if (prop == NULL) {
1066 return NULL;
1069 if (object_property_is_link(prop)) {
1070 return *(Object **)prop->opaque;
1071 } else if (object_property_is_child(prop)) {
1072 return prop->opaque;
1073 } else {
1074 return NULL;
1078 static Object *object_resolve_abs_path(Object *parent,
1079 gchar **parts,
1080 const char *typename,
1081 int index)
1083 Object *child;
1085 if (parts[index] == NULL) {
1086 return object_dynamic_cast(parent, typename);
1089 if (strcmp(parts[index], "") == 0) {
1090 return object_resolve_abs_path(parent, parts, typename, index + 1);
1093 child = object_resolve_path_component(parent, parts[index]);
1094 if (!child) {
1095 return NULL;
1098 return object_resolve_abs_path(child, parts, typename, index + 1);
1101 static Object *object_resolve_partial_path(Object *parent,
1102 gchar **parts,
1103 const char *typename,
1104 bool *ambiguous)
1106 Object *obj;
1107 ObjectProperty *prop;
1109 obj = object_resolve_abs_path(parent, parts, typename, 0);
1111 QTAILQ_FOREACH(prop, &parent->properties, node) {
1112 Object *found;
1114 if (!object_property_is_child(prop)) {
1115 continue;
1118 found = object_resolve_partial_path(prop->opaque, parts,
1119 typename, ambiguous);
1120 if (found) {
1121 if (obj) {
1122 if (ambiguous) {
1123 *ambiguous = true;
1125 return NULL;
1127 obj = found;
1130 if (ambiguous && *ambiguous) {
1131 return NULL;
1135 return obj;
1138 Object *object_resolve_path_type(const char *path, const char *typename,
1139 bool *ambiguous)
1141 bool partial_path = true;
1142 Object *obj;
1143 gchar **parts;
1145 parts = g_strsplit(path, "/", 0);
1146 if (parts == NULL || parts[0] == NULL) {
1147 g_strfreev(parts);
1148 return object_get_root();
1151 if (strcmp(parts[0], "") == 0) {
1152 partial_path = false;
1155 if (partial_path) {
1156 if (ambiguous) {
1157 *ambiguous = false;
1159 obj = object_resolve_partial_path(object_get_root(), parts,
1160 typename, ambiguous);
1161 } else {
1162 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
1165 g_strfreev(parts);
1167 return obj;
1170 Object *object_resolve_path(const char *path, bool *ambiguous)
1172 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1175 typedef struct StringProperty
1177 char *(*get)(Object *, Error **);
1178 void (*set)(Object *, const char *, Error **);
1179 } StringProperty;
1181 static void property_get_str(Object *obj, Visitor *v, void *opaque,
1182 const char *name, Error **errp)
1184 StringProperty *prop = opaque;
1185 char *value;
1187 value = prop->get(obj, errp);
1188 if (value) {
1189 visit_type_str(v, &value, name, errp);
1190 g_free(value);
1194 static void property_set_str(Object *obj, Visitor *v, void *opaque,
1195 const char *name, Error **errp)
1197 StringProperty *prop = opaque;
1198 char *value;
1199 Error *local_err = NULL;
1201 visit_type_str(v, &value, name, &local_err);
1202 if (local_err) {
1203 error_propagate(errp, local_err);
1204 return;
1207 prop->set(obj, value, errp);
1208 g_free(value);
1211 static void property_release_str(Object *obj, const char *name,
1212 void *opaque)
1214 StringProperty *prop = opaque;
1215 g_free(prop);
1218 void object_property_add_str(Object *obj, const char *name,
1219 char *(*get)(Object *, Error **),
1220 void (*set)(Object *, const char *, Error **),
1221 Error **errp)
1223 StringProperty *prop = g_malloc0(sizeof(*prop));
1225 prop->get = get;
1226 prop->set = set;
1228 object_property_add(obj, name, "string",
1229 get ? property_get_str : NULL,
1230 set ? property_set_str : NULL,
1231 property_release_str,
1232 prop, errp);
1235 static char *qdev_get_type(Object *obj, Error **errp)
1237 return g_strdup(object_get_typename(obj));
1240 static void object_instance_init(Object *obj)
1242 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1245 static void register_types(void)
1247 static TypeInfo interface_info = {
1248 .name = TYPE_INTERFACE,
1249 .instance_size = sizeof(Interface),
1250 .abstract = true,
1253 static TypeInfo object_info = {
1254 .name = TYPE_OBJECT,
1255 .instance_size = sizeof(Object),
1256 .instance_init = object_instance_init,
1257 .abstract = true,
1260 type_interface = type_register_internal(&interface_info);
1261 type_register_internal(&object_info);
1264 type_init(register_types)