blob: c7ef776b4ea22582dbc48f9eeac24c8eb25c0e75 [file] [log] [blame]
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001/*
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
Paolo Bonzini14cccb62012-12-17 18:19:50 +010013#include "qom/object.h"
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060014#include "qemu-common.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010015#include "qapi/visitor.h"
Hu Tao1f217722014-05-14 17:43:33 +080016#include "qapi-visit.h"
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +010017#include "qapi/string-input-visitor.h"
18#include "qapi/string-output-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010019#include "qapi/qmp/qerror.h"
Paolo Bonzinifa131d92013-05-10 14:16:39 +020020#include "trace.h"
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060021
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +010022/* TODO: replace QObject with a simpler visitor to avoid a dependency
23 * of the QOM core on QObject? */
Paolo Bonzini14cccb62012-12-17 18:19:50 +010024#include "qom/qom-qobject.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010025#include "qapi/qmp/qobject.h"
26#include "qapi/qmp/qbool.h"
27#include "qapi/qmp/qint.h"
28#include "qapi/qmp/qstring.h"
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +010029
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060030#define MAX_INTERFACES 32
31
32typedef struct InterfaceImpl InterfaceImpl;
33typedef struct TypeImpl TypeImpl;
34
35struct InterfaceImpl
36{
Anthony Liguori33e95c62012-08-10 13:16:10 +100037 const char *typename;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060038};
39
40struct TypeImpl
41{
42 const char *name;
43
44 size_t class_size;
45
46 size_t instance_size;
47
48 void (*class_init)(ObjectClass *klass, void *data);
Paolo Bonzini3b50e312012-05-02 13:30:55 +020049 void (*class_base_init)(ObjectClass *klass, void *data);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060050 void (*class_finalize)(ObjectClass *klass, void *data);
51
52 void *class_data;
53
54 void (*instance_init)(Object *obj);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -030055 void (*instance_post_init)(Object *obj);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060056 void (*instance_finalize)(Object *obj);
57
58 bool abstract;
59
60 const char *parent;
61 TypeImpl *parent_type;
62
63 ObjectClass *class;
64
65 int num_interfaces;
66 InterfaceImpl interfaces[MAX_INTERFACES];
67};
68
Paolo Bonzini9970bd82012-02-03 11:51:39 +010069static Type type_interface;
70
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060071static GHashTable *type_table_get(void)
72{
73 static GHashTable *type_table;
74
75 if (type_table == NULL) {
76 type_table = g_hash_table_new(g_str_hash, g_str_equal);
77 }
78
79 return type_table;
80}
81
Hervé Poussineauf54c19c2013-12-03 16:42:00 +010082static bool enumerating_types;
83
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060084static void type_table_add(TypeImpl *ti)
85{
Hervé Poussineauf54c19c2013-12-03 16:42:00 +010086 assert(!enumerating_types);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060087 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
88}
89
90static TypeImpl *type_table_lookup(const char *name)
91{
92 return g_hash_table_lookup(type_table_get(), name);
93}
94
Paolo Bonzinib061dc42013-12-03 16:41:59 +010095static TypeImpl *type_new(const TypeInfo *info)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060096{
97 TypeImpl *ti = g_malloc0(sizeof(*ti));
Anthony Liguori33e95c62012-08-10 13:16:10 +100098 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060099
100 g_assert(info->name != NULL);
101
Anthony Liguori73093352012-01-25 13:37:36 -0600102 if (type_table_lookup(info->name) != NULL) {
103 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
104 abort();
105 }
106
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600107 ti->name = g_strdup(info->name);
108 ti->parent = g_strdup(info->parent);
109
110 ti->class_size = info->class_size;
111 ti->instance_size = info->instance_size;
112
113 ti->class_init = info->class_init;
Paolo Bonzini3b50e312012-05-02 13:30:55 +0200114 ti->class_base_init = info->class_base_init;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600115 ti->class_finalize = info->class_finalize;
116 ti->class_data = info->class_data;
117
118 ti->instance_init = info->instance_init;
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300119 ti->instance_post_init = info->instance_post_init;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600120 ti->instance_finalize = info->instance_finalize;
121
122 ti->abstract = info->abstract;
123
Anthony Liguori33e95c62012-08-10 13:16:10 +1000124 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
125 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600126 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000127 ti->num_interfaces = i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600128
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100129 return ti;
130}
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600131
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100132static TypeImpl *type_register_internal(const TypeInfo *info)
133{
134 TypeImpl *ti;
135 ti = type_new(info);
136
137 type_table_add(ti);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600138 return ti;
139}
140
Paolo Bonzini049cb3c2012-04-04 15:58:40 +0200141TypeImpl *type_register(const TypeInfo *info)
142{
143 assert(info->parent);
144 return type_register_internal(info);
145}
146
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600147TypeImpl *type_register_static(const TypeInfo *info)
148{
149 return type_register(info);
150}
151
152static TypeImpl *type_get_by_name(const char *name)
153{
154 if (name == NULL) {
155 return NULL;
156 }
157
158 return type_table_lookup(name);
159}
160
161static TypeImpl *type_get_parent(TypeImpl *type)
162{
163 if (!type->parent_type && type->parent) {
164 type->parent_type = type_get_by_name(type->parent);
165 g_assert(type->parent_type != NULL);
166 }
167
168 return type->parent_type;
169}
170
171static bool type_has_parent(TypeImpl *type)
172{
173 return (type->parent != NULL);
174}
175
176static size_t type_class_get_size(TypeImpl *ti)
177{
178 if (ti->class_size) {
179 return ti->class_size;
180 }
181
182 if (type_has_parent(ti)) {
183 return type_class_get_size(type_get_parent(ti));
184 }
185
186 return sizeof(ObjectClass);
187}
188
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400189static size_t type_object_get_size(TypeImpl *ti)
190{
191 if (ti->instance_size) {
192 return ti->instance_size;
193 }
194
195 if (type_has_parent(ti)) {
196 return type_object_get_size(type_get_parent(ti));
197 }
198
199 return 0;
200}
201
Anthony Liguori33e95c62012-08-10 13:16:10 +1000202static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600203{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000204 assert(target_type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600205
Anthony Liguori33e95c62012-08-10 13:16:10 +1000206 /* Check if typename is a direct ancestor of type */
207 while (type) {
208 if (type == target_type) {
209 return true;
210 }
211
212 type = type_get_parent(type);
213 }
214
215 return false;
216}
217
218static void type_initialize(TypeImpl *ti);
219
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100220static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
221 TypeImpl *parent_type)
Anthony Liguori33e95c62012-08-10 13:16:10 +1000222{
223 InterfaceClass *new_iface;
224 TypeInfo info = { };
225 TypeImpl *iface_impl;
226
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100227 info.parent = parent_type->name;
228 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000229 info.abstract = true;
230
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100231 iface_impl = type_new(&info);
232 iface_impl->parent_type = parent_type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000233 type_initialize(iface_impl);
234 g_free((char *)info.name);
235
236 new_iface = (InterfaceClass *)iface_impl->class;
237 new_iface->concrete_class = ti->class;
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100238 new_iface->interface_type = interface_type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000239
240 ti->class->interfaces = g_slist_append(ti->class->interfaces,
241 iface_impl->class);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600242}
243
Igor Mitsyankoac451032012-02-28 15:57:11 +0400244static void type_initialize(TypeImpl *ti)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600245{
Paolo Bonzini745549c2012-03-31 16:45:54 +0200246 TypeImpl *parent;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600247
248 if (ti->class) {
249 return;
250 }
251
252 ti->class_size = type_class_get_size(ti);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400253 ti->instance_size = type_object_get_size(ti);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600254
255 ti->class = g_malloc0(ti->class_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600256
Paolo Bonzini745549c2012-03-31 16:45:54 +0200257 parent = type_get_parent(ti);
258 if (parent) {
Igor Mitsyankoac451032012-02-28 15:57:11 +0400259 type_initialize(parent);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000260 GSList *e;
261 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600262
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600263 g_assert(parent->class_size <= ti->class_size);
Paolo Bonzini745549c2012-03-31 16:45:54 +0200264 memcpy(ti->class, parent->class, parent->class_size);
Peter Crosthwaite3e407de2013-02-19 14:02:09 +1000265 ti->class->interfaces = NULL;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000266
267 for (e = parent->class->interfaces; e; e = e->next) {
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100268 InterfaceClass *iface = e->data;
269 ObjectClass *klass = OBJECT_CLASS(iface);
270
271 type_initialize_interface(ti, iface->interface_type, klass->type);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000272 }
273
274 for (i = 0; i < ti->num_interfaces; i++) {
275 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
276 for (e = ti->class->interfaces; e; e = e->next) {
277 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
278
279 if (type_is_ancestor(target_type, t)) {
280 break;
281 }
282 }
283
284 if (e) {
285 continue;
286 }
287
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100288 type_initialize_interface(ti, t, t);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000289 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600290 }
291
Paolo Bonzini745549c2012-03-31 16:45:54 +0200292 ti->class->type = ti;
293
294 while (parent) {
295 if (parent->class_base_init) {
296 parent->class_base_init(ti->class, ti->class_data);
297 }
298 parent = type_get_parent(parent);
299 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600300
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600301 if (ti->class_init) {
302 ti->class_init(ti->class, ti->class_data);
303 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600304}
305
306static void object_init_with_type(Object *obj, TypeImpl *ti)
307{
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600308 if (type_has_parent(ti)) {
309 object_init_with_type(obj, type_get_parent(ti));
310 }
311
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600312 if (ti->instance_init) {
313 ti->instance_init(obj);
314 }
315}
316
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300317static void object_post_init_with_type(Object *obj, TypeImpl *ti)
318{
319 if (ti->instance_post_init) {
320 ti->instance_post_init(obj);
321 }
322
323 if (type_has_parent(ti)) {
324 object_post_init_with_type(obj, type_get_parent(ti));
325 }
326}
327
Andreas Färber5b9237f2013-08-30 18:28:37 +0200328void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600329{
330 Object *obj = data;
331
332 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400333 type_initialize(type);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400334
335 g_assert(type->instance_size >= sizeof(Object));
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600336 g_assert(type->abstract == false);
Andreas Färber5b9237f2013-08-30 18:28:37 +0200337 g_assert(size >= type->instance_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600338
339 memset(obj, 0, type->instance_size);
340 obj->class = type->class;
Paolo Bonzini764b6312012-11-23 09:47:12 +0100341 object_ref(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600342 QTAILQ_INIT(&obj->properties);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600343 object_init_with_type(obj, type);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300344 object_post_init_with_type(obj, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600345}
346
Andreas Färber213f0c42013-08-23 19:37:12 +0200347void object_initialize(void *data, size_t size, const char *typename)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600348{
349 TypeImpl *type = type_get_by_name(typename);
350
Andreas Färber5b9237f2013-08-30 18:28:37 +0200351 object_initialize_with_type(data, size, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600352}
353
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200354static inline bool object_property_is_child(ObjectProperty *prop)
355{
356 return strstart(prop->type, "child<", NULL);
357}
358
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600359static void object_property_del_all(Object *obj)
360{
361 while (!QTAILQ_EMPTY(&obj->properties)) {
362 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
363
364 QTAILQ_REMOVE(&obj->properties, prop, node);
365
366 if (prop->release) {
367 prop->release(obj, prop->name, prop->opaque);
368 }
369
370 g_free(prop->name);
371 g_free(prop->type);
Gonglei80742642014-10-07 14:33:21 +0800372 g_free(prop->description);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600373 g_free(prop);
374 }
375}
376
377static void object_property_del_child(Object *obj, Object *child, Error **errp)
378{
379 ObjectProperty *prop;
380
381 QTAILQ_FOREACH(prop, &obj->properties, node) {
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200382 if (object_property_is_child(prop) && prop->opaque == child) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600383 object_property_del(obj, prop->name, errp);
Paolo Bonzini6c1fdcf2012-02-28 09:54:15 +0100384 break;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600385 }
386 }
387}
388
389void object_unparent(Object *obj)
390{
Michael S. Tsirkine998fa82013-03-18 21:01:37 +0200391 if (obj->parent) {
392 object_property_del_child(obj->parent, obj, NULL);
393 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600394}
395
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600396static void object_deinit(Object *obj, TypeImpl *type)
397{
398 if (type->instance_finalize) {
399 type->instance_finalize(obj);
400 }
401
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600402 if (type_has_parent(type)) {
403 object_deinit(obj, type_get_parent(type));
404 }
405}
406
Paolo Bonzini339c2702012-11-23 09:47:16 +0100407static void object_finalize(void *data)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600408{
409 Object *obj = data;
410 TypeImpl *ti = obj->class->type;
411
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600412 object_property_del_all(obj);
Paolo Bonzini76a6e1c2014-06-11 11:58:30 +0200413 object_deinit(obj, ti);
Anthony Liguoridb85b572011-12-23 08:47:39 -0600414
415 g_assert(obj->ref == 0);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100416 if (obj->free) {
417 obj->free(obj);
418 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600419}
420
421Object *object_new_with_type(Type type)
422{
423 Object *obj;
424
425 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400426 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600427
428 obj = g_malloc(type->instance_size);
Andreas Färber5b9237f2013-08-30 18:28:37 +0200429 object_initialize_with_type(obj, type->instance_size, type);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100430 obj->free = g_free;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600431
432 return obj;
433}
434
435Object *object_new(const char *typename)
436{
437 TypeImpl *ti = type_get_by_name(typename);
438
439 return object_new_with_type(ti);
440}
441
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600442Object *object_dynamic_cast(Object *obj, const char *typename)
443{
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100444 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100445 return obj;
446 }
447
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600448 return NULL;
449}
450
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200451Object *object_dynamic_cast_assert(Object *obj, const char *typename,
452 const char *file, int line, const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600453{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200454 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
455 typename, file, line, func);
456
Paolo Bonzini3556c232013-05-10 14:16:40 +0200457#ifdef CONFIG_QOM_CAST_DEBUG
Anthony Liguori03587322013-05-13 15:22:24 -0500458 int i;
459 Object *inst;
460
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000461 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800462 if (obj->class->object_cast_cache[i] == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500463 goto out;
464 }
465 }
466
467 inst = object_dynamic_cast(obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600468
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100469 if (!inst && obj) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200470 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
471 file, line, func, obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600472 abort();
473 }
474
Paolo Bonzini3556c232013-05-10 14:16:40 +0200475 assert(obj == inst);
Anthony Liguori03587322013-05-13 15:22:24 -0500476
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000477 if (obj && obj == inst) {
Anthony Liguori03587322013-05-13 15:22:24 -0500478 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800479 obj->class->object_cast_cache[i - 1] =
480 obj->class->object_cast_cache[i];
Anthony Liguori03587322013-05-13 15:22:24 -0500481 }
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800482 obj->class->object_cast_cache[i - 1] = typename;
Anthony Liguori03587322013-05-13 15:22:24 -0500483 }
484
485out:
Paolo Bonzini3556c232013-05-10 14:16:40 +0200486#endif
487 return obj;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600488}
489
490ObjectClass *object_class_dynamic_cast(ObjectClass *class,
491 const char *typename)
492{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000493 ObjectClass *ret = NULL;
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200494 TypeImpl *target_type;
495 TypeImpl *type;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600496
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200497 if (!class) {
498 return NULL;
499 }
500
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200501 /* A simple fast path that can trigger a lot for leaf classes. */
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200502 type = class->type;
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200503 if (type->name == typename) {
504 return class;
505 }
506
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200507 target_type = type_get_by_name(typename);
Alexander Graf9ab880b2013-04-30 15:02:16 +0200508 if (!target_type) {
509 /* target class type unknown, so fail the cast */
510 return NULL;
511 }
512
Peter Crosthwaite00e2cea2013-02-19 14:02:10 +1000513 if (type->class->interfaces &&
514 type_is_ancestor(target_type, type_interface)) {
Anthony Liguori33e95c62012-08-10 13:16:10 +1000515 int found = 0;
516 GSList *i;
517
518 for (i = class->interfaces; i; i = i->next) {
519 ObjectClass *target_class = i->data;
520
521 if (type_is_ancestor(target_class->type, target_type)) {
522 ret = target_class;
523 found++;
524 }
525 }
526
527 /* The match was ambiguous, don't allow a cast */
528 if (found > 1) {
529 ret = NULL;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600530 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000531 } else if (type_is_ancestor(type, target_type)) {
532 ret = class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600533 }
534
Anthony Liguori33e95c62012-08-10 13:16:10 +1000535 return ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600536}
537
538ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200539 const char *typename,
540 const char *file, int line,
541 const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600542{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200543 ObjectClass *ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600544
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200545 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
546 typename, file, line, func);
547
Anthony Liguori03587322013-05-13 15:22:24 -0500548#ifdef CONFIG_QOM_CAST_DEBUG
549 int i;
550
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000551 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800552 if (class->class_cast_cache[i] == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500553 ret = class;
554 goto out;
555 }
556 }
557#else
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000558 if (!class || !class->interfaces) {
Paolo Bonzini3556c232013-05-10 14:16:40 +0200559 return class;
560 }
561#endif
562
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200563 ret = object_class_dynamic_cast(class, typename);
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200564 if (!ret && class) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200565 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
566 file, line, func, class, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600567 abort();
568 }
569
Anthony Liguori03587322013-05-13 15:22:24 -0500570#ifdef CONFIG_QOM_CAST_DEBUG
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000571 if (class && ret == class) {
Anthony Liguori03587322013-05-13 15:22:24 -0500572 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800573 class->class_cast_cache[i - 1] = class->class_cast_cache[i];
Anthony Liguori03587322013-05-13 15:22:24 -0500574 }
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800575 class->class_cast_cache[i - 1] = typename;
Anthony Liguori03587322013-05-13 15:22:24 -0500576 }
577out:
578#endif
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600579 return ret;
580}
581
582const char *object_get_typename(Object *obj)
583{
584 return obj->class->type->name;
585}
586
587ObjectClass *object_get_class(Object *obj)
588{
589 return obj->class;
590}
591
Andreas Färber17862372013-01-23 12:20:18 +0100592bool object_class_is_abstract(ObjectClass *klass)
593{
594 return klass->type->abstract;
595}
596
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600597const char *object_class_get_name(ObjectClass *klass)
598{
599 return klass->type->name;
600}
601
602ObjectClass *object_class_by_name(const char *typename)
603{
604 TypeImpl *type = type_get_by_name(typename);
605
606 if (!type) {
607 return NULL;
608 }
609
Igor Mitsyankoac451032012-02-28 15:57:11 +0400610 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600611
612 return type->class;
613}
614
Paolo Bonzinie7cce672012-05-02 13:30:54 +0200615ObjectClass *object_class_get_parent(ObjectClass *class)
616{
617 TypeImpl *type = type_get_parent(class->type);
618
619 if (!type) {
620 return NULL;
621 }
622
623 type_initialize(type);
624
625 return type->class;
626}
627
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600628typedef struct OCFData
629{
630 void (*fn)(ObjectClass *klass, void *opaque);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600631 const char *implements_type;
632 bool include_abstract;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600633 void *opaque;
634} OCFData;
635
636static void object_class_foreach_tramp(gpointer key, gpointer value,
637 gpointer opaque)
638{
639 OCFData *data = opaque;
640 TypeImpl *type = value;
Anthony Liguori93c511a2011-12-22 14:11:53 -0600641 ObjectClass *k;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600642
Igor Mitsyankoac451032012-02-28 15:57:11 +0400643 type_initialize(type);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600644 k = type->class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600645
Anthony Liguori93c511a2011-12-22 14:11:53 -0600646 if (!data->include_abstract && type->abstract) {
647 return;
648 }
649
650 if (data->implements_type &&
651 !object_class_dynamic_cast(k, data->implements_type)) {
652 return;
653 }
654
655 data->fn(k, data->opaque);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600656}
657
658void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
Anthony Liguori93c511a2011-12-22 14:11:53 -0600659 const char *implements_type, bool include_abstract,
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600660 void *opaque)
661{
Anthony Liguori93c511a2011-12-22 14:11:53 -0600662 OCFData data = { fn, implements_type, include_abstract, opaque };
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600663
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100664 enumerating_types = true;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600665 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100666 enumerating_types = false;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600667}
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600668
Paolo Bonzini32efc532012-04-11 23:30:20 +0200669int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
670 void *opaque)
671{
Alexey Kardashevskiy8af734c2014-07-14 00:41:08 +1000672 ObjectProperty *prop, *next;
Paolo Bonzini32efc532012-04-11 23:30:20 +0200673 int ret = 0;
674
Alexey Kardashevskiy8af734c2014-07-14 00:41:08 +1000675 QTAILQ_FOREACH_SAFE(prop, &obj->properties, node, next) {
Paolo Bonzini32efc532012-04-11 23:30:20 +0200676 if (object_property_is_child(prop)) {
677 ret = fn(prop->opaque, opaque);
678 if (ret != 0) {
679 break;
680 }
681 }
682 }
683 return ret;
684}
685
Andreas Färber418ba9e2012-02-25 23:07:34 +0100686static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
687{
688 GSList **list = opaque;
689
690 *list = g_slist_prepend(*list, klass);
691}
692
693GSList *object_class_get_list(const char *implements_type,
694 bool include_abstract)
695{
696 GSList *list = NULL;
697
698 object_class_foreach(object_class_get_list_tramp,
699 implements_type, include_abstract, &list);
700 return list;
701}
702
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600703void object_ref(Object *obj)
704{
Peter Crosthwaite8ffad852014-06-05 23:13:36 -0700705 if (!obj) {
706 return;
707 }
Jan Kiszkaf08c03f2013-07-02 11:36:39 +0200708 atomic_inc(&obj->ref);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600709}
710
711void object_unref(Object *obj)
712{
Peter Crosthwaite8ffad852014-06-05 23:13:36 -0700713 if (!obj) {
714 return;
715 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600716 g_assert(obj->ref > 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600717
718 /* parent always holds a reference to its children */
Jan Kiszkaf08c03f2013-07-02 11:36:39 +0200719 if (atomic_fetch_dec(&obj->ref) == 1) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600720 object_finalize(obj);
721 }
722}
723
Paolo Bonzini64607d02014-06-05 13:11:51 +0200724ObjectProperty *
725object_property_add(Object *obj, const char *name, const char *type,
726 ObjectPropertyAccessor *get,
727 ObjectPropertyAccessor *set,
728 ObjectPropertyRelease *release,
729 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600730{
Peter Maydell54852b02013-03-25 13:15:13 +0000731 ObjectProperty *prop;
Peter Crosthwaite33965902014-08-19 23:55:52 -0700732 size_t name_len = strlen(name);
733
734 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
735 int i;
736 ObjectProperty *ret;
737 char *name_no_array = g_strdup(name);
738
739 name_no_array[name_len - 3] = '\0';
740 for (i = 0; ; ++i) {
741 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
742
743 ret = object_property_add(obj, full_name, type, get, set,
744 release, opaque, NULL);
745 g_free(full_name);
746 if (ret) {
747 break;
748 }
749 }
750 g_free(name_no_array);
751 return ret;
752 }
Peter Maydell54852b02013-03-25 13:15:13 +0000753
754 QTAILQ_FOREACH(prop, &obj->properties, node) {
755 if (strcmp(prop->name, name) == 0) {
756 error_setg(errp, "attempt to add duplicate property '%s'"
757 " to object (type '%s')", name,
758 object_get_typename(obj));
Paolo Bonzini64607d02014-06-05 13:11:51 +0200759 return NULL;
Peter Maydell54852b02013-03-25 13:15:13 +0000760 }
761 }
762
763 prop = g_malloc0(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600764
765 prop->name = g_strdup(name);
766 prop->type = g_strdup(type);
767
768 prop->get = get;
769 prop->set = set;
770 prop->release = release;
771 prop->opaque = opaque;
772
773 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
Paolo Bonzini64607d02014-06-05 13:11:51 +0200774 return prop;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600775}
776
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200777ObjectProperty *object_property_find(Object *obj, const char *name,
778 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600779{
780 ObjectProperty *prop;
781
782 QTAILQ_FOREACH(prop, &obj->properties, node) {
783 if (strcmp(prop->name, name) == 0) {
784 return prop;
785 }
786 }
787
Cole Robinsonf231b882014-03-21 19:42:26 -0400788 error_setg(errp, "Property '.%s' not found", name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600789 return NULL;
790}
791
792void object_property_del(Object *obj, const char *name, Error **errp)
793{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200794 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600795 if (prop == NULL) {
Anthony Liguori0866aca2011-12-23 15:34:39 -0600796 return;
797 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600798
Anthony Liguori0866aca2011-12-23 15:34:39 -0600799 if (prop->release) {
800 prop->release(obj, name, prop->opaque);
801 }
802
803 QTAILQ_REMOVE(&obj->properties, prop, node);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600804
805 g_free(prop->name);
806 g_free(prop->type);
Gonglei80742642014-10-07 14:33:21 +0800807 g_free(prop->description);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600808 g_free(prop);
809}
810
811void object_property_get(Object *obj, Visitor *v, const char *name,
812 Error **errp)
813{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200814 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600815 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600816 return;
817 }
818
819 if (!prop->get) {
820 error_set(errp, QERR_PERMISSION_DENIED);
821 } else {
822 prop->get(obj, v, prop->opaque, name, errp);
823 }
824}
825
826void object_property_set(Object *obj, Visitor *v, const char *name,
827 Error **errp)
828{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200829 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600830 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600831 return;
832 }
833
834 if (!prop->set) {
835 error_set(errp, QERR_PERMISSION_DENIED);
836 } else {
837 prop->set(obj, v, prop->opaque, name, errp);
838 }
839}
840
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +0100841void object_property_set_str(Object *obj, const char *value,
842 const char *name, Error **errp)
843{
844 QString *qstr = qstring_from_str(value);
845 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
846
847 QDECREF(qstr);
848}
849
850char *object_property_get_str(Object *obj, const char *name,
851 Error **errp)
852{
853 QObject *ret = object_property_get_qobject(obj, name, errp);
854 QString *qstring;
855 char *retval;
856
857 if (!ret) {
858 return NULL;
859 }
860 qstring = qobject_to_qstring(ret);
861 if (!qstring) {
862 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
863 retval = NULL;
864 } else {
865 retval = g_strdup(qstring_get_str(qstring));
866 }
867
868 QDECREF(qstring);
869 return retval;
870}
871
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +0100872void object_property_set_link(Object *obj, Object *value,
873 const char *name, Error **errp)
874{
Peter Crosthwaited3c49312014-09-25 22:19:19 -0700875 if (value) {
876 gchar *path = object_get_canonical_path(value);
877 object_property_set_str(obj, path, name, errp);
878 g_free(path);
879 } else {
880 object_property_set_str(obj, "", name, errp);
881 }
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +0100882}
883
884Object *object_property_get_link(Object *obj, const char *name,
885 Error **errp)
886{
887 char *str = object_property_get_str(obj, name, errp);
888 Object *target = NULL;
889
890 if (str && *str) {
891 target = object_resolve_path(str, NULL);
892 if (!target) {
893 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
894 }
895 }
896
897 g_free(str);
898 return target;
899}
900
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +0100901void object_property_set_bool(Object *obj, bool value,
902 const char *name, Error **errp)
903{
904 QBool *qbool = qbool_from_int(value);
905 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
906
907 QDECREF(qbool);
908}
909
910bool object_property_get_bool(Object *obj, const char *name,
911 Error **errp)
912{
913 QObject *ret = object_property_get_qobject(obj, name, errp);
914 QBool *qbool;
915 bool retval;
916
917 if (!ret) {
918 return false;
919 }
920 qbool = qobject_to_qbool(ret);
921 if (!qbool) {
922 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
923 retval = false;
924 } else {
925 retval = qbool_get_int(qbool);
926 }
927
928 QDECREF(qbool);
929 return retval;
930}
931
932void object_property_set_int(Object *obj, int64_t value,
933 const char *name, Error **errp)
934{
935 QInt *qint = qint_from_int(value);
936 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
937
938 QDECREF(qint);
939}
940
941int64_t object_property_get_int(Object *obj, const char *name,
942 Error **errp)
943{
944 QObject *ret = object_property_get_qobject(obj, name, errp);
945 QInt *qint;
946 int64_t retval;
947
948 if (!ret) {
949 return -1;
950 }
951 qint = qobject_to_qint(ret);
952 if (!qint) {
953 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
954 retval = -1;
955 } else {
956 retval = qint_get_int(qint);
957 }
958
959 QDECREF(qint);
960 return retval;
961}
962
Hu Tao1f217722014-05-14 17:43:33 +0800963int object_property_get_enum(Object *obj, const char *name,
964 const char *strings[], Error **errp)
965{
966 StringOutputVisitor *sov;
967 StringInputVisitor *siv;
Chen Fan976620a2014-08-18 14:46:34 +0800968 char *str;
Hu Tao1f217722014-05-14 17:43:33 +0800969 int ret;
970
971 sov = string_output_visitor_new(false);
972 object_property_get(obj, string_output_get_visitor(sov), name, errp);
Chen Fan976620a2014-08-18 14:46:34 +0800973 str = string_output_get_string(sov);
974 siv = string_input_visitor_new(str);
Hu Tao1f217722014-05-14 17:43:33 +0800975 string_output_visitor_cleanup(sov);
976 visit_type_enum(string_input_get_visitor(siv),
977 &ret, strings, NULL, name, errp);
Chen Fan976620a2014-08-18 14:46:34 +0800978
979 g_free(str);
Hu Tao1f217722014-05-14 17:43:33 +0800980 string_input_visitor_cleanup(siv);
981
982 return ret;
983}
984
985void object_property_get_uint16List(Object *obj, const char *name,
986 uint16List **list, Error **errp)
987{
988 StringOutputVisitor *ov;
989 StringInputVisitor *iv;
Chen Fan976620a2014-08-18 14:46:34 +0800990 char *str;
Hu Tao1f217722014-05-14 17:43:33 +0800991
992 ov = string_output_visitor_new(false);
993 object_property_get(obj, string_output_get_visitor(ov),
994 name, errp);
Chen Fan976620a2014-08-18 14:46:34 +0800995 str = string_output_get_string(ov);
996 iv = string_input_visitor_new(str);
Hu Tao1f217722014-05-14 17:43:33 +0800997 visit_type_uint16List(string_input_get_visitor(iv),
998 list, NULL, errp);
Chen Fan976620a2014-08-18 14:46:34 +0800999
1000 g_free(str);
Hu Tao1f217722014-05-14 17:43:33 +08001001 string_output_visitor_cleanup(ov);
1002 string_input_visitor_cleanup(iv);
1003}
1004
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001005void object_property_parse(Object *obj, const char *string,
1006 const char *name, Error **errp)
1007{
1008 StringInputVisitor *mi;
1009 mi = string_input_visitor_new(string);
1010 object_property_set(obj, string_input_get_visitor(mi), name, errp);
1011
1012 string_input_visitor_cleanup(mi);
1013}
1014
Paolo Bonzini0b7593e2014-02-08 11:01:50 +01001015char *object_property_print(Object *obj, const char *name, bool human,
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001016 Error **errp)
1017{
1018 StringOutputVisitor *mo;
Gonglei3a530092014-09-27 13:13:55 +08001019 char *string = NULL;
1020 Error *local_err = NULL;
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001021
Paolo Bonzini0b7593e2014-02-08 11:01:50 +01001022 mo = string_output_visitor_new(human);
Gonglei3a530092014-09-27 13:13:55 +08001023 object_property_get(obj, string_output_get_visitor(mo), name, &local_err);
1024 if (local_err) {
1025 error_propagate(errp, local_err);
1026 goto out;
1027 }
1028
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001029 string = string_output_get_string(mo);
Gonglei3a530092014-09-27 13:13:55 +08001030
1031out:
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001032 string_output_visitor_cleanup(mo);
1033 return string;
1034}
1035
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001036const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1037{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001038 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001039 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001040 return NULL;
1041 }
1042
1043 return prop->type;
1044}
1045
1046Object *object_get_root(void)
1047{
Anthony Liguori8b45d442011-12-23 09:08:05 -06001048 static Object *root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001049
Anthony Liguori8b45d442011-12-23 09:08:05 -06001050 if (!root) {
1051 root = object_new("container");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001052 }
1053
Anthony Liguori8b45d442011-12-23 09:08:05 -06001054 return root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001055}
1056
1057static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
1058 const char *name, Error **errp)
1059{
1060 Object *child = opaque;
1061 gchar *path;
1062
1063 path = object_get_canonical_path(child);
1064 visit_type_str(v, &path, name, errp);
1065 g_free(path);
1066}
1067
Paolo Bonzini64607d02014-06-05 13:11:51 +02001068static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part)
1069{
1070 return opaque;
1071}
1072
Anthony Liguoridb85b572011-12-23 08:47:39 -06001073static void object_finalize_child_property(Object *obj, const char *name,
1074 void *opaque)
1075{
1076 Object *child = opaque;
1077
Paolo Bonzinibffc6872014-06-11 11:57:38 +02001078 if (child->class->unparent) {
1079 (child->class->unparent)(child);
1080 }
1081 child->parent = NULL;
Anthony Liguoridb85b572011-12-23 08:47:39 -06001082 object_unref(child);
1083}
1084
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001085void object_property_add_child(Object *obj, const char *name,
1086 Object *child, Error **errp)
1087{
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001088 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001089 gchar *type;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001090 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001091
1092 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1093
Paolo Bonzini64607d02014-06-05 13:11:51 +02001094 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1095 object_finalize_child_property, child, &local_err);
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001096 if (local_err) {
1097 error_propagate(errp, local_err);
1098 goto out;
1099 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02001100
1101 op->resolve = object_resolve_child_property;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001102 object_ref(child);
1103 g_assert(child->parent == NULL);
1104 child->parent = obj;
1105
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001106out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001107 g_free(type);
1108}
1109
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001110void object_property_allow_set_link(Object *obj, const char *name,
1111 Object *val, Error **errp)
1112{
1113 /* Allow the link to be set, always */
1114}
1115
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001116typedef struct {
1117 Object **child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001118 void (*check)(Object *, const char *, Object *, Error **);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001119 ObjectPropertyLinkFlags flags;
1120} LinkProperty;
1121
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001122static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
1123 const char *name, Error **errp)
1124{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001125 LinkProperty *lprop = opaque;
1126 Object **child = lprop->child;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001127 gchar *path;
1128
1129 if (*child) {
1130 path = object_get_canonical_path(*child);
1131 visit_type_str(v, &path, name, errp);
1132 g_free(path);
1133 } else {
1134 path = (gchar *)"";
1135 visit_type_str(v, &path, name, errp);
1136 }
1137}
1138
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001139/*
1140 * object_resolve_link:
1141 *
1142 * Lookup an object and ensure its type matches the link property type. This
1143 * is similar to object_resolve_path() except type verification against the
1144 * link property is performed.
1145 *
1146 * Returns: The matched object or NULL on path lookup failures.
1147 */
1148static Object *object_resolve_link(Object *obj, const char *name,
1149 const char *path, Error **errp)
1150{
1151 const char *type;
1152 gchar *target_type;
1153 bool ambiguous = false;
1154 Object *target;
1155
1156 /* Go from link<FOO> to FOO. */
1157 type = object_property_get_type(obj, name, NULL);
1158 target_type = g_strndup(&type[5], strlen(type) - 6);
1159 target = object_resolve_path_type(path, target_type, &ambiguous);
1160
1161 if (ambiguous) {
Cole Robinsonf231b882014-03-21 19:42:26 -04001162 error_set(errp, ERROR_CLASS_GENERIC_ERROR,
1163 "Path '%s' does not uniquely identify an object", path);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001164 } else if (!target) {
1165 target = object_resolve_path(path, &ambiguous);
1166 if (target || ambiguous) {
1167 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1168 } else {
1169 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
1170 }
1171 target = NULL;
1172 }
1173 g_free(target_type);
1174
1175 return target;
1176}
1177
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001178static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
1179 const char *name, Error **errp)
1180{
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001181 Error *local_err = NULL;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001182 LinkProperty *prop = opaque;
1183 Object **child = prop->child;
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001184 Object *old_target = *child;
1185 Object *new_target = NULL;
1186 char *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001187
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001188 visit_type_str(v, &path, name, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001189
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001190 if (!local_err && strcmp(path, "") != 0) {
1191 new_target = object_resolve_link(obj, name, path, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001192 }
1193
1194 g_free(path);
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001195 if (local_err) {
1196 error_propagate(errp, local_err);
1197 return;
1198 }
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001199
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001200 prop->check(obj, name, new_target, &local_err);
1201 if (local_err) {
1202 error_propagate(errp, local_err);
1203 return;
1204 }
1205
Peter Crosthwaite8ffad852014-06-05 23:13:36 -07001206 object_ref(new_target);
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001207 *child = new_target;
Peter Crosthwaite8ffad852014-06-05 23:13:36 -07001208 object_unref(old_target);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001209}
1210
Paolo Bonzini64607d02014-06-05 13:11:51 +02001211static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part)
1212{
1213 LinkProperty *lprop = opaque;
1214
1215 return *lprop->child;
1216}
1217
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001218static void object_release_link_property(Object *obj, const char *name,
1219 void *opaque)
1220{
1221 LinkProperty *prop = opaque;
1222
1223 if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) {
1224 object_unref(*prop->child);
1225 }
1226 g_free(prop);
1227}
1228
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001229void object_property_add_link(Object *obj, const char *name,
1230 const char *type, Object **child,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001231 void (*check)(Object *, const char *,
1232 Object *, Error **),
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001233 ObjectPropertyLinkFlags flags,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001234 Error **errp)
1235{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001236 Error *local_err = NULL;
1237 LinkProperty *prop = g_malloc(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001238 gchar *full_type;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001239 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001240
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001241 prop->child = child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001242 prop->check = check;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001243 prop->flags = flags;
1244
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001245 full_type = g_strdup_printf("link<%s>", type);
1246
Paolo Bonzini64607d02014-06-05 13:11:51 +02001247 op = object_property_add(obj, name, full_type,
1248 object_get_link_property,
1249 check ? object_set_link_property : NULL,
1250 object_release_link_property,
1251 prop,
1252 &local_err);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001253 if (local_err) {
1254 error_propagate(errp, local_err);
1255 g_free(prop);
Paolo Bonzini64607d02014-06-05 13:11:51 +02001256 goto out;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001257 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001258
Paolo Bonzini64607d02014-06-05 13:11:51 +02001259 op->resolve = object_resolve_link_property;
1260
1261out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001262 g_free(full_type);
1263}
1264
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001265gchar *object_get_canonical_path_component(Object *obj)
1266{
1267 ObjectProperty *prop = NULL;
1268
1269 g_assert(obj);
1270 g_assert(obj->parent != NULL);
1271
1272 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
1273 if (!object_property_is_child(prop)) {
1274 continue;
1275 }
1276
1277 if (prop->opaque == obj) {
1278 return g_strdup(prop->name);
1279 }
1280 }
1281
1282 /* obj had a parent but was not a child, should never happen */
1283 g_assert_not_reached();
1284 return NULL;
1285}
1286
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001287gchar *object_get_canonical_path(Object *obj)
1288{
1289 Object *root = object_get_root();
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001290 char *newpath, *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001291
1292 while (obj != root) {
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001293 char *component = object_get_canonical_path_component(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001294
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001295 if (path) {
1296 newpath = g_strdup_printf("%s/%s", component, path);
1297 g_free(component);
1298 g_free(path);
1299 path = newpath;
1300 } else {
1301 path = component;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001302 }
1303
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001304 obj = obj->parent;
1305 }
1306
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001307 newpath = g_strdup_printf("/%s", path ? path : "");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001308 g_free(path);
1309
1310 return newpath;
1311}
1312
Andreas Färber3e84b482013-01-15 02:55:10 +01001313Object *object_resolve_path_component(Object *parent, const gchar *part)
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001314{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001315 ObjectProperty *prop = object_property_find(parent, part, NULL);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001316 if (prop == NULL) {
1317 return NULL;
1318 }
1319
Paolo Bonzini64607d02014-06-05 13:11:51 +02001320 if (prop->resolve) {
1321 return prop->resolve(parent, prop->opaque, part);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001322 } else {
1323 return NULL;
1324 }
1325}
1326
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001327static Object *object_resolve_abs_path(Object *parent,
1328 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001329 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001330 int index)
1331{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001332 Object *child;
1333
1334 if (parts[index] == NULL) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001335 return object_dynamic_cast(parent, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001336 }
1337
1338 if (strcmp(parts[index], "") == 0) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001339 return object_resolve_abs_path(parent, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001340 }
1341
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001342 child = object_resolve_path_component(parent, parts[index]);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001343 if (!child) {
1344 return NULL;
1345 }
1346
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001347 return object_resolve_abs_path(child, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001348}
1349
1350static Object *object_resolve_partial_path(Object *parent,
1351 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001352 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001353 bool *ambiguous)
1354{
1355 Object *obj;
1356 ObjectProperty *prop;
1357
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001358 obj = object_resolve_abs_path(parent, parts, typename, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001359
1360 QTAILQ_FOREACH(prop, &parent->properties, node) {
1361 Object *found;
1362
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001363 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001364 continue;
1365 }
1366
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001367 found = object_resolve_partial_path(prop->opaque, parts,
1368 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001369 if (found) {
1370 if (obj) {
1371 if (ambiguous) {
1372 *ambiguous = true;
1373 }
1374 return NULL;
1375 }
1376 obj = found;
1377 }
1378
1379 if (ambiguous && *ambiguous) {
1380 return NULL;
1381 }
1382 }
1383
1384 return obj;
1385}
1386
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001387Object *object_resolve_path_type(const char *path, const char *typename,
1388 bool *ambiguous)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001389{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001390 Object *obj;
1391 gchar **parts;
1392
1393 parts = g_strsplit(path, "/", 0);
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001394 assert(parts);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001395
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001396 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001397 if (ambiguous) {
1398 *ambiguous = false;
1399 }
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001400 obj = object_resolve_partial_path(object_get_root(), parts,
1401 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001402 } else {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001403 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001404 }
1405
1406 g_strfreev(parts);
1407
1408 return obj;
1409}
1410
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001411Object *object_resolve_path(const char *path, bool *ambiguous)
1412{
1413 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1414}
1415
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001416typedef struct StringProperty
1417{
1418 char *(*get)(Object *, Error **);
1419 void (*set)(Object *, const char *, Error **);
1420} StringProperty;
1421
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001422static void property_get_str(Object *obj, Visitor *v, void *opaque,
1423 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001424{
1425 StringProperty *prop = opaque;
1426 char *value;
1427
1428 value = prop->get(obj, errp);
1429 if (value) {
1430 visit_type_str(v, &value, name, errp);
1431 g_free(value);
1432 }
1433}
1434
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001435static void property_set_str(Object *obj, Visitor *v, void *opaque,
1436 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001437{
1438 StringProperty *prop = opaque;
1439 char *value;
1440 Error *local_err = NULL;
1441
1442 visit_type_str(v, &value, name, &local_err);
1443 if (local_err) {
1444 error_propagate(errp, local_err);
1445 return;
1446 }
1447
1448 prop->set(obj, value, errp);
1449 g_free(value);
1450}
1451
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001452static void property_release_str(Object *obj, const char *name,
1453 void *opaque)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001454{
1455 StringProperty *prop = opaque;
1456 g_free(prop);
1457}
1458
1459void object_property_add_str(Object *obj, const char *name,
1460 char *(*get)(Object *, Error **),
1461 void (*set)(Object *, const char *, Error **),
1462 Error **errp)
1463{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001464 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001465 StringProperty *prop = g_malloc0(sizeof(*prop));
1466
1467 prop->get = get;
1468 prop->set = set;
1469
1470 object_property_add(obj, name, "string",
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001471 get ? property_get_str : NULL,
1472 set ? property_set_str : NULL,
1473 property_release_str,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001474 prop, &local_err);
1475 if (local_err) {
1476 error_propagate(errp, local_err);
1477 g_free(prop);
1478 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001479}
Paolo Bonzini745549c2012-03-31 16:45:54 +02001480
Anthony Liguori0e558842012-06-25 10:32:46 -05001481typedef struct BoolProperty
1482{
1483 bool (*get)(Object *, Error **);
1484 void (*set)(Object *, bool, Error **);
1485} BoolProperty;
1486
1487static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1488 const char *name, Error **errp)
1489{
1490 BoolProperty *prop = opaque;
1491 bool value;
1492
1493 value = prop->get(obj, errp);
1494 visit_type_bool(v, &value, name, errp);
1495}
1496
1497static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1498 const char *name, Error **errp)
1499{
1500 BoolProperty *prop = opaque;
1501 bool value;
1502 Error *local_err = NULL;
1503
1504 visit_type_bool(v, &value, name, &local_err);
1505 if (local_err) {
1506 error_propagate(errp, local_err);
1507 return;
1508 }
1509
1510 prop->set(obj, value, errp);
1511}
1512
1513static void property_release_bool(Object *obj, const char *name,
1514 void *opaque)
1515{
1516 BoolProperty *prop = opaque;
1517 g_free(prop);
1518}
1519
1520void object_property_add_bool(Object *obj, const char *name,
1521 bool (*get)(Object *, Error **),
1522 void (*set)(Object *, bool, Error **),
1523 Error **errp)
1524{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001525 Error *local_err = NULL;
Anthony Liguori0e558842012-06-25 10:32:46 -05001526 BoolProperty *prop = g_malloc0(sizeof(*prop));
1527
1528 prop->get = get;
1529 prop->set = set;
1530
1531 object_property_add(obj, name, "bool",
1532 get ? property_get_bool : NULL,
1533 set ? property_set_bool : NULL,
1534 property_release_bool,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001535 prop, &local_err);
1536 if (local_err) {
1537 error_propagate(errp, local_err);
1538 g_free(prop);
1539 }
Anthony Liguori0e558842012-06-25 10:32:46 -05001540}
1541
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001542static char *qdev_get_type(Object *obj, Error **errp)
1543{
1544 return g_strdup(object_get_typename(obj));
1545}
1546
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03001547static void property_get_uint8_ptr(Object *obj, Visitor *v,
1548 void *opaque, const char *name,
1549 Error **errp)
1550{
1551 uint8_t value = *(uint8_t *)opaque;
1552 visit_type_uint8(v, &value, name, errp);
1553}
1554
1555static void property_get_uint16_ptr(Object *obj, Visitor *v,
1556 void *opaque, const char *name,
1557 Error **errp)
1558{
1559 uint16_t value = *(uint16_t *)opaque;
1560 visit_type_uint16(v, &value, name, errp);
1561}
1562
1563static void property_get_uint32_ptr(Object *obj, Visitor *v,
1564 void *opaque, const char *name,
1565 Error **errp)
1566{
1567 uint32_t value = *(uint32_t *)opaque;
1568 visit_type_uint32(v, &value, name, errp);
1569}
1570
1571static void property_get_uint64_ptr(Object *obj, Visitor *v,
1572 void *opaque, const char *name,
1573 Error **errp)
1574{
1575 uint64_t value = *(uint64_t *)opaque;
1576 visit_type_uint64(v, &value, name, errp);
1577}
1578
1579void object_property_add_uint8_ptr(Object *obj, const char *name,
1580 const uint8_t *v, Error **errp)
1581{
1582 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
1583 NULL, NULL, (void *)v, errp);
1584}
1585
1586void object_property_add_uint16_ptr(Object *obj, const char *name,
1587 const uint16_t *v, Error **errp)
1588{
1589 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
1590 NULL, NULL, (void *)v, errp);
1591}
1592
1593void object_property_add_uint32_ptr(Object *obj, const char *name,
1594 const uint32_t *v, Error **errp)
1595{
1596 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
1597 NULL, NULL, (void *)v, errp);
1598}
1599
1600void object_property_add_uint64_ptr(Object *obj, const char *name,
1601 const uint64_t *v, Error **errp)
1602{
1603 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
1604 NULL, NULL, (void *)v, errp);
1605}
1606
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001607typedef struct {
1608 Object *target_obj;
1609 const char *target_name;
1610} AliasProperty;
1611
1612static void property_get_alias(Object *obj, struct Visitor *v, void *opaque,
1613 const char *name, Error **errp)
1614{
1615 AliasProperty *prop = opaque;
1616
1617 object_property_get(prop->target_obj, v, prop->target_name, errp);
1618}
1619
1620static void property_set_alias(Object *obj, struct Visitor *v, void *opaque,
1621 const char *name, Error **errp)
1622{
1623 AliasProperty *prop = opaque;
1624
1625 object_property_set(prop->target_obj, v, prop->target_name, errp);
1626}
1627
Paolo Bonzini64607d02014-06-05 13:11:51 +02001628static Object *property_resolve_alias(Object *obj, void *opaque,
1629 const gchar *part)
1630{
1631 AliasProperty *prop = opaque;
1632
1633 return object_resolve_path_component(prop->target_obj, prop->target_name);
1634}
1635
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001636static void property_release_alias(Object *obj, const char *name, void *opaque)
1637{
1638 AliasProperty *prop = opaque;
1639
1640 g_free(prop);
1641}
1642
1643void object_property_add_alias(Object *obj, const char *name,
1644 Object *target_obj, const char *target_name,
1645 Error **errp)
1646{
1647 AliasProperty *prop;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001648 ObjectProperty *op;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001649 ObjectProperty *target_prop;
Paolo Bonzinid1906982014-06-10 11:17:35 +02001650 gchar *prop_type;
Gonglei8ae9a9e2014-09-27 13:13:56 +08001651 Error *local_err = NULL;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001652
1653 target_prop = object_property_find(target_obj, target_name, errp);
1654 if (!target_prop) {
1655 return;
1656 }
1657
Paolo Bonzinid1906982014-06-10 11:17:35 +02001658 if (object_property_is_child(target_prop)) {
1659 prop_type = g_strdup_printf("link%s",
1660 target_prop->type + strlen("child"));
1661 } else {
1662 prop_type = g_strdup(target_prop->type);
1663 }
1664
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001665 prop = g_malloc(sizeof(*prop));
1666 prop->target_obj = target_obj;
1667 prop->target_name = target_name;
1668
Paolo Bonzinid1906982014-06-10 11:17:35 +02001669 op = object_property_add(obj, name, prop_type,
Paolo Bonzini64607d02014-06-05 13:11:51 +02001670 property_get_alias,
1671 property_set_alias,
1672 property_release_alias,
Gonglei8ae9a9e2014-09-27 13:13:56 +08001673 prop, &local_err);
1674 if (local_err) {
1675 error_propagate(errp, local_err);
1676 g_free(prop);
1677 goto out;
1678 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02001679 op->resolve = property_resolve_alias;
Paolo Bonzinid1906982014-06-10 11:17:35 +02001680
Gonglei80742642014-10-07 14:33:21 +08001681 object_property_set_description(obj, name,
1682 target_prop->description,
1683 &error_abort);
1684
Gonglei8ae9a9e2014-09-27 13:13:56 +08001685out:
Paolo Bonzinid1906982014-06-10 11:17:35 +02001686 g_free(prop_type);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001687}
1688
Gonglei80742642014-10-07 14:33:21 +08001689void object_property_set_description(Object *obj, const char *name,
1690 const char *description, Error **errp)
1691{
1692 ObjectProperty *op;
1693
1694 op = object_property_find(obj, name, errp);
1695 if (!op) {
1696 return;
1697 }
1698
1699 g_free(op->description);
1700 op->description = g_strdup(description);
1701}
1702
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001703static void object_instance_init(Object *obj)
1704{
1705 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1706}
1707
Paolo Bonzini745549c2012-03-31 16:45:54 +02001708static void register_types(void)
1709{
1710 static TypeInfo interface_info = {
1711 .name = TYPE_INTERFACE,
Anthony Liguori33e95c62012-08-10 13:16:10 +10001712 .class_size = sizeof(InterfaceClass),
Paolo Bonzini745549c2012-03-31 16:45:54 +02001713 .abstract = true,
1714 };
1715
1716 static TypeInfo object_info = {
1717 .name = TYPE_OBJECT,
1718 .instance_size = sizeof(Object),
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001719 .instance_init = object_instance_init,
Paolo Bonzini745549c2012-03-31 16:45:54 +02001720 .abstract = true,
1721 };
1722
Paolo Bonzini049cb3c2012-04-04 15:58:40 +02001723 type_interface = type_register_internal(&interface_info);
1724 type_register_internal(&object_info);
Paolo Bonzini745549c2012-03-31 16:45:54 +02001725}
1726
1727type_init(register_types)