Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 1 | /* |
| 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 | |
| 13 | #include "qemu/object.h" |
| 14 | #include "qemu-common.h" |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 15 | #include "qapi/qapi-visit-core.h" |
| 16 | #include "hw/qdev.h" |
| 17 | // FIXME remove above |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 18 | |
| 19 | #define MAX_INTERFACES 32 |
| 20 | |
| 21 | typedef struct InterfaceImpl InterfaceImpl; |
| 22 | typedef struct TypeImpl TypeImpl; |
| 23 | |
| 24 | struct InterfaceImpl |
| 25 | { |
| 26 | const char *parent; |
| 27 | void (*interface_initfn)(ObjectClass *class, void *data); |
| 28 | TypeImpl *type; |
| 29 | }; |
| 30 | |
| 31 | struct TypeImpl |
| 32 | { |
| 33 | const char *name; |
| 34 | |
| 35 | size_t class_size; |
| 36 | |
| 37 | size_t instance_size; |
| 38 | |
| 39 | void (*class_init)(ObjectClass *klass, void *data); |
| 40 | void (*class_finalize)(ObjectClass *klass, void *data); |
| 41 | |
| 42 | void *class_data; |
| 43 | |
| 44 | void (*instance_init)(Object *obj); |
| 45 | void (*instance_finalize)(Object *obj); |
| 46 | |
| 47 | bool abstract; |
| 48 | |
| 49 | const char *parent; |
| 50 | TypeImpl *parent_type; |
| 51 | |
| 52 | ObjectClass *class; |
| 53 | |
| 54 | int num_interfaces; |
| 55 | InterfaceImpl interfaces[MAX_INTERFACES]; |
| 56 | }; |
| 57 | |
| 58 | typedef struct Interface |
| 59 | { |
| 60 | Object parent; |
| 61 | Object *obj; |
| 62 | } Interface; |
| 63 | |
| 64 | #define INTERFACE(obj) OBJECT_CHECK(Interface, obj, TYPE_INTERFACE) |
| 65 | |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame^] | 66 | static Type type_interface; |
| 67 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 68 | static GHashTable *type_table_get(void) |
| 69 | { |
| 70 | static GHashTable *type_table; |
| 71 | |
| 72 | if (type_table == NULL) { |
| 73 | type_table = g_hash_table_new(g_str_hash, g_str_equal); |
| 74 | } |
| 75 | |
| 76 | return type_table; |
| 77 | } |
| 78 | |
| 79 | static void type_table_add(TypeImpl *ti) |
| 80 | { |
| 81 | g_hash_table_insert(type_table_get(), (void *)ti->name, ti); |
| 82 | } |
| 83 | |
| 84 | static TypeImpl *type_table_lookup(const char *name) |
| 85 | { |
| 86 | return g_hash_table_lookup(type_table_get(), name); |
| 87 | } |
| 88 | |
| 89 | TypeImpl *type_register(const TypeInfo *info) |
| 90 | { |
| 91 | TypeImpl *ti = g_malloc0(sizeof(*ti)); |
| 92 | |
| 93 | g_assert(info->name != NULL); |
| 94 | |
Anthony Liguori | 7309335 | 2012-01-25 13:37:36 -0600 | [diff] [blame] | 95 | if (type_table_lookup(info->name) != NULL) { |
| 96 | fprintf(stderr, "Registering `%s' which already exists\n", info->name); |
| 97 | abort(); |
| 98 | } |
| 99 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 100 | ti->name = g_strdup(info->name); |
| 101 | ti->parent = g_strdup(info->parent); |
| 102 | |
| 103 | ti->class_size = info->class_size; |
| 104 | ti->instance_size = info->instance_size; |
| 105 | |
| 106 | ti->class_init = info->class_init; |
| 107 | ti->class_finalize = info->class_finalize; |
| 108 | ti->class_data = info->class_data; |
| 109 | |
| 110 | ti->instance_init = info->instance_init; |
| 111 | ti->instance_finalize = info->instance_finalize; |
| 112 | |
| 113 | ti->abstract = info->abstract; |
| 114 | |
| 115 | if (info->interfaces) { |
| 116 | int i; |
| 117 | |
| 118 | for (i = 0; info->interfaces[i].type; i++) { |
| 119 | ti->interfaces[i].parent = info->interfaces[i].type; |
| 120 | ti->interfaces[i].interface_initfn = info->interfaces[i].interface_initfn; |
| 121 | ti->num_interfaces++; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | type_table_add(ti); |
| 126 | |
| 127 | return ti; |
| 128 | } |
| 129 | |
| 130 | TypeImpl *type_register_static(const TypeInfo *info) |
| 131 | { |
| 132 | return type_register(info); |
| 133 | } |
| 134 | |
| 135 | static TypeImpl *type_get_by_name(const char *name) |
| 136 | { |
| 137 | if (name == NULL) { |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | return type_table_lookup(name); |
| 142 | } |
| 143 | |
| 144 | static TypeImpl *type_get_parent(TypeImpl *type) |
| 145 | { |
| 146 | if (!type->parent_type && type->parent) { |
| 147 | type->parent_type = type_get_by_name(type->parent); |
| 148 | g_assert(type->parent_type != NULL); |
| 149 | } |
| 150 | |
| 151 | return type->parent_type; |
| 152 | } |
| 153 | |
| 154 | static bool type_has_parent(TypeImpl *type) |
| 155 | { |
| 156 | return (type->parent != NULL); |
| 157 | } |
| 158 | |
| 159 | static size_t type_class_get_size(TypeImpl *ti) |
| 160 | { |
| 161 | if (ti->class_size) { |
| 162 | return ti->class_size; |
| 163 | } |
| 164 | |
| 165 | if (type_has_parent(ti)) { |
| 166 | return type_class_get_size(type_get_parent(ti)); |
| 167 | } |
| 168 | |
| 169 | return sizeof(ObjectClass); |
| 170 | } |
| 171 | |
| 172 | static void type_class_interface_init(TypeImpl *ti, InterfaceImpl *iface) |
| 173 | { |
| 174 | TypeInfo info = { |
| 175 | .instance_size = sizeof(Interface), |
| 176 | .parent = iface->parent, |
| 177 | .class_size = sizeof(InterfaceClass), |
| 178 | .class_init = iface->interface_initfn, |
| 179 | .abstract = true, |
| 180 | }; |
| 181 | char *name = g_strdup_printf("<%s::%s>", ti->name, iface->parent); |
| 182 | |
| 183 | info.name = name; |
| 184 | iface->type = type_register(&info); |
| 185 | g_free(name); |
| 186 | } |
| 187 | |
| 188 | static void type_class_init(TypeImpl *ti) |
| 189 | { |
| 190 | size_t class_size = sizeof(ObjectClass); |
| 191 | int i; |
| 192 | |
| 193 | if (ti->class) { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | ti->class_size = type_class_get_size(ti); |
| 198 | |
| 199 | ti->class = g_malloc0(ti->class_size); |
| 200 | ti->class->type = ti; |
| 201 | |
| 202 | if (type_has_parent(ti)) { |
| 203 | TypeImpl *parent = type_get_parent(ti); |
| 204 | |
| 205 | type_class_init(parent); |
| 206 | |
| 207 | class_size = parent->class_size; |
| 208 | g_assert(parent->class_size <= ti->class_size); |
| 209 | |
| 210 | memcpy((void *)ti->class + sizeof(ObjectClass), |
| 211 | (void *)parent->class + sizeof(ObjectClass), |
| 212 | parent->class_size - sizeof(ObjectClass)); |
| 213 | } |
| 214 | |
| 215 | memset((void *)ti->class + class_size, 0, ti->class_size - class_size); |
| 216 | |
| 217 | for (i = 0; i < ti->num_interfaces; i++) { |
| 218 | type_class_interface_init(ti, &ti->interfaces[i]); |
| 219 | } |
| 220 | |
| 221 | if (ti->class_init) { |
| 222 | ti->class_init(ti->class, ti->class_data); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static void object_interface_init(Object *obj, InterfaceImpl *iface) |
| 227 | { |
| 228 | TypeImpl *ti = iface->type; |
| 229 | Interface *iface_obj; |
| 230 | |
| 231 | iface_obj = INTERFACE(object_new(ti->name)); |
| 232 | iface_obj->obj = obj; |
| 233 | |
| 234 | obj->interfaces = g_slist_prepend(obj->interfaces, iface_obj); |
| 235 | } |
| 236 | |
| 237 | static void object_init_with_type(Object *obj, TypeImpl *ti) |
| 238 | { |
| 239 | int i; |
| 240 | |
| 241 | if (type_has_parent(ti)) { |
| 242 | object_init_with_type(obj, type_get_parent(ti)); |
| 243 | } |
| 244 | |
| 245 | for (i = 0; i < ti->num_interfaces; i++) { |
| 246 | object_interface_init(obj, &ti->interfaces[i]); |
| 247 | } |
| 248 | |
| 249 | if (ti->instance_init) { |
| 250 | ti->instance_init(obj); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void object_initialize_with_type(void *data, TypeImpl *type) |
| 255 | { |
| 256 | Object *obj = data; |
| 257 | |
| 258 | g_assert(type != NULL); |
| 259 | g_assert(type->instance_size >= sizeof(ObjectClass)); |
| 260 | |
| 261 | type_class_init(type); |
| 262 | g_assert(type->abstract == false); |
| 263 | |
| 264 | memset(obj, 0, type->instance_size); |
| 265 | obj->class = type->class; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 266 | QTAILQ_INIT(&obj->properties); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 267 | object_init_with_type(obj, type); |
| 268 | } |
| 269 | |
| 270 | void object_initialize(void *data, const char *typename) |
| 271 | { |
| 272 | TypeImpl *type = type_get_by_name(typename); |
| 273 | |
| 274 | object_initialize_with_type(data, type); |
| 275 | } |
| 276 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 277 | static void object_property_del_all(Object *obj) |
| 278 | { |
| 279 | while (!QTAILQ_EMPTY(&obj->properties)) { |
| 280 | ObjectProperty *prop = QTAILQ_FIRST(&obj->properties); |
| 281 | |
| 282 | QTAILQ_REMOVE(&obj->properties, prop, node); |
| 283 | |
| 284 | if (prop->release) { |
| 285 | prop->release(obj, prop->name, prop->opaque); |
| 286 | } |
| 287 | |
| 288 | g_free(prop->name); |
| 289 | g_free(prop->type); |
| 290 | g_free(prop); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static void object_property_del_child(Object *obj, Object *child, Error **errp) |
| 295 | { |
| 296 | ObjectProperty *prop; |
| 297 | |
| 298 | QTAILQ_FOREACH(prop, &obj->properties, node) { |
| 299 | if (!strstart(prop->type, "child<", NULL)) { |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | if (prop->opaque == child) { |
| 304 | object_property_del(obj, prop->name, errp); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void object_unparent(Object *obj) |
| 310 | { |
| 311 | if (obj->parent) { |
| 312 | object_property_del_child(obj->parent, obj, NULL); |
| 313 | } |
| 314 | } |
| 315 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 316 | static void object_deinit(Object *obj, TypeImpl *type) |
| 317 | { |
| 318 | if (type->instance_finalize) { |
| 319 | type->instance_finalize(obj); |
| 320 | } |
| 321 | |
| 322 | while (obj->interfaces) { |
| 323 | Interface *iface_obj = obj->interfaces->data; |
| 324 | obj->interfaces = g_slist_delete_link(obj->interfaces, obj->interfaces); |
| 325 | object_delete(OBJECT(iface_obj)); |
| 326 | } |
| 327 | |
| 328 | if (type_has_parent(type)) { |
| 329 | object_deinit(obj, type_get_parent(type)); |
| 330 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 331 | |
| 332 | object_unparent(obj); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void object_finalize(void *data) |
| 336 | { |
| 337 | Object *obj = data; |
| 338 | TypeImpl *ti = obj->class->type; |
| 339 | |
| 340 | object_deinit(obj, ti); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 341 | object_property_del_all(obj); |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 342 | |
| 343 | g_assert(obj->ref == 0); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | Object *object_new_with_type(Type type) |
| 347 | { |
| 348 | Object *obj; |
| 349 | |
| 350 | g_assert(type != NULL); |
| 351 | |
| 352 | obj = g_malloc(type->instance_size); |
| 353 | object_initialize_with_type(obj, type); |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 354 | object_ref(obj); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 355 | |
| 356 | return obj; |
| 357 | } |
| 358 | |
| 359 | Object *object_new(const char *typename) |
| 360 | { |
| 361 | TypeImpl *ti = type_get_by_name(typename); |
| 362 | |
| 363 | return object_new_with_type(ti); |
| 364 | } |
| 365 | |
| 366 | void object_delete(Object *obj) |
| 367 | { |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 368 | object_unref(obj); |
| 369 | g_assert(obj->ref == 0); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 370 | g_free(obj); |
| 371 | } |
| 372 | |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 373 | static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 374 | { |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 375 | assert(target_type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 376 | |
| 377 | /* Check if typename is a direct ancestor of type */ |
| 378 | while (type) { |
| 379 | if (type == target_type) { |
| 380 | return true; |
| 381 | } |
| 382 | |
| 383 | type = type_get_parent(type); |
| 384 | } |
| 385 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 386 | return false; |
| 387 | } |
| 388 | |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame^] | 389 | static bool object_is_type(Object *obj, TypeImpl *target_type) |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 390 | { |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame^] | 391 | return !target_type || type_is_ancestor(obj->class->type, target_type); |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 392 | } |
| 393 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 394 | Object *object_dynamic_cast(Object *obj, const char *typename) |
| 395 | { |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame^] | 396 | TypeImpl *target_type = type_get_by_name(typename); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 397 | GSList *i; |
| 398 | |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 399 | /* Check if typename is a direct ancestor. Special-case TYPE_OBJECT, |
| 400 | * we want to go back from interfaces to the parent. |
| 401 | */ |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame^] | 402 | if (target_type && object_is_type(obj, target_type)) { |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 403 | return obj; |
| 404 | } |
| 405 | |
| 406 | /* Check if obj is an interface and its containing object is a direct |
| 407 | * ancestor of typename. In principle we could do this test at the very |
| 408 | * beginning of object_dynamic_cast, avoiding a second call to |
| 409 | * object_is_type. However, casting between interfaces is relatively |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame^] | 410 | * rare, and object_is_type(obj, type_interface) would fail almost always. |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 411 | * |
| 412 | * Perhaps we could add a magic value to the object header for increased |
| 413 | * (run-time) type safety and to speed up tests like this one. If we ever |
| 414 | * do that we can revisit the order here. |
| 415 | */ |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame^] | 416 | if (object_is_type(obj, type_interface)) { |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 417 | assert(!obj->interfaces); |
| 418 | obj = INTERFACE(obj)->obj; |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame^] | 419 | if (object_is_type(obj, target_type)) { |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 420 | return obj; |
| 421 | } |
| 422 | } |
| 423 | |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame^] | 424 | if (!target_type) { |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 425 | return obj; |
| 426 | } |
| 427 | |
| 428 | /* Check if obj has an interface of typename */ |
| 429 | for (i = obj->interfaces; i; i = i->next) { |
| 430 | Interface *iface = i->data; |
| 431 | |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame^] | 432 | if (object_is_type(OBJECT(iface), target_type)) { |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 433 | return OBJECT(iface); |
| 434 | } |
| 435 | } |
| 436 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 437 | return NULL; |
| 438 | } |
| 439 | |
| 440 | |
| 441 | static void register_interface(void) |
| 442 | { |
| 443 | static TypeInfo interface_info = { |
| 444 | .name = TYPE_INTERFACE, |
| 445 | .instance_size = sizeof(Interface), |
| 446 | .abstract = true, |
| 447 | }; |
| 448 | |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame^] | 449 | type_interface = type_register_static(&interface_info); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | device_init(register_interface); |
| 453 | |
| 454 | Object *object_dynamic_cast_assert(Object *obj, const char *typename) |
| 455 | { |
| 456 | Object *inst; |
| 457 | |
| 458 | inst = object_dynamic_cast(obj, typename); |
| 459 | |
| 460 | if (!inst) { |
| 461 | fprintf(stderr, "Object %p is not an instance of type %s\n", |
| 462 | obj, typename); |
| 463 | abort(); |
| 464 | } |
| 465 | |
| 466 | return inst; |
| 467 | } |
| 468 | |
| 469 | ObjectClass *object_class_dynamic_cast(ObjectClass *class, |
| 470 | const char *typename) |
| 471 | { |
| 472 | TypeImpl *target_type = type_get_by_name(typename); |
| 473 | TypeImpl *type = class->type; |
| 474 | |
| 475 | while (type) { |
| 476 | if (type == target_type) { |
| 477 | return class; |
| 478 | } |
| 479 | |
| 480 | type = type_get_parent(type); |
| 481 | } |
| 482 | |
| 483 | return NULL; |
| 484 | } |
| 485 | |
| 486 | ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, |
| 487 | const char *typename) |
| 488 | { |
| 489 | ObjectClass *ret = object_class_dynamic_cast(class, typename); |
| 490 | |
| 491 | if (!ret) { |
| 492 | fprintf(stderr, "Object %p is not an instance of type %s\n", |
| 493 | class, typename); |
| 494 | abort(); |
| 495 | } |
| 496 | |
| 497 | return ret; |
| 498 | } |
| 499 | |
| 500 | const char *object_get_typename(Object *obj) |
| 501 | { |
| 502 | return obj->class->type->name; |
| 503 | } |
| 504 | |
| 505 | ObjectClass *object_get_class(Object *obj) |
| 506 | { |
| 507 | return obj->class; |
| 508 | } |
| 509 | |
| 510 | const char *object_class_get_name(ObjectClass *klass) |
| 511 | { |
| 512 | return klass->type->name; |
| 513 | } |
| 514 | |
| 515 | ObjectClass *object_class_by_name(const char *typename) |
| 516 | { |
| 517 | TypeImpl *type = type_get_by_name(typename); |
| 518 | |
| 519 | if (!type) { |
| 520 | return NULL; |
| 521 | } |
| 522 | |
| 523 | type_class_init(type); |
| 524 | |
| 525 | return type->class; |
| 526 | } |
| 527 | |
| 528 | typedef struct OCFData |
| 529 | { |
| 530 | void (*fn)(ObjectClass *klass, void *opaque); |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 531 | const char *implements_type; |
| 532 | bool include_abstract; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 533 | void *opaque; |
| 534 | } OCFData; |
| 535 | |
| 536 | static void object_class_foreach_tramp(gpointer key, gpointer value, |
| 537 | gpointer opaque) |
| 538 | { |
| 539 | OCFData *data = opaque; |
| 540 | TypeImpl *type = value; |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 541 | ObjectClass *k; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 542 | |
| 543 | type_class_init(type); |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 544 | k = type->class; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 545 | |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 546 | if (!data->include_abstract && type->abstract) { |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | if (data->implements_type && |
| 551 | !object_class_dynamic_cast(k, data->implements_type)) { |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | data->fn(k, data->opaque); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 559 | const char *implements_type, bool include_abstract, |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 560 | void *opaque) |
| 561 | { |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 562 | OCFData data = { fn, implements_type, include_abstract, opaque }; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 563 | |
| 564 | g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); |
| 565 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 566 | |
| 567 | void object_ref(Object *obj) |
| 568 | { |
| 569 | obj->ref++; |
| 570 | } |
| 571 | |
| 572 | void object_unref(Object *obj) |
| 573 | { |
| 574 | g_assert(obj->ref > 0); |
| 575 | obj->ref--; |
| 576 | |
| 577 | /* parent always holds a reference to its children */ |
| 578 | if (obj->ref == 0) { |
| 579 | object_finalize(obj); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | void object_property_add(Object *obj, const char *name, const char *type, |
| 584 | ObjectPropertyAccessor *get, |
| 585 | ObjectPropertyAccessor *set, |
| 586 | ObjectPropertyRelease *release, |
| 587 | void *opaque, Error **errp) |
| 588 | { |
| 589 | ObjectProperty *prop = g_malloc0(sizeof(*prop)); |
| 590 | |
| 591 | prop->name = g_strdup(name); |
| 592 | prop->type = g_strdup(type); |
| 593 | |
| 594 | prop->get = get; |
| 595 | prop->set = set; |
| 596 | prop->release = release; |
| 597 | prop->opaque = opaque; |
| 598 | |
| 599 | QTAILQ_INSERT_TAIL(&obj->properties, prop, node); |
| 600 | } |
| 601 | |
| 602 | static ObjectProperty *object_property_find(Object *obj, const char *name) |
| 603 | { |
| 604 | ObjectProperty *prop; |
| 605 | |
| 606 | QTAILQ_FOREACH(prop, &obj->properties, node) { |
| 607 | if (strcmp(prop->name, name) == 0) { |
| 608 | return prop; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | return NULL; |
| 613 | } |
| 614 | |
| 615 | void object_property_del(Object *obj, const char *name, Error **errp) |
| 616 | { |
| 617 | ObjectProperty *prop = object_property_find(obj, name); |
| 618 | |
| 619 | QTAILQ_REMOVE(&obj->properties, prop, node); |
| 620 | |
| 621 | prop->release(obj, prop->name, prop->opaque); |
| 622 | |
| 623 | g_free(prop->name); |
| 624 | g_free(prop->type); |
| 625 | g_free(prop); |
| 626 | } |
| 627 | |
| 628 | void object_property_get(Object *obj, Visitor *v, const char *name, |
| 629 | Error **errp) |
| 630 | { |
| 631 | ObjectProperty *prop = object_property_find(obj, name); |
| 632 | |
| 633 | if (prop == NULL) { |
| 634 | error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name); |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | if (!prop->get) { |
| 639 | error_set(errp, QERR_PERMISSION_DENIED); |
| 640 | } else { |
| 641 | prop->get(obj, v, prop->opaque, name, errp); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | void object_property_set(Object *obj, Visitor *v, const char *name, |
| 646 | Error **errp) |
| 647 | { |
| 648 | ObjectProperty *prop = object_property_find(obj, name); |
| 649 | |
| 650 | if (prop == NULL) { |
| 651 | error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name); |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | if (!prop->set) { |
| 656 | error_set(errp, QERR_PERMISSION_DENIED); |
| 657 | } else { |
| 658 | prop->set(obj, v, prop->opaque, name, errp); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | const char *object_property_get_type(Object *obj, const char *name, Error **errp) |
| 663 | { |
| 664 | ObjectProperty *prop = object_property_find(obj, name); |
| 665 | |
| 666 | if (prop == NULL) { |
| 667 | error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name); |
| 668 | return NULL; |
| 669 | } |
| 670 | |
| 671 | return prop->type; |
| 672 | } |
| 673 | |
| 674 | Object *object_get_root(void) |
| 675 | { |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 676 | static Object *root; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 677 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 678 | if (!root) { |
| 679 | root = object_new("container"); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 680 | } |
| 681 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 682 | return root; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static void object_get_child_property(Object *obj, Visitor *v, void *opaque, |
| 686 | const char *name, Error **errp) |
| 687 | { |
| 688 | Object *child = opaque; |
| 689 | gchar *path; |
| 690 | |
| 691 | path = object_get_canonical_path(child); |
| 692 | visit_type_str(v, &path, name, errp); |
| 693 | g_free(path); |
| 694 | } |
| 695 | |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 696 | static void object_finalize_child_property(Object *obj, const char *name, |
| 697 | void *opaque) |
| 698 | { |
| 699 | Object *child = opaque; |
| 700 | |
| 701 | object_unref(child); |
| 702 | } |
| 703 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 704 | void object_property_add_child(Object *obj, const char *name, |
| 705 | Object *child, Error **errp) |
| 706 | { |
| 707 | gchar *type; |
| 708 | |
| 709 | type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); |
| 710 | |
| 711 | object_property_add(obj, name, type, object_get_child_property, |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 712 | NULL, object_finalize_child_property, child, errp); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 713 | |
| 714 | object_ref(child); |
| 715 | g_assert(child->parent == NULL); |
| 716 | child->parent = obj; |
| 717 | |
| 718 | g_free(type); |
| 719 | } |
| 720 | |
| 721 | static void object_get_link_property(Object *obj, Visitor *v, void *opaque, |
| 722 | const char *name, Error **errp) |
| 723 | { |
| 724 | Object **child = opaque; |
| 725 | gchar *path; |
| 726 | |
| 727 | if (*child) { |
| 728 | path = object_get_canonical_path(*child); |
| 729 | visit_type_str(v, &path, name, errp); |
| 730 | g_free(path); |
| 731 | } else { |
| 732 | path = (gchar *)""; |
| 733 | visit_type_str(v, &path, name, errp); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | static void object_set_link_property(Object *obj, Visitor *v, void *opaque, |
| 738 | const char *name, Error **errp) |
| 739 | { |
| 740 | Object **child = opaque; |
| 741 | bool ambiguous = false; |
| 742 | const char *type; |
| 743 | char *path; |
| 744 | |
| 745 | type = object_property_get_type(obj, name, NULL); |
| 746 | |
| 747 | visit_type_str(v, &path, name, errp); |
| 748 | |
| 749 | if (*child) { |
| 750 | object_unref(*child); |
| 751 | } |
| 752 | |
| 753 | if (strcmp(path, "") != 0) { |
| 754 | Object *target; |
| 755 | |
| 756 | target = object_resolve_path(path, &ambiguous); |
| 757 | if (target) { |
| 758 | gchar *target_type; |
| 759 | |
Anthony Liguori | fe40e62 | 2011-12-23 08:35:43 -0600 | [diff] [blame] | 760 | target_type = g_strdup(&type[5]); |
| 761 | target_type[strlen(target_type) - 2] = 0; |
| 762 | |
| 763 | if (object_dynamic_cast(target, target_type)) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 764 | object_ref(target); |
Anthony Liguori | fe40e62 | 2011-12-23 08:35:43 -0600 | [diff] [blame] | 765 | *child = target; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 766 | } else { |
| 767 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, type); |
| 768 | } |
| 769 | |
| 770 | g_free(target_type); |
| 771 | } else { |
| 772 | error_set(errp, QERR_DEVICE_NOT_FOUND, path); |
| 773 | } |
| 774 | } else { |
| 775 | *child = NULL; |
| 776 | } |
| 777 | |
| 778 | g_free(path); |
| 779 | } |
| 780 | |
| 781 | void object_property_add_link(Object *obj, const char *name, |
| 782 | const char *type, Object **child, |
| 783 | Error **errp) |
| 784 | { |
| 785 | gchar *full_type; |
| 786 | |
| 787 | full_type = g_strdup_printf("link<%s>", type); |
| 788 | |
| 789 | object_property_add(obj, name, full_type, |
| 790 | object_get_link_property, |
| 791 | object_set_link_property, |
| 792 | NULL, child, errp); |
| 793 | |
| 794 | g_free(full_type); |
| 795 | } |
| 796 | |
| 797 | gchar *object_get_canonical_path(Object *obj) |
| 798 | { |
| 799 | Object *root = object_get_root(); |
| 800 | char *newpath = NULL, *path = NULL; |
| 801 | |
| 802 | while (obj != root) { |
| 803 | ObjectProperty *prop = NULL; |
| 804 | |
| 805 | g_assert(obj->parent != NULL); |
| 806 | |
| 807 | QTAILQ_FOREACH(prop, &obj->parent->properties, node) { |
| 808 | if (!strstart(prop->type, "child<", NULL)) { |
| 809 | continue; |
| 810 | } |
| 811 | |
| 812 | if (prop->opaque == obj) { |
| 813 | if (path) { |
| 814 | newpath = g_strdup_printf("%s/%s", prop->name, path); |
| 815 | g_free(path); |
| 816 | path = newpath; |
| 817 | } else { |
| 818 | path = g_strdup(prop->name); |
| 819 | } |
| 820 | break; |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | g_assert(prop != NULL); |
| 825 | |
| 826 | obj = obj->parent; |
| 827 | } |
| 828 | |
| 829 | newpath = g_strdup_printf("/%s", path); |
| 830 | g_free(path); |
| 831 | |
| 832 | return newpath; |
| 833 | } |
| 834 | |
| 835 | static Object *object_resolve_abs_path(Object *parent, |
| 836 | gchar **parts, |
| 837 | int index) |
| 838 | { |
| 839 | ObjectProperty *prop; |
| 840 | Object *child; |
| 841 | |
| 842 | if (parts[index] == NULL) { |
| 843 | return parent; |
| 844 | } |
| 845 | |
| 846 | if (strcmp(parts[index], "") == 0) { |
| 847 | return object_resolve_abs_path(parent, parts, index + 1); |
| 848 | } |
| 849 | |
| 850 | prop = object_property_find(parent, parts[index]); |
| 851 | if (prop == NULL) { |
| 852 | return NULL; |
| 853 | } |
| 854 | |
| 855 | child = NULL; |
| 856 | if (strstart(prop->type, "link<", NULL)) { |
| 857 | Object **pchild = prop->opaque; |
| 858 | if (*pchild) { |
| 859 | child = *pchild; |
| 860 | } |
| 861 | } else if (strstart(prop->type, "child<", NULL)) { |
| 862 | child = prop->opaque; |
| 863 | } |
| 864 | |
| 865 | if (!child) { |
| 866 | return NULL; |
| 867 | } |
| 868 | |
| 869 | return object_resolve_abs_path(child, parts, index + 1); |
| 870 | } |
| 871 | |
| 872 | static Object *object_resolve_partial_path(Object *parent, |
| 873 | gchar **parts, |
| 874 | bool *ambiguous) |
| 875 | { |
| 876 | Object *obj; |
| 877 | ObjectProperty *prop; |
| 878 | |
| 879 | obj = object_resolve_abs_path(parent, parts, 0); |
| 880 | |
| 881 | QTAILQ_FOREACH(prop, &parent->properties, node) { |
| 882 | Object *found; |
| 883 | |
| 884 | if (!strstart(prop->type, "child<", NULL)) { |
| 885 | continue; |
| 886 | } |
| 887 | |
| 888 | found = object_resolve_partial_path(prop->opaque, parts, ambiguous); |
| 889 | if (found) { |
| 890 | if (obj) { |
| 891 | if (ambiguous) { |
| 892 | *ambiguous = true; |
| 893 | } |
| 894 | return NULL; |
| 895 | } |
| 896 | obj = found; |
| 897 | } |
| 898 | |
| 899 | if (ambiguous && *ambiguous) { |
| 900 | return NULL; |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | return obj; |
| 905 | } |
| 906 | |
| 907 | Object *object_resolve_path(const char *path, bool *ambiguous) |
| 908 | { |
| 909 | bool partial_path = true; |
| 910 | Object *obj; |
| 911 | gchar **parts; |
| 912 | |
| 913 | parts = g_strsplit(path, "/", 0); |
| 914 | if (parts == NULL || parts[0] == NULL) { |
| 915 | g_strfreev(parts); |
| 916 | return object_get_root(); |
| 917 | } |
| 918 | |
| 919 | if (strcmp(parts[0], "") == 0) { |
| 920 | partial_path = false; |
| 921 | } |
| 922 | |
| 923 | if (partial_path) { |
| 924 | if (ambiguous) { |
| 925 | *ambiguous = false; |
| 926 | } |
| 927 | obj = object_resolve_partial_path(object_get_root(), parts, ambiguous); |
| 928 | } else { |
| 929 | obj = object_resolve_abs_path(object_get_root(), parts, 1); |
| 930 | } |
| 931 | |
| 932 | g_strfreev(parts); |
| 933 | |
| 934 | return obj; |
| 935 | } |
| 936 | |
| 937 | typedef struct StringProperty |
| 938 | { |
| 939 | char *(*get)(Object *, Error **); |
| 940 | void (*set)(Object *, const char *, Error **); |
| 941 | } StringProperty; |
| 942 | |
| 943 | static void object_property_get_str(Object *obj, Visitor *v, void *opaque, |
| 944 | const char *name, Error **errp) |
| 945 | { |
| 946 | StringProperty *prop = opaque; |
| 947 | char *value; |
| 948 | |
| 949 | value = prop->get(obj, errp); |
| 950 | if (value) { |
| 951 | visit_type_str(v, &value, name, errp); |
| 952 | g_free(value); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | static void object_property_set_str(Object *obj, Visitor *v, void *opaque, |
| 957 | const char *name, Error **errp) |
| 958 | { |
| 959 | StringProperty *prop = opaque; |
| 960 | char *value; |
| 961 | Error *local_err = NULL; |
| 962 | |
| 963 | visit_type_str(v, &value, name, &local_err); |
| 964 | if (local_err) { |
| 965 | error_propagate(errp, local_err); |
| 966 | return; |
| 967 | } |
| 968 | |
| 969 | prop->set(obj, value, errp); |
| 970 | g_free(value); |
| 971 | } |
| 972 | |
| 973 | static void object_property_release_str(Object *obj, const char *name, |
| 974 | void *opaque) |
| 975 | { |
| 976 | StringProperty *prop = opaque; |
| 977 | g_free(prop); |
| 978 | } |
| 979 | |
| 980 | void object_property_add_str(Object *obj, const char *name, |
| 981 | char *(*get)(Object *, Error **), |
| 982 | void (*set)(Object *, const char *, Error **), |
| 983 | Error **errp) |
| 984 | { |
| 985 | StringProperty *prop = g_malloc0(sizeof(*prop)); |
| 986 | |
| 987 | prop->get = get; |
| 988 | prop->set = set; |
| 989 | |
| 990 | object_property_add(obj, name, "string", |
| 991 | get ? object_property_get_str : NULL, |
| 992 | set ? object_property_set_str : NULL, |
| 993 | object_property_release_str, |
| 994 | prop, errp); |
| 995 | } |