blob: 96abd347b19527e32782eda5b44ef4af48c30b51 [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
Peter Crosthwaite8faa2f82014-09-25 22:19:52 -07001092 if (child->parent != NULL) {
1093 error_setg(errp, "child object is already parented");
1094 return;
1095 }
1096
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001097 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1098
Paolo Bonzini64607d02014-06-05 13:11:51 +02001099 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1100 object_finalize_child_property, child, &local_err);
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001101 if (local_err) {
1102 error_propagate(errp, local_err);
1103 goto out;
1104 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02001105
1106 op->resolve = object_resolve_child_property;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001107 object_ref(child);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001108 child->parent = obj;
1109
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001110out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001111 g_free(type);
1112}
1113
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001114void object_property_allow_set_link(Object *obj, const char *name,
1115 Object *val, Error **errp)
1116{
1117 /* Allow the link to be set, always */
1118}
1119
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001120typedef struct {
1121 Object **child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001122 void (*check)(Object *, const char *, Object *, Error **);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001123 ObjectPropertyLinkFlags flags;
1124} LinkProperty;
1125
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001126static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
1127 const char *name, Error **errp)
1128{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001129 LinkProperty *lprop = opaque;
1130 Object **child = lprop->child;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001131 gchar *path;
1132
1133 if (*child) {
1134 path = object_get_canonical_path(*child);
1135 visit_type_str(v, &path, name, errp);
1136 g_free(path);
1137 } else {
1138 path = (gchar *)"";
1139 visit_type_str(v, &path, name, errp);
1140 }
1141}
1142
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001143/*
1144 * object_resolve_link:
1145 *
1146 * Lookup an object and ensure its type matches the link property type. This
1147 * is similar to object_resolve_path() except type verification against the
1148 * link property is performed.
1149 *
1150 * Returns: The matched object or NULL on path lookup failures.
1151 */
1152static Object *object_resolve_link(Object *obj, const char *name,
1153 const char *path, Error **errp)
1154{
1155 const char *type;
1156 gchar *target_type;
1157 bool ambiguous = false;
1158 Object *target;
1159
1160 /* Go from link<FOO> to FOO. */
1161 type = object_property_get_type(obj, name, NULL);
1162 target_type = g_strndup(&type[5], strlen(type) - 6);
1163 target = object_resolve_path_type(path, target_type, &ambiguous);
1164
1165 if (ambiguous) {
Cole Robinsonf231b882014-03-21 19:42:26 -04001166 error_set(errp, ERROR_CLASS_GENERIC_ERROR,
1167 "Path '%s' does not uniquely identify an object", path);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001168 } else if (!target) {
1169 target = object_resolve_path(path, &ambiguous);
1170 if (target || ambiguous) {
1171 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1172 } else {
1173 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
1174 }
1175 target = NULL;
1176 }
1177 g_free(target_type);
1178
1179 return target;
1180}
1181
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001182static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
1183 const char *name, Error **errp)
1184{
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001185 Error *local_err = NULL;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001186 LinkProperty *prop = opaque;
1187 Object **child = prop->child;
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001188 Object *old_target = *child;
1189 Object *new_target = NULL;
1190 char *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001191
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001192 visit_type_str(v, &path, name, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001193
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001194 if (!local_err && strcmp(path, "") != 0) {
1195 new_target = object_resolve_link(obj, name, path, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001196 }
1197
1198 g_free(path);
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001199 if (local_err) {
1200 error_propagate(errp, local_err);
1201 return;
1202 }
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001203
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001204 prop->check(obj, name, new_target, &local_err);
1205 if (local_err) {
1206 error_propagate(errp, local_err);
1207 return;
1208 }
1209
Peter Crosthwaite8ffad852014-06-05 23:13:36 -07001210 object_ref(new_target);
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001211 *child = new_target;
Peter Crosthwaite8ffad852014-06-05 23:13:36 -07001212 object_unref(old_target);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001213}
1214
Paolo Bonzini64607d02014-06-05 13:11:51 +02001215static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part)
1216{
1217 LinkProperty *lprop = opaque;
1218
1219 return *lprop->child;
1220}
1221
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001222static void object_release_link_property(Object *obj, const char *name,
1223 void *opaque)
1224{
1225 LinkProperty *prop = opaque;
1226
1227 if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) {
1228 object_unref(*prop->child);
1229 }
1230 g_free(prop);
1231}
1232
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001233void object_property_add_link(Object *obj, const char *name,
1234 const char *type, Object **child,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001235 void (*check)(Object *, const char *,
1236 Object *, Error **),
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001237 ObjectPropertyLinkFlags flags,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001238 Error **errp)
1239{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001240 Error *local_err = NULL;
1241 LinkProperty *prop = g_malloc(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001242 gchar *full_type;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001243 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001244
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001245 prop->child = child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001246 prop->check = check;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001247 prop->flags = flags;
1248
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001249 full_type = g_strdup_printf("link<%s>", type);
1250
Paolo Bonzini64607d02014-06-05 13:11:51 +02001251 op = object_property_add(obj, name, full_type,
1252 object_get_link_property,
1253 check ? object_set_link_property : NULL,
1254 object_release_link_property,
1255 prop,
1256 &local_err);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001257 if (local_err) {
1258 error_propagate(errp, local_err);
1259 g_free(prop);
Paolo Bonzini64607d02014-06-05 13:11:51 +02001260 goto out;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001261 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001262
Paolo Bonzini64607d02014-06-05 13:11:51 +02001263 op->resolve = object_resolve_link_property;
1264
1265out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001266 g_free(full_type);
1267}
1268
Paolo Bonzinifb9e7e32015-05-05 18:29:00 +02001269void object_property_add_const_link(Object *obj, const char *name,
1270 Object *target, Error **errp)
1271{
1272 char *link_type;
1273 ObjectProperty *op;
1274
1275 link_type = g_strdup_printf("link<%s>", object_get_typename(target));
1276 op = object_property_add(obj, name, link_type,
1277 object_get_child_property, NULL,
1278 NULL, target, errp);
1279 if (op != NULL) {
1280 op->resolve = object_resolve_child_property;
1281 }
1282 g_free(link_type);
1283}
1284
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001285gchar *object_get_canonical_path_component(Object *obj)
1286{
1287 ObjectProperty *prop = NULL;
1288
1289 g_assert(obj);
1290 g_assert(obj->parent != NULL);
1291
1292 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
1293 if (!object_property_is_child(prop)) {
1294 continue;
1295 }
1296
1297 if (prop->opaque == obj) {
1298 return g_strdup(prop->name);
1299 }
1300 }
1301
1302 /* obj had a parent but was not a child, should never happen */
1303 g_assert_not_reached();
1304 return NULL;
1305}
1306
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001307gchar *object_get_canonical_path(Object *obj)
1308{
1309 Object *root = object_get_root();
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001310 char *newpath, *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001311
1312 while (obj != root) {
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001313 char *component = object_get_canonical_path_component(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001314
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001315 if (path) {
1316 newpath = g_strdup_printf("%s/%s", component, path);
1317 g_free(component);
1318 g_free(path);
1319 path = newpath;
1320 } else {
1321 path = component;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001322 }
1323
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001324 obj = obj->parent;
1325 }
1326
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001327 newpath = g_strdup_printf("/%s", path ? path : "");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001328 g_free(path);
1329
1330 return newpath;
1331}
1332
Andreas Färber3e84b482013-01-15 02:55:10 +01001333Object *object_resolve_path_component(Object *parent, const gchar *part)
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001334{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001335 ObjectProperty *prop = object_property_find(parent, part, NULL);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001336 if (prop == NULL) {
1337 return NULL;
1338 }
1339
Paolo Bonzini64607d02014-06-05 13:11:51 +02001340 if (prop->resolve) {
1341 return prop->resolve(parent, prop->opaque, part);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001342 } else {
1343 return NULL;
1344 }
1345}
1346
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001347static Object *object_resolve_abs_path(Object *parent,
1348 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001349 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001350 int index)
1351{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001352 Object *child;
1353
1354 if (parts[index] == NULL) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001355 return object_dynamic_cast(parent, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001356 }
1357
1358 if (strcmp(parts[index], "") == 0) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001359 return object_resolve_abs_path(parent, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001360 }
1361
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001362 child = object_resolve_path_component(parent, parts[index]);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001363 if (!child) {
1364 return NULL;
1365 }
1366
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001367 return object_resolve_abs_path(child, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001368}
1369
1370static Object *object_resolve_partial_path(Object *parent,
1371 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001372 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001373 bool *ambiguous)
1374{
1375 Object *obj;
1376 ObjectProperty *prop;
1377
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001378 obj = object_resolve_abs_path(parent, parts, typename, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001379
1380 QTAILQ_FOREACH(prop, &parent->properties, node) {
1381 Object *found;
1382
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001383 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001384 continue;
1385 }
1386
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001387 found = object_resolve_partial_path(prop->opaque, parts,
1388 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001389 if (found) {
1390 if (obj) {
1391 if (ambiguous) {
1392 *ambiguous = true;
1393 }
1394 return NULL;
1395 }
1396 obj = found;
1397 }
1398
1399 if (ambiguous && *ambiguous) {
1400 return NULL;
1401 }
1402 }
1403
1404 return obj;
1405}
1406
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001407Object *object_resolve_path_type(const char *path, const char *typename,
1408 bool *ambiguous)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001409{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001410 Object *obj;
1411 gchar **parts;
1412
1413 parts = g_strsplit(path, "/", 0);
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001414 assert(parts);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001415
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001416 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001417 if (ambiguous) {
1418 *ambiguous = false;
1419 }
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001420 obj = object_resolve_partial_path(object_get_root(), parts,
1421 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001422 } else {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001423 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001424 }
1425
1426 g_strfreev(parts);
1427
1428 return obj;
1429}
1430
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001431Object *object_resolve_path(const char *path, bool *ambiguous)
1432{
1433 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1434}
1435
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001436typedef struct StringProperty
1437{
1438 char *(*get)(Object *, Error **);
1439 void (*set)(Object *, const char *, Error **);
1440} StringProperty;
1441
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001442static void property_get_str(Object *obj, Visitor *v, void *opaque,
1443 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001444{
1445 StringProperty *prop = opaque;
1446 char *value;
1447
1448 value = prop->get(obj, errp);
1449 if (value) {
1450 visit_type_str(v, &value, name, errp);
1451 g_free(value);
1452 }
1453}
1454
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001455static void property_set_str(Object *obj, Visitor *v, void *opaque,
1456 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001457{
1458 StringProperty *prop = opaque;
1459 char *value;
1460 Error *local_err = NULL;
1461
1462 visit_type_str(v, &value, name, &local_err);
1463 if (local_err) {
1464 error_propagate(errp, local_err);
1465 return;
1466 }
1467
1468 prop->set(obj, value, errp);
1469 g_free(value);
1470}
1471
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001472static void property_release_str(Object *obj, const char *name,
1473 void *opaque)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001474{
1475 StringProperty *prop = opaque;
1476 g_free(prop);
1477}
1478
1479void object_property_add_str(Object *obj, const char *name,
1480 char *(*get)(Object *, Error **),
1481 void (*set)(Object *, const char *, Error **),
1482 Error **errp)
1483{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001484 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001485 StringProperty *prop = g_malloc0(sizeof(*prop));
1486
1487 prop->get = get;
1488 prop->set = set;
1489
1490 object_property_add(obj, name, "string",
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001491 get ? property_get_str : NULL,
1492 set ? property_set_str : NULL,
1493 property_release_str,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001494 prop, &local_err);
1495 if (local_err) {
1496 error_propagate(errp, local_err);
1497 g_free(prop);
1498 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001499}
Paolo Bonzini745549c2012-03-31 16:45:54 +02001500
Anthony Liguori0e558842012-06-25 10:32:46 -05001501typedef struct BoolProperty
1502{
1503 bool (*get)(Object *, Error **);
1504 void (*set)(Object *, bool, Error **);
1505} BoolProperty;
1506
1507static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1508 const char *name, Error **errp)
1509{
1510 BoolProperty *prop = opaque;
1511 bool value;
1512
1513 value = prop->get(obj, errp);
1514 visit_type_bool(v, &value, name, errp);
1515}
1516
1517static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1518 const char *name, Error **errp)
1519{
1520 BoolProperty *prop = opaque;
1521 bool value;
1522 Error *local_err = NULL;
1523
1524 visit_type_bool(v, &value, name, &local_err);
1525 if (local_err) {
1526 error_propagate(errp, local_err);
1527 return;
1528 }
1529
1530 prop->set(obj, value, errp);
1531}
1532
1533static void property_release_bool(Object *obj, const char *name,
1534 void *opaque)
1535{
1536 BoolProperty *prop = opaque;
1537 g_free(prop);
1538}
1539
1540void object_property_add_bool(Object *obj, const char *name,
1541 bool (*get)(Object *, Error **),
1542 void (*set)(Object *, bool, Error **),
1543 Error **errp)
1544{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001545 Error *local_err = NULL;
Anthony Liguori0e558842012-06-25 10:32:46 -05001546 BoolProperty *prop = g_malloc0(sizeof(*prop));
1547
1548 prop->get = get;
1549 prop->set = set;
1550
1551 object_property_add(obj, name, "bool",
1552 get ? property_get_bool : NULL,
1553 set ? property_set_bool : NULL,
1554 property_release_bool,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001555 prop, &local_err);
1556 if (local_err) {
1557 error_propagate(errp, local_err);
1558 g_free(prop);
1559 }
Anthony Liguori0e558842012-06-25 10:32:46 -05001560}
1561
David Gibson8e099d12015-02-06 14:55:45 +11001562typedef struct TMProperty {
1563 void (*get)(Object *, struct tm *, Error **);
1564} TMProperty;
1565
1566static void property_get_tm(Object *obj, Visitor *v, void *opaque,
1567 const char *name, Error **errp)
1568{
1569 TMProperty *prop = opaque;
1570 Error *err = NULL;
1571 struct tm value;
1572
1573 prop->get(obj, &value, &err);
1574 if (err) {
1575 goto out;
1576 }
1577
1578 visit_start_struct(v, NULL, "struct tm", name, 0, &err);
1579 if (err) {
1580 goto out;
1581 }
1582 visit_type_int32(v, &value.tm_year, "tm_year", &err);
1583 if (err) {
1584 goto out_end;
1585 }
1586 visit_type_int32(v, &value.tm_mon, "tm_mon", &err);
1587 if (err) {
1588 goto out_end;
1589 }
1590 visit_type_int32(v, &value.tm_mday, "tm_mday", &err);
1591 if (err) {
1592 goto out_end;
1593 }
1594 visit_type_int32(v, &value.tm_hour, "tm_hour", &err);
1595 if (err) {
1596 goto out_end;
1597 }
1598 visit_type_int32(v, &value.tm_min, "tm_min", &err);
1599 if (err) {
1600 goto out_end;
1601 }
1602 visit_type_int32(v, &value.tm_sec, "tm_sec", &err);
1603 if (err) {
1604 goto out_end;
1605 }
1606out_end:
1607 error_propagate(errp, err);
1608 err = NULL;
1609 visit_end_struct(v, errp);
1610out:
1611 error_propagate(errp, err);
1612
1613}
1614
1615static void property_release_tm(Object *obj, const char *name,
1616 void *opaque)
1617{
1618 TMProperty *prop = opaque;
1619 g_free(prop);
1620}
1621
1622void object_property_add_tm(Object *obj, const char *name,
1623 void (*get)(Object *, struct tm *, Error **),
1624 Error **errp)
1625{
1626 Error *local_err = NULL;
1627 TMProperty *prop = g_malloc0(sizeof(*prop));
1628
1629 prop->get = get;
1630
1631 object_property_add(obj, name, "struct tm",
1632 get ? property_get_tm : NULL, NULL,
1633 property_release_tm,
1634 prop, &local_err);
1635 if (local_err) {
1636 error_propagate(errp, local_err);
1637 g_free(prop);
1638 }
1639}
1640
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001641static char *qdev_get_type(Object *obj, Error **errp)
1642{
1643 return g_strdup(object_get_typename(obj));
1644}
1645
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03001646static void property_get_uint8_ptr(Object *obj, Visitor *v,
1647 void *opaque, const char *name,
1648 Error **errp)
1649{
1650 uint8_t value = *(uint8_t *)opaque;
1651 visit_type_uint8(v, &value, name, errp);
1652}
1653
1654static void property_get_uint16_ptr(Object *obj, Visitor *v,
1655 void *opaque, const char *name,
1656 Error **errp)
1657{
1658 uint16_t value = *(uint16_t *)opaque;
1659 visit_type_uint16(v, &value, name, errp);
1660}
1661
1662static void property_get_uint32_ptr(Object *obj, Visitor *v,
1663 void *opaque, const char *name,
1664 Error **errp)
1665{
1666 uint32_t value = *(uint32_t *)opaque;
1667 visit_type_uint32(v, &value, name, errp);
1668}
1669
1670static void property_get_uint64_ptr(Object *obj, Visitor *v,
1671 void *opaque, const char *name,
1672 Error **errp)
1673{
1674 uint64_t value = *(uint64_t *)opaque;
1675 visit_type_uint64(v, &value, name, errp);
1676}
1677
1678void object_property_add_uint8_ptr(Object *obj, const char *name,
1679 const uint8_t *v, Error **errp)
1680{
1681 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
1682 NULL, NULL, (void *)v, errp);
1683}
1684
1685void object_property_add_uint16_ptr(Object *obj, const char *name,
1686 const uint16_t *v, Error **errp)
1687{
1688 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
1689 NULL, NULL, (void *)v, errp);
1690}
1691
1692void object_property_add_uint32_ptr(Object *obj, const char *name,
1693 const uint32_t *v, Error **errp)
1694{
1695 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
1696 NULL, NULL, (void *)v, errp);
1697}
1698
1699void object_property_add_uint64_ptr(Object *obj, const char *name,
1700 const uint64_t *v, Error **errp)
1701{
1702 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
1703 NULL, NULL, (void *)v, errp);
1704}
1705
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001706typedef struct {
1707 Object *target_obj;
1708 const char *target_name;
1709} AliasProperty;
1710
1711static void property_get_alias(Object *obj, struct Visitor *v, void *opaque,
1712 const char *name, Error **errp)
1713{
1714 AliasProperty *prop = opaque;
1715
1716 object_property_get(prop->target_obj, v, prop->target_name, errp);
1717}
1718
1719static void property_set_alias(Object *obj, struct Visitor *v, void *opaque,
1720 const char *name, Error **errp)
1721{
1722 AliasProperty *prop = opaque;
1723
1724 object_property_set(prop->target_obj, v, prop->target_name, errp);
1725}
1726
Paolo Bonzini64607d02014-06-05 13:11:51 +02001727static Object *property_resolve_alias(Object *obj, void *opaque,
1728 const gchar *part)
1729{
1730 AliasProperty *prop = opaque;
1731
1732 return object_resolve_path_component(prop->target_obj, prop->target_name);
1733}
1734
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001735static void property_release_alias(Object *obj, const char *name, void *opaque)
1736{
1737 AliasProperty *prop = opaque;
1738
1739 g_free(prop);
1740}
1741
1742void object_property_add_alias(Object *obj, const char *name,
1743 Object *target_obj, const char *target_name,
1744 Error **errp)
1745{
1746 AliasProperty *prop;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001747 ObjectProperty *op;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001748 ObjectProperty *target_prop;
Paolo Bonzinid1906982014-06-10 11:17:35 +02001749 gchar *prop_type;
Gonglei8ae9a9e2014-09-27 13:13:56 +08001750 Error *local_err = NULL;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001751
1752 target_prop = object_property_find(target_obj, target_name, errp);
1753 if (!target_prop) {
1754 return;
1755 }
1756
Paolo Bonzinid1906982014-06-10 11:17:35 +02001757 if (object_property_is_child(target_prop)) {
1758 prop_type = g_strdup_printf("link%s",
1759 target_prop->type + strlen("child"));
1760 } else {
1761 prop_type = g_strdup(target_prop->type);
1762 }
1763
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001764 prop = g_malloc(sizeof(*prop));
1765 prop->target_obj = target_obj;
1766 prop->target_name = target_name;
1767
Paolo Bonzinid1906982014-06-10 11:17:35 +02001768 op = object_property_add(obj, name, prop_type,
Paolo Bonzini64607d02014-06-05 13:11:51 +02001769 property_get_alias,
1770 property_set_alias,
1771 property_release_alias,
Gonglei8ae9a9e2014-09-27 13:13:56 +08001772 prop, &local_err);
1773 if (local_err) {
1774 error_propagate(errp, local_err);
1775 g_free(prop);
1776 goto out;
1777 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02001778 op->resolve = property_resolve_alias;
Paolo Bonzinid1906982014-06-10 11:17:35 +02001779
Andreas Färbera18bb412015-03-27 17:34:10 +01001780 object_property_set_description(obj, op->name,
Gonglei80742642014-10-07 14:33:21 +08001781 target_prop->description,
1782 &error_abort);
1783
Gonglei8ae9a9e2014-09-27 13:13:56 +08001784out:
Paolo Bonzinid1906982014-06-10 11:17:35 +02001785 g_free(prop_type);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001786}
1787
Gonglei80742642014-10-07 14:33:21 +08001788void object_property_set_description(Object *obj, const char *name,
1789 const char *description, Error **errp)
1790{
1791 ObjectProperty *op;
1792
1793 op = object_property_find(obj, name, errp);
1794 if (!op) {
1795 return;
1796 }
1797
1798 g_free(op->description);
1799 op->description = g_strdup(description);
1800}
1801
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001802static void object_instance_init(Object *obj)
1803{
1804 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1805}
1806
Paolo Bonzini745549c2012-03-31 16:45:54 +02001807static void register_types(void)
1808{
1809 static TypeInfo interface_info = {
1810 .name = TYPE_INTERFACE,
Anthony Liguori33e95c62012-08-10 13:16:10 +10001811 .class_size = sizeof(InterfaceClass),
Paolo Bonzini745549c2012-03-31 16:45:54 +02001812 .abstract = true,
1813 };
1814
1815 static TypeInfo object_info = {
1816 .name = TYPE_OBJECT,
1817 .instance_size = sizeof(Object),
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001818 .instance_init = object_instance_init,
Paolo Bonzini745549c2012-03-31 16:45:54 +02001819 .abstract = true,
1820 };
1821
Paolo Bonzini049cb3c2012-04-04 15:58:40 +02001822 type_interface = type_register_internal(&interface_info);
1823 type_register_internal(&object_info);
Paolo Bonzini745549c2012-03-31 16:45:54 +02001824}
1825
1826type_init(register_types)