/*
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);
}
}
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);
}
}