target/riscv: vector single-width averaging add and subtract
[qemu/ar7.git] / qom / object.c
blob6ece96bc2bfc0555ed6e1f2b1f0b93f43eb099e0
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/osdep.h"
14 #include "hw/qdev-core.h"
15 #include "qapi/error.h"
16 #include "qom/object.h"
17 #include "qom/object_interfaces.h"
18 #include "qemu/cutils.h"
19 #include "qapi/visitor.h"
20 #include "qapi/string-input-visitor.h"
21 #include "qapi/string-output-visitor.h"
22 #include "qapi/qobject-input-visitor.h"
23 #include "qapi/qapi-builtin-visit.h"
24 #include "qapi/qmp/qerror.h"
25 #include "qapi/qmp/qjson.h"
26 #include "trace.h"
28 /* TODO: replace QObject with a simpler visitor to avoid a dependency
29 * of the QOM core on QObject? */
30 #include "qom/qom-qobject.h"
31 #include "qapi/qmp/qbool.h"
32 #include "qapi/qmp/qnum.h"
33 #include "qapi/qmp/qstring.h"
34 #include "qemu/error-report.h"
36 #define MAX_INTERFACES 32
38 typedef struct InterfaceImpl InterfaceImpl;
39 typedef struct TypeImpl TypeImpl;
41 struct InterfaceImpl
43 const char *typename;
46 struct TypeImpl
48 const char *name;
50 size_t class_size;
52 size_t instance_size;
54 void (*class_init)(ObjectClass *klass, void *data);
55 void (*class_base_init)(ObjectClass *klass, void *data);
57 void *class_data;
59 void (*instance_init)(Object *obj);
60 void (*instance_post_init)(Object *obj);
61 void (*instance_finalize)(Object *obj);
63 bool abstract;
65 const char *parent;
66 TypeImpl *parent_type;
68 ObjectClass *class;
70 int num_interfaces;
71 InterfaceImpl interfaces[MAX_INTERFACES];
74 static Type type_interface;
76 static GHashTable *type_table_get(void)
78 static GHashTable *type_table;
80 if (type_table == NULL) {
81 type_table = g_hash_table_new(g_str_hash, g_str_equal);
84 return type_table;
87 static bool enumerating_types;
89 static void type_table_add(TypeImpl *ti)
91 assert(!enumerating_types);
92 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
95 static TypeImpl *type_table_lookup(const char *name)
97 return g_hash_table_lookup(type_table_get(), name);
100 static TypeImpl *type_new(const TypeInfo *info)
102 TypeImpl *ti = g_malloc0(sizeof(*ti));
103 int i;
105 g_assert(info->name != NULL);
107 if (type_table_lookup(info->name) != NULL) {
108 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
109 abort();
112 ti->name = g_strdup(info->name);
113 ti->parent = g_strdup(info->parent);
115 ti->class_size = info->class_size;
116 ti->instance_size = info->instance_size;
118 ti->class_init = info->class_init;
119 ti->class_base_init = info->class_base_init;
120 ti->class_data = info->class_data;
122 ti->instance_init = info->instance_init;
123 ti->instance_post_init = info->instance_post_init;
124 ti->instance_finalize = info->instance_finalize;
126 ti->abstract = info->abstract;
128 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
129 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
131 ti->num_interfaces = i;
133 return ti;
136 static TypeImpl *type_register_internal(const TypeInfo *info)
138 TypeImpl *ti;
139 ti = type_new(info);
141 type_table_add(ti);
142 return ti;
145 TypeImpl *type_register(const TypeInfo *info)
147 assert(info->parent);
148 return type_register_internal(info);
151 TypeImpl *type_register_static(const TypeInfo *info)
153 return type_register(info);
156 void type_register_static_array(const TypeInfo *infos, int nr_infos)
158 int i;
160 for (i = 0; i < nr_infos; i++) {
161 type_register_static(&infos[i]);
165 static TypeImpl *type_get_by_name(const char *name)
167 if (name == NULL) {
168 return NULL;
171 return type_table_lookup(name);
174 static TypeImpl *type_get_parent(TypeImpl *type)
176 if (!type->parent_type && type->parent) {
177 type->parent_type = type_get_by_name(type->parent);
178 if (!type->parent_type) {
179 fprintf(stderr, "Type '%s' is missing its parent '%s'\n",
180 type->name, type->parent);
181 abort();
185 return type->parent_type;
188 static bool type_has_parent(TypeImpl *type)
190 return (type->parent != NULL);
193 static size_t type_class_get_size(TypeImpl *ti)
195 if (ti->class_size) {
196 return ti->class_size;
199 if (type_has_parent(ti)) {
200 return type_class_get_size(type_get_parent(ti));
203 return sizeof(ObjectClass);
206 static size_t type_object_get_size(TypeImpl *ti)
208 if (ti->instance_size) {
209 return ti->instance_size;
212 if (type_has_parent(ti)) {
213 return type_object_get_size(type_get_parent(ti));
216 return 0;
219 size_t object_type_get_instance_size(const char *typename)
221 TypeImpl *type = type_get_by_name(typename);
223 g_assert(type != NULL);
224 return type_object_get_size(type);
227 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
229 assert(target_type);
231 /* Check if target_type is a direct ancestor of type */
232 while (type) {
233 if (type == target_type) {
234 return true;
237 type = type_get_parent(type);
240 return false;
243 static void type_initialize(TypeImpl *ti);
245 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
246 TypeImpl *parent_type)
248 InterfaceClass *new_iface;
249 TypeInfo info = { };
250 TypeImpl *iface_impl;
252 info.parent = parent_type->name;
253 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
254 info.abstract = true;
256 iface_impl = type_new(&info);
257 iface_impl->parent_type = parent_type;
258 type_initialize(iface_impl);
259 g_free((char *)info.name);
261 new_iface = (InterfaceClass *)iface_impl->class;
262 new_iface->concrete_class = ti->class;
263 new_iface->interface_type = interface_type;
265 ti->class->interfaces = g_slist_append(ti->class->interfaces, new_iface);
268 static void object_property_free(gpointer data)
270 ObjectProperty *prop = data;
272 if (prop->defval) {
273 qobject_unref(prop->defval);
274 prop->defval = NULL;
276 g_free(prop->name);
277 g_free(prop->type);
278 g_free(prop->description);
279 g_free(prop);
282 static void type_initialize(TypeImpl *ti)
284 TypeImpl *parent;
286 if (ti->class) {
287 return;
290 ti->class_size = type_class_get_size(ti);
291 ti->instance_size = type_object_get_size(ti);
292 /* Any type with zero instance_size is implicitly abstract.
293 * This means interface types are all abstract.
295 if (ti->instance_size == 0) {
296 ti->abstract = true;
298 if (type_is_ancestor(ti, type_interface)) {
299 assert(ti->instance_size == 0);
300 assert(ti->abstract);
301 assert(!ti->instance_init);
302 assert(!ti->instance_post_init);
303 assert(!ti->instance_finalize);
304 assert(!ti->num_interfaces);
306 ti->class = g_malloc0(ti->class_size);
308 parent = type_get_parent(ti);
309 if (parent) {
310 type_initialize(parent);
311 GSList *e;
312 int i;
314 g_assert(parent->class_size <= ti->class_size);
315 g_assert(parent->instance_size <= ti->instance_size);
316 memcpy(ti->class, parent->class, parent->class_size);
317 ti->class->interfaces = NULL;
319 for (e = parent->class->interfaces; e; e = e->next) {
320 InterfaceClass *iface = e->data;
321 ObjectClass *klass = OBJECT_CLASS(iface);
323 type_initialize_interface(ti, iface->interface_type, klass->type);
326 for (i = 0; i < ti->num_interfaces; i++) {
327 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
328 if (!t) {
329 error_report("missing interface '%s' for object '%s'",
330 ti->interfaces[i].typename, parent->name);
331 abort();
333 for (e = ti->class->interfaces; e; e = e->next) {
334 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
336 if (type_is_ancestor(target_type, t)) {
337 break;
341 if (e) {
342 continue;
345 type_initialize_interface(ti, t, t);
349 ti->class->properties = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
350 object_property_free);
352 ti->class->type = ti;
354 while (parent) {
355 if (parent->class_base_init) {
356 parent->class_base_init(ti->class, ti->class_data);
358 parent = type_get_parent(parent);
361 if (ti->class_init) {
362 ti->class_init(ti->class, ti->class_data);
366 static void object_init_with_type(Object *obj, TypeImpl *ti)
368 if (type_has_parent(ti)) {
369 object_init_with_type(obj, type_get_parent(ti));
372 if (ti->instance_init) {
373 ti->instance_init(obj);
377 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
379 if (ti->instance_post_init) {
380 ti->instance_post_init(obj);
383 if (type_has_parent(ti)) {
384 object_post_init_with_type(obj, type_get_parent(ti));
388 void object_apply_global_props(Object *obj, const GPtrArray *props, Error **errp)
390 int i;
392 if (!props) {
393 return;
396 for (i = 0; i < props->len; i++) {
397 GlobalProperty *p = g_ptr_array_index(props, i);
398 Error *err = NULL;
400 if (object_dynamic_cast(obj, p->driver) == NULL) {
401 continue;
403 if (p->optional && !object_property_find(obj, p->property, NULL)) {
404 continue;
406 p->used = true;
407 object_property_parse(obj, p->value, p->property, &err);
408 if (err != NULL) {
409 error_prepend(&err, "can't apply global %s.%s=%s: ",
410 p->driver, p->property, p->value);
412 * If errp != NULL, propagate error and return.
413 * If errp == NULL, report a warning, but keep going
414 * with the remaining globals.
416 if (errp) {
417 error_propagate(errp, err);
418 return;
419 } else {
420 warn_report_err(err);
427 * Global property defaults
428 * Slot 0: accelerator's global property defaults
429 * Slot 1: machine's global property defaults
430 * Slot 2: global properties from legacy command line option
431 * Each is a GPtrArray of of GlobalProperty.
432 * Applied in order, later entries override earlier ones.
434 static GPtrArray *object_compat_props[3];
437 * Retrieve @GPtrArray for global property defined with options
438 * other than "-global". These are generally used for syntactic
439 * sugar and legacy command line options.
441 void object_register_sugar_prop(const char *driver, const char *prop, const char *value)
443 GlobalProperty *g;
444 if (!object_compat_props[2]) {
445 object_compat_props[2] = g_ptr_array_new();
447 g = g_new0(GlobalProperty, 1);
448 g->driver = g_strdup(driver);
449 g->property = g_strdup(prop);
450 g->value = g_strdup(value);
451 g_ptr_array_add(object_compat_props[2], g);
455 * Set machine's global property defaults to @compat_props.
456 * May be called at most once.
458 void object_set_machine_compat_props(GPtrArray *compat_props)
460 assert(!object_compat_props[1]);
461 object_compat_props[1] = compat_props;
465 * Set accelerator's global property defaults to @compat_props.
466 * May be called at most once.
468 void object_set_accelerator_compat_props(GPtrArray *compat_props)
470 assert(!object_compat_props[0]);
471 object_compat_props[0] = compat_props;
474 void object_apply_compat_props(Object *obj)
476 int i;
478 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
479 object_apply_global_props(obj, object_compat_props[i],
480 i == 2 ? &error_fatal : &error_abort);
484 static void object_class_property_init_all(Object *obj)
486 ObjectPropertyIterator iter;
487 ObjectProperty *prop;
489 object_class_property_iter_init(&iter, object_get_class(obj));
490 while ((prop = object_property_iter_next(&iter))) {
491 if (prop->init) {
492 prop->init(obj, prop);
497 static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type)
499 type_initialize(type);
501 g_assert(type->instance_size >= sizeof(Object));
502 g_assert(type->abstract == false);
503 g_assert(size >= type->instance_size);
505 memset(obj, 0, type->instance_size);
506 obj->class = type->class;
507 object_ref(obj);
508 object_class_property_init_all(obj);
509 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
510 NULL, object_property_free);
511 object_init_with_type(obj, type);
512 object_post_init_with_type(obj, type);
515 void object_initialize(void *data, size_t size, const char *typename)
517 TypeImpl *type = type_get_by_name(typename);
519 if (!type) {
520 error_report("missing object type '%s'", typename);
521 abort();
524 object_initialize_with_type(data, size, type);
527 void object_initialize_child_with_props(Object *parentobj,
528 const char *propname,
529 void *childobj, size_t size, const char *type,
530 Error **errp, ...)
532 va_list vargs;
534 va_start(vargs, errp);
535 object_initialize_child_with_propsv(parentobj, propname,
536 childobj, size, type, errp, vargs);
537 va_end(vargs);
540 void object_initialize_child_with_propsv(Object *parentobj,
541 const char *propname,
542 void *childobj, size_t size, const char *type,
543 Error **errp, va_list vargs)
545 Error *local_err = NULL;
546 Object *obj;
547 UserCreatable *uc;
549 object_initialize(childobj, size, type);
550 obj = OBJECT(childobj);
552 object_set_propv(obj, &local_err, vargs);
553 if (local_err) {
554 goto out;
557 object_property_add_child(parentobj, propname, obj);
559 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
560 if (uc) {
561 user_creatable_complete(uc, &local_err);
562 if (local_err) {
563 object_unparent(obj);
564 goto out;
568 out:
570 * We want @obj's reference to be 1 on success, 0 on failure.
571 * On success, it's 2: one taken by object_initialize(), and one
572 * by object_property_add_child().
573 * On failure in object_initialize() or earlier, it's 1.
574 * On failure afterwards, it's also 1: object_unparent() releases
575 * the reference taken by object_property_add_child().
577 object_unref(obj);
579 error_propagate(errp, local_err);
582 void object_initialize_child_internal(Object *parent,
583 const char *propname,
584 void *child, size_t size,
585 const char *type)
587 object_initialize_child_with_props(parent, propname, child, size, type,
588 &error_abort, NULL);
591 static inline bool object_property_is_child(ObjectProperty *prop)
593 return strstart(prop->type, "child<", NULL);
596 static void object_property_del_all(Object *obj)
598 g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL);
599 ObjectProperty *prop;
600 ObjectPropertyIterator iter;
601 bool released;
603 do {
604 released = false;
605 object_property_iter_init(&iter, obj);
606 while ((prop = object_property_iter_next(&iter)) != NULL) {
607 if (g_hash_table_add(done, prop)) {
608 if (prop->release) {
609 prop->release(obj, prop->name, prop->opaque);
610 released = true;
611 break;
615 } while (released);
617 g_hash_table_unref(obj->properties);
620 static void object_property_del_child(Object *obj, Object *child)
622 ObjectProperty *prop;
623 GHashTableIter iter;
624 gpointer key, value;
626 g_hash_table_iter_init(&iter, obj->properties);
627 while (g_hash_table_iter_next(&iter, &key, &value)) {
628 prop = value;
629 if (object_property_is_child(prop) && prop->opaque == child) {
630 if (prop->release) {
631 prop->release(obj, prop->name, prop->opaque);
632 prop->release = NULL;
634 break;
637 g_hash_table_iter_init(&iter, obj->properties);
638 while (g_hash_table_iter_next(&iter, &key, &value)) {
639 prop = value;
640 if (object_property_is_child(prop) && prop->opaque == child) {
641 g_hash_table_iter_remove(&iter);
642 break;
647 void object_unparent(Object *obj)
649 if (obj->parent) {
650 object_property_del_child(obj->parent, obj);
654 static void object_deinit(Object *obj, TypeImpl *type)
656 if (type->instance_finalize) {
657 type->instance_finalize(obj);
660 if (type_has_parent(type)) {
661 object_deinit(obj, type_get_parent(type));
665 static void object_finalize(void *data)
667 Object *obj = data;
668 TypeImpl *ti = obj->class->type;
670 object_property_del_all(obj);
671 object_deinit(obj, ti);
673 g_assert(obj->ref == 0);
674 if (obj->free) {
675 obj->free(obj);
679 static Object *object_new_with_type(Type type)
681 Object *obj;
683 g_assert(type != NULL);
684 type_initialize(type);
686 obj = g_malloc(type->instance_size);
687 object_initialize_with_type(obj, type->instance_size, type);
688 obj->free = g_free;
690 return obj;
693 Object *object_new_with_class(ObjectClass *klass)
695 return object_new_with_type(klass->type);
698 Object *object_new(const char *typename)
700 TypeImpl *ti = type_get_by_name(typename);
702 return object_new_with_type(ti);
706 Object *object_new_with_props(const char *typename,
707 Object *parent,
708 const char *id,
709 Error **errp,
710 ...)
712 va_list vargs;
713 Object *obj;
715 va_start(vargs, errp);
716 obj = object_new_with_propv(typename, parent, id, errp, vargs);
717 va_end(vargs);
719 return obj;
723 Object *object_new_with_propv(const char *typename,
724 Object *parent,
725 const char *id,
726 Error **errp,
727 va_list vargs)
729 Object *obj;
730 ObjectClass *klass;
731 Error *local_err = NULL;
732 UserCreatable *uc;
734 klass = object_class_by_name(typename);
735 if (!klass) {
736 error_setg(errp, "invalid object type: %s", typename);
737 return NULL;
740 if (object_class_is_abstract(klass)) {
741 error_setg(errp, "object type '%s' is abstract", typename);
742 return NULL;
744 obj = object_new_with_type(klass->type);
746 if (object_set_propv(obj, &local_err, vargs) < 0) {
747 goto error;
750 if (id != NULL) {
751 object_property_add_child(parent, id, obj);
754 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
755 if (uc) {
756 user_creatable_complete(uc, &local_err);
757 if (local_err) {
758 if (id != NULL) {
759 object_unparent(obj);
761 goto error;
765 object_unref(obj);
766 return obj;
768 error:
769 error_propagate(errp, local_err);
770 object_unref(obj);
771 return NULL;
775 int object_set_props(Object *obj,
776 Error **errp,
777 ...)
779 va_list vargs;
780 int ret;
782 va_start(vargs, errp);
783 ret = object_set_propv(obj, errp, vargs);
784 va_end(vargs);
786 return ret;
790 int object_set_propv(Object *obj,
791 Error **errp,
792 va_list vargs)
794 const char *propname;
795 Error *local_err = NULL;
797 propname = va_arg(vargs, char *);
798 while (propname != NULL) {
799 const char *value = va_arg(vargs, char *);
801 g_assert(value != NULL);
802 object_property_parse(obj, value, propname, &local_err);
803 if (local_err) {
804 error_propagate(errp, local_err);
805 return -1;
807 propname = va_arg(vargs, char *);
810 return 0;
814 Object *object_dynamic_cast(Object *obj, const char *typename)
816 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
817 return obj;
820 return NULL;
823 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
824 const char *file, int line, const char *func)
826 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
827 typename, file, line, func);
829 #ifdef CONFIG_QOM_CAST_DEBUG
830 int i;
831 Object *inst;
833 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
834 if (atomic_read(&obj->class->object_cast_cache[i]) == typename) {
835 goto out;
839 inst = object_dynamic_cast(obj, typename);
841 if (!inst && obj) {
842 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
843 file, line, func, obj, typename);
844 abort();
847 assert(obj == inst);
849 if (obj && obj == inst) {
850 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
851 atomic_set(&obj->class->object_cast_cache[i - 1],
852 atomic_read(&obj->class->object_cast_cache[i]));
854 atomic_set(&obj->class->object_cast_cache[i - 1], typename);
857 out:
858 #endif
859 return obj;
862 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
863 const char *typename)
865 ObjectClass *ret = NULL;
866 TypeImpl *target_type;
867 TypeImpl *type;
869 if (!class) {
870 return NULL;
873 /* A simple fast path that can trigger a lot for leaf classes. */
874 type = class->type;
875 if (type->name == typename) {
876 return class;
879 target_type = type_get_by_name(typename);
880 if (!target_type) {
881 /* target class type unknown, so fail the cast */
882 return NULL;
885 if (type->class->interfaces &&
886 type_is_ancestor(target_type, type_interface)) {
887 int found = 0;
888 GSList *i;
890 for (i = class->interfaces; i; i = i->next) {
891 ObjectClass *target_class = i->data;
893 if (type_is_ancestor(target_class->type, target_type)) {
894 ret = target_class;
895 found++;
899 /* The match was ambiguous, don't allow a cast */
900 if (found > 1) {
901 ret = NULL;
903 } else if (type_is_ancestor(type, target_type)) {
904 ret = class;
907 return ret;
910 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
911 const char *typename,
912 const char *file, int line,
913 const char *func)
915 ObjectClass *ret;
917 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
918 typename, file, line, func);
920 #ifdef CONFIG_QOM_CAST_DEBUG
921 int i;
923 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
924 if (atomic_read(&class->class_cast_cache[i]) == typename) {
925 ret = class;
926 goto out;
929 #else
930 if (!class || !class->interfaces) {
931 return class;
933 #endif
935 ret = object_class_dynamic_cast(class, typename);
936 if (!ret && class) {
937 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
938 file, line, func, class, typename);
939 abort();
942 #ifdef CONFIG_QOM_CAST_DEBUG
943 if (class && ret == class) {
944 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
945 atomic_set(&class->class_cast_cache[i - 1],
946 atomic_read(&class->class_cast_cache[i]));
948 atomic_set(&class->class_cast_cache[i - 1], typename);
950 out:
951 #endif
952 return ret;
955 const char *object_get_typename(const Object *obj)
957 return obj->class->type->name;
960 ObjectClass *object_get_class(Object *obj)
962 return obj->class;
965 bool object_class_is_abstract(ObjectClass *klass)
967 return klass->type->abstract;
970 const char *object_class_get_name(ObjectClass *klass)
972 return klass->type->name;
975 ObjectClass *object_class_by_name(const char *typename)
977 TypeImpl *type = type_get_by_name(typename);
979 if (!type) {
980 return NULL;
983 type_initialize(type);
985 return type->class;
988 ObjectClass *object_class_get_parent(ObjectClass *class)
990 TypeImpl *type = type_get_parent(class->type);
992 if (!type) {
993 return NULL;
996 type_initialize(type);
998 return type->class;
1001 typedef struct OCFData
1003 void (*fn)(ObjectClass *klass, void *opaque);
1004 const char *implements_type;
1005 bool include_abstract;
1006 void *opaque;
1007 } OCFData;
1009 static void object_class_foreach_tramp(gpointer key, gpointer value,
1010 gpointer opaque)
1012 OCFData *data = opaque;
1013 TypeImpl *type = value;
1014 ObjectClass *k;
1016 type_initialize(type);
1017 k = type->class;
1019 if (!data->include_abstract && type->abstract) {
1020 return;
1023 if (data->implements_type &&
1024 !object_class_dynamic_cast(k, data->implements_type)) {
1025 return;
1028 data->fn(k, data->opaque);
1031 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
1032 const char *implements_type, bool include_abstract,
1033 void *opaque)
1035 OCFData data = { fn, implements_type, include_abstract, opaque };
1037 enumerating_types = true;
1038 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
1039 enumerating_types = false;
1042 static int do_object_child_foreach(Object *obj,
1043 int (*fn)(Object *child, void *opaque),
1044 void *opaque, bool recurse)
1046 GHashTableIter iter;
1047 ObjectProperty *prop;
1048 int ret = 0;
1050 g_hash_table_iter_init(&iter, obj->properties);
1051 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1052 if (object_property_is_child(prop)) {
1053 Object *child = prop->opaque;
1055 ret = fn(child, opaque);
1056 if (ret != 0) {
1057 break;
1059 if (recurse) {
1060 ret = do_object_child_foreach(child, fn, opaque, true);
1061 if (ret != 0) {
1062 break;
1067 return ret;
1070 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1071 void *opaque)
1073 return do_object_child_foreach(obj, fn, opaque, false);
1076 int object_child_foreach_recursive(Object *obj,
1077 int (*fn)(Object *child, void *opaque),
1078 void *opaque)
1080 return do_object_child_foreach(obj, fn, opaque, true);
1083 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
1085 GSList **list = opaque;
1087 *list = g_slist_prepend(*list, klass);
1090 GSList *object_class_get_list(const char *implements_type,
1091 bool include_abstract)
1093 GSList *list = NULL;
1095 object_class_foreach(object_class_get_list_tramp,
1096 implements_type, include_abstract, &list);
1097 return list;
1100 static gint object_class_cmp(gconstpointer a, gconstpointer b)
1102 return strcasecmp(object_class_get_name((ObjectClass *)a),
1103 object_class_get_name((ObjectClass *)b));
1106 GSList *object_class_get_list_sorted(const char *implements_type,
1107 bool include_abstract)
1109 return g_slist_sort(object_class_get_list(implements_type, include_abstract),
1110 object_class_cmp);
1113 Object *object_ref(Object *obj)
1115 if (!obj) {
1116 return NULL;
1118 atomic_inc(&obj->ref);
1119 return obj;
1122 void object_unref(Object *obj)
1124 if (!obj) {
1125 return;
1127 g_assert(obj->ref > 0);
1129 /* parent always holds a reference to its children */
1130 if (atomic_fetch_dec(&obj->ref) == 1) {
1131 object_finalize(obj);
1135 static ObjectProperty *
1136 object_property_try_add(Object *obj, const char *name, const char *type,
1137 ObjectPropertyAccessor *get,
1138 ObjectPropertyAccessor *set,
1139 ObjectPropertyRelease *release,
1140 void *opaque, Error **errp)
1142 ObjectProperty *prop;
1143 size_t name_len = strlen(name);
1145 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
1146 int i;
1147 ObjectProperty *ret;
1148 char *name_no_array = g_strdup(name);
1150 name_no_array[name_len - 3] = '\0';
1151 for (i = 0; ; ++i) {
1152 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1154 ret = object_property_try_add(obj, full_name, type, get, set,
1155 release, opaque, NULL);
1156 g_free(full_name);
1157 if (ret) {
1158 break;
1161 g_free(name_no_array);
1162 return ret;
1165 if (object_property_find(obj, name, NULL) != NULL) {
1166 error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')",
1167 name, object_get_typename(obj));
1168 return NULL;
1171 prop = g_malloc0(sizeof(*prop));
1173 prop->name = g_strdup(name);
1174 prop->type = g_strdup(type);
1176 prop->get = get;
1177 prop->set = set;
1178 prop->release = release;
1179 prop->opaque = opaque;
1181 g_hash_table_insert(obj->properties, prop->name, prop);
1182 return prop;
1185 ObjectProperty *
1186 object_property_add(Object *obj, const char *name, const char *type,
1187 ObjectPropertyAccessor *get,
1188 ObjectPropertyAccessor *set,
1189 ObjectPropertyRelease *release,
1190 void *opaque)
1192 return object_property_try_add(obj, name, type, get, set, release,
1193 opaque, &error_abort);
1196 ObjectProperty *
1197 object_class_property_add(ObjectClass *klass,
1198 const char *name,
1199 const char *type,
1200 ObjectPropertyAccessor *get,
1201 ObjectPropertyAccessor *set,
1202 ObjectPropertyRelease *release,
1203 void *opaque)
1205 ObjectProperty *prop;
1207 assert(!object_class_property_find(klass, name, NULL));
1209 prop = g_malloc0(sizeof(*prop));
1211 prop->name = g_strdup(name);
1212 prop->type = g_strdup(type);
1214 prop->get = get;
1215 prop->set = set;
1216 prop->release = release;
1217 prop->opaque = opaque;
1219 g_hash_table_insert(klass->properties, prop->name, prop);
1221 return prop;
1224 ObjectProperty *object_property_find(Object *obj, const char *name,
1225 Error **errp)
1227 ObjectProperty *prop;
1228 ObjectClass *klass = object_get_class(obj);
1230 prop = object_class_property_find(klass, name, NULL);
1231 if (prop) {
1232 return prop;
1235 prop = g_hash_table_lookup(obj->properties, name);
1236 if (prop) {
1237 return prop;
1240 error_setg(errp, "Property '.%s' not found", name);
1241 return NULL;
1244 void object_property_iter_init(ObjectPropertyIterator *iter,
1245 Object *obj)
1247 g_hash_table_iter_init(&iter->iter, obj->properties);
1248 iter->nextclass = object_get_class(obj);
1251 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1253 gpointer key, val;
1254 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1255 if (!iter->nextclass) {
1256 return NULL;
1258 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1259 iter->nextclass = object_class_get_parent(iter->nextclass);
1261 return val;
1264 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1265 ObjectClass *klass)
1267 g_hash_table_iter_init(&iter->iter, klass->properties);
1268 iter->nextclass = object_class_get_parent(klass);
1271 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1272 Error **errp)
1274 ObjectProperty *prop;
1275 ObjectClass *parent_klass;
1277 parent_klass = object_class_get_parent(klass);
1278 if (parent_klass) {
1279 prop = object_class_property_find(parent_klass, name, NULL);
1280 if (prop) {
1281 return prop;
1285 prop = g_hash_table_lookup(klass->properties, name);
1286 if (!prop) {
1287 error_setg(errp, "Property '.%s' not found", name);
1289 return prop;
1292 void object_property_del(Object *obj, const char *name)
1294 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1296 if (prop->release) {
1297 prop->release(obj, name, prop->opaque);
1299 g_hash_table_remove(obj->properties, name);
1302 void object_property_get(Object *obj, Visitor *v, const char *name,
1303 Error **errp)
1305 ObjectProperty *prop = object_property_find(obj, name, errp);
1306 if (prop == NULL) {
1307 return;
1310 if (!prop->get) {
1311 error_setg(errp, QERR_PERMISSION_DENIED);
1312 } else {
1313 prop->get(obj, v, name, prop->opaque, errp);
1317 void object_property_set(Object *obj, Visitor *v, const char *name,
1318 Error **errp)
1320 ObjectProperty *prop = object_property_find(obj, name, errp);
1321 if (prop == NULL) {
1322 return;
1325 if (!prop->set) {
1326 error_setg(errp, QERR_PERMISSION_DENIED);
1327 } else {
1328 prop->set(obj, v, name, prop->opaque, errp);
1332 void object_property_set_str(Object *obj, const char *value,
1333 const char *name, Error **errp)
1335 QString *qstr = qstring_from_str(value);
1336 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
1338 qobject_unref(qstr);
1341 char *object_property_get_str(Object *obj, const char *name,
1342 Error **errp)
1344 QObject *ret = object_property_get_qobject(obj, name, errp);
1345 char *retval;
1347 if (!ret) {
1348 return NULL;
1351 retval = g_strdup(qobject_get_try_str(ret));
1352 if (!retval) {
1353 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
1356 qobject_unref(ret);
1357 return retval;
1360 void object_property_set_link(Object *obj, Object *value,
1361 const char *name, Error **errp)
1363 if (value) {
1364 char *path = object_get_canonical_path(value);
1365 object_property_set_str(obj, path, name, errp);
1366 g_free(path);
1367 } else {
1368 object_property_set_str(obj, "", name, errp);
1372 Object *object_property_get_link(Object *obj, const char *name,
1373 Error **errp)
1375 char *str = object_property_get_str(obj, name, errp);
1376 Object *target = NULL;
1378 if (str && *str) {
1379 target = object_resolve_path(str, NULL);
1380 if (!target) {
1381 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1382 "Device '%s' not found", str);
1386 g_free(str);
1387 return target;
1390 void object_property_set_bool(Object *obj, bool value,
1391 const char *name, Error **errp)
1393 QBool *qbool = qbool_from_bool(value);
1394 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
1396 qobject_unref(qbool);
1399 bool object_property_get_bool(Object *obj, const char *name,
1400 Error **errp)
1402 QObject *ret = object_property_get_qobject(obj, name, errp);
1403 QBool *qbool;
1404 bool retval;
1406 if (!ret) {
1407 return false;
1409 qbool = qobject_to(QBool, ret);
1410 if (!qbool) {
1411 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
1412 retval = false;
1413 } else {
1414 retval = qbool_get_bool(qbool);
1417 qobject_unref(ret);
1418 return retval;
1421 void object_property_set_int(Object *obj, int64_t value,
1422 const char *name, Error **errp)
1424 QNum *qnum = qnum_from_int(value);
1425 object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
1427 qobject_unref(qnum);
1430 int64_t object_property_get_int(Object *obj, const char *name,
1431 Error **errp)
1433 QObject *ret = object_property_get_qobject(obj, name, errp);
1434 QNum *qnum;
1435 int64_t retval;
1437 if (!ret) {
1438 return -1;
1441 qnum = qobject_to(QNum, ret);
1442 if (!qnum || !qnum_get_try_int(qnum, &retval)) {
1443 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
1444 retval = -1;
1447 qobject_unref(ret);
1448 return retval;
1451 static void object_property_init_defval(Object *obj, ObjectProperty *prop)
1453 Visitor *v = qobject_input_visitor_new(prop->defval);
1455 assert(prop->set != NULL);
1456 prop->set(obj, v, prop->name, prop->opaque, &error_abort);
1458 visit_free(v);
1461 static void object_property_set_default(ObjectProperty *prop, QObject *defval)
1463 assert(!prop->defval);
1464 assert(!prop->init);
1466 prop->defval = defval;
1467 prop->init = object_property_init_defval;
1470 void object_property_set_default_bool(ObjectProperty *prop, bool value)
1472 object_property_set_default(prop, QOBJECT(qbool_from_bool(value)));
1475 void object_property_set_default_str(ObjectProperty *prop, const char *value)
1477 object_property_set_default(prop, QOBJECT(qstring_from_str(value)));
1480 void object_property_set_default_int(ObjectProperty *prop, int64_t value)
1482 object_property_set_default(prop, QOBJECT(qnum_from_int(value)));
1485 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value)
1487 object_property_set_default(prop, QOBJECT(qnum_from_uint(value)));
1490 void object_property_set_uint(Object *obj, uint64_t value,
1491 const char *name, Error **errp)
1493 QNum *qnum = qnum_from_uint(value);
1495 object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
1496 qobject_unref(qnum);
1499 uint64_t object_property_get_uint(Object *obj, const char *name,
1500 Error **errp)
1502 QObject *ret = object_property_get_qobject(obj, name, errp);
1503 QNum *qnum;
1504 uint64_t retval;
1506 if (!ret) {
1507 return 0;
1509 qnum = qobject_to(QNum, ret);
1510 if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1511 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1512 retval = 0;
1515 qobject_unref(ret);
1516 return retval;
1519 typedef struct EnumProperty {
1520 const QEnumLookup *lookup;
1521 int (*get)(Object *, Error **);
1522 void (*set)(Object *, int, Error **);
1523 } EnumProperty;
1525 int object_property_get_enum(Object *obj, const char *name,
1526 const char *typename, Error **errp)
1528 char *str;
1529 int ret;
1530 ObjectProperty *prop = object_property_find(obj, name, errp);
1531 EnumProperty *enumprop;
1533 if (prop == NULL) {
1534 return 0;
1537 if (!g_str_equal(prop->type, typename)) {
1538 error_setg(errp, "Property %s on %s is not '%s' enum type",
1539 name, object_class_get_name(
1540 object_get_class(obj)), typename);
1541 return 0;
1544 enumprop = prop->opaque;
1546 str = object_property_get_str(obj, name, errp);
1547 if (!str) {
1548 return 0;
1551 ret = qapi_enum_parse(enumprop->lookup, str, -1, errp);
1552 g_free(str);
1554 return ret;
1557 void object_property_parse(Object *obj, const char *string,
1558 const char *name, Error **errp)
1560 Visitor *v = string_input_visitor_new(string);
1561 object_property_set(obj, v, name, errp);
1562 visit_free(v);
1565 char *object_property_print(Object *obj, const char *name, bool human,
1566 Error **errp)
1568 Visitor *v;
1569 char *string = NULL;
1570 Error *local_err = NULL;
1572 v = string_output_visitor_new(human, &string);
1573 object_property_get(obj, v, name, &local_err);
1574 if (local_err) {
1575 error_propagate(errp, local_err);
1576 goto out;
1579 visit_complete(v, &string);
1581 out:
1582 visit_free(v);
1583 return string;
1586 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1588 ObjectProperty *prop = object_property_find(obj, name, errp);
1589 if (prop == NULL) {
1590 return NULL;
1593 return prop->type;
1596 Object *object_get_root(void)
1598 static Object *root;
1600 if (!root) {
1601 root = object_new("container");
1604 return root;
1607 Object *object_get_objects_root(void)
1609 return container_get(object_get_root(), "/objects");
1612 Object *object_get_internal_root(void)
1614 static Object *internal_root;
1616 if (!internal_root) {
1617 internal_root = object_new("container");
1620 return internal_root;
1623 static void object_get_child_property(Object *obj, Visitor *v,
1624 const char *name, void *opaque,
1625 Error **errp)
1627 Object *child = opaque;
1628 char *path;
1630 path = object_get_canonical_path(child);
1631 visit_type_str(v, name, &path, errp);
1632 g_free(path);
1635 static Object *object_resolve_child_property(Object *parent, void *opaque,
1636 const char *part)
1638 return opaque;
1641 static void object_finalize_child_property(Object *obj, const char *name,
1642 void *opaque)
1644 Object *child = opaque;
1646 if (child->class->unparent) {
1647 (child->class->unparent)(child);
1649 child->parent = NULL;
1650 object_unref(child);
1653 ObjectProperty *
1654 object_property_add_child(Object *obj, const char *name,
1655 Object *child)
1657 g_autofree char *type = NULL;
1658 ObjectProperty *op;
1660 assert(!child->parent);
1662 type = g_strdup_printf("child<%s>", object_get_typename(child));
1664 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1665 object_finalize_child_property, child);
1666 op->resolve = object_resolve_child_property;
1667 object_ref(child);
1668 child->parent = obj;
1669 return op;
1672 void object_property_allow_set_link(const Object *obj, const char *name,
1673 Object *val, Error **errp)
1675 /* Allow the link to be set, always */
1678 typedef struct {
1679 union {
1680 Object **targetp;
1681 Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1682 ptrdiff_t offset; /* if OBJ_PROP_LINK_CLASS */
1684 void (*check)(const Object *, const char *, Object *, Error **);
1685 ObjectPropertyLinkFlags flags;
1686 } LinkProperty;
1688 static Object **
1689 object_link_get_targetp(Object *obj, LinkProperty *lprop)
1691 if (lprop->flags & OBJ_PROP_LINK_DIRECT) {
1692 return &lprop->target;
1693 } else if (lprop->flags & OBJ_PROP_LINK_CLASS) {
1694 return (void *)obj + lprop->offset;
1695 } else {
1696 return lprop->targetp;
1700 static void object_get_link_property(Object *obj, Visitor *v,
1701 const char *name, void *opaque,
1702 Error **errp)
1704 LinkProperty *lprop = opaque;
1705 Object **targetp = object_link_get_targetp(obj, lprop);
1706 char *path;
1708 if (*targetp) {
1709 path = object_get_canonical_path(*targetp);
1710 visit_type_str(v, name, &path, errp);
1711 g_free(path);
1712 } else {
1713 path = (char *)"";
1714 visit_type_str(v, name, &path, errp);
1719 * object_resolve_link:
1721 * Lookup an object and ensure its type matches the link property type. This
1722 * is similar to object_resolve_path() except type verification against the
1723 * link property is performed.
1725 * Returns: The matched object or NULL on path lookup failures.
1727 static Object *object_resolve_link(Object *obj, const char *name,
1728 const char *path, Error **errp)
1730 const char *type;
1731 char *target_type;
1732 bool ambiguous = false;
1733 Object *target;
1735 /* Go from link<FOO> to FOO. */
1736 type = object_property_get_type(obj, name, NULL);
1737 target_type = g_strndup(&type[5], strlen(type) - 6);
1738 target = object_resolve_path_type(path, target_type, &ambiguous);
1740 if (ambiguous) {
1741 error_setg(errp, "Path '%s' does not uniquely identify an object",
1742 path);
1743 } else if (!target) {
1744 target = object_resolve_path(path, &ambiguous);
1745 if (target || ambiguous) {
1746 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1747 } else {
1748 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1749 "Device '%s' not found", path);
1751 target = NULL;
1753 g_free(target_type);
1755 return target;
1758 static void object_set_link_property(Object *obj, Visitor *v,
1759 const char *name, void *opaque,
1760 Error **errp)
1762 Error *local_err = NULL;
1763 LinkProperty *prop = opaque;
1764 Object **targetp = object_link_get_targetp(obj, prop);
1765 Object *old_target = *targetp;
1766 Object *new_target = NULL;
1767 char *path = NULL;
1769 visit_type_str(v, name, &path, &local_err);
1771 if (!local_err && strcmp(path, "") != 0) {
1772 new_target = object_resolve_link(obj, name, path, &local_err);
1775 g_free(path);
1776 if (local_err) {
1777 error_propagate(errp, local_err);
1778 return;
1781 prop->check(obj, name, new_target, &local_err);
1782 if (local_err) {
1783 error_propagate(errp, local_err);
1784 return;
1787 *targetp = new_target;
1788 if (prop->flags & OBJ_PROP_LINK_STRONG) {
1789 object_ref(new_target);
1790 object_unref(old_target);
1794 static Object *object_resolve_link_property(Object *parent, void *opaque,
1795 const char *part)
1797 LinkProperty *lprop = opaque;
1799 return *object_link_get_targetp(parent, lprop);
1802 static void object_release_link_property(Object *obj, const char *name,
1803 void *opaque)
1805 LinkProperty *prop = opaque;
1806 Object **targetp = object_link_get_targetp(obj, prop);
1808 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) {
1809 object_unref(*targetp);
1811 if (!(prop->flags & OBJ_PROP_LINK_CLASS)) {
1812 g_free(prop);
1816 static ObjectProperty *
1817 object_add_link_prop(Object *obj, const char *name,
1818 const char *type, void *ptr,
1819 void (*check)(const Object *, const char *,
1820 Object *, Error **),
1821 ObjectPropertyLinkFlags flags)
1823 LinkProperty *prop = g_malloc(sizeof(*prop));
1824 g_autofree char *full_type = NULL;
1825 ObjectProperty *op;
1827 if (flags & OBJ_PROP_LINK_DIRECT) {
1828 prop->target = ptr;
1829 } else {
1830 prop->targetp = ptr;
1832 prop->check = check;
1833 prop->flags = flags;
1835 full_type = g_strdup_printf("link<%s>", type);
1837 op = object_property_add(obj, name, full_type,
1838 object_get_link_property,
1839 check ? object_set_link_property : NULL,
1840 object_release_link_property,
1841 prop);
1842 op->resolve = object_resolve_link_property;
1843 return op;
1846 ObjectProperty *
1847 object_property_add_link(Object *obj, const char *name,
1848 const char *type, Object **targetp,
1849 void (*check)(const Object *, const char *,
1850 Object *, Error **),
1851 ObjectPropertyLinkFlags flags)
1853 return object_add_link_prop(obj, name, type, targetp, check, flags);
1856 ObjectProperty *
1857 object_class_property_add_link(ObjectClass *oc,
1858 const char *name,
1859 const char *type, ptrdiff_t offset,
1860 void (*check)(const Object *obj, const char *name,
1861 Object *val, Error **errp),
1862 ObjectPropertyLinkFlags flags)
1864 LinkProperty *prop = g_new0(LinkProperty, 1);
1865 char *full_type;
1866 ObjectProperty *op;
1868 prop->offset = offset;
1869 prop->check = check;
1870 prop->flags = flags | OBJ_PROP_LINK_CLASS;
1872 full_type = g_strdup_printf("link<%s>", type);
1874 op = object_class_property_add(oc, name, full_type,
1875 object_get_link_property,
1876 check ? object_set_link_property : NULL,
1877 object_release_link_property,
1878 prop);
1880 op->resolve = object_resolve_link_property;
1882 g_free(full_type);
1883 return op;
1886 ObjectProperty *
1887 object_property_add_const_link(Object *obj, const char *name,
1888 Object *target)
1890 return object_add_link_prop(obj, name,
1891 object_get_typename(target), target,
1892 NULL, OBJ_PROP_LINK_DIRECT);
1895 char *object_get_canonical_path_component(const Object *obj)
1897 ObjectProperty *prop = NULL;
1898 GHashTableIter iter;
1900 if (obj->parent == NULL) {
1901 return NULL;
1904 g_hash_table_iter_init(&iter, obj->parent->properties);
1905 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1906 if (!object_property_is_child(prop)) {
1907 continue;
1910 if (prop->opaque == obj) {
1911 return g_strdup(prop->name);
1915 /* obj had a parent but was not a child, should never happen */
1916 g_assert_not_reached();
1917 return NULL;
1920 char *object_get_canonical_path(const Object *obj)
1922 Object *root = object_get_root();
1923 char *newpath, *path = NULL;
1925 if (obj == root) {
1926 return g_strdup("/");
1929 do {
1930 char *component = object_get_canonical_path_component(obj);
1932 if (!component) {
1933 /* A canonical path must be complete, so discard what was
1934 * collected so far.
1936 g_free(path);
1937 return NULL;
1940 newpath = g_strdup_printf("/%s%s", component, path ? path : "");
1941 g_free(path);
1942 g_free(component);
1943 path = newpath;
1944 obj = obj->parent;
1945 } while (obj != root);
1947 return path;
1950 Object *object_resolve_path_component(Object *parent, const char *part)
1952 ObjectProperty *prop = object_property_find(parent, part, NULL);
1953 if (prop == NULL) {
1954 return NULL;
1957 if (prop->resolve) {
1958 return prop->resolve(parent, prop->opaque, part);
1959 } else {
1960 return NULL;
1964 static Object *object_resolve_abs_path(Object *parent,
1965 char **parts,
1966 const char *typename)
1968 Object *child;
1970 if (*parts == NULL) {
1971 return object_dynamic_cast(parent, typename);
1974 if (strcmp(*parts, "") == 0) {
1975 return object_resolve_abs_path(parent, parts + 1, typename);
1978 child = object_resolve_path_component(parent, *parts);
1979 if (!child) {
1980 return NULL;
1983 return object_resolve_abs_path(child, parts + 1, typename);
1986 static Object *object_resolve_partial_path(Object *parent,
1987 char **parts,
1988 const char *typename,
1989 bool *ambiguous)
1991 Object *obj;
1992 GHashTableIter iter;
1993 ObjectProperty *prop;
1995 obj = object_resolve_abs_path(parent, parts, typename);
1997 g_hash_table_iter_init(&iter, parent->properties);
1998 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1999 Object *found;
2001 if (!object_property_is_child(prop)) {
2002 continue;
2005 found = object_resolve_partial_path(prop->opaque, parts,
2006 typename, ambiguous);
2007 if (found) {
2008 if (obj) {
2009 *ambiguous = true;
2010 return NULL;
2012 obj = found;
2015 if (*ambiguous) {
2016 return NULL;
2020 return obj;
2023 Object *object_resolve_path_type(const char *path, const char *typename,
2024 bool *ambiguousp)
2026 Object *obj;
2027 char **parts;
2029 parts = g_strsplit(path, "/", 0);
2030 assert(parts);
2032 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
2033 bool ambiguous = false;
2034 obj = object_resolve_partial_path(object_get_root(), parts,
2035 typename, &ambiguous);
2036 if (ambiguousp) {
2037 *ambiguousp = ambiguous;
2039 } else {
2040 obj = object_resolve_abs_path(object_get_root(), parts + 1, typename);
2043 g_strfreev(parts);
2045 return obj;
2048 Object *object_resolve_path(const char *path, bool *ambiguous)
2050 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
2053 typedef struct StringProperty
2055 char *(*get)(Object *, Error **);
2056 void (*set)(Object *, const char *, Error **);
2057 } StringProperty;
2059 static void property_get_str(Object *obj, Visitor *v, const char *name,
2060 void *opaque, Error **errp)
2062 StringProperty *prop = opaque;
2063 char *value;
2064 Error *err = NULL;
2066 value = prop->get(obj, &err);
2067 if (err) {
2068 error_propagate(errp, err);
2069 return;
2072 visit_type_str(v, name, &value, errp);
2073 g_free(value);
2076 static void property_set_str(Object *obj, Visitor *v, const char *name,
2077 void *opaque, Error **errp)
2079 StringProperty *prop = opaque;
2080 char *value;
2081 Error *local_err = NULL;
2083 visit_type_str(v, name, &value, &local_err);
2084 if (local_err) {
2085 error_propagate(errp, local_err);
2086 return;
2089 prop->set(obj, value, errp);
2090 g_free(value);
2093 static void property_release_str(Object *obj, const char *name,
2094 void *opaque)
2096 StringProperty *prop = opaque;
2097 g_free(prop);
2100 ObjectProperty *
2101 object_property_add_str(Object *obj, const char *name,
2102 char *(*get)(Object *, Error **),
2103 void (*set)(Object *, const char *, Error **))
2105 StringProperty *prop = g_malloc0(sizeof(*prop));
2107 prop->get = get;
2108 prop->set = set;
2110 return object_property_add(obj, name, "string",
2111 get ? property_get_str : NULL,
2112 set ? property_set_str : NULL,
2113 property_release_str,
2114 prop);
2117 ObjectProperty *
2118 object_class_property_add_str(ObjectClass *klass, const char *name,
2119 char *(*get)(Object *, Error **),
2120 void (*set)(Object *, const char *,
2121 Error **))
2123 StringProperty *prop = g_malloc0(sizeof(*prop));
2125 prop->get = get;
2126 prop->set = set;
2128 return object_class_property_add(klass, name, "string",
2129 get ? property_get_str : NULL,
2130 set ? property_set_str : NULL,
2131 NULL,
2132 prop);
2135 typedef struct BoolProperty
2137 bool (*get)(Object *, Error **);
2138 void (*set)(Object *, bool, Error **);
2139 } BoolProperty;
2141 static void property_get_bool(Object *obj, Visitor *v, const char *name,
2142 void *opaque, Error **errp)
2144 BoolProperty *prop = opaque;
2145 bool value;
2146 Error *err = NULL;
2148 value = prop->get(obj, &err);
2149 if (err) {
2150 error_propagate(errp, err);
2151 return;
2154 visit_type_bool(v, name, &value, errp);
2157 static void property_set_bool(Object *obj, Visitor *v, const char *name,
2158 void *opaque, Error **errp)
2160 BoolProperty *prop = opaque;
2161 bool value;
2162 Error *local_err = NULL;
2164 visit_type_bool(v, name, &value, &local_err);
2165 if (local_err) {
2166 error_propagate(errp, local_err);
2167 return;
2170 prop->set(obj, value, errp);
2173 static void property_release_bool(Object *obj, const char *name,
2174 void *opaque)
2176 BoolProperty *prop = opaque;
2177 g_free(prop);
2180 ObjectProperty *
2181 object_property_add_bool(Object *obj, const char *name,
2182 bool (*get)(Object *, Error **),
2183 void (*set)(Object *, bool, Error **))
2185 BoolProperty *prop = g_malloc0(sizeof(*prop));
2187 prop->get = get;
2188 prop->set = set;
2190 return object_property_add(obj, name, "bool",
2191 get ? property_get_bool : NULL,
2192 set ? property_set_bool : NULL,
2193 property_release_bool,
2194 prop);
2197 ObjectProperty *
2198 object_class_property_add_bool(ObjectClass *klass, const char *name,
2199 bool (*get)(Object *, Error **),
2200 void (*set)(Object *, bool, Error **))
2202 BoolProperty *prop = g_malloc0(sizeof(*prop));
2204 prop->get = get;
2205 prop->set = set;
2207 return object_class_property_add(klass, name, "bool",
2208 get ? property_get_bool : NULL,
2209 set ? property_set_bool : NULL,
2210 NULL,
2211 prop);
2214 static void property_get_enum(Object *obj, Visitor *v, const char *name,
2215 void *opaque, Error **errp)
2217 EnumProperty *prop = opaque;
2218 int value;
2219 Error *err = NULL;
2221 value = prop->get(obj, &err);
2222 if (err) {
2223 error_propagate(errp, err);
2224 return;
2227 visit_type_enum(v, name, &value, prop->lookup, errp);
2230 static void property_set_enum(Object *obj, Visitor *v, const char *name,
2231 void *opaque, Error **errp)
2233 EnumProperty *prop = opaque;
2234 int value;
2235 Error *err = NULL;
2237 visit_type_enum(v, name, &value, prop->lookup, &err);
2238 if (err) {
2239 error_propagate(errp, err);
2240 return;
2242 prop->set(obj, value, errp);
2245 static void property_release_enum(Object *obj, const char *name,
2246 void *opaque)
2248 EnumProperty *prop = opaque;
2249 g_free(prop);
2252 ObjectProperty *
2253 object_property_add_enum(Object *obj, const char *name,
2254 const char *typename,
2255 const QEnumLookup *lookup,
2256 int (*get)(Object *, Error **),
2257 void (*set)(Object *, int, Error **))
2259 EnumProperty *prop = g_malloc(sizeof(*prop));
2261 prop->lookup = lookup;
2262 prop->get = get;
2263 prop->set = set;
2265 return object_property_add(obj, name, typename,
2266 get ? property_get_enum : NULL,
2267 set ? property_set_enum : NULL,
2268 property_release_enum,
2269 prop);
2272 ObjectProperty *
2273 object_class_property_add_enum(ObjectClass *klass, const char *name,
2274 const char *typename,
2275 const QEnumLookup *lookup,
2276 int (*get)(Object *, Error **),
2277 void (*set)(Object *, int, Error **))
2279 EnumProperty *prop = g_malloc(sizeof(*prop));
2281 prop->lookup = lookup;
2282 prop->get = get;
2283 prop->set = set;
2285 return object_class_property_add(klass, name, typename,
2286 get ? property_get_enum : NULL,
2287 set ? property_set_enum : NULL,
2288 NULL,
2289 prop);
2292 typedef struct TMProperty {
2293 void (*get)(Object *, struct tm *, Error **);
2294 } TMProperty;
2296 static void property_get_tm(Object *obj, Visitor *v, const char *name,
2297 void *opaque, Error **errp)
2299 TMProperty *prop = opaque;
2300 Error *err = NULL;
2301 struct tm value;
2303 prop->get(obj, &value, &err);
2304 if (err) {
2305 goto out;
2308 visit_start_struct(v, name, NULL, 0, &err);
2309 if (err) {
2310 goto out;
2312 visit_type_int32(v, "tm_year", &value.tm_year, &err);
2313 if (err) {
2314 goto out_end;
2316 visit_type_int32(v, "tm_mon", &value.tm_mon, &err);
2317 if (err) {
2318 goto out_end;
2320 visit_type_int32(v, "tm_mday", &value.tm_mday, &err);
2321 if (err) {
2322 goto out_end;
2324 visit_type_int32(v, "tm_hour", &value.tm_hour, &err);
2325 if (err) {
2326 goto out_end;
2328 visit_type_int32(v, "tm_min", &value.tm_min, &err);
2329 if (err) {
2330 goto out_end;
2332 visit_type_int32(v, "tm_sec", &value.tm_sec, &err);
2333 if (err) {
2334 goto out_end;
2336 visit_check_struct(v, &err);
2337 out_end:
2338 visit_end_struct(v, NULL);
2339 out:
2340 error_propagate(errp, err);
2344 static void property_release_tm(Object *obj, const char *name,
2345 void *opaque)
2347 TMProperty *prop = opaque;
2348 g_free(prop);
2351 ObjectProperty *
2352 object_property_add_tm(Object *obj, const char *name,
2353 void (*get)(Object *, struct tm *, Error **))
2355 TMProperty *prop = g_malloc0(sizeof(*prop));
2357 prop->get = get;
2359 return object_property_add(obj, name, "struct tm",
2360 get ? property_get_tm : NULL, NULL,
2361 property_release_tm,
2362 prop);
2365 ObjectProperty *
2366 object_class_property_add_tm(ObjectClass *klass, const char *name,
2367 void (*get)(Object *, struct tm *, Error **))
2369 TMProperty *prop = g_malloc0(sizeof(*prop));
2371 prop->get = get;
2373 return object_class_property_add(klass, name, "struct tm",
2374 get ? property_get_tm : NULL,
2375 NULL, NULL, prop);
2378 static char *qdev_get_type(Object *obj, Error **errp)
2380 return g_strdup(object_get_typename(obj));
2383 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2384 void *opaque, Error **errp)
2386 uint8_t value = *(uint8_t *)opaque;
2387 visit_type_uint8(v, name, &value, errp);
2390 static void property_set_uint8_ptr(Object *obj, Visitor *v, const char *name,
2391 void *opaque, Error **errp)
2393 uint8_t *field = opaque;
2394 uint8_t value;
2395 Error *local_err = NULL;
2397 visit_type_uint8(v, name, &value, &local_err);
2398 if (local_err) {
2399 error_propagate(errp, local_err);
2400 return;
2403 *field = value;
2406 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2407 void *opaque, Error **errp)
2409 uint16_t value = *(uint16_t *)opaque;
2410 visit_type_uint16(v, name, &value, errp);
2413 static void property_set_uint16_ptr(Object *obj, Visitor *v, const char *name,
2414 void *opaque, Error **errp)
2416 uint16_t *field = opaque;
2417 uint16_t value;
2418 Error *local_err = NULL;
2420 visit_type_uint16(v, name, &value, &local_err);
2421 if (local_err) {
2422 error_propagate(errp, local_err);
2423 return;
2426 *field = value;
2429 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2430 void *opaque, Error **errp)
2432 uint32_t value = *(uint32_t *)opaque;
2433 visit_type_uint32(v, name, &value, errp);
2436 static void property_set_uint32_ptr(Object *obj, Visitor *v, const char *name,
2437 void *opaque, Error **errp)
2439 uint32_t *field = opaque;
2440 uint32_t value;
2441 Error *local_err = NULL;
2443 visit_type_uint32(v, name, &value, &local_err);
2444 if (local_err) {
2445 error_propagate(errp, local_err);
2446 return;
2449 *field = value;
2452 static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2453 void *opaque, Error **errp)
2455 uint64_t value = *(uint64_t *)opaque;
2456 visit_type_uint64(v, name, &value, errp);
2459 static void property_set_uint64_ptr(Object *obj, Visitor *v, const char *name,
2460 void *opaque, Error **errp)
2462 uint64_t *field = opaque;
2463 uint64_t value;
2464 Error *local_err = NULL;
2466 visit_type_uint64(v, name, &value, &local_err);
2467 if (local_err) {
2468 error_propagate(errp, local_err);
2469 return;
2472 *field = value;
2475 ObjectProperty *
2476 object_property_add_uint8_ptr(Object *obj, const char *name,
2477 const uint8_t *v,
2478 ObjectPropertyFlags flags)
2480 ObjectPropertyAccessor *getter = NULL;
2481 ObjectPropertyAccessor *setter = NULL;
2483 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2484 getter = property_get_uint8_ptr;
2487 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2488 setter = property_set_uint8_ptr;
2491 return object_property_add(obj, name, "uint8",
2492 getter, setter, NULL, (void *)v);
2495 ObjectProperty *
2496 object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2497 const uint8_t *v,
2498 ObjectPropertyFlags flags)
2500 ObjectPropertyAccessor *getter = NULL;
2501 ObjectPropertyAccessor *setter = NULL;
2503 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2504 getter = property_get_uint8_ptr;
2507 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2508 setter = property_set_uint8_ptr;
2511 return object_class_property_add(klass, name, "uint8",
2512 getter, setter, NULL, (void *)v);
2515 ObjectProperty *
2516 object_property_add_uint16_ptr(Object *obj, const char *name,
2517 const uint16_t *v,
2518 ObjectPropertyFlags flags)
2520 ObjectPropertyAccessor *getter = NULL;
2521 ObjectPropertyAccessor *setter = NULL;
2523 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2524 getter = property_get_uint16_ptr;
2527 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2528 setter = property_set_uint16_ptr;
2531 return object_property_add(obj, name, "uint16",
2532 getter, setter, NULL, (void *)v);
2535 ObjectProperty *
2536 object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2537 const uint16_t *v,
2538 ObjectPropertyFlags flags)
2540 ObjectPropertyAccessor *getter = NULL;
2541 ObjectPropertyAccessor *setter = NULL;
2543 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2544 getter = property_get_uint16_ptr;
2547 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2548 setter = property_set_uint16_ptr;
2551 return object_class_property_add(klass, name, "uint16",
2552 getter, setter, NULL, (void *)v);
2555 ObjectProperty *
2556 object_property_add_uint32_ptr(Object *obj, const char *name,
2557 const uint32_t *v,
2558 ObjectPropertyFlags flags)
2560 ObjectPropertyAccessor *getter = NULL;
2561 ObjectPropertyAccessor *setter = NULL;
2563 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2564 getter = property_get_uint32_ptr;
2567 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2568 setter = property_set_uint32_ptr;
2571 return object_property_add(obj, name, "uint32",
2572 getter, setter, NULL, (void *)v);
2575 ObjectProperty *
2576 object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2577 const uint32_t *v,
2578 ObjectPropertyFlags flags)
2580 ObjectPropertyAccessor *getter = NULL;
2581 ObjectPropertyAccessor *setter = NULL;
2583 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2584 getter = property_get_uint32_ptr;
2587 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2588 setter = property_set_uint32_ptr;
2591 return object_class_property_add(klass, name, "uint32",
2592 getter, setter, NULL, (void *)v);
2595 ObjectProperty *
2596 object_property_add_uint64_ptr(Object *obj, const char *name,
2597 const uint64_t *v,
2598 ObjectPropertyFlags flags)
2600 ObjectPropertyAccessor *getter = NULL;
2601 ObjectPropertyAccessor *setter = NULL;
2603 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2604 getter = property_get_uint64_ptr;
2607 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2608 setter = property_set_uint64_ptr;
2611 return object_property_add(obj, name, "uint64",
2612 getter, setter, NULL, (void *)v);
2615 ObjectProperty *
2616 object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2617 const uint64_t *v,
2618 ObjectPropertyFlags flags)
2620 ObjectPropertyAccessor *getter = NULL;
2621 ObjectPropertyAccessor *setter = NULL;
2623 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2624 getter = property_get_uint64_ptr;
2627 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2628 setter = property_set_uint64_ptr;
2631 return object_class_property_add(klass, name, "uint64",
2632 getter, setter, NULL, (void *)v);
2635 typedef struct {
2636 Object *target_obj;
2637 char *target_name;
2638 } AliasProperty;
2640 static void property_get_alias(Object *obj, Visitor *v, const char *name,
2641 void *opaque, Error **errp)
2643 AliasProperty *prop = opaque;
2645 object_property_get(prop->target_obj, v, prop->target_name, errp);
2648 static void property_set_alias(Object *obj, Visitor *v, const char *name,
2649 void *opaque, Error **errp)
2651 AliasProperty *prop = opaque;
2653 object_property_set(prop->target_obj, v, prop->target_name, errp);
2656 static Object *property_resolve_alias(Object *obj, void *opaque,
2657 const char *part)
2659 AliasProperty *prop = opaque;
2661 return object_resolve_path_component(prop->target_obj, prop->target_name);
2664 static void property_release_alias(Object *obj, const char *name, void *opaque)
2666 AliasProperty *prop = opaque;
2668 g_free(prop->target_name);
2669 g_free(prop);
2672 ObjectProperty *
2673 object_property_add_alias(Object *obj, const char *name,
2674 Object *target_obj, const char *target_name)
2676 AliasProperty *prop;
2677 ObjectProperty *op;
2678 ObjectProperty *target_prop;
2679 g_autofree char *prop_type = NULL;
2681 target_prop = object_property_find(target_obj, target_name,
2682 &error_abort);
2684 if (object_property_is_child(target_prop)) {
2685 prop_type = g_strdup_printf("link%s",
2686 target_prop->type + strlen("child"));
2687 } else {
2688 prop_type = g_strdup(target_prop->type);
2691 prop = g_malloc(sizeof(*prop));
2692 prop->target_obj = target_obj;
2693 prop->target_name = g_strdup(target_name);
2695 op = object_property_add(obj, name, prop_type,
2696 property_get_alias,
2697 property_set_alias,
2698 property_release_alias,
2699 prop);
2700 op->resolve = property_resolve_alias;
2701 if (target_prop->defval) {
2702 op->defval = qobject_ref(target_prop->defval);
2705 object_property_set_description(obj, op->name,
2706 target_prop->description);
2707 return op;
2710 void object_property_set_description(Object *obj, const char *name,
2711 const char *description)
2713 ObjectProperty *op;
2715 op = object_property_find(obj, name, &error_abort);
2716 g_free(op->description);
2717 op->description = g_strdup(description);
2720 void object_class_property_set_description(ObjectClass *klass,
2721 const char *name,
2722 const char *description)
2724 ObjectProperty *op;
2726 op = g_hash_table_lookup(klass->properties, name);
2727 g_free(op->description);
2728 op->description = g_strdup(description);
2731 static void object_class_init(ObjectClass *klass, void *data)
2733 object_class_property_add_str(klass, "type", qdev_get_type,
2734 NULL);
2737 static void register_types(void)
2739 static TypeInfo interface_info = {
2740 .name = TYPE_INTERFACE,
2741 .class_size = sizeof(InterfaceClass),
2742 .abstract = true,
2745 static TypeInfo object_info = {
2746 .name = TYPE_OBJECT,
2747 .instance_size = sizeof(Object),
2748 .class_init = object_class_init,
2749 .abstract = true,
2752 type_interface = type_register_internal(&interface_info);
2753 type_register_internal(&object_info);
2756 type_init(register_types)