xref: /openbmc/qemu/qom/object.c (revision 8fa3b702)
1 /*
2  * QEMU Object Model
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
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.
11  */
12 
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"
27 
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"
35 
36 #define MAX_INTERFACES 32
37 
38 typedef struct InterfaceImpl InterfaceImpl;
39 typedef struct TypeImpl TypeImpl;
40 
41 struct InterfaceImpl
42 {
43     const char *typename;
44 };
45 
46 struct TypeImpl
47 {
48     const char *name;
49 
50     size_t class_size;
51 
52     size_t instance_size;
53 
54     void (*class_init)(ObjectClass *klass, void *data);
55     void (*class_base_init)(ObjectClass *klass, void *data);
56 
57     void *class_data;
58 
59     void (*instance_init)(Object *obj);
60     void (*instance_post_init)(Object *obj);
61     void (*instance_finalize)(Object *obj);
62 
63     bool abstract;
64 
65     const char *parent;
66     TypeImpl *parent_type;
67 
68     ObjectClass *class;
69 
70     int num_interfaces;
71     InterfaceImpl interfaces[MAX_INTERFACES];
72 };
73 
74 static Type type_interface;
75 
76 static GHashTable *type_table_get(void)
77 {
78     static GHashTable *type_table;
79 
80     if (type_table == NULL) {
81         type_table = g_hash_table_new(g_str_hash, g_str_equal);
82     }
83 
84     return type_table;
85 }
86 
87 static bool enumerating_types;
88 
89 static void type_table_add(TypeImpl *ti)
90 {
91     assert(!enumerating_types);
92     g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
93 }
94 
95 static TypeImpl *type_table_lookup(const char *name)
96 {
97     return g_hash_table_lookup(type_table_get(), name);
98 }
99 
100 static TypeImpl *type_new(const TypeInfo *info)
101 {
102     TypeImpl *ti = g_malloc0(sizeof(*ti));
103     int i;
104 
105     g_assert(info->name != NULL);
106 
107     if (type_table_lookup(info->name) != NULL) {
108         fprintf(stderr, "Registering `%s' which already exists\n", info->name);
109         abort();
110     }
111 
112     ti->name = g_strdup(info->name);
113     ti->parent = g_strdup(info->parent);
114 
115     ti->class_size = info->class_size;
116     ti->instance_size = info->instance_size;
117 
118     ti->class_init = info->class_init;
119     ti->class_base_init = info->class_base_init;
120     ti->class_data = info->class_data;
121 
122     ti->instance_init = info->instance_init;
123     ti->instance_post_init = info->instance_post_init;
124     ti->instance_finalize = info->instance_finalize;
125 
126     ti->abstract = info->abstract;
127 
128     for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
129         ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
130     }
131     ti->num_interfaces = i;
132 
133     return ti;
134 }
135 
136 static TypeImpl *type_register_internal(const TypeInfo *info)
137 {
138     TypeImpl *ti;
139     ti = type_new(info);
140 
141     type_table_add(ti);
142     return ti;
143 }
144 
145 TypeImpl *type_register(const TypeInfo *info)
146 {
147     assert(info->parent);
148     return type_register_internal(info);
149 }
150 
151 TypeImpl *type_register_static(const TypeInfo *info)
152 {
153     return type_register(info);
154 }
155 
156 void type_register_static_array(const TypeInfo *infos, int nr_infos)
157 {
158     int i;
159 
160     for (i = 0; i < nr_infos; i++) {
161         type_register_static(&infos[i]);
162     }
163 }
164 
165 static TypeImpl *type_get_by_name(const char *name)
166 {
167     if (name == NULL) {
168         return NULL;
169     }
170 
171     return type_table_lookup(name);
172 }
173 
174 static TypeImpl *type_get_parent(TypeImpl *type)
175 {
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();
182         }
183     }
184 
185     return type->parent_type;
186 }
187 
188 static bool type_has_parent(TypeImpl *type)
189 {
190     return (type->parent != NULL);
191 }
192 
193 static size_t type_class_get_size(TypeImpl *ti)
194 {
195     if (ti->class_size) {
196         return ti->class_size;
197     }
198 
199     if (type_has_parent(ti)) {
200         return type_class_get_size(type_get_parent(ti));
201     }
202 
203     return sizeof(ObjectClass);
204 }
205 
206 static size_t type_object_get_size(TypeImpl *ti)
207 {
208     if (ti->instance_size) {
209         return ti->instance_size;
210     }
211 
212     if (type_has_parent(ti)) {
213         return type_object_get_size(type_get_parent(ti));
214     }
215 
216     return 0;
217 }
218 
219 size_t object_type_get_instance_size(const char *typename)
220 {
221     TypeImpl *type = type_get_by_name(typename);
222 
223     g_assert(type != NULL);
224     return type_object_get_size(type);
225 }
226 
227 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
228 {
229     assert(target_type);
230 
231     /* Check if target_type is a direct ancestor of type */
232     while (type) {
233         if (type == target_type) {
234             return true;
235         }
236 
237         type = type_get_parent(type);
238     }
239 
240     return false;
241 }
242 
243 static void type_initialize(TypeImpl *ti);
244 
245 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
246                                       TypeImpl *parent_type)
247 {
248     InterfaceClass *new_iface;
249     TypeInfo info = { };
250     TypeImpl *iface_impl;
251 
252     info.parent = parent_type->name;
253     info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
254     info.abstract = true;
255 
256     iface_impl = type_new(&info);
257     iface_impl->parent_type = parent_type;
258     type_initialize(iface_impl);
259     g_free((char *)info.name);
260 
261     new_iface = (InterfaceClass *)iface_impl->class;
262     new_iface->concrete_class = ti->class;
263     new_iface->interface_type = interface_type;
264 
265     ti->class->interfaces = g_slist_append(ti->class->interfaces, new_iface);
266 }
267 
268 static void object_property_free(gpointer data)
269 {
270     ObjectProperty *prop = data;
271 
272     if (prop->defval) {
273         qobject_unref(prop->defval);
274         prop->defval = NULL;
275     }
276     g_free(prop->name);
277     g_free(prop->type);
278     g_free(prop->description);
279     g_free(prop);
280 }
281 
282 static void type_initialize(TypeImpl *ti)
283 {
284     TypeImpl *parent;
285 
286     if (ti->class) {
287         return;
288     }
289 
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.
294      */
295     if (ti->instance_size == 0) {
296         ti->abstract = true;
297     }
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);
305     }
306     ti->class = g_malloc0(ti->class_size);
307 
308     parent = type_get_parent(ti);
309     if (parent) {
310         type_initialize(parent);
311         GSList *e;
312         int i;
313 
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;
318 
319         for (e = parent->class->interfaces; e; e = e->next) {
320             InterfaceClass *iface = e->data;
321             ObjectClass *klass = OBJECT_CLASS(iface);
322 
323             type_initialize_interface(ti, iface->interface_type, klass->type);
324         }
325 
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();
332             }
333             for (e = ti->class->interfaces; e; e = e->next) {
334                 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
335 
336                 if (type_is_ancestor(target_type, t)) {
337                     break;
338                 }
339             }
340 
341             if (e) {
342                 continue;
343             }
344 
345             type_initialize_interface(ti, t, t);
346         }
347     }
348 
349     ti->class->properties = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
350                                                   object_property_free);
351 
352     ti->class->type = ti;
353 
354     while (parent) {
355         if (parent->class_base_init) {
356             parent->class_base_init(ti->class, ti->class_data);
357         }
358         parent = type_get_parent(parent);
359     }
360 
361     if (ti->class_init) {
362         ti->class_init(ti->class, ti->class_data);
363     }
364 }
365 
366 static void object_init_with_type(Object *obj, TypeImpl *ti)
367 {
368     if (type_has_parent(ti)) {
369         object_init_with_type(obj, type_get_parent(ti));
370     }
371 
372     if (ti->instance_init) {
373         ti->instance_init(obj);
374     }
375 }
376 
377 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
378 {
379     if (ti->instance_post_init) {
380         ti->instance_post_init(obj);
381     }
382 
383     if (type_has_parent(ti)) {
384         object_post_init_with_type(obj, type_get_parent(ti));
385     }
386 }
387 
388 bool object_apply_global_props(Object *obj, const GPtrArray *props,
389                                Error **errp)
390 {
391     int i;
392 
393     if (!props) {
394         return true;
395     }
396 
397     for (i = 0; i < props->len; i++) {
398         GlobalProperty *p = g_ptr_array_index(props, i);
399         Error *err = NULL;
400 
401         if (object_dynamic_cast(obj, p->driver) == NULL) {
402             continue;
403         }
404         if (p->optional && !object_property_find(obj, p->property, NULL)) {
405             continue;
406         }
407         p->used = true;
408         if (!object_property_parse(obj, p->property, p->value, &err)) {
409             error_prepend(&err, "can't apply global %s.%s=%s: ",
410                           p->driver, p->property, p->value);
411             /*
412              * If errp != NULL, propagate error and return.
413              * If errp == NULL, report a warning, but keep going
414              * with the remaining globals.
415              */
416             if (errp) {
417                 error_propagate(errp, err);
418                 return false;
419             } else {
420                 warn_report_err(err);
421             }
422         }
423     }
424 
425     return true;
426 }
427 
428 /*
429  * Global property defaults
430  * Slot 0: accelerator's global property defaults
431  * Slot 1: machine's global property defaults
432  * Slot 2: global properties from legacy command line option
433  * Each is a GPtrArray of of GlobalProperty.
434  * Applied in order, later entries override earlier ones.
435  */
436 static GPtrArray *object_compat_props[3];
437 
438 /*
439  * Retrieve @GPtrArray for global property defined with options
440  * other than "-global".  These are generally used for syntactic
441  * sugar and legacy command line options.
442  */
443 void object_register_sugar_prop(const char *driver, const char *prop, const char *value)
444 {
445     GlobalProperty *g;
446     if (!object_compat_props[2]) {
447         object_compat_props[2] = g_ptr_array_new();
448     }
449     g = g_new0(GlobalProperty, 1);
450     g->driver = g_strdup(driver);
451     g->property = g_strdup(prop);
452     g->value = g_strdup(value);
453     g_ptr_array_add(object_compat_props[2], g);
454 }
455 
456 /*
457  * Set machine's global property defaults to @compat_props.
458  * May be called at most once.
459  */
460 void object_set_machine_compat_props(GPtrArray *compat_props)
461 {
462     assert(!object_compat_props[1]);
463     object_compat_props[1] = compat_props;
464 }
465 
466 /*
467  * Set accelerator's global property defaults to @compat_props.
468  * May be called at most once.
469  */
470 void object_set_accelerator_compat_props(GPtrArray *compat_props)
471 {
472     assert(!object_compat_props[0]);
473     object_compat_props[0] = compat_props;
474 }
475 
476 void object_apply_compat_props(Object *obj)
477 {
478     int i;
479 
480     for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
481         object_apply_global_props(obj, object_compat_props[i],
482                                   i == 2 ? &error_fatal : &error_abort);
483     }
484 }
485 
486 static void object_class_property_init_all(Object *obj)
487 {
488     ObjectPropertyIterator iter;
489     ObjectProperty *prop;
490 
491     object_class_property_iter_init(&iter, object_get_class(obj));
492     while ((prop = object_property_iter_next(&iter))) {
493         if (prop->init) {
494             prop->init(obj, prop);
495         }
496     }
497 }
498 
499 static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type)
500 {
501     type_initialize(type);
502 
503     g_assert(type->instance_size >= sizeof(Object));
504     g_assert(type->abstract == false);
505     g_assert(size >= type->instance_size);
506 
507     memset(obj, 0, type->instance_size);
508     obj->class = type->class;
509     object_ref(obj);
510     object_class_property_init_all(obj);
511     obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
512                                             NULL, object_property_free);
513     object_init_with_type(obj, type);
514     object_post_init_with_type(obj, type);
515 }
516 
517 void object_initialize(void *data, size_t size, const char *typename)
518 {
519     TypeImpl *type = type_get_by_name(typename);
520 
521     if (!type) {
522         error_report("missing object type '%s'", typename);
523         abort();
524     }
525 
526     object_initialize_with_type(data, size, type);
527 }
528 
529 bool object_initialize_child_with_props(Object *parentobj,
530                                         const char *propname,
531                                         void *childobj, size_t size,
532                                         const char *type,
533                                         Error **errp, ...)
534 {
535     va_list vargs;
536     bool ok;
537 
538     va_start(vargs, errp);
539     ok = object_initialize_child_with_propsv(parentobj, propname,
540                                              childobj, size, type, errp,
541                                              vargs);
542     va_end(vargs);
543     return ok;
544 }
545 
546 bool object_initialize_child_with_propsv(Object *parentobj,
547                                          const char *propname,
548                                          void *childobj, size_t size,
549                                          const char *type,
550                                          Error **errp, va_list vargs)
551 {
552     bool ok = false;
553     Object *obj;
554     UserCreatable *uc;
555 
556     object_initialize(childobj, size, type);
557     obj = OBJECT(childobj);
558 
559     if (!object_set_propv(obj, errp, vargs)) {
560         goto out;
561     }
562 
563     object_property_add_child(parentobj, propname, obj);
564 
565     uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
566     if (uc) {
567         if (!user_creatable_complete(uc, errp)) {
568             object_unparent(obj);
569             goto out;
570         }
571     }
572 
573     ok = true;
574 
575 out:
576     /*
577      * We want @obj's reference to be 1 on success, 0 on failure.
578      * On success, it's 2: one taken by object_initialize(), and one
579      * by object_property_add_child().
580      * On failure in object_initialize() or earlier, it's 1.
581      * On failure afterwards, it's also 1: object_unparent() releases
582      * the reference taken by object_property_add_child().
583      */
584     object_unref(obj);
585     return ok;
586 }
587 
588 void object_initialize_child_internal(Object *parent,
589                                       const char *propname,
590                                       void *child, size_t size,
591                                       const char *type)
592 {
593     object_initialize_child_with_props(parent, propname, child, size, type,
594                                        &error_abort, NULL);
595 }
596 
597 static inline bool object_property_is_child(ObjectProperty *prop)
598 {
599     return strstart(prop->type, "child<", NULL);
600 }
601 
602 static void object_property_del_all(Object *obj)
603 {
604     g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL);
605     ObjectProperty *prop;
606     ObjectPropertyIterator iter;
607     bool released;
608 
609     do {
610         released = false;
611         object_property_iter_init(&iter, obj);
612         while ((prop = object_property_iter_next(&iter)) != NULL) {
613             if (g_hash_table_add(done, prop)) {
614                 if (prop->release) {
615                     prop->release(obj, prop->name, prop->opaque);
616                     released = true;
617                     break;
618                 }
619             }
620         }
621     } while (released);
622 
623     g_hash_table_unref(obj->properties);
624 }
625 
626 static void object_property_del_child(Object *obj, Object *child)
627 {
628     ObjectProperty *prop;
629     GHashTableIter iter;
630     gpointer key, value;
631 
632     g_hash_table_iter_init(&iter, obj->properties);
633     while (g_hash_table_iter_next(&iter, &key, &value)) {
634         prop = value;
635         if (object_property_is_child(prop) && prop->opaque == child) {
636             if (prop->release) {
637                 prop->release(obj, prop->name, prop->opaque);
638                 prop->release = NULL;
639             }
640             break;
641         }
642     }
643     g_hash_table_iter_init(&iter, obj->properties);
644     while (g_hash_table_iter_next(&iter, &key, &value)) {
645         prop = value;
646         if (object_property_is_child(prop) && prop->opaque == child) {
647             g_hash_table_iter_remove(&iter);
648             break;
649         }
650     }
651 }
652 
653 void object_unparent(Object *obj)
654 {
655     if (obj->parent) {
656         object_property_del_child(obj->parent, obj);
657     }
658 }
659 
660 static void object_deinit(Object *obj, TypeImpl *type)
661 {
662     if (type->instance_finalize) {
663         type->instance_finalize(obj);
664     }
665 
666     if (type_has_parent(type)) {
667         object_deinit(obj, type_get_parent(type));
668     }
669 }
670 
671 static void object_finalize(void *data)
672 {
673     Object *obj = data;
674     TypeImpl *ti = obj->class->type;
675 
676     object_property_del_all(obj);
677     object_deinit(obj, ti);
678 
679     g_assert(obj->ref == 0);
680     if (obj->free) {
681         obj->free(obj);
682     }
683 }
684 
685 static Object *object_new_with_type(Type type)
686 {
687     Object *obj;
688 
689     g_assert(type != NULL);
690     type_initialize(type);
691 
692     obj = g_malloc(type->instance_size);
693     object_initialize_with_type(obj, type->instance_size, type);
694     obj->free = g_free;
695 
696     return obj;
697 }
698 
699 Object *object_new_with_class(ObjectClass *klass)
700 {
701     return object_new_with_type(klass->type);
702 }
703 
704 Object *object_new(const char *typename)
705 {
706     TypeImpl *ti = type_get_by_name(typename);
707 
708     return object_new_with_type(ti);
709 }
710 
711 
712 Object *object_new_with_props(const char *typename,
713                               Object *parent,
714                               const char *id,
715                               Error **errp,
716                               ...)
717 {
718     va_list vargs;
719     Object *obj;
720 
721     va_start(vargs, errp);
722     obj = object_new_with_propv(typename, parent, id, errp, vargs);
723     va_end(vargs);
724 
725     return obj;
726 }
727 
728 
729 Object *object_new_with_propv(const char *typename,
730                               Object *parent,
731                               const char *id,
732                               Error **errp,
733                               va_list vargs)
734 {
735     Object *obj;
736     ObjectClass *klass;
737     UserCreatable *uc;
738 
739     klass = object_class_by_name(typename);
740     if (!klass) {
741         error_setg(errp, "invalid object type: %s", typename);
742         return NULL;
743     }
744 
745     if (object_class_is_abstract(klass)) {
746         error_setg(errp, "object type '%s' is abstract", typename);
747         return NULL;
748     }
749     obj = object_new_with_type(klass->type);
750 
751     if (!object_set_propv(obj, errp, vargs)) {
752         goto error;
753     }
754 
755     if (id != NULL) {
756         object_property_add_child(parent, id, obj);
757     }
758 
759     uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
760     if (uc) {
761         if (!user_creatable_complete(uc, errp)) {
762             if (id != NULL) {
763                 object_unparent(obj);
764             }
765             goto error;
766         }
767     }
768 
769     object_unref(obj);
770     return obj;
771 
772  error:
773     object_unref(obj);
774     return NULL;
775 }
776 
777 
778 bool object_set_props(Object *obj,
779                      Error **errp,
780                      ...)
781 {
782     va_list vargs;
783     bool ret;
784 
785     va_start(vargs, errp);
786     ret = object_set_propv(obj, errp, vargs);
787     va_end(vargs);
788 
789     return ret;
790 }
791 
792 
793 bool object_set_propv(Object *obj,
794                      Error **errp,
795                      va_list vargs)
796 {
797     const char *propname;
798 
799     propname = va_arg(vargs, char *);
800     while (propname != NULL) {
801         const char *value = va_arg(vargs, char *);
802 
803         g_assert(value != NULL);
804         if (!object_property_parse(obj, propname, value, errp)) {
805             return false;
806         }
807         propname = va_arg(vargs, char *);
808     }
809 
810     return true;
811 }
812 
813 
814 Object *object_dynamic_cast(Object *obj, const char *typename)
815 {
816     if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
817         return obj;
818     }
819 
820     return NULL;
821 }
822 
823 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
824                                    const char *file, int line, const char *func)
825 {
826     trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
827                                      typename, file, line, func);
828 
829 #ifdef CONFIG_QOM_CAST_DEBUG
830     int i;
831     Object *inst;
832 
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;
836         }
837     }
838 
839     inst = object_dynamic_cast(obj, typename);
840 
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();
845     }
846 
847     assert(obj == inst);
848 
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]));
853         }
854         atomic_set(&obj->class->object_cast_cache[i - 1], typename);
855     }
856 
857 out:
858 #endif
859     return obj;
860 }
861 
862 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
863                                        const char *typename)
864 {
865     ObjectClass *ret = NULL;
866     TypeImpl *target_type;
867     TypeImpl *type;
868 
869     if (!class) {
870         return NULL;
871     }
872 
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;
877     }
878 
879     target_type = type_get_by_name(typename);
880     if (!target_type) {
881         /* target class type unknown, so fail the cast */
882         return NULL;
883     }
884 
885     if (type->class->interfaces &&
886             type_is_ancestor(target_type, type_interface)) {
887         int found = 0;
888         GSList *i;
889 
890         for (i = class->interfaces; i; i = i->next) {
891             ObjectClass *target_class = i->data;
892 
893             if (type_is_ancestor(target_class->type, target_type)) {
894                 ret = target_class;
895                 found++;
896             }
897          }
898 
899         /* The match was ambiguous, don't allow a cast */
900         if (found > 1) {
901             ret = NULL;
902         }
903     } else if (type_is_ancestor(type, target_type)) {
904         ret = class;
905     }
906 
907     return ret;
908 }
909 
910 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
911                                               const char *typename,
912                                               const char *file, int line,
913                                               const char *func)
914 {
915     ObjectClass *ret;
916 
917     trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
918                                            typename, file, line, func);
919 
920 #ifdef CONFIG_QOM_CAST_DEBUG
921     int i;
922 
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;
927         }
928     }
929 #else
930     if (!class || !class->interfaces) {
931         return class;
932     }
933 #endif
934 
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();
940     }
941 
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]));
947         }
948         atomic_set(&class->class_cast_cache[i - 1], typename);
949     }
950 out:
951 #endif
952     return ret;
953 }
954 
955 const char *object_get_typename(const Object *obj)
956 {
957     return obj->class->type->name;
958 }
959 
960 ObjectClass *object_get_class(Object *obj)
961 {
962     return obj->class;
963 }
964 
965 bool object_class_is_abstract(ObjectClass *klass)
966 {
967     return klass->type->abstract;
968 }
969 
970 const char *object_class_get_name(ObjectClass *klass)
971 {
972     return klass->type->name;
973 }
974 
975 ObjectClass *object_class_by_name(const char *typename)
976 {
977     TypeImpl *type = type_get_by_name(typename);
978 
979     if (!type) {
980         return NULL;
981     }
982 
983     type_initialize(type);
984 
985     return type->class;
986 }
987 
988 ObjectClass *module_object_class_by_name(const char *typename)
989 {
990     ObjectClass *oc;
991 
992     oc = object_class_by_name(typename);
993 #ifdef CONFIG_MODULES
994     if (!oc) {
995         module_load_qom_one(typename);
996         oc = object_class_by_name(typename);
997     }
998 #endif
999     return oc;
1000 }
1001 
1002 ObjectClass *object_class_get_parent(ObjectClass *class)
1003 {
1004     TypeImpl *type = type_get_parent(class->type);
1005 
1006     if (!type) {
1007         return NULL;
1008     }
1009 
1010     type_initialize(type);
1011 
1012     return type->class;
1013 }
1014 
1015 typedef struct OCFData
1016 {
1017     void (*fn)(ObjectClass *klass, void *opaque);
1018     const char *implements_type;
1019     bool include_abstract;
1020     void *opaque;
1021 } OCFData;
1022 
1023 static void object_class_foreach_tramp(gpointer key, gpointer value,
1024                                        gpointer opaque)
1025 {
1026     OCFData *data = opaque;
1027     TypeImpl *type = value;
1028     ObjectClass *k;
1029 
1030     type_initialize(type);
1031     k = type->class;
1032 
1033     if (!data->include_abstract && type->abstract) {
1034         return;
1035     }
1036 
1037     if (data->implements_type &&
1038         !object_class_dynamic_cast(k, data->implements_type)) {
1039         return;
1040     }
1041 
1042     data->fn(k, data->opaque);
1043 }
1044 
1045 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
1046                           const char *implements_type, bool include_abstract,
1047                           void *opaque)
1048 {
1049     OCFData data = { fn, implements_type, include_abstract, opaque };
1050 
1051     enumerating_types = true;
1052     g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
1053     enumerating_types = false;
1054 }
1055 
1056 static int do_object_child_foreach(Object *obj,
1057                                    int (*fn)(Object *child, void *opaque),
1058                                    void *opaque, bool recurse)
1059 {
1060     GHashTableIter iter;
1061     ObjectProperty *prop;
1062     int ret = 0;
1063 
1064     g_hash_table_iter_init(&iter, obj->properties);
1065     while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1066         if (object_property_is_child(prop)) {
1067             Object *child = prop->opaque;
1068 
1069             ret = fn(child, opaque);
1070             if (ret != 0) {
1071                 break;
1072             }
1073             if (recurse) {
1074                 ret = do_object_child_foreach(child, fn, opaque, true);
1075                 if (ret != 0) {
1076                     break;
1077                 }
1078             }
1079         }
1080     }
1081     return ret;
1082 }
1083 
1084 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1085                          void *opaque)
1086 {
1087     return do_object_child_foreach(obj, fn, opaque, false);
1088 }
1089 
1090 int object_child_foreach_recursive(Object *obj,
1091                                    int (*fn)(Object *child, void *opaque),
1092                                    void *opaque)
1093 {
1094     return do_object_child_foreach(obj, fn, opaque, true);
1095 }
1096 
1097 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
1098 {
1099     GSList **list = opaque;
1100 
1101     *list = g_slist_prepend(*list, klass);
1102 }
1103 
1104 GSList *object_class_get_list(const char *implements_type,
1105                               bool include_abstract)
1106 {
1107     GSList *list = NULL;
1108 
1109     object_class_foreach(object_class_get_list_tramp,
1110                          implements_type, include_abstract, &list);
1111     return list;
1112 }
1113 
1114 static gint object_class_cmp(gconstpointer a, gconstpointer b)
1115 {
1116     return strcasecmp(object_class_get_name((ObjectClass *)a),
1117                       object_class_get_name((ObjectClass *)b));
1118 }
1119 
1120 GSList *object_class_get_list_sorted(const char *implements_type,
1121                                      bool include_abstract)
1122 {
1123     return g_slist_sort(object_class_get_list(implements_type, include_abstract),
1124                         object_class_cmp);
1125 }
1126 
1127 Object *object_ref(void *objptr)
1128 {
1129     Object *obj = OBJECT(objptr);
1130     if (!obj) {
1131         return NULL;
1132     }
1133     atomic_inc(&obj->ref);
1134     return obj;
1135 }
1136 
1137 void object_unref(void *objptr)
1138 {
1139     Object *obj = OBJECT(objptr);
1140     if (!obj) {
1141         return;
1142     }
1143     g_assert(obj->ref > 0);
1144 
1145     /* parent always holds a reference to its children */
1146     if (atomic_fetch_dec(&obj->ref) == 1) {
1147         object_finalize(obj);
1148     }
1149 }
1150 
1151 ObjectProperty *
1152 object_property_try_add(Object *obj, const char *name, const char *type,
1153                         ObjectPropertyAccessor *get,
1154                         ObjectPropertyAccessor *set,
1155                         ObjectPropertyRelease *release,
1156                         void *opaque, Error **errp)
1157 {
1158     ObjectProperty *prop;
1159     size_t name_len = strlen(name);
1160 
1161     if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
1162         int i;
1163         ObjectProperty *ret;
1164         char *name_no_array = g_strdup(name);
1165 
1166         name_no_array[name_len - 3] = '\0';
1167         for (i = 0; ; ++i) {
1168             char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1169 
1170             ret = object_property_try_add(obj, full_name, type, get, set,
1171                                           release, opaque, NULL);
1172             g_free(full_name);
1173             if (ret) {
1174                 break;
1175             }
1176         }
1177         g_free(name_no_array);
1178         return ret;
1179     }
1180 
1181     if (object_property_find(obj, name, NULL) != NULL) {
1182         error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')",
1183                    name, object_get_typename(obj));
1184         return NULL;
1185     }
1186 
1187     prop = g_malloc0(sizeof(*prop));
1188 
1189     prop->name = g_strdup(name);
1190     prop->type = g_strdup(type);
1191 
1192     prop->get = get;
1193     prop->set = set;
1194     prop->release = release;
1195     prop->opaque = opaque;
1196 
1197     g_hash_table_insert(obj->properties, prop->name, prop);
1198     return prop;
1199 }
1200 
1201 ObjectProperty *
1202 object_property_add(Object *obj, const char *name, const char *type,
1203                     ObjectPropertyAccessor *get,
1204                     ObjectPropertyAccessor *set,
1205                     ObjectPropertyRelease *release,
1206                     void *opaque)
1207 {
1208     return object_property_try_add(obj, name, type, get, set, release,
1209                                    opaque, &error_abort);
1210 }
1211 
1212 ObjectProperty *
1213 object_class_property_add(ObjectClass *klass,
1214                           const char *name,
1215                           const char *type,
1216                           ObjectPropertyAccessor *get,
1217                           ObjectPropertyAccessor *set,
1218                           ObjectPropertyRelease *release,
1219                           void *opaque)
1220 {
1221     ObjectProperty *prop;
1222 
1223     assert(!object_class_property_find(klass, name, NULL));
1224 
1225     prop = g_malloc0(sizeof(*prop));
1226 
1227     prop->name = g_strdup(name);
1228     prop->type = g_strdup(type);
1229 
1230     prop->get = get;
1231     prop->set = set;
1232     prop->release = release;
1233     prop->opaque = opaque;
1234 
1235     g_hash_table_insert(klass->properties, prop->name, prop);
1236 
1237     return prop;
1238 }
1239 
1240 ObjectProperty *object_property_find(Object *obj, const char *name,
1241                                      Error **errp)
1242 {
1243     ObjectProperty *prop;
1244     ObjectClass *klass = object_get_class(obj);
1245 
1246     prop = object_class_property_find(klass, name, NULL);
1247     if (prop) {
1248         return prop;
1249     }
1250 
1251     prop = g_hash_table_lookup(obj->properties, name);
1252     if (prop) {
1253         return prop;
1254     }
1255 
1256     error_setg(errp, "Property '.%s' not found", name);
1257     return NULL;
1258 }
1259 
1260 void object_property_iter_init(ObjectPropertyIterator *iter,
1261                                Object *obj)
1262 {
1263     g_hash_table_iter_init(&iter->iter, obj->properties);
1264     iter->nextclass = object_get_class(obj);
1265 }
1266 
1267 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1268 {
1269     gpointer key, val;
1270     while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1271         if (!iter->nextclass) {
1272             return NULL;
1273         }
1274         g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1275         iter->nextclass = object_class_get_parent(iter->nextclass);
1276     }
1277     return val;
1278 }
1279 
1280 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1281                                      ObjectClass *klass)
1282 {
1283     g_hash_table_iter_init(&iter->iter, klass->properties);
1284     iter->nextclass = object_class_get_parent(klass);
1285 }
1286 
1287 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1288                                            Error **errp)
1289 {
1290     ObjectProperty *prop;
1291     ObjectClass *parent_klass;
1292 
1293     parent_klass = object_class_get_parent(klass);
1294     if (parent_klass) {
1295         prop = object_class_property_find(parent_klass, name, NULL);
1296         if (prop) {
1297             return prop;
1298         }
1299     }
1300 
1301     prop = g_hash_table_lookup(klass->properties, name);
1302     if (!prop) {
1303         error_setg(errp, "Property '.%s' not found", name);
1304     }
1305     return prop;
1306 }
1307 
1308 void object_property_del(Object *obj, const char *name)
1309 {
1310     ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1311 
1312     if (prop->release) {
1313         prop->release(obj, name, prop->opaque);
1314     }
1315     g_hash_table_remove(obj->properties, name);
1316 }
1317 
1318 bool object_property_get(Object *obj, const char *name, Visitor *v,
1319                          Error **errp)
1320 {
1321     Error *err = NULL;
1322     ObjectProperty *prop = object_property_find(obj, name, errp);
1323 
1324     if (prop == NULL) {
1325         return false;
1326     }
1327 
1328     if (!prop->get) {
1329         error_setg(errp, QERR_PERMISSION_DENIED);
1330         return false;
1331     }
1332     prop->get(obj, v, name, prop->opaque, &err);
1333     error_propagate(errp, err);
1334     return !err;
1335 }
1336 
1337 bool object_property_set(Object *obj, const char *name, Visitor *v,
1338                          Error **errp)
1339 {
1340     Error *err = NULL;
1341     ObjectProperty *prop = object_property_find(obj, name, errp);
1342 
1343     if (prop == NULL) {
1344         return false;
1345     }
1346 
1347     if (!prop->set) {
1348         error_setg(errp, QERR_PERMISSION_DENIED);
1349         return false;
1350     }
1351     prop->set(obj, v, name, prop->opaque, &err);
1352     error_propagate(errp, err);
1353     return !err;
1354 }
1355 
1356 bool object_property_set_str(Object *obj, const char *name,
1357                              const char *value, Error **errp)
1358 {
1359     QString *qstr = qstring_from_str(value);
1360     bool ok = object_property_set_qobject(obj, name, QOBJECT(qstr), errp);
1361 
1362     qobject_unref(qstr);
1363     return ok;
1364 }
1365 
1366 char *object_property_get_str(Object *obj, const char *name,
1367                               Error **errp)
1368 {
1369     QObject *ret = object_property_get_qobject(obj, name, errp);
1370     char *retval;
1371 
1372     if (!ret) {
1373         return NULL;
1374     }
1375 
1376     retval = g_strdup(qobject_get_try_str(ret));
1377     if (!retval) {
1378         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
1379     }
1380 
1381     qobject_unref(ret);
1382     return retval;
1383 }
1384 
1385 bool object_property_set_link(Object *obj, const char *name,
1386                               Object *value, Error **errp)
1387 {
1388     g_autofree char *path = NULL;
1389 
1390     if (value) {
1391         path = object_get_canonical_path(value);
1392     }
1393     return object_property_set_str(obj, name, path ?: "", errp);
1394 }
1395 
1396 Object *object_property_get_link(Object *obj, const char *name,
1397                                  Error **errp)
1398 {
1399     char *str = object_property_get_str(obj, name, errp);
1400     Object *target = NULL;
1401 
1402     if (str && *str) {
1403         target = object_resolve_path(str, NULL);
1404         if (!target) {
1405             error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1406                       "Device '%s' not found", str);
1407         }
1408     }
1409 
1410     g_free(str);
1411     return target;
1412 }
1413 
1414 bool object_property_set_bool(Object *obj, const char *name,
1415                               bool value, Error **errp)
1416 {
1417     QBool *qbool = qbool_from_bool(value);
1418     bool ok = object_property_set_qobject(obj, name, QOBJECT(qbool), errp);
1419 
1420     qobject_unref(qbool);
1421     return ok;
1422 }
1423 
1424 bool object_property_get_bool(Object *obj, const char *name,
1425                               Error **errp)
1426 {
1427     QObject *ret = object_property_get_qobject(obj, name, errp);
1428     QBool *qbool;
1429     bool retval;
1430 
1431     if (!ret) {
1432         return false;
1433     }
1434     qbool = qobject_to(QBool, ret);
1435     if (!qbool) {
1436         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
1437         retval = false;
1438     } else {
1439         retval = qbool_get_bool(qbool);
1440     }
1441 
1442     qobject_unref(ret);
1443     return retval;
1444 }
1445 
1446 bool object_property_set_int(Object *obj, const char *name,
1447                              int64_t value, Error **errp)
1448 {
1449     QNum *qnum = qnum_from_int(value);
1450     bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1451 
1452     qobject_unref(qnum);
1453     return ok;
1454 }
1455 
1456 int64_t object_property_get_int(Object *obj, const char *name,
1457                                 Error **errp)
1458 {
1459     QObject *ret = object_property_get_qobject(obj, name, errp);
1460     QNum *qnum;
1461     int64_t retval;
1462 
1463     if (!ret) {
1464         return -1;
1465     }
1466 
1467     qnum = qobject_to(QNum, ret);
1468     if (!qnum || !qnum_get_try_int(qnum, &retval)) {
1469         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
1470         retval = -1;
1471     }
1472 
1473     qobject_unref(ret);
1474     return retval;
1475 }
1476 
1477 static void object_property_init_defval(Object *obj, ObjectProperty *prop)
1478 {
1479     Visitor *v = qobject_input_visitor_new(prop->defval);
1480 
1481     assert(prop->set != NULL);
1482     prop->set(obj, v, prop->name, prop->opaque, &error_abort);
1483 
1484     visit_free(v);
1485 }
1486 
1487 static void object_property_set_default(ObjectProperty *prop, QObject *defval)
1488 {
1489     assert(!prop->defval);
1490     assert(!prop->init);
1491 
1492     prop->defval = defval;
1493     prop->init = object_property_init_defval;
1494 }
1495 
1496 void object_property_set_default_bool(ObjectProperty *prop, bool value)
1497 {
1498     object_property_set_default(prop, QOBJECT(qbool_from_bool(value)));
1499 }
1500 
1501 void object_property_set_default_str(ObjectProperty *prop, const char *value)
1502 {
1503     object_property_set_default(prop, QOBJECT(qstring_from_str(value)));
1504 }
1505 
1506 void object_property_set_default_int(ObjectProperty *prop, int64_t value)
1507 {
1508     object_property_set_default(prop, QOBJECT(qnum_from_int(value)));
1509 }
1510 
1511 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value)
1512 {
1513     object_property_set_default(prop, QOBJECT(qnum_from_uint(value)));
1514 }
1515 
1516 bool object_property_set_uint(Object *obj, const char *name,
1517                               uint64_t value, Error **errp)
1518 {
1519     QNum *qnum = qnum_from_uint(value);
1520     bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1521 
1522     qobject_unref(qnum);
1523     return ok;
1524 }
1525 
1526 uint64_t object_property_get_uint(Object *obj, const char *name,
1527                                   Error **errp)
1528 {
1529     QObject *ret = object_property_get_qobject(obj, name, errp);
1530     QNum *qnum;
1531     uint64_t retval;
1532 
1533     if (!ret) {
1534         return 0;
1535     }
1536     qnum = qobject_to(QNum, ret);
1537     if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1538         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1539         retval = 0;
1540     }
1541 
1542     qobject_unref(ret);
1543     return retval;
1544 }
1545 
1546 typedef struct EnumProperty {
1547     const QEnumLookup *lookup;
1548     int (*get)(Object *, Error **);
1549     void (*set)(Object *, int, Error **);
1550 } EnumProperty;
1551 
1552 int object_property_get_enum(Object *obj, const char *name,
1553                              const char *typename, Error **errp)
1554 {
1555     char *str;
1556     int ret;
1557     ObjectProperty *prop = object_property_find(obj, name, errp);
1558     EnumProperty *enumprop;
1559 
1560     if (prop == NULL) {
1561         return 0;
1562     }
1563 
1564     if (!g_str_equal(prop->type, typename)) {
1565         error_setg(errp, "Property %s on %s is not '%s' enum type",
1566                    name, object_class_get_name(
1567                        object_get_class(obj)), typename);
1568         return 0;
1569     }
1570 
1571     enumprop = prop->opaque;
1572 
1573     str = object_property_get_str(obj, name, errp);
1574     if (!str) {
1575         return 0;
1576     }
1577 
1578     ret = qapi_enum_parse(enumprop->lookup, str, -1, errp);
1579     g_free(str);
1580 
1581     return ret;
1582 }
1583 
1584 bool object_property_parse(Object *obj, const char *name,
1585                            const char *string, Error **errp)
1586 {
1587     Visitor *v = string_input_visitor_new(string);
1588     bool ok = object_property_set(obj, name, v, errp);
1589 
1590     visit_free(v);
1591     return ok;
1592 }
1593 
1594 char *object_property_print(Object *obj, const char *name, bool human,
1595                             Error **errp)
1596 {
1597     Visitor *v;
1598     char *string = NULL;
1599 
1600     v = string_output_visitor_new(human, &string);
1601     if (!object_property_get(obj, name, v, errp)) {
1602         goto out;
1603     }
1604 
1605     visit_complete(v, &string);
1606 
1607 out:
1608     visit_free(v);
1609     return string;
1610 }
1611 
1612 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1613 {
1614     ObjectProperty *prop = object_property_find(obj, name, errp);
1615     if (prop == NULL) {
1616         return NULL;
1617     }
1618 
1619     return prop->type;
1620 }
1621 
1622 Object *object_get_root(void)
1623 {
1624     static Object *root;
1625 
1626     if (!root) {
1627         root = object_new("container");
1628     }
1629 
1630     return root;
1631 }
1632 
1633 Object *object_get_objects_root(void)
1634 {
1635     return container_get(object_get_root(), "/objects");
1636 }
1637 
1638 Object *object_get_internal_root(void)
1639 {
1640     static Object *internal_root;
1641 
1642     if (!internal_root) {
1643         internal_root = object_new("container");
1644     }
1645 
1646     return internal_root;
1647 }
1648 
1649 static void object_get_child_property(Object *obj, Visitor *v,
1650                                       const char *name, void *opaque,
1651                                       Error **errp)
1652 {
1653     Object *child = opaque;
1654     char *path;
1655 
1656     path = object_get_canonical_path(child);
1657     visit_type_str(v, name, &path, errp);
1658     g_free(path);
1659 }
1660 
1661 static Object *object_resolve_child_property(Object *parent, void *opaque,
1662                                              const char *part)
1663 {
1664     return opaque;
1665 }
1666 
1667 static void object_finalize_child_property(Object *obj, const char *name,
1668                                            void *opaque)
1669 {
1670     Object *child = opaque;
1671 
1672     if (child->class->unparent) {
1673         (child->class->unparent)(child);
1674     }
1675     child->parent = NULL;
1676     object_unref(child);
1677 }
1678 
1679 ObjectProperty *
1680 object_property_try_add_child(Object *obj, const char *name,
1681                               Object *child, Error **errp)
1682 {
1683     g_autofree char *type = NULL;
1684     ObjectProperty *op;
1685 
1686     assert(!child->parent);
1687 
1688     type = g_strdup_printf("child<%s>", object_get_typename(child));
1689 
1690     op = object_property_try_add(obj, name, type, object_get_child_property,
1691                                  NULL, object_finalize_child_property,
1692                                  child, errp);
1693     if (!op) {
1694         return NULL;
1695     }
1696     op->resolve = object_resolve_child_property;
1697     object_ref(child);
1698     child->parent = obj;
1699     return op;
1700 }
1701 
1702 ObjectProperty *
1703 object_property_add_child(Object *obj, const char *name,
1704                           Object *child)
1705 {
1706     return object_property_try_add_child(obj, name, child, &error_abort);
1707 }
1708 
1709 void object_property_allow_set_link(const Object *obj, const char *name,
1710                                     Object *val, Error **errp)
1711 {
1712     /* Allow the link to be set, always */
1713 }
1714 
1715 typedef struct {
1716     union {
1717         Object **targetp;
1718         Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer  */
1719         ptrdiff_t offset; /* if OBJ_PROP_LINK_CLASS */
1720     };
1721     void (*check)(const Object *, const char *, Object *, Error **);
1722     ObjectPropertyLinkFlags flags;
1723 } LinkProperty;
1724 
1725 static Object **
1726 object_link_get_targetp(Object *obj, LinkProperty *lprop)
1727 {
1728     if (lprop->flags & OBJ_PROP_LINK_DIRECT) {
1729         return &lprop->target;
1730     } else if (lprop->flags & OBJ_PROP_LINK_CLASS) {
1731         return (void *)obj + lprop->offset;
1732     } else {
1733         return lprop->targetp;
1734     }
1735 }
1736 
1737 static void object_get_link_property(Object *obj, Visitor *v,
1738                                      const char *name, void *opaque,
1739                                      Error **errp)
1740 {
1741     LinkProperty *lprop = opaque;
1742     Object **targetp = object_link_get_targetp(obj, lprop);
1743     char *path;
1744 
1745     if (*targetp) {
1746         path = object_get_canonical_path(*targetp);
1747         visit_type_str(v, name, &path, errp);
1748         g_free(path);
1749     } else {
1750         path = (char *)"";
1751         visit_type_str(v, name, &path, errp);
1752     }
1753 }
1754 
1755 /*
1756  * object_resolve_link:
1757  *
1758  * Lookup an object and ensure its type matches the link property type.  This
1759  * is similar to object_resolve_path() except type verification against the
1760  * link property is performed.
1761  *
1762  * Returns: The matched object or NULL on path lookup failures.
1763  */
1764 static Object *object_resolve_link(Object *obj, const char *name,
1765                                    const char *path, Error **errp)
1766 {
1767     const char *type;
1768     char *target_type;
1769     bool ambiguous = false;
1770     Object *target;
1771 
1772     /* Go from link<FOO> to FOO.  */
1773     type = object_property_get_type(obj, name, NULL);
1774     target_type = g_strndup(&type[5], strlen(type) - 6);
1775     target = object_resolve_path_type(path, target_type, &ambiguous);
1776 
1777     if (ambiguous) {
1778         error_setg(errp, "Path '%s' does not uniquely identify an object",
1779                    path);
1780     } else if (!target) {
1781         target = object_resolve_path(path, &ambiguous);
1782         if (target || ambiguous) {
1783             error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1784         } else {
1785             error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1786                       "Device '%s' not found", path);
1787         }
1788         target = NULL;
1789     }
1790     g_free(target_type);
1791 
1792     return target;
1793 }
1794 
1795 static void object_set_link_property(Object *obj, Visitor *v,
1796                                      const char *name, void *opaque,
1797                                      Error **errp)
1798 {
1799     Error *local_err = NULL;
1800     LinkProperty *prop = opaque;
1801     Object **targetp = object_link_get_targetp(obj, prop);
1802     Object *old_target = *targetp;
1803     Object *new_target;
1804     char *path = NULL;
1805 
1806     if (!visit_type_str(v, name, &path, errp)) {
1807         return;
1808     }
1809 
1810     if (*path) {
1811         new_target = object_resolve_link(obj, name, path, errp);
1812         if (!new_target) {
1813             g_free(path);
1814             return;
1815         }
1816     } else {
1817         new_target = NULL;
1818     }
1819 
1820     g_free(path);
1821 
1822     prop->check(obj, name, new_target, &local_err);
1823     if (local_err) {
1824         error_propagate(errp, local_err);
1825         return;
1826     }
1827 
1828     *targetp = new_target;
1829     if (prop->flags & OBJ_PROP_LINK_STRONG) {
1830         object_ref(new_target);
1831         object_unref(old_target);
1832     }
1833 }
1834 
1835 static Object *object_resolve_link_property(Object *parent, void *opaque,
1836                                             const char *part)
1837 {
1838     LinkProperty *lprop = opaque;
1839 
1840     return *object_link_get_targetp(parent, lprop);
1841 }
1842 
1843 static void object_release_link_property(Object *obj, const char *name,
1844                                          void *opaque)
1845 {
1846     LinkProperty *prop = opaque;
1847     Object **targetp = object_link_get_targetp(obj, prop);
1848 
1849     if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) {
1850         object_unref(*targetp);
1851     }
1852     if (!(prop->flags & OBJ_PROP_LINK_CLASS)) {
1853         g_free(prop);
1854     }
1855 }
1856 
1857 static ObjectProperty *
1858 object_add_link_prop(Object *obj, const char *name,
1859                      const char *type, void *ptr,
1860                      void (*check)(const Object *, const char *,
1861                                    Object *, Error **),
1862                      ObjectPropertyLinkFlags flags)
1863 {
1864     LinkProperty *prop = g_malloc(sizeof(*prop));
1865     g_autofree char *full_type = NULL;
1866     ObjectProperty *op;
1867 
1868     if (flags & OBJ_PROP_LINK_DIRECT) {
1869         prop->target = ptr;
1870     } else {
1871         prop->targetp = ptr;
1872     }
1873     prop->check = check;
1874     prop->flags = flags;
1875 
1876     full_type = g_strdup_printf("link<%s>", type);
1877 
1878     op = object_property_add(obj, name, full_type,
1879                              object_get_link_property,
1880                              check ? object_set_link_property : NULL,
1881                              object_release_link_property,
1882                              prop);
1883     op->resolve = object_resolve_link_property;
1884     return op;
1885 }
1886 
1887 ObjectProperty *
1888 object_property_add_link(Object *obj, const char *name,
1889                          const char *type, Object **targetp,
1890                          void (*check)(const Object *, const char *,
1891                                        Object *, Error **),
1892                          ObjectPropertyLinkFlags flags)
1893 {
1894     return object_add_link_prop(obj, name, type, targetp, check, flags);
1895 }
1896 
1897 ObjectProperty *
1898 object_class_property_add_link(ObjectClass *oc,
1899     const char *name,
1900     const char *type, ptrdiff_t offset,
1901     void (*check)(const Object *obj, const char *name,
1902                   Object *val, Error **errp),
1903     ObjectPropertyLinkFlags flags)
1904 {
1905     LinkProperty *prop = g_new0(LinkProperty, 1);
1906     char *full_type;
1907     ObjectProperty *op;
1908 
1909     prop->offset = offset;
1910     prop->check = check;
1911     prop->flags = flags | OBJ_PROP_LINK_CLASS;
1912 
1913     full_type = g_strdup_printf("link<%s>", type);
1914 
1915     op = object_class_property_add(oc, name, full_type,
1916                                    object_get_link_property,
1917                                    check ? object_set_link_property : NULL,
1918                                    object_release_link_property,
1919                                    prop);
1920 
1921     op->resolve = object_resolve_link_property;
1922 
1923     g_free(full_type);
1924     return op;
1925 }
1926 
1927 ObjectProperty *
1928 object_property_add_const_link(Object *obj, const char *name,
1929                                Object *target)
1930 {
1931     return object_add_link_prop(obj, name,
1932                                 object_get_typename(target), target,
1933                                 NULL, OBJ_PROP_LINK_DIRECT);
1934 }
1935 
1936 const char *object_get_canonical_path_component(const Object *obj)
1937 {
1938     ObjectProperty *prop = NULL;
1939     GHashTableIter iter;
1940 
1941     if (obj->parent == NULL) {
1942         return NULL;
1943     }
1944 
1945     g_hash_table_iter_init(&iter, obj->parent->properties);
1946     while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1947         if (!object_property_is_child(prop)) {
1948             continue;
1949         }
1950 
1951         if (prop->opaque == obj) {
1952             return prop->name;
1953         }
1954     }
1955 
1956     /* obj had a parent but was not a child, should never happen */
1957     g_assert_not_reached();
1958     return NULL;
1959 }
1960 
1961 char *object_get_canonical_path(const Object *obj)
1962 {
1963     Object *root = object_get_root();
1964     char *newpath, *path = NULL;
1965 
1966     if (obj == root) {
1967         return g_strdup("/");
1968     }
1969 
1970     do {
1971         const char *component = object_get_canonical_path_component(obj);
1972 
1973         if (!component) {
1974             /* A canonical path must be complete, so discard what was
1975              * collected so far.
1976              */
1977             g_free(path);
1978             return NULL;
1979         }
1980 
1981         newpath = g_strdup_printf("/%s%s", component, path ? path : "");
1982         g_free(path);
1983         path = newpath;
1984         obj = obj->parent;
1985     } while (obj != root);
1986 
1987     return path;
1988 }
1989 
1990 Object *object_resolve_path_component(Object *parent, const char *part)
1991 {
1992     ObjectProperty *prop = object_property_find(parent, part, NULL);
1993     if (prop == NULL) {
1994         return NULL;
1995     }
1996 
1997     if (prop->resolve) {
1998         return prop->resolve(parent, prop->opaque, part);
1999     } else {
2000         return NULL;
2001     }
2002 }
2003 
2004 static Object *object_resolve_abs_path(Object *parent,
2005                                           char **parts,
2006                                           const char *typename)
2007 {
2008     Object *child;
2009 
2010     if (*parts == NULL) {
2011         return object_dynamic_cast(parent, typename);
2012     }
2013 
2014     if (strcmp(*parts, "") == 0) {
2015         return object_resolve_abs_path(parent, parts + 1, typename);
2016     }
2017 
2018     child = object_resolve_path_component(parent, *parts);
2019     if (!child) {
2020         return NULL;
2021     }
2022 
2023     return object_resolve_abs_path(child, parts + 1, typename);
2024 }
2025 
2026 static Object *object_resolve_partial_path(Object *parent,
2027                                            char **parts,
2028                                            const char *typename,
2029                                            bool *ambiguous)
2030 {
2031     Object *obj;
2032     GHashTableIter iter;
2033     ObjectProperty *prop;
2034 
2035     obj = object_resolve_abs_path(parent, parts, typename);
2036 
2037     g_hash_table_iter_init(&iter, parent->properties);
2038     while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
2039         Object *found;
2040 
2041         if (!object_property_is_child(prop)) {
2042             continue;
2043         }
2044 
2045         found = object_resolve_partial_path(prop->opaque, parts,
2046                                             typename, ambiguous);
2047         if (found) {
2048             if (obj) {
2049                 *ambiguous = true;
2050                 return NULL;
2051             }
2052             obj = found;
2053         }
2054 
2055         if (*ambiguous) {
2056             return NULL;
2057         }
2058     }
2059 
2060     return obj;
2061 }
2062 
2063 Object *object_resolve_path_type(const char *path, const char *typename,
2064                                  bool *ambiguousp)
2065 {
2066     Object *obj;
2067     char **parts;
2068 
2069     parts = g_strsplit(path, "/", 0);
2070     assert(parts);
2071 
2072     if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
2073         bool ambiguous = false;
2074         obj = object_resolve_partial_path(object_get_root(), parts,
2075                                           typename, &ambiguous);
2076         if (ambiguousp) {
2077             *ambiguousp = ambiguous;
2078         }
2079     } else {
2080         obj = object_resolve_abs_path(object_get_root(), parts + 1, typename);
2081     }
2082 
2083     g_strfreev(parts);
2084 
2085     return obj;
2086 }
2087 
2088 Object *object_resolve_path(const char *path, bool *ambiguous)
2089 {
2090     return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
2091 }
2092 
2093 typedef struct StringProperty
2094 {
2095     char *(*get)(Object *, Error **);
2096     void (*set)(Object *, const char *, Error **);
2097 } StringProperty;
2098 
2099 static void property_get_str(Object *obj, Visitor *v, const char *name,
2100                              void *opaque, Error **errp)
2101 {
2102     StringProperty *prop = opaque;
2103     char *value;
2104     Error *err = NULL;
2105 
2106     value = prop->get(obj, &err);
2107     if (err) {
2108         error_propagate(errp, err);
2109         return;
2110     }
2111 
2112     visit_type_str(v, name, &value, errp);
2113     g_free(value);
2114 }
2115 
2116 static void property_set_str(Object *obj, Visitor *v, const char *name,
2117                              void *opaque, Error **errp)
2118 {
2119     StringProperty *prop = opaque;
2120     char *value;
2121 
2122     if (!visit_type_str(v, name, &value, errp)) {
2123         return;
2124     }
2125 
2126     prop->set(obj, value, errp);
2127     g_free(value);
2128 }
2129 
2130 static void property_release_str(Object *obj, const char *name,
2131                                  void *opaque)
2132 {
2133     StringProperty *prop = opaque;
2134     g_free(prop);
2135 }
2136 
2137 ObjectProperty *
2138 object_property_add_str(Object *obj, const char *name,
2139                         char *(*get)(Object *, Error **),
2140                         void (*set)(Object *, const char *, Error **))
2141 {
2142     StringProperty *prop = g_malloc0(sizeof(*prop));
2143 
2144     prop->get = get;
2145     prop->set = set;
2146 
2147     return object_property_add(obj, name, "string",
2148                                get ? property_get_str : NULL,
2149                                set ? property_set_str : NULL,
2150                                property_release_str,
2151                                prop);
2152 }
2153 
2154 ObjectProperty *
2155 object_class_property_add_str(ObjectClass *klass, const char *name,
2156                                    char *(*get)(Object *, Error **),
2157                                    void (*set)(Object *, const char *,
2158                                                Error **))
2159 {
2160     StringProperty *prop = g_malloc0(sizeof(*prop));
2161 
2162     prop->get = get;
2163     prop->set = set;
2164 
2165     return object_class_property_add(klass, name, "string",
2166                                      get ? property_get_str : NULL,
2167                                      set ? property_set_str : NULL,
2168                                      NULL,
2169                                      prop);
2170 }
2171 
2172 typedef struct BoolProperty
2173 {
2174     bool (*get)(Object *, Error **);
2175     void (*set)(Object *, bool, Error **);
2176 } BoolProperty;
2177 
2178 static void property_get_bool(Object *obj, Visitor *v, const char *name,
2179                               void *opaque, Error **errp)
2180 {
2181     BoolProperty *prop = opaque;
2182     bool value;
2183     Error *err = NULL;
2184 
2185     value = prop->get(obj, &err);
2186     if (err) {
2187         error_propagate(errp, err);
2188         return;
2189     }
2190 
2191     visit_type_bool(v, name, &value, errp);
2192 }
2193 
2194 static void property_set_bool(Object *obj, Visitor *v, const char *name,
2195                               void *opaque, Error **errp)
2196 {
2197     BoolProperty *prop = opaque;
2198     bool value;
2199 
2200     if (!visit_type_bool(v, name, &value, errp)) {
2201         return;
2202     }
2203 
2204     prop->set(obj, value, errp);
2205 }
2206 
2207 static void property_release_bool(Object *obj, const char *name,
2208                                   void *opaque)
2209 {
2210     BoolProperty *prop = opaque;
2211     g_free(prop);
2212 }
2213 
2214 ObjectProperty *
2215 object_property_add_bool(Object *obj, const char *name,
2216                          bool (*get)(Object *, Error **),
2217                          void (*set)(Object *, bool, Error **))
2218 {
2219     BoolProperty *prop = g_malloc0(sizeof(*prop));
2220 
2221     prop->get = get;
2222     prop->set = set;
2223 
2224     return object_property_add(obj, name, "bool",
2225                                get ? property_get_bool : NULL,
2226                                set ? property_set_bool : NULL,
2227                                property_release_bool,
2228                                prop);
2229 }
2230 
2231 ObjectProperty *
2232 object_class_property_add_bool(ObjectClass *klass, const char *name,
2233                                     bool (*get)(Object *, Error **),
2234                                     void (*set)(Object *, bool, Error **))
2235 {
2236     BoolProperty *prop = g_malloc0(sizeof(*prop));
2237 
2238     prop->get = get;
2239     prop->set = set;
2240 
2241     return object_class_property_add(klass, name, "bool",
2242                                      get ? property_get_bool : NULL,
2243                                      set ? property_set_bool : NULL,
2244                                      NULL,
2245                                      prop);
2246 }
2247 
2248 static void property_get_enum(Object *obj, Visitor *v, const char *name,
2249                               void *opaque, Error **errp)
2250 {
2251     EnumProperty *prop = opaque;
2252     int value;
2253     Error *err = NULL;
2254 
2255     value = prop->get(obj, &err);
2256     if (err) {
2257         error_propagate(errp, err);
2258         return;
2259     }
2260 
2261     visit_type_enum(v, name, &value, prop->lookup, errp);
2262 }
2263 
2264 static void property_set_enum(Object *obj, Visitor *v, const char *name,
2265                               void *opaque, Error **errp)
2266 {
2267     EnumProperty *prop = opaque;
2268     int value;
2269 
2270     if (!visit_type_enum(v, name, &value, prop->lookup, errp)) {
2271         return;
2272     }
2273     prop->set(obj, value, errp);
2274 }
2275 
2276 static void property_release_enum(Object *obj, const char *name,
2277                                   void *opaque)
2278 {
2279     EnumProperty *prop = opaque;
2280     g_free(prop);
2281 }
2282 
2283 ObjectProperty *
2284 object_property_add_enum(Object *obj, const char *name,
2285                          const char *typename,
2286                          const QEnumLookup *lookup,
2287                          int (*get)(Object *, Error **),
2288                          void (*set)(Object *, int, Error **))
2289 {
2290     EnumProperty *prop = g_malloc(sizeof(*prop));
2291 
2292     prop->lookup = lookup;
2293     prop->get = get;
2294     prop->set = set;
2295 
2296     return object_property_add(obj, name, typename,
2297                                get ? property_get_enum : NULL,
2298                                set ? property_set_enum : NULL,
2299                                property_release_enum,
2300                                prop);
2301 }
2302 
2303 ObjectProperty *
2304 object_class_property_add_enum(ObjectClass *klass, const char *name,
2305                                     const char *typename,
2306                                     const QEnumLookup *lookup,
2307                                     int (*get)(Object *, Error **),
2308                                     void (*set)(Object *, int, Error **))
2309 {
2310     EnumProperty *prop = g_malloc(sizeof(*prop));
2311 
2312     prop->lookup = lookup;
2313     prop->get = get;
2314     prop->set = set;
2315 
2316     return object_class_property_add(klass, name, typename,
2317                                      get ? property_get_enum : NULL,
2318                                      set ? property_set_enum : NULL,
2319                                      NULL,
2320                                      prop);
2321 }
2322 
2323 typedef struct TMProperty {
2324     void (*get)(Object *, struct tm *, Error **);
2325 } TMProperty;
2326 
2327 static void property_get_tm(Object *obj, Visitor *v, const char *name,
2328                             void *opaque, Error **errp)
2329 {
2330     TMProperty *prop = opaque;
2331     Error *err = NULL;
2332     struct tm value;
2333 
2334     prop->get(obj, &value, &err);
2335     if (err) {
2336         error_propagate(errp, err);
2337         return;
2338     }
2339 
2340     if (!visit_start_struct(v, name, NULL, 0, errp)) {
2341         return;
2342     }
2343     if (!visit_type_int32(v, "tm_year", &value.tm_year, errp)) {
2344         goto out_end;
2345     }
2346     if (!visit_type_int32(v, "tm_mon", &value.tm_mon, errp)) {
2347         goto out_end;
2348     }
2349     if (!visit_type_int32(v, "tm_mday", &value.tm_mday, errp)) {
2350         goto out_end;
2351     }
2352     if (!visit_type_int32(v, "tm_hour", &value.tm_hour, errp)) {
2353         goto out_end;
2354     }
2355     if (!visit_type_int32(v, "tm_min", &value.tm_min, errp)) {
2356         goto out_end;
2357     }
2358     if (!visit_type_int32(v, "tm_sec", &value.tm_sec, errp)) {
2359         goto out_end;
2360     }
2361     visit_check_struct(v, errp);
2362 out_end:
2363     visit_end_struct(v, NULL);
2364 }
2365 
2366 static void property_release_tm(Object *obj, const char *name,
2367                                 void *opaque)
2368 {
2369     TMProperty *prop = opaque;
2370     g_free(prop);
2371 }
2372 
2373 ObjectProperty *
2374 object_property_add_tm(Object *obj, const char *name,
2375                        void (*get)(Object *, struct tm *, Error **))
2376 {
2377     TMProperty *prop = g_malloc0(sizeof(*prop));
2378 
2379     prop->get = get;
2380 
2381     return object_property_add(obj, name, "struct tm",
2382                                get ? property_get_tm : NULL, NULL,
2383                                property_release_tm,
2384                                prop);
2385 }
2386 
2387 ObjectProperty *
2388 object_class_property_add_tm(ObjectClass *klass, const char *name,
2389                              void (*get)(Object *, struct tm *, Error **))
2390 {
2391     TMProperty *prop = g_malloc0(sizeof(*prop));
2392 
2393     prop->get = get;
2394 
2395     return object_class_property_add(klass, name, "struct tm",
2396                                      get ? property_get_tm : NULL,
2397                                      NULL, NULL, prop);
2398 }
2399 
2400 static char *object_get_type(Object *obj, Error **errp)
2401 {
2402     return g_strdup(object_get_typename(obj));
2403 }
2404 
2405 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2406                                    void *opaque, Error **errp)
2407 {
2408     uint8_t value = *(uint8_t *)opaque;
2409     visit_type_uint8(v, name, &value, errp);
2410 }
2411 
2412 static void property_set_uint8_ptr(Object *obj, Visitor *v, const char *name,
2413                                    void *opaque, Error **errp)
2414 {
2415     uint8_t *field = opaque;
2416     uint8_t value;
2417 
2418     if (!visit_type_uint8(v, name, &value, errp)) {
2419         return;
2420     }
2421 
2422     *field = value;
2423 }
2424 
2425 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2426                                     void *opaque, Error **errp)
2427 {
2428     uint16_t value = *(uint16_t *)opaque;
2429     visit_type_uint16(v, name, &value, errp);
2430 }
2431 
2432 static void property_set_uint16_ptr(Object *obj, Visitor *v, const char *name,
2433                                     void *opaque, Error **errp)
2434 {
2435     uint16_t *field = opaque;
2436     uint16_t value;
2437 
2438     if (!visit_type_uint16(v, name, &value, errp)) {
2439         return;
2440     }
2441 
2442     *field = value;
2443 }
2444 
2445 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2446                                     void *opaque, Error **errp)
2447 {
2448     uint32_t value = *(uint32_t *)opaque;
2449     visit_type_uint32(v, name, &value, errp);
2450 }
2451 
2452 static void property_set_uint32_ptr(Object *obj, Visitor *v, const char *name,
2453                                     void *opaque, Error **errp)
2454 {
2455     uint32_t *field = opaque;
2456     uint32_t value;
2457 
2458     if (!visit_type_uint32(v, name, &value, errp)) {
2459         return;
2460     }
2461 
2462     *field = value;
2463 }
2464 
2465 static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2466                                     void *opaque, Error **errp)
2467 {
2468     uint64_t value = *(uint64_t *)opaque;
2469     visit_type_uint64(v, name, &value, errp);
2470 }
2471 
2472 static void property_set_uint64_ptr(Object *obj, Visitor *v, const char *name,
2473                                     void *opaque, Error **errp)
2474 {
2475     uint64_t *field = opaque;
2476     uint64_t value;
2477 
2478     if (!visit_type_uint64(v, name, &value, errp)) {
2479         return;
2480     }
2481 
2482     *field = value;
2483 }
2484 
2485 ObjectProperty *
2486 object_property_add_uint8_ptr(Object *obj, const char *name,
2487                               const uint8_t *v,
2488                               ObjectPropertyFlags flags)
2489 {
2490     ObjectPropertyAccessor *getter = NULL;
2491     ObjectPropertyAccessor *setter = NULL;
2492 
2493     if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2494         getter = property_get_uint8_ptr;
2495     }
2496 
2497     if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2498         setter = property_set_uint8_ptr;
2499     }
2500 
2501     return object_property_add(obj, name, "uint8",
2502                                getter, setter, NULL, (void *)v);
2503 }
2504 
2505 ObjectProperty *
2506 object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2507                                     const uint8_t *v,
2508                                     ObjectPropertyFlags flags)
2509 {
2510     ObjectPropertyAccessor *getter = NULL;
2511     ObjectPropertyAccessor *setter = NULL;
2512 
2513     if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2514         getter = property_get_uint8_ptr;
2515     }
2516 
2517     if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2518         setter = property_set_uint8_ptr;
2519     }
2520 
2521     return object_class_property_add(klass, name, "uint8",
2522                                      getter, setter, NULL, (void *)v);
2523 }
2524 
2525 ObjectProperty *
2526 object_property_add_uint16_ptr(Object *obj, const char *name,
2527                                const uint16_t *v,
2528                                ObjectPropertyFlags flags)
2529 {
2530     ObjectPropertyAccessor *getter = NULL;
2531     ObjectPropertyAccessor *setter = NULL;
2532 
2533     if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2534         getter = property_get_uint16_ptr;
2535     }
2536 
2537     if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2538         setter = property_set_uint16_ptr;
2539     }
2540 
2541     return object_property_add(obj, name, "uint16",
2542                                getter, setter, NULL, (void *)v);
2543 }
2544 
2545 ObjectProperty *
2546 object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2547                                      const uint16_t *v,
2548                                      ObjectPropertyFlags flags)
2549 {
2550     ObjectPropertyAccessor *getter = NULL;
2551     ObjectPropertyAccessor *setter = NULL;
2552 
2553     if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2554         getter = property_get_uint16_ptr;
2555     }
2556 
2557     if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2558         setter = property_set_uint16_ptr;
2559     }
2560 
2561     return object_class_property_add(klass, name, "uint16",
2562                                      getter, setter, NULL, (void *)v);
2563 }
2564 
2565 ObjectProperty *
2566 object_property_add_uint32_ptr(Object *obj, const char *name,
2567                                const uint32_t *v,
2568                                ObjectPropertyFlags flags)
2569 {
2570     ObjectPropertyAccessor *getter = NULL;
2571     ObjectPropertyAccessor *setter = NULL;
2572 
2573     if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2574         getter = property_get_uint32_ptr;
2575     }
2576 
2577     if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2578         setter = property_set_uint32_ptr;
2579     }
2580 
2581     return object_property_add(obj, name, "uint32",
2582                                getter, setter, NULL, (void *)v);
2583 }
2584 
2585 ObjectProperty *
2586 object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2587                                      const uint32_t *v,
2588                                      ObjectPropertyFlags flags)
2589 {
2590     ObjectPropertyAccessor *getter = NULL;
2591     ObjectPropertyAccessor *setter = NULL;
2592 
2593     if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2594         getter = property_get_uint32_ptr;
2595     }
2596 
2597     if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2598         setter = property_set_uint32_ptr;
2599     }
2600 
2601     return object_class_property_add(klass, name, "uint32",
2602                                      getter, setter, NULL, (void *)v);
2603 }
2604 
2605 ObjectProperty *
2606 object_property_add_uint64_ptr(Object *obj, const char *name,
2607                                const uint64_t *v,
2608                                ObjectPropertyFlags flags)
2609 {
2610     ObjectPropertyAccessor *getter = NULL;
2611     ObjectPropertyAccessor *setter = NULL;
2612 
2613     if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2614         getter = property_get_uint64_ptr;
2615     }
2616 
2617     if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2618         setter = property_set_uint64_ptr;
2619     }
2620 
2621     return object_property_add(obj, name, "uint64",
2622                                getter, setter, NULL, (void *)v);
2623 }
2624 
2625 ObjectProperty *
2626 object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2627                                      const uint64_t *v,
2628                                      ObjectPropertyFlags flags)
2629 {
2630     ObjectPropertyAccessor *getter = NULL;
2631     ObjectPropertyAccessor *setter = NULL;
2632 
2633     if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2634         getter = property_get_uint64_ptr;
2635     }
2636 
2637     if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2638         setter = property_set_uint64_ptr;
2639     }
2640 
2641     return object_class_property_add(klass, name, "uint64",
2642                                      getter, setter, NULL, (void *)v);
2643 }
2644 
2645 typedef struct {
2646     Object *target_obj;
2647     char *target_name;
2648 } AliasProperty;
2649 
2650 static void property_get_alias(Object *obj, Visitor *v, const char *name,
2651                                void *opaque, Error **errp)
2652 {
2653     AliasProperty *prop = opaque;
2654 
2655     object_property_get(prop->target_obj, prop->target_name, v, errp);
2656 }
2657 
2658 static void property_set_alias(Object *obj, Visitor *v, const char *name,
2659                                void *opaque, Error **errp)
2660 {
2661     AliasProperty *prop = opaque;
2662 
2663     object_property_set(prop->target_obj, prop->target_name, v, errp);
2664 }
2665 
2666 static Object *property_resolve_alias(Object *obj, void *opaque,
2667                                       const char *part)
2668 {
2669     AliasProperty *prop = opaque;
2670 
2671     return object_resolve_path_component(prop->target_obj, prop->target_name);
2672 }
2673 
2674 static void property_release_alias(Object *obj, const char *name, void *opaque)
2675 {
2676     AliasProperty *prop = opaque;
2677 
2678     g_free(prop->target_name);
2679     g_free(prop);
2680 }
2681 
2682 ObjectProperty *
2683 object_property_add_alias(Object *obj, const char *name,
2684                           Object *target_obj, const char *target_name)
2685 {
2686     AliasProperty *prop;
2687     ObjectProperty *op;
2688     ObjectProperty *target_prop;
2689     g_autofree char *prop_type = NULL;
2690 
2691     target_prop = object_property_find(target_obj, target_name,
2692                                        &error_abort);
2693 
2694     if (object_property_is_child(target_prop)) {
2695         prop_type = g_strdup_printf("link%s",
2696                                     target_prop->type + strlen("child"));
2697     } else {
2698         prop_type = g_strdup(target_prop->type);
2699     }
2700 
2701     prop = g_malloc(sizeof(*prop));
2702     prop->target_obj = target_obj;
2703     prop->target_name = g_strdup(target_name);
2704 
2705     op = object_property_add(obj, name, prop_type,
2706                              property_get_alias,
2707                              property_set_alias,
2708                              property_release_alias,
2709                              prop);
2710     op->resolve = property_resolve_alias;
2711     if (target_prop->defval) {
2712         op->defval = qobject_ref(target_prop->defval);
2713     }
2714 
2715     object_property_set_description(obj, op->name,
2716                                     target_prop->description);
2717     return op;
2718 }
2719 
2720 void object_property_set_description(Object *obj, const char *name,
2721                                      const char *description)
2722 {
2723     ObjectProperty *op;
2724 
2725     op = object_property_find(obj, name, &error_abort);
2726     g_free(op->description);
2727     op->description = g_strdup(description);
2728 }
2729 
2730 void object_class_property_set_description(ObjectClass *klass,
2731                                            const char *name,
2732                                            const char *description)
2733 {
2734     ObjectProperty *op;
2735 
2736     op = g_hash_table_lookup(klass->properties, name);
2737     g_free(op->description);
2738     op->description = g_strdup(description);
2739 }
2740 
2741 static void object_class_init(ObjectClass *klass, void *data)
2742 {
2743     object_class_property_add_str(klass, "type", object_get_type,
2744                                   NULL);
2745 }
2746 
2747 static void register_types(void)
2748 {
2749     static TypeInfo interface_info = {
2750         .name = TYPE_INTERFACE,
2751         .class_size = sizeof(InterfaceClass),
2752         .abstract = true,
2753     };
2754 
2755     static TypeInfo object_info = {
2756         .name = TYPE_OBJECT,
2757         .instance_size = sizeof(Object),
2758         .class_init = object_class_init,
2759         .abstract = true,
2760     };
2761 
2762     type_interface = type_register_internal(&interface_info);
2763     type_register_internal(&object_info);
2764 }
2765 
2766 type_init(register_types)
2767