xref: /openbmc/qemu/qom/object.c (revision 845b54efafa5c28040570dcb6d7f8f84d23e37f3)
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 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
266 {
267     assert(target_type);
268 
269     /* Check if target_type is a direct ancestor of type */
270     while (type) {
271         if (type == target_type) {
272             return true;
273         }
274 
275         type = type_get_parent(type);
276     }
277 
278     return false;
279 }
280 
281 static void type_initialize(TypeImpl *ti);
282 
283 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
284                                       TypeImpl *parent_type)
285 {
286     InterfaceClass *new_iface;
287     TypeInfo info = { };
288     TypeImpl *iface_impl;
289 
290     info.parent = parent_type->name;
291     info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
292     info.abstract = true;
293 
294     iface_impl = type_new(&info);
295     iface_impl->parent_type = parent_type;
296     type_initialize(iface_impl);
297     g_free((char *)info.name);
298 
299     new_iface = (InterfaceClass *)iface_impl->class;
300     new_iface->concrete_class = ti->class;
301     new_iface->interface_type = interface_type;
302 
303     ti->class->interfaces = g_slist_append(ti->class->interfaces, new_iface);
304 }
305 
306 static void object_property_free(gpointer data)
307 {
308     ObjectProperty *prop = data;
309 
310     if (prop->defval) {
311         qobject_unref(prop->defval);
312         prop->defval = NULL;
313     }
314     g_free(prop->name);
315     g_free(prop->type);
316     g_free(prop->description);
317     g_free(prop);
318 }
319 
320 static void type_initialize(TypeImpl *ti)
321 {
322     TypeImpl *parent;
323 
324     if (ti->class) {
325         return;
326     }
327 
328     ti->class_size = type_class_get_size(ti);
329     ti->instance_size = type_object_get_size(ti);
330     ti->instance_align = type_object_get_align(ti);
331     /* Any type with zero instance_size is implicitly abstract.
332      * This means interface types are all abstract.
333      */
334     if (ti->instance_size == 0) {
335         ti->abstract = true;
336     }
337     if (type_is_ancestor(ti, type_interface)) {
338         assert(ti->instance_size == 0);
339         assert(ti->abstract);
340         assert(!ti->instance_init);
341         assert(!ti->instance_post_init);
342         assert(!ti->instance_finalize);
343         assert(!ti->num_interfaces);
344     }
345     ti->class = g_malloc0(ti->class_size);
346 
347     parent = type_get_parent(ti);
348     if (parent) {
349         type_initialize(parent);
350         GSList *e;
351         int i;
352 
353         g_assert(parent->class_size <= ti->class_size);
354         g_assert(parent->instance_size <= ti->instance_size);
355         memcpy(ti->class, parent->class, parent->class_size);
356         ti->class->interfaces = NULL;
357 
358         for (e = parent->class->interfaces; e; e = e->next) {
359             InterfaceClass *iface = e->data;
360             ObjectClass *klass = OBJECT_CLASS(iface);
361 
362             type_initialize_interface(ti, iface->interface_type, klass->type);
363         }
364 
365         for (i = 0; i < ti->num_interfaces; i++) {
366             TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
367             if (!t) {
368                 error_report("missing interface '%s' for object '%s'",
369                              ti->interfaces[i].typename, parent->name);
370                 abort();
371             }
372             for (e = ti->class->interfaces; e; e = e->next) {
373                 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
374 
375                 if (type_is_ancestor(target_type, t)) {
376                     break;
377                 }
378             }
379 
380             if (e) {
381                 continue;
382             }
383 
384             type_initialize_interface(ti, t, t);
385         }
386     }
387 
388     ti->class->properties = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
389                                                   object_property_free);
390 
391     ti->class->type = ti;
392 
393     while (parent) {
394         if (parent->class_base_init) {
395             parent->class_base_init(ti->class, ti->class_data);
396         }
397         parent = type_get_parent(parent);
398     }
399 
400     if (ti->class_init) {
401         ti->class_init(ti->class, ti->class_data);
402     }
403 }
404 
405 static void object_init_with_type(Object *obj, TypeImpl *ti)
406 {
407     if (type_has_parent(ti)) {
408         object_init_with_type(obj, type_get_parent(ti));
409     }
410 
411     if (ti->instance_init) {
412         ti->instance_init(obj);
413     }
414 }
415 
416 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
417 {
418     if (ti->instance_post_init) {
419         ti->instance_post_init(obj);
420     }
421 
422     if (type_has_parent(ti)) {
423         object_post_init_with_type(obj, type_get_parent(ti));
424     }
425 }
426 
427 bool object_apply_global_props(Object *obj, const GPtrArray *props,
428                                Error **errp)
429 {
430     int i;
431 
432     if (!props) {
433         return true;
434     }
435 
436     for (i = 0; i < props->len; i++) {
437         GlobalProperty *p = g_ptr_array_index(props, i);
438         Error *err = NULL;
439 
440         if (object_dynamic_cast(obj, p->driver) == NULL) {
441             continue;
442         }
443         if (p->optional && !object_property_find(obj, p->property)) {
444             continue;
445         }
446         p->used = true;
447         if (!object_property_parse(obj, p->property, p->value, &err)) {
448             error_prepend(&err, "can't apply global %s.%s=%s: ",
449                           p->driver, p->property, p->value);
450             /*
451              * If errp != NULL, propagate error and return.
452              * If errp == NULL, report a warning, but keep going
453              * with the remaining globals.
454              */
455             if (errp) {
456                 error_propagate(errp, err);
457                 return false;
458             } else {
459                 warn_report_err(err);
460             }
461         }
462     }
463 
464     return true;
465 }
466 
467 /*
468  * Global property defaults
469  * Slot 0: accelerator's global property defaults
470  * Slot 1: machine's global property defaults
471  * Slot 2: global properties from legacy command line option
472  * Each is a GPtrArray of of GlobalProperty.
473  * Applied in order, later entries override earlier ones.
474  */
475 static GPtrArray *object_compat_props[3];
476 
477 /*
478  * Retrieve @GPtrArray for global property defined with options
479  * other than "-global".  These are generally used for syntactic
480  * sugar and legacy command line options.
481  */
482 void object_register_sugar_prop(const char *driver, const char *prop,
483                                 const char *value, bool optional)
484 {
485     GlobalProperty *g;
486     if (!object_compat_props[2]) {
487         object_compat_props[2] = g_ptr_array_new();
488     }
489     g = g_new0(GlobalProperty, 1);
490     g->driver = g_strdup(driver);
491     g->property = g_strdup(prop);
492     g->value = g_strdup(value);
493     g->optional = optional;
494     g_ptr_array_add(object_compat_props[2], g);
495 }
496 
497 /*
498  * Set machine's global property defaults to @compat_props.
499  * May be called at most once.
500  */
501 void object_set_machine_compat_props(GPtrArray *compat_props)
502 {
503     assert(!object_compat_props[1]);
504     object_compat_props[1] = compat_props;
505 }
506 
507 /*
508  * Set accelerator's global property defaults to @compat_props.
509  * May be called at most once.
510  */
511 void object_set_accelerator_compat_props(GPtrArray *compat_props)
512 {
513     assert(!object_compat_props[0]);
514     object_compat_props[0] = compat_props;
515 }
516 
517 void object_apply_compat_props(Object *obj)
518 {
519     int i;
520 
521     for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
522         object_apply_global_props(obj, object_compat_props[i],
523                                   i == 2 ? &error_fatal : &error_abort);
524     }
525 }
526 
527 static void object_class_property_init_all(Object *obj)
528 {
529     ObjectPropertyIterator iter;
530     ObjectProperty *prop;
531 
532     object_class_property_iter_init(&iter, object_get_class(obj));
533     while ((prop = object_property_iter_next(&iter))) {
534         if (prop->init) {
535             prop->init(obj, prop);
536         }
537     }
538 }
539 
540 static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type)
541 {
542     type_initialize(type);
543 
544     g_assert(type->instance_size >= sizeof(Object));
545     g_assert(type->abstract == false);
546     g_assert(size >= type->instance_size);
547 
548     memset(obj, 0, type->instance_size);
549     obj->class = type->class;
550     object_ref(obj);
551     object_class_property_init_all(obj);
552     obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
553                                             NULL, object_property_free);
554     object_init_with_type(obj, type);
555     object_post_init_with_type(obj, type);
556 }
557 
558 void object_initialize(void *data, size_t size, const char *typename)
559 {
560     TypeImpl *type = type_get_by_name(typename);
561 
562 #ifdef CONFIG_MODULES
563     if (!type) {
564         int rv = module_load_qom(typename, &error_fatal);
565         if (rv > 0) {
566             type = type_get_by_name(typename);
567         } else {
568             error_report("missing object type '%s'", typename);
569             exit(1);
570         }
571     }
572 #endif
573     if (!type) {
574         error_report("missing object type '%s'", typename);
575         abort();
576     }
577 
578     object_initialize_with_type(data, size, type);
579 }
580 
581 bool object_initialize_child_with_props(Object *parentobj,
582                                         const char *propname,
583                                         void *childobj, size_t size,
584                                         const char *type,
585                                         Error **errp, ...)
586 {
587     va_list vargs;
588     bool ok;
589 
590     va_start(vargs, errp);
591     ok = object_initialize_child_with_propsv(parentobj, propname,
592                                              childobj, size, type, errp,
593                                              vargs);
594     va_end(vargs);
595     return ok;
596 }
597 
598 bool object_initialize_child_with_propsv(Object *parentobj,
599                                          const char *propname,
600                                          void *childobj, size_t size,
601                                          const char *type,
602                                          Error **errp, va_list vargs)
603 {
604     bool ok = false;
605     Object *obj;
606     UserCreatable *uc;
607 
608     object_initialize(childobj, size, type);
609     obj = OBJECT(childobj);
610 
611     if (!object_set_propv(obj, errp, vargs)) {
612         goto out;
613     }
614 
615     object_property_add_child(parentobj, propname, obj);
616 
617     uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
618     if (uc) {
619         if (!user_creatable_complete(uc, errp)) {
620             object_unparent(obj);
621             goto out;
622         }
623     }
624 
625     ok = true;
626 
627 out:
628     /*
629      * We want @obj's reference to be 1 on success, 0 on failure.
630      * On success, it's 2: one taken by object_initialize(), and one
631      * by object_property_add_child().
632      * On failure in object_initialize() or earlier, it's 1.
633      * On failure afterwards, it's also 1: object_unparent() releases
634      * the reference taken by object_property_add_child().
635      */
636     object_unref(obj);
637     return ok;
638 }
639 
640 void object_initialize_child_internal(Object *parent,
641                                       const char *propname,
642                                       void *child, size_t size,
643                                       const char *type)
644 {
645     object_initialize_child_with_props(parent, propname, child, size, type,
646                                        &error_abort, NULL);
647 }
648 
649 static inline bool object_property_is_child(ObjectProperty *prop)
650 {
651     return strstart(prop->type, "child<", NULL);
652 }
653 
654 static void object_property_del_all(Object *obj)
655 {
656     g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL);
657     ObjectProperty *prop;
658     ObjectPropertyIterator iter;
659     bool released;
660 
661     do {
662         released = false;
663         object_property_iter_init(&iter, obj);
664         while ((prop = object_property_iter_next(&iter)) != NULL) {
665             if (g_hash_table_add(done, prop)) {
666                 if (prop->release) {
667                     prop->release(obj, prop->name, prop->opaque);
668                     released = true;
669                     break;
670                 }
671             }
672         }
673     } while (released);
674 
675     g_hash_table_unref(obj->properties);
676 }
677 
678 static void object_property_del_child(Object *obj, Object *child)
679 {
680     ObjectProperty *prop;
681     GHashTableIter iter;
682     gpointer key, value;
683 
684     g_hash_table_iter_init(&iter, obj->properties);
685     while (g_hash_table_iter_next(&iter, &key, &value)) {
686         prop = value;
687         if (object_property_is_child(prop) && prop->opaque == child) {
688             if (prop->release) {
689                 prop->release(obj, prop->name, prop->opaque);
690                 prop->release = NULL;
691             }
692             break;
693         }
694     }
695     g_hash_table_iter_init(&iter, obj->properties);
696     while (g_hash_table_iter_next(&iter, &key, &value)) {
697         prop = value;
698         if (object_property_is_child(prop) && prop->opaque == child) {
699             g_hash_table_iter_remove(&iter);
700             break;
701         }
702     }
703 }
704 
705 void object_unparent(Object *obj)
706 {
707     if (obj->parent) {
708         object_property_del_child(obj->parent, obj);
709     }
710 }
711 
712 static void object_deinit(Object *obj, TypeImpl *type)
713 {
714     if (type->instance_finalize) {
715         type->instance_finalize(obj);
716     }
717 
718     if (type_has_parent(type)) {
719         object_deinit(obj, type_get_parent(type));
720     }
721 }
722 
723 static void object_finalize(void *data)
724 {
725     Object *obj = data;
726     TypeImpl *ti = obj->class->type;
727 
728     object_property_del_all(obj);
729     object_deinit(obj, ti);
730 
731     g_assert(obj->ref == 0);
732     g_assert(obj->parent == NULL);
733     if (obj->free) {
734         obj->free(obj);
735     }
736 }
737 
738 /* Find the minimum alignment guaranteed by the system malloc. */
739 #if __STDC_VERSION__ >= 201112L
740 typedef max_align_t qemu_max_align_t;
741 #else
742 typedef union {
743     long l;
744     void *p;
745     double d;
746     long double ld;
747 } qemu_max_align_t;
748 #endif
749 
750 static Object *object_new_with_type(Type type)
751 {
752     Object *obj;
753     size_t size, align;
754     void (*obj_free)(void *);
755 
756     g_assert(type != NULL);
757     type_initialize(type);
758 
759     size = type->instance_size;
760     align = type->instance_align;
761 
762     /*
763      * Do not use qemu_memalign unless required.  Depending on the
764      * implementation, extra alignment implies extra overhead.
765      */
766     if (likely(align <= __alignof__(qemu_max_align_t))) {
767         obj = g_malloc(size);
768         obj_free = g_free;
769     } else {
770         obj = qemu_memalign(align, size);
771         obj_free = qemu_vfree;
772     }
773 
774     object_initialize_with_type(obj, size, type);
775     obj->free = obj_free;
776 
777     return obj;
778 }
779 
780 Object *object_new_with_class(ObjectClass *klass)
781 {
782     return object_new_with_type(klass->type);
783 }
784 
785 Object *object_new(const char *typename)
786 {
787     TypeImpl *ti = type_get_by_name(typename);
788 
789     return object_new_with_type(ti);
790 }
791 
792 
793 Object *object_new_with_props(const char *typename,
794                               Object *parent,
795                               const char *id,
796                               Error **errp,
797                               ...)
798 {
799     va_list vargs;
800     Object *obj;
801 
802     va_start(vargs, errp);
803     obj = object_new_with_propv(typename, parent, id, errp, vargs);
804     va_end(vargs);
805 
806     return obj;
807 }
808 
809 
810 Object *object_new_with_propv(const char *typename,
811                               Object *parent,
812                               const char *id,
813                               Error **errp,
814                               va_list vargs)
815 {
816     Object *obj;
817     ObjectClass *klass;
818     UserCreatable *uc;
819 
820     klass = object_class_by_name(typename);
821     if (!klass) {
822         error_setg(errp, "invalid object type: %s", typename);
823         return NULL;
824     }
825 
826     if (object_class_is_abstract(klass)) {
827         error_setg(errp, "object type '%s' is abstract", typename);
828         return NULL;
829     }
830     obj = object_new_with_type(klass->type);
831 
832     if (!object_set_propv(obj, errp, vargs)) {
833         goto error;
834     }
835 
836     if (id != NULL) {
837         object_property_add_child(parent, id, obj);
838     }
839 
840     uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
841     if (uc) {
842         if (!user_creatable_complete(uc, errp)) {
843             if (id != NULL) {
844                 object_unparent(obj);
845             }
846             goto error;
847         }
848     }
849 
850     object_unref(obj);
851     return obj;
852 
853  error:
854     object_unref(obj);
855     return NULL;
856 }
857 
858 
859 bool object_set_props(Object *obj,
860                      Error **errp,
861                      ...)
862 {
863     va_list vargs;
864     bool ret;
865 
866     va_start(vargs, errp);
867     ret = object_set_propv(obj, errp, vargs);
868     va_end(vargs);
869 
870     return ret;
871 }
872 
873 
874 bool object_set_propv(Object *obj,
875                      Error **errp,
876                      va_list vargs)
877 {
878     const char *propname;
879 
880     propname = va_arg(vargs, char *);
881     while (propname != NULL) {
882         const char *value = va_arg(vargs, char *);
883 
884         g_assert(value != NULL);
885         if (!object_property_parse(obj, propname, value, errp)) {
886             return false;
887         }
888         propname = va_arg(vargs, char *);
889     }
890 
891     return true;
892 }
893 
894 
895 Object *object_dynamic_cast(Object *obj, const char *typename)
896 {
897     if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
898         return obj;
899     }
900 
901     return NULL;
902 }
903 
904 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
905                                    const char *file, int line, const char *func)
906 {
907     trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
908                                      typename, file, line, func);
909 
910 #ifdef CONFIG_QOM_CAST_DEBUG
911     int i;
912     Object *inst;
913 
914     for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
915         if (qatomic_read(&obj->class->object_cast_cache[i]) == typename) {
916             goto out;
917         }
918     }
919 
920     inst = object_dynamic_cast(obj, typename);
921 
922     if (!inst && obj) {
923         fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
924                 file, line, func, obj, typename);
925         abort();
926     }
927 
928     assert(obj == inst);
929 
930     if (obj && obj == inst) {
931         for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
932             qatomic_set(&obj->class->object_cast_cache[i - 1],
933                        qatomic_read(&obj->class->object_cast_cache[i]));
934         }
935         qatomic_set(&obj->class->object_cast_cache[i - 1], typename);
936     }
937 
938 out:
939 #endif
940     return obj;
941 }
942 
943 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
944                                        const char *typename)
945 {
946     ObjectClass *ret = NULL;
947     TypeImpl *target_type;
948     TypeImpl *type;
949 
950     if (!class) {
951         return NULL;
952     }
953 
954     /* A simple fast path that can trigger a lot for leaf classes.  */
955     type = class->type;
956     if (type->name == typename) {
957         return class;
958     }
959 
960     target_type = type_get_by_name(typename);
961     if (!target_type) {
962         /* target class type unknown, so fail the cast */
963         return NULL;
964     }
965 
966     if (type->class->interfaces &&
967             type_is_ancestor(target_type, type_interface)) {
968         int found = 0;
969         GSList *i;
970 
971         for (i = class->interfaces; i; i = i->next) {
972             ObjectClass *target_class = i->data;
973 
974             if (type_is_ancestor(target_class->type, target_type)) {
975                 ret = target_class;
976                 found++;
977             }
978          }
979 
980         /* The match was ambiguous, don't allow a cast */
981         if (found > 1) {
982             ret = NULL;
983         }
984     } else if (type_is_ancestor(type, target_type)) {
985         ret = class;
986     }
987 
988     return ret;
989 }
990 
991 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
992                                               const char *typename,
993                                               const char *file, int line,
994                                               const char *func)
995 {
996     ObjectClass *ret;
997 
998     trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
999                                            typename, file, line, func);
1000 
1001 #ifdef CONFIG_QOM_CAST_DEBUG
1002     int i;
1003 
1004     for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
1005         if (qatomic_read(&class->class_cast_cache[i]) == typename) {
1006             ret = class;
1007             goto out;
1008         }
1009     }
1010 #else
1011     if (!class || !class->interfaces) {
1012         return class;
1013     }
1014 #endif
1015 
1016     ret = object_class_dynamic_cast(class, typename);
1017     if (!ret && class) {
1018         fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
1019                 file, line, func, class, typename);
1020         abort();
1021     }
1022 
1023 #ifdef CONFIG_QOM_CAST_DEBUG
1024     if (class && ret == class) {
1025         for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
1026             qatomic_set(&class->class_cast_cache[i - 1],
1027                        qatomic_read(&class->class_cast_cache[i]));
1028         }
1029         qatomic_set(&class->class_cast_cache[i - 1], typename);
1030     }
1031 out:
1032 #endif
1033     return ret;
1034 }
1035 
1036 const char *object_get_typename(const Object *obj)
1037 {
1038     return obj->class->type->name;
1039 }
1040 
1041 ObjectClass *object_get_class(Object *obj)
1042 {
1043     return obj->class;
1044 }
1045 
1046 bool object_class_is_abstract(ObjectClass *klass)
1047 {
1048     return klass->type->abstract;
1049 }
1050 
1051 const char *object_class_get_name(ObjectClass *klass)
1052 {
1053     return klass->type->name;
1054 }
1055 
1056 ObjectClass *object_class_by_name(const char *typename)
1057 {
1058     TypeImpl *type = type_get_by_name(typename);
1059 
1060     if (!type) {
1061         return NULL;
1062     }
1063 
1064     type_initialize(type);
1065 
1066     return type->class;
1067 }
1068 
1069 ObjectClass *module_object_class_by_name(const char *typename)
1070 {
1071     ObjectClass *oc;
1072 
1073     oc = object_class_by_name(typename);
1074 #ifdef CONFIG_MODULES
1075     if (!oc) {
1076         Error *local_err = NULL;
1077         int rv = module_load_qom(typename, &local_err);
1078         if (rv > 0) {
1079             oc = object_class_by_name(typename);
1080         } else if (rv < 0) {
1081             error_report_err(local_err);
1082         }
1083     }
1084 #endif
1085     return oc;
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