blob: f4de619b7b503e3f738a99d4c224fbf2103d8fd7 [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"
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +010016#include "qapi/string-input-visitor.h"
17#include "qapi/string-output-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010018#include "qapi/qmp/qerror.h"
Paolo Bonzinifa131d92013-05-10 14:16:39 +020019#include "trace.h"
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060020
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +010021/* TODO: replace QObject with a simpler visitor to avoid a dependency
22 * of the QOM core on QObject? */
Paolo Bonzini14cccb62012-12-17 18:19:50 +010023#include "qom/qom-qobject.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010024#include "qapi/qmp/qobject.h"
25#include "qapi/qmp/qbool.h"
26#include "qapi/qmp/qint.h"
27#include "qapi/qmp/qstring.h"
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +010028
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060029#define MAX_INTERFACES 32
30
31typedef struct InterfaceImpl InterfaceImpl;
32typedef struct TypeImpl TypeImpl;
33
34struct InterfaceImpl
35{
Anthony Liguori33e95c62012-08-10 13:16:10 +100036 const char *typename;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060037};
38
39struct TypeImpl
40{
41 const char *name;
42
43 size_t class_size;
44
45 size_t instance_size;
46
47 void (*class_init)(ObjectClass *klass, void *data);
Paolo Bonzini3b50e312012-05-02 13:30:55 +020048 void (*class_base_init)(ObjectClass *klass, void *data);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060049 void (*class_finalize)(ObjectClass *klass, void *data);
50
51 void *class_data;
52
53 void (*instance_init)(Object *obj);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -030054 void (*instance_post_init)(Object *obj);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060055 void (*instance_finalize)(Object *obj);
56
57 bool abstract;
58
59 const char *parent;
60 TypeImpl *parent_type;
61
62 ObjectClass *class;
63
64 int num_interfaces;
65 InterfaceImpl interfaces[MAX_INTERFACES];
66};
67
Paolo Bonzini9970bd82012-02-03 11:51:39 +010068static Type type_interface;
69
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060070static GHashTable *type_table_get(void)
71{
72 static GHashTable *type_table;
73
74 if (type_table == NULL) {
75 type_table = g_hash_table_new(g_str_hash, g_str_equal);
76 }
77
78 return type_table;
79}
80
Hervé Poussineauf54c19c2013-12-03 16:42:00 +010081static bool enumerating_types;
82
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060083static void type_table_add(TypeImpl *ti)
84{
Hervé Poussineauf54c19c2013-12-03 16:42:00 +010085 assert(!enumerating_types);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060086 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
87}
88
89static TypeImpl *type_table_lookup(const char *name)
90{
91 return g_hash_table_lookup(type_table_get(), name);
92}
93
Paolo Bonzinib061dc42013-12-03 16:41:59 +010094static TypeImpl *type_new(const TypeInfo *info)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060095{
96 TypeImpl *ti = g_malloc0(sizeof(*ti));
Anthony Liguori33e95c62012-08-10 13:16:10 +100097 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060098
99 g_assert(info->name != NULL);
100
Anthony Liguori73093352012-01-25 13:37:36 -0600101 if (type_table_lookup(info->name) != NULL) {
102 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
103 abort();
104 }
105
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600106 ti->name = g_strdup(info->name);
107 ti->parent = g_strdup(info->parent);
108
109 ti->class_size = info->class_size;
110 ti->instance_size = info->instance_size;
111
112 ti->class_init = info->class_init;
Paolo Bonzini3b50e312012-05-02 13:30:55 +0200113 ti->class_base_init = info->class_base_init;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600114 ti->class_finalize = info->class_finalize;
115 ti->class_data = info->class_data;
116
117 ti->instance_init = info->instance_init;
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300118 ti->instance_post_init = info->instance_post_init;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600119 ti->instance_finalize = info->instance_finalize;
120
121 ti->abstract = info->abstract;
122
Anthony Liguori33e95c62012-08-10 13:16:10 +1000123 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
124 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600125 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000126 ti->num_interfaces = i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600127
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100128 return ti;
129}
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600130
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100131static TypeImpl *type_register_internal(const TypeInfo *info)
132{
133 TypeImpl *ti;
134 ti = type_new(info);
135
136 type_table_add(ti);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600137 return ti;
138}
139
Paolo Bonzini049cb3c2012-04-04 15:58:40 +0200140TypeImpl *type_register(const TypeInfo *info)
141{
142 assert(info->parent);
143 return type_register_internal(info);
144}
145
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600146TypeImpl *type_register_static(const TypeInfo *info)
147{
148 return type_register(info);
149}
150
151static TypeImpl *type_get_by_name(const char *name)
152{
153 if (name == NULL) {
154 return NULL;
155 }
156
157 return type_table_lookup(name);
158}
159
160static TypeImpl *type_get_parent(TypeImpl *type)
161{
162 if (!type->parent_type && type->parent) {
163 type->parent_type = type_get_by_name(type->parent);
164 g_assert(type->parent_type != NULL);
165 }
166
167 return type->parent_type;
168}
169
170static bool type_has_parent(TypeImpl *type)
171{
172 return (type->parent != NULL);
173}
174
175static size_t type_class_get_size(TypeImpl *ti)
176{
177 if (ti->class_size) {
178 return ti->class_size;
179 }
180
181 if (type_has_parent(ti)) {
182 return type_class_get_size(type_get_parent(ti));
183 }
184
185 return sizeof(ObjectClass);
186}
187
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400188static size_t type_object_get_size(TypeImpl *ti)
189{
190 if (ti->instance_size) {
191 return ti->instance_size;
192 }
193
194 if (type_has_parent(ti)) {
195 return type_object_get_size(type_get_parent(ti));
196 }
197
198 return 0;
199}
200
Anthony Liguori33e95c62012-08-10 13:16:10 +1000201static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600202{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000203 assert(target_type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600204
Anthony Liguori33e95c62012-08-10 13:16:10 +1000205 /* Check if typename is a direct ancestor of type */
206 while (type) {
207 if (type == target_type) {
208 return true;
209 }
210
211 type = type_get_parent(type);
212 }
213
214 return false;
215}
216
217static void type_initialize(TypeImpl *ti);
218
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100219static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
220 TypeImpl *parent_type)
Anthony Liguori33e95c62012-08-10 13:16:10 +1000221{
222 InterfaceClass *new_iface;
223 TypeInfo info = { };
224 TypeImpl *iface_impl;
225
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100226 info.parent = parent_type->name;
227 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000228 info.abstract = true;
229
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100230 iface_impl = type_new(&info);
231 iface_impl->parent_type = parent_type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000232 type_initialize(iface_impl);
233 g_free((char *)info.name);
234
235 new_iface = (InterfaceClass *)iface_impl->class;
236 new_iface->concrete_class = ti->class;
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100237 new_iface->interface_type = interface_type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000238
239 ti->class->interfaces = g_slist_append(ti->class->interfaces,
240 iface_impl->class);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600241}
242
Igor Mitsyankoac451032012-02-28 15:57:11 +0400243static void type_initialize(TypeImpl *ti)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600244{
Paolo Bonzini745549c2012-03-31 16:45:54 +0200245 TypeImpl *parent;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600246
247 if (ti->class) {
248 return;
249 }
250
251 ti->class_size = type_class_get_size(ti);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400252 ti->instance_size = type_object_get_size(ti);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600253
254 ti->class = g_malloc0(ti->class_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600255
Paolo Bonzini745549c2012-03-31 16:45:54 +0200256 parent = type_get_parent(ti);
257 if (parent) {
Igor Mitsyankoac451032012-02-28 15:57:11 +0400258 type_initialize(parent);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000259 GSList *e;
260 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600261
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600262 g_assert(parent->class_size <= ti->class_size);
Paolo Bonzini745549c2012-03-31 16:45:54 +0200263 memcpy(ti->class, parent->class, parent->class_size);
Peter Crosthwaite3e407de2013-02-19 14:02:09 +1000264 ti->class->interfaces = NULL;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000265
266 for (e = parent->class->interfaces; e; e = e->next) {
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100267 InterfaceClass *iface = e->data;
268 ObjectClass *klass = OBJECT_CLASS(iface);
269
270 type_initialize_interface(ti, iface->interface_type, klass->type);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000271 }
272
273 for (i = 0; i < ti->num_interfaces; i++) {
274 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
275 for (e = ti->class->interfaces; e; e = e->next) {
276 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
277
278 if (type_is_ancestor(target_type, t)) {
279 break;
280 }
281 }
282
283 if (e) {
284 continue;
285 }
286
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100287 type_initialize_interface(ti, t, t);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000288 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600289 }
290
Paolo Bonzini745549c2012-03-31 16:45:54 +0200291 ti->class->type = ti;
292
293 while (parent) {
294 if (parent->class_base_init) {
295 parent->class_base_init(ti->class, ti->class_data);
296 }
297 parent = type_get_parent(parent);
298 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600299
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600300 if (ti->class_init) {
301 ti->class_init(ti->class, ti->class_data);
302 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600303}
304
305static void object_init_with_type(Object *obj, TypeImpl *ti)
306{
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600307 if (type_has_parent(ti)) {
308 object_init_with_type(obj, type_get_parent(ti));
309 }
310
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600311 if (ti->instance_init) {
312 ti->instance_init(obj);
313 }
314}
315
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300316static void object_post_init_with_type(Object *obj, TypeImpl *ti)
317{
318 if (ti->instance_post_init) {
319 ti->instance_post_init(obj);
320 }
321
322 if (type_has_parent(ti)) {
323 object_post_init_with_type(obj, type_get_parent(ti));
324 }
325}
326
Andreas Färber5b9237f2013-08-30 18:28:37 +0200327void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600328{
329 Object *obj = data;
330
331 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400332 type_initialize(type);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400333
334 g_assert(type->instance_size >= sizeof(Object));
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600335 g_assert(type->abstract == false);
Andreas Färber5b9237f2013-08-30 18:28:37 +0200336 g_assert(size >= type->instance_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600337
338 memset(obj, 0, type->instance_size);
339 obj->class = type->class;
Paolo Bonzini764b6312012-11-23 09:47:12 +0100340 object_ref(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600341 QTAILQ_INIT(&obj->properties);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600342 object_init_with_type(obj, type);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300343 object_post_init_with_type(obj, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600344}
345
Andreas Färber213f0c42013-08-23 19:37:12 +0200346void object_initialize(void *data, size_t size, const char *typename)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600347{
348 TypeImpl *type = type_get_by_name(typename);
349
Andreas Färber5b9237f2013-08-30 18:28:37 +0200350 object_initialize_with_type(data, size, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600351}
352
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200353static inline bool object_property_is_child(ObjectProperty *prop)
354{
355 return strstart(prop->type, "child<", NULL);
356}
357
358static inline bool object_property_is_link(ObjectProperty *prop)
359{
360 return strstart(prop->type, "link<", NULL);
361}
362
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600363static void object_property_del_all(Object *obj)
364{
365 while (!QTAILQ_EMPTY(&obj->properties)) {
366 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
367
368 QTAILQ_REMOVE(&obj->properties, prop, node);
369
370 if (prop->release) {
371 prop->release(obj, prop->name, prop->opaque);
372 }
373
374 g_free(prop->name);
375 g_free(prop->type);
376 g_free(prop);
377 }
378}
379
380static void object_property_del_child(Object *obj, Object *child, Error **errp)
381{
382 ObjectProperty *prop;
383
384 QTAILQ_FOREACH(prop, &obj->properties, node) {
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200385 if (object_property_is_child(prop) && prop->opaque == child) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600386 object_property_del(obj, prop->name, errp);
Paolo Bonzini6c1fdcf2012-02-28 09:54:15 +0100387 break;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600388 }
389 }
390}
391
392void object_unparent(Object *obj)
393{
Paolo Bonzinie0a83fc2013-04-02 15:50:00 +0200394 if (!obj->parent) {
395 return;
396 }
397
Paolo Bonzini52e636c2013-01-25 14:12:30 +0100398 object_ref(obj);
Paolo Bonzini667d22d2012-11-23 09:47:13 +0100399 if (obj->class->unparent) {
400 (obj->class->unparent)(obj);
401 }
Michael S. Tsirkine998fa82013-03-18 21:01:37 +0200402 if (obj->parent) {
403 object_property_del_child(obj->parent, obj, NULL);
404 }
Paolo Bonzini52e636c2013-01-25 14:12:30 +0100405 object_unref(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600406}
407
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600408static void object_deinit(Object *obj, TypeImpl *type)
409{
410 if (type->instance_finalize) {
411 type->instance_finalize(obj);
412 }
413
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600414 if (type_has_parent(type)) {
415 object_deinit(obj, type_get_parent(type));
416 }
417}
418
Paolo Bonzini339c2702012-11-23 09:47:16 +0100419static void object_finalize(void *data)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600420{
421 Object *obj = data;
422 TypeImpl *ti = obj->class->type;
423
424 object_deinit(obj, ti);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600425 object_property_del_all(obj);
Anthony Liguoridb85b572011-12-23 08:47:39 -0600426
427 g_assert(obj->ref == 0);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100428 if (obj->free) {
429 obj->free(obj);
430 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600431}
432
433Object *object_new_with_type(Type type)
434{
435 Object *obj;
436
437 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400438 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600439
440 obj = g_malloc(type->instance_size);
Andreas Färber5b9237f2013-08-30 18:28:37 +0200441 object_initialize_with_type(obj, type->instance_size, type);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100442 obj->free = g_free;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600443
444 return obj;
445}
446
447Object *object_new(const char *typename)
448{
449 TypeImpl *ti = type_get_by_name(typename);
450
451 return object_new_with_type(ti);
452}
453
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600454Object *object_dynamic_cast(Object *obj, const char *typename)
455{
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100456 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100457 return obj;
458 }
459
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600460 return NULL;
461}
462
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200463Object *object_dynamic_cast_assert(Object *obj, const char *typename,
464 const char *file, int line, const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600465{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200466 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
467 typename, file, line, func);
468
Paolo Bonzini3556c232013-05-10 14:16:40 +0200469#ifdef CONFIG_QOM_CAST_DEBUG
Anthony Liguori03587322013-05-13 15:22:24 -0500470 int i;
471 Object *inst;
472
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000473 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800474 if (obj->class->object_cast_cache[i] == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500475 goto out;
476 }
477 }
478
479 inst = object_dynamic_cast(obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600480
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100481 if (!inst && obj) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200482 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
483 file, line, func, obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600484 abort();
485 }
486
Paolo Bonzini3556c232013-05-10 14:16:40 +0200487 assert(obj == inst);
Anthony Liguori03587322013-05-13 15:22:24 -0500488
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000489 if (obj && obj == inst) {
Anthony Liguori03587322013-05-13 15:22:24 -0500490 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800491 obj->class->object_cast_cache[i - 1] =
492 obj->class->object_cast_cache[i];
Anthony Liguori03587322013-05-13 15:22:24 -0500493 }
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800494 obj->class->object_cast_cache[i - 1] = typename;
Anthony Liguori03587322013-05-13 15:22:24 -0500495 }
496
497out:
Paolo Bonzini3556c232013-05-10 14:16:40 +0200498#endif
499 return obj;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600500}
501
502ObjectClass *object_class_dynamic_cast(ObjectClass *class,
503 const char *typename)
504{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000505 ObjectClass *ret = NULL;
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200506 TypeImpl *target_type;
507 TypeImpl *type;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600508
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200509 if (!class) {
510 return NULL;
511 }
512
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200513 /* A simple fast path that can trigger a lot for leaf classes. */
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200514 type = class->type;
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200515 if (type->name == typename) {
516 return class;
517 }
518
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200519 target_type = type_get_by_name(typename);
Alexander Graf9ab880b2013-04-30 15:02:16 +0200520 if (!target_type) {
521 /* target class type unknown, so fail the cast */
522 return NULL;
523 }
524
Peter Crosthwaite00e2cea2013-02-19 14:02:10 +1000525 if (type->class->interfaces &&
526 type_is_ancestor(target_type, type_interface)) {
Anthony Liguori33e95c62012-08-10 13:16:10 +1000527 int found = 0;
528 GSList *i;
529
530 for (i = class->interfaces; i; i = i->next) {
531 ObjectClass *target_class = i->data;
532
533 if (type_is_ancestor(target_class->type, target_type)) {
534 ret = target_class;
535 found++;
536 }
537 }
538
539 /* The match was ambiguous, don't allow a cast */
540 if (found > 1) {
541 ret = NULL;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600542 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000543 } else if (type_is_ancestor(type, target_type)) {
544 ret = class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600545 }
546
Anthony Liguori33e95c62012-08-10 13:16:10 +1000547 return ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600548}
549
550ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200551 const char *typename,
552 const char *file, int line,
553 const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600554{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200555 ObjectClass *ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600556
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200557 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
558 typename, file, line, func);
559
Anthony Liguori03587322013-05-13 15:22:24 -0500560#ifdef CONFIG_QOM_CAST_DEBUG
561 int i;
562
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000563 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800564 if (class->class_cast_cache[i] == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500565 ret = class;
566 goto out;
567 }
568 }
569#else
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000570 if (!class || !class->interfaces) {
Paolo Bonzini3556c232013-05-10 14:16:40 +0200571 return class;
572 }
573#endif
574
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200575 ret = object_class_dynamic_cast(class, typename);
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200576 if (!ret && class) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200577 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
578 file, line, func, class, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600579 abort();
580 }
581
Anthony Liguori03587322013-05-13 15:22:24 -0500582#ifdef CONFIG_QOM_CAST_DEBUG
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000583 if (class && ret == class) {
Anthony Liguori03587322013-05-13 15:22:24 -0500584 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800585 class->class_cast_cache[i - 1] = class->class_cast_cache[i];
Anthony Liguori03587322013-05-13 15:22:24 -0500586 }
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800587 class->class_cast_cache[i - 1] = typename;
Anthony Liguori03587322013-05-13 15:22:24 -0500588 }
589out:
590#endif
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600591 return ret;
592}
593
594const char *object_get_typename(Object *obj)
595{
596 return obj->class->type->name;
597}
598
599ObjectClass *object_get_class(Object *obj)
600{
601 return obj->class;
602}
603
Andreas Färber17862372013-01-23 12:20:18 +0100604bool object_class_is_abstract(ObjectClass *klass)
605{
606 return klass->type->abstract;
607}
608
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600609const char *object_class_get_name(ObjectClass *klass)
610{
611 return klass->type->name;
612}
613
614ObjectClass *object_class_by_name(const char *typename)
615{
616 TypeImpl *type = type_get_by_name(typename);
617
618 if (!type) {
619 return NULL;
620 }
621
Igor Mitsyankoac451032012-02-28 15:57:11 +0400622 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600623
624 return type->class;
625}
626
Paolo Bonzinie7cce672012-05-02 13:30:54 +0200627ObjectClass *object_class_get_parent(ObjectClass *class)
628{
629 TypeImpl *type = type_get_parent(class->type);
630
631 if (!type) {
632 return NULL;
633 }
634
635 type_initialize(type);
636
637 return type->class;
638}
639
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600640typedef struct OCFData
641{
642 void (*fn)(ObjectClass *klass, void *opaque);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600643 const char *implements_type;
644 bool include_abstract;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600645 void *opaque;
646} OCFData;
647
648static void object_class_foreach_tramp(gpointer key, gpointer value,
649 gpointer opaque)
650{
651 OCFData *data = opaque;
652 TypeImpl *type = value;
Anthony Liguori93c511a2011-12-22 14:11:53 -0600653 ObjectClass *k;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600654
Igor Mitsyankoac451032012-02-28 15:57:11 +0400655 type_initialize(type);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600656 k = type->class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600657
Anthony Liguori93c511a2011-12-22 14:11:53 -0600658 if (!data->include_abstract && type->abstract) {
659 return;
660 }
661
662 if (data->implements_type &&
663 !object_class_dynamic_cast(k, data->implements_type)) {
664 return;
665 }
666
667 data->fn(k, data->opaque);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600668}
669
670void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
Anthony Liguori93c511a2011-12-22 14:11:53 -0600671 const char *implements_type, bool include_abstract,
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600672 void *opaque)
673{
Anthony Liguori93c511a2011-12-22 14:11:53 -0600674 OCFData data = { fn, implements_type, include_abstract, opaque };
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600675
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100676 enumerating_types = true;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600677 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100678 enumerating_types = false;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600679}
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600680
Paolo Bonzini32efc532012-04-11 23:30:20 +0200681int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
682 void *opaque)
683{
684 ObjectProperty *prop;
685 int ret = 0;
686
687 QTAILQ_FOREACH(prop, &obj->properties, node) {
688 if (object_property_is_child(prop)) {
689 ret = fn(prop->opaque, opaque);
690 if (ret != 0) {
691 break;
692 }
693 }
694 }
695 return ret;
696}
697
Andreas Färber418ba9e2012-02-25 23:07:34 +0100698static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
699{
700 GSList **list = opaque;
701
702 *list = g_slist_prepend(*list, klass);
703}
704
705GSList *object_class_get_list(const char *implements_type,
706 bool include_abstract)
707{
708 GSList *list = NULL;
709
710 object_class_foreach(object_class_get_list_tramp,
711 implements_type, include_abstract, &list);
712 return list;
713}
714
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600715void object_ref(Object *obj)
716{
Jan Kiszkaf08c03f2013-07-02 11:36:39 +0200717 atomic_inc(&obj->ref);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600718}
719
720void object_unref(Object *obj)
721{
722 g_assert(obj->ref > 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600723
724 /* parent always holds a reference to its children */
Jan Kiszkaf08c03f2013-07-02 11:36:39 +0200725 if (atomic_fetch_dec(&obj->ref) == 1) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600726 object_finalize(obj);
727 }
728}
729
730void object_property_add(Object *obj, const char *name, const char *type,
731 ObjectPropertyAccessor *get,
732 ObjectPropertyAccessor *set,
733 ObjectPropertyRelease *release,
734 void *opaque, Error **errp)
735{
Peter Maydell54852b02013-03-25 13:15:13 +0000736 ObjectProperty *prop;
737
738 QTAILQ_FOREACH(prop, &obj->properties, node) {
739 if (strcmp(prop->name, name) == 0) {
740 error_setg(errp, "attempt to add duplicate property '%s'"
741 " to object (type '%s')", name,
742 object_get_typename(obj));
743 return;
744 }
745 }
746
747 prop = g_malloc0(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600748
749 prop->name = g_strdup(name);
750 prop->type = g_strdup(type);
751
752 prop->get = get;
753 prop->set = set;
754 prop->release = release;
755 prop->opaque = opaque;
756
757 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
758}
759
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200760ObjectProperty *object_property_find(Object *obj, const char *name,
761 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600762{
763 ObjectProperty *prop;
764
765 QTAILQ_FOREACH(prop, &obj->properties, node) {
766 if (strcmp(prop->name, name) == 0) {
767 return prop;
768 }
769 }
770
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200771 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600772 return NULL;
773}
774
775void object_property_del(Object *obj, const char *name, Error **errp)
776{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200777 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600778 if (prop == NULL) {
Anthony Liguori0866aca2011-12-23 15:34:39 -0600779 return;
780 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600781
Anthony Liguori0866aca2011-12-23 15:34:39 -0600782 if (prop->release) {
783 prop->release(obj, name, prop->opaque);
784 }
785
786 QTAILQ_REMOVE(&obj->properties, prop, node);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600787
788 g_free(prop->name);
789 g_free(prop->type);
790 g_free(prop);
791}
792
793void object_property_get(Object *obj, Visitor *v, const char *name,
794 Error **errp)
795{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200796 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600797 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600798 return;
799 }
800
801 if (!prop->get) {
802 error_set(errp, QERR_PERMISSION_DENIED);
803 } else {
804 prop->get(obj, v, prop->opaque, name, errp);
805 }
806}
807
808void object_property_set(Object *obj, Visitor *v, const char *name,
809 Error **errp)
810{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200811 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600812 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600813 return;
814 }
815
816 if (!prop->set) {
817 error_set(errp, QERR_PERMISSION_DENIED);
818 } else {
819 prop->set(obj, v, prop->opaque, name, errp);
820 }
821}
822
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +0100823void object_property_set_str(Object *obj, const char *value,
824 const char *name, Error **errp)
825{
826 QString *qstr = qstring_from_str(value);
827 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
828
829 QDECREF(qstr);
830}
831
832char *object_property_get_str(Object *obj, const char *name,
833 Error **errp)
834{
835 QObject *ret = object_property_get_qobject(obj, name, errp);
836 QString *qstring;
837 char *retval;
838
839 if (!ret) {
840 return NULL;
841 }
842 qstring = qobject_to_qstring(ret);
843 if (!qstring) {
844 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
845 retval = NULL;
846 } else {
847 retval = g_strdup(qstring_get_str(qstring));
848 }
849
850 QDECREF(qstring);
851 return retval;
852}
853
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +0100854void object_property_set_link(Object *obj, Object *value,
855 const char *name, Error **errp)
856{
Vlad Yasevich2d3aa282013-11-15 12:09:47 -0500857 gchar *path = object_get_canonical_path(value);
858 object_property_set_str(obj, path, name, errp);
859 g_free(path);
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +0100860}
861
862Object *object_property_get_link(Object *obj, const char *name,
863 Error **errp)
864{
865 char *str = object_property_get_str(obj, name, errp);
866 Object *target = NULL;
867
868 if (str && *str) {
869 target = object_resolve_path(str, NULL);
870 if (!target) {
871 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
872 }
873 }
874
875 g_free(str);
876 return target;
877}
878
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +0100879void object_property_set_bool(Object *obj, bool value,
880 const char *name, Error **errp)
881{
882 QBool *qbool = qbool_from_int(value);
883 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
884
885 QDECREF(qbool);
886}
887
888bool object_property_get_bool(Object *obj, const char *name,
889 Error **errp)
890{
891 QObject *ret = object_property_get_qobject(obj, name, errp);
892 QBool *qbool;
893 bool retval;
894
895 if (!ret) {
896 return false;
897 }
898 qbool = qobject_to_qbool(ret);
899 if (!qbool) {
900 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
901 retval = false;
902 } else {
903 retval = qbool_get_int(qbool);
904 }
905
906 QDECREF(qbool);
907 return retval;
908}
909
910void object_property_set_int(Object *obj, int64_t value,
911 const char *name, Error **errp)
912{
913 QInt *qint = qint_from_int(value);
914 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
915
916 QDECREF(qint);
917}
918
919int64_t object_property_get_int(Object *obj, const char *name,
920 Error **errp)
921{
922 QObject *ret = object_property_get_qobject(obj, name, errp);
923 QInt *qint;
924 int64_t retval;
925
926 if (!ret) {
927 return -1;
928 }
929 qint = qobject_to_qint(ret);
930 if (!qint) {
931 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
932 retval = -1;
933 } else {
934 retval = qint_get_int(qint);
935 }
936
937 QDECREF(qint);
938 return retval;
939}
940
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +0100941void object_property_parse(Object *obj, const char *string,
942 const char *name, Error **errp)
943{
944 StringInputVisitor *mi;
945 mi = string_input_visitor_new(string);
946 object_property_set(obj, string_input_get_visitor(mi), name, errp);
947
948 string_input_visitor_cleanup(mi);
949}
950
Paolo Bonzini0b7593e2014-02-08 11:01:50 +0100951char *object_property_print(Object *obj, const char *name, bool human,
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +0100952 Error **errp)
953{
954 StringOutputVisitor *mo;
955 char *string;
956
Paolo Bonzini0b7593e2014-02-08 11:01:50 +0100957 mo = string_output_visitor_new(human);
Paolo Bonzini8185bfc2012-05-02 13:31:00 +0200958 object_property_get(obj, string_output_get_visitor(mo), name, errp);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +0100959 string = string_output_get_string(mo);
960 string_output_visitor_cleanup(mo);
961 return string;
962}
963
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600964const char *object_property_get_type(Object *obj, const char *name, Error **errp)
965{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200966 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600967 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600968 return NULL;
969 }
970
971 return prop->type;
972}
973
974Object *object_get_root(void)
975{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600976 static Object *root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600977
Anthony Liguori8b45d442011-12-23 09:08:05 -0600978 if (!root) {
979 root = object_new("container");
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600980 }
981
Anthony Liguori8b45d442011-12-23 09:08:05 -0600982 return root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600983}
984
985static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
986 const char *name, Error **errp)
987{
988 Object *child = opaque;
989 gchar *path;
990
991 path = object_get_canonical_path(child);
992 visit_type_str(v, &path, name, errp);
993 g_free(path);
994}
995
Anthony Liguoridb85b572011-12-23 08:47:39 -0600996static void object_finalize_child_property(Object *obj, const char *name,
997 void *opaque)
998{
999 Object *child = opaque;
1000
1001 object_unref(child);
1002}
1003
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001004void object_property_add_child(Object *obj, const char *name,
1005 Object *child, Error **errp)
1006{
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001007 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001008 gchar *type;
1009
1010 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1011
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001012 object_property_add(obj, name, type, object_get_child_property, NULL,
1013 object_finalize_child_property, child, &local_err);
1014 if (local_err) {
1015 error_propagate(errp, local_err);
1016 goto out;
1017 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001018 object_ref(child);
1019 g_assert(child->parent == NULL);
1020 child->parent = obj;
1021
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001022out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001023 g_free(type);
1024}
1025
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001026void object_property_allow_set_link(Object *obj, const char *name,
1027 Object *val, Error **errp)
1028{
1029 /* Allow the link to be set, always */
1030}
1031
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001032typedef struct {
1033 Object **child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001034 void (*check)(Object *, const char *, Object *, Error **);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001035 ObjectPropertyLinkFlags flags;
1036} LinkProperty;
1037
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001038static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
1039 const char *name, Error **errp)
1040{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001041 LinkProperty *lprop = opaque;
1042 Object **child = lprop->child;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001043 gchar *path;
1044
1045 if (*child) {
1046 path = object_get_canonical_path(*child);
1047 visit_type_str(v, &path, name, errp);
1048 g_free(path);
1049 } else {
1050 path = (gchar *)"";
1051 visit_type_str(v, &path, name, errp);
1052 }
1053}
1054
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001055/*
1056 * object_resolve_link:
1057 *
1058 * Lookup an object and ensure its type matches the link property type. This
1059 * is similar to object_resolve_path() except type verification against the
1060 * link property is performed.
1061 *
1062 * Returns: The matched object or NULL on path lookup failures.
1063 */
1064static Object *object_resolve_link(Object *obj, const char *name,
1065 const char *path, Error **errp)
1066{
1067 const char *type;
1068 gchar *target_type;
1069 bool ambiguous = false;
1070 Object *target;
1071
1072 /* Go from link<FOO> to FOO. */
1073 type = object_property_get_type(obj, name, NULL);
1074 target_type = g_strndup(&type[5], strlen(type) - 6);
1075 target = object_resolve_path_type(path, target_type, &ambiguous);
1076
1077 if (ambiguous) {
1078 error_set(errp, QERR_AMBIGUOUS_PATH, path);
1079 } else if (!target) {
1080 target = object_resolve_path(path, &ambiguous);
1081 if (target || ambiguous) {
1082 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1083 } else {
1084 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
1085 }
1086 target = NULL;
1087 }
1088 g_free(target_type);
1089
1090 return target;
1091}
1092
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001093static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
1094 const char *name, Error **errp)
1095{
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001096 Error *local_err = NULL;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001097 LinkProperty *prop = opaque;
1098 Object **child = prop->child;
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001099 Object *old_target = *child;
1100 Object *new_target = NULL;
1101 char *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001102
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001103 visit_type_str(v, &path, name, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001104
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001105 if (!local_err && strcmp(path, "") != 0) {
1106 new_target = object_resolve_link(obj, name, path, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001107 }
1108
1109 g_free(path);
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001110 if (local_err) {
1111 error_propagate(errp, local_err);
1112 return;
1113 }
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001114
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001115 prop->check(obj, name, new_target, &local_err);
1116 if (local_err) {
1117 error_propagate(errp, local_err);
1118 return;
1119 }
1120
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001121 if (new_target) {
1122 object_ref(new_target);
1123 }
1124 *child = new_target;
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001125 if (old_target != NULL) {
1126 object_unref(old_target);
1127 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001128}
1129
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001130static void object_release_link_property(Object *obj, const char *name,
1131 void *opaque)
1132{
1133 LinkProperty *prop = opaque;
1134
1135 if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) {
1136 object_unref(*prop->child);
1137 }
1138 g_free(prop);
1139}
1140
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001141void object_property_add_link(Object *obj, const char *name,
1142 const char *type, Object **child,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001143 void (*check)(Object *, const char *,
1144 Object *, Error **),
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001145 ObjectPropertyLinkFlags flags,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001146 Error **errp)
1147{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001148 Error *local_err = NULL;
1149 LinkProperty *prop = g_malloc(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001150 gchar *full_type;
1151
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001152 prop->child = child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001153 prop->check = check;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001154 prop->flags = flags;
1155
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001156 full_type = g_strdup_printf("link<%s>", type);
1157
1158 object_property_add(obj, name, full_type,
1159 object_get_link_property,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001160 check ? object_set_link_property : NULL,
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001161 object_release_link_property,
1162 prop,
1163 &local_err);
1164 if (local_err) {
1165 error_propagate(errp, local_err);
1166 g_free(prop);
1167 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001168
1169 g_free(full_type);
1170}
1171
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001172gchar *object_get_canonical_path_component(Object *obj)
1173{
1174 ObjectProperty *prop = NULL;
1175
1176 g_assert(obj);
1177 g_assert(obj->parent != NULL);
1178
1179 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
1180 if (!object_property_is_child(prop)) {
1181 continue;
1182 }
1183
1184 if (prop->opaque == obj) {
1185 return g_strdup(prop->name);
1186 }
1187 }
1188
1189 /* obj had a parent but was not a child, should never happen */
1190 g_assert_not_reached();
1191 return NULL;
1192}
1193
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001194gchar *object_get_canonical_path(Object *obj)
1195{
1196 Object *root = object_get_root();
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001197 char *newpath, *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001198
1199 while (obj != root) {
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001200 char *component = object_get_canonical_path_component(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001201
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001202 if (path) {
1203 newpath = g_strdup_printf("%s/%s", component, path);
1204 g_free(component);
1205 g_free(path);
1206 path = newpath;
1207 } else {
1208 path = component;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001209 }
1210
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001211 obj = obj->parent;
1212 }
1213
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001214 newpath = g_strdup_printf("/%s", path ? path : "");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001215 g_free(path);
1216
1217 return newpath;
1218}
1219
Andreas Färber3e84b482013-01-15 02:55:10 +01001220Object *object_resolve_path_component(Object *parent, const gchar *part)
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001221{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001222 ObjectProperty *prop = object_property_find(parent, part, NULL);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001223 if (prop == NULL) {
1224 return NULL;
1225 }
1226
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001227 if (object_property_is_link(prop)) {
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001228 return *(Object **)prop->opaque;
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001229 } else if (object_property_is_child(prop)) {
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001230 return prop->opaque;
1231 } else {
1232 return NULL;
1233 }
1234}
1235
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001236static Object *object_resolve_abs_path(Object *parent,
1237 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001238 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001239 int index)
1240{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001241 Object *child;
1242
1243 if (parts[index] == NULL) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001244 return object_dynamic_cast(parent, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001245 }
1246
1247 if (strcmp(parts[index], "") == 0) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001248 return object_resolve_abs_path(parent, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001249 }
1250
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001251 child = object_resolve_path_component(parent, parts[index]);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001252 if (!child) {
1253 return NULL;
1254 }
1255
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001256 return object_resolve_abs_path(child, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001257}
1258
1259static Object *object_resolve_partial_path(Object *parent,
1260 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001261 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001262 bool *ambiguous)
1263{
1264 Object *obj;
1265 ObjectProperty *prop;
1266
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001267 obj = object_resolve_abs_path(parent, parts, typename, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001268
1269 QTAILQ_FOREACH(prop, &parent->properties, node) {
1270 Object *found;
1271
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001272 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001273 continue;
1274 }
1275
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001276 found = object_resolve_partial_path(prop->opaque, parts,
1277 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001278 if (found) {
1279 if (obj) {
1280 if (ambiguous) {
1281 *ambiguous = true;
1282 }
1283 return NULL;
1284 }
1285 obj = found;
1286 }
1287
1288 if (ambiguous && *ambiguous) {
1289 return NULL;
1290 }
1291 }
1292
1293 return obj;
1294}
1295
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001296Object *object_resolve_path_type(const char *path, const char *typename,
1297 bool *ambiguous)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001298{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001299 Object *obj;
1300 gchar **parts;
1301
1302 parts = g_strsplit(path, "/", 0);
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001303 assert(parts);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001304
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001305 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001306 if (ambiguous) {
1307 *ambiguous = false;
1308 }
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001309 obj = object_resolve_partial_path(object_get_root(), parts,
1310 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001311 } else {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001312 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001313 }
1314
1315 g_strfreev(parts);
1316
1317 return obj;
1318}
1319
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001320Object *object_resolve_path(const char *path, bool *ambiguous)
1321{
1322 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1323}
1324
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001325typedef struct StringProperty
1326{
1327 char *(*get)(Object *, Error **);
1328 void (*set)(Object *, const char *, Error **);
1329} StringProperty;
1330
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001331static void property_get_str(Object *obj, Visitor *v, void *opaque,
1332 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001333{
1334 StringProperty *prop = opaque;
1335 char *value;
1336
1337 value = prop->get(obj, errp);
1338 if (value) {
1339 visit_type_str(v, &value, name, errp);
1340 g_free(value);
1341 }
1342}
1343
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001344static void property_set_str(Object *obj, Visitor *v, void *opaque,
1345 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001346{
1347 StringProperty *prop = opaque;
1348 char *value;
1349 Error *local_err = NULL;
1350
1351 visit_type_str(v, &value, name, &local_err);
1352 if (local_err) {
1353 error_propagate(errp, local_err);
1354 return;
1355 }
1356
1357 prop->set(obj, value, errp);
1358 g_free(value);
1359}
1360
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001361static void property_release_str(Object *obj, const char *name,
1362 void *opaque)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001363{
1364 StringProperty *prop = opaque;
1365 g_free(prop);
1366}
1367
1368void object_property_add_str(Object *obj, const char *name,
1369 char *(*get)(Object *, Error **),
1370 void (*set)(Object *, const char *, Error **),
1371 Error **errp)
1372{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001373 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001374 StringProperty *prop = g_malloc0(sizeof(*prop));
1375
1376 prop->get = get;
1377 prop->set = set;
1378
1379 object_property_add(obj, name, "string",
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001380 get ? property_get_str : NULL,
1381 set ? property_set_str : NULL,
1382 property_release_str,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001383 prop, &local_err);
1384 if (local_err) {
1385 error_propagate(errp, local_err);
1386 g_free(prop);
1387 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001388}
Paolo Bonzini745549c2012-03-31 16:45:54 +02001389
Anthony Liguori0e558842012-06-25 10:32:46 -05001390typedef struct BoolProperty
1391{
1392 bool (*get)(Object *, Error **);
1393 void (*set)(Object *, bool, Error **);
1394} BoolProperty;
1395
1396static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1397 const char *name, Error **errp)
1398{
1399 BoolProperty *prop = opaque;
1400 bool value;
1401
1402 value = prop->get(obj, errp);
1403 visit_type_bool(v, &value, name, errp);
1404}
1405
1406static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1407 const char *name, Error **errp)
1408{
1409 BoolProperty *prop = opaque;
1410 bool value;
1411 Error *local_err = NULL;
1412
1413 visit_type_bool(v, &value, name, &local_err);
1414 if (local_err) {
1415 error_propagate(errp, local_err);
1416 return;
1417 }
1418
1419 prop->set(obj, value, errp);
1420}
1421
1422static void property_release_bool(Object *obj, const char *name,
1423 void *opaque)
1424{
1425 BoolProperty *prop = opaque;
1426 g_free(prop);
1427}
1428
1429void object_property_add_bool(Object *obj, const char *name,
1430 bool (*get)(Object *, Error **),
1431 void (*set)(Object *, bool, Error **),
1432 Error **errp)
1433{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001434 Error *local_err = NULL;
Anthony Liguori0e558842012-06-25 10:32:46 -05001435 BoolProperty *prop = g_malloc0(sizeof(*prop));
1436
1437 prop->get = get;
1438 prop->set = set;
1439
1440 object_property_add(obj, name, "bool",
1441 get ? property_get_bool : NULL,
1442 set ? property_set_bool : NULL,
1443 property_release_bool,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001444 prop, &local_err);
1445 if (local_err) {
1446 error_propagate(errp, local_err);
1447 g_free(prop);
1448 }
Anthony Liguori0e558842012-06-25 10:32:46 -05001449}
1450
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001451static char *qdev_get_type(Object *obj, Error **errp)
1452{
1453 return g_strdup(object_get_typename(obj));
1454}
1455
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03001456static void property_get_uint8_ptr(Object *obj, Visitor *v,
1457 void *opaque, const char *name,
1458 Error **errp)
1459{
1460 uint8_t value = *(uint8_t *)opaque;
1461 visit_type_uint8(v, &value, name, errp);
1462}
1463
1464static void property_get_uint16_ptr(Object *obj, Visitor *v,
1465 void *opaque, const char *name,
1466 Error **errp)
1467{
1468 uint16_t value = *(uint16_t *)opaque;
1469 visit_type_uint16(v, &value, name, errp);
1470}
1471
1472static void property_get_uint32_ptr(Object *obj, Visitor *v,
1473 void *opaque, const char *name,
1474 Error **errp)
1475{
1476 uint32_t value = *(uint32_t *)opaque;
1477 visit_type_uint32(v, &value, name, errp);
1478}
1479
1480static void property_get_uint64_ptr(Object *obj, Visitor *v,
1481 void *opaque, const char *name,
1482 Error **errp)
1483{
1484 uint64_t value = *(uint64_t *)opaque;
1485 visit_type_uint64(v, &value, name, errp);
1486}
1487
1488void object_property_add_uint8_ptr(Object *obj, const char *name,
1489 const uint8_t *v, Error **errp)
1490{
1491 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
1492 NULL, NULL, (void *)v, errp);
1493}
1494
1495void object_property_add_uint16_ptr(Object *obj, const char *name,
1496 const uint16_t *v, Error **errp)
1497{
1498 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
1499 NULL, NULL, (void *)v, errp);
1500}
1501
1502void object_property_add_uint32_ptr(Object *obj, const char *name,
1503 const uint32_t *v, Error **errp)
1504{
1505 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
1506 NULL, NULL, (void *)v, errp);
1507}
1508
1509void object_property_add_uint64_ptr(Object *obj, const char *name,
1510 const uint64_t *v, Error **errp)
1511{
1512 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
1513 NULL, NULL, (void *)v, errp);
1514}
1515
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001516static void object_instance_init(Object *obj)
1517{
1518 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1519}
1520
Paolo Bonzini745549c2012-03-31 16:45:54 +02001521static void register_types(void)
1522{
1523 static TypeInfo interface_info = {
1524 .name = TYPE_INTERFACE,
Anthony Liguori33e95c62012-08-10 13:16:10 +10001525 .class_size = sizeof(InterfaceClass),
Paolo Bonzini745549c2012-03-31 16:45:54 +02001526 .abstract = true,
1527 };
1528
1529 static TypeInfo object_info = {
1530 .name = TYPE_OBJECT,
1531 .instance_size = sizeof(Object),
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001532 .instance_init = object_instance_init,
Paolo Bonzini745549c2012-03-31 16:45:54 +02001533 .abstract = true,
1534 };
1535
Paolo Bonzini049cb3c2012-04-04 15:58:40 +02001536 type_interface = type_register_internal(&interface_info);
1537 type_register_internal(&object_info);
Paolo Bonzini745549c2012-03-31 16:45:54 +02001538}
1539
1540type_init(register_types)