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