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 | { |
Michael S. Tsirkin | e998fa8 | 2013-03-18 21:01:37 +0200 | [diff] [blame] | 390 | if (obj->parent) { |
| 391 | object_property_del_child(obj->parent, obj, NULL); |
| 392 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 393 | } |
| 394 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 395 | static void object_deinit(Object *obj, TypeImpl *type) |
| 396 | { |
| 397 | if (type->instance_finalize) { |
| 398 | type->instance_finalize(obj); |
| 399 | } |
| 400 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 401 | if (type_has_parent(type)) { |
| 402 | object_deinit(obj, type_get_parent(type)); |
| 403 | } |
| 404 | } |
| 405 | |
Paolo Bonzini | 339c270 | 2012-11-23 09:47:16 +0100 | [diff] [blame] | 406 | static void object_finalize(void *data) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 407 | { |
| 408 | Object *obj = data; |
| 409 | TypeImpl *ti = obj->class->type; |
| 410 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 411 | object_property_del_all(obj); |
Paolo Bonzini | 76a6e1c | 2014-06-11 11:58:30 +0200 | [diff] [blame] | 412 | object_deinit(obj, ti); |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 413 | |
| 414 | g_assert(obj->ref == 0); |
Paolo Bonzini | fde9bf4 | 2012-11-23 09:47:14 +0100 | [diff] [blame] | 415 | if (obj->free) { |
| 416 | obj->free(obj); |
| 417 | } |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | Object *object_new_with_type(Type type) |
| 421 | { |
| 422 | Object *obj; |
| 423 | |
| 424 | g_assert(type != NULL); |
Igor Mitsyanko | ac45103 | 2012-02-28 15:57:11 +0400 | [diff] [blame] | 425 | type_initialize(type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 426 | |
| 427 | obj = g_malloc(type->instance_size); |
Andreas Färber | 5b9237f | 2013-08-30 18:28:37 +0200 | [diff] [blame] | 428 | object_initialize_with_type(obj, type->instance_size, type); |
Paolo Bonzini | fde9bf4 | 2012-11-23 09:47:14 +0100 | [diff] [blame] | 429 | obj->free = g_free; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 430 | |
| 431 | return obj; |
| 432 | } |
| 433 | |
| 434 | Object *object_new(const char *typename) |
| 435 | { |
| 436 | TypeImpl *ti = type_get_by_name(typename); |
| 437 | |
| 438 | return object_new_with_type(ti); |
| 439 | } |
| 440 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 441 | Object *object_dynamic_cast(Object *obj, const char *typename) |
| 442 | { |
Paolo Bonzini | b7f43fe | 2012-11-23 16:56:17 +0100 | [diff] [blame] | 443 | if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) { |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 444 | return obj; |
| 445 | } |
| 446 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 447 | return NULL; |
| 448 | } |
| 449 | |
Paolo Bonzini | be17f18 | 2013-05-10 14:16:38 +0200 | [diff] [blame] | 450 | Object *object_dynamic_cast_assert(Object *obj, const char *typename, |
| 451 | const char *file, int line, const char *func) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 452 | { |
Paolo Bonzini | fa131d9 | 2013-05-10 14:16:39 +0200 | [diff] [blame] | 453 | trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)", |
| 454 | typename, file, line, func); |
| 455 | |
Paolo Bonzini | 3556c23 | 2013-05-10 14:16:40 +0200 | [diff] [blame] | 456 | #ifdef CONFIG_QOM_CAST_DEBUG |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 457 | int i; |
| 458 | Object *inst; |
| 459 | |
Peter Crosthwaite | 95916ab | 2013-05-22 11:19:16 +1000 | [diff] [blame] | 460 | for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) { |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 461 | if (obj->class->object_cast_cache[i] == typename) { |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 462 | goto out; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | inst = object_dynamic_cast(obj, typename); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 467 | |
Paolo Bonzini | b7f43fe | 2012-11-23 16:56:17 +0100 | [diff] [blame] | 468 | if (!inst && obj) { |
Paolo Bonzini | be17f18 | 2013-05-10 14:16:38 +0200 | [diff] [blame] | 469 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
| 470 | file, line, func, obj, typename); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 471 | abort(); |
| 472 | } |
| 473 | |
Paolo Bonzini | 3556c23 | 2013-05-10 14:16:40 +0200 | [diff] [blame] | 474 | assert(obj == inst); |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 475 | |
Peter Crosthwaite | 95916ab | 2013-05-22 11:19:16 +1000 | [diff] [blame] | 476 | if (obj && obj == inst) { |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 477 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 478 | obj->class->object_cast_cache[i - 1] = |
| 479 | obj->class->object_cast_cache[i]; |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 480 | } |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 481 | obj->class->object_cast_cache[i - 1] = typename; |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | out: |
Paolo Bonzini | 3556c23 | 2013-05-10 14:16:40 +0200 | [diff] [blame] | 485 | #endif |
| 486 | return obj; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | ObjectClass *object_class_dynamic_cast(ObjectClass *class, |
| 490 | const char *typename) |
| 491 | { |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 492 | ObjectClass *ret = NULL; |
Paolo Bonzini | bf0fda3 | 2013-05-10 14:16:36 +0200 | [diff] [blame] | 493 | TypeImpl *target_type; |
| 494 | TypeImpl *type; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 495 | |
Paolo Bonzini | bf0fda3 | 2013-05-10 14:16:36 +0200 | [diff] [blame] | 496 | if (!class) { |
| 497 | return NULL; |
| 498 | } |
| 499 | |
Paolo Bonzini | 793c96b | 2013-05-10 14:16:37 +0200 | [diff] [blame] | 500 | /* A simple fast path that can trigger a lot for leaf classes. */ |
Paolo Bonzini | bf0fda3 | 2013-05-10 14:16:36 +0200 | [diff] [blame] | 501 | type = class->type; |
Paolo Bonzini | 793c96b | 2013-05-10 14:16:37 +0200 | [diff] [blame] | 502 | if (type->name == typename) { |
| 503 | return class; |
| 504 | } |
| 505 | |
Paolo Bonzini | bf0fda3 | 2013-05-10 14:16:36 +0200 | [diff] [blame] | 506 | target_type = type_get_by_name(typename); |
Alexander Graf | 9ab880b | 2013-04-30 15:02:16 +0200 | [diff] [blame] | 507 | if (!target_type) { |
| 508 | /* target class type unknown, so fail the cast */ |
| 509 | return NULL; |
| 510 | } |
| 511 | |
Peter Crosthwaite | 00e2cea | 2013-02-19 14:02:10 +1000 | [diff] [blame] | 512 | if (type->class->interfaces && |
| 513 | type_is_ancestor(target_type, type_interface)) { |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 514 | int found = 0; |
| 515 | GSList *i; |
| 516 | |
| 517 | for (i = class->interfaces; i; i = i->next) { |
| 518 | ObjectClass *target_class = i->data; |
| 519 | |
| 520 | if (type_is_ancestor(target_class->type, target_type)) { |
| 521 | ret = target_class; |
| 522 | found++; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /* The match was ambiguous, don't allow a cast */ |
| 527 | if (found > 1) { |
| 528 | ret = NULL; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 529 | } |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 530 | } else if (type_is_ancestor(type, target_type)) { |
| 531 | ret = class; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 532 | } |
| 533 | |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 534 | return ret; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, |
Paolo Bonzini | be17f18 | 2013-05-10 14:16:38 +0200 | [diff] [blame] | 538 | const char *typename, |
| 539 | const char *file, int line, |
| 540 | const char *func) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 541 | { |
Paolo Bonzini | fa131d9 | 2013-05-10 14:16:39 +0200 | [diff] [blame] | 542 | ObjectClass *ret; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 543 | |
Paolo Bonzini | fa131d9 | 2013-05-10 14:16:39 +0200 | [diff] [blame] | 544 | trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)", |
| 545 | typename, file, line, func); |
| 546 | |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 547 | #ifdef CONFIG_QOM_CAST_DEBUG |
| 548 | int i; |
| 549 | |
Peter Crosthwaite | 9d6a3d5 | 2013-06-18 19:18:59 +1000 | [diff] [blame] | 550 | for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) { |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 551 | if (class->class_cast_cache[i] == typename) { |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 552 | ret = class; |
| 553 | goto out; |
| 554 | } |
| 555 | } |
| 556 | #else |
Peter Crosthwaite | 9d6a3d5 | 2013-06-18 19:18:59 +1000 | [diff] [blame] | 557 | if (!class || !class->interfaces) { |
Paolo Bonzini | 3556c23 | 2013-05-10 14:16:40 +0200 | [diff] [blame] | 558 | return class; |
| 559 | } |
| 560 | #endif |
| 561 | |
Paolo Bonzini | fa131d9 | 2013-05-10 14:16:39 +0200 | [diff] [blame] | 562 | ret = object_class_dynamic_cast(class, typename); |
Paolo Bonzini | bf0fda3 | 2013-05-10 14:16:36 +0200 | [diff] [blame] | 563 | if (!ret && class) { |
Paolo Bonzini | be17f18 | 2013-05-10 14:16:38 +0200 | [diff] [blame] | 564 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
| 565 | file, line, func, class, typename); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 566 | abort(); |
| 567 | } |
| 568 | |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 569 | #ifdef CONFIG_QOM_CAST_DEBUG |
Peter Crosthwaite | 9d6a3d5 | 2013-06-18 19:18:59 +1000 | [diff] [blame] | 570 | if (class && ret == class) { |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 571 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 572 | class->class_cast_cache[i - 1] = class->class_cast_cache[i]; |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 573 | } |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 574 | class->class_cast_cache[i - 1] = typename; |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 575 | } |
| 576 | out: |
| 577 | #endif |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | const char *object_get_typename(Object *obj) |
| 582 | { |
| 583 | return obj->class->type->name; |
| 584 | } |
| 585 | |
| 586 | ObjectClass *object_get_class(Object *obj) |
| 587 | { |
| 588 | return obj->class; |
| 589 | } |
| 590 | |
Andreas Färber | 1786237 | 2013-01-23 12:20:18 +0100 | [diff] [blame] | 591 | bool object_class_is_abstract(ObjectClass *klass) |
| 592 | { |
| 593 | return klass->type->abstract; |
| 594 | } |
| 595 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 596 | const char *object_class_get_name(ObjectClass *klass) |
| 597 | { |
| 598 | return klass->type->name; |
| 599 | } |
| 600 | |
| 601 | ObjectClass *object_class_by_name(const char *typename) |
| 602 | { |
| 603 | TypeImpl *type = type_get_by_name(typename); |
| 604 | |
| 605 | if (!type) { |
| 606 | return NULL; |
| 607 | } |
| 608 | |
Igor Mitsyanko | ac45103 | 2012-02-28 15:57:11 +0400 | [diff] [blame] | 609 | type_initialize(type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 610 | |
| 611 | return type->class; |
| 612 | } |
| 613 | |
Paolo Bonzini | e7cce67 | 2012-05-02 13:30:54 +0200 | [diff] [blame] | 614 | ObjectClass *object_class_get_parent(ObjectClass *class) |
| 615 | { |
| 616 | TypeImpl *type = type_get_parent(class->type); |
| 617 | |
| 618 | if (!type) { |
| 619 | return NULL; |
| 620 | } |
| 621 | |
| 622 | type_initialize(type); |
| 623 | |
| 624 | return type->class; |
| 625 | } |
| 626 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 627 | typedef struct OCFData |
| 628 | { |
| 629 | void (*fn)(ObjectClass *klass, void *opaque); |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 630 | const char *implements_type; |
| 631 | bool include_abstract; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 632 | void *opaque; |
| 633 | } OCFData; |
| 634 | |
| 635 | static void object_class_foreach_tramp(gpointer key, gpointer value, |
| 636 | gpointer opaque) |
| 637 | { |
| 638 | OCFData *data = opaque; |
| 639 | TypeImpl *type = value; |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 640 | ObjectClass *k; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 641 | |
Igor Mitsyanko | ac45103 | 2012-02-28 15:57:11 +0400 | [diff] [blame] | 642 | type_initialize(type); |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 643 | k = type->class; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 644 | |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 645 | if (!data->include_abstract && type->abstract) { |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | if (data->implements_type && |
| 650 | !object_class_dynamic_cast(k, data->implements_type)) { |
| 651 | return; |
| 652 | } |
| 653 | |
| 654 | data->fn(k, data->opaque); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 658 | const char *implements_type, bool include_abstract, |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 659 | void *opaque) |
| 660 | { |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 661 | OCFData data = { fn, implements_type, include_abstract, opaque }; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 662 | |
Hervé Poussineau | f54c19c | 2013-12-03 16:42:00 +0100 | [diff] [blame] | 663 | enumerating_types = true; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 664 | g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); |
Hervé Poussineau | f54c19c | 2013-12-03 16:42:00 +0100 | [diff] [blame] | 665 | enumerating_types = false; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 666 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 667 | |
Paolo Bonzini | 32efc53 | 2012-04-11 23:30:20 +0200 | [diff] [blame] | 668 | int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), |
| 669 | void *opaque) |
| 670 | { |
Alexey Kardashevskiy | 8af734c | 2014-07-14 00:41:08 +1000 | [diff] [blame] | 671 | ObjectProperty *prop, *next; |
Paolo Bonzini | 32efc53 | 2012-04-11 23:30:20 +0200 | [diff] [blame] | 672 | int ret = 0; |
| 673 | |
Alexey Kardashevskiy | 8af734c | 2014-07-14 00:41:08 +1000 | [diff] [blame] | 674 | QTAILQ_FOREACH_SAFE(prop, &obj->properties, node, next) { |
Paolo Bonzini | 32efc53 | 2012-04-11 23:30:20 +0200 | [diff] [blame] | 675 | if (object_property_is_child(prop)) { |
| 676 | ret = fn(prop->opaque, opaque); |
| 677 | if (ret != 0) { |
| 678 | break; |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | return ret; |
| 683 | } |
| 684 | |
Andreas Färber | 418ba9e | 2012-02-25 23:07:34 +0100 | [diff] [blame] | 685 | static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) |
| 686 | { |
| 687 | GSList **list = opaque; |
| 688 | |
| 689 | *list = g_slist_prepend(*list, klass); |
| 690 | } |
| 691 | |
| 692 | GSList *object_class_get_list(const char *implements_type, |
| 693 | bool include_abstract) |
| 694 | { |
| 695 | GSList *list = NULL; |
| 696 | |
| 697 | object_class_foreach(object_class_get_list_tramp, |
| 698 | implements_type, include_abstract, &list); |
| 699 | return list; |
| 700 | } |
| 701 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 702 | void object_ref(Object *obj) |
| 703 | { |
Peter Crosthwaite | 8ffad85 | 2014-06-05 23:13:36 -0700 | [diff] [blame] | 704 | if (!obj) { |
| 705 | return; |
| 706 | } |
Jan Kiszka | f08c03f | 2013-07-02 11:36:39 +0200 | [diff] [blame] | 707 | atomic_inc(&obj->ref); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | void object_unref(Object *obj) |
| 711 | { |
Peter Crosthwaite | 8ffad85 | 2014-06-05 23:13:36 -0700 | [diff] [blame] | 712 | if (!obj) { |
| 713 | return; |
| 714 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 715 | g_assert(obj->ref > 0); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 716 | |
| 717 | /* parent always holds a reference to its children */ |
Jan Kiszka | f08c03f | 2013-07-02 11:36:39 +0200 | [diff] [blame] | 718 | if (atomic_fetch_dec(&obj->ref) == 1) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 719 | object_finalize(obj); |
| 720 | } |
| 721 | } |
| 722 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 723 | ObjectProperty * |
| 724 | object_property_add(Object *obj, const char *name, const char *type, |
| 725 | ObjectPropertyAccessor *get, |
| 726 | ObjectPropertyAccessor *set, |
| 727 | ObjectPropertyRelease *release, |
| 728 | void *opaque, Error **errp) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 729 | { |
Peter Maydell | 54852b0 | 2013-03-25 13:15:13 +0000 | [diff] [blame] | 730 | ObjectProperty *prop; |
Peter Crosthwaite | 3396590 | 2014-08-19 23:55:52 -0700 | [diff] [blame] | 731 | size_t name_len = strlen(name); |
| 732 | |
| 733 | if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) { |
| 734 | int i; |
| 735 | ObjectProperty *ret; |
| 736 | char *name_no_array = g_strdup(name); |
| 737 | |
| 738 | name_no_array[name_len - 3] = '\0'; |
| 739 | for (i = 0; ; ++i) { |
| 740 | char *full_name = g_strdup_printf("%s[%d]", name_no_array, i); |
| 741 | |
| 742 | ret = object_property_add(obj, full_name, type, get, set, |
| 743 | release, opaque, NULL); |
| 744 | g_free(full_name); |
| 745 | if (ret) { |
| 746 | break; |
| 747 | } |
| 748 | } |
| 749 | g_free(name_no_array); |
| 750 | return ret; |
| 751 | } |
Peter Maydell | 54852b0 | 2013-03-25 13:15:13 +0000 | [diff] [blame] | 752 | |
| 753 | QTAILQ_FOREACH(prop, &obj->properties, node) { |
| 754 | if (strcmp(prop->name, name) == 0) { |
| 755 | error_setg(errp, "attempt to add duplicate property '%s'" |
| 756 | " to object (type '%s')", name, |
| 757 | object_get_typename(obj)); |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 758 | return NULL; |
Peter Maydell | 54852b0 | 2013-03-25 13:15:13 +0000 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
| 762 | prop = g_malloc0(sizeof(*prop)); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 763 | |
| 764 | prop->name = g_strdup(name); |
| 765 | prop->type = g_strdup(type); |
| 766 | |
| 767 | prop->get = get; |
| 768 | prop->set = set; |
| 769 | prop->release = release; |
| 770 | prop->opaque = opaque; |
| 771 | |
| 772 | QTAILQ_INSERT_TAIL(&obj->properties, prop, node); |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 773 | return prop; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 774 | } |
| 775 | |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 776 | ObjectProperty *object_property_find(Object *obj, const char *name, |
| 777 | Error **errp) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 778 | { |
| 779 | ObjectProperty *prop; |
| 780 | |
| 781 | QTAILQ_FOREACH(prop, &obj->properties, node) { |
| 782 | if (strcmp(prop->name, name) == 0) { |
| 783 | return prop; |
| 784 | } |
| 785 | } |
| 786 | |
Cole Robinson | f231b88 | 2014-03-21 19:42:26 -0400 | [diff] [blame] | 787 | error_setg(errp, "Property '.%s' not found", name); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 788 | return NULL; |
| 789 | } |
| 790 | |
| 791 | void object_property_del(Object *obj, const char *name, Error **errp) |
| 792 | { |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 793 | ObjectProperty *prop = object_property_find(obj, name, errp); |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 794 | if (prop == NULL) { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 795 | return; |
| 796 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 797 | |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 798 | if (prop->release) { |
| 799 | prop->release(obj, name, prop->opaque); |
| 800 | } |
| 801 | |
| 802 | QTAILQ_REMOVE(&obj->properties, prop, node); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 803 | |
| 804 | g_free(prop->name); |
| 805 | g_free(prop->type); |
| 806 | g_free(prop); |
| 807 | } |
| 808 | |
| 809 | void object_property_get(Object *obj, Visitor *v, const char *name, |
| 810 | Error **errp) |
| 811 | { |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 812 | ObjectProperty *prop = object_property_find(obj, name, errp); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 813 | if (prop == NULL) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 814 | return; |
| 815 | } |
| 816 | |
| 817 | if (!prop->get) { |
| 818 | error_set(errp, QERR_PERMISSION_DENIED); |
| 819 | } else { |
| 820 | prop->get(obj, v, prop->opaque, name, errp); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | void object_property_set(Object *obj, Visitor *v, const char *name, |
| 825 | Error **errp) |
| 826 | { |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 827 | ObjectProperty *prop = object_property_find(obj, name, errp); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 828 | if (prop == NULL) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 829 | return; |
| 830 | } |
| 831 | |
| 832 | if (!prop->set) { |
| 833 | error_set(errp, QERR_PERMISSION_DENIED); |
| 834 | } else { |
| 835 | prop->set(obj, v, prop->opaque, name, errp); |
| 836 | } |
| 837 | } |
| 838 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 839 | void object_property_set_str(Object *obj, const char *value, |
| 840 | const char *name, Error **errp) |
| 841 | { |
| 842 | QString *qstr = qstring_from_str(value); |
| 843 | object_property_set_qobject(obj, QOBJECT(qstr), name, errp); |
| 844 | |
| 845 | QDECREF(qstr); |
| 846 | } |
| 847 | |
| 848 | char *object_property_get_str(Object *obj, const char *name, |
| 849 | Error **errp) |
| 850 | { |
| 851 | QObject *ret = object_property_get_qobject(obj, name, errp); |
| 852 | QString *qstring; |
| 853 | char *retval; |
| 854 | |
| 855 | if (!ret) { |
| 856 | return NULL; |
| 857 | } |
| 858 | qstring = qobject_to_qstring(ret); |
| 859 | if (!qstring) { |
| 860 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); |
| 861 | retval = NULL; |
| 862 | } else { |
| 863 | retval = g_strdup(qstring_get_str(qstring)); |
| 864 | } |
| 865 | |
| 866 | QDECREF(qstring); |
| 867 | return retval; |
| 868 | } |
| 869 | |
Paolo Bonzini | 1d9c5a1 | 2012-02-02 10:51:57 +0100 | [diff] [blame] | 870 | void object_property_set_link(Object *obj, Object *value, |
| 871 | const char *name, Error **errp) |
| 872 | { |
Vlad Yasevich | 2d3aa28 | 2013-11-15 12:09:47 -0500 | [diff] [blame] | 873 | gchar *path = object_get_canonical_path(value); |
| 874 | object_property_set_str(obj, path, name, errp); |
| 875 | g_free(path); |
Paolo Bonzini | 1d9c5a1 | 2012-02-02 10:51:57 +0100 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | Object *object_property_get_link(Object *obj, const char *name, |
| 879 | Error **errp) |
| 880 | { |
| 881 | char *str = object_property_get_str(obj, name, errp); |
| 882 | Object *target = NULL; |
| 883 | |
| 884 | if (str && *str) { |
| 885 | target = object_resolve_path(str, NULL); |
| 886 | if (!target) { |
| 887 | error_set(errp, QERR_DEVICE_NOT_FOUND, str); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | g_free(str); |
| 892 | return target; |
| 893 | } |
| 894 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 895 | void object_property_set_bool(Object *obj, bool value, |
| 896 | const char *name, Error **errp) |
| 897 | { |
| 898 | QBool *qbool = qbool_from_int(value); |
| 899 | object_property_set_qobject(obj, QOBJECT(qbool), name, errp); |
| 900 | |
| 901 | QDECREF(qbool); |
| 902 | } |
| 903 | |
| 904 | bool object_property_get_bool(Object *obj, const char *name, |
| 905 | Error **errp) |
| 906 | { |
| 907 | QObject *ret = object_property_get_qobject(obj, name, errp); |
| 908 | QBool *qbool; |
| 909 | bool retval; |
| 910 | |
| 911 | if (!ret) { |
| 912 | return false; |
| 913 | } |
| 914 | qbool = qobject_to_qbool(ret); |
| 915 | if (!qbool) { |
| 916 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); |
| 917 | retval = false; |
| 918 | } else { |
| 919 | retval = qbool_get_int(qbool); |
| 920 | } |
| 921 | |
| 922 | QDECREF(qbool); |
| 923 | return retval; |
| 924 | } |
| 925 | |
| 926 | void object_property_set_int(Object *obj, int64_t value, |
| 927 | const char *name, Error **errp) |
| 928 | { |
| 929 | QInt *qint = qint_from_int(value); |
| 930 | object_property_set_qobject(obj, QOBJECT(qint), name, errp); |
| 931 | |
| 932 | QDECREF(qint); |
| 933 | } |
| 934 | |
| 935 | int64_t object_property_get_int(Object *obj, const char *name, |
| 936 | Error **errp) |
| 937 | { |
| 938 | QObject *ret = object_property_get_qobject(obj, name, errp); |
| 939 | QInt *qint; |
| 940 | int64_t retval; |
| 941 | |
| 942 | if (!ret) { |
| 943 | return -1; |
| 944 | } |
| 945 | qint = qobject_to_qint(ret); |
| 946 | if (!qint) { |
| 947 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); |
| 948 | retval = -1; |
| 949 | } else { |
| 950 | retval = qint_get_int(qint); |
| 951 | } |
| 952 | |
| 953 | QDECREF(qint); |
| 954 | return retval; |
| 955 | } |
| 956 | |
Hu Tao | 1f21772 | 2014-05-14 17:43:33 +0800 | [diff] [blame] | 957 | int object_property_get_enum(Object *obj, const char *name, |
| 958 | const char *strings[], Error **errp) |
| 959 | { |
| 960 | StringOutputVisitor *sov; |
| 961 | StringInputVisitor *siv; |
Chen Fan | 976620a | 2014-08-18 14:46:34 +0800 | [diff] [blame] | 962 | char *str; |
Hu Tao | 1f21772 | 2014-05-14 17:43:33 +0800 | [diff] [blame] | 963 | int ret; |
| 964 | |
| 965 | sov = string_output_visitor_new(false); |
| 966 | object_property_get(obj, string_output_get_visitor(sov), name, errp); |
Chen Fan | 976620a | 2014-08-18 14:46:34 +0800 | [diff] [blame] | 967 | str = string_output_get_string(sov); |
| 968 | siv = string_input_visitor_new(str); |
Hu Tao | 1f21772 | 2014-05-14 17:43:33 +0800 | [diff] [blame] | 969 | string_output_visitor_cleanup(sov); |
| 970 | visit_type_enum(string_input_get_visitor(siv), |
| 971 | &ret, strings, NULL, name, errp); |
Chen Fan | 976620a | 2014-08-18 14:46:34 +0800 | [diff] [blame] | 972 | |
| 973 | g_free(str); |
Hu Tao | 1f21772 | 2014-05-14 17:43:33 +0800 | [diff] [blame] | 974 | string_input_visitor_cleanup(siv); |
| 975 | |
| 976 | return ret; |
| 977 | } |
| 978 | |
| 979 | void object_property_get_uint16List(Object *obj, const char *name, |
| 980 | uint16List **list, Error **errp) |
| 981 | { |
| 982 | StringOutputVisitor *ov; |
| 983 | StringInputVisitor *iv; |
Chen Fan | 976620a | 2014-08-18 14:46:34 +0800 | [diff] [blame] | 984 | char *str; |
Hu Tao | 1f21772 | 2014-05-14 17:43:33 +0800 | [diff] [blame] | 985 | |
| 986 | ov = string_output_visitor_new(false); |
| 987 | object_property_get(obj, string_output_get_visitor(ov), |
| 988 | name, errp); |
Chen Fan | 976620a | 2014-08-18 14:46:34 +0800 | [diff] [blame] | 989 | str = string_output_get_string(ov); |
| 990 | iv = string_input_visitor_new(str); |
Hu Tao | 1f21772 | 2014-05-14 17:43:33 +0800 | [diff] [blame] | 991 | visit_type_uint16List(string_input_get_visitor(iv), |
| 992 | list, NULL, errp); |
Chen Fan | 976620a | 2014-08-18 14:46:34 +0800 | [diff] [blame] | 993 | |
| 994 | g_free(str); |
Hu Tao | 1f21772 | 2014-05-14 17:43:33 +0800 | [diff] [blame] | 995 | string_output_visitor_cleanup(ov); |
| 996 | string_input_visitor_cleanup(iv); |
| 997 | } |
| 998 | |
Paolo Bonzini | b2cd7de | 2012-02-09 09:52:59 +0100 | [diff] [blame] | 999 | void object_property_parse(Object *obj, const char *string, |
| 1000 | const char *name, Error **errp) |
| 1001 | { |
| 1002 | StringInputVisitor *mi; |
| 1003 | mi = string_input_visitor_new(string); |
| 1004 | object_property_set(obj, string_input_get_visitor(mi), name, errp); |
| 1005 | |
| 1006 | string_input_visitor_cleanup(mi); |
| 1007 | } |
| 1008 | |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 1009 | char *object_property_print(Object *obj, const char *name, bool human, |
Paolo Bonzini | b2cd7de | 2012-02-09 09:52:59 +0100 | [diff] [blame] | 1010 | Error **errp) |
| 1011 | { |
| 1012 | StringOutputVisitor *mo; |
Gonglei | 3a53009 | 2014-09-27 13:13:55 +0800 | [diff] [blame] | 1013 | char *string = NULL; |
| 1014 | Error *local_err = NULL; |
Paolo Bonzini | b2cd7de | 2012-02-09 09:52:59 +0100 | [diff] [blame] | 1015 | |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 1016 | mo = string_output_visitor_new(human); |
Gonglei | 3a53009 | 2014-09-27 13:13:55 +0800 | [diff] [blame] | 1017 | object_property_get(obj, string_output_get_visitor(mo), name, &local_err); |
| 1018 | if (local_err) { |
| 1019 | error_propagate(errp, local_err); |
| 1020 | goto out; |
| 1021 | } |
| 1022 | |
Paolo Bonzini | b2cd7de | 2012-02-09 09:52:59 +0100 | [diff] [blame] | 1023 | string = string_output_get_string(mo); |
Gonglei | 3a53009 | 2014-09-27 13:13:55 +0800 | [diff] [blame] | 1024 | |
| 1025 | out: |
Paolo Bonzini | b2cd7de | 2012-02-09 09:52:59 +0100 | [diff] [blame] | 1026 | string_output_visitor_cleanup(mo); |
| 1027 | return string; |
| 1028 | } |
| 1029 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1030 | const char *object_property_get_type(Object *obj, const char *name, Error **errp) |
| 1031 | { |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 1032 | ObjectProperty *prop = object_property_find(obj, name, errp); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1033 | if (prop == NULL) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1034 | return NULL; |
| 1035 | } |
| 1036 | |
| 1037 | return prop->type; |
| 1038 | } |
| 1039 | |
| 1040 | Object *object_get_root(void) |
| 1041 | { |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 1042 | static Object *root; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1043 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 1044 | if (!root) { |
| 1045 | root = object_new("container"); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1046 | } |
| 1047 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 1048 | return root; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | static void object_get_child_property(Object *obj, Visitor *v, void *opaque, |
| 1052 | const char *name, Error **errp) |
| 1053 | { |
| 1054 | Object *child = opaque; |
| 1055 | gchar *path; |
| 1056 | |
| 1057 | path = object_get_canonical_path(child); |
| 1058 | visit_type_str(v, &path, name, errp); |
| 1059 | g_free(path); |
| 1060 | } |
| 1061 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1062 | static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part) |
| 1063 | { |
| 1064 | return opaque; |
| 1065 | } |
| 1066 | |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 1067 | static void object_finalize_child_property(Object *obj, const char *name, |
| 1068 | void *opaque) |
| 1069 | { |
| 1070 | Object *child = opaque; |
| 1071 | |
Paolo Bonzini | bffc687 | 2014-06-11 11:57:38 +0200 | [diff] [blame] | 1072 | if (child->class->unparent) { |
| 1073 | (child->class->unparent)(child); |
| 1074 | } |
| 1075 | child->parent = NULL; |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 1076 | object_unref(child); |
| 1077 | } |
| 1078 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1079 | void object_property_add_child(Object *obj, const char *name, |
| 1080 | Object *child, Error **errp) |
| 1081 | { |
Paolo Bonzini | b0ed5e9 | 2013-12-20 23:21:08 +0100 | [diff] [blame] | 1082 | Error *local_err = NULL; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1083 | gchar *type; |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1084 | ObjectProperty *op; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1085 | |
| 1086 | type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); |
| 1087 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1088 | op = object_property_add(obj, name, type, object_get_child_property, NULL, |
| 1089 | object_finalize_child_property, child, &local_err); |
Paolo Bonzini | b0ed5e9 | 2013-12-20 23:21:08 +0100 | [diff] [blame] | 1090 | if (local_err) { |
| 1091 | error_propagate(errp, local_err); |
| 1092 | goto out; |
| 1093 | } |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1094 | |
| 1095 | op->resolve = object_resolve_child_property; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1096 | object_ref(child); |
| 1097 | g_assert(child->parent == NULL); |
| 1098 | child->parent = obj; |
| 1099 | |
Paolo Bonzini | b0ed5e9 | 2013-12-20 23:21:08 +0100 | [diff] [blame] | 1100 | out: |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1101 | g_free(type); |
| 1102 | } |
| 1103 | |
Stefan Hajnoczi | 39f72ef | 2014-03-19 08:58:56 +0100 | [diff] [blame] | 1104 | void object_property_allow_set_link(Object *obj, const char *name, |
| 1105 | Object *val, Error **errp) |
| 1106 | { |
| 1107 | /* Allow the link to be set, always */ |
| 1108 | } |
| 1109 | |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1110 | typedef struct { |
| 1111 | Object **child; |
Stefan Hajnoczi | 39f72ef | 2014-03-19 08:58:56 +0100 | [diff] [blame] | 1112 | void (*check)(Object *, const char *, Object *, Error **); |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1113 | ObjectPropertyLinkFlags flags; |
| 1114 | } LinkProperty; |
| 1115 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1116 | static void object_get_link_property(Object *obj, Visitor *v, void *opaque, |
| 1117 | const char *name, Error **errp) |
| 1118 | { |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1119 | LinkProperty *lprop = opaque; |
| 1120 | Object **child = lprop->child; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1121 | gchar *path; |
| 1122 | |
| 1123 | if (*child) { |
| 1124 | path = object_get_canonical_path(*child); |
| 1125 | visit_type_str(v, &path, name, errp); |
| 1126 | g_free(path); |
| 1127 | } else { |
| 1128 | path = (gchar *)""; |
| 1129 | visit_type_str(v, &path, name, errp); |
| 1130 | } |
| 1131 | } |
| 1132 | |
Stefan Hajnoczi | f5ec670 | 2014-03-19 08:58:53 +0100 | [diff] [blame] | 1133 | /* |
| 1134 | * object_resolve_link: |
| 1135 | * |
| 1136 | * Lookup an object and ensure its type matches the link property type. This |
| 1137 | * is similar to object_resolve_path() except type verification against the |
| 1138 | * link property is performed. |
| 1139 | * |
| 1140 | * Returns: The matched object or NULL on path lookup failures. |
| 1141 | */ |
| 1142 | static Object *object_resolve_link(Object *obj, const char *name, |
| 1143 | const char *path, Error **errp) |
| 1144 | { |
| 1145 | const char *type; |
| 1146 | gchar *target_type; |
| 1147 | bool ambiguous = false; |
| 1148 | Object *target; |
| 1149 | |
| 1150 | /* Go from link<FOO> to FOO. */ |
| 1151 | type = object_property_get_type(obj, name, NULL); |
| 1152 | target_type = g_strndup(&type[5], strlen(type) - 6); |
| 1153 | target = object_resolve_path_type(path, target_type, &ambiguous); |
| 1154 | |
| 1155 | if (ambiguous) { |
Cole Robinson | f231b88 | 2014-03-21 19:42:26 -0400 | [diff] [blame] | 1156 | error_set(errp, ERROR_CLASS_GENERIC_ERROR, |
| 1157 | "Path '%s' does not uniquely identify an object", path); |
Stefan Hajnoczi | f5ec670 | 2014-03-19 08:58:53 +0100 | [diff] [blame] | 1158 | } else if (!target) { |
| 1159 | target = object_resolve_path(path, &ambiguous); |
| 1160 | if (target || ambiguous) { |
| 1161 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type); |
| 1162 | } else { |
| 1163 | error_set(errp, QERR_DEVICE_NOT_FOUND, path); |
| 1164 | } |
| 1165 | target = NULL; |
| 1166 | } |
| 1167 | g_free(target_type); |
| 1168 | |
| 1169 | return target; |
| 1170 | } |
| 1171 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1172 | static void object_set_link_property(Object *obj, Visitor *v, void *opaque, |
| 1173 | const char *name, Error **errp) |
| 1174 | { |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1175 | Error *local_err = NULL; |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1176 | LinkProperty *prop = opaque; |
| 1177 | Object **child = prop->child; |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1178 | Object *old_target = *child; |
| 1179 | Object *new_target = NULL; |
| 1180 | char *path = NULL; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1181 | |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1182 | visit_type_str(v, &path, name, &local_err); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1183 | |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1184 | if (!local_err && strcmp(path, "") != 0) { |
| 1185 | new_target = object_resolve_link(obj, name, path, &local_err); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | g_free(path); |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1189 | if (local_err) { |
| 1190 | error_propagate(errp, local_err); |
| 1191 | return; |
| 1192 | } |
Alexander Barabash | f0cdc96 | 2012-02-22 19:22:26 +0200 | [diff] [blame] | 1193 | |
Stefan Hajnoczi | 39f72ef | 2014-03-19 08:58:56 +0100 | [diff] [blame] | 1194 | prop->check(obj, name, new_target, &local_err); |
| 1195 | if (local_err) { |
| 1196 | error_propagate(errp, local_err); |
| 1197 | return; |
| 1198 | } |
| 1199 | |
Peter Crosthwaite | 8ffad85 | 2014-06-05 23:13:36 -0700 | [diff] [blame] | 1200 | object_ref(new_target); |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1201 | *child = new_target; |
Peter Crosthwaite | 8ffad85 | 2014-06-05 23:13:36 -0700 | [diff] [blame] | 1202 | object_unref(old_target); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1203 | } |
| 1204 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1205 | static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part) |
| 1206 | { |
| 1207 | LinkProperty *lprop = opaque; |
| 1208 | |
| 1209 | return *lprop->child; |
| 1210 | } |
| 1211 | |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1212 | static void object_release_link_property(Object *obj, const char *name, |
| 1213 | void *opaque) |
| 1214 | { |
| 1215 | LinkProperty *prop = opaque; |
| 1216 | |
| 1217 | if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) { |
| 1218 | object_unref(*prop->child); |
| 1219 | } |
| 1220 | g_free(prop); |
| 1221 | } |
| 1222 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1223 | void object_property_add_link(Object *obj, const char *name, |
| 1224 | const char *type, Object **child, |
Stefan Hajnoczi | 39f72ef | 2014-03-19 08:58:56 +0100 | [diff] [blame] | 1225 | void (*check)(Object *, const char *, |
| 1226 | Object *, Error **), |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1227 | ObjectPropertyLinkFlags flags, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1228 | Error **errp) |
| 1229 | { |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1230 | Error *local_err = NULL; |
| 1231 | LinkProperty *prop = g_malloc(sizeof(*prop)); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1232 | gchar *full_type; |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1233 | ObjectProperty *op; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1234 | |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1235 | prop->child = child; |
Stefan Hajnoczi | 39f72ef | 2014-03-19 08:58:56 +0100 | [diff] [blame] | 1236 | prop->check = check; |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1237 | prop->flags = flags; |
| 1238 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1239 | full_type = g_strdup_printf("link<%s>", type); |
| 1240 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1241 | op = object_property_add(obj, name, full_type, |
| 1242 | object_get_link_property, |
| 1243 | check ? object_set_link_property : NULL, |
| 1244 | object_release_link_property, |
| 1245 | prop, |
| 1246 | &local_err); |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1247 | if (local_err) { |
| 1248 | error_propagate(errp, local_err); |
| 1249 | g_free(prop); |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1250 | goto out; |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1251 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1252 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1253 | op->resolve = object_resolve_link_property; |
| 1254 | |
| 1255 | out: |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1256 | g_free(full_type); |
| 1257 | } |
| 1258 | |
Stefan Hajnoczi | 11f590b | 2014-03-03 11:30:02 +0100 | [diff] [blame] | 1259 | gchar *object_get_canonical_path_component(Object *obj) |
| 1260 | { |
| 1261 | ObjectProperty *prop = NULL; |
| 1262 | |
| 1263 | g_assert(obj); |
| 1264 | g_assert(obj->parent != NULL); |
| 1265 | |
| 1266 | QTAILQ_FOREACH(prop, &obj->parent->properties, node) { |
| 1267 | if (!object_property_is_child(prop)) { |
| 1268 | continue; |
| 1269 | } |
| 1270 | |
| 1271 | if (prop->opaque == obj) { |
| 1272 | return g_strdup(prop->name); |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | /* obj had a parent but was not a child, should never happen */ |
| 1277 | g_assert_not_reached(); |
| 1278 | return NULL; |
| 1279 | } |
| 1280 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1281 | gchar *object_get_canonical_path(Object *obj) |
| 1282 | { |
| 1283 | Object *root = object_get_root(); |
Stefan Hajnoczi | 11f590b | 2014-03-03 11:30:02 +0100 | [diff] [blame] | 1284 | char *newpath, *path = NULL; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1285 | |
| 1286 | while (obj != root) { |
Stefan Hajnoczi | 11f590b | 2014-03-03 11:30:02 +0100 | [diff] [blame] | 1287 | char *component = object_get_canonical_path_component(obj); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1288 | |
Stefan Hajnoczi | 11f590b | 2014-03-03 11:30:02 +0100 | [diff] [blame] | 1289 | if (path) { |
| 1290 | newpath = g_strdup_printf("%s/%s", component, path); |
| 1291 | g_free(component); |
| 1292 | g_free(path); |
| 1293 | path = newpath; |
| 1294 | } else { |
| 1295 | path = component; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1296 | } |
| 1297 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1298 | obj = obj->parent; |
| 1299 | } |
| 1300 | |
Stefan Hajnoczi | 11f590b | 2014-03-03 11:30:02 +0100 | [diff] [blame] | 1301 | newpath = g_strdup_printf("/%s", path ? path : ""); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1302 | g_free(path); |
| 1303 | |
| 1304 | return newpath; |
| 1305 | } |
| 1306 | |
Andreas Färber | 3e84b48 | 2013-01-15 02:55:10 +0100 | [diff] [blame] | 1307 | Object *object_resolve_path_component(Object *parent, const gchar *part) |
Paolo Bonzini | a612b2a | 2012-03-27 18:38:45 +0200 | [diff] [blame] | 1308 | { |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 1309 | ObjectProperty *prop = object_property_find(parent, part, NULL); |
Paolo Bonzini | a612b2a | 2012-03-27 18:38:45 +0200 | [diff] [blame] | 1310 | if (prop == NULL) { |
| 1311 | return NULL; |
| 1312 | } |
| 1313 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1314 | if (prop->resolve) { |
| 1315 | return prop->resolve(parent, prop->opaque, part); |
Paolo Bonzini | a612b2a | 2012-03-27 18:38:45 +0200 | [diff] [blame] | 1316 | } else { |
| 1317 | return NULL; |
| 1318 | } |
| 1319 | } |
| 1320 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1321 | static Object *object_resolve_abs_path(Object *parent, |
| 1322 | gchar **parts, |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1323 | const char *typename, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1324 | int index) |
| 1325 | { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1326 | Object *child; |
| 1327 | |
| 1328 | if (parts[index] == NULL) { |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1329 | return object_dynamic_cast(parent, typename); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | if (strcmp(parts[index], "") == 0) { |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1333 | return object_resolve_abs_path(parent, parts, typename, index + 1); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1334 | } |
| 1335 | |
Paolo Bonzini | a612b2a | 2012-03-27 18:38:45 +0200 | [diff] [blame] | 1336 | child = object_resolve_path_component(parent, parts[index]); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1337 | if (!child) { |
| 1338 | return NULL; |
| 1339 | } |
| 1340 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1341 | return object_resolve_abs_path(child, parts, typename, index + 1); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | static Object *object_resolve_partial_path(Object *parent, |
| 1345 | gchar **parts, |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1346 | const char *typename, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1347 | bool *ambiguous) |
| 1348 | { |
| 1349 | Object *obj; |
| 1350 | ObjectProperty *prop; |
| 1351 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1352 | obj = object_resolve_abs_path(parent, parts, typename, 0); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1353 | |
| 1354 | QTAILQ_FOREACH(prop, &parent->properties, node) { |
| 1355 | Object *found; |
| 1356 | |
Andreas Färber | 5d9d3f4 | 2012-05-27 00:32:40 +0200 | [diff] [blame] | 1357 | if (!object_property_is_child(prop)) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1358 | continue; |
| 1359 | } |
| 1360 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1361 | found = object_resolve_partial_path(prop->opaque, parts, |
| 1362 | typename, ambiguous); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1363 | if (found) { |
| 1364 | if (obj) { |
| 1365 | if (ambiguous) { |
| 1366 | *ambiguous = true; |
| 1367 | } |
| 1368 | return NULL; |
| 1369 | } |
| 1370 | obj = found; |
| 1371 | } |
| 1372 | |
| 1373 | if (ambiguous && *ambiguous) { |
| 1374 | return NULL; |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | return obj; |
| 1379 | } |
| 1380 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1381 | Object *object_resolve_path_type(const char *path, const char *typename, |
| 1382 | bool *ambiguous) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1383 | { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1384 | Object *obj; |
| 1385 | gchar **parts; |
| 1386 | |
| 1387 | parts = g_strsplit(path, "/", 0); |
Paolo Bonzini | 2e1103f | 2013-04-18 18:44:02 +0200 | [diff] [blame] | 1388 | assert(parts); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1389 | |
Paolo Bonzini | 2e1103f | 2013-04-18 18:44:02 +0200 | [diff] [blame] | 1390 | if (parts[0] == NULL || strcmp(parts[0], "") != 0) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1391 | if (ambiguous) { |
| 1392 | *ambiguous = false; |
| 1393 | } |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1394 | obj = object_resolve_partial_path(object_get_root(), parts, |
| 1395 | typename, ambiguous); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1396 | } else { |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1397 | obj = object_resolve_abs_path(object_get_root(), parts, typename, 1); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | g_strfreev(parts); |
| 1401 | |
| 1402 | return obj; |
| 1403 | } |
| 1404 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1405 | Object *object_resolve_path(const char *path, bool *ambiguous) |
| 1406 | { |
| 1407 | return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); |
| 1408 | } |
| 1409 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1410 | typedef struct StringProperty |
| 1411 | { |
| 1412 | char *(*get)(Object *, Error **); |
| 1413 | void (*set)(Object *, const char *, Error **); |
| 1414 | } StringProperty; |
| 1415 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1416 | static void property_get_str(Object *obj, Visitor *v, void *opaque, |
| 1417 | const char *name, Error **errp) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1418 | { |
| 1419 | StringProperty *prop = opaque; |
| 1420 | char *value; |
| 1421 | |
| 1422 | value = prop->get(obj, errp); |
| 1423 | if (value) { |
| 1424 | visit_type_str(v, &value, name, errp); |
| 1425 | g_free(value); |
| 1426 | } |
| 1427 | } |
| 1428 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1429 | static void property_set_str(Object *obj, Visitor *v, void *opaque, |
| 1430 | const char *name, Error **errp) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1431 | { |
| 1432 | StringProperty *prop = opaque; |
| 1433 | char *value; |
| 1434 | Error *local_err = NULL; |
| 1435 | |
| 1436 | visit_type_str(v, &value, name, &local_err); |
| 1437 | if (local_err) { |
| 1438 | error_propagate(errp, local_err); |
| 1439 | return; |
| 1440 | } |
| 1441 | |
| 1442 | prop->set(obj, value, errp); |
| 1443 | g_free(value); |
| 1444 | } |
| 1445 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1446 | static void property_release_str(Object *obj, const char *name, |
| 1447 | void *opaque) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1448 | { |
| 1449 | StringProperty *prop = opaque; |
| 1450 | g_free(prop); |
| 1451 | } |
| 1452 | |
| 1453 | void object_property_add_str(Object *obj, const char *name, |
| 1454 | char *(*get)(Object *, Error **), |
| 1455 | void (*set)(Object *, const char *, Error **), |
| 1456 | Error **errp) |
| 1457 | { |
Stefan Hajnoczi | a01aedc | 2014-03-04 15:28:18 +0100 | [diff] [blame] | 1458 | Error *local_err = NULL; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1459 | StringProperty *prop = g_malloc0(sizeof(*prop)); |
| 1460 | |
| 1461 | prop->get = get; |
| 1462 | prop->set = set; |
| 1463 | |
| 1464 | object_property_add(obj, name, "string", |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1465 | get ? property_get_str : NULL, |
| 1466 | set ? property_set_str : NULL, |
| 1467 | property_release_str, |
Stefan Hajnoczi | a01aedc | 2014-03-04 15:28:18 +0100 | [diff] [blame] | 1468 | prop, &local_err); |
| 1469 | if (local_err) { |
| 1470 | error_propagate(errp, local_err); |
| 1471 | g_free(prop); |
| 1472 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1473 | } |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 1474 | |
Anthony Liguori | 0e55884 | 2012-06-25 10:32:46 -0500 | [diff] [blame] | 1475 | typedef struct BoolProperty |
| 1476 | { |
| 1477 | bool (*get)(Object *, Error **); |
| 1478 | void (*set)(Object *, bool, Error **); |
| 1479 | } BoolProperty; |
| 1480 | |
| 1481 | static void property_get_bool(Object *obj, Visitor *v, void *opaque, |
| 1482 | const char *name, Error **errp) |
| 1483 | { |
| 1484 | BoolProperty *prop = opaque; |
| 1485 | bool value; |
| 1486 | |
| 1487 | value = prop->get(obj, errp); |
| 1488 | visit_type_bool(v, &value, name, errp); |
| 1489 | } |
| 1490 | |
| 1491 | static void property_set_bool(Object *obj, Visitor *v, void *opaque, |
| 1492 | const char *name, Error **errp) |
| 1493 | { |
| 1494 | BoolProperty *prop = opaque; |
| 1495 | bool value; |
| 1496 | Error *local_err = NULL; |
| 1497 | |
| 1498 | visit_type_bool(v, &value, name, &local_err); |
| 1499 | if (local_err) { |
| 1500 | error_propagate(errp, local_err); |
| 1501 | return; |
| 1502 | } |
| 1503 | |
| 1504 | prop->set(obj, value, errp); |
| 1505 | } |
| 1506 | |
| 1507 | static void property_release_bool(Object *obj, const char *name, |
| 1508 | void *opaque) |
| 1509 | { |
| 1510 | BoolProperty *prop = opaque; |
| 1511 | g_free(prop); |
| 1512 | } |
| 1513 | |
| 1514 | void object_property_add_bool(Object *obj, const char *name, |
| 1515 | bool (*get)(Object *, Error **), |
| 1516 | void (*set)(Object *, bool, Error **), |
| 1517 | Error **errp) |
| 1518 | { |
Stefan Hajnoczi | a01aedc | 2014-03-04 15:28:18 +0100 | [diff] [blame] | 1519 | Error *local_err = NULL; |
Anthony Liguori | 0e55884 | 2012-06-25 10:32:46 -0500 | [diff] [blame] | 1520 | BoolProperty *prop = g_malloc0(sizeof(*prop)); |
| 1521 | |
| 1522 | prop->get = get; |
| 1523 | prop->set = set; |
| 1524 | |
| 1525 | object_property_add(obj, name, "bool", |
| 1526 | get ? property_get_bool : NULL, |
| 1527 | set ? property_set_bool : NULL, |
| 1528 | property_release_bool, |
Stefan Hajnoczi | a01aedc | 2014-03-04 15:28:18 +0100 | [diff] [blame] | 1529 | prop, &local_err); |
| 1530 | if (local_err) { |
| 1531 | error_propagate(errp, local_err); |
| 1532 | g_free(prop); |
| 1533 | } |
Anthony Liguori | 0e55884 | 2012-06-25 10:32:46 -0500 | [diff] [blame] | 1534 | } |
| 1535 | |
Paolo Bonzini | 2f262e0 | 2012-04-02 17:33:51 +0200 | [diff] [blame] | 1536 | static char *qdev_get_type(Object *obj, Error **errp) |
| 1537 | { |
| 1538 | return g_strdup(object_get_typename(obj)); |
| 1539 | } |
| 1540 | |
Michael S. Tsirkin | e732ea6 | 2013-09-22 10:10:17 +0300 | [diff] [blame] | 1541 | static void property_get_uint8_ptr(Object *obj, Visitor *v, |
| 1542 | void *opaque, const char *name, |
| 1543 | Error **errp) |
| 1544 | { |
| 1545 | uint8_t value = *(uint8_t *)opaque; |
| 1546 | visit_type_uint8(v, &value, name, errp); |
| 1547 | } |
| 1548 | |
| 1549 | static void property_get_uint16_ptr(Object *obj, Visitor *v, |
| 1550 | void *opaque, const char *name, |
| 1551 | Error **errp) |
| 1552 | { |
| 1553 | uint16_t value = *(uint16_t *)opaque; |
| 1554 | visit_type_uint16(v, &value, name, errp); |
| 1555 | } |
| 1556 | |
| 1557 | static void property_get_uint32_ptr(Object *obj, Visitor *v, |
| 1558 | void *opaque, const char *name, |
| 1559 | Error **errp) |
| 1560 | { |
| 1561 | uint32_t value = *(uint32_t *)opaque; |
| 1562 | visit_type_uint32(v, &value, name, errp); |
| 1563 | } |
| 1564 | |
| 1565 | static void property_get_uint64_ptr(Object *obj, Visitor *v, |
| 1566 | void *opaque, const char *name, |
| 1567 | Error **errp) |
| 1568 | { |
| 1569 | uint64_t value = *(uint64_t *)opaque; |
| 1570 | visit_type_uint64(v, &value, name, errp); |
| 1571 | } |
| 1572 | |
| 1573 | void object_property_add_uint8_ptr(Object *obj, const char *name, |
| 1574 | const uint8_t *v, Error **errp) |
| 1575 | { |
| 1576 | object_property_add(obj, name, "uint8", property_get_uint8_ptr, |
| 1577 | NULL, NULL, (void *)v, errp); |
| 1578 | } |
| 1579 | |
| 1580 | void object_property_add_uint16_ptr(Object *obj, const char *name, |
| 1581 | const uint16_t *v, Error **errp) |
| 1582 | { |
| 1583 | object_property_add(obj, name, "uint16", property_get_uint16_ptr, |
| 1584 | NULL, NULL, (void *)v, errp); |
| 1585 | } |
| 1586 | |
| 1587 | void object_property_add_uint32_ptr(Object *obj, const char *name, |
| 1588 | const uint32_t *v, Error **errp) |
| 1589 | { |
| 1590 | object_property_add(obj, name, "uint32", property_get_uint32_ptr, |
| 1591 | NULL, NULL, (void *)v, errp); |
| 1592 | } |
| 1593 | |
| 1594 | void object_property_add_uint64_ptr(Object *obj, const char *name, |
| 1595 | const uint64_t *v, Error **errp) |
| 1596 | { |
| 1597 | object_property_add(obj, name, "uint64", property_get_uint64_ptr, |
| 1598 | NULL, NULL, (void *)v, errp); |
| 1599 | } |
| 1600 | |
Stefan Hajnoczi | ef7c7ff | 2014-06-18 17:58:28 +0800 | [diff] [blame] | 1601 | typedef struct { |
| 1602 | Object *target_obj; |
| 1603 | const char *target_name; |
| 1604 | } AliasProperty; |
| 1605 | |
| 1606 | static void property_get_alias(Object *obj, struct Visitor *v, void *opaque, |
| 1607 | const char *name, Error **errp) |
| 1608 | { |
| 1609 | AliasProperty *prop = opaque; |
| 1610 | |
| 1611 | object_property_get(prop->target_obj, v, prop->target_name, errp); |
| 1612 | } |
| 1613 | |
| 1614 | static void property_set_alias(Object *obj, struct Visitor *v, void *opaque, |
| 1615 | const char *name, Error **errp) |
| 1616 | { |
| 1617 | AliasProperty *prop = opaque; |
| 1618 | |
| 1619 | object_property_set(prop->target_obj, v, prop->target_name, errp); |
| 1620 | } |
| 1621 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1622 | static Object *property_resolve_alias(Object *obj, void *opaque, |
| 1623 | const gchar *part) |
| 1624 | { |
| 1625 | AliasProperty *prop = opaque; |
| 1626 | |
| 1627 | return object_resolve_path_component(prop->target_obj, prop->target_name); |
| 1628 | } |
| 1629 | |
Stefan Hajnoczi | ef7c7ff | 2014-06-18 17:58:28 +0800 | [diff] [blame] | 1630 | static void property_release_alias(Object *obj, const char *name, void *opaque) |
| 1631 | { |
| 1632 | AliasProperty *prop = opaque; |
| 1633 | |
| 1634 | g_free(prop); |
| 1635 | } |
| 1636 | |
| 1637 | void object_property_add_alias(Object *obj, const char *name, |
| 1638 | Object *target_obj, const char *target_name, |
| 1639 | Error **errp) |
| 1640 | { |
| 1641 | AliasProperty *prop; |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1642 | ObjectProperty *op; |
Stefan Hajnoczi | ef7c7ff | 2014-06-18 17:58:28 +0800 | [diff] [blame] | 1643 | ObjectProperty *target_prop; |
Paolo Bonzini | d190698 | 2014-06-10 11:17:35 +0200 | [diff] [blame] | 1644 | gchar *prop_type; |
Gonglei | 8ae9a9e | 2014-09-27 13:13:56 +0800 | [diff] [blame^] | 1645 | Error *local_err = NULL; |
Stefan Hajnoczi | ef7c7ff | 2014-06-18 17:58:28 +0800 | [diff] [blame] | 1646 | |
| 1647 | target_prop = object_property_find(target_obj, target_name, errp); |
| 1648 | if (!target_prop) { |
| 1649 | return; |
| 1650 | } |
| 1651 | |
Paolo Bonzini | d190698 | 2014-06-10 11:17:35 +0200 | [diff] [blame] | 1652 | if (object_property_is_child(target_prop)) { |
| 1653 | prop_type = g_strdup_printf("link%s", |
| 1654 | target_prop->type + strlen("child")); |
| 1655 | } else { |
| 1656 | prop_type = g_strdup(target_prop->type); |
| 1657 | } |
| 1658 | |
Stefan Hajnoczi | ef7c7ff | 2014-06-18 17:58:28 +0800 | [diff] [blame] | 1659 | prop = g_malloc(sizeof(*prop)); |
| 1660 | prop->target_obj = target_obj; |
| 1661 | prop->target_name = target_name; |
| 1662 | |
Paolo Bonzini | d190698 | 2014-06-10 11:17:35 +0200 | [diff] [blame] | 1663 | op = object_property_add(obj, name, prop_type, |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1664 | property_get_alias, |
| 1665 | property_set_alias, |
| 1666 | property_release_alias, |
Gonglei | 8ae9a9e | 2014-09-27 13:13:56 +0800 | [diff] [blame^] | 1667 | prop, &local_err); |
| 1668 | if (local_err) { |
| 1669 | error_propagate(errp, local_err); |
| 1670 | g_free(prop); |
| 1671 | goto out; |
| 1672 | } |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1673 | op->resolve = property_resolve_alias; |
Paolo Bonzini | d190698 | 2014-06-10 11:17:35 +0200 | [diff] [blame] | 1674 | |
Gonglei | 8ae9a9e | 2014-09-27 13:13:56 +0800 | [diff] [blame^] | 1675 | out: |
Paolo Bonzini | d190698 | 2014-06-10 11:17:35 +0200 | [diff] [blame] | 1676 | g_free(prop_type); |
Stefan Hajnoczi | ef7c7ff | 2014-06-18 17:58:28 +0800 | [diff] [blame] | 1677 | } |
| 1678 | |
Paolo Bonzini | 2f262e0 | 2012-04-02 17:33:51 +0200 | [diff] [blame] | 1679 | static void object_instance_init(Object *obj) |
| 1680 | { |
| 1681 | object_property_add_str(obj, "type", qdev_get_type, NULL, NULL); |
| 1682 | } |
| 1683 | |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 1684 | static void register_types(void) |
| 1685 | { |
| 1686 | static TypeInfo interface_info = { |
| 1687 | .name = TYPE_INTERFACE, |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 1688 | .class_size = sizeof(InterfaceClass), |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 1689 | .abstract = true, |
| 1690 | }; |
| 1691 | |
| 1692 | static TypeInfo object_info = { |
| 1693 | .name = TYPE_OBJECT, |
| 1694 | .instance_size = sizeof(Object), |
Paolo Bonzini | 2f262e0 | 2012-04-02 17:33:51 +0200 | [diff] [blame] | 1695 | .instance_init = object_instance_init, |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 1696 | .abstract = true, |
| 1697 | }; |
| 1698 | |
Paolo Bonzini | 049cb3c | 2012-04-04 15:58:40 +0200 | [diff] [blame] | 1699 | type_interface = type_register_internal(&interface_info); |
| 1700 | type_register_internal(&object_info); |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | type_init(register_types) |