rt-thread的内核对象管理系统分析(四)

2014-11-24 08:13:35 · 作者: · 浏览: 2
bject and release object memory.
*
* @param object the specified object to be deleted.
*/
void rt_object_delete(rt_object_t object)
{
register rt_base_t temp;
/* object check */
RT_ASSERT(object != RT_NULL);
RT_ASSERT(!(object->type & RT_Object_Class_Static));//删除的对象必须是非系统对象
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);//开中断
#if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)//如果使用了模块功能且采用的是SLAB动态内存管理模式
if (object->flag & RT_OBJECT_FLAG_MODULE)
rt_module_free((rt_module_t)object->module_id, object);//释放模块ID所占空间
else
#endif
/* free the memory of object */
rt_free(object);//释放内核对象所占空间
}
其中rt_list_remove会自动找到对象的前一节点和后一节点,然后删除本身节点.
1.3 判断是否为系统内核对象
[cpp]
/**
* This function will judge the object is system object or not.
* Normally, the system object is a static object and the type
* of object set to RT_Object_Class_Static.
*
* @param object the specified object to be judged.
*
* @return RT_TRUE if a system object, RT_FALSE for others.
*/
rt_bool_t rt_object_is_systemobject(rt_object_t object)
{
/* object check */
RT_ASSERT(object != RT_NULL);
if (object->type & RT_Object_Class_Static)//RTT是通过内核对象的type的最高位是否为1来判断此对象是否为系统内核对象的
return RT_TRUE;
return RT_FALSE;
}
1.4 查找内核对象
[cpp]
/**
* This function will find specified name object from object
* container.
*
* @param name the specified name of object.
* @param type the type of object
*
* @return the found object or RT_NULL if there is no this object
* in object container.
*
* @note this function shall not be invoked in interrupt status.
*/
rt_object_t rt_object_find(const char *name, rt_uint8_t type)
{
struct rt_object *object;
struct rt_list_node *node;
struct rt_object_information *information;
extern volatile rt_uint8_t rt_interrupt_nest;
/* parameter check *///输入系统检查
if ((name == RT_NULL) || (type > RT_Object_Class_Unknown))
return RT_NULL;
/* which is invoke in interrupt status */
if (rt_interrupt_nest != 0)//确保当前没有中断嵌套
RT_ASSERT(0);
/* enter critical */
rt_enter_critical();//进入临界区
/* try to find object */
information = &rt_object_container[type];//获取对应的对象容器
for (node = information->object_list.next;//开始通过名字来扫描内核对象
node != &(information->object_list);
node = node->next)
{
object = rt_list_entry(node, struct rt_object, list);//获取内核对象
if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)//判断名字是否相符
{
/* leave critical */
rt_exit_critical();//退出临界区
return object;
}
}
/* leave critical */
rt_exit_critical();//退出临界区
return RT_NULL;
}