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