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/osdep.h" 14 #include "hw/qdev-core.h" 15 #include "qapi/error.h" 16 #include "qom/object.h" 17 #include "qom/object_interfaces.h" 18 #include "qemu/cutils.h" 19 #include "qemu/memalign.h" 20 #include "qapi/visitor.h" 21 #include "qapi/string-input-visitor.h" 22 #include "qapi/string-output-visitor.h" 23 #include "qapi/qobject-input-visitor.h" 24 #include "qapi/forward-visitor.h" 25 #include "qapi/qapi-builtin-visit.h" 26 #include "qapi/qmp/qjson.h" 27 #include "trace.h" 28 29 /* TODO: replace QObject with a simpler visitor to avoid a dependency 30 * of the QOM core on QObject? */ 31 #include "qom/qom-qobject.h" 32 #include "qapi/qmp/qbool.h" 33 #include "qapi/qmp/qlist.h" 34 #include "qapi/qmp/qnum.h" 35 #include "qapi/qmp/qstring.h" 36 #include "qemu/error-report.h" 37 38 #define MAX_INTERFACES 32 39 40 typedef struct InterfaceImpl InterfaceImpl; 41 typedef struct TypeImpl TypeImpl; 42 43 struct InterfaceImpl 44 { 45 const char *typename; 46 }; 47 48 struct TypeImpl 49 { 50 const char *name; 51 52 size_t class_size; 53 54 size_t instance_size; 55 size_t instance_align; 56 57 void (*class_init)(ObjectClass *klass, void *data); 58 void (*class_base_init)(ObjectClass *klass, void *data); 59 60 void *class_data; 61 62 void (*instance_init)(Object *obj); 63 void (*instance_post_init)(Object *obj); 64 void (*instance_finalize)(Object *obj); 65 66 bool abstract; 67 68 const char *parent; 69 TypeImpl *parent_type; 70 71 ObjectClass *class; 72 73 int num_interfaces; 74 InterfaceImpl interfaces[MAX_INTERFACES]; 75 }; 76 77 static Type type_interface; 78 79 static GHashTable *type_table_get(void) 80 { 81 static GHashTable *type_table; 82 83 if (type_table == NULL) { 84 type_table = g_hash_table_new(g_str_hash, g_str_equal); 85 } 86 87 return type_table; 88 } 89 90 static bool enumerating_types; 91 92 static void type_table_add(TypeImpl *ti) 93 { 94 assert(!enumerating_types); 95 g_hash_table_insert(type_table_get(), (void *)ti->name, ti); 96 } 97 98 static TypeImpl *type_table_lookup(const char *name) 99 { 100 return g_hash_table_lookup(type_table_get(), name); 101 } 102 103 static TypeImpl *type_new(const TypeInfo *info) 104 { 105 TypeImpl *ti = g_malloc0(sizeof(*ti)); 106 int i; 107 108 g_assert(info->name != NULL); 109 110 if (type_table_lookup(info->name) != NULL) { 111 fprintf(stderr, "Registering `%s' which already exists\n", info->name); 112 abort(); 113 } 114 115 ti->name = g_strdup(info->name); 116 ti->parent = g_strdup(info->parent); 117 118 ti->class_size = info->class_size; 119 ti->instance_size = info->instance_size; 120 ti->instance_align = info->instance_align; 121 122 ti->class_init = info->class_init; 123 ti->class_base_init = info->class_base_init; 124 ti->class_data = info->class_data; 125 126 ti->instance_init = info->instance_init; 127 ti->instance_post_init = info->instance_post_init; 128 ti->instance_finalize = info->instance_finalize; 129 130 ti->abstract = info->abstract; 131 132 for (i = 0; info->interfaces && info->interfaces[i].type; i++) { 133 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type); 134 } 135 ti->num_interfaces = i; 136 137 return ti; 138 } 139 140 static bool type_name_is_valid(const char *name) 141 { 142 const int slen = strlen(name); 143 int plen; 144 145 g_assert(slen > 1); 146 147 /* 148 * Ideally, the name should start with a letter - however, we've got 149 * too many names starting with a digit already, so allow digits here, 150 * too (except '0' which is not used yet) 151 */ 152 if (!g_ascii_isalnum(name[0]) || name[0] == '0') { 153 return false; 154 } 155 156 plen = strspn(name, "abcdefghijklmnopqrstuvwxyz" 157 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 158 "0123456789-_."); 159 160 return plen == slen; 161 } 162 163 static TypeImpl *type_register_internal(const TypeInfo *info) 164 { 165 TypeImpl *ti; 166 167 if (!type_name_is_valid(info->name)) { 168 fprintf(stderr, "Registering '%s' with illegal type name\n", info->name); 169 abort(); 170 } 171 172 ti = type_new(info); 173 174 type_table_add(ti); 175 return ti; 176 } 177 178 TypeImpl *type_register(const TypeInfo *info) 179 { 180 assert(info->parent); 181 return type_register_internal(info); 182 } 183 184 TypeImpl *type_register_static(const TypeInfo *info) 185 { 186 return type_register(info); 187 } 188 189 void type_register_static_array(const TypeInfo *infos, int nr_infos) 190 { 191 int i; 192 193 for (i = 0; i < nr_infos; i++) { 194 type_register_static(&infos[i]); 195 } 196 } 197 198 static TypeImpl *type_get_by_name(const char *name) 199 { 200 if (name == NULL) { 201 return NULL; 202 } 203 204 return type_table_lookup(name); 205 } 206 207 static TypeImpl *type_get_parent(TypeImpl *type) 208 { 209 if (!type->parent_type && type->parent) { 210 type->parent_type = type_get_by_name(type->parent); 211 if (!type->parent_type) { 212 fprintf(stderr, "Type '%s' is missing its parent '%s'\n", 213 type->name, type->parent); 214 abort(); 215 } 216 } 217 218 return type->parent_type; 219 } 220 221 static bool type_has_parent(TypeImpl *type) 222 { 223 return (type->parent != NULL); 224 } 225 226 static size_t type_class_get_size(TypeImpl *ti) 227 { 228 if (ti->class_size) { 229 return ti->class_size; 230 } 231 232 if (type_has_parent(ti)) { 233 return type_class_get_size(type_get_parent(ti)); 234 } 235 236 return sizeof(ObjectClass); 237 } 238 239 static size_t type_object_get_size(TypeImpl *ti) 240 { 241 if (ti->instance_size) { 242 return ti->instance_size; 243 } 244 245 if (type_has_parent(ti)) { 246 return type_object_get_size(type_get_parent(ti)); 247 } 248 249 return 0; 250 } 251 252 static size_t type_object_get_align(TypeImpl *ti) 253 { 254 if (ti->instance_align) { 255 return ti->instance_align; 256 } 257 258 if (type_has_parent(ti)) { 259 return type_object_get_align(type_get_parent(ti)); 260 } 261 262 return 0; 263 } 264 265 size_t object_type_get_instance_size(const char *typename) 266 { 267 TypeImpl *type = type_get_by_name(typename); 268 269 g_assert(type != NULL); 270 return type_object_get_size(type); 271 } 272 273 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type) 274 { 275 assert(target_type); 276 277 /* Check if target_type is a direct ancestor of type */ 278 while (type) { 279 if (type == target_type) { 280 return true; 281 } 282 283 type = type_get_parent(type); 284 } 285 286 return false; 287 } 288 289 static void type_initialize(TypeImpl *ti); 290 291 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type, 292 TypeImpl *parent_type) 293 { 294 InterfaceClass *new_iface; 295 TypeInfo info = { }; 296 TypeImpl *iface_impl; 297 298 info.parent = parent_type->name; 299 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name); 300 info.abstract = true; 301 302 iface_impl = type_new(&info); 303 iface_impl->parent_type = parent_type; 304 type_initialize(iface_impl); 305 g_free((char *)info.name); 306 307 new_iface = (InterfaceClass *)iface_impl->class; 308 new_iface->concrete_class = ti->class; 309 new_iface->interface_type = interface_type; 310 311 ti->class->interfaces = g_slist_append(ti->class->interfaces, new_iface); 312 } 313 314 static void object_property_free(gpointer data) 315 { 316 ObjectProperty *prop = data; 317 318 if (prop->defval) { 319 qobject_unref(prop->defval); 320 prop->defval = NULL; 321 } 322 g_free(prop->name); 323 g_free(prop->type); 324 g_free(prop->description); 325 g_free(prop); 326 } 327 328 static void type_initialize(TypeImpl *ti) 329 { 330 TypeImpl *parent; 331 332 if (ti->class) { 333 return; 334 } 335 336 ti->class_size = type_class_get_size(ti); 337 ti->instance_size = type_object_get_size(ti); 338 ti->instance_align = type_object_get_align(ti); 339 /* Any type with zero instance_size is implicitly abstract. 340 * This means interface types are all abstract. 341 */ 342 if (ti->instance_size == 0) { 343 ti->abstract = true; 344 } 345 if (type_is_ancestor(ti, type_interface)) { 346 assert(ti->instance_size == 0); 347 assert(ti->abstract); 348 assert(!ti->instance_init); 349 assert(!ti->instance_post_init); 350 assert(!ti->instance_finalize); 351 assert(!ti->num_interfaces); 352 } 353 ti->class = g_malloc0(ti->class_size); 354 355 parent = type_get_parent(ti); 356 if (parent) { 357 type_initialize(parent); 358 GSList *e; 359 int i; 360 361 g_assert(parent->class_size <= ti->class_size); 362 g_assert(parent->instance_size <= ti->instance_size); 363 memcpy(ti->class, parent->class, parent->class_size); 364 ti->class->interfaces = NULL; 365 366 for (e = parent->class->interfaces; e; e = e->next) { 367 InterfaceClass *iface = e->data; 368 ObjectClass *klass = OBJECT_CLASS(iface); 369 370 type_initialize_interface(ti, iface->interface_type, klass->type); 371 } 372 373 for (i = 0; i < ti->num_interfaces; i++) { 374 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename); 375 if (!t) { 376 error_report("missing interface '%s' for object '%s'", 377 ti->interfaces[i].typename, parent->name); 378 abort(); 379 } 380 for (e = ti->class->interfaces; e; e = e->next) { 381 TypeImpl *target_type = OBJECT_CLASS(e->data)->type; 382 383 if (type_is_ancestor(target_type, t)) { 384 break; 385 } 386 } 387 388 if (e) { 389 continue; 390 } 391 392 type_initialize_interface(ti, t, t); 393 } 394 } 395 396 ti->class->properties = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, 397 object_property_free); 398 399 ti->class->type = ti; 400 401 while (parent) { 402 if (parent->class_base_init) { 403 parent->class_base_init(ti->class, ti->class_data); 404 } 405 parent = type_get_parent(parent); 406 } 407 408 if (ti->class_init) { 409 ti->class_init(ti->class, ti->class_data); 410 } 411 } 412 413 static void object_init_with_type(Object *obj, TypeImpl *ti) 414 { 415 if (type_has_parent(ti)) { 416 object_init_with_type(obj, type_get_parent(ti)); 417 } 418 419 if (ti->instance_init) { 420 ti->instance_init(obj); 421 } 422 } 423 424 static void object_post_init_with_type(Object *obj, TypeImpl *ti) 425 { 426 if (ti->instance_post_init) { 427 ti->instance_post_init(obj); 428 } 429 430 if (type_has_parent(ti)) { 431 object_post_init_with_type(obj, type_get_parent(ti)); 432 } 433 } 434 435 bool object_apply_global_props(Object *obj, const GPtrArray *props, 436 Error **errp) 437 { 438 int i; 439 440 if (!props) { 441 return true; 442 } 443 444 for (i = 0; i < props->len; i++) { 445 GlobalProperty *p = g_ptr_array_index(props, i); 446 Error *err = NULL; 447 448 if (object_dynamic_cast(obj, p->driver) == NULL) { 449 continue; 450 } 451 if (p->optional && !object_property_find(obj, p->property)) { 452 continue; 453 } 454 p->used = true; 455 if (!object_property_parse(obj, p->property, p->value, &err)) { 456 error_prepend(&err, "can't apply global %s.%s=%s: ", 457 p->driver, p->property, p->value); 458 /* 459 * If errp != NULL, propagate error and return. 460 * If errp == NULL, report a warning, but keep going 461 * with the remaining globals. 462 */ 463 if (errp) { 464 error_propagate(errp, err); 465 return false; 466 } else { 467 warn_report_err(err); 468 } 469 } 470 } 471 472 return true; 473 } 474 475 /* 476 * Global property defaults 477 * Slot 0: accelerator's global property defaults 478 * Slot 1: machine's global property defaults 479 * Slot 2: global properties from legacy command line option 480 * Each is a GPtrArray of of GlobalProperty. 481 * Applied in order, later entries override earlier ones. 482 */ 483 static GPtrArray *object_compat_props[3]; 484 485 /* 486 * Retrieve @GPtrArray for global property defined with options 487 * other than "-global". These are generally used for syntactic 488 * sugar and legacy command line options. 489 */ 490 void object_register_sugar_prop(const char *driver, const char *prop, 491 const char *value, bool optional) 492 { 493 GlobalProperty *g; 494 if (!object_compat_props[2]) { 495 object_compat_props[2] = g_ptr_array_new(); 496 } 497 g = g_new0(GlobalProperty, 1); 498 g->driver = g_strdup(driver); 499 g->property = g_strdup(prop); 500 g->value = g_strdup(value); 501 g->optional = optional; 502 g_ptr_array_add(object_compat_props[2], g); 503 } 504 505 /* 506 * Set machine's global property defaults to @compat_props. 507 * May be called at most once. 508 */ 509 void object_set_machine_compat_props(GPtrArray *compat_props) 510 { 511 assert(!object_compat_props[1]); 512 object_compat_props[1] = compat_props; 513 } 514 515 /* 516 * Set accelerator's global property defaults to @compat_props. 517 * May be called at most once. 518 */ 519 void object_set_accelerator_compat_props(GPtrArray *compat_props) 520 { 521 assert(!object_compat_props[0]); 522 object_compat_props[0] = compat_props; 523 } 524 525 void object_apply_compat_props(Object *obj) 526 { 527 int i; 528 529 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) { 530 object_apply_global_props(obj, object_compat_props[i], 531 i == 2 ? &error_fatal : &error_abort); 532 } 533 } 534 535 static void object_class_property_init_all(Object *obj) 536 { 537 ObjectPropertyIterator iter; 538 ObjectProperty *prop; 539 540 object_class_property_iter_init(&iter, object_get_class(obj)); 541 while ((prop = object_property_iter_next(&iter))) { 542 if (prop->init) { 543 prop->init(obj, prop); 544 } 545 } 546 } 547 548 static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type) 549 { 550 type_initialize(type); 551 552 g_assert(type->instance_size >= sizeof(Object)); 553 g_assert(type->abstract == false); 554 g_assert(size >= type->instance_size); 555 556 memset(obj, 0, type->instance_size); 557 obj->class = type->class; 558 object_ref(obj); 559 object_class_property_init_all(obj); 560 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal, 561 NULL, object_property_free); 562 object_init_with_type(obj, type); 563 object_post_init_with_type(obj, type); 564 } 565 566 void object_initialize(void *data, size_t size, const char *typename) 567 { 568 TypeImpl *type = type_get_by_name(typename); 569 570 #ifdef CONFIG_MODULES 571 if (!type) { 572 int rv = module_load_qom(typename, &error_fatal); 573 if (rv > 0) { 574 type = type_get_by_name(typename); 575 } else { 576 error_report("missing object type '%s'", typename); 577 exit(1); 578 } 579 } 580 #endif 581 if (!type) { 582 error_report("missing object type '%s'", typename); 583 abort(); 584 } 585 586 object_initialize_with_type(data, size, type); 587 } 588 589 bool object_initialize_child_with_props(Object *parentobj, 590 const char *propname, 591 void *childobj, size_t size, 592 const char *type, 593 Error **errp, ...) 594 { 595 va_list vargs; 596 bool ok; 597 598 va_start(vargs, errp); 599 ok = object_initialize_child_with_propsv(parentobj, propname, 600 childobj, size, type, errp, 601 vargs); 602 va_end(vargs); 603 return ok; 604 } 605 606 bool object_initialize_child_with_propsv(Object *parentobj, 607 const char *propname, 608 void *childobj, size_t size, 609 const char *type, 610 Error **errp, va_list vargs) 611 { 612 bool ok = false; 613 Object *obj; 614 UserCreatable *uc; 615 616 object_initialize(childobj, size, type); 617 obj = OBJECT(childobj); 618 619 if (!object_set_propv(obj, errp, vargs)) { 620 goto out; 621 } 622 623 object_property_add_child(parentobj, propname, obj); 624 625 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE); 626 if (uc) { 627 if (!user_creatable_complete(uc, errp)) { 628 object_unparent(obj); 629 goto out; 630 } 631 } 632 633 ok = true; 634 635 out: 636 /* 637 * We want @obj's reference to be 1 on success, 0 on failure. 638 * On success, it's 2: one taken by object_initialize(), and one 639 * by object_property_add_child(). 640 * On failure in object_initialize() or earlier, it's 1. 641 * On failure afterwards, it's also 1: object_unparent() releases 642 * the reference taken by object_property_add_child(). 643 */ 644 object_unref(obj); 645 return ok; 646 } 647 648 void object_initialize_child_internal(Object *parent, 649 const char *propname, 650 void *child, size_t size, 651 const char *type) 652 { 653 object_initialize_child_with_props(parent, propname, child, size, type, 654 &error_abort, NULL); 655 } 656 657 static inline bool object_property_is_child(ObjectProperty *prop) 658 { 659 return strstart(prop->type, "child<", NULL); 660 } 661 662 static void object_property_del_all(Object *obj) 663 { 664 g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL); 665 ObjectProperty *prop; 666 ObjectPropertyIterator iter; 667 bool released; 668 669 do { 670 released = false; 671 object_property_iter_init(&iter, obj); 672 while ((prop = object_property_iter_next(&iter)) != NULL) { 673 if (g_hash_table_add(done, prop)) { 674 if (prop->release) { 675 prop->release(obj, prop->name, prop->opaque); 676 released = true; 677 break; 678 } 679 } 680 } 681 } while (released); 682 683 g_hash_table_unref(obj->properties); 684 } 685 686 static void object_property_del_child(Object *obj, Object *child) 687 { 688 ObjectProperty *prop; 689 GHashTableIter iter; 690 gpointer key, value; 691 692 g_hash_table_iter_init(&iter, obj->properties); 693 while (g_hash_table_iter_next(&iter, &key, &value)) { 694 prop = value; 695 if (object_property_is_child(prop) && prop->opaque == child) { 696 if (prop->release) { 697 prop->release(obj, prop->name, prop->opaque); 698 prop->release = NULL; 699 } 700 break; 701 } 702 } 703 g_hash_table_iter_init(&iter, obj->properties); 704 while (g_hash_table_iter_next(&iter, &key, &value)) { 705 prop = value; 706 if (object_property_is_child(prop) && prop->opaque == child) { 707 g_hash_table_iter_remove(&iter); 708 break; 709 } 710 } 711 } 712 713 void object_unparent(Object *obj) 714 { 715 if (obj->parent) { 716 object_property_del_child(obj->parent, obj); 717 } 718 } 719 720 static void object_deinit(Object *obj, TypeImpl *type) 721 { 722 if (type->instance_finalize) { 723 type->instance_finalize(obj); 724 } 725 726 if (type_has_parent(type)) { 727 object_deinit(obj, type_get_parent(type)); 728 } 729 } 730 731 static void object_finalize(void *data) 732 { 733 Object *obj = data; 734 TypeImpl *ti = obj->class->type; 735 736 object_property_del_all(obj); 737 object_deinit(obj, ti); 738 739 g_assert(obj->ref == 0); 740 g_assert(obj->parent == NULL); 741 if (obj->free) { 742 obj->free(obj); 743 } 744 } 745 746 /* Find the minimum alignment guaranteed by the system malloc. */ 747 #if __STDC_VERSION__ >= 201112L 748 typedef max_align_t qemu_max_align_t; 749 #else 750 typedef union { 751 long l; 752 void *p; 753 double d; 754 long double ld; 755 } qemu_max_align_t; 756 #endif 757 758 static Object *object_new_with_type(Type type) 759 { 760 Object *obj; 761 size_t size, align; 762 void (*obj_free)(void *); 763 764 g_assert(type != NULL); 765 type_initialize(type); 766 767 size = type->instance_size; 768 align = type->instance_align; 769 770 /* 771 * Do not use qemu_memalign unless required. Depending on the 772 * implementation, extra alignment implies extra overhead. 773 */ 774 if (likely(align <= __alignof__(qemu_max_align_t))) { 775 obj = g_malloc(size); 776 obj_free = g_free; 777 } else { 778 obj = qemu_memalign(align, size); 779 obj_free = qemu_vfree; 780 } 781 782 object_initialize_with_type(obj, size, type); 783 obj->free = obj_free; 784 785 return obj; 786 } 787 788 Object *object_new_with_class(ObjectClass *klass) 789 { 790 return object_new_with_type(klass->type); 791 } 792 793 Object *object_new(const char *typename) 794 { 795 TypeImpl *ti = type_get_by_name(typename); 796 797 return object_new_with_type(ti); 798 } 799 800 801 Object *object_new_with_props(const char *typename, 802 Object *parent, 803 const char *id, 804 Error **errp, 805 ...) 806 { 807 va_list vargs; 808 Object *obj; 809 810 va_start(vargs, errp); 811 obj = object_new_with_propv(typename, parent, id, errp, vargs); 812 va_end(vargs); 813 814 return obj; 815 } 816 817 818 Object *object_new_with_propv(const char *typename, 819 Object *parent, 820 const char *id, 821 Error **errp, 822 va_list vargs) 823 { 824 Object *obj; 825 ObjectClass *klass; 826 UserCreatable *uc; 827 828 klass = object_class_by_name(typename); 829 if (!klass) { 830 error_setg(errp, "invalid object type: %s", typename); 831 return NULL; 832 } 833 834 if (object_class_is_abstract(klass)) { 835 error_setg(errp, "object type '%s' is abstract", typename); 836 return NULL; 837 } 838 obj = object_new_with_type(klass->type); 839 840 if (!object_set_propv(obj, errp, vargs)) { 841 goto error; 842 } 843 844 if (id != NULL) { 845 object_property_add_child(parent, id, obj); 846 } 847 848 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE); 849 if (uc) { 850 if (!user_creatable_complete(uc, errp)) { 851 if (id != NULL) { 852 object_unparent(obj); 853 } 854 goto error; 855 } 856 } 857 858 object_unref(obj); 859 return obj; 860 861 error: 862 object_unref(obj); 863 return NULL; 864 } 865 866 867 bool object_set_props(Object *obj, 868 Error **errp, 869 ...) 870 { 871 va_list vargs; 872 bool ret; 873 874 va_start(vargs, errp); 875 ret = object_set_propv(obj, errp, vargs); 876 va_end(vargs); 877 878 return ret; 879 } 880 881 882 bool object_set_propv(Object *obj, 883 Error **errp, 884 va_list vargs) 885 { 886 const char *propname; 887 888 propname = va_arg(vargs, char *); 889 while (propname != NULL) { 890 const char *value = va_arg(vargs, char *); 891 892 g_assert(value != NULL); 893 if (!object_property_parse(obj, propname, value, errp)) { 894 return false; 895 } 896 propname = va_arg(vargs, char *); 897 } 898 899 return true; 900 } 901 902 903 Object *object_dynamic_cast(Object *obj, const char *typename) 904 { 905 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) { 906 return obj; 907 } 908 909 return NULL; 910 } 911 912 Object *object_dynamic_cast_assert(Object *obj, const char *typename, 913 const char *file, int line, const char *func) 914 { 915 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)", 916 typename, file, line, func); 917 918 #ifdef CONFIG_QOM_CAST_DEBUG 919 int i; 920 Object *inst; 921 922 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) { 923 if (qatomic_read(&obj->class->object_cast_cache[i]) == typename) { 924 goto out; 925 } 926 } 927 928 inst = object_dynamic_cast(obj, typename); 929 930 if (!inst && obj) { 931 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", 932 file, line, func, obj, typename); 933 abort(); 934 } 935 936 assert(obj == inst); 937 938 if (obj && obj == inst) { 939 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { 940 qatomic_set(&obj->class->object_cast_cache[i - 1], 941 qatomic_read(&obj->class->object_cast_cache[i])); 942 } 943 qatomic_set(&obj->class->object_cast_cache[i - 1], typename); 944 } 945 946 out: 947 #endif 948 return obj; 949 } 950 951 ObjectClass *object_class_dynamic_cast(ObjectClass *class, 952 const char *typename) 953 { 954 ObjectClass *ret = NULL; 955 TypeImpl *target_type; 956 TypeImpl *type; 957 958 if (!class) { 959 return NULL; 960 } 961 962 /* A simple fast path that can trigger a lot for leaf classes. */ 963 type = class->type; 964 if (type->name == typename) { 965 return class; 966 } 967 968 target_type = type_get_by_name(typename); 969 if (!target_type) { 970 /* target class type unknown, so fail the cast */ 971 return NULL; 972 } 973 974 if (type->class->interfaces && 975 type_is_ancestor(target_type, type_interface)) { 976 int found = 0; 977 GSList *i; 978 979 for (i = class->interfaces; i; i = i->next) { 980 ObjectClass *target_class = i->data; 981 982 if (type_is_ancestor(target_class->type, target_type)) { 983 ret = target_class; 984 found++; 985 } 986 } 987 988 /* The match was ambiguous, don't allow a cast */ 989 if (found > 1) { 990 ret = NULL; 991 } 992 } else if (type_is_ancestor(type, target_type)) { 993 ret = class; 994 } 995 996 return ret; 997 } 998 999 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, 1000 const char *typename, 1001 const char *file, int line, 1002 const char *func) 1003 { 1004 ObjectClass *ret; 1005 1006 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)", 1007 typename, file, line, func); 1008 1009 #ifdef CONFIG_QOM_CAST_DEBUG 1010 int i; 1011 1012 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) { 1013 if (qatomic_read(&class->class_cast_cache[i]) == typename) { 1014 ret = class; 1015 goto out; 1016 } 1017 } 1018 #else 1019 if (!class || !class->interfaces) { 1020 return class; 1021 } 1022 #endif 1023 1024 ret = object_class_dynamic_cast(class, typename); 1025 if (!ret && class) { 1026 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", 1027 file, line, func, class, typename); 1028 abort(); 1029 } 1030 1031 #ifdef CONFIG_QOM_CAST_DEBUG 1032 if (class && ret == class) { 1033 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { 1034 qatomic_set(&class->class_cast_cache[i - 1], 1035 qatomic_read(&class->class_cast_cache[i])); 1036 } 1037 qatomic_set(&class->class_cast_cache[i - 1], typename); 1038 } 1039 out: 1040 #endif 1041 return ret; 1042 } 1043 1044 const char *object_get_typename(const Object *obj) 1045 { 1046 return obj->class->type->name; 1047 } 1048 1049 ObjectClass *object_get_class(Object *obj) 1050 { 1051 return obj->class; 1052 } 1053 1054 bool object_class_is_abstract(ObjectClass *klass) 1055 { 1056 return klass->type->abstract; 1057 } 1058 1059 const char *object_class_get_name(ObjectClass *klass) 1060 { 1061 return klass->type->name; 1062 } 1063 1064 ObjectClass *object_class_by_name(const char *typename) 1065 { 1066 TypeImpl *type = type_get_by_name(typename); 1067 1068 if (!type) { 1069 return NULL; 1070 } 1071 1072 type_initialize(type); 1073 1074 return type->class; 1075 } 1076 1077 ObjectClass *module_object_class_by_name(const char *typename) 1078 { 1079 ObjectClass *oc; 1080 1081 oc = object_class_by_name(typename); 1082 #ifdef CONFIG_MODULES 1083 if (!oc) { 1084 Error *local_err = NULL; 1085 int rv = module_load_qom(typename, &local_err); 1086 if (rv > 0) { 1087 oc = object_class_by_name(typename); 1088 } else if (rv < 0) { 1089 error_report_err(local_err); 1090 } 1091 } 1092 #endif 1093 return oc; 1094 } 1095 1096 ObjectClass *object_class_get_parent(ObjectClass *class) 1097 { 1098 TypeImpl *type = type_get_parent(class->type); 1099 1100 if (!type) { 1101 return NULL; 1102 } 1103 1104 type_initialize(type); 1105 1106 return type->class; 1107 } 1108 1109 typedef struct OCFData 1110 { 1111 void (*fn)(ObjectClass *klass, void *opaque); 1112 const char *implements_type; 1113 bool include_abstract; 1114 void *opaque; 1115 } OCFData; 1116 1117 static void object_class_foreach_tramp(gpointer key, gpointer value, 1118 gpointer opaque) 1119 { 1120 OCFData *data = opaque; 1121 TypeImpl *type = value; 1122 ObjectClass *k; 1123 1124 type_initialize(type); 1125 k = type->class; 1126 1127 if (!data->include_abstract && type->abstract) { 1128 return; 1129 } 1130 1131 if (data->implements_type && 1132 !object_class_dynamic_cast(k, data->implements_type)) { 1133 return; 1134 } 1135 1136 data->fn(k, data->opaque); 1137 } 1138 1139 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), 1140 const char *implements_type, bool include_abstract, 1141 void *opaque) 1142 { 1143 OCFData data = { fn, implements_type, include_abstract, opaque }; 1144 1145 enumerating_types = true; 1146 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); 1147 enumerating_types = false; 1148 } 1149 1150 static int do_object_child_foreach(Object *obj, 1151 int (*fn)(Object *child, void *opaque), 1152 void *opaque, bool recurse) 1153 { 1154 GHashTableIter iter; 1155 ObjectProperty *prop; 1156 int ret = 0; 1157 1158 g_hash_table_iter_init(&iter, obj->properties); 1159 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { 1160 if (object_property_is_child(prop)) { 1161 Object *child = prop->opaque; 1162 1163 ret = fn(child, opaque); 1164 if (ret != 0) { 1165 break; 1166 } 1167 if (recurse) { 1168 ret = do_object_child_foreach(child, fn, opaque, true); 1169 if (ret != 0) { 1170 break; 1171 } 1172 } 1173 } 1174 } 1175 return ret; 1176 } 1177 1178 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), 1179 void *opaque) 1180 { 1181 return do_object_child_foreach(obj, fn, opaque, false); 1182 } 1183 1184 int object_child_foreach_recursive(Object *obj, 1185 int (*fn)(Object *child, void *opaque), 1186 void *opaque) 1187 { 1188 return do_object_child_foreach(obj, fn, opaque, true); 1189 } 1190 1191 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) 1192 { 1193 GSList **list = opaque; 1194 1195 *list = g_slist_prepend(*list, klass); 1196 } 1197 1198 GSList *object_class_get_list(const char *implements_type, 1199 bool include_abstract) 1200 { 1201 GSList *list = NULL; 1202 1203 object_class_foreach(object_class_get_list_tramp, 1204 implements_type, include_abstract, &list); 1205 return list; 1206 } 1207 1208 static gint object_class_cmp(gconstpointer a, gconstpointer b) 1209 { 1210 return strcasecmp(object_class_get_name((ObjectClass *)a), 1211 object_class_get_name((ObjectClass *)b)); 1212 } 1213 1214 GSList *object_class_get_list_sorted(const char *implements_type, 1215 bool include_abstract) 1216 { 1217 return g_slist_sort(object_class_get_list(implements_type, include_abstract), 1218 object_class_cmp); 1219 } 1220 1221 Object *object_ref(void *objptr) 1222 { 1223 Object *obj = OBJECT(objptr); 1224 uint32_t ref; 1225 1226 if (!obj) { 1227 return NULL; 1228 } 1229 ref = qatomic_fetch_inc(&obj->ref); 1230 /* Assert waaay before the integer overflows */ 1231 g_assert(ref < INT_MAX); 1232 return obj; 1233 } 1234 1235 void object_unref(void *objptr) 1236 { 1237 Object *obj = OBJECT(objptr); 1238 if (!obj) { 1239 return; 1240 } 1241 g_assert(obj->ref > 0); 1242 1243 /* parent always holds a reference to its children */ 1244 if (qatomic_fetch_dec(&obj->ref) == 1) { 1245 object_finalize(obj); 1246 } 1247 } 1248 1249 ObjectProperty * 1250 object_property_try_add(Object *obj, const char *name, const char *type, 1251 ObjectPropertyAccessor *get, 1252 ObjectPropertyAccessor *set, 1253 ObjectPropertyRelease *release, 1254 void *opaque, Error **errp) 1255 { 1256 ObjectProperty *prop; 1257 size_t name_len = strlen(name); 1258 1259 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) { 1260 int i; 1261 ObjectProperty *ret = NULL; 1262 char *name_no_array = g_strdup(name); 1263 1264 name_no_array[name_len - 3] = '\0'; 1265 for (i = 0; i < INT16_MAX; ++i) { 1266 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i); 1267 1268 ret = object_property_try_add(obj, full_name, type, get, set, 1269 release, opaque, NULL); 1270 g_free(full_name); 1271 if (ret) { 1272 break; 1273 } 1274 } 1275 g_free(name_no_array); 1276 assert(ret); 1277 return ret; 1278 } 1279 1280 if (object_property_find(obj, name) != NULL) { 1281 error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')", 1282 name, object_get_typename(obj)); 1283 return NULL; 1284 } 1285 1286 prop = g_malloc0(sizeof(*prop)); 1287 1288 prop->name = g_strdup(name); 1289 prop->type = g_strdup(type); 1290 1291 prop->get = get; 1292 prop->set = set; 1293 prop->release = release; 1294 prop->opaque = opaque; 1295 1296 g_hash_table_insert(obj->properties, prop->name, prop); 1297 return prop; 1298 } 1299 1300 ObjectProperty * 1301 object_property_add(Object *obj, const char *name, const char *type, 1302 ObjectPropertyAccessor *get, 1303 ObjectPropertyAccessor *set, 1304 ObjectPropertyRelease *release, 1305 void *opaque) 1306 { 1307 return object_property_try_add(obj, name, type, get, set, release, 1308 opaque, &error_abort); 1309 } 1310 1311 ObjectProperty * 1312 object_class_property_add(ObjectClass *klass, 1313 const char *name, 1314 const char *type, 1315 ObjectPropertyAccessor *get, 1316 ObjectPropertyAccessor *set, 1317 ObjectPropertyRelease *release, 1318 void *opaque) 1319 { 1320 ObjectProperty *prop; 1321 1322 assert(!object_class_property_find(klass, name)); 1323 1324 prop = g_malloc0(sizeof(*prop)); 1325 1326 prop->name = g_strdup(name); 1327 prop->type = g_strdup(type); 1328 1329 prop->get = get; 1330 prop->set = set; 1331 prop->release = release; 1332 prop->opaque = opaque; 1333 1334 g_hash_table_insert(klass->properties, prop->name, prop); 1335 1336 return prop; 1337 } 1338 1339 ObjectProperty *object_property_find(Object *obj, const char *name) 1340 { 1341 ObjectProperty *prop; 1342 ObjectClass *klass = object_get_class(obj); 1343 1344 prop = object_class_property_find(klass, name); 1345 if (prop) { 1346 return prop; 1347 } 1348 1349 return g_hash_table_lookup(obj->properties, name); 1350 } 1351 1352 ObjectProperty *object_property_find_err(Object *obj, const char *name, 1353 Error **errp) 1354 { 1355 ObjectProperty *prop = object_property_find(obj, name); 1356 if (!prop) { 1357 error_setg(errp, "Property '%s.%s' not found", 1358 object_get_typename(obj), name); 1359 } 1360 return prop; 1361 } 1362 1363 void object_property_iter_init(ObjectPropertyIterator *iter, 1364 Object *obj) 1365 { 1366 g_hash_table_iter_init(&iter->iter, obj->properties); 1367 iter->nextclass = object_get_class(obj); 1368 } 1369 1370 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter) 1371 { 1372 gpointer key, val; 1373 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) { 1374 if (!iter->nextclass) { 1375 return NULL; 1376 } 1377 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties); 1378 iter->nextclass = object_class_get_parent(iter->nextclass); 1379 } 1380 return val; 1381 } 1382 1383 void object_class_property_iter_init(ObjectPropertyIterator *iter, 1384 ObjectClass *klass) 1385 { 1386 g_hash_table_iter_init(&iter->iter, klass->properties); 1387 iter->nextclass = object_class_get_parent(klass); 1388 } 1389 1390 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name) 1391 { 1392 ObjectClass *parent_klass; 1393 1394 parent_klass = object_class_get_parent(klass); 1395 if (parent_klass) { 1396 ObjectProperty *prop = 1397 object_class_property_find(parent_klass, name); 1398 if (prop) { 1399 return prop; 1400 } 1401 } 1402 1403 return g_hash_table_lookup(klass->properties, name); 1404 } 1405 1406 ObjectProperty *object_class_property_find_err(ObjectClass *klass, 1407 const char *name, 1408 Error **errp) 1409 { 1410 ObjectProperty *prop = object_class_property_find(klass, name); 1411 if (!prop) { 1412 error_setg(errp, "Property '.%s' not found", name); 1413 } 1414 return prop; 1415 } 1416 1417 1418 void object_property_del(Object *obj, const char *name) 1419 { 1420 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name); 1421 1422 if (prop->release) { 1423 prop->release(obj, name, prop->opaque); 1424 } 1425 g_hash_table_remove(obj->properties, name); 1426 } 1427 1428 bool object_property_get(Object *obj, const char *name, Visitor *v, 1429 Error **errp) 1430 { 1431 Error *err = NULL; 1432 ObjectProperty *prop = object_property_find_err(obj, name, errp); 1433 1434 if (prop == NULL) { 1435 return false; 1436 } 1437 1438 if (!prop->get) { 1439 error_setg(errp, "Property '%s.%s' is not readable", 1440 object_get_typename(obj), name); 1441 return false; 1442 } 1443 prop->get(obj, v, name, prop->opaque, &err); 1444 error_propagate(errp, err); 1445 return !err; 1446 } 1447 1448 bool object_property_set(Object *obj, const char *name, Visitor *v, 1449 Error **errp) 1450 { 1451 ERRP_GUARD(); 1452 ObjectProperty *prop = object_property_find_err(obj, name, errp); 1453 1454 if (prop == NULL) { 1455 return false; 1456 } 1457 1458 if (!prop->set) { 1459 error_setg(errp, "Property '%s.%s' is not writable", 1460 object_get_typename(obj), name); 1461 return false; 1462 } 1463 prop->set(obj, v, name, prop->opaque, errp); 1464 return !*errp; 1465 } 1466 1467 bool object_property_set_str(Object *obj, const char *name, 1468 const char *value, Error **errp) 1469 { 1470 QString *qstr = qstring_from_str(value); 1471 bool ok = object_property_set_qobject(obj, name, QOBJECT(qstr), errp); 1472 1473 qobject_unref(qstr); 1474 return ok; 1475 } 1476 1477 char *object_property_get_str(Object *obj, const char *name, 1478 Error **errp) 1479 { 1480 QObject *ret = object_property_get_qobject(obj, name, errp); 1481 QString *qstring; 1482 char *retval; 1483 1484 if (!ret) { 1485 return NULL; 1486 } 1487 qstring = qobject_to(QString, ret); 1488 if (!qstring) { 1489 error_setg(errp, "Invalid parameter type for '%s', expected: string", 1490 name); 1491 retval = NULL; 1492 } else { 1493 retval = g_strdup(qstring_get_str(qstring)); 1494 } 1495 1496 qobject_unref(ret); 1497 return retval; 1498 } 1499 1500 bool object_property_set_link(Object *obj, const char *name, 1501 Object *value, Error **errp) 1502 { 1503 g_autofree char *path = NULL; 1504 1505 if (value) { 1506 path = object_get_canonical_path(value); 1507 } 1508 return object_property_set_str(obj, name, path ?: "", errp); 1509 } 1510 1511 Object *object_property_get_link(Object *obj, const char *name, 1512 Error **errp) 1513 { 1514 char *str = object_property_get_str(obj, name, errp); 1515 Object *target = NULL; 1516 1517 if (str && *str) { 1518 target = object_resolve_path(str, NULL); 1519 if (!target) { 1520 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1521 "Device '%s' not found", str); 1522 } 1523 } 1524 1525 g_free(str); 1526 return target; 1527 } 1528 1529 bool object_property_set_bool(Object *obj, const char *name, 1530 bool value, Error **errp) 1531 { 1532 QBool *qbool = qbool_from_bool(value); 1533 bool ok = object_property_set_qobject(obj, name, QOBJECT(qbool), errp); 1534 1535 qobject_unref(qbool); 1536 return ok; 1537 } 1538 1539 bool object_property_get_bool(Object *obj, const char *name, 1540 Error **errp) 1541 { 1542 QObject *ret = object_property_get_qobject(obj, name, errp); 1543 QBool *qbool; 1544 bool retval; 1545 1546 if (!ret) { 1547 return false; 1548 } 1549 qbool = qobject_to(QBool, ret); 1550 if (!qbool) { 1551 error_setg(errp, "Invalid parameter type for '%s', expected: boolean", 1552 name); 1553 retval = false; 1554 } else { 1555 retval = qbool_get_bool(qbool); 1556 } 1557 1558 qobject_unref(ret); 1559 return retval; 1560 } 1561 1562 bool object_property_set_int(Object *obj, const char *name, 1563 int64_t value, Error **errp) 1564 { 1565 QNum *qnum = qnum_from_int(value); 1566 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp); 1567 1568 qobject_unref(qnum); 1569 return ok; 1570 } 1571 1572 int64_t object_property_get_int(Object *obj, const char *name, 1573 Error **errp) 1574 { 1575 QObject *ret = object_property_get_qobject(obj, name, errp); 1576 QNum *qnum; 1577 int64_t retval; 1578 1579 if (!ret) { 1580 return -1; 1581 } 1582 1583 qnum = qobject_to(QNum, ret); 1584 if (!qnum || !qnum_get_try_int(qnum, &retval)) { 1585 error_setg(errp, "Invalid parameter type for '%s', expected: int", 1586 name); 1587 retval = -1; 1588 } 1589 1590 qobject_unref(ret); 1591 return retval; 1592 } 1593 1594 static void object_property_init_defval(Object *obj, ObjectProperty *prop) 1595 { 1596 Visitor *v = qobject_input_visitor_new(prop->defval); 1597 1598 assert(prop->set != NULL); 1599 prop->set(obj, v, prop->name, prop->opaque, &error_abort); 1600 1601 visit_free(v); 1602 } 1603 1604 static void object_property_set_default(ObjectProperty *prop, QObject *defval) 1605 { 1606 assert(!prop->defval); 1607 assert(!prop->init); 1608 1609 prop->defval = defval; 1610 prop->init = object_property_init_defval; 1611 } 1612 1613 void object_property_set_default_bool(ObjectProperty *prop, bool value) 1614 { 1615 object_property_set_default(prop, QOBJECT(qbool_from_bool(value))); 1616 } 1617 1618 void object_property_set_default_str(ObjectProperty *prop, const char *value) 1619 { 1620 object_property_set_default(prop, QOBJECT(qstring_from_str(value))); 1621 } 1622 1623 void object_property_set_default_list(ObjectProperty *prop) 1624 { 1625 object_property_set_default(prop, QOBJECT(qlist_new())); 1626 } 1627 1628 void object_property_set_default_int(ObjectProperty *prop, int64_t value) 1629 { 1630 object_property_set_default(prop, QOBJECT(qnum_from_int(value))); 1631 } 1632 1633 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value) 1634 { 1635 object_property_set_default(prop, QOBJECT(qnum_from_uint(value))); 1636 } 1637 1638 bool object_property_set_uint(Object *obj, const char *name, 1639 uint64_t value, Error **errp) 1640 { 1641 QNum *qnum = qnum_from_uint(value); 1642 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp); 1643 1644 qobject_unref(qnum); 1645 return ok; 1646 } 1647 1648 uint64_t object_property_get_uint(Object *obj, const char *name, 1649 Error **errp) 1650 { 1651 QObject *ret = object_property_get_qobject(obj, name, errp); 1652 QNum *qnum; 1653 uint64_t retval; 1654 1655 if (!ret) { 1656 return 0; 1657 } 1658 qnum = qobject_to(QNum, ret); 1659 if (!qnum || !qnum_get_try_uint(qnum, &retval)) { 1660 error_setg(errp, "Invalid parameter type for '%s', expected: uint", 1661 name); 1662 retval = 0; 1663 } 1664 1665 qobject_unref(ret); 1666 return retval; 1667 } 1668 1669 typedef struct EnumProperty { 1670 const QEnumLookup *lookup; 1671 int (*get)(Object *, Error **); 1672 void (*set)(Object *, int, Error **); 1673 } EnumProperty; 1674 1675 int object_property_get_enum(Object *obj, const char *name, 1676 const char *typename, Error **errp) 1677 { 1678 char *str; 1679 int ret; 1680 ObjectProperty *prop = object_property_find_err(obj, name, errp); 1681 EnumProperty *enumprop; 1682 1683 if (prop == NULL) { 1684 return -1; 1685 } 1686 1687 if (!g_str_equal(prop->type, typename)) { 1688 error_setg(errp, "Property %s on %s is not '%s' enum type", 1689 name, object_class_get_name( 1690 object_get_class(obj)), typename); 1691 return -1; 1692 } 1693 1694 enumprop = prop->opaque; 1695 1696 str = object_property_get_str(obj, name, errp); 1697 if (!str) { 1698 return -1; 1699 } 1700 1701 ret = qapi_enum_parse(enumprop->lookup, str, -1, errp); 1702 g_free(str); 1703 1704 return ret; 1705 } 1706 1707 bool object_property_parse(Object *obj, const char *name, 1708 const char *string, Error **errp) 1709 { 1710 Visitor *v = string_input_visitor_new(string); 1711 bool ok = object_property_set(obj, name, v, errp); 1712 1713 visit_free(v); 1714 return ok; 1715 } 1716 1717 char *object_property_print(Object *obj, const char *name, bool human, 1718 Error **errp) 1719 { 1720 Visitor *v; 1721 char *string = NULL; 1722 1723 v = string_output_visitor_new(human, &string); 1724 if (!object_property_get(obj, name, v, errp)) { 1725 goto out; 1726 } 1727 1728 visit_complete(v, &string); 1729 1730 out: 1731 visit_free(v); 1732 return string; 1733 } 1734 1735 const char *object_property_get_type(Object *obj, const char *name, Error **errp) 1736 { 1737 ObjectProperty *prop = object_property_find_err(obj, name, errp); 1738 if (prop == NULL) { 1739 return NULL; 1740 } 1741 1742 return prop->type; 1743 } 1744 1745 Object *object_get_root(void) 1746 { 1747 static Object *root; 1748 1749 if (!root) { 1750 root = object_new("container"); 1751 } 1752 1753 return root; 1754 } 1755 1756 Object *object_get_objects_root(void) 1757 { 1758 return container_get(object_get_root(), "/objects"); 1759 } 1760 1761 Object *object_get_internal_root(void) 1762 { 1763 static Object *internal_root; 1764 1765 if (!internal_root) { 1766 internal_root = object_new("container"); 1767 } 1768 1769 return internal_root; 1770 } 1771 1772 static void object_get_child_property(Object *obj, Visitor *v, 1773 const char *name, void *opaque, 1774 Error **errp) 1775 { 1776 Object *child = opaque; 1777 char *path; 1778 1779 path = object_get_canonical_path(child); 1780 visit_type_str(v, name, &path, errp); 1781 g_free(path); 1782 } 1783 1784 static Object *object_resolve_child_property(Object *parent, void *opaque, 1785 const char *part) 1786 { 1787 return opaque; 1788 } 1789 1790 static void object_finalize_child_property(Object *obj, const char *name, 1791 void *opaque) 1792 { 1793 Object *child = opaque; 1794 1795 if (child->class->unparent) { 1796 (child->class->unparent)(child); 1797 } 1798 child->parent = NULL; 1799 object_unref(child); 1800 } 1801 1802 ObjectProperty * 1803 object_property_try_add_child(Object *obj, const char *name, 1804 Object *child, Error **errp) 1805 { 1806 g_autofree char *type = NULL; 1807 ObjectProperty *op; 1808 1809 assert(!child->parent); 1810 1811 type = g_strdup_printf("child<%s>", object_get_typename(child)); 1812 1813 op = object_property_try_add(obj, name, type, object_get_child_property, 1814 NULL, object_finalize_child_property, 1815 child, errp); 1816 if (!op) { 1817 return NULL; 1818 } 1819 op->resolve = object_resolve_child_property; 1820 object_ref(child); 1821 child->parent = obj; 1822 return op; 1823 } 1824 1825 ObjectProperty * 1826 object_property_add_child(Object *obj, const char *name, 1827 Object *child) 1828 { 1829 return object_property_try_add_child(obj, name, child, &error_abort); 1830 } 1831 1832 void object_property_allow_set_link(const Object *obj, const char *name, 1833 Object *val, Error **errp) 1834 { 1835 /* Allow the link to be set, always */ 1836 } 1837 1838 typedef struct { 1839 union { 1840 Object **targetp; 1841 Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */ 1842 ptrdiff_t offset; /* if OBJ_PROP_LINK_CLASS */ 1843 }; 1844 void (*check)(const Object *, const char *, Object *, Error **); 1845 ObjectPropertyLinkFlags flags; 1846 } LinkProperty; 1847 1848 static Object ** 1849 object_link_get_targetp(Object *obj, LinkProperty *lprop) 1850 { 1851 if (lprop->flags & OBJ_PROP_LINK_DIRECT) { 1852 return &lprop->target; 1853 } else if (lprop->flags & OBJ_PROP_LINK_CLASS) { 1854 return (void *)obj + lprop->offset; 1855 } else { 1856 return lprop->targetp; 1857 } 1858 } 1859 1860 static void object_get_link_property(Object *obj, Visitor *v, 1861 const char *name, void *opaque, 1862 Error **errp) 1863 { 1864 LinkProperty *lprop = opaque; 1865 Object **targetp = object_link_get_targetp(obj, lprop); 1866 char *path; 1867 1868 if (*targetp) { 1869 path = object_get_canonical_path(*targetp); 1870 visit_type_str(v, name, &path, errp); 1871 g_free(path); 1872 } else { 1873 path = (char *)""; 1874 visit_type_str(v, name, &path, errp); 1875 } 1876 } 1877 1878 /* 1879 * object_resolve_link: 1880 * 1881 * Lookup an object and ensure its type matches the link property type. This 1882 * is similar to object_resolve_path() except type verification against the 1883 * link property is performed. 1884 * 1885 * Returns: The matched object or NULL on path lookup failures. 1886 */ 1887 static Object *object_resolve_link(Object *obj, const char *name, 1888 const char *path, Error **errp) 1889 { 1890 const char *type; 1891 char *target_type; 1892 bool ambiguous = false; 1893 Object *target; 1894 1895 /* Go from link<FOO> to FOO. */ 1896 type = object_property_get_type(obj, name, NULL); 1897 target_type = g_strndup(&type[5], strlen(type) - 6); 1898 target = object_resolve_path_type(path, target_type, &ambiguous); 1899 1900 if (ambiguous) { 1901 error_setg(errp, "Path '%s' does not uniquely identify an object", 1902 path); 1903 } else if (!target) { 1904 target = object_resolve_path(path, &ambiguous); 1905 if (target || ambiguous) { 1906 error_setg(errp, "Invalid parameter type for '%s', expected: %s", 1907 name, target_type); 1908 } else { 1909 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1910 "Device '%s' not found", path); 1911 } 1912 target = NULL; 1913 } 1914 g_free(target_type); 1915 1916 return target; 1917 } 1918 1919 static void object_set_link_property(Object *obj, Visitor *v, 1920 const char *name, void *opaque, 1921 Error **errp) 1922 { 1923 Error *local_err = NULL; 1924 LinkProperty *prop = opaque; 1925 Object **targetp = object_link_get_targetp(obj, prop); 1926 Object *old_target = *targetp; 1927 Object *new_target; 1928 char *path = NULL; 1929 1930 if (!visit_type_str(v, name, &path, errp)) { 1931 return; 1932 } 1933 1934 if (*path) { 1935 new_target = object_resolve_link(obj, name, path, errp); 1936 if (!new_target) { 1937 g_free(path); 1938 return; 1939 } 1940 } else { 1941 new_target = NULL; 1942 } 1943 1944 g_free(path); 1945 1946 prop->check(obj, name, new_target, &local_err); 1947 if (local_err) { 1948 error_propagate(errp, local_err); 1949 return; 1950 } 1951 1952 *targetp = new_target; 1953 if (prop->flags & OBJ_PROP_LINK_STRONG) { 1954 object_ref(new_target); 1955 object_unref(old_target); 1956 } 1957 } 1958 1959 static Object *object_resolve_link_property(Object *parent, void *opaque, 1960 const char *part) 1961 { 1962 LinkProperty *lprop = opaque; 1963 1964 return *object_link_get_targetp(parent, lprop); 1965 } 1966 1967 static void object_release_link_property(Object *obj, const char *name, 1968 void *opaque) 1969 { 1970 LinkProperty *prop = opaque; 1971 Object **targetp = object_link_get_targetp(obj, prop); 1972 1973 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) { 1974 object_unref(*targetp); 1975 } 1976 if (!(prop->flags & OBJ_PROP_LINK_CLASS)) { 1977 g_free(prop); 1978 } 1979 } 1980 1981 static ObjectProperty * 1982 object_add_link_prop(Object *obj, const char *name, 1983 const char *type, void *ptr, 1984 void (*check)(const Object *, const char *, 1985 Object *, Error **), 1986 ObjectPropertyLinkFlags flags) 1987 { 1988 LinkProperty *prop = g_malloc(sizeof(*prop)); 1989 g_autofree char *full_type = NULL; 1990 ObjectProperty *op; 1991 1992 if (flags & OBJ_PROP_LINK_DIRECT) { 1993 prop->target = ptr; 1994 } else { 1995 prop->targetp = ptr; 1996 } 1997 prop->check = check; 1998 prop->flags = flags; 1999 2000 full_type = g_strdup_printf("link<%s>", type); 2001 2002 op = object_property_add(obj, name, full_type, 2003 object_get_link_property, 2004 check ? object_set_link_property : NULL, 2005 object_release_link_property, 2006 prop); 2007 op->resolve = object_resolve_link_property; 2008 return op; 2009 } 2010 2011 ObjectProperty * 2012 object_property_add_link(Object *obj, const char *name, 2013 const char *type, Object **targetp, 2014 void (*check)(const Object *, const char *, 2015 Object *, Error **), 2016 ObjectPropertyLinkFlags flags) 2017 { 2018 return object_add_link_prop(obj, name, type, targetp, check, flags); 2019 } 2020 2021 ObjectProperty * 2022 object_class_property_add_link(ObjectClass *oc, 2023 const char *name, 2024 const char *type, ptrdiff_t offset, 2025 void (*check)(const Object *obj, const char *name, 2026 Object *val, Error **errp), 2027 ObjectPropertyLinkFlags flags) 2028 { 2029 LinkProperty *prop = g_new0(LinkProperty, 1); 2030 char *full_type; 2031 ObjectProperty *op; 2032 2033 prop->offset = offset; 2034 prop->check = check; 2035 prop->flags = flags | OBJ_PROP_LINK_CLASS; 2036 2037 full_type = g_strdup_printf("link<%s>", type); 2038 2039 op = object_class_property_add(oc, name, full_type, 2040 object_get_link_property, 2041 check ? object_set_link_property : NULL, 2042 object_release_link_property, 2043 prop); 2044 2045 op->resolve = object_resolve_link_property; 2046 2047 g_free(full_type); 2048 return op; 2049 } 2050 2051 ObjectProperty * 2052 object_property_add_const_link(Object *obj, const char *name, 2053 Object *target) 2054 { 2055 return object_add_link_prop(obj, name, 2056 object_get_typename(target), target, 2057 NULL, OBJ_PROP_LINK_DIRECT); 2058 } 2059 2060 const char *object_get_canonical_path_component(const Object *obj) 2061 { 2062 ObjectProperty *prop = NULL; 2063 GHashTableIter iter; 2064 2065 if (obj->parent == NULL) { 2066 return NULL; 2067 } 2068 2069 g_hash_table_iter_init(&iter, obj->parent->properties); 2070 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { 2071 if (!object_property_is_child(prop)) { 2072 continue; 2073 } 2074 2075 if (prop->opaque == obj) { 2076 return prop->name; 2077 } 2078 } 2079 2080 /* obj had a parent but was not a child, should never happen */ 2081 g_assert_not_reached(); 2082 return NULL; 2083 } 2084 2085 char *object_get_canonical_path(const Object *obj) 2086 { 2087 Object *root = object_get_root(); 2088 char *newpath, *path = NULL; 2089 2090 if (obj == root) { 2091 return g_strdup("/"); 2092 } 2093 2094 do { 2095 const char *component = object_get_canonical_path_component(obj); 2096 2097 if (!component) { 2098 /* A canonical path must be complete, so discard what was 2099 * collected so far. 2100 */ 2101 g_free(path); 2102 return NULL; 2103 } 2104 2105 newpath = g_strdup_printf("/%s%s", component, path ? path : ""); 2106 g_free(path); 2107 path = newpath; 2108 obj = obj->parent; 2109 } while (obj != root); 2110 2111 return path; 2112 } 2113 2114 Object *object_resolve_path_component(Object *parent, const char *part) 2115 { 2116 ObjectProperty *prop = object_property_find(parent, part); 2117 if (prop == NULL) { 2118 return NULL; 2119 } 2120 2121 if (prop->resolve) { 2122 return prop->resolve(parent, prop->opaque, part); 2123 } else { 2124 return NULL; 2125 } 2126 } 2127 2128 static Object *object_resolve_abs_path(Object *parent, 2129 char **parts, 2130 const char *typename) 2131 { 2132 Object *child; 2133 2134 if (*parts == NULL) { 2135 return object_dynamic_cast(parent, typename); 2136 } 2137 2138 if (strcmp(*parts, "") == 0) { 2139 return object_resolve_abs_path(parent, parts + 1, typename); 2140 } 2141 2142 child = object_resolve_path_component(parent, *parts); 2143 if (!child) { 2144 return NULL; 2145 } 2146 2147 return object_resolve_abs_path(child, parts + 1, typename); 2148 } 2149 2150 static Object *object_resolve_partial_path(Object *parent, 2151 char **parts, 2152 const char *typename, 2153 bool *ambiguous) 2154 { 2155 Object *obj; 2156 GHashTableIter iter; 2157 ObjectProperty *prop; 2158 2159 obj = object_resolve_abs_path(parent, parts, typename); 2160 2161 g_hash_table_iter_init(&iter, parent->properties); 2162 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { 2163 Object *found; 2164 2165 if (!object_property_is_child(prop)) { 2166 continue; 2167 } 2168 2169 found = object_resolve_partial_path(prop->opaque, parts, 2170 typename, ambiguous); 2171 if (found) { 2172 if (obj) { 2173 *ambiguous = true; 2174 return NULL; 2175 } 2176 obj = found; 2177 } 2178 2179 if (*ambiguous) { 2180 return NULL; 2181 } 2182 } 2183 2184 return obj; 2185 } 2186 2187 Object *object_resolve_path_type(const char *path, const char *typename, 2188 bool *ambiguousp) 2189 { 2190 Object *obj; 2191 char **parts; 2192 2193 parts = g_strsplit(path, "/", 0); 2194 assert(parts); 2195 2196 if (parts[0] == NULL || strcmp(parts[0], "") != 0) { 2197 bool ambiguous = false; 2198 obj = object_resolve_partial_path(object_get_root(), parts, 2199 typename, &ambiguous); 2200 if (ambiguousp) { 2201 *ambiguousp = ambiguous; 2202 } 2203 } else { 2204 obj = object_resolve_abs_path(object_get_root(), parts + 1, typename); 2205 } 2206 2207 g_strfreev(parts); 2208 2209 return obj; 2210 } 2211 2212 Object *object_resolve_path(const char *path, bool *ambiguous) 2213 { 2214 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); 2215 } 2216 2217 Object *object_resolve_path_at(Object *parent, const char *path) 2218 { 2219 g_auto(GStrv) parts = g_strsplit(path, "/", 0); 2220 2221 if (*path == '/') { 2222 return object_resolve_abs_path(object_get_root(), parts + 1, 2223 TYPE_OBJECT); 2224 } 2225 return object_resolve_abs_path(parent, parts, TYPE_OBJECT); 2226 } 2227 2228 Object *object_resolve_type_unambiguous(const char *typename, Error **errp) 2229 { 2230 bool ambig; 2231 Object *o = object_resolve_path_type("", typename, &ambig); 2232 2233 if (ambig) { 2234 error_setg(errp, "More than one object of type %s", typename); 2235 return NULL; 2236 } 2237 if (!o) { 2238 error_setg(errp, "No object found of type %s", typename); 2239 return NULL; 2240 } 2241 return o; 2242 } 2243 2244 typedef struct StringProperty 2245 { 2246 char *(*get)(Object *, Error **); 2247 void (*set)(Object *, const char *, Error **); 2248 } StringProperty; 2249 2250 static void property_get_str(Object *obj, Visitor *v, const char *name, 2251 void *opaque, Error **errp) 2252 { 2253 StringProperty *prop = opaque; 2254 char *value; 2255 Error *err = NULL; 2256 2257 value = prop->get(obj, &err); 2258 if (err) { 2259 error_propagate(errp, err); 2260 return; 2261 } 2262 2263 visit_type_str(v, name, &value, errp); 2264 g_free(value); 2265 } 2266 2267 static void property_set_str(Object *obj, Visitor *v, const char *name, 2268 void *opaque, Error **errp) 2269 { 2270 StringProperty *prop = opaque; 2271 char *value; 2272 2273 if (!visit_type_str(v, name, &value, errp)) { 2274 return; 2275 } 2276 2277 prop->set(obj, value, errp); 2278 g_free(value); 2279 } 2280 2281 static void property_release_data(Object *obj, const char *name, 2282 void *opaque) 2283 { 2284 g_free(opaque); 2285 } 2286 2287 ObjectProperty * 2288 object_property_add_str(Object *obj, const char *name, 2289 char *(*get)(Object *, Error **), 2290 void (*set)(Object *, const char *, Error **)) 2291 { 2292 StringProperty *prop = g_malloc0(sizeof(*prop)); 2293 2294 prop->get = get; 2295 prop->set = set; 2296 2297 return object_property_add(obj, name, "string", 2298 get ? property_get_str : NULL, 2299 set ? property_set_str : NULL, 2300 property_release_data, 2301 prop); 2302 } 2303 2304 ObjectProperty * 2305 object_class_property_add_str(ObjectClass *klass, const char *name, 2306 char *(*get)(Object *, Error **), 2307 void (*set)(Object *, const char *, 2308 Error **)) 2309 { 2310 StringProperty *prop = g_malloc0(sizeof(*prop)); 2311 2312 prop->get = get; 2313 prop->set = set; 2314 2315 return object_class_property_add(klass, name, "string", 2316 get ? property_get_str : NULL, 2317 set ? property_set_str : NULL, 2318 NULL, 2319 prop); 2320 } 2321 2322 typedef struct BoolProperty 2323 { 2324 bool (*get)(Object *, Error **); 2325 void (*set)(Object *, bool, Error **); 2326 } BoolProperty; 2327 2328 static void property_get_bool(Object *obj, Visitor *v, const char *name, 2329 void *opaque, Error **errp) 2330 { 2331 BoolProperty *prop = opaque; 2332 bool value; 2333 Error *err = NULL; 2334 2335 value = prop->get(obj, &err); 2336 if (err) { 2337 error_propagate(errp, err); 2338 return; 2339 } 2340 2341 visit_type_bool(v, name, &value, errp); 2342 } 2343 2344 static void property_set_bool(Object *obj, Visitor *v, const char *name, 2345 void *opaque, Error **errp) 2346 { 2347 BoolProperty *prop = opaque; 2348 bool value; 2349 2350 if (!visit_type_bool(v, name, &value, errp)) { 2351 return; 2352 } 2353 2354 prop->set(obj, value, errp); 2355 } 2356 2357 ObjectProperty * 2358 object_property_add_bool(Object *obj, const char *name, 2359 bool (*get)(Object *, Error **), 2360 void (*set)(Object *, bool, Error **)) 2361 { 2362 BoolProperty *prop = g_malloc0(sizeof(*prop)); 2363 2364 prop->get = get; 2365 prop->set = set; 2366 2367 return object_property_add(obj, name, "bool", 2368 get ? property_get_bool : NULL, 2369 set ? property_set_bool : NULL, 2370 property_release_data, 2371 prop); 2372 } 2373 2374 ObjectProperty * 2375 object_class_property_add_bool(ObjectClass *klass, const char *name, 2376 bool (*get)(Object *, Error **), 2377 void (*set)(Object *, bool, Error **)) 2378 { 2379 BoolProperty *prop = g_malloc0(sizeof(*prop)); 2380 2381 prop->get = get; 2382 prop->set = set; 2383 2384 return object_class_property_add(klass, name, "bool", 2385 get ? property_get_bool : NULL, 2386 set ? property_set_bool : NULL, 2387 NULL, 2388 prop); 2389 } 2390 2391 static void property_get_enum(Object *obj, Visitor *v, const char *name, 2392 void *opaque, Error **errp) 2393 { 2394 EnumProperty *prop = opaque; 2395 int value; 2396 Error *err = NULL; 2397 2398 value = prop->get(obj, &err); 2399 if (err) { 2400 error_propagate(errp, err); 2401 return; 2402 } 2403 2404 visit_type_enum(v, name, &value, prop->lookup, errp); 2405 } 2406 2407 static void property_set_enum(Object *obj, Visitor *v, const char *name, 2408 void *opaque, Error **errp) 2409 { 2410 EnumProperty *prop = opaque; 2411 int value; 2412 2413 if (!visit_type_enum(v, name, &value, prop->lookup, errp)) { 2414 return; 2415 } 2416 prop->set(obj, value, errp); 2417 } 2418 2419 ObjectProperty * 2420 object_property_add_enum(Object *obj, const char *name, 2421 const char *typename, 2422 const QEnumLookup *lookup, 2423 int (*get)(Object *, Error **), 2424 void (*set)(Object *, int, Error **)) 2425 { 2426 EnumProperty *prop = g_malloc(sizeof(*prop)); 2427 2428 prop->lookup = lookup; 2429 prop->get = get; 2430 prop->set = set; 2431 2432 return object_property_add(obj, name, typename, 2433 get ? property_get_enum : NULL, 2434 set ? property_set_enum : NULL, 2435 property_release_data, 2436 prop); 2437 } 2438 2439 ObjectProperty * 2440 object_class_property_add_enum(ObjectClass *klass, const char *name, 2441 const char *typename, 2442 const QEnumLookup *lookup, 2443 int (*get)(Object *, Error **), 2444 void (*set)(Object *, int, Error **)) 2445 { 2446 EnumProperty *prop = g_malloc(sizeof(*prop)); 2447 2448 prop->lookup = lookup; 2449 prop->get = get; 2450 prop->set = set; 2451 2452 return object_class_property_add(klass, name, typename, 2453 get ? property_get_enum : NULL, 2454 set ? property_set_enum : NULL, 2455 NULL, 2456 prop); 2457 } 2458 2459 typedef struct TMProperty { 2460 void (*get)(Object *, struct tm *, Error **); 2461 } TMProperty; 2462 2463 static void property_get_tm(Object *obj, Visitor *v, const char *name, 2464 void *opaque, Error **errp) 2465 { 2466 TMProperty *prop = opaque; 2467 Error *err = NULL; 2468 struct tm value; 2469 2470 prop->get(obj, &value, &err); 2471 if (err) { 2472 error_propagate(errp, err); 2473 return; 2474 } 2475 2476 if (!visit_start_struct(v, name, NULL, 0, errp)) { 2477 return; 2478 } 2479 if (!visit_type_int32(v, "tm_year", &value.tm_year, errp)) { 2480 goto out_end; 2481 } 2482 if (!visit_type_int32(v, "tm_mon", &value.tm_mon, errp)) { 2483 goto out_end; 2484 } 2485 if (!visit_type_int32(v, "tm_mday", &value.tm_mday, errp)) { 2486 goto out_end; 2487 } 2488 if (!visit_type_int32(v, "tm_hour", &value.tm_hour, errp)) { 2489 goto out_end; 2490 } 2491 if (!visit_type_int32(v, "tm_min", &value.tm_min, errp)) { 2492 goto out_end; 2493 } 2494 if (!visit_type_int32(v, "tm_sec", &value.tm_sec, errp)) { 2495 goto out_end; 2496 } 2497 visit_check_struct(v, errp); 2498 out_end: 2499 visit_end_struct(v, NULL); 2500 } 2501 2502 ObjectProperty * 2503 object_property_add_tm(Object *obj, const char *name, 2504 void (*get)(Object *, struct tm *, Error **)) 2505 { 2506 TMProperty *prop = g_malloc0(sizeof(*prop)); 2507 2508 prop->get = get; 2509 2510 return object_property_add(obj, name, "struct tm", 2511 get ? property_get_tm : NULL, NULL, 2512 property_release_data, 2513 prop); 2514 } 2515 2516 ObjectProperty * 2517 object_class_property_add_tm(ObjectClass *klass, const char *name, 2518 void (*get)(Object *, struct tm *, Error **)) 2519 { 2520 TMProperty *prop = g_malloc0(sizeof(*prop)); 2521 2522 prop->get = get; 2523 2524 return object_class_property_add(klass, name, "struct tm", 2525 get ? property_get_tm : NULL, 2526 NULL, NULL, prop); 2527 } 2528 2529 static char *object_get_type(Object *obj, Error **errp) 2530 { 2531 return g_strdup(object_get_typename(obj)); 2532 } 2533 2534 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name, 2535 void *opaque, Error **errp) 2536 { 2537 uint8_t value = *(uint8_t *)opaque; 2538 visit_type_uint8(v, name, &value, errp); 2539 } 2540 2541 static void property_set_uint8_ptr(Object *obj, Visitor *v, const char *name, 2542 void *opaque, Error **errp) 2543 { 2544 uint8_t *field = opaque; 2545 uint8_t value; 2546 2547 if (!visit_type_uint8(v, name, &value, errp)) { 2548 return; 2549 } 2550 2551 *field = value; 2552 } 2553 2554 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name, 2555 void *opaque, Error **errp) 2556 { 2557 uint16_t value = *(uint16_t *)opaque; 2558 visit_type_uint16(v, name, &value, errp); 2559 } 2560 2561 static void property_set_uint16_ptr(Object *obj, Visitor *v, const char *name, 2562 void *opaque, Error **errp) 2563 { 2564 uint16_t *field = opaque; 2565 uint16_t value; 2566 2567 if (!visit_type_uint16(v, name, &value, errp)) { 2568 return; 2569 } 2570 2571 *field = value; 2572 } 2573 2574 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name, 2575 void *opaque, Error **errp) 2576 { 2577 uint32_t value = *(uint32_t *)opaque; 2578 visit_type_uint32(v, name, &value, errp); 2579 } 2580 2581 static void property_set_uint32_ptr(Object *obj, Visitor *v, const char *name, 2582 void *opaque, Error **errp) 2583 { 2584 uint32_t *field = opaque; 2585 uint32_t value; 2586 2587 if (!visit_type_uint32(v, name, &value, errp)) { 2588 return; 2589 } 2590 2591 *field = value; 2592 } 2593 2594 static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name, 2595 void *opaque, Error **errp) 2596 { 2597 uint64_t value = *(uint64_t *)opaque; 2598 visit_type_uint64(v, name, &value, errp); 2599 } 2600 2601 static void property_set_uint64_ptr(Object *obj, Visitor *v, const char *name, 2602 void *opaque, Error **errp) 2603 { 2604 uint64_t *field = opaque; 2605 uint64_t value; 2606 2607 if (!visit_type_uint64(v, name, &value, errp)) { 2608 return; 2609 } 2610 2611 *field = value; 2612 } 2613 2614 ObjectProperty * 2615 object_property_add_uint8_ptr(Object *obj, const char *name, 2616 const uint8_t *v, 2617 ObjectPropertyFlags flags) 2618 { 2619 ObjectPropertyAccessor *getter = NULL; 2620 ObjectPropertyAccessor *setter = NULL; 2621 2622 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { 2623 getter = property_get_uint8_ptr; 2624 } 2625 2626 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { 2627 setter = property_set_uint8_ptr; 2628 } 2629 2630 return object_property_add(obj, name, "uint8", 2631 getter, setter, NULL, (void *)v); 2632 } 2633 2634 ObjectProperty * 2635 object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name, 2636 const uint8_t *v, 2637 ObjectPropertyFlags flags) 2638 { 2639 ObjectPropertyAccessor *getter = NULL; 2640 ObjectPropertyAccessor *setter = NULL; 2641 2642 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { 2643 getter = property_get_uint8_ptr; 2644 } 2645 2646 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { 2647 setter = property_set_uint8_ptr; 2648 } 2649 2650 return object_class_property_add(klass, name, "uint8", 2651 getter, setter, NULL, (void *)v); 2652 } 2653 2654 ObjectProperty * 2655 object_property_add_uint16_ptr(Object *obj, const char *name, 2656 const uint16_t *v, 2657 ObjectPropertyFlags flags) 2658 { 2659 ObjectPropertyAccessor *getter = NULL; 2660 ObjectPropertyAccessor *setter = NULL; 2661 2662 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { 2663 getter = property_get_uint16_ptr; 2664 } 2665 2666 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { 2667 setter = property_set_uint16_ptr; 2668 } 2669 2670 return object_property_add(obj, name, "uint16", 2671 getter, setter, NULL, (void *)v); 2672 } 2673 2674 ObjectProperty * 2675 object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name, 2676 const uint16_t *v, 2677 ObjectPropertyFlags flags) 2678 { 2679 ObjectPropertyAccessor *getter = NULL; 2680 ObjectPropertyAccessor *setter = NULL; 2681 2682 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { 2683 getter = property_get_uint16_ptr; 2684 } 2685 2686 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { 2687 setter = property_set_uint16_ptr; 2688 } 2689 2690 return object_class_property_add(klass, name, "uint16", 2691 getter, setter, NULL, (void *)v); 2692 } 2693 2694 ObjectProperty * 2695 object_property_add_uint32_ptr(Object *obj, const char *name, 2696 const uint32_t *v, 2697 ObjectPropertyFlags flags) 2698 { 2699 ObjectPropertyAccessor *getter = NULL; 2700 ObjectPropertyAccessor *setter = NULL; 2701 2702 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { 2703 getter = property_get_uint32_ptr; 2704 } 2705 2706 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { 2707 setter = property_set_uint32_ptr; 2708 } 2709 2710 return object_property_add(obj, name, "uint32", 2711 getter, setter, NULL, (void *)v); 2712 } 2713 2714 ObjectProperty * 2715 object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name, 2716 const uint32_t *v, 2717 ObjectPropertyFlags flags) 2718 { 2719 ObjectPropertyAccessor *getter = NULL; 2720 ObjectPropertyAccessor *setter = NULL; 2721 2722 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { 2723 getter = property_get_uint32_ptr; 2724 } 2725 2726 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { 2727 setter = property_set_uint32_ptr; 2728 } 2729 2730 return object_class_property_add(klass, name, "uint32", 2731 getter, setter, NULL, (void *)v); 2732 } 2733 2734 ObjectProperty * 2735 object_property_add_uint64_ptr(Object *obj, const char *name, 2736 const uint64_t *v, 2737 ObjectPropertyFlags flags) 2738 { 2739 ObjectPropertyAccessor *getter = NULL; 2740 ObjectPropertyAccessor *setter = NULL; 2741 2742 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { 2743 getter = property_get_uint64_ptr; 2744 } 2745 2746 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { 2747 setter = property_set_uint64_ptr; 2748 } 2749 2750 return object_property_add(obj, name, "uint64", 2751 getter, setter, NULL, (void *)v); 2752 } 2753 2754 ObjectProperty * 2755 object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name, 2756 const uint64_t *v, 2757 ObjectPropertyFlags flags) 2758 { 2759 ObjectPropertyAccessor *getter = NULL; 2760 ObjectPropertyAccessor *setter = NULL; 2761 2762 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { 2763 getter = property_get_uint64_ptr; 2764 } 2765 2766 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { 2767 setter = property_set_uint64_ptr; 2768 } 2769 2770 return object_class_property_add(klass, name, "uint64", 2771 getter, setter, NULL, (void *)v); 2772 } 2773 2774 typedef struct { 2775 Object *target_obj; 2776 char *target_name; 2777 } AliasProperty; 2778 2779 static void property_get_alias(Object *obj, Visitor *v, const char *name, 2780 void *opaque, Error **errp) 2781 { 2782 AliasProperty *prop = opaque; 2783 Visitor *alias_v = visitor_forward_field(v, prop->target_name, name); 2784 2785 object_property_get(prop->target_obj, prop->target_name, alias_v, errp); 2786 visit_free(alias_v); 2787 } 2788 2789 static void property_set_alias(Object *obj, Visitor *v, const char *name, 2790 void *opaque, Error **errp) 2791 { 2792 AliasProperty *prop = opaque; 2793 Visitor *alias_v = visitor_forward_field(v, prop->target_name, name); 2794 2795 object_property_set(prop->target_obj, prop->target_name, alias_v, errp); 2796 visit_free(alias_v); 2797 } 2798 2799 static Object *property_resolve_alias(Object *obj, void *opaque, 2800 const char *part) 2801 { 2802 AliasProperty *prop = opaque; 2803 2804 return object_resolve_path_component(prop->target_obj, prop->target_name); 2805 } 2806 2807 static void property_release_alias(Object *obj, const char *name, void *opaque) 2808 { 2809 AliasProperty *prop = opaque; 2810 2811 g_free(prop->target_name); 2812 g_free(prop); 2813 } 2814 2815 ObjectProperty * 2816 object_property_add_alias(Object *obj, const char *name, 2817 Object *target_obj, const char *target_name) 2818 { 2819 AliasProperty *prop; 2820 ObjectProperty *op; 2821 ObjectProperty *target_prop; 2822 g_autofree char *prop_type = NULL; 2823 2824 target_prop = object_property_find_err(target_obj, target_name, 2825 &error_abort); 2826 2827 if (object_property_is_child(target_prop)) { 2828 prop_type = g_strdup_printf("link%s", 2829 target_prop->type + strlen("child")); 2830 } else { 2831 prop_type = g_strdup(target_prop->type); 2832 } 2833 2834 prop = g_malloc(sizeof(*prop)); 2835 prop->target_obj = target_obj; 2836 prop->target_name = g_strdup(target_name); 2837 2838 op = object_property_add(obj, name, prop_type, 2839 property_get_alias, 2840 property_set_alias, 2841 property_release_alias, 2842 prop); 2843 op->resolve = property_resolve_alias; 2844 if (target_prop->defval) { 2845 op->defval = qobject_ref(target_prop->defval); 2846 } 2847 2848 object_property_set_description(obj, op->name, 2849 target_prop->description); 2850 return op; 2851 } 2852 2853 void object_property_set_description(Object *obj, const char *name, 2854 const char *description) 2855 { 2856 ObjectProperty *op; 2857 2858 op = object_property_find_err(obj, name, &error_abort); 2859 g_free(op->description); 2860 op->description = g_strdup(description); 2861 } 2862 2863 void object_class_property_set_description(ObjectClass *klass, 2864 const char *name, 2865 const char *description) 2866 { 2867 ObjectProperty *op; 2868 2869 op = g_hash_table_lookup(klass->properties, name); 2870 g_free(op->description); 2871 op->description = g_strdup(description); 2872 } 2873 2874 static void object_class_init(ObjectClass *klass, void *data) 2875 { 2876 object_class_property_add_str(klass, "type", object_get_type, 2877 NULL); 2878 } 2879 2880 static void register_types(void) 2881 { 2882 static const TypeInfo interface_info = { 2883 .name = TYPE_INTERFACE, 2884 .class_size = sizeof(InterfaceClass), 2885 .abstract = true, 2886 }; 2887 2888 static const TypeInfo object_info = { 2889 .name = TYPE_OBJECT, 2890 .instance_size = sizeof(Object), 2891 .class_init = object_class_init, 2892 .abstract = true, 2893 }; 2894 2895 type_interface = type_register_internal(&interface_info); 2896 type_register_internal(&object_info); 2897 } 2898 2899 type_init(register_types) 2900