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