qemu 设备模拟(5)

2019-06-11 09:33

object_class_get_name(iface)); } //4.如果本类型有自己的interfaces,初始化 for (i = 0; i < ti->num_interfaces; i++) { TypeImpl *t = type_get_by_name(ti->interfaces[i].typename); 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,

ti->interfaces[i].typename); } } ti->class->type = ti; while (parent) { if (parent->class_base_init) { //回溯回调parent的class_base_init函数 parent->class_base_init(ti->class, ti->class_data); } parent = type_get_parent(parent); } if (ti->class_init) { /* 如果本类设置了class_init,回调它,ti->class_data是一个void*的参数 比如\我们设置了pit_class_initfn 这个函数主要干啥?主要填充class里的其他该填充的地方。 malloc之后你总得调用构造函数吧,调用构造函数的第一句都是super(xxx) 这工作前面,2,3步骤已经做了,然后干你自己的活。见pit_class_initfn定义 */ ti->class_init(ti->class, ti->class_data); } }

1 2 3 4 5 6 7 8

DeviceState*qdev_try_create(BusState*bus,constchar*type) {

DeviceState*dev;

//这个type为TypeInfo.name,例如\ if(object_class_by_name(type)==NULL){ returnNULL; }

//type_initialize完成后,object_new用来实例化一个instance

9 dev=DEVICE(object_new(type));// =

10 DEVICE(object_new_with_type(type_get_by_name(typename))) 11 if(!dev){

12 returnNULL; 13 }

14 if(!bus){

15 bus=sysbus_get_default(); 16 }

17 qdev_set_parent_bus(dev,bus); 18 object_unref(OBJECT(dev)); 19 returndev; 20 } 21

22 ObjectClass*object_class_by_name(constchar*typename) 23 {

24 //之前在type_register_static的时候,注册了TypeInfo.name,25 例如\为key的TypeImpl

26 TypeImpl*type=type_get_by_name(typename); 27 if(!type){

28 returnNULL; 29 }

30 type_initialize(type);//这里面,初始化class, 31 returntype->class; 32 } 33

34 //其实这个函数更应该叫做new_TypeInfo_class() 35 staticvoidtype_initialize(TypeImpl*ti) 36 {

37 TypeImpl*parent; 38 if(ti->class){ 39 return; 40 } 41 /*

42 type_class_get_size 首先获取自己的class_size变量,如果没有,43 再找parent类型所指的TypeImpl的class_size,直到找到为止

44 比如\没有设置class_size,那么获取的是\45 的class_size, 而type_object_get_size也是类似 46 static const TypeInfo pit_common_type = {

47 .name = \48 .parent = \49 .instance_size = sizeof(PITCommonState), 50 .class_size = sizeof(PITCommonClass), 51 .class_init = pit_common_class_init, 52 .abstract = true,

53 }; 54 */

55 ti->class_size=type_class_get_size(ti); 56 ti->instance_size=type_object_get_size(ti); 57 ti->class=g_malloc0(ti->class_size); 58 parent=type_get_parent(ti); 59 if(parent){

60 //1,保证parent初始化了 61 type_initialize(parent); 62 GSList*e; 63 inti; 64 65 //2,将parent的class内容memcpy一份给自己的对应的66 parent区域

67 g_assert(parent->class_size<=ti->class_size); 68 memcpy(ti->class,parent->class,parent->class_size)69 ; 70

71 //3,将parent里面的class的interfaces做一次深度复72 制,复制给自己

73 for(e=parent->class->interfaces;e;e=e->next){ 74 ObjectClass*iface=e->data;

75 type_initialize_interface(ti,object_class_76 get_name(iface)); 77 } 78

79 //4.如果本类型有自己的interfaces,初始化 80 for(i=0;inum_interfaces;i++){

81 TypeImpl*t=type_get_by_name(ti->interfaces82 [i].typename);

83 for(e=ti->class->interfaces;e;e=e->next){ 84 TypeImpl*target_type=OBJECT_CLASS(85 e->data)->type;

86 if(type_is_ancestor(target_type,t)87 ){

88 break; 89 } 90 }

91 if(e){

92 continue; 93 }

94 type_initialize_interface(ti,ti->interface95 s[i].typename); 96 }

97 } 98

99 ti->class->type=ti; 10 while(parent){

0 if(parent->class_base_init){

10 //回溯回调parent的class_base_init函数 1 parent->class_base_init(ti->class,ti->clas10s_data);

2 }

10 parent=type_get_parent(parent); 3 } 10

4 if(ti->class_init){ 10 /*

5 如果本类设置了class_init,回调它,ti->class_data是10一个void*的参数

6 比如\我们设置了pit_class_initfn

这个函数主要干啥?主要填充class里的其他该填充的地方。

malloc之后你总得调用构造函数吧,调用构造函数的第一句都是super(xxx)

这工作前面,2,3步骤已经做了,然后干你自己的活。见pit_class_initfn定义 */

ti->class_init(ti->class,ti->class_data); } }

上述代码把object_class_by_name的流程说完了,再看看object_new(type) =

object_new_with_type(type_get_by_name(typename))的流程: Object *object_new_with_type(Type type) { Object *obj; g_assert(type != NULL); type_initialize(type); obj = g_malloc(type->instance_size); //这个instance_size是初始化TypeInfo的时候设置的sizeof(PITCommonState)

object_initialize_with_type(obj, type); obj->free = g_free;

return obj; } void object_initialize_with_type(void *data, TypeImpl *type) { Object *obj = data; g_assert(type != NULL); type_initialize(type); g_assert(type->instance_size >= sizeof(Object)); g_assert(type->abstract == false); memset(obj, 0, type->instance_size); obj->class = type->class; //instace的类型通过class指针指定 object_ref(obj);

QTAILQ_INIT(&obj->properties); object_init_with_type(obj, type); //深度递归调用TypeImpl及其parent的instance_init函数指针,相当于new instance的构造函数 }

Object*object_new_with_type(Typetype) 1 {

2 Object*obj;

3 g_assert(type!=NULL); 4 type_initialize(type);

5 obj=g_malloc(type->instance_size);//这个instance_size是初6 始化TypeInfo的时候设置的sizeof(PITCommonState) 7 object_initialize_with_type(obj,type); 8 obj->free=g_free; 9 returnobj; 10 } 11 voidobject_initialize_with_type(void*data,TypeImpl*type) 12 { 13 Object*obj=data; 14 g_assert(type!=NULL); 15 type_initialize(type); 16 g_assert(type->instance_size>=sizeof(Object)); 17 g_assert(type->abstract==false); 18 memset(obj,0,type->instance_size); 19 obj->class=type->class;//instace的类型通过class指针指定 20 object_ref(obj); 21 QTAILQ_INIT(&obj->properties); 22 object_init_with_type(obj,type);//深度递归调用TypeImpl及其23 parent的instance_init函数指针,相当于new instance的构造函数 }


qemu 设备模拟(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:管理学原理与方法课后习题

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: