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
|
/*
* 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/object.h"
#include "qemu-common.h"
#define MAX_INTERFACES 32
typedef struct InterfaceImpl InterfaceImpl;
typedef struct TypeImpl TypeImpl;
struct InterfaceImpl
{
const char *parent;
void (*interface_initfn)(ObjectClass *class, void *data);
TypeImpl *type;
};
struct TypeImpl
{
const char *name;
size_t class_size;
size_t instance_size;
void (*class_init)(ObjectClass *klass, void *data);
void (*class_finalize)(ObjectClass *klass, void *data);
void *class_data;
void (*instance_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];
};
typedef struct Interface
{
Object parent;
Object *obj;
} Interface;
#define INTERFACE(obj) OBJECT_CHECK(Interface, obj, 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 void type_table_add(TypeImpl *ti)
{
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);
}
TypeImpl *type_register(const TypeInfo *info)
{
TypeImpl *ti = g_malloc0(sizeof(*ti));
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->class_init = info->class_init;
ti->class_finalize = info->class_finalize;
ti->class_data = info->class_data;
ti->instance_init = info->instance_init;
ti->instance_finalize = info->instance_finalize;
ti->abstract = info->abstract;
if (info->interfaces) {
int i;
for (i = 0; info->interfaces[i].type; i++) {
ti->interfaces[i].parent = info->interfaces[i].type;
ti->interfaces[i].interface_initfn = info->interfaces[i].interface_initfn;
ti->num_interfaces++;
}
}
type_table_add(ti);
return ti;
}
TypeImpl *type_register_static(const TypeInfo *info)
{
return type_register(info);
}
static TypeImpl *type_get_by_name(const char *name)
{
if (name == NULL) {
return NULL;
}
return type_table_lookup(name);
}
static TypeImpl *type_get_parent(TypeImpl *type)
{
if (!type->parent_type && type->parent) {
type->parent_type = type_get_by_name(type->parent);
g_assert(type->parent_type != NULL);
}
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 void type_class_interface_init(TypeImpl *ti, InterfaceImpl *iface)
{
TypeInfo info = {
.instance_size = sizeof(Interface),
.parent = iface->parent,
.class_size = sizeof(InterfaceClass),
.class_init = iface->interface_initfn,
.abstract = true,
};
char *name = g_strdup_printf("<%s::%s>", ti->name, iface->parent);
info.name = name;
iface->type = type_register(&info);
g_free(name);
}
static void type_class_init(TypeImpl *ti)
{
size_t class_size = sizeof(ObjectClass);
int i;
if (ti->class) {
return;
}
ti->class_size = type_class_get_size(ti);
ti->class = g_malloc0(ti->class_size);
ti->class->type = ti;
if (type_has_parent(ti)) {
TypeImpl *parent = type_get_parent(ti);
type_class_init(parent);
class_size = parent->class_size;
g_assert(parent->class_size <= ti->class_size);
memcpy((void *)ti->class + sizeof(ObjectClass),
(void *)parent->class + sizeof(ObjectClass),
parent->class_size - sizeof(ObjectClass));
}
memset((void *)ti->class + class_size, 0, ti->class_size - class_size);
for (i = 0; i < ti->num_interfaces; i++) {
type_class_interface_init(ti, &ti->interfaces[i]);
}
if (ti->class_init) {
ti->class_init(ti->class, ti->class_data);
}
}
static void object_interface_init(Object *obj, InterfaceImpl *iface)
{
TypeImpl *ti = iface->type;
Interface *iface_obj;
iface_obj = INTERFACE(object_new(ti->name));
iface_obj->obj = obj;
obj->interfaces = g_slist_prepend(obj->interfaces, iface_obj);
}
static void object_init_with_type(Object *obj, TypeImpl *ti)
{
int i;
if (type_has_parent(ti)) {
object_init_with_type(obj, type_get_parent(ti));
}
for (i = 0; i < ti->num_interfaces; i++) {
object_interface_init(obj, &ti->interfaces[i]);
}
if (ti->instance_init) {
ti->instance_init(obj);
}
}
void object_initialize_with_type(void *data, TypeImpl *type)
{
Object *obj = data;
g_assert(type != NULL);
g_assert(type->instance_size >= sizeof(ObjectClass));
type_class_init(type);
g_assert(type->abstract == false);
memset(obj, 0, type->instance_size);
obj->class = type->class;
object_init_with_type(obj, type);
}
void object_initialize(void *data, const char *typename)
{
TypeImpl *type = type_get_by_name(typename);
object_initialize_with_type(data, type);
}
static void object_deinit(Object *obj, TypeImpl *type)
{
if (type->instance_finalize) {
type->instance_finalize(obj);
}
while (obj->interfaces) {
Interface *iface_obj = obj->interfaces->data;
obj->interfaces = g_slist_delete_link(obj->interfaces, obj->interfaces);
object_delete(OBJECT(iface_obj));
}
if (type_has_parent(type)) {
object_deinit(obj, type_get_parent(type));
}
}
void object_finalize(void *data)
{
Object *obj = data;
TypeImpl *ti = obj->class->type;
object_deinit(obj, ti);
}
Object *object_new_with_type(Type type)
{
Object *obj;
g_assert(type != NULL);
obj = g_malloc(type->instance_size);
object_initialize_with_type(obj, type);
return obj;
}
Object *object_new(const char *typename)
{
TypeImpl *ti = type_get_by_name(typename);
return object_new_with_type(ti);
}
void object_delete(Object *obj)
{
object_finalize(obj);
g_free(obj);
}
static bool object_is_type(Object *obj, const char *typename)
{
TypeImpl *target_type = type_get_by_name(typename);
TypeImpl *type = obj->class->type;
GSList *i;
/* Check if typename is a direct ancestor of type */
while (type) {
if (type == target_type) {
return true;
}
type = type_get_parent(type);
}
/* Check if obj has an interface of typename */
for (i = obj->interfaces; i; i = i->next) {
Interface *iface = i->data;
if (object_is_type(OBJECT(iface), typename)) {
return true;
}
}
return false;
}
Object *object_dynamic_cast(Object *obj, const char *typename)
{
GSList *i;
/* Check if typename is a direct ancestor */
if (object_is_type(obj, typename)) {
return obj;
}
/* Check if obj has an interface of typename */
for (i = obj->interfaces; i; i = i->next) {
Interface *iface = i->data;
if (object_is_type(OBJECT(iface), typename)) {
return OBJECT(iface);
}
}
/* Check if obj is an interface and its containing object is a direct
* ancestor of typename */
if (object_is_type(obj, TYPE_INTERFACE)) {
Interface *iface = INTERFACE(obj);
if (object_is_type(iface->obj, typename)) {
return iface->obj;
}
}
return NULL;
}
static void register_interface(void)
{
static TypeInfo interface_info = {
.name = TYPE_INTERFACE,
.instance_size = sizeof(Interface),
.abstract = true,
};
type_register_static(&interface_info);
}
device_init(register_interface);
Object *object_dynamic_cast_assert(Object *obj, const char *typename)
{
Object *inst;
inst = object_dynamic_cast(obj, typename);
if (!inst) {
fprintf(stderr, "Object %p is not an instance of type %s\n",
obj, typename);
abort();
}
return inst;
}
ObjectClass *object_class_dynamic_cast(ObjectClass *class,
const char *typename)
{
TypeImpl *target_type = type_get_by_name(typename);
TypeImpl *type = class->type;
while (type) {
if (type == target_type) {
return class;
}
type = type_get_parent(type);
}
return NULL;
}
ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
const char *typename)
{
ObjectClass *ret = object_class_dynamic_cast(class, typename);
if (!ret) {
fprintf(stderr, "Object %p is not an instance of type %s\n",
class, typename);
abort();
}
return ret;
}
const char *object_get_typename(Object *obj)
{
return obj->class->type->name;
}
ObjectClass *object_get_class(Object *obj)
{
return obj->class;
}
const char *object_class_get_name(ObjectClass *klass)
{
return klass->type->name;
}
ObjectClass *object_class_by_name(const char *typename)
{
TypeImpl *type = type_get_by_name(typename);
if (!type) {
return NULL;
}
type_class_init(type);
return type->class;
}
typedef struct OCFData
{
void (*fn)(ObjectClass *klass, void *opaque);
void *opaque;
} OCFData;
static void object_class_foreach_tramp(gpointer key, gpointer value,
gpointer opaque)
{
OCFData *data = opaque;
TypeImpl *type = value;
type_class_init(type);
data->fn(value, type->class);
}
void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
void *opaque)
{
OCFData data = { fn, opaque };
g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
}
|