4 * Copyright IBM, Corp. 2011
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-visit.h"
17 #include "qapi/string-input-visitor.h"
18 #include "qapi/string-output-visitor.h"
19 #include "qapi/qmp/qerror.h"
22 /* TODO: replace QObject with a simpler visitor to avoid a dependency
23 * of the QOM core on QObject? */
24 #include "qom/qom-qobject.h"
25 #include "qapi/qmp/qobject.h"
26 #include "qapi/qmp/qbool.h"
27 #include "qapi/qmp/qint.h"
28 #include "qapi/qmp/qstring.h"
30 #define MAX_INTERFACES 32
32 typedef struct InterfaceImpl InterfaceImpl
;
33 typedef struct TypeImpl TypeImpl
;
48 void (*class_init
)(ObjectClass
*klass
, void *data
);
49 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
50 void (*class_finalize
)(ObjectClass
*klass
, void *data
);
54 void (*instance_init
)(Object
*obj
);
55 void (*instance_post_init
)(Object
*obj
);
56 void (*instance_finalize
)(Object
*obj
);
61 TypeImpl
*parent_type
;
66 InterfaceImpl interfaces
[MAX_INTERFACES
];
69 static Type type_interface
;
71 static GHashTable
*type_table_get(void)
73 static GHashTable
*type_table
;
75 if (type_table
== NULL
) {
76 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
82 static bool enumerating_types
;
84 static void type_table_add(TypeImpl
*ti
)
86 assert(!enumerating_types
);
87 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
90 static TypeImpl
*type_table_lookup(const char *name
)
92 return g_hash_table_lookup(type_table_get(), name
);
95 static TypeImpl
*type_new(const TypeInfo
*info
)
97 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
100 g_assert(info
->name
!= NULL
);
102 if (type_table_lookup(info
->name
) != NULL
) {
103 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
107 ti
->name
= g_strdup(info
->name
);
108 ti
->parent
= g_strdup(info
->parent
);
110 ti
->class_size
= info
->class_size
;
111 ti
->instance_size
= info
->instance_size
;
113 ti
->class_init
= info
->class_init
;
114 ti
->class_base_init
= info
->class_base_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_post_init
= info
->instance_post_init
;
120 ti
->instance_finalize
= info
->instance_finalize
;
122 ti
->abstract
= info
->abstract
;
124 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
125 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
127 ti
->num_interfaces
= i
;
132 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
141 TypeImpl
*type_register(const TypeInfo
*info
)
143 assert(info
->parent
);
144 return type_register_internal(info
);
147 TypeImpl
*type_register_static(const TypeInfo
*info
)
149 return type_register(info
);
152 static TypeImpl
*type_get_by_name(const char *name
)
158 return type_table_lookup(name
);
161 static TypeImpl
*type_get_parent(TypeImpl
*type
)
163 if (!type
->parent_type
&& type
->parent
) {
164 type
->parent_type
= type_get_by_name(type
->parent
);
165 g_assert(type
->parent_type
!= NULL
);
168 return type
->parent_type
;
171 static bool type_has_parent(TypeImpl
*type
)
173 return (type
->parent
!= NULL
);
176 static size_t type_class_get_size(TypeImpl
*ti
)
178 if (ti
->class_size
) {
179 return ti
->class_size
;
182 if (type_has_parent(ti
)) {
183 return type_class_get_size(type_get_parent(ti
));
186 return sizeof(ObjectClass
);
189 static size_t type_object_get_size(TypeImpl
*ti
)
191 if (ti
->instance_size
) {
192 return ti
->instance_size
;
195 if (type_has_parent(ti
)) {
196 return type_object_get_size(type_get_parent(ti
));
202 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
206 /* Check if typename is a direct ancestor of type */
208 if (type
== target_type
) {
212 type
= type_get_parent(type
);
218 static void type_initialize(TypeImpl
*ti
);
220 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
221 TypeImpl
*parent_type
)
223 InterfaceClass
*new_iface
;
225 TypeImpl
*iface_impl
;
227 info
.parent
= parent_type
->name
;
228 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
229 info
.abstract
= true;
231 iface_impl
= type_new(&info
);
232 iface_impl
->parent_type
= parent_type
;
233 type_initialize(iface_impl
);
234 g_free((char *)info
.name
);
236 new_iface
= (InterfaceClass
*)iface_impl
->class;
237 new_iface
->concrete_class
= ti
->class;
238 new_iface
->interface_type
= interface_type
;
240 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
,
244 static void type_initialize(TypeImpl
*ti
)
252 ti
->class_size
= type_class_get_size(ti
);
253 ti
->instance_size
= type_object_get_size(ti
);
255 ti
->class = g_malloc0(ti
->class_size
);
257 parent
= type_get_parent(ti
);
259 type_initialize(parent
);
263 g_assert(parent
->class_size
<= ti
->class_size
);
264 memcpy(ti
->class, parent
->class, parent
->class_size
);
265 ti
->class->interfaces
= NULL
;
267 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
268 InterfaceClass
*iface
= e
->data
;
269 ObjectClass
*klass
= OBJECT_CLASS(iface
);
271 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
274 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
275 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
276 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
277 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
279 if (type_is_ancestor(target_type
, t
)) {
288 type_initialize_interface(ti
, t
, t
);
292 ti
->class->type
= ti
;
295 if (parent
->class_base_init
) {
296 parent
->class_base_init(ti
->class, ti
->class_data
);
298 parent
= type_get_parent(parent
);
301 if (ti
->class_init
) {
302 ti
->class_init(ti
->class, ti
->class_data
);
306 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
308 if (type_has_parent(ti
)) {
309 object_init_with_type(obj
, type_get_parent(ti
));
312 if (ti
->instance_init
) {
313 ti
->instance_init(obj
);
317 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
319 if (ti
->instance_post_init
) {
320 ti
->instance_post_init(obj
);
323 if (type_has_parent(ti
)) {
324 object_post_init_with_type(obj
, type_get_parent(ti
));
328 void object_initialize_with_type(void *data
, size_t size
, TypeImpl
*type
)
332 g_assert(type
!= NULL
);
333 type_initialize(type
);
335 g_assert(type
->instance_size
>= sizeof(Object
));
336 g_assert(type
->abstract
== false);
337 g_assert(size
>= type
->instance_size
);
339 memset(obj
, 0, type
->instance_size
);
340 obj
->class = type
->class;
342 QTAILQ_INIT(&obj
->properties
);
343 object_init_with_type(obj
, type
);
344 object_post_init_with_type(obj
, type
);
347 void object_initialize(void *data
, size_t size
, const char *typename
)
349 TypeImpl
*type
= type_get_by_name(typename
);
351 object_initialize_with_type(data
, size
, type
);
354 static inline bool object_property_is_child(ObjectProperty
*prop
)
356 return strstart(prop
->type
, "child<", NULL
);
359 static void object_property_del_all(Object
*obj
)
361 while (!QTAILQ_EMPTY(&obj
->properties
)) {
362 ObjectProperty
*prop
= QTAILQ_FIRST(&obj
->properties
);
364 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
367 prop
->release(obj
, prop
->name
, prop
->opaque
);
376 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
378 ObjectProperty
*prop
;
380 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
381 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
382 object_property_del(obj
, prop
->name
, errp
);
388 void object_unparent(Object
*obj
)
391 object_property_del_child(obj
->parent
, obj
, NULL
);
395 static void object_deinit(Object
*obj
, TypeImpl
*type
)
397 if (type
->instance_finalize
) {
398 type
->instance_finalize(obj
);
401 if (type_has_parent(type
)) {
402 object_deinit(obj
, type_get_parent(type
));
406 static void object_finalize(void *data
)
409 TypeImpl
*ti
= obj
->class->type
;
411 object_property_del_all(obj
);
412 object_deinit(obj
, ti
);
414 g_assert(obj
->ref
== 0);
420 Object
*object_new_with_type(Type type
)
424 g_assert(type
!= NULL
);
425 type_initialize(type
);
427 obj
= g_malloc(type
->instance_size
);
428 object_initialize_with_type(obj
, type
->instance_size
, type
);
434 Object
*object_new(const char *typename
)
436 TypeImpl
*ti
= type_get_by_name(typename
);
438 return object_new_with_type(ti
);
441 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
443 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
450 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
451 const char *file
, int line
, const char *func
)
453 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
454 typename
, file
, line
, func
);
456 #ifdef CONFIG_QOM_CAST_DEBUG
460 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
461 if (obj
->class->object_cast_cache
[i
] == typename
) {
466 inst
= object_dynamic_cast(obj
, typename
);
469 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
470 file
, line
, func
, obj
, typename
);
476 if (obj
&& obj
== inst
) {
477 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
478 obj
->class->object_cast_cache
[i
- 1] =
479 obj
->class->object_cast_cache
[i
];
481 obj
->class->object_cast_cache
[i
- 1] = typename
;
489 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
490 const char *typename
)
492 ObjectClass
*ret
= NULL
;
493 TypeImpl
*target_type
;
500 /* A simple fast path that can trigger a lot for leaf classes. */
502 if (type
->name
== typename
) {
506 target_type
= type_get_by_name(typename
);
508 /* target class type unknown, so fail the cast */
512 if (type
->class->interfaces
&&
513 type_is_ancestor(target_type
, type_interface
)) {
517 for (i
= class->interfaces
; i
; i
= i
->next
) {
518 ObjectClass
*target_class
= i
->data
;
520 if (type_is_ancestor(target_class
->type
, target_type
)) {
526 /* The match was ambiguous, don't allow a cast */
530 } else if (type_is_ancestor(type
, target_type
)) {
537 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
538 const char *typename
,
539 const char *file
, int line
,
544 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
545 typename
, file
, line
, func
);
547 #ifdef CONFIG_QOM_CAST_DEBUG
550 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
551 if (class->class_cast_cache
[i
] == typename
) {
557 if (!class || !class->interfaces
) {
562 ret
= object_class_dynamic_cast(class, typename
);
564 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
565 file
, line
, func
, class, typename
);
569 #ifdef CONFIG_QOM_CAST_DEBUG
570 if (class && ret
== class) {
571 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
572 class->class_cast_cache
[i
- 1] = class->class_cast_cache
[i
];
574 class->class_cast_cache
[i
- 1] = typename
;
581 const char *object_get_typename(Object
*obj
)
583 return obj
->class->type
->name
;
586 ObjectClass
*object_get_class(Object
*obj
)
591 bool object_class_is_abstract(ObjectClass
*klass
)
593 return klass
->type
->abstract
;
596 const char *object_class_get_name(ObjectClass
*klass
)
598 return klass
->type
->name
;
601 ObjectClass
*object_class_by_name(const char *typename
)
603 TypeImpl
*type
= type_get_by_name(typename
);
609 type_initialize(type
);
614 ObjectClass
*object_class_get_parent(ObjectClass
*class)
616 TypeImpl
*type
= type_get_parent(class->type
);
622 type_initialize(type
);
627 typedef struct OCFData
629 void (*fn
)(ObjectClass
*klass
, void *opaque
);
630 const char *implements_type
;
631 bool include_abstract
;
635 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
638 OCFData
*data
= opaque
;
639 TypeImpl
*type
= value
;
642 type_initialize(type
);
645 if (!data
->include_abstract
&& type
->abstract
) {
649 if (data
->implements_type
&&
650 !object_class_dynamic_cast(k
, data
->implements_type
)) {
654 data
->fn(k
, data
->opaque
);
657 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
658 const char *implements_type
, bool include_abstract
,
661 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
663 enumerating_types
= true;
664 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
665 enumerating_types
= false;
668 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
671 ObjectProperty
*prop
, *next
;
674 QTAILQ_FOREACH_SAFE(prop
, &obj
->properties
, node
, next
) {
675 if (object_property_is_child(prop
)) {
676 ret
= fn(prop
->opaque
, opaque
);
685 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
687 GSList
**list
= opaque
;
689 *list
= g_slist_prepend(*list
, klass
);
692 GSList
*object_class_get_list(const char *implements_type
,
693 bool include_abstract
)
697 object_class_foreach(object_class_get_list_tramp
,
698 implements_type
, include_abstract
, &list
);
702 void object_ref(Object
*obj
)
707 atomic_inc(&obj
->ref
);
710 void object_unref(Object
*obj
)
715 g_assert(obj
->ref
> 0);
717 /* parent always holds a reference to its children */
718 if (atomic_fetch_dec(&obj
->ref
) == 1) {
719 object_finalize(obj
);
724 object_property_add(Object
*obj
, const char *name
, const char *type
,
725 ObjectPropertyAccessor
*get
,
726 ObjectPropertyAccessor
*set
,
727 ObjectPropertyRelease
*release
,
728 void *opaque
, Error
**errp
)
730 ObjectProperty
*prop
;
731 size_t name_len
= strlen(name
);
733 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
736 char *name_no_array
= g_strdup(name
);
738 name_no_array
[name_len
- 3] = '\0';
740 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
742 ret
= object_property_add(obj
, full_name
, type
, get
, set
,
743 release
, opaque
, NULL
);
749 g_free(name_no_array
);
753 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
754 if (strcmp(prop
->name
, name
) == 0) {
755 error_setg(errp
, "attempt to add duplicate property '%s'"
756 " to object (type '%s')", name
,
757 object_get_typename(obj
));
762 prop
= g_malloc0(sizeof(*prop
));
764 prop
->name
= g_strdup(name
);
765 prop
->type
= g_strdup(type
);
769 prop
->release
= release
;
770 prop
->opaque
= opaque
;
772 QTAILQ_INSERT_TAIL(&obj
->properties
, prop
, node
);
776 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
779 ObjectProperty
*prop
;
781 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
782 if (strcmp(prop
->name
, name
) == 0) {
787 error_setg(errp
, "Property '.%s' not found", name
);
791 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
793 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
799 prop
->release(obj
, name
, prop
->opaque
);
802 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
809 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
812 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
818 error_set(errp
, QERR_PERMISSION_DENIED
);
820 prop
->get(obj
, v
, prop
->opaque
, name
, errp
);
824 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
827 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
833 error_set(errp
, QERR_PERMISSION_DENIED
);
835 prop
->set(obj
, v
, prop
->opaque
, name
, errp
);
839 void object_property_set_str(Object
*obj
, const char *value
,
840 const char *name
, Error
**errp
)
842 QString
*qstr
= qstring_from_str(value
);
843 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
848 char *object_property_get_str(Object
*obj
, const char *name
,
851 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
858 qstring
= qobject_to_qstring(ret
);
860 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
863 retval
= g_strdup(qstring_get_str(qstring
));
870 void object_property_set_link(Object
*obj
, Object
*value
,
871 const char *name
, Error
**errp
)
873 gchar
*path
= object_get_canonical_path(value
);
874 object_property_set_str(obj
, path
, name
, errp
);
878 Object
*object_property_get_link(Object
*obj
, const char *name
,
881 char *str
= object_property_get_str(obj
, name
, errp
);
882 Object
*target
= NULL
;
885 target
= object_resolve_path(str
, NULL
);
887 error_set(errp
, QERR_DEVICE_NOT_FOUND
, str
);
895 void object_property_set_bool(Object
*obj
, bool value
,
896 const char *name
, Error
**errp
)
898 QBool
*qbool
= qbool_from_int(value
);
899 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
904 bool object_property_get_bool(Object
*obj
, const char *name
,
907 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
914 qbool
= qobject_to_qbool(ret
);
916 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
919 retval
= qbool_get_int(qbool
);
926 void object_property_set_int(Object
*obj
, int64_t value
,
927 const char *name
, Error
**errp
)
929 QInt
*qint
= qint_from_int(value
);
930 object_property_set_qobject(obj
, QOBJECT(qint
), name
, errp
);
935 int64_t object_property_get_int(Object
*obj
, const char *name
,
938 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
945 qint
= qobject_to_qint(ret
);
947 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
950 retval
= qint_get_int(qint
);
957 int object_property_get_enum(Object
*obj
, const char *name
,
958 const char *strings
[], Error
**errp
)
960 StringOutputVisitor
*sov
;
961 StringInputVisitor
*siv
;
965 sov
= string_output_visitor_new(false);
966 object_property_get(obj
, string_output_get_visitor(sov
), name
, errp
);
967 str
= string_output_get_string(sov
);
968 siv
= string_input_visitor_new(str
);
969 string_output_visitor_cleanup(sov
);
970 visit_type_enum(string_input_get_visitor(siv
),
971 &ret
, strings
, NULL
, name
, errp
);
974 string_input_visitor_cleanup(siv
);
979 void object_property_get_uint16List(Object
*obj
, const char *name
,
980 uint16List
**list
, Error
**errp
)
982 StringOutputVisitor
*ov
;
983 StringInputVisitor
*iv
;
986 ov
= string_output_visitor_new(false);
987 object_property_get(obj
, string_output_get_visitor(ov
),
989 str
= string_output_get_string(ov
);
990 iv
= string_input_visitor_new(str
);
991 visit_type_uint16List(string_input_get_visitor(iv
),
995 string_output_visitor_cleanup(ov
);
996 string_input_visitor_cleanup(iv
);
999 void object_property_parse(Object
*obj
, const char *string
,
1000 const char *name
, Error
**errp
)
1002 StringInputVisitor
*mi
;
1003 mi
= string_input_visitor_new(string
);
1004 object_property_set(obj
, string_input_get_visitor(mi
), name
, errp
);
1006 string_input_visitor_cleanup(mi
);
1009 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1012 StringOutputVisitor
*mo
;
1015 mo
= string_output_visitor_new(human
);
1016 object_property_get(obj
, string_output_get_visitor(mo
), name
, errp
);
1017 string
= string_output_get_string(mo
);
1018 string_output_visitor_cleanup(mo
);
1022 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1024 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1032 Object
*object_get_root(void)
1034 static Object
*root
;
1037 root
= object_new("container");
1043 static void object_get_child_property(Object
*obj
, Visitor
*v
, void *opaque
,
1044 const char *name
, Error
**errp
)
1046 Object
*child
= opaque
;
1049 path
= object_get_canonical_path(child
);
1050 visit_type_str(v
, &path
, name
, errp
);
1054 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
, const gchar
*part
)
1059 static void object_finalize_child_property(Object
*obj
, const char *name
,
1062 Object
*child
= opaque
;
1064 if (child
->class->unparent
) {
1065 (child
->class->unparent
)(child
);
1067 child
->parent
= NULL
;
1068 object_unref(child
);
1071 void object_property_add_child(Object
*obj
, const char *name
,
1072 Object
*child
, Error
**errp
)
1074 Error
*local_err
= NULL
;
1078 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
1080 op
= object_property_add(obj
, name
, type
, object_get_child_property
, NULL
,
1081 object_finalize_child_property
, child
, &local_err
);
1083 error_propagate(errp
, local_err
);
1087 op
->resolve
= object_resolve_child_property
;
1089 g_assert(child
->parent
== NULL
);
1090 child
->parent
= obj
;
1096 void object_property_allow_set_link(Object
*obj
, const char *name
,
1097 Object
*val
, Error
**errp
)
1099 /* Allow the link to be set, always */
1104 void (*check
)(Object
*, const char *, Object
*, Error
**);
1105 ObjectPropertyLinkFlags flags
;
1108 static void object_get_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
1109 const char *name
, Error
**errp
)
1111 LinkProperty
*lprop
= opaque
;
1112 Object
**child
= lprop
->child
;
1116 path
= object_get_canonical_path(*child
);
1117 visit_type_str(v
, &path
, name
, errp
);
1121 visit_type_str(v
, &path
, name
, errp
);
1126 * object_resolve_link:
1128 * Lookup an object and ensure its type matches the link property type. This
1129 * is similar to object_resolve_path() except type verification against the
1130 * link property is performed.
1132 * Returns: The matched object or NULL on path lookup failures.
1134 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1135 const char *path
, Error
**errp
)
1139 bool ambiguous
= false;
1142 /* Go from link<FOO> to FOO. */
1143 type
= object_property_get_type(obj
, name
, NULL
);
1144 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1145 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1148 error_set(errp
, ERROR_CLASS_GENERIC_ERROR
,
1149 "Path '%s' does not uniquely identify an object", path
);
1150 } else if (!target
) {
1151 target
= object_resolve_path(path
, &ambiguous
);
1152 if (target
|| ambiguous
) {
1153 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1155 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
1159 g_free(target_type
);
1164 static void object_set_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
1165 const char *name
, Error
**errp
)
1167 Error
*local_err
= NULL
;
1168 LinkProperty
*prop
= opaque
;
1169 Object
**child
= prop
->child
;
1170 Object
*old_target
= *child
;
1171 Object
*new_target
= NULL
;
1174 visit_type_str(v
, &path
, name
, &local_err
);
1176 if (!local_err
&& strcmp(path
, "") != 0) {
1177 new_target
= object_resolve_link(obj
, name
, path
, &local_err
);
1182 error_propagate(errp
, local_err
);
1186 prop
->check(obj
, name
, new_target
, &local_err
);
1188 error_propagate(errp
, local_err
);
1192 object_ref(new_target
);
1193 *child
= new_target
;
1194 object_unref(old_target
);
1197 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
, const gchar
*part
)
1199 LinkProperty
*lprop
= opaque
;
1201 return *lprop
->child
;
1204 static void object_release_link_property(Object
*obj
, const char *name
,
1207 LinkProperty
*prop
= opaque
;
1209 if ((prop
->flags
& OBJ_PROP_LINK_UNREF_ON_RELEASE
) && *prop
->child
) {
1210 object_unref(*prop
->child
);
1215 void object_property_add_link(Object
*obj
, const char *name
,
1216 const char *type
, Object
**child
,
1217 void (*check
)(Object
*, const char *,
1218 Object
*, Error
**),
1219 ObjectPropertyLinkFlags flags
,
1222 Error
*local_err
= NULL
;
1223 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1227 prop
->child
= child
;
1228 prop
->check
= check
;
1229 prop
->flags
= flags
;
1231 full_type
= g_strdup_printf("link<%s>", type
);
1233 op
= object_property_add(obj
, name
, full_type
,
1234 object_get_link_property
,
1235 check
? object_set_link_property
: NULL
,
1236 object_release_link_property
,
1240 error_propagate(errp
, local_err
);
1245 op
->resolve
= object_resolve_link_property
;
1251 gchar
*object_get_canonical_path_component(Object
*obj
)
1253 ObjectProperty
*prop
= NULL
;
1256 g_assert(obj
->parent
!= NULL
);
1258 QTAILQ_FOREACH(prop
, &obj
->parent
->properties
, node
) {
1259 if (!object_property_is_child(prop
)) {
1263 if (prop
->opaque
== obj
) {
1264 return g_strdup(prop
->name
);
1268 /* obj had a parent but was not a child, should never happen */
1269 g_assert_not_reached();
1273 gchar
*object_get_canonical_path(Object
*obj
)
1275 Object
*root
= object_get_root();
1276 char *newpath
, *path
= NULL
;
1278 while (obj
!= root
) {
1279 char *component
= object_get_canonical_path_component(obj
);
1282 newpath
= g_strdup_printf("%s/%s", component
, path
);
1293 newpath
= g_strdup_printf("/%s", path
? path
: "");
1299 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1301 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1306 if (prop
->resolve
) {
1307 return prop
->resolve(parent
, prop
->opaque
, part
);
1313 static Object
*object_resolve_abs_path(Object
*parent
,
1315 const char *typename
,
1320 if (parts
[index
] == NULL
) {
1321 return object_dynamic_cast(parent
, typename
);
1324 if (strcmp(parts
[index
], "") == 0) {
1325 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1328 child
= object_resolve_path_component(parent
, parts
[index
]);
1333 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1336 static Object
*object_resolve_partial_path(Object
*parent
,
1338 const char *typename
,
1342 ObjectProperty
*prop
;
1344 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1346 QTAILQ_FOREACH(prop
, &parent
->properties
, node
) {
1349 if (!object_property_is_child(prop
)) {
1353 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1354 typename
, ambiguous
);
1365 if (ambiguous
&& *ambiguous
) {
1373 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1379 parts
= g_strsplit(path
, "/", 0);
1382 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1386 obj
= object_resolve_partial_path(object_get_root(), parts
,
1387 typename
, ambiguous
);
1389 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1397 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1399 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1402 typedef struct StringProperty
1404 char *(*get
)(Object
*, Error
**);
1405 void (*set
)(Object
*, const char *, Error
**);
1408 static void property_get_str(Object
*obj
, Visitor
*v
, void *opaque
,
1409 const char *name
, Error
**errp
)
1411 StringProperty
*prop
= opaque
;
1414 value
= prop
->get(obj
, errp
);
1416 visit_type_str(v
, &value
, name
, errp
);
1421 static void property_set_str(Object
*obj
, Visitor
*v
, void *opaque
,
1422 const char *name
, Error
**errp
)
1424 StringProperty
*prop
= opaque
;
1426 Error
*local_err
= NULL
;
1428 visit_type_str(v
, &value
, name
, &local_err
);
1430 error_propagate(errp
, local_err
);
1434 prop
->set(obj
, value
, errp
);
1438 static void property_release_str(Object
*obj
, const char *name
,
1441 StringProperty
*prop
= opaque
;
1445 void object_property_add_str(Object
*obj
, const char *name
,
1446 char *(*get
)(Object
*, Error
**),
1447 void (*set
)(Object
*, const char *, Error
**),
1450 Error
*local_err
= NULL
;
1451 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1456 object_property_add(obj
, name
, "string",
1457 get
? property_get_str
: NULL
,
1458 set
? property_set_str
: NULL
,
1459 property_release_str
,
1462 error_propagate(errp
, local_err
);
1467 typedef struct BoolProperty
1469 bool (*get
)(Object
*, Error
**);
1470 void (*set
)(Object
*, bool, Error
**);
1473 static void property_get_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1474 const char *name
, Error
**errp
)
1476 BoolProperty
*prop
= opaque
;
1479 value
= prop
->get(obj
, errp
);
1480 visit_type_bool(v
, &value
, name
, errp
);
1483 static void property_set_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1484 const char *name
, Error
**errp
)
1486 BoolProperty
*prop
= opaque
;
1488 Error
*local_err
= NULL
;
1490 visit_type_bool(v
, &value
, name
, &local_err
);
1492 error_propagate(errp
, local_err
);
1496 prop
->set(obj
, value
, errp
);
1499 static void property_release_bool(Object
*obj
, const char *name
,
1502 BoolProperty
*prop
= opaque
;
1506 void object_property_add_bool(Object
*obj
, const char *name
,
1507 bool (*get
)(Object
*, Error
**),
1508 void (*set
)(Object
*, bool, Error
**),
1511 Error
*local_err
= NULL
;
1512 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1517 object_property_add(obj
, name
, "bool",
1518 get
? property_get_bool
: NULL
,
1519 set
? property_set_bool
: NULL
,
1520 property_release_bool
,
1523 error_propagate(errp
, local_err
);
1528 static char *qdev_get_type(Object
*obj
, Error
**errp
)
1530 return g_strdup(object_get_typename(obj
));
1533 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
,
1534 void *opaque
, const char *name
,
1537 uint8_t value
= *(uint8_t *)opaque
;
1538 visit_type_uint8(v
, &value
, name
, errp
);
1541 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
,
1542 void *opaque
, const char *name
,
1545 uint16_t value
= *(uint16_t *)opaque
;
1546 visit_type_uint16(v
, &value
, name
, errp
);
1549 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
,
1550 void *opaque
, const char *name
,
1553 uint32_t value
= *(uint32_t *)opaque
;
1554 visit_type_uint32(v
, &value
, name
, errp
);
1557 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
,
1558 void *opaque
, const char *name
,
1561 uint64_t value
= *(uint64_t *)opaque
;
1562 visit_type_uint64(v
, &value
, name
, errp
);
1565 void object_property_add_uint8_ptr(Object
*obj
, const char *name
,
1566 const uint8_t *v
, Error
**errp
)
1568 object_property_add(obj
, name
, "uint8", property_get_uint8_ptr
,
1569 NULL
, NULL
, (void *)v
, errp
);
1572 void object_property_add_uint16_ptr(Object
*obj
, const char *name
,
1573 const uint16_t *v
, Error
**errp
)
1575 object_property_add(obj
, name
, "uint16", property_get_uint16_ptr
,
1576 NULL
, NULL
, (void *)v
, errp
);
1579 void object_property_add_uint32_ptr(Object
*obj
, const char *name
,
1580 const uint32_t *v
, Error
**errp
)
1582 object_property_add(obj
, name
, "uint32", property_get_uint32_ptr
,
1583 NULL
, NULL
, (void *)v
, errp
);
1586 void object_property_add_uint64_ptr(Object
*obj
, const char *name
,
1587 const uint64_t *v
, Error
**errp
)
1589 object_property_add(obj
, name
, "uint64", property_get_uint64_ptr
,
1590 NULL
, NULL
, (void *)v
, errp
);
1595 const char *target_name
;
1598 static void property_get_alias(Object
*obj
, struct Visitor
*v
, void *opaque
,
1599 const char *name
, Error
**errp
)
1601 AliasProperty
*prop
= opaque
;
1603 object_property_get(prop
->target_obj
, v
, prop
->target_name
, errp
);
1606 static void property_set_alias(Object
*obj
, struct Visitor
*v
, void *opaque
,
1607 const char *name
, Error
**errp
)
1609 AliasProperty
*prop
= opaque
;
1611 object_property_set(prop
->target_obj
, v
, prop
->target_name
, errp
);
1614 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
1617 AliasProperty
*prop
= opaque
;
1619 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
1622 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
1624 AliasProperty
*prop
= opaque
;
1629 void object_property_add_alias(Object
*obj
, const char *name
,
1630 Object
*target_obj
, const char *target_name
,
1633 AliasProperty
*prop
;
1635 ObjectProperty
*target_prop
;
1638 target_prop
= object_property_find(target_obj
, target_name
, errp
);
1643 if (object_property_is_child(target_prop
)) {
1644 prop_type
= g_strdup_printf("link%s",
1645 target_prop
->type
+ strlen("child"));
1647 prop_type
= g_strdup(target_prop
->type
);
1650 prop
= g_malloc(sizeof(*prop
));
1651 prop
->target_obj
= target_obj
;
1652 prop
->target_name
= target_name
;
1654 op
= object_property_add(obj
, name
, prop_type
,
1657 property_release_alias
,
1659 op
->resolve
= property_resolve_alias
;
1664 static void object_instance_init(Object
*obj
)
1666 object_property_add_str(obj
, "type", qdev_get_type
, NULL
, NULL
);
1669 static void register_types(void)
1671 static TypeInfo interface_info
= {
1672 .name
= TYPE_INTERFACE
,
1673 .class_size
= sizeof(InterfaceClass
),
1677 static TypeInfo object_info
= {
1678 .name
= TYPE_OBJECT
,
1679 .instance_size
= sizeof(Object
),
1680 .instance_init
= object_instance_init
,
1684 type_interface
= type_register_internal(&interface_info
);
1685 type_register_internal(&object_info
);
1688 type_init(register_types
)