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 "qemu/object.h"
14 #include "qemu-common.h"
15 #include "qapi/qapi-visit-core.h"
19 #define MAX_INTERFACES 32
21 typedef struct InterfaceImpl InterfaceImpl
;
22 typedef struct TypeImpl TypeImpl
;
27 void (*interface_initfn
)(ObjectClass
*class, void *data
);
39 void (*class_init
)(ObjectClass
*klass
, void *data
);
40 void (*class_finalize
)(ObjectClass
*klass
, void *data
);
44 void (*instance_init
)(Object
*obj
);
45 void (*instance_finalize
)(Object
*obj
);
50 TypeImpl
*parent_type
;
55 InterfaceImpl interfaces
[MAX_INTERFACES
];
58 typedef struct Interface
64 #define INTERFACE(obj) OBJECT_CHECK(Interface, obj, TYPE_INTERFACE)
66 static GHashTable
*type_table_get(void)
68 static GHashTable
*type_table
;
70 if (type_table
== NULL
) {
71 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
77 static void type_table_add(TypeImpl
*ti
)
79 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
82 static TypeImpl
*type_table_lookup(const char *name
)
84 return g_hash_table_lookup(type_table_get(), name
);
87 TypeImpl
*type_register(const TypeInfo
*info
)
89 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
91 g_assert(info
->name
!= NULL
);
93 if (type_table_lookup(info
->name
) != NULL
) {
94 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
98 ti
->name
= g_strdup(info
->name
);
99 ti
->parent
= g_strdup(info
->parent
);
101 ti
->class_size
= info
->class_size
;
102 ti
->instance_size
= info
->instance_size
;
104 ti
->class_init
= info
->class_init
;
105 ti
->class_finalize
= info
->class_finalize
;
106 ti
->class_data
= info
->class_data
;
108 ti
->instance_init
= info
->instance_init
;
109 ti
->instance_finalize
= info
->instance_finalize
;
111 ti
->abstract
= info
->abstract
;
113 if (info
->interfaces
) {
116 for (i
= 0; info
->interfaces
[i
].type
; i
++) {
117 ti
->interfaces
[i
].parent
= info
->interfaces
[i
].type
;
118 ti
->interfaces
[i
].interface_initfn
= info
->interfaces
[i
].interface_initfn
;
119 ti
->num_interfaces
++;
128 TypeImpl
*type_register_static(const TypeInfo
*info
)
130 return type_register(info
);
133 static TypeImpl
*type_get_by_name(const char *name
)
139 return type_table_lookup(name
);
142 static TypeImpl
*type_get_parent(TypeImpl
*type
)
144 if (!type
->parent_type
&& type
->parent
) {
145 type
->parent_type
= type_get_by_name(type
->parent
);
146 g_assert(type
->parent_type
!= NULL
);
149 return type
->parent_type
;
152 static bool type_has_parent(TypeImpl
*type
)
154 return (type
->parent
!= NULL
);
157 static size_t type_class_get_size(TypeImpl
*ti
)
159 if (ti
->class_size
) {
160 return ti
->class_size
;
163 if (type_has_parent(ti
)) {
164 return type_class_get_size(type_get_parent(ti
));
167 return sizeof(ObjectClass
);
170 static void type_class_interface_init(TypeImpl
*ti
, InterfaceImpl
*iface
)
173 .instance_size
= sizeof(Interface
),
174 .parent
= iface
->parent
,
175 .class_size
= sizeof(InterfaceClass
),
176 .class_init
= iface
->interface_initfn
,
179 char *name
= g_strdup_printf("<%s::%s>", ti
->name
, iface
->parent
);
182 iface
->type
= type_register(&info
);
186 static void type_class_init(TypeImpl
*ti
)
188 size_t class_size
= sizeof(ObjectClass
);
195 ti
->class_size
= type_class_get_size(ti
);
197 ti
->class = g_malloc0(ti
->class_size
);
198 ti
->class->type
= ti
;
200 if (type_has_parent(ti
)) {
201 TypeImpl
*parent
= type_get_parent(ti
);
203 type_class_init(parent
);
205 class_size
= parent
->class_size
;
206 g_assert(parent
->class_size
<= ti
->class_size
);
208 memcpy((void *)ti
->class + sizeof(ObjectClass
),
209 (void *)parent
->class + sizeof(ObjectClass
),
210 parent
->class_size
- sizeof(ObjectClass
));
213 memset((void *)ti
->class + class_size
, 0, ti
->class_size
- class_size
);
215 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
216 type_class_interface_init(ti
, &ti
->interfaces
[i
]);
219 if (ti
->class_init
) {
220 ti
->class_init(ti
->class, ti
->class_data
);
224 static void object_interface_init(Object
*obj
, InterfaceImpl
*iface
)
226 TypeImpl
*ti
= iface
->type
;
227 Interface
*iface_obj
;
229 iface_obj
= INTERFACE(object_new(ti
->name
));
230 iface_obj
->obj
= obj
;
232 obj
->interfaces
= g_slist_prepend(obj
->interfaces
, iface_obj
);
235 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
239 if (type_has_parent(ti
)) {
240 object_init_with_type(obj
, type_get_parent(ti
));
243 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
244 object_interface_init(obj
, &ti
->interfaces
[i
]);
247 if (ti
->instance_init
) {
248 ti
->instance_init(obj
);
252 void object_initialize_with_type(void *data
, TypeImpl
*type
)
256 g_assert(type
!= NULL
);
257 g_assert(type
->instance_size
>= sizeof(ObjectClass
));
259 type_class_init(type
);
260 g_assert(type
->abstract
== false);
262 memset(obj
, 0, type
->instance_size
);
263 obj
->class = type
->class;
264 QTAILQ_INIT(&obj
->properties
);
265 object_init_with_type(obj
, type
);
268 void object_initialize(void *data
, const char *typename
)
270 TypeImpl
*type
= type_get_by_name(typename
);
272 object_initialize_with_type(data
, type
);
275 static void object_property_del_all(Object
*obj
)
277 while (!QTAILQ_EMPTY(&obj
->properties
)) {
278 ObjectProperty
*prop
= QTAILQ_FIRST(&obj
->properties
);
280 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
283 prop
->release(obj
, prop
->name
, prop
->opaque
);
292 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
294 ObjectProperty
*prop
;
296 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
297 if (!strstart(prop
->type
, "child<", NULL
)) {
301 if (prop
->opaque
== child
) {
302 object_property_del(obj
, prop
->name
, errp
);
307 void object_unparent(Object
*obj
)
310 object_property_del_child(obj
->parent
, obj
, NULL
);
314 static void object_deinit(Object
*obj
, TypeImpl
*type
)
316 if (type
->instance_finalize
) {
317 type
->instance_finalize(obj
);
320 while (obj
->interfaces
) {
321 Interface
*iface_obj
= obj
->interfaces
->data
;
322 obj
->interfaces
= g_slist_delete_link(obj
->interfaces
, obj
->interfaces
);
323 object_delete(OBJECT(iface_obj
));
326 if (type_has_parent(type
)) {
327 object_deinit(obj
, type_get_parent(type
));
330 object_unparent(obj
);
333 void object_finalize(void *data
)
336 TypeImpl
*ti
= obj
->class->type
;
338 object_deinit(obj
, ti
);
339 object_property_del_all(obj
);
341 g_assert(obj
->ref
== 0);
344 Object
*object_new_with_type(Type type
)
348 g_assert(type
!= NULL
);
350 obj
= g_malloc(type
->instance_size
);
351 object_initialize_with_type(obj
, type
);
357 Object
*object_new(const char *typename
)
359 TypeImpl
*ti
= type_get_by_name(typename
);
361 return object_new_with_type(ti
);
364 void object_delete(Object
*obj
)
367 g_assert(obj
->ref
== 0);
371 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
375 /* Check if typename is a direct ancestor of type */
377 if (type
== target_type
) {
381 type
= type_get_parent(type
);
387 static bool object_is_type(Object
*obj
, const char *typename
)
389 TypeImpl
*target_type
;
391 if (typename
== TYPE_OBJECT
) {
394 target_type
= type_get_by_name(typename
);
395 return type_is_ancestor(obj
->class->type
, target_type
);
398 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
402 /* Check if typename is a direct ancestor. Special-case TYPE_OBJECT,
403 * we want to go back from interfaces to the parent.
405 if (typename
&& object_is_type(obj
, typename
)) {
409 /* Check if obj is an interface and its containing object is a direct
410 * ancestor of typename. In principle we could do this test at the very
411 * beginning of object_dynamic_cast, avoiding a second call to
412 * object_is_type. However, casting between interfaces is relatively
413 * rare, and object_is_type(obj, TYPE_INTERFACE) would fail almost always.
415 * Perhaps we could add a magic value to the object header for increased
416 * (run-time) type safety and to speed up tests like this one. If we ever
417 * do that we can revisit the order here.
419 if (object_is_type(obj
, TYPE_INTERFACE
)) {
420 assert(!obj
->interfaces
);
421 obj
= INTERFACE(obj
)->obj
;
422 if (object_is_type(obj
, typename
)) {
427 if (typename
== TYPE_OBJECT
) {
431 /* Check if obj has an interface of typename */
432 for (i
= obj
->interfaces
; i
; i
= i
->next
) {
433 Interface
*iface
= i
->data
;
435 if (object_is_type(OBJECT(iface
), typename
)) {
436 return OBJECT(iface
);
444 static void register_interface(void)
446 static TypeInfo interface_info
= {
447 .name
= TYPE_INTERFACE
,
448 .instance_size
= sizeof(Interface
),
452 type_register_static(&interface_info
);
455 device_init(register_interface
);
457 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
)
461 inst
= object_dynamic_cast(obj
, typename
);
464 fprintf(stderr
, "Object %p is not an instance of type %s\n",
472 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
473 const char *typename
)
475 TypeImpl
*target_type
= type_get_by_name(typename
);
476 TypeImpl
*type
= class->type
;
479 if (type
== target_type
) {
483 type
= type_get_parent(type
);
489 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
490 const char *typename
)
492 ObjectClass
*ret
= object_class_dynamic_cast(class, typename
);
495 fprintf(stderr
, "Object %p is not an instance of type %s\n",
503 const char *object_get_typename(Object
*obj
)
505 return obj
->class->type
->name
;
508 ObjectClass
*object_get_class(Object
*obj
)
513 const char *object_class_get_name(ObjectClass
*klass
)
515 return klass
->type
->name
;
518 ObjectClass
*object_class_by_name(const char *typename
)
520 TypeImpl
*type
= type_get_by_name(typename
);
526 type_class_init(type
);
531 typedef struct OCFData
533 void (*fn
)(ObjectClass
*klass
, void *opaque
);
534 const char *implements_type
;
535 bool include_abstract
;
539 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
542 OCFData
*data
= opaque
;
543 TypeImpl
*type
= value
;
546 type_class_init(type
);
549 if (!data
->include_abstract
&& type
->abstract
) {
553 if (data
->implements_type
&&
554 !object_class_dynamic_cast(k
, data
->implements_type
)) {
558 data
->fn(k
, data
->opaque
);
561 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
562 const char *implements_type
, bool include_abstract
,
565 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
567 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
570 void object_ref(Object
*obj
)
575 void object_unref(Object
*obj
)
577 g_assert(obj
->ref
> 0);
580 /* parent always holds a reference to its children */
582 object_finalize(obj
);
586 void object_property_add(Object
*obj
, const char *name
, const char *type
,
587 ObjectPropertyAccessor
*get
,
588 ObjectPropertyAccessor
*set
,
589 ObjectPropertyRelease
*release
,
590 void *opaque
, Error
**errp
)
592 ObjectProperty
*prop
= g_malloc0(sizeof(*prop
));
594 prop
->name
= g_strdup(name
);
595 prop
->type
= g_strdup(type
);
599 prop
->release
= release
;
600 prop
->opaque
= opaque
;
602 QTAILQ_INSERT_TAIL(&obj
->properties
, prop
, node
);
605 static ObjectProperty
*object_property_find(Object
*obj
, const char *name
)
607 ObjectProperty
*prop
;
609 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
610 if (strcmp(prop
->name
, name
) == 0) {
618 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
620 ObjectProperty
*prop
= object_property_find(obj
, name
);
622 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
624 prop
->release(obj
, prop
->name
, prop
->opaque
);
631 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
634 ObjectProperty
*prop
= object_property_find(obj
, name
);
637 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, "", name
);
642 error_set(errp
, QERR_PERMISSION_DENIED
);
644 prop
->get(obj
, v
, prop
->opaque
, name
, errp
);
648 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
651 ObjectProperty
*prop
= object_property_find(obj
, name
);
654 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, "", name
);
659 error_set(errp
, QERR_PERMISSION_DENIED
);
661 prop
->set(obj
, v
, prop
->opaque
, name
, errp
);
665 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
667 ObjectProperty
*prop
= object_property_find(obj
, name
);
670 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, "", name
);
677 Object
*object_get_root(void)
682 root
= object_new("container");
688 static void object_get_child_property(Object
*obj
, Visitor
*v
, void *opaque
,
689 const char *name
, Error
**errp
)
691 Object
*child
= opaque
;
694 path
= object_get_canonical_path(child
);
695 visit_type_str(v
, &path
, name
, errp
);
699 static void object_finalize_child_property(Object
*obj
, const char *name
,
702 Object
*child
= opaque
;
707 void object_property_add_child(Object
*obj
, const char *name
,
708 Object
*child
, Error
**errp
)
712 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
714 object_property_add(obj
, name
, type
, object_get_child_property
,
715 NULL
, object_finalize_child_property
, child
, errp
);
718 g_assert(child
->parent
== NULL
);
724 static void object_get_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
725 const char *name
, Error
**errp
)
727 Object
**child
= opaque
;
731 path
= object_get_canonical_path(*child
);
732 visit_type_str(v
, &path
, name
, errp
);
736 visit_type_str(v
, &path
, name
, errp
);
740 static void object_set_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
741 const char *name
, Error
**errp
)
743 Object
**child
= opaque
;
744 bool ambiguous
= false;
748 type
= object_property_get_type(obj
, name
, NULL
);
750 visit_type_str(v
, &path
, name
, errp
);
753 object_unref(*child
);
756 if (strcmp(path
, "") != 0) {
759 target
= object_resolve_path(path
, &ambiguous
);
763 target_type
= g_strdup(&type
[5]);
764 target_type
[strlen(target_type
) - 2] = 0;
766 if (object_dynamic_cast(target
, target_type
)) {
770 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, type
);
775 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
784 void object_property_add_link(Object
*obj
, const char *name
,
785 const char *type
, Object
**child
,
790 full_type
= g_strdup_printf("link<%s>", type
);
792 object_property_add(obj
, name
, full_type
,
793 object_get_link_property
,
794 object_set_link_property
,
800 gchar
*object_get_canonical_path(Object
*obj
)
802 Object
*root
= object_get_root();
803 char *newpath
= NULL
, *path
= NULL
;
805 while (obj
!= root
) {
806 ObjectProperty
*prop
= NULL
;
808 g_assert(obj
->parent
!= NULL
);
810 QTAILQ_FOREACH(prop
, &obj
->parent
->properties
, node
) {
811 if (!strstart(prop
->type
, "child<", NULL
)) {
815 if (prop
->opaque
== obj
) {
817 newpath
= g_strdup_printf("%s/%s", prop
->name
, path
);
821 path
= g_strdup(prop
->name
);
827 g_assert(prop
!= NULL
);
832 newpath
= g_strdup_printf("/%s", path
);
838 static Object
*object_resolve_abs_path(Object
*parent
,
842 ObjectProperty
*prop
;
845 if (parts
[index
] == NULL
) {
849 if (strcmp(parts
[index
], "") == 0) {
850 return object_resolve_abs_path(parent
, parts
, index
+ 1);
853 prop
= object_property_find(parent
, parts
[index
]);
859 if (strstart(prop
->type
, "link<", NULL
)) {
860 Object
**pchild
= prop
->opaque
;
864 } else if (strstart(prop
->type
, "child<", NULL
)) {
865 child
= prop
->opaque
;
872 return object_resolve_abs_path(child
, parts
, index
+ 1);
875 static Object
*object_resolve_partial_path(Object
*parent
,
880 ObjectProperty
*prop
;
882 obj
= object_resolve_abs_path(parent
, parts
, 0);
884 QTAILQ_FOREACH(prop
, &parent
->properties
, node
) {
887 if (!strstart(prop
->type
, "child<", NULL
)) {
891 found
= object_resolve_partial_path(prop
->opaque
, parts
, ambiguous
);
902 if (ambiguous
&& *ambiguous
) {
910 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
912 bool partial_path
= true;
916 parts
= g_strsplit(path
, "/", 0);
917 if (parts
== NULL
|| parts
[0] == NULL
) {
919 return object_get_root();
922 if (strcmp(parts
[0], "") == 0) {
923 partial_path
= false;
930 obj
= object_resolve_partial_path(object_get_root(), parts
, ambiguous
);
932 obj
= object_resolve_abs_path(object_get_root(), parts
, 1);
940 typedef struct StringProperty
942 char *(*get
)(Object
*, Error
**);
943 void (*set
)(Object
*, const char *, Error
**);
946 static void object_property_get_str(Object
*obj
, Visitor
*v
, void *opaque
,
947 const char *name
, Error
**errp
)
949 StringProperty
*prop
= opaque
;
952 value
= prop
->get(obj
, errp
);
954 visit_type_str(v
, &value
, name
, errp
);
959 static void object_property_set_str(Object
*obj
, Visitor
*v
, void *opaque
,
960 const char *name
, Error
**errp
)
962 StringProperty
*prop
= opaque
;
964 Error
*local_err
= NULL
;
966 visit_type_str(v
, &value
, name
, &local_err
);
968 error_propagate(errp
, local_err
);
972 prop
->set(obj
, value
, errp
);
976 static void object_property_release_str(Object
*obj
, const char *name
,
979 StringProperty
*prop
= opaque
;
983 void object_property_add_str(Object
*obj
, const char *name
,
984 char *(*get
)(Object
*, Error
**),
985 void (*set
)(Object
*, const char *, Error
**),
988 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
993 object_property_add(obj
, name
, "string",
994 get
? object_property_get_str
: NULL
,
995 set
? object_property_set_str
: NULL
,
996 object_property_release_str
,