blob: 7a892ef4a6411890f2660d70caeea3e604bb0ffe [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);
372 g_free(prop);
373 }
374}
375
376static void object_property_del_child(Object *obj, Object *child, Error **errp)
377{
378 ObjectProperty *prop;
379
380 QTAILQ_FOREACH(prop, &obj->properties, node) {
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200381 if (object_property_is_child(prop) && prop->opaque == child) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600382 object_property_del(obj, prop->name, errp);
Paolo Bonzini6c1fdcf2012-02-28 09:54:15 +0100383 break;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600384 }
385 }
386}
387
388void object_unparent(Object *obj)
389{
Paolo Bonzinie0a83fc2013-04-02 15:50:00 +0200390 if (!obj->parent) {
391 return;
392 }
393
Paolo Bonzini52e636c2013-01-25 14:12:30 +0100394 object_ref(obj);
Paolo Bonzini667d22d2012-11-23 09:47:13 +0100395 if (obj->class->unparent) {
396 (obj->class->unparent)(obj);
397 }
Michael S. Tsirkine998fa82013-03-18 21:01:37 +0200398 if (obj->parent) {
399 object_property_del_child(obj->parent, obj, NULL);
400 }
Paolo Bonzini52e636c2013-01-25 14:12:30 +0100401 object_unref(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600402}
403
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600404static void object_deinit(Object *obj, TypeImpl *type)
405{
406 if (type->instance_finalize) {
407 type->instance_finalize(obj);
408 }
409
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600410 if (type_has_parent(type)) {
411 object_deinit(obj, type_get_parent(type));
412 }
413}
414
Paolo Bonzini339c2702012-11-23 09:47:16 +0100415static void object_finalize(void *data)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600416{
417 Object *obj = data;
418 TypeImpl *ti = obj->class->type;
419
420 object_deinit(obj, ti);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600421 object_property_del_all(obj);
Anthony Liguoridb85b572011-12-23 08:47:39 -0600422
423 g_assert(obj->ref == 0);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100424 if (obj->free) {
425 obj->free(obj);
426 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600427}
428
429Object *object_new_with_type(Type type)
430{
431 Object *obj;
432
433 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400434 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600435
436 obj = g_malloc(type->instance_size);
Andreas Färber5b9237f2013-08-30 18:28:37 +0200437 object_initialize_with_type(obj, type->instance_size, type);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100438 obj->free = g_free;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600439
440 return obj;
441}
442
443Object *object_new(const char *typename)
444{
445 TypeImpl *ti = type_get_by_name(typename);
446
447 return object_new_with_type(ti);
448}
449
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600450Object *object_dynamic_cast(Object *obj, const char *typename)
451{
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100452 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100453 return obj;
454 }
455
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600456 return NULL;
457}
458
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200459Object *object_dynamic_cast_assert(Object *obj, const char *typename,
460 const char *file, int line, const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600461{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200462 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
463 typename, file, line, func);
464
Paolo Bonzini3556c232013-05-10 14:16:40 +0200465#ifdef CONFIG_QOM_CAST_DEBUG
Anthony Liguori03587322013-05-13 15:22:24 -0500466 int i;
467 Object *inst;
468
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000469 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800470 if (obj->class->object_cast_cache[i] == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500471 goto out;
472 }
473 }
474
475 inst = object_dynamic_cast(obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600476
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100477 if (!inst && obj) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200478 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
479 file, line, func, obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600480 abort();
481 }
482
Paolo Bonzini3556c232013-05-10 14:16:40 +0200483 assert(obj == inst);
Anthony Liguori03587322013-05-13 15:22:24 -0500484
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000485 if (obj && obj == inst) {
Anthony Liguori03587322013-05-13 15:22:24 -0500486 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800487 obj->class->object_cast_cache[i - 1] =
488 obj->class->object_cast_cache[i];
Anthony Liguori03587322013-05-13 15:22:24 -0500489 }
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800490 obj->class->object_cast_cache[i - 1] = typename;
Anthony Liguori03587322013-05-13 15:22:24 -0500491 }
492
493out:
Paolo Bonzini3556c232013-05-10 14:16:40 +0200494#endif
495 return obj;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600496}
497
498ObjectClass *object_class_dynamic_cast(ObjectClass *class,
499 const char *typename)
500{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000501 ObjectClass *ret = NULL;
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200502 TypeImpl *target_type;
503 TypeImpl *type;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600504
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200505 if (!class) {
506 return NULL;
507 }
508
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200509 /* A simple fast path that can trigger a lot for leaf classes. */
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200510 type = class->type;
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200511 if (type->name == typename) {
512 return class;
513 }
514
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200515 target_type = type_get_by_name(typename);
Alexander Graf9ab880b2013-04-30 15:02:16 +0200516 if (!target_type) {
517 /* target class type unknown, so fail the cast */
518 return NULL;
519 }
520
Peter Crosthwaite00e2cea2013-02-19 14:02:10 +1000521 if (type->class->interfaces &&
522 type_is_ancestor(target_type, type_interface)) {
Anthony Liguori33e95c62012-08-10 13:16:10 +1000523 int found = 0;
524 GSList *i;
525
526 for (i = class->interfaces; i; i = i->next) {
527 ObjectClass *target_class = i->data;
528
529 if (type_is_ancestor(target_class->type, target_type)) {
530 ret = target_class;
531 found++;
532 }
533 }
534
535 /* The match was ambiguous, don't allow a cast */
536 if (found > 1) {
537 ret = NULL;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600538 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000539 } else if (type_is_ancestor(type, target_type)) {
540 ret = class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600541 }
542
Anthony Liguori33e95c62012-08-10 13:16:10 +1000543 return ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600544}
545
546ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200547 const char *typename,
548 const char *file, int line,
549 const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600550{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200551 ObjectClass *ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600552
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200553 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
554 typename, file, line, func);
555
Anthony Liguori03587322013-05-13 15:22:24 -0500556#ifdef CONFIG_QOM_CAST_DEBUG
557 int i;
558
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000559 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800560 if (class->class_cast_cache[i] == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500561 ret = class;
562 goto out;
563 }
564 }
565#else
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000566 if (!class || !class->interfaces) {
Paolo Bonzini3556c232013-05-10 14:16:40 +0200567 return class;
568 }
569#endif
570
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200571 ret = object_class_dynamic_cast(class, typename);
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200572 if (!ret && class) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200573 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
574 file, line, func, class, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600575 abort();
576 }
577
Anthony Liguori03587322013-05-13 15:22:24 -0500578#ifdef CONFIG_QOM_CAST_DEBUG
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000579 if (class && ret == class) {
Anthony Liguori03587322013-05-13 15:22:24 -0500580 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800581 class->class_cast_cache[i - 1] = class->class_cast_cache[i];
Anthony Liguori03587322013-05-13 15:22:24 -0500582 }
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800583 class->class_cast_cache[i - 1] = typename;
Anthony Liguori03587322013-05-13 15:22:24 -0500584 }
585out:
586#endif
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600587 return ret;
588}
589
590const char *object_get_typename(Object *obj)
591{
592 return obj->class->type->name;
593}
594
595ObjectClass *object_get_class(Object *obj)
596{
597 return obj->class;
598}
599
Andreas Färber17862372013-01-23 12:20:18 +0100600bool object_class_is_abstract(ObjectClass *klass)
601{
602 return klass->type->abstract;
603}
604
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600605const char *object_class_get_name(ObjectClass *klass)
606{
607 return klass->type->name;
608}
609
610ObjectClass *object_class_by_name(const char *typename)
611{
612 TypeImpl *type = type_get_by_name(typename);
613
614 if (!type) {
615 return NULL;
616 }
617
Igor Mitsyankoac451032012-02-28 15:57:11 +0400618 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600619
620 return type->class;
621}
622
Paolo Bonzinie7cce672012-05-02 13:30:54 +0200623ObjectClass *object_class_get_parent(ObjectClass *class)
624{
625 TypeImpl *type = type_get_parent(class->type);
626
627 if (!type) {
628 return NULL;
629 }
630
631 type_initialize(type);
632
633 return type->class;
634}
635
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600636typedef struct OCFData
637{
638 void (*fn)(ObjectClass *klass, void *opaque);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600639 const char *implements_type;
640 bool include_abstract;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600641 void *opaque;
642} OCFData;
643
644static void object_class_foreach_tramp(gpointer key, gpointer value,
645 gpointer opaque)
646{
647 OCFData *data = opaque;
648 TypeImpl *type = value;
Anthony Liguori93c511a2011-12-22 14:11:53 -0600649 ObjectClass *k;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600650
Igor Mitsyankoac451032012-02-28 15:57:11 +0400651 type_initialize(type);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600652 k = type->class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600653
Anthony Liguori93c511a2011-12-22 14:11:53 -0600654 if (!data->include_abstract && type->abstract) {
655 return;
656 }
657
658 if (data->implements_type &&
659 !object_class_dynamic_cast(k, data->implements_type)) {
660 return;
661 }
662
663 data->fn(k, data->opaque);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600664}
665
666void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
Anthony Liguori93c511a2011-12-22 14:11:53 -0600667 const char *implements_type, bool include_abstract,
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600668 void *opaque)
669{
Anthony Liguori93c511a2011-12-22 14:11:53 -0600670 OCFData data = { fn, implements_type, include_abstract, opaque };
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600671
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100672 enumerating_types = true;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600673 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100674 enumerating_types = false;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600675}
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600676
Paolo Bonzini32efc532012-04-11 23:30:20 +0200677int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
678 void *opaque)
679{
680 ObjectProperty *prop;
681 int ret = 0;
682
683 QTAILQ_FOREACH(prop, &obj->properties, node) {
684 if (object_property_is_child(prop)) {
685 ret = fn(prop->opaque, opaque);
686 if (ret != 0) {
687 break;
688 }
689 }
690 }
691 return ret;
692}
693
Andreas Färber418ba9e2012-02-25 23:07:34 +0100694static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
695{
696 GSList **list = opaque;
697
698 *list = g_slist_prepend(*list, klass);
699}
700
701GSList *object_class_get_list(const char *implements_type,
702 bool include_abstract)
703{
704 GSList *list = NULL;
705
706 object_class_foreach(object_class_get_list_tramp,
707 implements_type, include_abstract, &list);
708 return list;
709}
710
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600711void object_ref(Object *obj)
712{
Jan Kiszkaf08c03f2013-07-02 11:36:39 +0200713 atomic_inc(&obj->ref);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600714}
715
716void object_unref(Object *obj)
717{
718 g_assert(obj->ref > 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600719
720 /* parent always holds a reference to its children */
Jan Kiszkaf08c03f2013-07-02 11:36:39 +0200721 if (atomic_fetch_dec(&obj->ref) == 1) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600722 object_finalize(obj);
723 }
724}
725
Paolo Bonzini64607d02014-06-05 13:11:51 +0200726ObjectProperty *
727object_property_add(Object *obj, const char *name, const char *type,
728 ObjectPropertyAccessor *get,
729 ObjectPropertyAccessor *set,
730 ObjectPropertyRelease *release,
731 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600732{
Peter Maydell54852b02013-03-25 13:15:13 +0000733 ObjectProperty *prop;
734
735 QTAILQ_FOREACH(prop, &obj->properties, node) {
736 if (strcmp(prop->name, name) == 0) {
737 error_setg(errp, "attempt to add duplicate property '%s'"
738 " to object (type '%s')", name,
739 object_get_typename(obj));
Paolo Bonzini64607d02014-06-05 13:11:51 +0200740 return NULL;
Peter Maydell54852b02013-03-25 13:15:13 +0000741 }
742 }
743
744 prop = g_malloc0(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600745
746 prop->name = g_strdup(name);
747 prop->type = g_strdup(type);
748
749 prop->get = get;
750 prop->set = set;
751 prop->release = release;
752 prop->opaque = opaque;
753
754 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
Paolo Bonzini64607d02014-06-05 13:11:51 +0200755 return prop;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600756}
757
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200758ObjectProperty *object_property_find(Object *obj, const char *name,
759 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600760{
761 ObjectProperty *prop;
762
763 QTAILQ_FOREACH(prop, &obj->properties, node) {
764 if (strcmp(prop->name, name) == 0) {
765 return prop;
766 }
767 }
768
Cole Robinsonf231b882014-03-21 19:42:26 -0400769 error_setg(errp, "Property '.%s' not found", name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600770 return NULL;
771}
772
773void object_property_del(Object *obj, const char *name, Error **errp)
774{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200775 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600776 if (prop == NULL) {
Anthony Liguori0866aca2011-12-23 15:34:39 -0600777 return;
778 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600779
Anthony Liguori0866aca2011-12-23 15:34:39 -0600780 if (prop->release) {
781 prop->release(obj, name, prop->opaque);
782 }
783
784 QTAILQ_REMOVE(&obj->properties, prop, node);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600785
786 g_free(prop->name);
787 g_free(prop->type);
788 g_free(prop);
789}
790
791void object_property_get(Object *obj, Visitor *v, const char *name,
792 Error **errp)
793{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200794 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600795 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600796 return;
797 }
798
799 if (!prop->get) {
800 error_set(errp, QERR_PERMISSION_DENIED);
801 } else {
802 prop->get(obj, v, prop->opaque, name, errp);
803 }
804}
805
806void object_property_set(Object *obj, Visitor *v, const char *name,
807 Error **errp)
808{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200809 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600810 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600811 return;
812 }
813
814 if (!prop->set) {
815 error_set(errp, QERR_PERMISSION_DENIED);
816 } else {
817 prop->set(obj, v, prop->opaque, name, errp);
818 }
819}
820
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +0100821void object_property_set_str(Object *obj, const char *value,
822 const char *name, Error **errp)
823{
824 QString *qstr = qstring_from_str(value);
825 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
826
827 QDECREF(qstr);
828}
829
830char *object_property_get_str(Object *obj, const char *name,
831 Error **errp)
832{
833 QObject *ret = object_property_get_qobject(obj, name, errp);
834 QString *qstring;
835 char *retval;
836
837 if (!ret) {
838 return NULL;
839 }
840 qstring = qobject_to_qstring(ret);
841 if (!qstring) {
842 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
843 retval = NULL;
844 } else {
845 retval = g_strdup(qstring_get_str(qstring));
846 }
847
848 QDECREF(qstring);
849 return retval;
850}
851
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +0100852void object_property_set_link(Object *obj, Object *value,
853 const char *name, Error **errp)
854{
Vlad Yasevich2d3aa282013-11-15 12:09:47 -0500855 gchar *path = object_get_canonical_path(value);
856 object_property_set_str(obj, path, name, errp);
857 g_free(path);
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +0100858}
859
860Object *object_property_get_link(Object *obj, const char *name,
861 Error **errp)
862{
863 char *str = object_property_get_str(obj, name, errp);
864 Object *target = NULL;
865
866 if (str && *str) {
867 target = object_resolve_path(str, NULL);
868 if (!target) {
869 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
870 }
871 }
872
873 g_free(str);
874 return target;
875}
876
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +0100877void object_property_set_bool(Object *obj, bool value,
878 const char *name, Error **errp)
879{
880 QBool *qbool = qbool_from_int(value);
881 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
882
883 QDECREF(qbool);
884}
885
886bool object_property_get_bool(Object *obj, const char *name,
887 Error **errp)
888{
889 QObject *ret = object_property_get_qobject(obj, name, errp);
890 QBool *qbool;
891 bool retval;
892
893 if (!ret) {
894 return false;
895 }
896 qbool = qobject_to_qbool(ret);
897 if (!qbool) {
898 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
899 retval = false;
900 } else {
901 retval = qbool_get_int(qbool);
902 }
903
904 QDECREF(qbool);
905 return retval;
906}
907
908void object_property_set_int(Object *obj, int64_t value,
909 const char *name, Error **errp)
910{
911 QInt *qint = qint_from_int(value);
912 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
913
914 QDECREF(qint);
915}
916
917int64_t object_property_get_int(Object *obj, const char *name,
918 Error **errp)
919{
920 QObject *ret = object_property_get_qobject(obj, name, errp);
921 QInt *qint;
922 int64_t retval;
923
924 if (!ret) {
925 return -1;
926 }
927 qint = qobject_to_qint(ret);
928 if (!qint) {
929 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
930 retval = -1;
931 } else {
932 retval = qint_get_int(qint);
933 }
934
935 QDECREF(qint);
936 return retval;
937}
938
Hu Tao1f217722014-05-14 17:43:33 +0800939int object_property_get_enum(Object *obj, const char *name,
940 const char *strings[], Error **errp)
941{
942 StringOutputVisitor *sov;
943 StringInputVisitor *siv;
944 int ret;
945
946 sov = string_output_visitor_new(false);
947 object_property_get(obj, string_output_get_visitor(sov), name, errp);
948 siv = string_input_visitor_new(string_output_get_string(sov));
949 string_output_visitor_cleanup(sov);
950 visit_type_enum(string_input_get_visitor(siv),
951 &ret, strings, NULL, name, errp);
952 string_input_visitor_cleanup(siv);
953
954 return ret;
955}
956
957void object_property_get_uint16List(Object *obj, const char *name,
958 uint16List **list, Error **errp)
959{
960 StringOutputVisitor *ov;
961 StringInputVisitor *iv;
962
963 ov = string_output_visitor_new(false);
964 object_property_get(obj, string_output_get_visitor(ov),
965 name, errp);
966 iv = string_input_visitor_new(string_output_get_string(ov));
967 visit_type_uint16List(string_input_get_visitor(iv),
968 list, NULL, errp);
969 string_output_visitor_cleanup(ov);
970 string_input_visitor_cleanup(iv);
971}
972
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +0100973void object_property_parse(Object *obj, const char *string,
974 const char *name, Error **errp)
975{
976 StringInputVisitor *mi;
977 mi = string_input_visitor_new(string);
978 object_property_set(obj, string_input_get_visitor(mi), name, errp);
979
980 string_input_visitor_cleanup(mi);
981}
982
Paolo Bonzini0b7593e2014-02-08 11:01:50 +0100983char *object_property_print(Object *obj, const char *name, bool human,
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +0100984 Error **errp)
985{
986 StringOutputVisitor *mo;
987 char *string;
988
Paolo Bonzini0b7593e2014-02-08 11:01:50 +0100989 mo = string_output_visitor_new(human);
Paolo Bonzini8185bfc2012-05-02 13:31:00 +0200990 object_property_get(obj, string_output_get_visitor(mo), name, errp);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +0100991 string = string_output_get_string(mo);
992 string_output_visitor_cleanup(mo);
993 return string;
994}
995
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600996const char *object_property_get_type(Object *obj, const char *name, Error **errp)
997{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200998 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600999 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001000 return NULL;
1001 }
1002
1003 return prop->type;
1004}
1005
1006Object *object_get_root(void)
1007{
Anthony Liguori8b45d442011-12-23 09:08:05 -06001008 static Object *root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001009
Anthony Liguori8b45d442011-12-23 09:08:05 -06001010 if (!root) {
1011 root = object_new("container");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001012 }
1013
Anthony Liguori8b45d442011-12-23 09:08:05 -06001014 return root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001015}
1016
1017static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
1018 const char *name, Error **errp)
1019{
1020 Object *child = opaque;
1021 gchar *path;
1022
1023 path = object_get_canonical_path(child);
1024 visit_type_str(v, &path, name, errp);
1025 g_free(path);
1026}
1027
Paolo Bonzini64607d02014-06-05 13:11:51 +02001028static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part)
1029{
1030 return opaque;
1031}
1032
Anthony Liguoridb85b572011-12-23 08:47:39 -06001033static void object_finalize_child_property(Object *obj, const char *name,
1034 void *opaque)
1035{
1036 Object *child = opaque;
1037
1038 object_unref(child);
1039}
1040
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001041void object_property_add_child(Object *obj, const char *name,
1042 Object *child, Error **errp)
1043{
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001044 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001045 gchar *type;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001046 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001047
1048 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1049
Paolo Bonzini64607d02014-06-05 13:11:51 +02001050 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1051 object_finalize_child_property, child, &local_err);
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001052 if (local_err) {
1053 error_propagate(errp, local_err);
1054 goto out;
1055 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02001056
1057 op->resolve = object_resolve_child_property;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001058 object_ref(child);
1059 g_assert(child->parent == NULL);
1060 child->parent = obj;
1061
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001062out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001063 g_free(type);
1064}
1065
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001066void object_property_allow_set_link(Object *obj, const char *name,
1067 Object *val, Error **errp)
1068{
1069 /* Allow the link to be set, always */
1070}
1071
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001072typedef struct {
1073 Object **child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001074 void (*check)(Object *, const char *, Object *, Error **);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001075 ObjectPropertyLinkFlags flags;
1076} LinkProperty;
1077
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001078static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
1079 const char *name, Error **errp)
1080{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001081 LinkProperty *lprop = opaque;
1082 Object **child = lprop->child;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001083 gchar *path;
1084
1085 if (*child) {
1086 path = object_get_canonical_path(*child);
1087 visit_type_str(v, &path, name, errp);
1088 g_free(path);
1089 } else {
1090 path = (gchar *)"";
1091 visit_type_str(v, &path, name, errp);
1092 }
1093}
1094
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001095/*
1096 * object_resolve_link:
1097 *
1098 * Lookup an object and ensure its type matches the link property type. This
1099 * is similar to object_resolve_path() except type verification against the
1100 * link property is performed.
1101 *
1102 * Returns: The matched object or NULL on path lookup failures.
1103 */
1104static Object *object_resolve_link(Object *obj, const char *name,
1105 const char *path, Error **errp)
1106{
1107 const char *type;
1108 gchar *target_type;
1109 bool ambiguous = false;
1110 Object *target;
1111
1112 /* Go from link<FOO> to FOO. */
1113 type = object_property_get_type(obj, name, NULL);
1114 target_type = g_strndup(&type[5], strlen(type) - 6);
1115 target = object_resolve_path_type(path, target_type, &ambiguous);
1116
1117 if (ambiguous) {
Cole Robinsonf231b882014-03-21 19:42:26 -04001118 error_set(errp, ERROR_CLASS_GENERIC_ERROR,
1119 "Path '%s' does not uniquely identify an object", path);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001120 } else if (!target) {
1121 target = object_resolve_path(path, &ambiguous);
1122 if (target || ambiguous) {
1123 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1124 } else {
1125 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
1126 }
1127 target = NULL;
1128 }
1129 g_free(target_type);
1130
1131 return target;
1132}
1133
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001134static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
1135 const char *name, Error **errp)
1136{
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001137 Error *local_err = NULL;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001138 LinkProperty *prop = opaque;
1139 Object **child = prop->child;
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001140 Object *old_target = *child;
1141 Object *new_target = NULL;
1142 char *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001143
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001144 visit_type_str(v, &path, name, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001145
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001146 if (!local_err && strcmp(path, "") != 0) {
1147 new_target = object_resolve_link(obj, name, path, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001148 }
1149
1150 g_free(path);
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001151 if (local_err) {
1152 error_propagate(errp, local_err);
1153 return;
1154 }
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001155
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001156 prop->check(obj, name, new_target, &local_err);
1157 if (local_err) {
1158 error_propagate(errp, local_err);
1159 return;
1160 }
1161
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001162 if (new_target) {
1163 object_ref(new_target);
1164 }
1165 *child = new_target;
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001166 if (old_target != NULL) {
1167 object_unref(old_target);
1168 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001169}
1170
Paolo Bonzini64607d02014-06-05 13:11:51 +02001171static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part)
1172{
1173 LinkProperty *lprop = opaque;
1174
1175 return *lprop->child;
1176}
1177
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001178static void object_release_link_property(Object *obj, const char *name,
1179 void *opaque)
1180{
1181 LinkProperty *prop = opaque;
1182
1183 if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) {
1184 object_unref(*prop->child);
1185 }
1186 g_free(prop);
1187}
1188
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001189void object_property_add_link(Object *obj, const char *name,
1190 const char *type, Object **child,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001191 void (*check)(Object *, const char *,
1192 Object *, Error **),
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001193 ObjectPropertyLinkFlags flags,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001194 Error **errp)
1195{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001196 Error *local_err = NULL;
1197 LinkProperty *prop = g_malloc(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001198 gchar *full_type;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001199 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001200
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001201 prop->child = child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001202 prop->check = check;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001203 prop->flags = flags;
1204
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001205 full_type = g_strdup_printf("link<%s>", type);
1206
Paolo Bonzini64607d02014-06-05 13:11:51 +02001207 op = object_property_add(obj, name, full_type,
1208 object_get_link_property,
1209 check ? object_set_link_property : NULL,
1210 object_release_link_property,
1211 prop,
1212 &local_err);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001213 if (local_err) {
1214 error_propagate(errp, local_err);
1215 g_free(prop);
Paolo Bonzini64607d02014-06-05 13:11:51 +02001216 goto out;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001217 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001218
Paolo Bonzini64607d02014-06-05 13:11:51 +02001219 op->resolve = object_resolve_link_property;
1220
1221out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001222 g_free(full_type);
1223}
1224
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001225gchar *object_get_canonical_path_component(Object *obj)
1226{
1227 ObjectProperty *prop = NULL;
1228
1229 g_assert(obj);
1230 g_assert(obj->parent != NULL);
1231
1232 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
1233 if (!object_property_is_child(prop)) {
1234 continue;
1235 }
1236
1237 if (prop->opaque == obj) {
1238 return g_strdup(prop->name);
1239 }
1240 }
1241
1242 /* obj had a parent but was not a child, should never happen */
1243 g_assert_not_reached();
1244 return NULL;
1245}
1246
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001247gchar *object_get_canonical_path(Object *obj)
1248{
1249 Object *root = object_get_root();
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001250 char *newpath, *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001251
1252 while (obj != root) {
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001253 char *component = object_get_canonical_path_component(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001254
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001255 if (path) {
1256 newpath = g_strdup_printf("%s/%s", component, path);
1257 g_free(component);
1258 g_free(path);
1259 path = newpath;
1260 } else {
1261 path = component;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001262 }
1263
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001264 obj = obj->parent;
1265 }
1266
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001267 newpath = g_strdup_printf("/%s", path ? path : "");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001268 g_free(path);
1269
1270 return newpath;
1271}
1272
Andreas Färber3e84b482013-01-15 02:55:10 +01001273Object *object_resolve_path_component(Object *parent, const gchar *part)
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001274{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001275 ObjectProperty *prop = object_property_find(parent, part, NULL);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001276 if (prop == NULL) {
1277 return NULL;
1278 }
1279
Paolo Bonzini64607d02014-06-05 13:11:51 +02001280 if (prop->resolve) {
1281 return prop->resolve(parent, prop->opaque, part);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001282 } else {
1283 return NULL;
1284 }
1285}
1286
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001287static Object *object_resolve_abs_path(Object *parent,
1288 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001289 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001290 int index)
1291{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001292 Object *child;
1293
1294 if (parts[index] == NULL) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001295 return object_dynamic_cast(parent, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001296 }
1297
1298 if (strcmp(parts[index], "") == 0) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001299 return object_resolve_abs_path(parent, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001300 }
1301
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001302 child = object_resolve_path_component(parent, parts[index]);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001303 if (!child) {
1304 return NULL;
1305 }
1306
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001307 return object_resolve_abs_path(child, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001308}
1309
1310static Object *object_resolve_partial_path(Object *parent,
1311 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001312 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001313 bool *ambiguous)
1314{
1315 Object *obj;
1316 ObjectProperty *prop;
1317
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001318 obj = object_resolve_abs_path(parent, parts, typename, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001319
1320 QTAILQ_FOREACH(prop, &parent->properties, node) {
1321 Object *found;
1322
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001323 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001324 continue;
1325 }
1326
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001327 found = object_resolve_partial_path(prop->opaque, parts,
1328 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001329 if (found) {
1330 if (obj) {
1331 if (ambiguous) {
1332 *ambiguous = true;
1333 }
1334 return NULL;
1335 }
1336 obj = found;
1337 }
1338
1339 if (ambiguous && *ambiguous) {
1340 return NULL;
1341 }
1342 }
1343
1344 return obj;
1345}
1346
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001347Object *object_resolve_path_type(const char *path, const char *typename,
1348 bool *ambiguous)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001349{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001350 Object *obj;
1351 gchar **parts;
1352
1353 parts = g_strsplit(path, "/", 0);
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001354 assert(parts);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001355
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001356 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001357 if (ambiguous) {
1358 *ambiguous = false;
1359 }
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001360 obj = object_resolve_partial_path(object_get_root(), parts,
1361 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001362 } else {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001363 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001364 }
1365
1366 g_strfreev(parts);
1367
1368 return obj;
1369}
1370
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001371Object *object_resolve_path(const char *path, bool *ambiguous)
1372{
1373 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1374}
1375
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001376typedef struct StringProperty
1377{
1378 char *(*get)(Object *, Error **);
1379 void (*set)(Object *, const char *, Error **);
1380} StringProperty;
1381
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001382static void property_get_str(Object *obj, Visitor *v, void *opaque,
1383 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001384{
1385 StringProperty *prop = opaque;
1386 char *value;
1387
1388 value = prop->get(obj, errp);
1389 if (value) {
1390 visit_type_str(v, &value, name, errp);
1391 g_free(value);
1392 }
1393}
1394
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001395static void property_set_str(Object *obj, Visitor *v, void *opaque,
1396 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001397{
1398 StringProperty *prop = opaque;
1399 char *value;
1400 Error *local_err = NULL;
1401
1402 visit_type_str(v, &value, name, &local_err);
1403 if (local_err) {
1404 error_propagate(errp, local_err);
1405 return;
1406 }
1407
1408 prop->set(obj, value, errp);
1409 g_free(value);
1410}
1411
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001412static void property_release_str(Object *obj, const char *name,
1413 void *opaque)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001414{
1415 StringProperty *prop = opaque;
1416 g_free(prop);
1417}
1418
1419void object_property_add_str(Object *obj, const char *name,
1420 char *(*get)(Object *, Error **),
1421 void (*set)(Object *, const char *, Error **),
1422 Error **errp)
1423{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001424 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001425 StringProperty *prop = g_malloc0(sizeof(*prop));
1426
1427 prop->get = get;
1428 prop->set = set;
1429
1430 object_property_add(obj, name, "string",
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001431 get ? property_get_str : NULL,
1432 set ? property_set_str : NULL,
1433 property_release_str,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001434 prop, &local_err);
1435 if (local_err) {
1436 error_propagate(errp, local_err);
1437 g_free(prop);
1438 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001439}
Paolo Bonzini745549c2012-03-31 16:45:54 +02001440
Anthony Liguori0e558842012-06-25 10:32:46 -05001441typedef struct BoolProperty
1442{
1443 bool (*get)(Object *, Error **);
1444 void (*set)(Object *, bool, Error **);
1445} BoolProperty;
1446
1447static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1448 const char *name, Error **errp)
1449{
1450 BoolProperty *prop = opaque;
1451 bool value;
1452
1453 value = prop->get(obj, errp);
1454 visit_type_bool(v, &value, name, errp);
1455}
1456
1457static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1458 const char *name, Error **errp)
1459{
1460 BoolProperty *prop = opaque;
1461 bool value;
1462 Error *local_err = NULL;
1463
1464 visit_type_bool(v, &value, name, &local_err);
1465 if (local_err) {
1466 error_propagate(errp, local_err);
1467 return;
1468 }
1469
1470 prop->set(obj, value, errp);
1471}
1472
1473static void property_release_bool(Object *obj, const char *name,
1474 void *opaque)
1475{
1476 BoolProperty *prop = opaque;
1477 g_free(prop);
1478}
1479
1480void object_property_add_bool(Object *obj, const char *name,
1481 bool (*get)(Object *, Error **),
1482 void (*set)(Object *, bool, Error **),
1483 Error **errp)
1484{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001485 Error *local_err = NULL;
Anthony Liguori0e558842012-06-25 10:32:46 -05001486 BoolProperty *prop = g_malloc0(sizeof(*prop));
1487
1488 prop->get = get;
1489 prop->set = set;
1490
1491 object_property_add(obj, name, "bool",
1492 get ? property_get_bool : NULL,
1493 set ? property_set_bool : NULL,
1494 property_release_bool,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001495 prop, &local_err);
1496 if (local_err) {
1497 error_propagate(errp, local_err);
1498 g_free(prop);
1499 }
Anthony Liguori0e558842012-06-25 10:32:46 -05001500}
1501
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001502static char *qdev_get_type(Object *obj, Error **errp)
1503{
1504 return g_strdup(object_get_typename(obj));
1505}
1506
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03001507static void property_get_uint8_ptr(Object *obj, Visitor *v,
1508 void *opaque, const char *name,
1509 Error **errp)
1510{
1511 uint8_t value = *(uint8_t *)opaque;
1512 visit_type_uint8(v, &value, name, errp);
1513}
1514
1515static void property_get_uint16_ptr(Object *obj, Visitor *v,
1516 void *opaque, const char *name,
1517 Error **errp)
1518{
1519 uint16_t value = *(uint16_t *)opaque;
1520 visit_type_uint16(v, &value, name, errp);
1521}
1522
1523static void property_get_uint32_ptr(Object *obj, Visitor *v,
1524 void *opaque, const char *name,
1525 Error **errp)
1526{
1527 uint32_t value = *(uint32_t *)opaque;
1528 visit_type_uint32(v, &value, name, errp);
1529}
1530
1531static void property_get_uint64_ptr(Object *obj, Visitor *v,
1532 void *opaque, const char *name,
1533 Error **errp)
1534{
1535 uint64_t value = *(uint64_t *)opaque;
1536 visit_type_uint64(v, &value, name, errp);
1537}
1538
1539void object_property_add_uint8_ptr(Object *obj, const char *name,
1540 const uint8_t *v, Error **errp)
1541{
1542 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
1543 NULL, NULL, (void *)v, errp);
1544}
1545
1546void object_property_add_uint16_ptr(Object *obj, const char *name,
1547 const uint16_t *v, Error **errp)
1548{
1549 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
1550 NULL, NULL, (void *)v, errp);
1551}
1552
1553void object_property_add_uint32_ptr(Object *obj, const char *name,
1554 const uint32_t *v, Error **errp)
1555{
1556 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
1557 NULL, NULL, (void *)v, errp);
1558}
1559
1560void object_property_add_uint64_ptr(Object *obj, const char *name,
1561 const uint64_t *v, Error **errp)
1562{
1563 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
1564 NULL, NULL, (void *)v, errp);
1565}
1566
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001567typedef struct {
1568 Object *target_obj;
1569 const char *target_name;
1570} AliasProperty;
1571
1572static void property_get_alias(Object *obj, struct Visitor *v, void *opaque,
1573 const char *name, Error **errp)
1574{
1575 AliasProperty *prop = opaque;
1576
1577 object_property_get(prop->target_obj, v, prop->target_name, errp);
1578}
1579
1580static void property_set_alias(Object *obj, struct Visitor *v, void *opaque,
1581 const char *name, Error **errp)
1582{
1583 AliasProperty *prop = opaque;
1584
1585 object_property_set(prop->target_obj, v, prop->target_name, errp);
1586}
1587
Paolo Bonzini64607d02014-06-05 13:11:51 +02001588static Object *property_resolve_alias(Object *obj, void *opaque,
1589 const gchar *part)
1590{
1591 AliasProperty *prop = opaque;
1592
1593 return object_resolve_path_component(prop->target_obj, prop->target_name);
1594}
1595
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001596static void property_release_alias(Object *obj, const char *name, void *opaque)
1597{
1598 AliasProperty *prop = opaque;
1599
1600 g_free(prop);
1601}
1602
1603void object_property_add_alias(Object *obj, const char *name,
1604 Object *target_obj, const char *target_name,
1605 Error **errp)
1606{
1607 AliasProperty *prop;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001608 ObjectProperty *op;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001609 ObjectProperty *target_prop;
1610
1611 target_prop = object_property_find(target_obj, target_name, errp);
1612 if (!target_prop) {
1613 return;
1614 }
1615
1616 prop = g_malloc(sizeof(*prop));
1617 prop->target_obj = target_obj;
1618 prop->target_name = target_name;
1619
Paolo Bonzini64607d02014-06-05 13:11:51 +02001620 op = object_property_add(obj, name, target_prop->type,
1621 property_get_alias,
1622 property_set_alias,
1623 property_release_alias,
1624 prop, errp);
1625 op->resolve = property_resolve_alias;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001626}
1627
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001628static void object_instance_init(Object *obj)
1629{
1630 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1631}
1632
Paolo Bonzini745549c2012-03-31 16:45:54 +02001633static void register_types(void)
1634{
1635 static TypeInfo interface_info = {
1636 .name = TYPE_INTERFACE,
Anthony Liguori33e95c62012-08-10 13:16:10 +10001637 .class_size = sizeof(InterfaceClass),
Paolo Bonzini745549c2012-03-31 16:45:54 +02001638 .abstract = true,
1639 };
1640
1641 static TypeInfo object_info = {
1642 .name = TYPE_OBJECT,
1643 .instance_size = sizeof(Object),
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001644 .instance_init = object_instance_init,
Paolo Bonzini745549c2012-03-31 16:45:54 +02001645 .abstract = true,
1646 };
1647
Paolo Bonzini049cb3c2012-04-04 15:58:40 +02001648 type_interface = type_register_internal(&interface_info);
1649 type_register_internal(&object_info);
Paolo Bonzini745549c2012-03-31 16:45:54 +02001650}
1651
1652type_init(register_types)