rt-thread的内核对象管理系统分析(三)
t_attach_hook, (object));//使用钩子函数
/* lock interrupt */
temp = rt_hw_interrupt_disable();//关中断
/* insert object into information object list */
rt_list_insert_after(&(information->object_list), &(object->list));//将初始化的内核对象加入到对应容器中
/* unlock interrupt */
rt_hw_interrupt_enable(temp);//开中断
}
动态初始化是指对象原本并不存在,在不内存中,需要动态为其分配内存,其接口如下:
[cpp]
/**
* This function will allocate an object from object system
*
* @param type the type of object
* @param name the object name. In system, the object's name must be unique.
*
* @return object
*/
rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)//动态初始化接口只需要传入名字和类型
{
struct rt_object *object;
register rt_base_t temp;
struct rt_object_information *information;//对象容器
RT_DEBUG_NOT_IN_INTERRUPT;
#ifdef RT_USING_MODULE//同上面那个接口一样,获取对象容器
/*
* get module object information,
* module object should be managed by kernel object container
*/
information = (rt_module_self() != RT_NULL && (type != RT_Object_Class_Module))
&rt_module_self()->module_object[type] : &rt_object_container[type];
#else
/* get object information */
information = &rt_object_container[type];
#endif
object = (struct rt_object *)rt_malloc(information->object_size);//为对象动态分配内存空间
if (object == RT_NULL)
{
/* no memory can be allocated */
return RT_NULL;
}
/* initialize object's parameters */
/* set object type */
object->type = type;//设置类型
/* set object flag */
object->flag = 0;//设置标志为0
#ifdef RT_USING_MODULE
if (rt_module_self() != RT_NULL)
{
object->flag |= RT_OBJECT_FLAG_MODULE;//如果使用了模块功能,则将flag标志设置为模块标志
}
object->module_id = (void *)rt_module_self();//设置模块ID
#endif
/* copy name */
rt_strncpy(object->name, name, RT_NAME_MAX);//给名称赋值
RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));//使用钩子函数
/* lock interrupt */
temp = rt_hw_interrupt_disable();//关中断
/* insert object into information object list */
rt_list_insert_after(&(information->object_list), &(object->list));//将此对象加入对应容器
/* unlock interrupt */
rt_hw_interrupt_enable(temp);//关中断
/* return object */
return object;
}
2.2 脱离或删除对象
如果对象是静态初始化的,那么对应的是脱离,如果是动态初始化的,则是删除.
脱离接口如下:
[cpp]
/**
* This function will detach a static object from object system,
* and the memory of static object is not freed.
*
* @param object the specified object to be detached.
*/
void rt_object_detach(rt_object_t object)
{
register rt_base_t temp;
/* object check */
RT_ASSERT(object != RT_NULL);
RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));//使用钩子函数
/* lock interrupt */
temp = rt_hw_interrupt_disable();//关中断
/* remove from old list */
rt_list_remove(&(object->list));//从窗口中移除
/* unlock interrupt */
rt_hw_interrupt_enable(temp);//开中断
}
删除接口如下:
[cpp]
/**
* This function will delete an o