-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
object.c
2893 lines (2370 loc) · 75.5 KB
/
object.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* QEMU Object Model
*
* Copyright IBM, Corp. 2011
*
* Authors:
* Anthony Liguori <aliguori@us.ibm.com>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "hw/qdev-core.h"
#include "qapi/error.h"
#include "qom/object.h"
#include "qom/object_interfaces.h"
#include "qemu/cutils.h"
#include "qemu/memalign.h"
#include "qapi/visitor.h"
#include "qapi/string-input-visitor.h"
#include "qapi/string-output-visitor.h"
#include "qapi/qobject-input-visitor.h"
#include "qapi/forward-visitor.h"
#include "qapi/qapi-builtin-visit.h"
#include "qapi/qmp/qjson.h"
#include "trace.h"
/* TODO: replace QObject with a simpler visitor to avoid a dependency
* of the QOM core on QObject? */
#include "qom/qom-qobject.h"
#include "qapi/qmp/qbool.h"
#include "qapi/qmp/qlist.h"
#include "qapi/qmp/qnum.h"
#include "qapi/qmp/qstring.h"
#include "qemu/error-report.h"
#define MAX_INTERFACES 32
typedef struct InterfaceImpl InterfaceImpl;
typedef struct TypeImpl TypeImpl;
struct InterfaceImpl
{
const char *typename;
};
struct TypeImpl
{
const char *name;
size_t class_size;
size_t instance_size;
size_t instance_align;
void (*class_init)(ObjectClass *klass, void *data);
void (*class_base_init)(ObjectClass *klass, void *data);
void *class_data;
void (*instance_init)(Object *obj);
void (*instance_post_init)(Object *obj);
void (*instance_finalize)(Object *obj);
bool abstract;
const char *parent;
TypeImpl *parent_type;
ObjectClass *class;
int num_interfaces;
InterfaceImpl interfaces[MAX_INTERFACES];
};
static Type type_interface;
static GHashTable *type_table_get(void)
{
static GHashTable *type_table;
if (type_table == NULL) {
type_table = g_hash_table_new(g_str_hash, g_str_equal);
}
return type_table;
}
static bool enumerating_types;
static void type_table_add(TypeImpl *ti)
{
assert(!enumerating_types);
g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
}
static TypeImpl *type_table_lookup(const char *name)
{
return g_hash_table_lookup(type_table_get(), name);
}
static TypeImpl *type_new(const TypeInfo *info)
{
TypeImpl *ti = g_malloc0(sizeof(*ti));
int i;
g_assert(info->name != NULL);
if (type_table_lookup(info->name) != NULL) {
fprintf(stderr, "Registering `%s' which already exists\n", info->name);
abort();
}
ti->name = g_strdup(info->name);
ti->parent = g_strdup(info->parent);
ti->class_size = info->class_size;
ti->instance_size = info->instance_size;
ti->instance_align = info->instance_align;
ti->class_init = info->class_init;
ti->class_base_init = info->class_base_init;
ti->class_data = info->class_data;
ti->instance_init = info->instance_init;
ti->instance_post_init = info->instance_post_init;
ti->instance_finalize = info->instance_finalize;
ti->abstract = info->abstract;
for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
}
ti->num_interfaces = i;
return ti;
}
static bool type_name_is_valid(const char *name)
{
const int slen = strlen(name);
int plen;
g_assert(slen > 1);
/*
* Ideally, the name should start with a letter - however, we've got
* too many names starting with a digit already, so allow digits here,
* too (except '0' which is not used yet)
*/
if (!g_ascii_isalnum(name[0]) || name[0] == '0') {
return false;
}
plen = strspn(name, "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789-_.");
return plen == slen;
}
static TypeImpl *type_register_internal(const TypeInfo *info)
{
TypeImpl *ti;
if (!type_name_is_valid(info->name)) {
fprintf(stderr, "Registering '%s' with illegal type name\n", info->name);
abort();
}
ti = type_new(info);
type_table_add(ti);
return ti;
}
TypeImpl *type_register(const TypeInfo *info)
{
assert(info->parent);
return type_register_internal(info);
}
TypeImpl *type_register_static(const TypeInfo *info)
{
return type_register(info);
}
void type_register_static_array(const TypeInfo *infos, int nr_infos)
{
int i;
for (i = 0; i < nr_infos; i++) {
type_register_static(&infos[i]);
}
}
static TypeImpl *type_get_by_name_noload(const char *name)
{
if (name == NULL) {
return NULL;
}
return type_table_lookup(name);
}
static TypeImpl *type_get_or_load_by_name(const char *name, Error **errp)
{
TypeImpl *type = type_get_by_name_noload(name);
#ifdef CONFIG_MODULES
if (!type) {
int rv = module_load_qom(name, errp);
if (rv > 0) {
type = type_get_by_name_noload(name);
} else {
error_prepend(errp, "could not load a module for type '%s'", name);
return NULL;
}
}
#endif
if (!type) {
error_setg(errp, "unknown type '%s'", name);
}
return type;
}
static TypeImpl *type_get_parent(TypeImpl *type)
{
if (!type->parent_type && type->parent) {
type->parent_type = type_get_by_name_noload(type->parent);
if (!type->parent_type) {
fprintf(stderr, "Type '%s' is missing its parent '%s'\n",
type->name, type->parent);
abort();
}
}
return type->parent_type;
}
static bool type_has_parent(TypeImpl *type)
{
return (type->parent != NULL);
}
static size_t type_class_get_size(TypeImpl *ti)
{
if (ti->class_size) {
return ti->class_size;
}
if (type_has_parent(ti)) {
return type_class_get_size(type_get_parent(ti));
}
return sizeof(ObjectClass);
}
static size_t type_object_get_size(TypeImpl *ti)
{
if (ti->instance_size) {
return ti->instance_size;
}
if (type_has_parent(ti)) {
return type_object_get_size(type_get_parent(ti));
}
return 0;
}
static size_t type_object_get_align(TypeImpl *ti)
{
if (ti->instance_align) {
return ti->instance_align;
}
if (type_has_parent(ti)) {
return type_object_get_align(type_get_parent(ti));
}
return 0;
}
static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
{
assert(target_type);
/* Check if target_type is a direct ancestor of type */
while (type) {
if (type == target_type) {
return true;
}
type = type_get_parent(type);
}
return false;
}
static void type_initialize(TypeImpl *ti);
static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
TypeImpl *parent_type)
{
InterfaceClass *new_iface;
TypeInfo info = { };
TypeImpl *iface_impl;
info.parent = parent_type->name;
info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
info.abstract = true;
iface_impl = type_new(&info);
iface_impl->parent_type = parent_type;
type_initialize(iface_impl);
g_free((char *)info.name);
new_iface = (InterfaceClass *)iface_impl->class;
new_iface->concrete_class = ti->class;
new_iface->interface_type = interface_type;
ti->class->interfaces = g_slist_append(ti->class->interfaces, new_iface);
}
static void object_property_free(gpointer data)
{
ObjectProperty *prop = data;
if (prop->defval) {
qobject_unref(prop->defval);
prop->defval = NULL;
}
g_free(prop->name);
g_free(prop->type);
g_free(prop->description);
g_free(prop);
}
static void type_initialize(TypeImpl *ti)
{
TypeImpl *parent;
if (ti->class) {
return;
}
ti->class_size = type_class_get_size(ti);
ti->instance_size = type_object_get_size(ti);
ti->instance_align = type_object_get_align(ti);
/* Any type with zero instance_size is implicitly abstract.
* This means interface types are all abstract.
*/
if (ti->instance_size == 0) {
ti->abstract = true;
}
if (type_is_ancestor(ti, type_interface)) {
assert(ti->instance_size == 0);
assert(ti->abstract);
assert(!ti->instance_init);
assert(!ti->instance_post_init);
assert(!ti->instance_finalize);
assert(!ti->num_interfaces);
}
ti->class = g_malloc0(ti->class_size);
parent = type_get_parent(ti);
if (parent) {
type_initialize(parent);
GSList *e;
int i;
g_assert(parent->class_size <= ti->class_size);
g_assert(parent->instance_size <= ti->instance_size);
memcpy(ti->class, parent->class, parent->class_size);
ti->class->interfaces = NULL;
for (e = parent->class->interfaces; e; e = e->next) {
InterfaceClass *iface = e->data;
ObjectClass *klass = OBJECT_CLASS(iface);
type_initialize_interface(ti, iface->interface_type, klass->type);
}
for (i = 0; i < ti->num_interfaces; i++) {
TypeImpl *t = type_get_by_name_noload(ti->interfaces[i].typename);
if (!t) {
error_report("missing interface '%s' for object '%s'",
ti->interfaces[i].typename, parent->name);
abort();
}
for (e = ti->class->interfaces; e; e = e->next) {
TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
if (type_is_ancestor(target_type, t)) {
break;
}
}
if (e) {
continue;
}
type_initialize_interface(ti, t, t);
}
}
ti->class->properties = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
object_property_free);
ti->class->type = ti;
while (parent) {
if (parent->class_base_init) {
parent->class_base_init(ti->class, ti->class_data);
}
parent = type_get_parent(parent);
}
if (ti->class_init) {
ti->class_init(ti->class, ti->class_data);
}
}
static void object_init_with_type(Object *obj, TypeImpl *ti)
{
if (type_has_parent(ti)) {
object_init_with_type(obj, type_get_parent(ti));
}
if (ti->instance_init) {
ti->instance_init(obj);
}
}
static void object_post_init_with_type(Object *obj, TypeImpl *ti)
{
if (ti->instance_post_init) {
ti->instance_post_init(obj);
}
if (type_has_parent(ti)) {
object_post_init_with_type(obj, type_get_parent(ti));
}
}
bool object_apply_global_props(Object *obj, const GPtrArray *props,
Error **errp)
{
int i;
if (!props) {
return true;
}
for (i = 0; i < props->len; i++) {
GlobalProperty *p = g_ptr_array_index(props, i);
Error *err = NULL;
if (object_dynamic_cast(obj, p->driver) == NULL) {
continue;
}
if (p->optional && !object_property_find(obj, p->property)) {
continue;
}
p->used = true;
if (!object_property_parse(obj, p->property, p->value, &err)) {
error_prepend(&err, "can't apply global %s.%s=%s: ",
p->driver, p->property, p->value);
/*
* If errp != NULL, propagate error and return.
* If errp == NULL, report a warning, but keep going
* with the remaining globals.
*/
if (errp) {
error_propagate(errp, err);
return false;
} else {
warn_report_err(err);
}
}
}
return true;
}
/*
* Global property defaults
* Slot 0: accelerator's global property defaults
* Slot 1: machine's global property defaults
* Slot 2: global properties from legacy command line option
* Each is a GPtrArray of of GlobalProperty.
* Applied in order, later entries override earlier ones.
*/
static GPtrArray *object_compat_props[3];
/*
* Retrieve @GPtrArray for global property defined with options
* other than "-global". These are generally used for syntactic
* sugar and legacy command line options.
*/
void object_register_sugar_prop(const char *driver, const char *prop,
const char *value, bool optional)
{
GlobalProperty *g;
if (!object_compat_props[2]) {
object_compat_props[2] = g_ptr_array_new();
}
g = g_new0(GlobalProperty, 1);
g->driver = g_strdup(driver);
g->property = g_strdup(prop);
g->value = g_strdup(value);
g->optional = optional;
g_ptr_array_add(object_compat_props[2], g);
}
/*
* Set machine's global property defaults to @compat_props.
* May be called at most once.
*/
void object_set_machine_compat_props(GPtrArray *compat_props)
{
assert(!object_compat_props[1]);
object_compat_props[1] = compat_props;
}
/*
* Set accelerator's global property defaults to @compat_props.
* May be called at most once.
*/
void object_set_accelerator_compat_props(GPtrArray *compat_props)
{
assert(!object_compat_props[0]);
object_compat_props[0] = compat_props;
}
void object_apply_compat_props(Object *obj)
{
int i;
for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
object_apply_global_props(obj, object_compat_props[i],
i == 2 ? &error_fatal : &error_abort);
}
}
static void object_class_property_init_all(Object *obj)
{
ObjectPropertyIterator iter;
ObjectProperty *prop;
object_class_property_iter_init(&iter, object_get_class(obj));
while ((prop = object_property_iter_next(&iter))) {
if (prop->init) {
prop->init(obj, prop);
}
}
}
static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type)
{
type_initialize(type);
g_assert(type->instance_size >= sizeof(Object));
g_assert(type->abstract == false);
g_assert(size >= type->instance_size);
memset(obj, 0, type->instance_size);
obj->class = type->class;
object_ref(obj);
object_class_property_init_all(obj);
obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
NULL, object_property_free);
object_init_with_type(obj, type);
object_post_init_with_type(obj, type);
}
void object_initialize(void *data, size_t size, const char *typename)
{
TypeImpl *type = type_get_or_load_by_name(typename, &error_fatal);
object_initialize_with_type(data, size, type);
}
bool object_initialize_child_with_props(Object *parentobj,
const char *propname,
void *childobj, size_t size,
const char *type,
Error **errp, ...)
{
va_list vargs;
bool ok;
va_start(vargs, errp);
ok = object_initialize_child_with_propsv(parentobj, propname,
childobj, size, type, errp,
vargs);
va_end(vargs);
return ok;
}
bool object_initialize_child_with_propsv(Object *parentobj,
const char *propname,
void *childobj, size_t size,
const char *type,
Error **errp, va_list vargs)
{
bool ok = false;
Object *obj;
UserCreatable *uc;
object_initialize(childobj, size, type);
obj = OBJECT(childobj);
if (!object_set_propv(obj, errp, vargs)) {
goto out;
}
object_property_add_child(parentobj, propname, obj);
uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
if (uc) {
if (!user_creatable_complete(uc, errp)) {
object_unparent(obj);
goto out;
}
}
ok = true;
out:
/*
* We want @obj's reference to be 1 on success, 0 on failure.
* On success, it's 2: one taken by object_initialize(), and one
* by object_property_add_child().
* On failure in object_initialize() or earlier, it's 1.
* On failure afterwards, it's also 1: object_unparent() releases
* the reference taken by object_property_add_child().
*/
object_unref(obj);
return ok;
}
void object_initialize_child_internal(Object *parent,
const char *propname,
void *child, size_t size,
const char *type)
{
object_initialize_child_with_props(parent, propname, child, size, type,
&error_abort, NULL);
}
static inline bool object_property_is_child(ObjectProperty *prop)
{
return strstart(prop->type, "child<", NULL);
}
static void object_property_del_all(Object *obj)
{
g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL);
ObjectProperty *prop;
ObjectPropertyIterator iter;
bool released;
do {
released = false;
object_property_iter_init(&iter, obj);
while ((prop = object_property_iter_next(&iter)) != NULL) {
if (g_hash_table_add(done, prop)) {
if (prop->release) {
prop->release(obj, prop->name, prop->opaque);
released = true;
break;
}
}
}
} while (released);
g_hash_table_unref(obj->properties);
}
static void object_property_del_child(Object *obj, Object *child)
{
ObjectProperty *prop;
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init(&iter, obj->properties);
while (g_hash_table_iter_next(&iter, &key, &value)) {
prop = value;
if (object_property_is_child(prop) && prop->opaque == child) {
if (prop->release) {
prop->release(obj, prop->name, prop->opaque);
prop->release = NULL;
}
break;
}
}
g_hash_table_iter_init(&iter, obj->properties);
while (g_hash_table_iter_next(&iter, &key, &value)) {
prop = value;
if (object_property_is_child(prop) && prop->opaque == child) {
g_hash_table_iter_remove(&iter);
break;
}
}
}
void object_unparent(Object *obj)
{
if (obj->parent) {
object_property_del_child(obj->parent, obj);
}
}
static void object_deinit(Object *obj, TypeImpl *type)
{
if (type->instance_finalize) {
type->instance_finalize(obj);
}
if (type_has_parent(type)) {
object_deinit(obj, type_get_parent(type));
}
}
static void object_finalize(void *data)
{
Object *obj = data;
TypeImpl *ti = obj->class->type;
object_property_del_all(obj);
object_deinit(obj, ti);
g_assert(obj->ref == 0);
g_assert(obj->parent == NULL);
if (obj->free) {
obj->free(obj);
}
}
/* Find the minimum alignment guaranteed by the system malloc. */
#if __STDC_VERSION__ >= 201112L
typedef max_align_t qemu_max_align_t;
#else
typedef union {
long l;
void *p;
double d;
long double ld;
} qemu_max_align_t;
#endif
static Object *object_new_with_type(Type type)
{
Object *obj;
size_t size, align;
void (*obj_free)(void *);
g_assert(type != NULL);
type_initialize(type);
size = type->instance_size;
align = type->instance_align;
/*
* Do not use qemu_memalign unless required. Depending on the
* implementation, extra alignment implies extra overhead.
*/
if (likely(align <= __alignof__(qemu_max_align_t))) {
obj = g_malloc(size);
obj_free = g_free;
} else {
obj = qemu_memalign(align, size);
obj_free = qemu_vfree;
}
object_initialize_with_type(obj, size, type);
obj->free = obj_free;
return obj;
}
Object *object_new_with_class(ObjectClass *klass)
{
return object_new_with_type(klass->type);
}
Object *object_new(const char *typename)
{
TypeImpl *ti = type_get_or_load_by_name(typename, &error_fatal);
return object_new_with_type(ti);
}
Object *object_new_with_props(const char *typename,
Object *parent,
const char *id,
Error **errp,
...)
{
va_list vargs;
Object *obj;
va_start(vargs, errp);
obj = object_new_with_propv(typename, parent, id, errp, vargs);
va_end(vargs);
return obj;
}
Object *object_new_with_propv(const char *typename,
Object *parent,
const char *id,
Error **errp,
va_list vargs)
{
Object *obj;
ObjectClass *klass;
UserCreatable *uc;
klass = object_class_by_name(typename);
if (!klass) {
error_setg(errp, "invalid object type: %s", typename);
return NULL;
}
if (object_class_is_abstract(klass)) {
error_setg(errp, "object type '%s' is abstract", typename);
return NULL;
}
obj = object_new_with_type(klass->type);
if (!object_set_propv(obj, errp, vargs)) {
goto error;
}
if (id != NULL) {
object_property_add_child(parent, id, obj);
}
uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
if (uc) {
if (!user_creatable_complete(uc, errp)) {
if (id != NULL) {
object_unparent(obj);
}
goto error;
}
}
object_unref(obj);
return obj;
error:
object_unref(obj);
return NULL;
}
bool object_set_props(Object *obj,
Error **errp,
...)
{
va_list vargs;
bool ret;
va_start(vargs, errp);
ret = object_set_propv(obj, errp, vargs);
va_end(vargs);
return ret;
}
bool object_set_propv(Object *obj,
Error **errp,
va_list vargs)
{
const char *propname;
propname = va_arg(vargs, char *);
while (propname != NULL) {
const char *value = va_arg(vargs, char *);
g_assert(value != NULL);
if (!object_property_parse(obj, propname, value, errp)) {
return false;
}
propname = va_arg(vargs, char *);
}
return true;
}
Object *object_dynamic_cast(Object *obj, const char *typename)
{
if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
return obj;
}
return NULL;
}
Object *object_dynamic_cast_assert(Object *obj, const char *typename,
const char *file, int line, const char *func)
{
trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
typename, file, line, func);
#ifdef CONFIG_QOM_CAST_DEBUG
int i;
Object *inst;
for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
if (qatomic_read(&obj->class->object_cast_cache[i]) == typename) {
goto out;
}
}
inst = object_dynamic_cast(obj, typename);
if (!inst && obj) {
fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
file, line, func, obj, typename);
abort();
}
assert(obj == inst);
if (obj && obj == inst) {
for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
qatomic_set(&obj->class->object_cast_cache[i - 1],
qatomic_read(&obj->class->object_cast_cache[i]));
}
qatomic_set(&obj->class->object_cast_cache[i - 1], typename);
}
out:
#endif
return obj;
}
ObjectClass *object_class_dynamic_cast(ObjectClass *class,
const char *typename)
{
ObjectClass *ret = NULL;
TypeImpl *target_type;
TypeImpl *type;
if (!class) {
return NULL;
}
/* A simple fast path that can trigger a lot for leaf classes. */
type = class->type;
if (type->name == typename) {
return class;
}
target_type = type_get_by_name_noload(typename);
if (!target_type) {
/* target class type unknown, so fail the cast */
return NULL;
}
if (type->class->interfaces &&
type_is_ancestor(target_type, type_interface)) {
int found = 0;
GSList *i;
for (i = class->interfaces; i; i = i->next) {
ObjectClass *target_class = i->data;
if (type_is_ancestor(target_class->type, target_type)) {
ret = target_class;
found++;
}
}
/* The match was ambiguous, don't allow a cast */
if (found > 1) {
ret = NULL;
}
} else if (type_is_ancestor(type, target_type)) {
ret = class;
}
return ret;
}
ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
const char *typename,
const char *file, int line,
const char *func)