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