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