xref: /openbmc/qemu/qom/object.c (revision a42e9c41)
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 "qom/object.h"
14 #include "qemu-common.h"
15 #include "qapi/visitor.h"
16 #include "qapi/string-input-visitor.h"
17 #include "qapi/string-output-visitor.h"
18 #include "qapi/qmp/qerror.h"
19 #include "trace.h"
20 
21 /* TODO: replace QObject with a simpler visitor to avoid a dependency
22  * of the QOM core on QObject?  */
23 #include "qom/qom-qobject.h"
24 #include "qapi/qmp/qobject.h"
25 #include "qapi/qmp/qbool.h"
26 #include "qapi/qmp/qint.h"
27 #include "qapi/qmp/qstring.h"
28 
29 #define MAX_INTERFACES 32
30 
31 typedef struct InterfaceImpl InterfaceImpl;
32 typedef struct TypeImpl TypeImpl;
33 
34 struct InterfaceImpl
35 {
36     const char *typename;
37 };
38 
39 struct TypeImpl
40 {
41     const char *name;
42 
43     size_t class_size;
44 
45     size_t instance_size;
46 
47     void (*class_init)(ObjectClass *klass, void *data);
48     void (*class_base_init)(ObjectClass *klass, void *data);
49     void (*class_finalize)(ObjectClass *klass, void *data);
50 
51     void *class_data;
52 
53     void (*instance_init)(Object *obj);
54     void (*instance_post_init)(Object *obj);
55     void (*instance_finalize)(Object *obj);
56 
57     bool abstract;
58 
59     const char *parent;
60     TypeImpl *parent_type;
61 
62     ObjectClass *class;
63 
64     int num_interfaces;
65     InterfaceImpl interfaces[MAX_INTERFACES];
66 };
67 
68 static Type type_interface;
69 
70 static GHashTable *type_table_get(void)
71 {
72     static GHashTable *type_table;
73 
74     if (type_table == NULL) {
75         type_table = g_hash_table_new(g_str_hash, g_str_equal);
76     }
77 
78     return type_table;
79 }
80 
81 static void type_table_add(TypeImpl *ti)
82 {
83     g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
84 }
85 
86 static TypeImpl *type_table_lookup(const char *name)
87 {
88     return g_hash_table_lookup(type_table_get(), name);
89 }
90 
91 static TypeImpl *type_register_internal(const TypeInfo *info)
92 {
93     TypeImpl *ti = g_malloc0(sizeof(*ti));
94     int i;
95 
96     g_assert(info->name != NULL);
97 
98     if (type_table_lookup(info->name) != NULL) {
99         fprintf(stderr, "Registering `%s' which already exists\n", info->name);
100         abort();
101     }
102 
103     ti->name = g_strdup(info->name);
104     ti->parent = g_strdup(info->parent);
105 
106     ti->class_size = info->class_size;
107     ti->instance_size = info->instance_size;
108 
109     ti->class_init = info->class_init;
110     ti->class_base_init = info->class_base_init;
111     ti->class_finalize = info->class_finalize;
112     ti->class_data = info->class_data;
113 
114     ti->instance_init = info->instance_init;
115     ti->instance_post_init = info->instance_post_init;
116     ti->instance_finalize = info->instance_finalize;
117 
118     ti->abstract = info->abstract;
119 
120     for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
121         ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
122     }
123     ti->num_interfaces = i;
124 
125     type_table_add(ti);
126 
127     return ti;
128 }
129 
130 TypeImpl *type_register(const TypeInfo *info)
131 {
132     assert(info->parent);
133     return type_register_internal(info);
134 }
135 
136 TypeImpl *type_register_static(const TypeInfo *info)
137 {
138     return type_register(info);
139 }
140 
141 static TypeImpl *type_get_by_name(const char *name)
142 {
143     if (name == NULL) {
144         return NULL;
145     }
146 
147     return type_table_lookup(name);
148 }
149 
150 static TypeImpl *type_get_parent(TypeImpl *type)
151 {
152     if (!type->parent_type && type->parent) {
153         type->parent_type = type_get_by_name(type->parent);
154         g_assert(type->parent_type != NULL);
155     }
156 
157     return type->parent_type;
158 }
159 
160 static bool type_has_parent(TypeImpl *type)
161 {
162     return (type->parent != NULL);
163 }
164 
165 static size_t type_class_get_size(TypeImpl *ti)
166 {
167     if (ti->class_size) {
168         return ti->class_size;
169     }
170 
171     if (type_has_parent(ti)) {
172         return type_class_get_size(type_get_parent(ti));
173     }
174 
175     return sizeof(ObjectClass);
176 }
177 
178 static size_t type_object_get_size(TypeImpl *ti)
179 {
180     if (ti->instance_size) {
181         return ti->instance_size;
182     }
183 
184     if (type_has_parent(ti)) {
185         return type_object_get_size(type_get_parent(ti));
186     }
187 
188     return 0;
189 }
190 
191 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
192 {
193     assert(target_type);
194 
195     /* Check if typename is a direct ancestor of type */
196     while (type) {
197         if (type == target_type) {
198             return true;
199         }
200 
201         type = type_get_parent(type);
202     }
203 
204     return false;
205 }
206 
207 static void type_initialize(TypeImpl *ti);
208 
209 static void type_initialize_interface(TypeImpl *ti, const char *parent)
210 {
211     InterfaceClass *new_iface;
212     TypeInfo info = { };
213     TypeImpl *iface_impl;
214 
215     info.parent = parent;
216     info.name = g_strdup_printf("%s::%s", ti->name, info.parent);
217     info.abstract = true;
218 
219     iface_impl = type_register(&info);
220     type_initialize(iface_impl);
221     g_free((char *)info.name);
222 
223     new_iface = (InterfaceClass *)iface_impl->class;
224     new_iface->concrete_class = ti->class;
225 
226     ti->class->interfaces = g_slist_append(ti->class->interfaces,
227                                            iface_impl->class);
228 }
229 
230 static void type_initialize(TypeImpl *ti)
231 {
232     TypeImpl *parent;
233 
234     if (ti->class) {
235         return;
236     }
237 
238     ti->class_size = type_class_get_size(ti);
239     ti->instance_size = type_object_get_size(ti);
240 
241     ti->class = g_malloc0(ti->class_size);
242 
243     parent = type_get_parent(ti);
244     if (parent) {
245         type_initialize(parent);
246         GSList *e;
247         int i;
248 
249         g_assert(parent->class_size <= ti->class_size);
250         memcpy(ti->class, parent->class, parent->class_size);
251         ti->class->interfaces = NULL;
252 
253         for (e = parent->class->interfaces; e; e = e->next) {
254             ObjectClass *iface = e->data;
255             type_initialize_interface(ti, object_class_get_name(iface));
256         }
257 
258         for (i = 0; i < ti->num_interfaces; i++) {
259             TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
260             for (e = ti->class->interfaces; e; e = e->next) {
261                 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
262 
263                 if (type_is_ancestor(target_type, t)) {
264                     break;
265                 }
266             }
267 
268             if (e) {
269                 continue;
270             }
271 
272             type_initialize_interface(ti, ti->interfaces[i].typename);
273         }
274     }
275 
276     ti->class->type = ti;
277 
278     while (parent) {
279         if (parent->class_base_init) {
280             parent->class_base_init(ti->class, ti->class_data);
281         }
282         parent = type_get_parent(parent);
283     }
284 
285     if (ti->class_init) {
286         ti->class_init(ti->class, ti->class_data);
287     }
288 
289 
290 }
291 
292 static void object_init_with_type(Object *obj, TypeImpl *ti)
293 {
294     if (type_has_parent(ti)) {
295         object_init_with_type(obj, type_get_parent(ti));
296     }
297 
298     if (ti->instance_init) {
299         ti->instance_init(obj);
300     }
301 }
302 
303 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
304 {
305     if (ti->instance_post_init) {
306         ti->instance_post_init(obj);
307     }
308 
309     if (type_has_parent(ti)) {
310         object_post_init_with_type(obj, type_get_parent(ti));
311     }
312 }
313 
314 void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
315 {
316     Object *obj = data;
317 
318     g_assert(type != NULL);
319     type_initialize(type);
320 
321     g_assert(type->instance_size >= sizeof(Object));
322     g_assert(type->abstract == false);
323     g_assert(size >= type->instance_size);
324 
325     memset(obj, 0, type->instance_size);
326     obj->class = type->class;
327     object_ref(obj);
328     QTAILQ_INIT(&obj->properties);
329     object_init_with_type(obj, type);
330     object_post_init_with_type(obj, type);
331 }
332 
333 void object_initialize(void *data, size_t size, const char *typename)
334 {
335     TypeImpl *type = type_get_by_name(typename);
336 
337     object_initialize_with_type(data, size, type);
338 }
339 
340 static inline bool object_property_is_child(ObjectProperty *prop)
341 {
342     return strstart(prop->type, "child<", NULL);
343 }
344 
345 static inline bool object_property_is_link(ObjectProperty *prop)
346 {
347     return strstart(prop->type, "link<", NULL);
348 }
349 
350 static void object_property_del_all(Object *obj)
351 {
352     while (!QTAILQ_EMPTY(&obj->properties)) {
353         ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
354 
355         QTAILQ_REMOVE(&obj->properties, prop, node);
356 
357         if (prop->release) {
358             prop->release(obj, prop->name, prop->opaque);
359         }
360 
361         g_free(prop->name);
362         g_free(prop->type);
363         g_free(prop);
364     }
365 }
366 
367 static void object_property_del_child(Object *obj, Object *child, Error **errp)
368 {
369     ObjectProperty *prop;
370 
371     QTAILQ_FOREACH(prop, &obj->properties, node) {
372         if (object_property_is_child(prop) && prop->opaque == child) {
373             object_property_del(obj, prop->name, errp);
374             break;
375         }
376     }
377 }
378 
379 void object_unparent(Object *obj)
380 {
381     if (!obj->parent) {
382         return;
383     }
384 
385     object_ref(obj);
386     if (obj->class->unparent) {
387         (obj->class->unparent)(obj);
388     }
389     if (obj->parent) {
390         object_property_del_child(obj->parent, obj, NULL);
391     }
392     object_unref(obj);
393 }
394 
395 static void object_deinit(Object *obj, TypeImpl *type)
396 {
397     if (type->instance_finalize) {
398         type->instance_finalize(obj);
399     }
400 
401     if (type_has_parent(type)) {
402         object_deinit(obj, type_get_parent(type));
403     }
404 }
405 
406 static void object_finalize(void *data)
407 {
408     Object *obj = data;
409     TypeImpl *ti = obj->class->type;
410 
411     object_deinit(obj, ti);
412     object_property_del_all(obj);
413 
414     g_assert(obj->ref == 0);
415     if (obj->free) {
416         obj->free(obj);
417     }
418 }
419 
420 Object *object_new_with_type(Type type)
421 {
422     Object *obj;
423 
424     g_assert(type != NULL);
425     type_initialize(type);
426 
427     obj = g_malloc(type->instance_size);
428     object_initialize_with_type(obj, type->instance_size, type);
429     obj->free = g_free;
430 
431     return obj;
432 }
433 
434 Object *object_new(const char *typename)
435 {
436     TypeImpl *ti = type_get_by_name(typename);
437 
438     return object_new_with_type(ti);
439 }
440 
441 Object *object_dynamic_cast(Object *obj, const char *typename)
442 {
443     if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
444         return obj;
445     }
446 
447     return NULL;
448 }
449 
450 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
451                                    const char *file, int line, const char *func)
452 {
453     trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
454                                      typename, file, line, func);
455 
456 #ifdef CONFIG_QOM_CAST_DEBUG
457     int i;
458     Object *inst;
459 
460     for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
461         if (obj->class->cast_cache[i] == typename) {
462             goto out;
463         }
464     }
465 
466     inst = object_dynamic_cast(obj, typename);
467 
468     if (!inst && obj) {
469         fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
470                 file, line, func, obj, typename);
471         abort();
472     }
473 
474     assert(obj == inst);
475 
476     if (obj && obj == inst) {
477         for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
478             obj->class->cast_cache[i - 1] = obj->class->cast_cache[i];
479         }
480         obj->class->cast_cache[i - 1] = typename;
481     }
482 
483 out:
484 #endif
485     return obj;
486 }
487 
488 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
489                                        const char *typename)
490 {
491     ObjectClass *ret = NULL;
492     TypeImpl *target_type;
493     TypeImpl *type;
494 
495     if (!class) {
496         return NULL;
497     }
498 
499     /* A simple fast path that can trigger a lot for leaf classes.  */
500     type = class->type;
501     if (type->name == typename) {
502         return class;
503     }
504 
505     target_type = type_get_by_name(typename);
506     if (!target_type) {
507         /* target class type unknown, so fail the cast */
508         return NULL;
509     }
510 
511     if (type->class->interfaces &&
512             type_is_ancestor(target_type, type_interface)) {
513         int found = 0;
514         GSList *i;
515 
516         for (i = class->interfaces; i; i = i->next) {
517             ObjectClass *target_class = i->data;
518 
519             if (type_is_ancestor(target_class->type, target_type)) {
520                 ret = target_class;
521                 found++;
522             }
523          }
524 
525         /* The match was ambiguous, don't allow a cast */
526         if (found > 1) {
527             ret = NULL;
528         }
529     } else if (type_is_ancestor(type, target_type)) {
530         ret = class;
531     }
532 
533     return ret;
534 }
535 
536 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
537                                               const char *typename,
538                                               const char *file, int line,
539                                               const char *func)
540 {
541     ObjectClass *ret;
542 
543     trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
544                                            typename, file, line, func);
545 
546 #ifdef CONFIG_QOM_CAST_DEBUG
547     int i;
548 
549     for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
550         if (class->cast_cache[i] == typename) {
551             ret = class;
552             goto out;
553         }
554     }
555 #else
556     if (!class || !class->interfaces) {
557         return class;
558     }
559 #endif
560 
561     ret = object_class_dynamic_cast(class, typename);
562     if (!ret && class) {
563         fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
564                 file, line, func, class, typename);
565         abort();
566     }
567 
568 #ifdef CONFIG_QOM_CAST_DEBUG
569     if (class && ret == class) {
570         for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
571             class->cast_cache[i - 1] = class->cast_cache[i];
572         }
573         class->cast_cache[i - 1] = typename;
574     }
575 out:
576 #endif
577     return ret;
578 }
579 
580 const char *object_get_typename(Object *obj)
581 {
582     return obj->class->type->name;
583 }
584 
585 ObjectClass *object_get_class(Object *obj)
586 {
587     return obj->class;
588 }
589 
590 bool object_class_is_abstract(ObjectClass *klass)
591 {
592     return klass->type->abstract;
593 }
594 
595 const char *object_class_get_name(ObjectClass *klass)
596 {
597     return klass->type->name;
598 }
599 
600 ObjectClass *object_class_by_name(const char *typename)
601 {
602     TypeImpl *type = type_get_by_name(typename);
603 
604     if (!type) {
605         return NULL;
606     }
607 
608     type_initialize(type);
609 
610     return type->class;
611 }
612 
613 ObjectClass *object_class_get_parent(ObjectClass *class)
614 {
615     TypeImpl *type = type_get_parent(class->type);
616 
617     if (!type) {
618         return NULL;
619     }
620 
621     type_initialize(type);
622 
623     return type->class;
624 }
625 
626 typedef struct OCFData
627 {
628     void (*fn)(ObjectClass *klass, void *opaque);
629     const char *implements_type;
630     bool include_abstract;
631     void *opaque;
632 } OCFData;
633 
634 static void object_class_foreach_tramp(gpointer key, gpointer value,
635                                        gpointer opaque)
636 {
637     OCFData *data = opaque;
638     TypeImpl *type = value;
639     ObjectClass *k;
640 
641     type_initialize(type);
642     k = type->class;
643 
644     if (!data->include_abstract && type->abstract) {
645         return;
646     }
647 
648     if (data->implements_type &&
649         !object_class_dynamic_cast(k, data->implements_type)) {
650         return;
651     }
652 
653     data->fn(k, data->opaque);
654 }
655 
656 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
657                           const char *implements_type, bool include_abstract,
658                           void *opaque)
659 {
660     OCFData data = { fn, implements_type, include_abstract, opaque };
661 
662     g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
663 }
664 
665 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
666                          void *opaque)
667 {
668     ObjectProperty *prop;
669     int ret = 0;
670 
671     QTAILQ_FOREACH(prop, &obj->properties, node) {
672         if (object_property_is_child(prop)) {
673             ret = fn(prop->opaque, opaque);
674             if (ret != 0) {
675                 break;
676             }
677         }
678     }
679     return ret;
680 }
681 
682 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
683 {
684     GSList **list = opaque;
685 
686     *list = g_slist_prepend(*list, klass);
687 }
688 
689 GSList *object_class_get_list(const char *implements_type,
690                               bool include_abstract)
691 {
692     GSList *list = NULL;
693 
694     object_class_foreach(object_class_get_list_tramp,
695                          implements_type, include_abstract, &list);
696     return list;
697 }
698 
699 void object_ref(Object *obj)
700 {
701      atomic_inc(&obj->ref);
702 }
703 
704 void object_unref(Object *obj)
705 {
706     g_assert(obj->ref > 0);
707 
708     /* parent always holds a reference to its children */
709     if (atomic_fetch_dec(&obj->ref) == 1) {
710         object_finalize(obj);
711     }
712 }
713 
714 void object_property_add(Object *obj, const char *name, const char *type,
715                          ObjectPropertyAccessor *get,
716                          ObjectPropertyAccessor *set,
717                          ObjectPropertyRelease *release,
718                          void *opaque, Error **errp)
719 {
720     ObjectProperty *prop;
721 
722     QTAILQ_FOREACH(prop, &obj->properties, node) {
723         if (strcmp(prop->name, name) == 0) {
724             error_setg(errp, "attempt to add duplicate property '%s'"
725                        " to object (type '%s')", name,
726                        object_get_typename(obj));
727             return;
728         }
729     }
730 
731     prop = g_malloc0(sizeof(*prop));
732 
733     prop->name = g_strdup(name);
734     prop->type = g_strdup(type);
735 
736     prop->get = get;
737     prop->set = set;
738     prop->release = release;
739     prop->opaque = opaque;
740 
741     QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
742 }
743 
744 ObjectProperty *object_property_find(Object *obj, const char *name,
745                                      Error **errp)
746 {
747     ObjectProperty *prop;
748 
749     QTAILQ_FOREACH(prop, &obj->properties, node) {
750         if (strcmp(prop->name, name) == 0) {
751             return prop;
752         }
753     }
754 
755     error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
756     return NULL;
757 }
758 
759 void object_property_del(Object *obj, const char *name, Error **errp)
760 {
761     ObjectProperty *prop = object_property_find(obj, name, errp);
762     if (prop == NULL) {
763         return;
764     }
765 
766     if (prop->release) {
767         prop->release(obj, name, prop->opaque);
768     }
769 
770     QTAILQ_REMOVE(&obj->properties, prop, node);
771 
772     g_free(prop->name);
773     g_free(prop->type);
774     g_free(prop);
775 }
776 
777 void object_property_get(Object *obj, Visitor *v, const char *name,
778                          Error **errp)
779 {
780     ObjectProperty *prop = object_property_find(obj, name, errp);
781     if (prop == NULL) {
782         return;
783     }
784 
785     if (!prop->get) {
786         error_set(errp, QERR_PERMISSION_DENIED);
787     } else {
788         prop->get(obj, v, prop->opaque, name, errp);
789     }
790 }
791 
792 void object_property_set(Object *obj, Visitor *v, const char *name,
793                          Error **errp)
794 {
795     ObjectProperty *prop = object_property_find(obj, name, errp);
796     if (prop == NULL) {
797         return;
798     }
799 
800     if (!prop->set) {
801         error_set(errp, QERR_PERMISSION_DENIED);
802     } else {
803         prop->set(obj, v, prop->opaque, name, errp);
804     }
805 }
806 
807 void object_property_set_str(Object *obj, const char *value,
808                              const char *name, Error **errp)
809 {
810     QString *qstr = qstring_from_str(value);
811     object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
812 
813     QDECREF(qstr);
814 }
815 
816 char *object_property_get_str(Object *obj, const char *name,
817                               Error **errp)
818 {
819     QObject *ret = object_property_get_qobject(obj, name, errp);
820     QString *qstring;
821     char *retval;
822 
823     if (!ret) {
824         return NULL;
825     }
826     qstring = qobject_to_qstring(ret);
827     if (!qstring) {
828         error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
829         retval = NULL;
830     } else {
831         retval = g_strdup(qstring_get_str(qstring));
832     }
833 
834     QDECREF(qstring);
835     return retval;
836 }
837 
838 void object_property_set_link(Object *obj, Object *value,
839                               const char *name, Error **errp)
840 {
841     object_property_set_str(obj, object_get_canonical_path(value),
842                             name, errp);
843 }
844 
845 Object *object_property_get_link(Object *obj, const char *name,
846                                  Error **errp)
847 {
848     char *str = object_property_get_str(obj, name, errp);
849     Object *target = NULL;
850 
851     if (str && *str) {
852         target = object_resolve_path(str, NULL);
853         if (!target) {
854             error_set(errp, QERR_DEVICE_NOT_FOUND, str);
855         }
856     }
857 
858     g_free(str);
859     return target;
860 }
861 
862 void object_property_set_bool(Object *obj, bool value,
863                               const char *name, Error **errp)
864 {
865     QBool *qbool = qbool_from_int(value);
866     object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
867 
868     QDECREF(qbool);
869 }
870 
871 bool object_property_get_bool(Object *obj, const char *name,
872                               Error **errp)
873 {
874     QObject *ret = object_property_get_qobject(obj, name, errp);
875     QBool *qbool;
876     bool retval;
877 
878     if (!ret) {
879         return false;
880     }
881     qbool = qobject_to_qbool(ret);
882     if (!qbool) {
883         error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
884         retval = false;
885     } else {
886         retval = qbool_get_int(qbool);
887     }
888 
889     QDECREF(qbool);
890     return retval;
891 }
892 
893 void object_property_set_int(Object *obj, int64_t value,
894                              const char *name, Error **errp)
895 {
896     QInt *qint = qint_from_int(value);
897     object_property_set_qobject(obj, QOBJECT(qint), name, errp);
898 
899     QDECREF(qint);
900 }
901 
902 int64_t object_property_get_int(Object *obj, const char *name,
903                                 Error **errp)
904 {
905     QObject *ret = object_property_get_qobject(obj, name, errp);
906     QInt *qint;
907     int64_t retval;
908 
909     if (!ret) {
910         return -1;
911     }
912     qint = qobject_to_qint(ret);
913     if (!qint) {
914         error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
915         retval = -1;
916     } else {
917         retval = qint_get_int(qint);
918     }
919 
920     QDECREF(qint);
921     return retval;
922 }
923 
924 void object_property_parse(Object *obj, const char *string,
925                            const char *name, Error **errp)
926 {
927     StringInputVisitor *mi;
928     mi = string_input_visitor_new(string);
929     object_property_set(obj, string_input_get_visitor(mi), name, errp);
930 
931     string_input_visitor_cleanup(mi);
932 }
933 
934 char *object_property_print(Object *obj, const char *name,
935                             Error **errp)
936 {
937     StringOutputVisitor *mo;
938     char *string;
939 
940     mo = string_output_visitor_new();
941     object_property_get(obj, string_output_get_visitor(mo), name, errp);
942     string = string_output_get_string(mo);
943     string_output_visitor_cleanup(mo);
944     return string;
945 }
946 
947 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
948 {
949     ObjectProperty *prop = object_property_find(obj, name, errp);
950     if (prop == NULL) {
951         return NULL;
952     }
953 
954     return prop->type;
955 }
956 
957 Object *object_get_root(void)
958 {
959     static Object *root;
960 
961     if (!root) {
962         root = object_new("container");
963     }
964 
965     return root;
966 }
967 
968 static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
969                                       const char *name, Error **errp)
970 {
971     Object *child = opaque;
972     gchar *path;
973 
974     path = object_get_canonical_path(child);
975     visit_type_str(v, &path, name, errp);
976     g_free(path);
977 }
978 
979 static void object_finalize_child_property(Object *obj, const char *name,
980                                            void *opaque)
981 {
982     Object *child = opaque;
983 
984     object_unref(child);
985 }
986 
987 void object_property_add_child(Object *obj, const char *name,
988                                Object *child, Error **errp)
989 {
990     gchar *type;
991 
992     type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
993 
994     object_property_add(obj, name, type, object_get_child_property,
995                         NULL, object_finalize_child_property, child, errp);
996 
997     object_ref(child);
998     g_assert(child->parent == NULL);
999     child->parent = obj;
1000 
1001     g_free(type);
1002 }
1003 
1004 static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
1005                                      const char *name, Error **errp)
1006 {
1007     Object **child = opaque;
1008     gchar *path;
1009 
1010     if (*child) {
1011         path = object_get_canonical_path(*child);
1012         visit_type_str(v, &path, name, errp);
1013         g_free(path);
1014     } else {
1015         path = (gchar *)"";
1016         visit_type_str(v, &path, name, errp);
1017     }
1018 }
1019 
1020 static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
1021                                      const char *name, Error **errp)
1022 {
1023     Object **child = opaque;
1024     Object *old_target;
1025     bool ambiguous = false;
1026     const char *type;
1027     char *path;
1028     gchar *target_type;
1029 
1030     type = object_property_get_type(obj, name, NULL);
1031 
1032     visit_type_str(v, &path, name, errp);
1033 
1034     old_target = *child;
1035     *child = NULL;
1036 
1037     if (strcmp(path, "") != 0) {
1038         Object *target;
1039 
1040         /* Go from link<FOO> to FOO.  */
1041         target_type = g_strndup(&type[5], strlen(type) - 6);
1042         target = object_resolve_path_type(path, target_type, &ambiguous);
1043 
1044         if (ambiguous) {
1045             error_set(errp, QERR_AMBIGUOUS_PATH, path);
1046         } else if (target) {
1047             object_ref(target);
1048             *child = target;
1049         } else {
1050             target = object_resolve_path(path, &ambiguous);
1051             if (target || ambiguous) {
1052                 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1053             } else {
1054                 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
1055             }
1056         }
1057         g_free(target_type);
1058     }
1059 
1060     g_free(path);
1061 
1062     if (old_target != NULL) {
1063         object_unref(old_target);
1064     }
1065 }
1066 
1067 void object_property_add_link(Object *obj, const char *name,
1068                               const char *type, Object **child,
1069                               Error **errp)
1070 {
1071     gchar *full_type;
1072 
1073     full_type = g_strdup_printf("link<%s>", type);
1074 
1075     object_property_add(obj, name, full_type,
1076                         object_get_link_property,
1077                         object_set_link_property,
1078                         NULL, child, errp);
1079 
1080     g_free(full_type);
1081 }
1082 
1083 gchar *object_get_canonical_path(Object *obj)
1084 {
1085     Object *root = object_get_root();
1086     char *newpath = NULL, *path = NULL;
1087 
1088     while (obj != root) {
1089         ObjectProperty *prop = NULL;
1090 
1091         g_assert(obj->parent != NULL);
1092 
1093         QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
1094             if (!object_property_is_child(prop)) {
1095                 continue;
1096             }
1097 
1098             if (prop->opaque == obj) {
1099                 if (path) {
1100                     newpath = g_strdup_printf("%s/%s", prop->name, path);
1101                     g_free(path);
1102                     path = newpath;
1103                 } else {
1104                     path = g_strdup(prop->name);
1105                 }
1106                 break;
1107             }
1108         }
1109 
1110         g_assert(prop != NULL);
1111 
1112         obj = obj->parent;
1113     }
1114 
1115     newpath = g_strdup_printf("/%s", path);
1116     g_free(path);
1117 
1118     return newpath;
1119 }
1120 
1121 Object *object_resolve_path_component(Object *parent, const gchar *part)
1122 {
1123     ObjectProperty *prop = object_property_find(parent, part, NULL);
1124     if (prop == NULL) {
1125         return NULL;
1126     }
1127 
1128     if (object_property_is_link(prop)) {
1129         return *(Object **)prop->opaque;
1130     } else if (object_property_is_child(prop)) {
1131         return prop->opaque;
1132     } else {
1133         return NULL;
1134     }
1135 }
1136 
1137 static Object *object_resolve_abs_path(Object *parent,
1138                                           gchar **parts,
1139                                           const char *typename,
1140                                           int index)
1141 {
1142     Object *child;
1143 
1144     if (parts[index] == NULL) {
1145         return object_dynamic_cast(parent, typename);
1146     }
1147 
1148     if (strcmp(parts[index], "") == 0) {
1149         return object_resolve_abs_path(parent, parts, typename, index + 1);
1150     }
1151 
1152     child = object_resolve_path_component(parent, parts[index]);
1153     if (!child) {
1154         return NULL;
1155     }
1156 
1157     return object_resolve_abs_path(child, parts, typename, index + 1);
1158 }
1159 
1160 static Object *object_resolve_partial_path(Object *parent,
1161                                               gchar **parts,
1162                                               const char *typename,
1163                                               bool *ambiguous)
1164 {
1165     Object *obj;
1166     ObjectProperty *prop;
1167 
1168     obj = object_resolve_abs_path(parent, parts, typename, 0);
1169 
1170     QTAILQ_FOREACH(prop, &parent->properties, node) {
1171         Object *found;
1172 
1173         if (!object_property_is_child(prop)) {
1174             continue;
1175         }
1176 
1177         found = object_resolve_partial_path(prop->opaque, parts,
1178                                             typename, ambiguous);
1179         if (found) {
1180             if (obj) {
1181                 if (ambiguous) {
1182                     *ambiguous = true;
1183                 }
1184                 return NULL;
1185             }
1186             obj = found;
1187         }
1188 
1189         if (ambiguous && *ambiguous) {
1190             return NULL;
1191         }
1192     }
1193 
1194     return obj;
1195 }
1196 
1197 Object *object_resolve_path_type(const char *path, const char *typename,
1198                                  bool *ambiguous)
1199 {
1200     Object *obj;
1201     gchar **parts;
1202 
1203     parts = g_strsplit(path, "/", 0);
1204     assert(parts);
1205 
1206     if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
1207         if (ambiguous) {
1208             *ambiguous = false;
1209         }
1210         obj = object_resolve_partial_path(object_get_root(), parts,
1211                                           typename, ambiguous);
1212     } else {
1213         obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
1214     }
1215 
1216     g_strfreev(parts);
1217 
1218     return obj;
1219 }
1220 
1221 Object *object_resolve_path(const char *path, bool *ambiguous)
1222 {
1223     return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1224 }
1225 
1226 typedef struct StringProperty
1227 {
1228     char *(*get)(Object *, Error **);
1229     void (*set)(Object *, const char *, Error **);
1230 } StringProperty;
1231 
1232 static void property_get_str(Object *obj, Visitor *v, void *opaque,
1233                              const char *name, Error **errp)
1234 {
1235     StringProperty *prop = opaque;
1236     char *value;
1237 
1238     value = prop->get(obj, errp);
1239     if (value) {
1240         visit_type_str(v, &value, name, errp);
1241         g_free(value);
1242     }
1243 }
1244 
1245 static void property_set_str(Object *obj, Visitor *v, void *opaque,
1246                              const char *name, Error **errp)
1247 {
1248     StringProperty *prop = opaque;
1249     char *value;
1250     Error *local_err = NULL;
1251 
1252     visit_type_str(v, &value, name, &local_err);
1253     if (local_err) {
1254         error_propagate(errp, local_err);
1255         return;
1256     }
1257 
1258     prop->set(obj, value, errp);
1259     g_free(value);
1260 }
1261 
1262 static void property_release_str(Object *obj, const char *name,
1263                                  void *opaque)
1264 {
1265     StringProperty *prop = opaque;
1266     g_free(prop);
1267 }
1268 
1269 void object_property_add_str(Object *obj, const char *name,
1270                            char *(*get)(Object *, Error **),
1271                            void (*set)(Object *, const char *, Error **),
1272                            Error **errp)
1273 {
1274     StringProperty *prop = g_malloc0(sizeof(*prop));
1275 
1276     prop->get = get;
1277     prop->set = set;
1278 
1279     object_property_add(obj, name, "string",
1280                         get ? property_get_str : NULL,
1281                         set ? property_set_str : NULL,
1282                         property_release_str,
1283                         prop, errp);
1284 }
1285 
1286 typedef struct BoolProperty
1287 {
1288     bool (*get)(Object *, Error **);
1289     void (*set)(Object *, bool, Error **);
1290 } BoolProperty;
1291 
1292 static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1293                               const char *name, Error **errp)
1294 {
1295     BoolProperty *prop = opaque;
1296     bool value;
1297 
1298     value = prop->get(obj, errp);
1299     visit_type_bool(v, &value, name, errp);
1300 }
1301 
1302 static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1303                               const char *name, Error **errp)
1304 {
1305     BoolProperty *prop = opaque;
1306     bool value;
1307     Error *local_err = NULL;
1308 
1309     visit_type_bool(v, &value, name, &local_err);
1310     if (local_err) {
1311         error_propagate(errp, local_err);
1312         return;
1313     }
1314 
1315     prop->set(obj, value, errp);
1316 }
1317 
1318 static void property_release_bool(Object *obj, const char *name,
1319                                   void *opaque)
1320 {
1321     BoolProperty *prop = opaque;
1322     g_free(prop);
1323 }
1324 
1325 void object_property_add_bool(Object *obj, const char *name,
1326                               bool (*get)(Object *, Error **),
1327                               void (*set)(Object *, bool, Error **),
1328                               Error **errp)
1329 {
1330     BoolProperty *prop = g_malloc0(sizeof(*prop));
1331 
1332     prop->get = get;
1333     prop->set = set;
1334 
1335     object_property_add(obj, name, "bool",
1336                         get ? property_get_bool : NULL,
1337                         set ? property_set_bool : NULL,
1338                         property_release_bool,
1339                         prop, errp);
1340 }
1341 
1342 static char *qdev_get_type(Object *obj, Error **errp)
1343 {
1344     return g_strdup(object_get_typename(obj));
1345 }
1346 
1347 static void object_instance_init(Object *obj)
1348 {
1349     object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1350 }
1351 
1352 static void register_types(void)
1353 {
1354     static TypeInfo interface_info = {
1355         .name = TYPE_INTERFACE,
1356         .class_size = sizeof(InterfaceClass),
1357         .abstract = true,
1358     };
1359 
1360     static TypeInfo object_info = {
1361         .name = TYPE_OBJECT,
1362         .instance_size = sizeof(Object),
1363         .instance_init = object_instance_init,
1364         .abstract = true,
1365     };
1366 
1367     type_interface = type_register_internal(&interface_info);
1368     type_register_internal(&object_info);
1369 }
1370 
1371 type_init(register_types)
1372