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