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