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