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