qemu-type-object-initialize

/*
object创建和初始化
*/
Object *object_new(const char *typename)
{
    TypeImpl *ti = type_get_by_name(typename);


    return object_new_with_type(ti);
}


/*
object_new_with_type 函数完成type对应的class、object的创建和初始化
*/
Object *object_new_with_type(Type type)
{
    Object *obj;


    g_assert(type != NULL);
    /* 创建Class,并从type的父类型的class_base_init函数先执行,再执行当       前类型中定义的的class_init函数。
       实现类似于面向对象的子对象初始化函数类似功能,执行子对象初始化函        数之前先执行父对象初始化函数。
    */
    type_initialize(type); 


    obj = g_malloc(type->instance_size); /* object创建 */


    object_initialize_with_type(obj, type); /* object初始化 */
    object_ref(obj);


    return obj;
}


/*
object已经创建,完成初始化
*/
void object_initialize(void *data, const char *typename)
{
    TypeImpl *type = type_get_by_name(typename);


    object_initialize_with_type(data, type);
}


/*
object已经创建,进行object初始化
*/
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);
    /* for virtio-scsi-pci, type->class pointed to a variable of PCIDeviceClass.
         so here the obj->class point to the PCIDeviceClass too.
    */
    obj->class = type->class;
    QTAILQ_INIT(&obj->properties);


    object_init_with_type(obj, type); /* 执行object初始化函数 */
}


/*
从父type开始,依次向下执行type中定义的用来初始化object的interface初始化函数、instance_init函数
*/
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);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值