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