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