blob: 660859c0e7fe6eea39a07c5734d2526a36c0bc5a [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
1026static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
1027 const char *name, Error **errp)
1028{
1029 Object **child = opaque;
1030 gchar *path;
1031
1032 if (*child) {
1033 path = object_get_canonical_path(*child);
1034 visit_type_str(v, &path, name, errp);
1035 g_free(path);
1036 } else {
1037 path = (gchar *)"";
1038 visit_type_str(v, &path, name, errp);
1039 }
1040}
1041
1042static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
1043 const char *name, Error **errp)
1044{
1045 Object **child = opaque;
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001046 Object *old_target;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001047 bool ambiguous = false;
1048 const char *type;
1049 char *path;
Paolo Bonzini11e35bf2012-02-02 12:37:53 +01001050 gchar *target_type;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001051
1052 type = object_property_get_type(obj, name, NULL);
1053
1054 visit_type_str(v, &path, name, errp);
1055
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001056 old_target = *child;
1057 *child = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001058
1059 if (strcmp(path, "") != 0) {
1060 Object *target;
1061
Paolo Bonzini11e35bf2012-02-02 12:37:53 +01001062 /* Go from link<FOO> to FOO. */
1063 target_type = g_strndup(&type[5], strlen(type) - 6);
1064 target = object_resolve_path_type(path, target_type, &ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001065
Paolo Bonzini11e35bf2012-02-02 12:37:53 +01001066 if (ambiguous) {
1067 error_set(errp, QERR_AMBIGUOUS_PATH, path);
1068 } else if (target) {
1069 object_ref(target);
1070 *child = target;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001071 } else {
Paolo Bonzini11e35bf2012-02-02 12:37:53 +01001072 target = object_resolve_path(path, &ambiguous);
1073 if (target || ambiguous) {
1074 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1075 } else {
1076 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
1077 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001078 }
Paolo Bonzini11e35bf2012-02-02 12:37:53 +01001079 g_free(target_type);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001080 }
1081
1082 g_free(path);
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001083
1084 if (old_target != NULL) {
1085 object_unref(old_target);
1086 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001087}
1088
1089void object_property_add_link(Object *obj, const char *name,
1090 const char *type, Object **child,
1091 Error **errp)
1092{
1093 gchar *full_type;
1094
1095 full_type = g_strdup_printf("link<%s>", type);
1096
1097 object_property_add(obj, name, full_type,
1098 object_get_link_property,
1099 object_set_link_property,
1100 NULL, child, errp);
1101
1102 g_free(full_type);
1103}
1104
1105gchar *object_get_canonical_path(Object *obj)
1106{
1107 Object *root = object_get_root();
1108 char *newpath = NULL, *path = NULL;
1109
1110 while (obj != root) {
1111 ObjectProperty *prop = NULL;
1112
1113 g_assert(obj->parent != NULL);
1114
1115 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001116 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001117 continue;
1118 }
1119
1120 if (prop->opaque == obj) {
1121 if (path) {
1122 newpath = g_strdup_printf("%s/%s", prop->name, path);
1123 g_free(path);
1124 path = newpath;
1125 } else {
1126 path = g_strdup(prop->name);
1127 }
1128 break;
1129 }
1130 }
1131
1132 g_assert(prop != NULL);
1133
1134 obj = obj->parent;
1135 }
1136
1137 newpath = g_strdup_printf("/%s", path);
1138 g_free(path);
1139
1140 return newpath;
1141}
1142
Andreas Färber3e84b482013-01-15 02:55:10 +01001143Object *object_resolve_path_component(Object *parent, const gchar *part)
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001144{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001145 ObjectProperty *prop = object_property_find(parent, part, NULL);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001146 if (prop == NULL) {
1147 return NULL;
1148 }
1149
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001150 if (object_property_is_link(prop)) {
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001151 return *(Object **)prop->opaque;
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001152 } else if (object_property_is_child(prop)) {
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001153 return prop->opaque;
1154 } else {
1155 return NULL;
1156 }
1157}
1158
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001159static Object *object_resolve_abs_path(Object *parent,
1160 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001161 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001162 int index)
1163{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001164 Object *child;
1165
1166 if (parts[index] == NULL) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001167 return object_dynamic_cast(parent, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001168 }
1169
1170 if (strcmp(parts[index], "") == 0) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001171 return object_resolve_abs_path(parent, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001172 }
1173
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001174 child = object_resolve_path_component(parent, parts[index]);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001175 if (!child) {
1176 return NULL;
1177 }
1178
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001179 return object_resolve_abs_path(child, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001180}
1181
1182static Object *object_resolve_partial_path(Object *parent,
1183 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001184 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001185 bool *ambiguous)
1186{
1187 Object *obj;
1188 ObjectProperty *prop;
1189
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001190 obj = object_resolve_abs_path(parent, parts, typename, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001191
1192 QTAILQ_FOREACH(prop, &parent->properties, node) {
1193 Object *found;
1194
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001195 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001196 continue;
1197 }
1198
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001199 found = object_resolve_partial_path(prop->opaque, parts,
1200 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001201 if (found) {
1202 if (obj) {
1203 if (ambiguous) {
1204 *ambiguous = true;
1205 }
1206 return NULL;
1207 }
1208 obj = found;
1209 }
1210
1211 if (ambiguous && *ambiguous) {
1212 return NULL;
1213 }
1214 }
1215
1216 return obj;
1217}
1218
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001219Object *object_resolve_path_type(const char *path, const char *typename,
1220 bool *ambiguous)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001221{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001222 Object *obj;
1223 gchar **parts;
1224
1225 parts = g_strsplit(path, "/", 0);
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001226 assert(parts);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001227
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001228 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001229 if (ambiguous) {
1230 *ambiguous = false;
1231 }
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001232 obj = object_resolve_partial_path(object_get_root(), parts,
1233 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001234 } else {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001235 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001236 }
1237
1238 g_strfreev(parts);
1239
1240 return obj;
1241}
1242
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001243Object *object_resolve_path(const char *path, bool *ambiguous)
1244{
1245 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1246}
1247
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001248typedef struct StringProperty
1249{
1250 char *(*get)(Object *, Error **);
1251 void (*set)(Object *, const char *, Error **);
1252} StringProperty;
1253
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001254static void property_get_str(Object *obj, Visitor *v, void *opaque,
1255 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001256{
1257 StringProperty *prop = opaque;
1258 char *value;
1259
1260 value = prop->get(obj, errp);
1261 if (value) {
1262 visit_type_str(v, &value, name, errp);
1263 g_free(value);
1264 }
1265}
1266
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001267static void property_set_str(Object *obj, Visitor *v, void *opaque,
1268 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001269{
1270 StringProperty *prop = opaque;
1271 char *value;
1272 Error *local_err = NULL;
1273
1274 visit_type_str(v, &value, name, &local_err);
1275 if (local_err) {
1276 error_propagate(errp, local_err);
1277 return;
1278 }
1279
1280 prop->set(obj, value, errp);
1281 g_free(value);
1282}
1283
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001284static void property_release_str(Object *obj, const char *name,
1285 void *opaque)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001286{
1287 StringProperty *prop = opaque;
1288 g_free(prop);
1289}
1290
1291void object_property_add_str(Object *obj, const char *name,
1292 char *(*get)(Object *, Error **),
1293 void (*set)(Object *, const char *, Error **),
1294 Error **errp)
1295{
1296 StringProperty *prop = g_malloc0(sizeof(*prop));
1297
1298 prop->get = get;
1299 prop->set = set;
1300
1301 object_property_add(obj, name, "string",
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001302 get ? property_get_str : NULL,
1303 set ? property_set_str : NULL,
1304 property_release_str,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001305 prop, errp);
1306}
Paolo Bonzini745549c2012-03-31 16:45:54 +02001307
Anthony Liguori0e558842012-06-25 10:32:46 -05001308typedef struct BoolProperty
1309{
1310 bool (*get)(Object *, Error **);
1311 void (*set)(Object *, bool, Error **);
1312} BoolProperty;
1313
1314static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1315 const char *name, Error **errp)
1316{
1317 BoolProperty *prop = opaque;
1318 bool value;
1319
1320 value = prop->get(obj, errp);
1321 visit_type_bool(v, &value, name, errp);
1322}
1323
1324static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1325 const char *name, Error **errp)
1326{
1327 BoolProperty *prop = opaque;
1328 bool value;
1329 Error *local_err = NULL;
1330
1331 visit_type_bool(v, &value, name, &local_err);
1332 if (local_err) {
1333 error_propagate(errp, local_err);
1334 return;
1335 }
1336
1337 prop->set(obj, value, errp);
1338}
1339
1340static void property_release_bool(Object *obj, const char *name,
1341 void *opaque)
1342{
1343 BoolProperty *prop = opaque;
1344 g_free(prop);
1345}
1346
1347void object_property_add_bool(Object *obj, const char *name,
1348 bool (*get)(Object *, Error **),
1349 void (*set)(Object *, bool, Error **),
1350 Error **errp)
1351{
1352 BoolProperty *prop = g_malloc0(sizeof(*prop));
1353
1354 prop->get = get;
1355 prop->set = set;
1356
1357 object_property_add(obj, name, "bool",
1358 get ? property_get_bool : NULL,
1359 set ? property_set_bool : NULL,
1360 property_release_bool,
1361 prop, errp);
1362}
1363
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001364static char *qdev_get_type(Object *obj, Error **errp)
1365{
1366 return g_strdup(object_get_typename(obj));
1367}
1368
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03001369static void property_get_uint8_ptr(Object *obj, Visitor *v,
1370 void *opaque, const char *name,
1371 Error **errp)
1372{
1373 uint8_t value = *(uint8_t *)opaque;
1374 visit_type_uint8(v, &value, name, errp);
1375}
1376
1377static void property_get_uint16_ptr(Object *obj, Visitor *v,
1378 void *opaque, const char *name,
1379 Error **errp)
1380{
1381 uint16_t value = *(uint16_t *)opaque;
1382 visit_type_uint16(v, &value, name, errp);
1383}
1384
1385static void property_get_uint32_ptr(Object *obj, Visitor *v,
1386 void *opaque, const char *name,
1387 Error **errp)
1388{
1389 uint32_t value = *(uint32_t *)opaque;
1390 visit_type_uint32(v, &value, name, errp);
1391}
1392
1393static void property_get_uint64_ptr(Object *obj, Visitor *v,
1394 void *opaque, const char *name,
1395 Error **errp)
1396{
1397 uint64_t value = *(uint64_t *)opaque;
1398 visit_type_uint64(v, &value, name, errp);
1399}
1400
1401void object_property_add_uint8_ptr(Object *obj, const char *name,
1402 const uint8_t *v, Error **errp)
1403{
1404 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
1405 NULL, NULL, (void *)v, errp);
1406}
1407
1408void object_property_add_uint16_ptr(Object *obj, const char *name,
1409 const uint16_t *v, Error **errp)
1410{
1411 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
1412 NULL, NULL, (void *)v, errp);
1413}
1414
1415void object_property_add_uint32_ptr(Object *obj, const char *name,
1416 const uint32_t *v, Error **errp)
1417{
1418 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
1419 NULL, NULL, (void *)v, errp);
1420}
1421
1422void object_property_add_uint64_ptr(Object *obj, const char *name,
1423 const uint64_t *v, Error **errp)
1424{
1425 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
1426 NULL, NULL, (void *)v, errp);
1427}
1428
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001429static void object_instance_init(Object *obj)
1430{
1431 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1432}
1433
Paolo Bonzini745549c2012-03-31 16:45:54 +02001434static void register_types(void)
1435{
1436 static TypeInfo interface_info = {
1437 .name = TYPE_INTERFACE,
Anthony Liguori33e95c62012-08-10 13:16:10 +10001438 .class_size = sizeof(InterfaceClass),
Paolo Bonzini745549c2012-03-31 16:45:54 +02001439 .abstract = true,
1440 };
1441
1442 static TypeInfo object_info = {
1443 .name = TYPE_OBJECT,
1444 .instance_size = sizeof(Object),
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001445 .instance_init = object_instance_init,
Paolo Bonzini745549c2012-03-31 16:45:54 +02001446 .abstract = true,
1447 };
1448
Paolo Bonzini049cb3c2012-04-04 15:58:40 +02001449 type_interface = type_register_internal(&interface_info);
1450 type_register_internal(&object_info);
Paolo Bonzini745549c2012-03-31 16:45:54 +02001451}
1452
1453type_init(register_types)