]> git.proxmox.com Git - qemu.git/blame - qom/object.c
Merge git://github.com/hw-claudio/qemu-aarch64-queue into tcg-next
[qemu.git] / qom / object.c
CommitLineData
2f28d2ff
AL
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
14cccb61 13#include "qom/object.h"
2f28d2ff 14#include "qemu-common.h"
7b1b5d19 15#include "qapi/visitor.h"
b2cd7dee
PB
16#include "qapi/string-input-visitor.h"
17#include "qapi/string-output-visitor.h"
7b1b5d19 18#include "qapi/qmp/qerror.h"
fa131d94 19#include "trace.h"
2f28d2ff 20
7b7b7d18
PB
21/* TODO: replace QObject with a simpler visitor to avoid a dependency
22 * of the QOM core on QObject? */
14cccb61 23#include "qom/qom-qobject.h"
7b1b5d19
PB
24#include "qapi/qmp/qobject.h"
25#include "qapi/qmp/qbool.h"
26#include "qapi/qmp/qint.h"
27#include "qapi/qmp/qstring.h"
7b7b7d18 28
2f28d2ff
AL
29#define MAX_INTERFACES 32
30
31typedef struct InterfaceImpl InterfaceImpl;
32typedef struct TypeImpl TypeImpl;
33
34struct InterfaceImpl
35{
33e95c63 36 const char *typename;
2f28d2ff
AL
37};
38
39struct 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);
3b50e311 48 void (*class_base_init)(ObjectClass *klass, void *data);
2f28d2ff
AL
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
9970bd88
PB
67static Type type_interface;
68
2f28d2ff
AL
69static 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
80static void type_table_add(TypeImpl *ti)
81{
82 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
83}
84
85static TypeImpl *type_table_lookup(const char *name)
86{
87 return g_hash_table_lookup(type_table_get(), name);
88}
89
049cb3cf 90static TypeImpl *type_register_internal(const TypeInfo *info)
2f28d2ff
AL
91{
92 TypeImpl *ti = g_malloc0(sizeof(*ti));
33e95c63 93 int i;
2f28d2ff
AL
94
95 g_assert(info->name != NULL);
96
73093354
AL
97 if (type_table_lookup(info->name) != NULL) {
98 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
99 abort();
100 }
101
2f28d2ff
AL
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;
3b50e311 109 ti->class_base_init = info->class_base_init;
2f28d2ff
AL
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
33e95c63
AL
118 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
119 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
2f28d2ff 120 }
33e95c63 121 ti->num_interfaces = i;
2f28d2ff
AL
122
123 type_table_add(ti);
124
125 return ti;
126}
127
049cb3cf
PB
128TypeImpl *type_register(const TypeInfo *info)
129{
130 assert(info->parent);
131 return type_register_internal(info);
132}
133
2f28d2ff
AL
134TypeImpl *type_register_static(const TypeInfo *info)
135{
136 return type_register(info);
137}
138
139static 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
148static 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
158static bool type_has_parent(TypeImpl *type)
159{
160 return (type->parent != NULL);
161}
162
163static 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
aca59af6
IM
176static 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
33e95c63 189static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
2f28d2ff 190{
33e95c63
AL
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 }
2f28d2ff 198
33e95c63
AL
199 type = type_get_parent(type);
200 }
201
202 return false;
203}
204
205static void type_initialize(TypeImpl *ti);
206
207static 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);
2f28d2ff
AL
226}
227
ac451033 228static void type_initialize(TypeImpl *ti)
2f28d2ff 229{
745549c8 230 TypeImpl *parent;
2f28d2ff
AL
231
232 if (ti->class) {
233 return;
234 }
235
236 ti->class_size = type_class_get_size(ti);
aca59af6 237 ti->instance_size = type_object_get_size(ti);
2f28d2ff
AL
238
239 ti->class = g_malloc0(ti->class_size);
2f28d2ff 240
745549c8
PB
241 parent = type_get_parent(ti);
242 if (parent) {
ac451033 243 type_initialize(parent);
33e95c63
AL
244 GSList *e;
245 int i;
2f28d2ff 246
2f28d2ff 247 g_assert(parent->class_size <= ti->class_size);
745549c8 248 memcpy(ti->class, parent->class, parent->class_size);
3e407de4 249 ti->class->interfaces = NULL;
33e95c63
AL
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 }
745549c8 272 }
2f28d2ff 273
745549c8 274 ti->class->type = ti;
3b50e311 275
745549c8
PB
276 while (parent) {
277 if (parent->class_base_init) {
278 parent->class_base_init(ti->class, ti->class_data);
3b50e311 279 }
745549c8 280 parent = type_get_parent(parent);
2f28d2ff
AL
281 }
282
2f28d2ff
AL
283 if (ti->class_init) {
284 ti->class_init(ti->class, ti->class_data);
285 }
2f28d2ff 286
2f28d2ff 287
2f28d2ff
AL
288}
289
290static void object_init_with_type(Object *obj, TypeImpl *ti)
291{
2f28d2ff
AL
292 if (type_has_parent(ti)) {
293 object_init_with_type(obj, type_get_parent(ti));
294 }
295
2f28d2ff
AL
296 if (ti->instance_init) {
297 ti->instance_init(obj);
298 }
299}
300
301void object_initialize_with_type(void *data, TypeImpl *type)
302{
303 Object *obj = data;
304
305 g_assert(type != NULL);
ac451033 306 type_initialize(type);
aca59af6
IM
307
308 g_assert(type->instance_size >= sizeof(Object));
2f28d2ff
AL
309 g_assert(type->abstract == false);
310
311 memset(obj, 0, type->instance_size);
312 obj->class = type->class;
764b6312 313 object_ref(obj);
57c9fafe 314 QTAILQ_INIT(&obj->properties);
2f28d2ff
AL
315 object_init_with_type(obj, type);
316}
317
318void 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
5d9d3f47
AF
325static inline bool object_property_is_child(ObjectProperty *prop)
326{
327 return strstart(prop->type, "child<", NULL);
328}
329
330static inline bool object_property_is_link(ObjectProperty *prop)
331{
332 return strstart(prop->type, "link<", NULL);
333}
334
57c9fafe
AL
335static 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
352static void object_property_del_child(Object *obj, Object *child, Error **errp)
353{
354 ObjectProperty *prop;
355
356 QTAILQ_FOREACH(prop, &obj->properties, node) {
5d9d3f47 357 if (object_property_is_child(prop) && prop->opaque == child) {
57c9fafe 358 object_property_del(obj, prop->name, errp);
6c1fdcf9 359 break;
57c9fafe
AL
360 }
361 }
362}
363
364void object_unparent(Object *obj)
365{
e0a83fc2
PB
366 if (!obj->parent) {
367 return;
368 }
369
52e636cd 370 object_ref(obj);
667d22d1
PB
371 if (obj->class->unparent) {
372 (obj->class->unparent)(obj);
373 }
e998fa8d
MT
374 if (obj->parent) {
375 object_property_del_child(obj->parent, obj, NULL);
376 }
52e636cd 377 object_unref(obj);
57c9fafe
AL
378}
379
2f28d2ff
AL
380static void object_deinit(Object *obj, TypeImpl *type)
381{
382 if (type->instance_finalize) {
383 type->instance_finalize(obj);
384 }
385
2f28d2ff
AL
386 if (type_has_parent(type)) {
387 object_deinit(obj, type_get_parent(type));
388 }
389}
390
339c2708 391static void object_finalize(void *data)
2f28d2ff
AL
392{
393 Object *obj = data;
394 TypeImpl *ti = obj->class->type;
395
396 object_deinit(obj, ti);
57c9fafe 397 object_property_del_all(obj);
db85b575
AL
398
399 g_assert(obj->ref == 0);
fde9bf44
PB
400 if (obj->free) {
401 obj->free(obj);
402 }
2f28d2ff
AL
403}
404
405Object *object_new_with_type(Type type)
406{
407 Object *obj;
408
409 g_assert(type != NULL);
ac451033 410 type_initialize(type);
2f28d2ff
AL
411
412 obj = g_malloc(type->instance_size);
413 object_initialize_with_type(obj, type);
fde9bf44 414 obj->free = g_free;
2f28d2ff
AL
415
416 return obj;
417}
418
419Object *object_new(const char *typename)
420{
421 TypeImpl *ti = type_get_by_name(typename);
422
423 return object_new_with_type(ti);
424}
425
2f28d2ff
AL
426Object *object_dynamic_cast(Object *obj, const char *typename)
427{
b7f43fe4 428 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
acc4af3f
PB
429 return obj;
430 }
431
2f28d2ff
AL
432 return NULL;
433}
434
be17f18b
PB
435Object *object_dynamic_cast_assert(Object *obj, const char *typename,
436 const char *file, int line, const char *func)
2f28d2ff 437{
fa131d94
PB
438 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
439 typename, file, line, func);
440
3556c233 441#ifdef CONFIG_QOM_CAST_DEBUG
03587328
AL
442 int i;
443 Object *inst;
444
95916abc 445 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
03587328
AL
446 if (obj->class->cast_cache[i] == typename) {
447 goto out;
448 }
449 }
450
451 inst = object_dynamic_cast(obj, typename);
2f28d2ff 452
b7f43fe4 453 if (!inst && obj) {
be17f18b
PB
454 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
455 file, line, func, obj, typename);
2f28d2ff
AL
456 abort();
457 }
458
3556c233 459 assert(obj == inst);
03587328 460
95916abc 461 if (obj && obj == inst) {
03587328
AL
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
468out:
3556c233
PB
469#endif
470 return obj;
2f28d2ff
AL
471}
472
473ObjectClass *object_class_dynamic_cast(ObjectClass *class,
474 const char *typename)
475{
33e95c63 476 ObjectClass *ret = NULL;
bf0fda34
PB
477 TypeImpl *target_type;
478 TypeImpl *type;
2f28d2ff 479
bf0fda34
PB
480 if (!class) {
481 return NULL;
482 }
483
793c96b5 484 /* A simple fast path that can trigger a lot for leaf classes. */
bf0fda34 485 type = class->type;
793c96b5
PB
486 if (type->name == typename) {
487 return class;
488 }
489
bf0fda34 490 target_type = type_get_by_name(typename);
9ab880b3
AG
491 if (!target_type) {
492 /* target class type unknown, so fail the cast */
493 return NULL;
494 }
495
00e2ceae
PC
496 if (type->class->interfaces &&
497 type_is_ancestor(target_type, type_interface)) {
33e95c63
AL
498 int found = 0;
499 GSList *i;
2f28d2ff 500
33e95c63
AL
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;
2f28d2ff
AL
516 }
517
33e95c63 518 return ret;
2f28d2ff
AL
519}
520
521ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
be17f18b
PB
522 const char *typename,
523 const char *file, int line,
524 const char *func)
2f28d2ff 525{
fa131d94
PB
526 ObjectClass *ret;
527
528 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
529 typename, file, line, func);
2f28d2ff 530
03587328
AL
531#ifdef CONFIG_QOM_CAST_DEBUG
532 int i;
533
9d6a3d58 534 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
03587328
AL
535 if (class->cast_cache[i] == typename) {
536 ret = class;
537 goto out;
538 }
539 }
540#else
9d6a3d58 541 if (!class || !class->interfaces) {
3556c233
PB
542 return class;
543 }
544#endif
545
fa131d94 546 ret = object_class_dynamic_cast(class, typename);
bf0fda34 547 if (!ret && class) {
be17f18b
PB
548 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
549 file, line, func, class, typename);
2f28d2ff
AL
550 abort();
551 }
552
03587328 553#ifdef CONFIG_QOM_CAST_DEBUG
9d6a3d58 554 if (class && ret == class) {
03587328
AL
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 }
560out:
561#endif
2f28d2ff
AL
562 return ret;
563}
564
565const char *object_get_typename(Object *obj)
566{
567 return obj->class->type->name;
568}
569
570ObjectClass *object_get_class(Object *obj)
571{
572 return obj->class;
573}
574
17862378
AF
575bool object_class_is_abstract(ObjectClass *klass)
576{
577 return klass->type->abstract;
578}
579
2f28d2ff
AL
580const char *object_class_get_name(ObjectClass *klass)
581{
582 return klass->type->name;
583}
584
585ObjectClass *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
ac451033 593 type_initialize(type);
2f28d2ff
AL
594
595 return type->class;
596}
597
e7cce67f
PB
598ObjectClass *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
2f28d2ff
AL
611typedef struct OCFData
612{
613 void (*fn)(ObjectClass *klass, void *opaque);
93c511a1
AL
614 const char *implements_type;
615 bool include_abstract;
2f28d2ff
AL
616 void *opaque;
617} OCFData;
618
619static void object_class_foreach_tramp(gpointer key, gpointer value,
620 gpointer opaque)
621{
622 OCFData *data = opaque;
623 TypeImpl *type = value;
93c511a1 624 ObjectClass *k;
2f28d2ff 625
ac451033 626 type_initialize(type);
93c511a1 627 k = type->class;
2f28d2ff 628
93c511a1
AL
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);
2f28d2ff
AL
639}
640
641void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
93c511a1 642 const char *implements_type, bool include_abstract,
2f28d2ff
AL
643 void *opaque)
644{
93c511a1 645 OCFData data = { fn, implements_type, include_abstract, opaque };
2f28d2ff
AL
646
647 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
648}
57c9fafe 649
32efc535
PB
650int 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
418ba9e5
AF
667static 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
674GSList *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
57c9fafe
AL
684void object_ref(Object *obj)
685{
f08c03f3 686 atomic_inc(&obj->ref);
57c9fafe
AL
687}
688
689void object_unref(Object *obj)
690{
691 g_assert(obj->ref > 0);
57c9fafe
AL
692
693 /* parent always holds a reference to its children */
f08c03f3 694 if (atomic_fetch_dec(&obj->ref) == 1) {
57c9fafe
AL
695 object_finalize(obj);
696 }
697}
698
699void object_property_add(Object *obj, const char *name, const char *type,
700 ObjectPropertyAccessor *get,
701 ObjectPropertyAccessor *set,
702 ObjectPropertyRelease *release,
703 void *opaque, Error **errp)
704{
54852b03
PM
705 ObjectProperty *prop;
706
707 QTAILQ_FOREACH(prop, &obj->properties, node) {
708 if (strcmp(prop->name, name) == 0) {
709 error_setg(errp, "attempt to add duplicate property '%s'"
710 " to object (type '%s')", name,
711 object_get_typename(obj));
712 return;
713 }
714 }
715
716 prop = g_malloc0(sizeof(*prop));
57c9fafe
AL
717
718 prop->name = g_strdup(name);
719 prop->type = g_strdup(type);
720
721 prop->get = get;
722 prop->set = set;
723 prop->release = release;
724 prop->opaque = opaque;
725
726 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
727}
728
89bfe000
PB
729ObjectProperty *object_property_find(Object *obj, const char *name,
730 Error **errp)
57c9fafe
AL
731{
732 ObjectProperty *prop;
733
734 QTAILQ_FOREACH(prop, &obj->properties, node) {
735 if (strcmp(prop->name, name) == 0) {
736 return prop;
737 }
738 }
739
89bfe000 740 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
57c9fafe
AL
741 return NULL;
742}
743
744void object_property_del(Object *obj, const char *name, Error **errp)
745{
89bfe000 746 ObjectProperty *prop = object_property_find(obj, name, errp);
0866aca1 747 if (prop == NULL) {
0866aca1
AL
748 return;
749 }
57c9fafe 750
0866aca1
AL
751 if (prop->release) {
752 prop->release(obj, name, prop->opaque);
753 }
754
755 QTAILQ_REMOVE(&obj->properties, prop, node);
57c9fafe
AL
756
757 g_free(prop->name);
758 g_free(prop->type);
759 g_free(prop);
760}
761
762void object_property_get(Object *obj, Visitor *v, const char *name,
763 Error **errp)
764{
89bfe000 765 ObjectProperty *prop = object_property_find(obj, name, errp);
57c9fafe 766 if (prop == NULL) {
57c9fafe
AL
767 return;
768 }
769
770 if (!prop->get) {
771 error_set(errp, QERR_PERMISSION_DENIED);
772 } else {
773 prop->get(obj, v, prop->opaque, name, errp);
774 }
775}
776
777void object_property_set(Object *obj, Visitor *v, const char *name,
778 Error **errp)
779{
89bfe000 780 ObjectProperty *prop = object_property_find(obj, name, errp);
57c9fafe 781 if (prop == NULL) {
57c9fafe
AL
782 return;
783 }
784
785 if (!prop->set) {
786 error_set(errp, QERR_PERMISSION_DENIED);
787 } else {
788 prop->set(obj, v, prop->opaque, name, errp);
789 }
790}
791
7b7b7d18
PB
792void object_property_set_str(Object *obj, const char *value,
793 const char *name, Error **errp)
794{
795 QString *qstr = qstring_from_str(value);
796 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
797
798 QDECREF(qstr);
799}
800
801char *object_property_get_str(Object *obj, const char *name,
802 Error **errp)
803{
804 QObject *ret = object_property_get_qobject(obj, name, errp);
805 QString *qstring;
806 char *retval;
807
808 if (!ret) {
809 return NULL;
810 }
811 qstring = qobject_to_qstring(ret);
812 if (!qstring) {
813 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
814 retval = NULL;
815 } else {
816 retval = g_strdup(qstring_get_str(qstring));
817 }
818
819 QDECREF(qstring);
820 return retval;
821}
822
1d9c5a12
PB
823void object_property_set_link(Object *obj, Object *value,
824 const char *name, Error **errp)
825{
826 object_property_set_str(obj, object_get_canonical_path(value),
827 name, errp);
828}
829
830Object *object_property_get_link(Object *obj, const char *name,
831 Error **errp)
832{
833 char *str = object_property_get_str(obj, name, errp);
834 Object *target = NULL;
835
836 if (str && *str) {
837 target = object_resolve_path(str, NULL);
838 if (!target) {
839 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
840 }
841 }
842
843 g_free(str);
844 return target;
845}
846
7b7b7d18
PB
847void object_property_set_bool(Object *obj, bool value,
848 const char *name, Error **errp)
849{
850 QBool *qbool = qbool_from_int(value);
851 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
852
853 QDECREF(qbool);
854}
855
856bool object_property_get_bool(Object *obj, const char *name,
857 Error **errp)
858{
859 QObject *ret = object_property_get_qobject(obj, name, errp);
860 QBool *qbool;
861 bool retval;
862
863 if (!ret) {
864 return false;
865 }
866 qbool = qobject_to_qbool(ret);
867 if (!qbool) {
868 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
869 retval = false;
870 } else {
871 retval = qbool_get_int(qbool);
872 }
873
874 QDECREF(qbool);
875 return retval;
876}
877
878void object_property_set_int(Object *obj, int64_t value,
879 const char *name, Error **errp)
880{
881 QInt *qint = qint_from_int(value);
882 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
883
884 QDECREF(qint);
885}
886
887int64_t object_property_get_int(Object *obj, const char *name,
888 Error **errp)
889{
890 QObject *ret = object_property_get_qobject(obj, name, errp);
891 QInt *qint;
892 int64_t retval;
893
894 if (!ret) {
895 return -1;
896 }
897 qint = qobject_to_qint(ret);
898 if (!qint) {
899 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
900 retval = -1;
901 } else {
902 retval = qint_get_int(qint);
903 }
904
905 QDECREF(qint);
906 return retval;
b2cd7dee
PB
907}
908
909void object_property_parse(Object *obj, const char *string,
910 const char *name, Error **errp)
911{
912 StringInputVisitor *mi;
913 mi = string_input_visitor_new(string);
914 object_property_set(obj, string_input_get_visitor(mi), name, errp);
915
916 string_input_visitor_cleanup(mi);
917}
918
919char *object_property_print(Object *obj, const char *name,
920 Error **errp)
921{
922 StringOutputVisitor *mo;
923 char *string;
924
925 mo = string_output_visitor_new();
8185bfc1 926 object_property_get(obj, string_output_get_visitor(mo), name, errp);
b2cd7dee
PB
927 string = string_output_get_string(mo);
928 string_output_visitor_cleanup(mo);
929 return string;
7b7b7d18
PB
930}
931
57c9fafe
AL
932const char *object_property_get_type(Object *obj, const char *name, Error **errp)
933{
89bfe000 934 ObjectProperty *prop = object_property_find(obj, name, errp);
57c9fafe 935 if (prop == NULL) {
57c9fafe
AL
936 return NULL;
937 }
938
939 return prop->type;
940}
941
942Object *object_get_root(void)
943{
8b45d447 944 static Object *root;
57c9fafe 945
8b45d447
AL
946 if (!root) {
947 root = object_new("container");
57c9fafe
AL
948 }
949
8b45d447 950 return root;
57c9fafe
AL
951}
952
953static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
954 const char *name, Error **errp)
955{
956 Object *child = opaque;
957 gchar *path;
958
959 path = object_get_canonical_path(child);
960 visit_type_str(v, &path, name, errp);
961 g_free(path);
962}
963
db85b575
AL
964static void object_finalize_child_property(Object *obj, const char *name,
965 void *opaque)
966{
967 Object *child = opaque;
968
969 object_unref(child);
970}
971
57c9fafe
AL
972void object_property_add_child(Object *obj, const char *name,
973 Object *child, Error **errp)
974{
975 gchar *type;
976
977 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
978
979 object_property_add(obj, name, type, object_get_child_property,
db85b575 980 NULL, object_finalize_child_property, child, errp);
57c9fafe
AL
981
982 object_ref(child);
983 g_assert(child->parent == NULL);
984 child->parent = obj;
985
986 g_free(type);
987}
988
989static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
990 const char *name, Error **errp)
991{
992 Object **child = opaque;
993 gchar *path;
994
995 if (*child) {
996 path = object_get_canonical_path(*child);
997 visit_type_str(v, &path, name, errp);
998 g_free(path);
999 } else {
1000 path = (gchar *)"";
1001 visit_type_str(v, &path, name, errp);
1002 }
1003}
1004
1005static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
1006 const char *name, Error **errp)
1007{
1008 Object **child = opaque;
f0cdc966 1009 Object *old_target;
57c9fafe
AL
1010 bool ambiguous = false;
1011 const char *type;
1012 char *path;
11e35bfd 1013 gchar *target_type;
57c9fafe
AL
1014
1015 type = object_property_get_type(obj, name, NULL);
1016
1017 visit_type_str(v, &path, name, errp);
1018
f0cdc966
AB
1019 old_target = *child;
1020 *child = NULL;
57c9fafe
AL
1021
1022 if (strcmp(path, "") != 0) {
1023 Object *target;
1024
11e35bfd
PB
1025 /* Go from link<FOO> to FOO. */
1026 target_type = g_strndup(&type[5], strlen(type) - 6);
1027 target = object_resolve_path_type(path, target_type, &ambiguous);
57c9fafe 1028
11e35bfd
PB
1029 if (ambiguous) {
1030 error_set(errp, QERR_AMBIGUOUS_PATH, path);
1031 } else if (target) {
1032 object_ref(target);
1033 *child = target;
57c9fafe 1034 } else {
11e35bfd
PB
1035 target = object_resolve_path(path, &ambiguous);
1036 if (target || ambiguous) {
1037 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1038 } else {
1039 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
1040 }
57c9fafe 1041 }
11e35bfd 1042 g_free(target_type);
57c9fafe
AL
1043 }
1044
1045 g_free(path);
f0cdc966
AB
1046
1047 if (old_target != NULL) {
1048 object_unref(old_target);
1049 }
57c9fafe
AL
1050}
1051
1052void object_property_add_link(Object *obj, const char *name,
1053 const char *type, Object **child,
1054 Error **errp)
1055{
1056 gchar *full_type;
1057
1058 full_type = g_strdup_printf("link<%s>", type);
1059
1060 object_property_add(obj, name, full_type,
1061 object_get_link_property,
1062 object_set_link_property,
1063 NULL, child, errp);
1064
1065 g_free(full_type);
1066}
1067
1068gchar *object_get_canonical_path(Object *obj)
1069{
1070 Object *root = object_get_root();
1071 char *newpath = NULL, *path = NULL;
1072
1073 while (obj != root) {
1074 ObjectProperty *prop = NULL;
1075
1076 g_assert(obj->parent != NULL);
1077
1078 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
5d9d3f47 1079 if (!object_property_is_child(prop)) {
57c9fafe
AL
1080 continue;
1081 }
1082
1083 if (prop->opaque == obj) {
1084 if (path) {
1085 newpath = g_strdup_printf("%s/%s", prop->name, path);
1086 g_free(path);
1087 path = newpath;
1088 } else {
1089 path = g_strdup(prop->name);
1090 }
1091 break;
1092 }
1093 }
1094
1095 g_assert(prop != NULL);
1096
1097 obj = obj->parent;
1098 }
1099
1100 newpath = g_strdup_printf("/%s", path);
1101 g_free(path);
1102
1103 return newpath;
1104}
1105
3e84b483 1106Object *object_resolve_path_component(Object *parent, const gchar *part)
a612b2a6 1107{
89bfe000 1108 ObjectProperty *prop = object_property_find(parent, part, NULL);
a612b2a6
PB
1109 if (prop == NULL) {
1110 return NULL;
1111 }
1112
5d9d3f47 1113 if (object_property_is_link(prop)) {
a612b2a6 1114 return *(Object **)prop->opaque;
5d9d3f47 1115 } else if (object_property_is_child(prop)) {
a612b2a6
PB
1116 return prop->opaque;
1117 } else {
1118 return NULL;
1119 }
1120}
1121
57c9fafe
AL
1122static Object *object_resolve_abs_path(Object *parent,
1123 gchar **parts,
02fe2db6 1124 const char *typename,
57c9fafe
AL
1125 int index)
1126{
57c9fafe
AL
1127 Object *child;
1128
1129 if (parts[index] == NULL) {
02fe2db6 1130 return object_dynamic_cast(parent, typename);
57c9fafe
AL
1131 }
1132
1133 if (strcmp(parts[index], "") == 0) {
02fe2db6 1134 return object_resolve_abs_path(parent, parts, typename, index + 1);
57c9fafe
AL
1135 }
1136
a612b2a6 1137 child = object_resolve_path_component(parent, parts[index]);
57c9fafe
AL
1138 if (!child) {
1139 return NULL;
1140 }
1141
02fe2db6 1142 return object_resolve_abs_path(child, parts, typename, index + 1);
57c9fafe
AL
1143}
1144
1145static Object *object_resolve_partial_path(Object *parent,
1146 gchar **parts,
02fe2db6 1147 const char *typename,
57c9fafe
AL
1148 bool *ambiguous)
1149{
1150 Object *obj;
1151 ObjectProperty *prop;
1152
02fe2db6 1153 obj = object_resolve_abs_path(parent, parts, typename, 0);
57c9fafe
AL
1154
1155 QTAILQ_FOREACH(prop, &parent->properties, node) {
1156 Object *found;
1157
5d9d3f47 1158 if (!object_property_is_child(prop)) {
57c9fafe
AL
1159 continue;
1160 }
1161
02fe2db6
PB
1162 found = object_resolve_partial_path(prop->opaque, parts,
1163 typename, ambiguous);
57c9fafe
AL
1164 if (found) {
1165 if (obj) {
1166 if (ambiguous) {
1167 *ambiguous = true;
1168 }
1169 return NULL;
1170 }
1171 obj = found;
1172 }
1173
1174 if (ambiguous && *ambiguous) {
1175 return NULL;
1176 }
1177 }
1178
1179 return obj;
1180}
1181
02fe2db6
PB
1182Object *object_resolve_path_type(const char *path, const char *typename,
1183 bool *ambiguous)
57c9fafe 1184{
57c9fafe
AL
1185 Object *obj;
1186 gchar **parts;
1187
1188 parts = g_strsplit(path, "/", 0);
2e1103f6 1189 assert(parts);
57c9fafe 1190
2e1103f6 1191 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
57c9fafe
AL
1192 if (ambiguous) {
1193 *ambiguous = false;
1194 }
02fe2db6
PB
1195 obj = object_resolve_partial_path(object_get_root(), parts,
1196 typename, ambiguous);
57c9fafe 1197 } else {
02fe2db6 1198 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
57c9fafe
AL
1199 }
1200
1201 g_strfreev(parts);
1202
1203 return obj;
1204}
1205
02fe2db6
PB
1206Object *object_resolve_path(const char *path, bool *ambiguous)
1207{
1208 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1209}
1210
57c9fafe
AL
1211typedef struct StringProperty
1212{
1213 char *(*get)(Object *, Error **);
1214 void (*set)(Object *, const char *, Error **);
1215} StringProperty;
1216
7b7b7d18
PB
1217static void property_get_str(Object *obj, Visitor *v, void *opaque,
1218 const char *name, Error **errp)
57c9fafe
AL
1219{
1220 StringProperty *prop = opaque;
1221 char *value;
1222
1223 value = prop->get(obj, errp);
1224 if (value) {
1225 visit_type_str(v, &value, name, errp);
1226 g_free(value);
1227 }
1228}
1229
7b7b7d18
PB
1230static void property_set_str(Object *obj, Visitor *v, void *opaque,
1231 const char *name, Error **errp)
57c9fafe
AL
1232{
1233 StringProperty *prop = opaque;
1234 char *value;
1235 Error *local_err = NULL;
1236
1237 visit_type_str(v, &value, name, &local_err);
1238 if (local_err) {
1239 error_propagate(errp, local_err);
1240 return;
1241 }
1242
1243 prop->set(obj, value, errp);
1244 g_free(value);
1245}
1246
7b7b7d18
PB
1247static void property_release_str(Object *obj, const char *name,
1248 void *opaque)
57c9fafe
AL
1249{
1250 StringProperty *prop = opaque;
1251 g_free(prop);
1252}
1253
1254void object_property_add_str(Object *obj, const char *name,
1255 char *(*get)(Object *, Error **),
1256 void (*set)(Object *, const char *, Error **),
1257 Error **errp)
1258{
1259 StringProperty *prop = g_malloc0(sizeof(*prop));
1260
1261 prop->get = get;
1262 prop->set = set;
1263
1264 object_property_add(obj, name, "string",
7b7b7d18
PB
1265 get ? property_get_str : NULL,
1266 set ? property_set_str : NULL,
1267 property_release_str,
57c9fafe
AL
1268 prop, errp);
1269}
745549c8 1270
0e558843
AL
1271typedef struct BoolProperty
1272{
1273 bool (*get)(Object *, Error **);
1274 void (*set)(Object *, bool, Error **);
1275} BoolProperty;
1276
1277static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1278 const char *name, Error **errp)
1279{
1280 BoolProperty *prop = opaque;
1281 bool value;
1282
1283 value = prop->get(obj, errp);
1284 visit_type_bool(v, &value, name, errp);
1285}
1286
1287static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1288 const char *name, Error **errp)
1289{
1290 BoolProperty *prop = opaque;
1291 bool value;
1292 Error *local_err = NULL;
1293
1294 visit_type_bool(v, &value, name, &local_err);
1295 if (local_err) {
1296 error_propagate(errp, local_err);
1297 return;
1298 }
1299
1300 prop->set(obj, value, errp);
1301}
1302
1303static void property_release_bool(Object *obj, const char *name,
1304 void *opaque)
1305{
1306 BoolProperty *prop = opaque;
1307 g_free(prop);
1308}
1309
1310void object_property_add_bool(Object *obj, const char *name,
1311 bool (*get)(Object *, Error **),
1312 void (*set)(Object *, bool, Error **),
1313 Error **errp)
1314{
1315 BoolProperty *prop = g_malloc0(sizeof(*prop));
1316
1317 prop->get = get;
1318 prop->set = set;
1319
1320 object_property_add(obj, name, "bool",
1321 get ? property_get_bool : NULL,
1322 set ? property_set_bool : NULL,
1323 property_release_bool,
1324 prop, errp);
1325}
1326
2f262e06
PB
1327static char *qdev_get_type(Object *obj, Error **errp)
1328{
1329 return g_strdup(object_get_typename(obj));
1330}
1331
1332static void object_instance_init(Object *obj)
1333{
1334 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1335}
1336
745549c8
PB
1337static void register_types(void)
1338{
1339 static TypeInfo interface_info = {
1340 .name = TYPE_INTERFACE,
33e95c63 1341 .class_size = sizeof(InterfaceClass),
745549c8
PB
1342 .abstract = true,
1343 };
1344
1345 static TypeInfo object_info = {
1346 .name = TYPE_OBJECT,
1347 .instance_size = sizeof(Object),
2f262e06 1348 .instance_init = object_instance_init,
745549c8
PB
1349 .abstract = true,
1350 };
1351
049cb3cf
PB
1352 type_interface = type_register_internal(&interface_info);
1353 type_register_internal(&object_info);
745549c8
PB
1354}
1355
1356type_init(register_types)