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 | |
Paolo Bonzini | 14cccb6 | 2012-12-17 18:19:50 +0100 | [diff] [blame] | 13 | #include "qom/object.h" |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 14 | #include "qemu-common.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 15 | #include "qapi/visitor.h" |
Paolo Bonzini | b2cd7de | 2012-02-09 09:52:59 +0100 | [diff] [blame] | 16 | #include "qapi/string-input-visitor.h" |
| 17 | #include "qapi/string-output-visitor.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 18 | #include "qapi/qmp/qerror.h" |
Paolo Bonzini | fa131d9 | 2013-05-10 14:16:39 +0200 | [diff] [blame] | 19 | #include "trace.h" |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 20 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 21 | /* TODO: replace QObject with a simpler visitor to avoid a dependency |
| 22 | * of the QOM core on QObject? */ |
Paolo Bonzini | 14cccb6 | 2012-12-17 18:19:50 +0100 | [diff] [blame] | 23 | #include "qom/qom-qobject.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 24 | #include "qapi/qmp/qobject.h" |
| 25 | #include "qapi/qmp/qbool.h" |
| 26 | #include "qapi/qmp/qint.h" |
| 27 | #include "qapi/qmp/qstring.h" |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 28 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 29 | #define MAX_INTERFACES 32 |
| 30 | |
| 31 | typedef struct InterfaceImpl InterfaceImpl; |
| 32 | typedef struct TypeImpl TypeImpl; |
| 33 | |
| 34 | struct InterfaceImpl |
| 35 | { |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 36 | const char *typename; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | struct TypeImpl |
| 40 | { |
| 41 | const char *name; |
| 42 | |
| 43 | size_t class_size; |
| 44 | |
| 45 | size_t instance_size; |
| 46 | |
| 47 | void (*class_init)(ObjectClass *klass, void *data); |
Paolo Bonzini | 3b50e31 | 2012-05-02 13:30:55 +0200 | [diff] [blame] | 48 | void (*class_base_init)(ObjectClass *klass, void *data); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 49 | void (*class_finalize)(ObjectClass *klass, void *data); |
| 50 | |
| 51 | void *class_data; |
| 52 | |
| 53 | void (*instance_init)(Object *obj); |
Eduardo Habkost | 8231c2d | 2013-07-10 17:08:41 -0300 | [diff] [blame] | 54 | void (*instance_post_init)(Object *obj); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 55 | void (*instance_finalize)(Object *obj); |
| 56 | |
| 57 | bool abstract; |
| 58 | |
| 59 | const char *parent; |
| 60 | TypeImpl *parent_type; |
| 61 | |
| 62 | ObjectClass *class; |
| 63 | |
| 64 | int num_interfaces; |
| 65 | InterfaceImpl interfaces[MAX_INTERFACES]; |
| 66 | }; |
| 67 | |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame] | 68 | static Type type_interface; |
| 69 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 70 | static GHashTable *type_table_get(void) |
| 71 | { |
| 72 | static GHashTable *type_table; |
| 73 | |
| 74 | if (type_table == NULL) { |
| 75 | type_table = g_hash_table_new(g_str_hash, g_str_equal); |
| 76 | } |
| 77 | |
| 78 | return type_table; |
| 79 | } |
| 80 | |
Hervé Poussineau | f54c19c | 2013-12-03 16:42:00 +0100 | [diff] [blame] | 81 | static bool enumerating_types; |
| 82 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 83 | static void type_table_add(TypeImpl *ti) |
| 84 | { |
Hervé Poussineau | f54c19c | 2013-12-03 16:42:00 +0100 | [diff] [blame] | 85 | assert(!enumerating_types); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 86 | g_hash_table_insert(type_table_get(), (void *)ti->name, ti); |
| 87 | } |
| 88 | |
| 89 | static TypeImpl *type_table_lookup(const char *name) |
| 90 | { |
| 91 | return g_hash_table_lookup(type_table_get(), name); |
| 92 | } |
| 93 | |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 94 | static TypeImpl *type_new(const TypeInfo *info) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 95 | { |
| 96 | TypeImpl *ti = g_malloc0(sizeof(*ti)); |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 97 | int i; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 98 | |
| 99 | g_assert(info->name != NULL); |
| 100 | |
Anthony Liguori | 7309335 | 2012-01-25 13:37:36 -0600 | [diff] [blame] | 101 | if (type_table_lookup(info->name) != NULL) { |
| 102 | fprintf(stderr, "Registering `%s' which already exists\n", info->name); |
| 103 | abort(); |
| 104 | } |
| 105 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 106 | ti->name = g_strdup(info->name); |
| 107 | ti->parent = g_strdup(info->parent); |
| 108 | |
| 109 | ti->class_size = info->class_size; |
| 110 | ti->instance_size = info->instance_size; |
| 111 | |
| 112 | ti->class_init = info->class_init; |
Paolo Bonzini | 3b50e31 | 2012-05-02 13:30:55 +0200 | [diff] [blame] | 113 | ti->class_base_init = info->class_base_init; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 114 | ti->class_finalize = info->class_finalize; |
| 115 | ti->class_data = info->class_data; |
| 116 | |
| 117 | ti->instance_init = info->instance_init; |
Eduardo Habkost | 8231c2d | 2013-07-10 17:08:41 -0300 | [diff] [blame] | 118 | ti->instance_post_init = info->instance_post_init; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 119 | ti->instance_finalize = info->instance_finalize; |
| 120 | |
| 121 | ti->abstract = info->abstract; |
| 122 | |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 123 | for (i = 0; info->interfaces && info->interfaces[i].type; i++) { |
| 124 | ti->interfaces[i].typename = g_strdup(info->interfaces[i].type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 125 | } |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 126 | ti->num_interfaces = i; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 127 | |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 128 | return ti; |
| 129 | } |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 130 | |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 131 | static TypeImpl *type_register_internal(const TypeInfo *info) |
| 132 | { |
| 133 | TypeImpl *ti; |
| 134 | ti = type_new(info); |
| 135 | |
| 136 | type_table_add(ti); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 137 | return ti; |
| 138 | } |
| 139 | |
Paolo Bonzini | 049cb3c | 2012-04-04 15:58:40 +0200 | [diff] [blame] | 140 | TypeImpl *type_register(const TypeInfo *info) |
| 141 | { |
| 142 | assert(info->parent); |
| 143 | return type_register_internal(info); |
| 144 | } |
| 145 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 146 | TypeImpl *type_register_static(const TypeInfo *info) |
| 147 | { |
| 148 | return type_register(info); |
| 149 | } |
| 150 | |
| 151 | static TypeImpl *type_get_by_name(const char *name) |
| 152 | { |
| 153 | if (name == NULL) { |
| 154 | return NULL; |
| 155 | } |
| 156 | |
| 157 | return type_table_lookup(name); |
| 158 | } |
| 159 | |
| 160 | static TypeImpl *type_get_parent(TypeImpl *type) |
| 161 | { |
| 162 | if (!type->parent_type && type->parent) { |
| 163 | type->parent_type = type_get_by_name(type->parent); |
| 164 | g_assert(type->parent_type != NULL); |
| 165 | } |
| 166 | |
| 167 | return type->parent_type; |
| 168 | } |
| 169 | |
| 170 | static bool type_has_parent(TypeImpl *type) |
| 171 | { |
| 172 | return (type->parent != NULL); |
| 173 | } |
| 174 | |
| 175 | static size_t type_class_get_size(TypeImpl *ti) |
| 176 | { |
| 177 | if (ti->class_size) { |
| 178 | return ti->class_size; |
| 179 | } |
| 180 | |
| 181 | if (type_has_parent(ti)) { |
| 182 | return type_class_get_size(type_get_parent(ti)); |
| 183 | } |
| 184 | |
| 185 | return sizeof(ObjectClass); |
| 186 | } |
| 187 | |
Igor Mitsyanko | aca59af | 2012-02-28 15:57:10 +0400 | [diff] [blame] | 188 | static size_t type_object_get_size(TypeImpl *ti) |
| 189 | { |
| 190 | if (ti->instance_size) { |
| 191 | return ti->instance_size; |
| 192 | } |
| 193 | |
| 194 | if (type_has_parent(ti)) { |
| 195 | return type_object_get_size(type_get_parent(ti)); |
| 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 201 | static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 202 | { |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 203 | assert(target_type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 204 | |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 205 | /* Check if typename is a direct ancestor of type */ |
| 206 | while (type) { |
| 207 | if (type == target_type) { |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | type = type_get_parent(type); |
| 212 | } |
| 213 | |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | static void type_initialize(TypeImpl *ti); |
| 218 | |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 219 | static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type, |
| 220 | TypeImpl *parent_type) |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 221 | { |
| 222 | InterfaceClass *new_iface; |
| 223 | TypeInfo info = { }; |
| 224 | TypeImpl *iface_impl; |
| 225 | |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 226 | info.parent = parent_type->name; |
| 227 | info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name); |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 228 | info.abstract = true; |
| 229 | |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 230 | iface_impl = type_new(&info); |
| 231 | iface_impl->parent_type = parent_type; |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 232 | type_initialize(iface_impl); |
| 233 | g_free((char *)info.name); |
| 234 | |
| 235 | new_iface = (InterfaceClass *)iface_impl->class; |
| 236 | new_iface->concrete_class = ti->class; |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 237 | new_iface->interface_type = interface_type; |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 238 | |
| 239 | ti->class->interfaces = g_slist_append(ti->class->interfaces, |
| 240 | iface_impl->class); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 241 | } |
| 242 | |
Igor Mitsyanko | ac45103 | 2012-02-28 15:57:11 +0400 | [diff] [blame] | 243 | static void type_initialize(TypeImpl *ti) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 244 | { |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 245 | TypeImpl *parent; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 246 | |
| 247 | if (ti->class) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | ti->class_size = type_class_get_size(ti); |
Igor Mitsyanko | aca59af | 2012-02-28 15:57:10 +0400 | [diff] [blame] | 252 | ti->instance_size = type_object_get_size(ti); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 253 | |
| 254 | ti->class = g_malloc0(ti->class_size); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 255 | |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 256 | parent = type_get_parent(ti); |
| 257 | if (parent) { |
Igor Mitsyanko | ac45103 | 2012-02-28 15:57:11 +0400 | [diff] [blame] | 258 | type_initialize(parent); |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 259 | GSList *e; |
| 260 | int i; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 261 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 262 | g_assert(parent->class_size <= ti->class_size); |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 263 | memcpy(ti->class, parent->class, parent->class_size); |
Peter Crosthwaite | 3e407de | 2013-02-19 14:02:09 +1000 | [diff] [blame] | 264 | ti->class->interfaces = NULL; |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 265 | |
| 266 | for (e = parent->class->interfaces; e; e = e->next) { |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 267 | InterfaceClass *iface = e->data; |
| 268 | ObjectClass *klass = OBJECT_CLASS(iface); |
| 269 | |
| 270 | type_initialize_interface(ti, iface->interface_type, klass->type); |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | for (i = 0; i < ti->num_interfaces; i++) { |
| 274 | TypeImpl *t = type_get_by_name(ti->interfaces[i].typename); |
| 275 | for (e = ti->class->interfaces; e; e = e->next) { |
| 276 | TypeImpl *target_type = OBJECT_CLASS(e->data)->type; |
| 277 | |
| 278 | if (type_is_ancestor(target_type, t)) { |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | if (e) { |
| 284 | continue; |
| 285 | } |
| 286 | |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 287 | type_initialize_interface(ti, t, t); |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 288 | } |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 289 | } |
| 290 | |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 291 | ti->class->type = ti; |
| 292 | |
| 293 | while (parent) { |
| 294 | if (parent->class_base_init) { |
| 295 | parent->class_base_init(ti->class, ti->class_data); |
| 296 | } |
| 297 | parent = type_get_parent(parent); |
| 298 | } |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 299 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 300 | if (ti->class_init) { |
| 301 | ti->class_init(ti->class, ti->class_data); |
| 302 | } |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static void object_init_with_type(Object *obj, TypeImpl *ti) |
| 306 | { |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 307 | if (type_has_parent(ti)) { |
| 308 | object_init_with_type(obj, type_get_parent(ti)); |
| 309 | } |
| 310 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 311 | if (ti->instance_init) { |
| 312 | ti->instance_init(obj); |
| 313 | } |
| 314 | } |
| 315 | |
Eduardo Habkost | 8231c2d | 2013-07-10 17:08:41 -0300 | [diff] [blame] | 316 | static void object_post_init_with_type(Object *obj, TypeImpl *ti) |
| 317 | { |
| 318 | if (ti->instance_post_init) { |
| 319 | ti->instance_post_init(obj); |
| 320 | } |
| 321 | |
| 322 | if (type_has_parent(ti)) { |
| 323 | object_post_init_with_type(obj, type_get_parent(ti)); |
| 324 | } |
| 325 | } |
| 326 | |
Andreas Färber | 5b9237f | 2013-08-30 18:28:37 +0200 | [diff] [blame] | 327 | void object_initialize_with_type(void *data, size_t size, TypeImpl *type) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 328 | { |
| 329 | Object *obj = data; |
| 330 | |
| 331 | g_assert(type != NULL); |
Igor Mitsyanko | ac45103 | 2012-02-28 15:57:11 +0400 | [diff] [blame] | 332 | type_initialize(type); |
Igor Mitsyanko | aca59af | 2012-02-28 15:57:10 +0400 | [diff] [blame] | 333 | |
| 334 | g_assert(type->instance_size >= sizeof(Object)); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 335 | g_assert(type->abstract == false); |
Andreas Färber | 5b9237f | 2013-08-30 18:28:37 +0200 | [diff] [blame] | 336 | g_assert(size >= type->instance_size); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 337 | |
| 338 | memset(obj, 0, type->instance_size); |
| 339 | obj->class = type->class; |
Paolo Bonzini | 764b631 | 2012-11-23 09:47:12 +0100 | [diff] [blame] | 340 | object_ref(obj); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 341 | QTAILQ_INIT(&obj->properties); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 342 | object_init_with_type(obj, type); |
Eduardo Habkost | 8231c2d | 2013-07-10 17:08:41 -0300 | [diff] [blame] | 343 | object_post_init_with_type(obj, type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 344 | } |
| 345 | |
Andreas Färber | 213f0c4 | 2013-08-23 19:37:12 +0200 | [diff] [blame] | 346 | void object_initialize(void *data, size_t size, const char *typename) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 347 | { |
| 348 | TypeImpl *type = type_get_by_name(typename); |
| 349 | |
Andreas Färber | 5b9237f | 2013-08-30 18:28:37 +0200 | [diff] [blame] | 350 | object_initialize_with_type(data, size, type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 351 | } |
| 352 | |
Andreas Färber | 5d9d3f4 | 2012-05-27 00:32:40 +0200 | [diff] [blame] | 353 | static inline bool object_property_is_child(ObjectProperty *prop) |
| 354 | { |
| 355 | return strstart(prop->type, "child<", NULL); |
| 356 | } |
| 357 | |
| 358 | static inline bool object_property_is_link(ObjectProperty *prop) |
| 359 | { |
| 360 | return strstart(prop->type, "link<", NULL); |
| 361 | } |
| 362 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 363 | static void object_property_del_all(Object *obj) |
| 364 | { |
| 365 | while (!QTAILQ_EMPTY(&obj->properties)) { |
| 366 | ObjectProperty *prop = QTAILQ_FIRST(&obj->properties); |
| 367 | |
| 368 | QTAILQ_REMOVE(&obj->properties, prop, node); |
| 369 | |
| 370 | if (prop->release) { |
| 371 | prop->release(obj, prop->name, prop->opaque); |
| 372 | } |
| 373 | |
| 374 | g_free(prop->name); |
| 375 | g_free(prop->type); |
| 376 | g_free(prop); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | static void object_property_del_child(Object *obj, Object *child, Error **errp) |
| 381 | { |
| 382 | ObjectProperty *prop; |
| 383 | |
| 384 | QTAILQ_FOREACH(prop, &obj->properties, node) { |
Andreas Färber | 5d9d3f4 | 2012-05-27 00:32:40 +0200 | [diff] [blame] | 385 | if (object_property_is_child(prop) && prop->opaque == child) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 386 | object_property_del(obj, prop->name, errp); |
Paolo Bonzini | 6c1fdcf | 2012-02-28 09:54:15 +0100 | [diff] [blame] | 387 | break; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | void object_unparent(Object *obj) |
| 393 | { |
Paolo Bonzini | e0a83fc | 2013-04-02 15:50:00 +0200 | [diff] [blame] | 394 | if (!obj->parent) { |
| 395 | return; |
| 396 | } |
| 397 | |
Paolo Bonzini | 52e636c | 2013-01-25 14:12:30 +0100 | [diff] [blame] | 398 | object_ref(obj); |
Paolo Bonzini | 667d22d | 2012-11-23 09:47:13 +0100 | [diff] [blame] | 399 | if (obj->class->unparent) { |
| 400 | (obj->class->unparent)(obj); |
| 401 | } |
Michael S. Tsirkin | e998fa8 | 2013-03-18 21:01:37 +0200 | [diff] [blame] | 402 | if (obj->parent) { |
| 403 | object_property_del_child(obj->parent, obj, NULL); |
| 404 | } |
Paolo Bonzini | 52e636c | 2013-01-25 14:12:30 +0100 | [diff] [blame] | 405 | object_unref(obj); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 406 | } |
| 407 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 408 | static void object_deinit(Object *obj, TypeImpl *type) |
| 409 | { |
| 410 | if (type->instance_finalize) { |
| 411 | type->instance_finalize(obj); |
| 412 | } |
| 413 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 414 | if (type_has_parent(type)) { |
| 415 | object_deinit(obj, type_get_parent(type)); |
| 416 | } |
| 417 | } |
| 418 | |
Paolo Bonzini | 339c270 | 2012-11-23 09:47:16 +0100 | [diff] [blame] | 419 | static void object_finalize(void *data) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 420 | { |
| 421 | Object *obj = data; |
| 422 | TypeImpl *ti = obj->class->type; |
| 423 | |
| 424 | object_deinit(obj, ti); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 425 | object_property_del_all(obj); |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 426 | |
| 427 | g_assert(obj->ref == 0); |
Paolo Bonzini | fde9bf4 | 2012-11-23 09:47:14 +0100 | [diff] [blame] | 428 | if (obj->free) { |
| 429 | obj->free(obj); |
| 430 | } |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | Object *object_new_with_type(Type type) |
| 434 | { |
| 435 | Object *obj; |
| 436 | |
| 437 | g_assert(type != NULL); |
Igor Mitsyanko | ac45103 | 2012-02-28 15:57:11 +0400 | [diff] [blame] | 438 | type_initialize(type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 439 | |
| 440 | obj = g_malloc(type->instance_size); |
Andreas Färber | 5b9237f | 2013-08-30 18:28:37 +0200 | [diff] [blame] | 441 | object_initialize_with_type(obj, type->instance_size, type); |
Paolo Bonzini | fde9bf4 | 2012-11-23 09:47:14 +0100 | [diff] [blame] | 442 | obj->free = g_free; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 443 | |
| 444 | return obj; |
| 445 | } |
| 446 | |
| 447 | Object *object_new(const char *typename) |
| 448 | { |
| 449 | TypeImpl *ti = type_get_by_name(typename); |
| 450 | |
| 451 | return object_new_with_type(ti); |
| 452 | } |
| 453 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 454 | Object *object_dynamic_cast(Object *obj, const char *typename) |
| 455 | { |
Paolo Bonzini | b7f43fe | 2012-11-23 16:56:17 +0100 | [diff] [blame] | 456 | if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) { |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 457 | return obj; |
| 458 | } |
| 459 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 460 | return NULL; |
| 461 | } |
| 462 | |
Paolo Bonzini | be17f18 | 2013-05-10 14:16:38 +0200 | [diff] [blame] | 463 | Object *object_dynamic_cast_assert(Object *obj, const char *typename, |
| 464 | const char *file, int line, const char *func) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 465 | { |
Paolo Bonzini | fa131d9 | 2013-05-10 14:16:39 +0200 | [diff] [blame] | 466 | trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)", |
| 467 | typename, file, line, func); |
| 468 | |
Paolo Bonzini | 3556c23 | 2013-05-10 14:16:40 +0200 | [diff] [blame] | 469 | #ifdef CONFIG_QOM_CAST_DEBUG |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 470 | int i; |
| 471 | Object *inst; |
| 472 | |
Peter Crosthwaite | 95916ab | 2013-05-22 11:19:16 +1000 | [diff] [blame] | 473 | for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) { |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 474 | if (obj->class->object_cast_cache[i] == typename) { |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 475 | goto out; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | inst = object_dynamic_cast(obj, typename); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 480 | |
Paolo Bonzini | b7f43fe | 2012-11-23 16:56:17 +0100 | [diff] [blame] | 481 | if (!inst && obj) { |
Paolo Bonzini | be17f18 | 2013-05-10 14:16:38 +0200 | [diff] [blame] | 482 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
| 483 | file, line, func, obj, typename); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 484 | abort(); |
| 485 | } |
| 486 | |
Paolo Bonzini | 3556c23 | 2013-05-10 14:16:40 +0200 | [diff] [blame] | 487 | assert(obj == inst); |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 488 | |
Peter Crosthwaite | 95916ab | 2013-05-22 11:19:16 +1000 | [diff] [blame] | 489 | if (obj && obj == inst) { |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 490 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 491 | obj->class->object_cast_cache[i - 1] = |
| 492 | obj->class->object_cast_cache[i]; |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 493 | } |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 494 | obj->class->object_cast_cache[i - 1] = typename; |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | out: |
Paolo Bonzini | 3556c23 | 2013-05-10 14:16:40 +0200 | [diff] [blame] | 498 | #endif |
| 499 | return obj; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | ObjectClass *object_class_dynamic_cast(ObjectClass *class, |
| 503 | const char *typename) |
| 504 | { |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 505 | ObjectClass *ret = NULL; |
Paolo Bonzini | bf0fda3 | 2013-05-10 14:16:36 +0200 | [diff] [blame] | 506 | TypeImpl *target_type; |
| 507 | TypeImpl *type; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 508 | |
Paolo Bonzini | bf0fda3 | 2013-05-10 14:16:36 +0200 | [diff] [blame] | 509 | if (!class) { |
| 510 | return NULL; |
| 511 | } |
| 512 | |
Paolo Bonzini | 793c96b | 2013-05-10 14:16:37 +0200 | [diff] [blame] | 513 | /* A simple fast path that can trigger a lot for leaf classes. */ |
Paolo Bonzini | bf0fda3 | 2013-05-10 14:16:36 +0200 | [diff] [blame] | 514 | type = class->type; |
Paolo Bonzini | 793c96b | 2013-05-10 14:16:37 +0200 | [diff] [blame] | 515 | if (type->name == typename) { |
| 516 | return class; |
| 517 | } |
| 518 | |
Paolo Bonzini | bf0fda3 | 2013-05-10 14:16:36 +0200 | [diff] [blame] | 519 | target_type = type_get_by_name(typename); |
Alexander Graf | 9ab880b | 2013-04-30 15:02:16 +0200 | [diff] [blame] | 520 | if (!target_type) { |
| 521 | /* target class type unknown, so fail the cast */ |
| 522 | return NULL; |
| 523 | } |
| 524 | |
Peter Crosthwaite | 00e2cea | 2013-02-19 14:02:10 +1000 | [diff] [blame] | 525 | if (type->class->interfaces && |
| 526 | type_is_ancestor(target_type, type_interface)) { |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 527 | int found = 0; |
| 528 | GSList *i; |
| 529 | |
| 530 | for (i = class->interfaces; i; i = i->next) { |
| 531 | ObjectClass *target_class = i->data; |
| 532 | |
| 533 | if (type_is_ancestor(target_class->type, target_type)) { |
| 534 | ret = target_class; |
| 535 | found++; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /* The match was ambiguous, don't allow a cast */ |
| 540 | if (found > 1) { |
| 541 | ret = NULL; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 542 | } |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 543 | } else if (type_is_ancestor(type, target_type)) { |
| 544 | ret = class; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 545 | } |
| 546 | |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 547 | return ret; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, |
Paolo Bonzini | be17f18 | 2013-05-10 14:16:38 +0200 | [diff] [blame] | 551 | const char *typename, |
| 552 | const char *file, int line, |
| 553 | const char *func) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 554 | { |
Paolo Bonzini | fa131d9 | 2013-05-10 14:16:39 +0200 | [diff] [blame] | 555 | ObjectClass *ret; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 556 | |
Paolo Bonzini | fa131d9 | 2013-05-10 14:16:39 +0200 | [diff] [blame] | 557 | trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)", |
| 558 | typename, file, line, func); |
| 559 | |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 560 | #ifdef CONFIG_QOM_CAST_DEBUG |
| 561 | int i; |
| 562 | |
Peter Crosthwaite | 9d6a3d5 | 2013-06-18 19:18:59 +1000 | [diff] [blame] | 563 | for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) { |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 564 | if (class->class_cast_cache[i] == typename) { |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 565 | ret = class; |
| 566 | goto out; |
| 567 | } |
| 568 | } |
| 569 | #else |
Peter Crosthwaite | 9d6a3d5 | 2013-06-18 19:18:59 +1000 | [diff] [blame] | 570 | if (!class || !class->interfaces) { |
Paolo Bonzini | 3556c23 | 2013-05-10 14:16:40 +0200 | [diff] [blame] | 571 | return class; |
| 572 | } |
| 573 | #endif |
| 574 | |
Paolo Bonzini | fa131d9 | 2013-05-10 14:16:39 +0200 | [diff] [blame] | 575 | ret = object_class_dynamic_cast(class, typename); |
Paolo Bonzini | bf0fda3 | 2013-05-10 14:16:36 +0200 | [diff] [blame] | 576 | if (!ret && class) { |
Paolo Bonzini | be17f18 | 2013-05-10 14:16:38 +0200 | [diff] [blame] | 577 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
| 578 | file, line, func, class, typename); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 579 | abort(); |
| 580 | } |
| 581 | |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 582 | #ifdef CONFIG_QOM_CAST_DEBUG |
Peter Crosthwaite | 9d6a3d5 | 2013-06-18 19:18:59 +1000 | [diff] [blame] | 583 | if (class && ret == class) { |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 584 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 585 | class->class_cast_cache[i - 1] = class->class_cast_cache[i]; |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 586 | } |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 587 | class->class_cast_cache[i - 1] = typename; |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 588 | } |
| 589 | out: |
| 590 | #endif |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 591 | return ret; |
| 592 | } |
| 593 | |
| 594 | const char *object_get_typename(Object *obj) |
| 595 | { |
| 596 | return obj->class->type->name; |
| 597 | } |
| 598 | |
| 599 | ObjectClass *object_get_class(Object *obj) |
| 600 | { |
| 601 | return obj->class; |
| 602 | } |
| 603 | |
Andreas Färber | 1786237 | 2013-01-23 12:20:18 +0100 | [diff] [blame] | 604 | bool object_class_is_abstract(ObjectClass *klass) |
| 605 | { |
| 606 | return klass->type->abstract; |
| 607 | } |
| 608 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 609 | const char *object_class_get_name(ObjectClass *klass) |
| 610 | { |
| 611 | return klass->type->name; |
| 612 | } |
| 613 | |
| 614 | ObjectClass *object_class_by_name(const char *typename) |
| 615 | { |
| 616 | TypeImpl *type = type_get_by_name(typename); |
| 617 | |
| 618 | if (!type) { |
| 619 | return NULL; |
| 620 | } |
| 621 | |
Igor Mitsyanko | ac45103 | 2012-02-28 15:57:11 +0400 | [diff] [blame] | 622 | type_initialize(type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 623 | |
| 624 | return type->class; |
| 625 | } |
| 626 | |
Paolo Bonzini | e7cce67 | 2012-05-02 13:30:54 +0200 | [diff] [blame] | 627 | ObjectClass *object_class_get_parent(ObjectClass *class) |
| 628 | { |
| 629 | TypeImpl *type = type_get_parent(class->type); |
| 630 | |
| 631 | if (!type) { |
| 632 | return NULL; |
| 633 | } |
| 634 | |
| 635 | type_initialize(type); |
| 636 | |
| 637 | return type->class; |
| 638 | } |
| 639 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 640 | typedef struct OCFData |
| 641 | { |
| 642 | void (*fn)(ObjectClass *klass, void *opaque); |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 643 | const char *implements_type; |
| 644 | bool include_abstract; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 645 | void *opaque; |
| 646 | } OCFData; |
| 647 | |
| 648 | static void object_class_foreach_tramp(gpointer key, gpointer value, |
| 649 | gpointer opaque) |
| 650 | { |
| 651 | OCFData *data = opaque; |
| 652 | TypeImpl *type = value; |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 653 | ObjectClass *k; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 654 | |
Igor Mitsyanko | ac45103 | 2012-02-28 15:57:11 +0400 | [diff] [blame] | 655 | type_initialize(type); |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 656 | k = type->class; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 657 | |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 658 | if (!data->include_abstract && type->abstract) { |
| 659 | return; |
| 660 | } |
| 661 | |
| 662 | if (data->implements_type && |
| 663 | !object_class_dynamic_cast(k, data->implements_type)) { |
| 664 | return; |
| 665 | } |
| 666 | |
| 667 | data->fn(k, data->opaque); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 671 | const char *implements_type, bool include_abstract, |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 672 | void *opaque) |
| 673 | { |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 674 | OCFData data = { fn, implements_type, include_abstract, opaque }; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 675 | |
Hervé Poussineau | f54c19c | 2013-12-03 16:42:00 +0100 | [diff] [blame] | 676 | enumerating_types = true; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 677 | g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); |
Hervé Poussineau | f54c19c | 2013-12-03 16:42:00 +0100 | [diff] [blame] | 678 | enumerating_types = false; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 679 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 680 | |
Paolo Bonzini | 32efc53 | 2012-04-11 23:30:20 +0200 | [diff] [blame] | 681 | int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), |
| 682 | void *opaque) |
| 683 | { |
| 684 | ObjectProperty *prop; |
| 685 | int ret = 0; |
| 686 | |
| 687 | QTAILQ_FOREACH(prop, &obj->properties, node) { |
| 688 | if (object_property_is_child(prop)) { |
| 689 | ret = fn(prop->opaque, opaque); |
| 690 | if (ret != 0) { |
| 691 | break; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | return ret; |
| 696 | } |
| 697 | |
Andreas Färber | 418ba9e | 2012-02-25 23:07:34 +0100 | [diff] [blame] | 698 | static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) |
| 699 | { |
| 700 | GSList **list = opaque; |
| 701 | |
| 702 | *list = g_slist_prepend(*list, klass); |
| 703 | } |
| 704 | |
| 705 | GSList *object_class_get_list(const char *implements_type, |
| 706 | bool include_abstract) |
| 707 | { |
| 708 | GSList *list = NULL; |
| 709 | |
| 710 | object_class_foreach(object_class_get_list_tramp, |
| 711 | implements_type, include_abstract, &list); |
| 712 | return list; |
| 713 | } |
| 714 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 715 | void object_ref(Object *obj) |
| 716 | { |
Jan Kiszka | f08c03f | 2013-07-02 11:36:39 +0200 | [diff] [blame] | 717 | atomic_inc(&obj->ref); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | void object_unref(Object *obj) |
| 721 | { |
| 722 | g_assert(obj->ref > 0); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 723 | |
| 724 | /* parent always holds a reference to its children */ |
Jan Kiszka | f08c03f | 2013-07-02 11:36:39 +0200 | [diff] [blame] | 725 | if (atomic_fetch_dec(&obj->ref) == 1) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 726 | object_finalize(obj); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | void object_property_add(Object *obj, const char *name, const char *type, |
| 731 | ObjectPropertyAccessor *get, |
| 732 | ObjectPropertyAccessor *set, |
| 733 | ObjectPropertyRelease *release, |
| 734 | void *opaque, Error **errp) |
| 735 | { |
Peter Maydell | 54852b0 | 2013-03-25 13:15:13 +0000 | [diff] [blame] | 736 | ObjectProperty *prop; |
| 737 | |
| 738 | QTAILQ_FOREACH(prop, &obj->properties, node) { |
| 739 | if (strcmp(prop->name, name) == 0) { |
| 740 | error_setg(errp, "attempt to add duplicate property '%s'" |
| 741 | " to object (type '%s')", name, |
| 742 | object_get_typename(obj)); |
| 743 | return; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | prop = g_malloc0(sizeof(*prop)); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 748 | |
| 749 | prop->name = g_strdup(name); |
| 750 | prop->type = g_strdup(type); |
| 751 | |
| 752 | prop->get = get; |
| 753 | prop->set = set; |
| 754 | prop->release = release; |
| 755 | prop->opaque = opaque; |
| 756 | |
| 757 | QTAILQ_INSERT_TAIL(&obj->properties, prop, node); |
| 758 | } |
| 759 | |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 760 | ObjectProperty *object_property_find(Object *obj, const char *name, |
| 761 | Error **errp) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 762 | { |
| 763 | ObjectProperty *prop; |
| 764 | |
| 765 | QTAILQ_FOREACH(prop, &obj->properties, node) { |
| 766 | if (strcmp(prop->name, name) == 0) { |
| 767 | return prop; |
| 768 | } |
| 769 | } |
| 770 | |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 771 | error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 772 | return NULL; |
| 773 | } |
| 774 | |
| 775 | void object_property_del(Object *obj, const char *name, Error **errp) |
| 776 | { |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 777 | ObjectProperty *prop = object_property_find(obj, name, errp); |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 778 | if (prop == NULL) { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 779 | return; |
| 780 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 781 | |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 782 | if (prop->release) { |
| 783 | prop->release(obj, name, prop->opaque); |
| 784 | } |
| 785 | |
| 786 | QTAILQ_REMOVE(&obj->properties, prop, node); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 787 | |
| 788 | g_free(prop->name); |
| 789 | g_free(prop->type); |
| 790 | g_free(prop); |
| 791 | } |
| 792 | |
| 793 | void object_property_get(Object *obj, Visitor *v, const char *name, |
| 794 | Error **errp) |
| 795 | { |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 796 | ObjectProperty *prop = object_property_find(obj, name, errp); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 797 | if (prop == NULL) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 798 | return; |
| 799 | } |
| 800 | |
| 801 | if (!prop->get) { |
| 802 | error_set(errp, QERR_PERMISSION_DENIED); |
| 803 | } else { |
| 804 | prop->get(obj, v, prop->opaque, name, errp); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | void object_property_set(Object *obj, Visitor *v, const char *name, |
| 809 | Error **errp) |
| 810 | { |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 811 | ObjectProperty *prop = object_property_find(obj, name, errp); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 812 | if (prop == NULL) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 813 | return; |
| 814 | } |
| 815 | |
| 816 | if (!prop->set) { |
| 817 | error_set(errp, QERR_PERMISSION_DENIED); |
| 818 | } else { |
| 819 | prop->set(obj, v, prop->opaque, name, errp); |
| 820 | } |
| 821 | } |
| 822 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 823 | void object_property_set_str(Object *obj, const char *value, |
| 824 | const char *name, Error **errp) |
| 825 | { |
| 826 | QString *qstr = qstring_from_str(value); |
| 827 | object_property_set_qobject(obj, QOBJECT(qstr), name, errp); |
| 828 | |
| 829 | QDECREF(qstr); |
| 830 | } |
| 831 | |
| 832 | char *object_property_get_str(Object *obj, const char *name, |
| 833 | Error **errp) |
| 834 | { |
| 835 | QObject *ret = object_property_get_qobject(obj, name, errp); |
| 836 | QString *qstring; |
| 837 | char *retval; |
| 838 | |
| 839 | if (!ret) { |
| 840 | return NULL; |
| 841 | } |
| 842 | qstring = qobject_to_qstring(ret); |
| 843 | if (!qstring) { |
| 844 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); |
| 845 | retval = NULL; |
| 846 | } else { |
| 847 | retval = g_strdup(qstring_get_str(qstring)); |
| 848 | } |
| 849 | |
| 850 | QDECREF(qstring); |
| 851 | return retval; |
| 852 | } |
| 853 | |
Paolo Bonzini | 1d9c5a1 | 2012-02-02 10:51:57 +0100 | [diff] [blame] | 854 | void object_property_set_link(Object *obj, Object *value, |
| 855 | const char *name, Error **errp) |
| 856 | { |
Vlad Yasevich | 2d3aa28 | 2013-11-15 12:09:47 -0500 | [diff] [blame] | 857 | gchar *path = object_get_canonical_path(value); |
| 858 | object_property_set_str(obj, path, name, errp); |
| 859 | g_free(path); |
Paolo Bonzini | 1d9c5a1 | 2012-02-02 10:51:57 +0100 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | Object *object_property_get_link(Object *obj, const char *name, |
| 863 | Error **errp) |
| 864 | { |
| 865 | char *str = object_property_get_str(obj, name, errp); |
| 866 | Object *target = NULL; |
| 867 | |
| 868 | if (str && *str) { |
| 869 | target = object_resolve_path(str, NULL); |
| 870 | if (!target) { |
| 871 | error_set(errp, QERR_DEVICE_NOT_FOUND, str); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | g_free(str); |
| 876 | return target; |
| 877 | } |
| 878 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 879 | void object_property_set_bool(Object *obj, bool value, |
| 880 | const char *name, Error **errp) |
| 881 | { |
| 882 | QBool *qbool = qbool_from_int(value); |
| 883 | object_property_set_qobject(obj, QOBJECT(qbool), name, errp); |
| 884 | |
| 885 | QDECREF(qbool); |
| 886 | } |
| 887 | |
| 888 | bool object_property_get_bool(Object *obj, const char *name, |
| 889 | Error **errp) |
| 890 | { |
| 891 | QObject *ret = object_property_get_qobject(obj, name, errp); |
| 892 | QBool *qbool; |
| 893 | bool retval; |
| 894 | |
| 895 | if (!ret) { |
| 896 | return false; |
| 897 | } |
| 898 | qbool = qobject_to_qbool(ret); |
| 899 | if (!qbool) { |
| 900 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); |
| 901 | retval = false; |
| 902 | } else { |
| 903 | retval = qbool_get_int(qbool); |
| 904 | } |
| 905 | |
| 906 | QDECREF(qbool); |
| 907 | return retval; |
| 908 | } |
| 909 | |
| 910 | void object_property_set_int(Object *obj, int64_t value, |
| 911 | const char *name, Error **errp) |
| 912 | { |
| 913 | QInt *qint = qint_from_int(value); |
| 914 | object_property_set_qobject(obj, QOBJECT(qint), name, errp); |
| 915 | |
| 916 | QDECREF(qint); |
| 917 | } |
| 918 | |
| 919 | int64_t object_property_get_int(Object *obj, const char *name, |
| 920 | Error **errp) |
| 921 | { |
| 922 | QObject *ret = object_property_get_qobject(obj, name, errp); |
| 923 | QInt *qint; |
| 924 | int64_t retval; |
| 925 | |
| 926 | if (!ret) { |
| 927 | return -1; |
| 928 | } |
| 929 | qint = qobject_to_qint(ret); |
| 930 | if (!qint) { |
| 931 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); |
| 932 | retval = -1; |
| 933 | } else { |
| 934 | retval = qint_get_int(qint); |
| 935 | } |
| 936 | |
| 937 | QDECREF(qint); |
| 938 | return retval; |
| 939 | } |
| 940 | |
Paolo Bonzini | b2cd7de | 2012-02-09 09:52:59 +0100 | [diff] [blame] | 941 | void object_property_parse(Object *obj, const char *string, |
| 942 | const char *name, Error **errp) |
| 943 | { |
| 944 | StringInputVisitor *mi; |
| 945 | mi = string_input_visitor_new(string); |
| 946 | object_property_set(obj, string_input_get_visitor(mi), name, errp); |
| 947 | |
| 948 | string_input_visitor_cleanup(mi); |
| 949 | } |
| 950 | |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 951 | char *object_property_print(Object *obj, const char *name, bool human, |
Paolo Bonzini | b2cd7de | 2012-02-09 09:52:59 +0100 | [diff] [blame] | 952 | Error **errp) |
| 953 | { |
| 954 | StringOutputVisitor *mo; |
| 955 | char *string; |
| 956 | |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 957 | mo = string_output_visitor_new(human); |
Paolo Bonzini | 8185bfc | 2012-05-02 13:31:00 +0200 | [diff] [blame] | 958 | object_property_get(obj, string_output_get_visitor(mo), name, errp); |
Paolo Bonzini | b2cd7de | 2012-02-09 09:52:59 +0100 | [diff] [blame] | 959 | string = string_output_get_string(mo); |
| 960 | string_output_visitor_cleanup(mo); |
| 961 | return string; |
| 962 | } |
| 963 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 964 | const char *object_property_get_type(Object *obj, const char *name, Error **errp) |
| 965 | { |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 966 | ObjectProperty *prop = object_property_find(obj, name, errp); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 967 | if (prop == NULL) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 968 | return NULL; |
| 969 | } |
| 970 | |
| 971 | return prop->type; |
| 972 | } |
| 973 | |
| 974 | Object *object_get_root(void) |
| 975 | { |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 976 | static Object *root; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 977 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 978 | if (!root) { |
| 979 | root = object_new("container"); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 980 | } |
| 981 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 982 | return root; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | static void object_get_child_property(Object *obj, Visitor *v, void *opaque, |
| 986 | const char *name, Error **errp) |
| 987 | { |
| 988 | Object *child = opaque; |
| 989 | gchar *path; |
| 990 | |
| 991 | path = object_get_canonical_path(child); |
| 992 | visit_type_str(v, &path, name, errp); |
| 993 | g_free(path); |
| 994 | } |
| 995 | |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 996 | static void object_finalize_child_property(Object *obj, const char *name, |
| 997 | void *opaque) |
| 998 | { |
| 999 | Object *child = opaque; |
| 1000 | |
| 1001 | object_unref(child); |
| 1002 | } |
| 1003 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1004 | void object_property_add_child(Object *obj, const char *name, |
| 1005 | Object *child, Error **errp) |
| 1006 | { |
Paolo Bonzini | b0ed5e9 | 2013-12-20 23:21:08 +0100 | [diff] [blame] | 1007 | Error *local_err = NULL; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1008 | gchar *type; |
| 1009 | |
| 1010 | type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); |
| 1011 | |
Paolo Bonzini | b0ed5e9 | 2013-12-20 23:21:08 +0100 | [diff] [blame] | 1012 | object_property_add(obj, name, type, object_get_child_property, NULL, |
| 1013 | object_finalize_child_property, child, &local_err); |
| 1014 | if (local_err) { |
| 1015 | error_propagate(errp, local_err); |
| 1016 | goto out; |
| 1017 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1018 | object_ref(child); |
| 1019 | g_assert(child->parent == NULL); |
| 1020 | child->parent = obj; |
| 1021 | |
Paolo Bonzini | b0ed5e9 | 2013-12-20 23:21:08 +0100 | [diff] [blame] | 1022 | out: |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1023 | g_free(type); |
| 1024 | } |
| 1025 | |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame^] | 1026 | typedef struct { |
| 1027 | Object **child; |
| 1028 | ObjectPropertyLinkFlags flags; |
| 1029 | } LinkProperty; |
| 1030 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1031 | static void object_get_link_property(Object *obj, Visitor *v, void *opaque, |
| 1032 | const char *name, Error **errp) |
| 1033 | { |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame^] | 1034 | LinkProperty *lprop = opaque; |
| 1035 | Object **child = lprop->child; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1036 | gchar *path; |
| 1037 | |
| 1038 | if (*child) { |
| 1039 | path = object_get_canonical_path(*child); |
| 1040 | visit_type_str(v, &path, name, errp); |
| 1041 | g_free(path); |
| 1042 | } else { |
| 1043 | path = (gchar *)""; |
| 1044 | visit_type_str(v, &path, name, errp); |
| 1045 | } |
| 1046 | } |
| 1047 | |
Stefan Hajnoczi | f5ec670 | 2014-03-19 08:58:53 +0100 | [diff] [blame] | 1048 | /* |
| 1049 | * object_resolve_link: |
| 1050 | * |
| 1051 | * Lookup an object and ensure its type matches the link property type. This |
| 1052 | * is similar to object_resolve_path() except type verification against the |
| 1053 | * link property is performed. |
| 1054 | * |
| 1055 | * Returns: The matched object or NULL on path lookup failures. |
| 1056 | */ |
| 1057 | static Object *object_resolve_link(Object *obj, const char *name, |
| 1058 | const char *path, Error **errp) |
| 1059 | { |
| 1060 | const char *type; |
| 1061 | gchar *target_type; |
| 1062 | bool ambiguous = false; |
| 1063 | Object *target; |
| 1064 | |
| 1065 | /* Go from link<FOO> to FOO. */ |
| 1066 | type = object_property_get_type(obj, name, NULL); |
| 1067 | target_type = g_strndup(&type[5], strlen(type) - 6); |
| 1068 | target = object_resolve_path_type(path, target_type, &ambiguous); |
| 1069 | |
| 1070 | if (ambiguous) { |
| 1071 | error_set(errp, QERR_AMBIGUOUS_PATH, path); |
| 1072 | } else if (!target) { |
| 1073 | target = object_resolve_path(path, &ambiguous); |
| 1074 | if (target || ambiguous) { |
| 1075 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type); |
| 1076 | } else { |
| 1077 | error_set(errp, QERR_DEVICE_NOT_FOUND, path); |
| 1078 | } |
| 1079 | target = NULL; |
| 1080 | } |
| 1081 | g_free(target_type); |
| 1082 | |
| 1083 | return target; |
| 1084 | } |
| 1085 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1086 | static void object_set_link_property(Object *obj, Visitor *v, void *opaque, |
| 1087 | const char *name, Error **errp) |
| 1088 | { |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1089 | Error *local_err = NULL; |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame^] | 1090 | LinkProperty *prop = opaque; |
| 1091 | Object **child = prop->child; |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1092 | Object *old_target = *child; |
| 1093 | Object *new_target = NULL; |
| 1094 | char *path = NULL; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1095 | |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1096 | visit_type_str(v, &path, name, &local_err); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1097 | |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1098 | if (!local_err && strcmp(path, "") != 0) { |
| 1099 | new_target = object_resolve_link(obj, name, path, &local_err); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | g_free(path); |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1103 | if (local_err) { |
| 1104 | error_propagate(errp, local_err); |
| 1105 | return; |
| 1106 | } |
Alexander Barabash | f0cdc96 | 2012-02-22 19:22:26 +0200 | [diff] [blame] | 1107 | |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1108 | if (new_target) { |
| 1109 | object_ref(new_target); |
| 1110 | } |
| 1111 | *child = new_target; |
Alexander Barabash | f0cdc96 | 2012-02-22 19:22:26 +0200 | [diff] [blame] | 1112 | if (old_target != NULL) { |
| 1113 | object_unref(old_target); |
| 1114 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1115 | } |
| 1116 | |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame^] | 1117 | static void object_release_link_property(Object *obj, const char *name, |
| 1118 | void *opaque) |
| 1119 | { |
| 1120 | LinkProperty *prop = opaque; |
| 1121 | |
| 1122 | if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) { |
| 1123 | object_unref(*prop->child); |
| 1124 | } |
| 1125 | g_free(prop); |
| 1126 | } |
| 1127 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1128 | void object_property_add_link(Object *obj, const char *name, |
| 1129 | const char *type, Object **child, |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame^] | 1130 | ObjectPropertyLinkFlags flags, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1131 | Error **errp) |
| 1132 | { |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame^] | 1133 | Error *local_err = NULL; |
| 1134 | LinkProperty *prop = g_malloc(sizeof(*prop)); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1135 | gchar *full_type; |
| 1136 | |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame^] | 1137 | prop->child = child; |
| 1138 | prop->flags = flags; |
| 1139 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1140 | full_type = g_strdup_printf("link<%s>", type); |
| 1141 | |
| 1142 | object_property_add(obj, name, full_type, |
| 1143 | object_get_link_property, |
| 1144 | object_set_link_property, |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame^] | 1145 | object_release_link_property, |
| 1146 | prop, |
| 1147 | &local_err); |
| 1148 | if (local_err) { |
| 1149 | error_propagate(errp, local_err); |
| 1150 | g_free(prop); |
| 1151 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1152 | |
| 1153 | g_free(full_type); |
| 1154 | } |
| 1155 | |
Stefan Hajnoczi | 11f590b | 2014-03-03 11:30:02 +0100 | [diff] [blame] | 1156 | gchar *object_get_canonical_path_component(Object *obj) |
| 1157 | { |
| 1158 | ObjectProperty *prop = NULL; |
| 1159 | |
| 1160 | g_assert(obj); |
| 1161 | g_assert(obj->parent != NULL); |
| 1162 | |
| 1163 | QTAILQ_FOREACH(prop, &obj->parent->properties, node) { |
| 1164 | if (!object_property_is_child(prop)) { |
| 1165 | continue; |
| 1166 | } |
| 1167 | |
| 1168 | if (prop->opaque == obj) { |
| 1169 | return g_strdup(prop->name); |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | /* obj had a parent but was not a child, should never happen */ |
| 1174 | g_assert_not_reached(); |
| 1175 | return NULL; |
| 1176 | } |
| 1177 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1178 | gchar *object_get_canonical_path(Object *obj) |
| 1179 | { |
| 1180 | Object *root = object_get_root(); |
Stefan Hajnoczi | 11f590b | 2014-03-03 11:30:02 +0100 | [diff] [blame] | 1181 | char *newpath, *path = NULL; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1182 | |
| 1183 | while (obj != root) { |
Stefan Hajnoczi | 11f590b | 2014-03-03 11:30:02 +0100 | [diff] [blame] | 1184 | char *component = object_get_canonical_path_component(obj); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1185 | |
Stefan Hajnoczi | 11f590b | 2014-03-03 11:30:02 +0100 | [diff] [blame] | 1186 | if (path) { |
| 1187 | newpath = g_strdup_printf("%s/%s", component, path); |
| 1188 | g_free(component); |
| 1189 | g_free(path); |
| 1190 | path = newpath; |
| 1191 | } else { |
| 1192 | path = component; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1193 | } |
| 1194 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1195 | obj = obj->parent; |
| 1196 | } |
| 1197 | |
Stefan Hajnoczi | 11f590b | 2014-03-03 11:30:02 +0100 | [diff] [blame] | 1198 | newpath = g_strdup_printf("/%s", path ? path : ""); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1199 | g_free(path); |
| 1200 | |
| 1201 | return newpath; |
| 1202 | } |
| 1203 | |
Andreas Färber | 3e84b48 | 2013-01-15 02:55:10 +0100 | [diff] [blame] | 1204 | Object *object_resolve_path_component(Object *parent, const gchar *part) |
Paolo Bonzini | a612b2a | 2012-03-27 18:38:45 +0200 | [diff] [blame] | 1205 | { |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 1206 | ObjectProperty *prop = object_property_find(parent, part, NULL); |
Paolo Bonzini | a612b2a | 2012-03-27 18:38:45 +0200 | [diff] [blame] | 1207 | if (prop == NULL) { |
| 1208 | return NULL; |
| 1209 | } |
| 1210 | |
Andreas Färber | 5d9d3f4 | 2012-05-27 00:32:40 +0200 | [diff] [blame] | 1211 | if (object_property_is_link(prop)) { |
Paolo Bonzini | a612b2a | 2012-03-27 18:38:45 +0200 | [diff] [blame] | 1212 | return *(Object **)prop->opaque; |
Andreas Färber | 5d9d3f4 | 2012-05-27 00:32:40 +0200 | [diff] [blame] | 1213 | } else if (object_property_is_child(prop)) { |
Paolo Bonzini | a612b2a | 2012-03-27 18:38:45 +0200 | [diff] [blame] | 1214 | return prop->opaque; |
| 1215 | } else { |
| 1216 | return NULL; |
| 1217 | } |
| 1218 | } |
| 1219 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1220 | static Object *object_resolve_abs_path(Object *parent, |
| 1221 | gchar **parts, |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1222 | const char *typename, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1223 | int index) |
| 1224 | { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1225 | Object *child; |
| 1226 | |
| 1227 | if (parts[index] == NULL) { |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1228 | return object_dynamic_cast(parent, typename); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | if (strcmp(parts[index], "") == 0) { |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1232 | return object_resolve_abs_path(parent, parts, typename, index + 1); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1233 | } |
| 1234 | |
Paolo Bonzini | a612b2a | 2012-03-27 18:38:45 +0200 | [diff] [blame] | 1235 | child = object_resolve_path_component(parent, parts[index]); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1236 | if (!child) { |
| 1237 | return NULL; |
| 1238 | } |
| 1239 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1240 | return object_resolve_abs_path(child, parts, typename, index + 1); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | static Object *object_resolve_partial_path(Object *parent, |
| 1244 | gchar **parts, |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1245 | const char *typename, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1246 | bool *ambiguous) |
| 1247 | { |
| 1248 | Object *obj; |
| 1249 | ObjectProperty *prop; |
| 1250 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1251 | obj = object_resolve_abs_path(parent, parts, typename, 0); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1252 | |
| 1253 | QTAILQ_FOREACH(prop, &parent->properties, node) { |
| 1254 | Object *found; |
| 1255 | |
Andreas Färber | 5d9d3f4 | 2012-05-27 00:32:40 +0200 | [diff] [blame] | 1256 | if (!object_property_is_child(prop)) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1257 | continue; |
| 1258 | } |
| 1259 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1260 | found = object_resolve_partial_path(prop->opaque, parts, |
| 1261 | typename, ambiguous); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1262 | if (found) { |
| 1263 | if (obj) { |
| 1264 | if (ambiguous) { |
| 1265 | *ambiguous = true; |
| 1266 | } |
| 1267 | return NULL; |
| 1268 | } |
| 1269 | obj = found; |
| 1270 | } |
| 1271 | |
| 1272 | if (ambiguous && *ambiguous) { |
| 1273 | return NULL; |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | return obj; |
| 1278 | } |
| 1279 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1280 | Object *object_resolve_path_type(const char *path, const char *typename, |
| 1281 | bool *ambiguous) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1282 | { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1283 | Object *obj; |
| 1284 | gchar **parts; |
| 1285 | |
| 1286 | parts = g_strsplit(path, "/", 0); |
Paolo Bonzini | 2e1103f | 2013-04-18 18:44:02 +0200 | [diff] [blame] | 1287 | assert(parts); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1288 | |
Paolo Bonzini | 2e1103f | 2013-04-18 18:44:02 +0200 | [diff] [blame] | 1289 | if (parts[0] == NULL || strcmp(parts[0], "") != 0) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1290 | if (ambiguous) { |
| 1291 | *ambiguous = false; |
| 1292 | } |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1293 | obj = object_resolve_partial_path(object_get_root(), parts, |
| 1294 | typename, ambiguous); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1295 | } else { |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1296 | obj = object_resolve_abs_path(object_get_root(), parts, typename, 1); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | g_strfreev(parts); |
| 1300 | |
| 1301 | return obj; |
| 1302 | } |
| 1303 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1304 | Object *object_resolve_path(const char *path, bool *ambiguous) |
| 1305 | { |
| 1306 | return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); |
| 1307 | } |
| 1308 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1309 | typedef struct StringProperty |
| 1310 | { |
| 1311 | char *(*get)(Object *, Error **); |
| 1312 | void (*set)(Object *, const char *, Error **); |
| 1313 | } StringProperty; |
| 1314 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1315 | static void property_get_str(Object *obj, Visitor *v, void *opaque, |
| 1316 | const char *name, Error **errp) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1317 | { |
| 1318 | StringProperty *prop = opaque; |
| 1319 | char *value; |
| 1320 | |
| 1321 | value = prop->get(obj, errp); |
| 1322 | if (value) { |
| 1323 | visit_type_str(v, &value, name, errp); |
| 1324 | g_free(value); |
| 1325 | } |
| 1326 | } |
| 1327 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1328 | static void property_set_str(Object *obj, Visitor *v, void *opaque, |
| 1329 | const char *name, Error **errp) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1330 | { |
| 1331 | StringProperty *prop = opaque; |
| 1332 | char *value; |
| 1333 | Error *local_err = NULL; |
| 1334 | |
| 1335 | visit_type_str(v, &value, name, &local_err); |
| 1336 | if (local_err) { |
| 1337 | error_propagate(errp, local_err); |
| 1338 | return; |
| 1339 | } |
| 1340 | |
| 1341 | prop->set(obj, value, errp); |
| 1342 | g_free(value); |
| 1343 | } |
| 1344 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1345 | static void property_release_str(Object *obj, const char *name, |
| 1346 | void *opaque) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1347 | { |
| 1348 | StringProperty *prop = opaque; |
| 1349 | g_free(prop); |
| 1350 | } |
| 1351 | |
| 1352 | void object_property_add_str(Object *obj, const char *name, |
| 1353 | char *(*get)(Object *, Error **), |
| 1354 | void (*set)(Object *, const char *, Error **), |
| 1355 | Error **errp) |
| 1356 | { |
Stefan Hajnoczi | a01aedc | 2014-03-04 15:28:18 +0100 | [diff] [blame] | 1357 | Error *local_err = NULL; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1358 | StringProperty *prop = g_malloc0(sizeof(*prop)); |
| 1359 | |
| 1360 | prop->get = get; |
| 1361 | prop->set = set; |
| 1362 | |
| 1363 | object_property_add(obj, name, "string", |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1364 | get ? property_get_str : NULL, |
| 1365 | set ? property_set_str : NULL, |
| 1366 | property_release_str, |
Stefan Hajnoczi | a01aedc | 2014-03-04 15:28:18 +0100 | [diff] [blame] | 1367 | prop, &local_err); |
| 1368 | if (local_err) { |
| 1369 | error_propagate(errp, local_err); |
| 1370 | g_free(prop); |
| 1371 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1372 | } |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 1373 | |
Anthony Liguori | 0e55884 | 2012-06-25 10:32:46 -0500 | [diff] [blame] | 1374 | typedef struct BoolProperty |
| 1375 | { |
| 1376 | bool (*get)(Object *, Error **); |
| 1377 | void (*set)(Object *, bool, Error **); |
| 1378 | } BoolProperty; |
| 1379 | |
| 1380 | static void property_get_bool(Object *obj, Visitor *v, void *opaque, |
| 1381 | const char *name, Error **errp) |
| 1382 | { |
| 1383 | BoolProperty *prop = opaque; |
| 1384 | bool value; |
| 1385 | |
| 1386 | value = prop->get(obj, errp); |
| 1387 | visit_type_bool(v, &value, name, errp); |
| 1388 | } |
| 1389 | |
| 1390 | static void property_set_bool(Object *obj, Visitor *v, void *opaque, |
| 1391 | const char *name, Error **errp) |
| 1392 | { |
| 1393 | BoolProperty *prop = opaque; |
| 1394 | bool value; |
| 1395 | Error *local_err = NULL; |
| 1396 | |
| 1397 | visit_type_bool(v, &value, name, &local_err); |
| 1398 | if (local_err) { |
| 1399 | error_propagate(errp, local_err); |
| 1400 | return; |
| 1401 | } |
| 1402 | |
| 1403 | prop->set(obj, value, errp); |
| 1404 | } |
| 1405 | |
| 1406 | static void property_release_bool(Object *obj, const char *name, |
| 1407 | void *opaque) |
| 1408 | { |
| 1409 | BoolProperty *prop = opaque; |
| 1410 | g_free(prop); |
| 1411 | } |
| 1412 | |
| 1413 | void object_property_add_bool(Object *obj, const char *name, |
| 1414 | bool (*get)(Object *, Error **), |
| 1415 | void (*set)(Object *, bool, Error **), |
| 1416 | Error **errp) |
| 1417 | { |
Stefan Hajnoczi | a01aedc | 2014-03-04 15:28:18 +0100 | [diff] [blame] | 1418 | Error *local_err = NULL; |
Anthony Liguori | 0e55884 | 2012-06-25 10:32:46 -0500 | [diff] [blame] | 1419 | BoolProperty *prop = g_malloc0(sizeof(*prop)); |
| 1420 | |
| 1421 | prop->get = get; |
| 1422 | prop->set = set; |
| 1423 | |
| 1424 | object_property_add(obj, name, "bool", |
| 1425 | get ? property_get_bool : NULL, |
| 1426 | set ? property_set_bool : NULL, |
| 1427 | property_release_bool, |
Stefan Hajnoczi | a01aedc | 2014-03-04 15:28:18 +0100 | [diff] [blame] | 1428 | prop, &local_err); |
| 1429 | if (local_err) { |
| 1430 | error_propagate(errp, local_err); |
| 1431 | g_free(prop); |
| 1432 | } |
Anthony Liguori | 0e55884 | 2012-06-25 10:32:46 -0500 | [diff] [blame] | 1433 | } |
| 1434 | |
Paolo Bonzini | 2f262e0 | 2012-04-02 17:33:51 +0200 | [diff] [blame] | 1435 | static char *qdev_get_type(Object *obj, Error **errp) |
| 1436 | { |
| 1437 | return g_strdup(object_get_typename(obj)); |
| 1438 | } |
| 1439 | |
Michael S. Tsirkin | e732ea6 | 2013-09-22 10:10:17 +0300 | [diff] [blame] | 1440 | static void property_get_uint8_ptr(Object *obj, Visitor *v, |
| 1441 | void *opaque, const char *name, |
| 1442 | Error **errp) |
| 1443 | { |
| 1444 | uint8_t value = *(uint8_t *)opaque; |
| 1445 | visit_type_uint8(v, &value, name, errp); |
| 1446 | } |
| 1447 | |
| 1448 | static void property_get_uint16_ptr(Object *obj, Visitor *v, |
| 1449 | void *opaque, const char *name, |
| 1450 | Error **errp) |
| 1451 | { |
| 1452 | uint16_t value = *(uint16_t *)opaque; |
| 1453 | visit_type_uint16(v, &value, name, errp); |
| 1454 | } |
| 1455 | |
| 1456 | static void property_get_uint32_ptr(Object *obj, Visitor *v, |
| 1457 | void *opaque, const char *name, |
| 1458 | Error **errp) |
| 1459 | { |
| 1460 | uint32_t value = *(uint32_t *)opaque; |
| 1461 | visit_type_uint32(v, &value, name, errp); |
| 1462 | } |
| 1463 | |
| 1464 | static void property_get_uint64_ptr(Object *obj, Visitor *v, |
| 1465 | void *opaque, const char *name, |
| 1466 | Error **errp) |
| 1467 | { |
| 1468 | uint64_t value = *(uint64_t *)opaque; |
| 1469 | visit_type_uint64(v, &value, name, errp); |
| 1470 | } |
| 1471 | |
| 1472 | void object_property_add_uint8_ptr(Object *obj, const char *name, |
| 1473 | const uint8_t *v, Error **errp) |
| 1474 | { |
| 1475 | object_property_add(obj, name, "uint8", property_get_uint8_ptr, |
| 1476 | NULL, NULL, (void *)v, errp); |
| 1477 | } |
| 1478 | |
| 1479 | void object_property_add_uint16_ptr(Object *obj, const char *name, |
| 1480 | const uint16_t *v, Error **errp) |
| 1481 | { |
| 1482 | object_property_add(obj, name, "uint16", property_get_uint16_ptr, |
| 1483 | NULL, NULL, (void *)v, errp); |
| 1484 | } |
| 1485 | |
| 1486 | void object_property_add_uint32_ptr(Object *obj, const char *name, |
| 1487 | const uint32_t *v, Error **errp) |
| 1488 | { |
| 1489 | object_property_add(obj, name, "uint32", property_get_uint32_ptr, |
| 1490 | NULL, NULL, (void *)v, errp); |
| 1491 | } |
| 1492 | |
| 1493 | void object_property_add_uint64_ptr(Object *obj, const char *name, |
| 1494 | const uint64_t *v, Error **errp) |
| 1495 | { |
| 1496 | object_property_add(obj, name, "uint64", property_get_uint64_ptr, |
| 1497 | NULL, NULL, (void *)v, errp); |
| 1498 | } |
| 1499 | |
Paolo Bonzini | 2f262e0 | 2012-04-02 17:33:51 +0200 | [diff] [blame] | 1500 | static void object_instance_init(Object *obj) |
| 1501 | { |
| 1502 | object_property_add_str(obj, "type", qdev_get_type, NULL, NULL); |
| 1503 | } |
| 1504 | |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 1505 | static void register_types(void) |
| 1506 | { |
| 1507 | static TypeInfo interface_info = { |
| 1508 | .name = TYPE_INTERFACE, |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 1509 | .class_size = sizeof(InterfaceClass), |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 1510 | .abstract = true, |
| 1511 | }; |
| 1512 | |
| 1513 | static TypeInfo object_info = { |
| 1514 | .name = TYPE_OBJECT, |
| 1515 | .instance_size = sizeof(Object), |
Paolo Bonzini | 2f262e0 | 2012-04-02 17:33:51 +0200 | [diff] [blame] | 1516 | .instance_init = object_instance_init, |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 1517 | .abstract = true, |
| 1518 | }; |
| 1519 | |
Paolo Bonzini | 049cb3c | 2012-04-04 15:58:40 +0200 | [diff] [blame] | 1520 | type_interface = type_register_internal(&interface_info); |
| 1521 | type_register_internal(&object_info); |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | type_init(register_types) |