概述
目录
1 前言
2 相关数据结构
2.1 对象
1)类型
2)数据结构
2.3 容器
1)数据结构
2)容器定义 _object_container[]
2.3 对象容器链接图
3 获取对象容器 rt_object_get_information()
4 对象初始化 rt_object_init()
5 对象删除 rt_object_delete()
6 查找对象rt_object_find()
7 总结
1 前言
在内核中,所有数据结构都视作一个对象,包括:线程、信号量、互斥量、事件、邮箱、消息队列、内存堆、内存池、设备和定时器。为了方便管理,当用户或者内核创建一个对象时,就会将该对象放到容器中。容器在内核中是一个全局数组,记录着所有对象的信息。
2 相关数据结构
2.1 对象
1)类型
enum rt_object_class_type
{
RT_Object_Class_Null = 0x00, /**< The object is not used. */
RT_Object_Class_Thread = 0x01, /**< The object is a thread. */
RT_Object_Class_Semaphore = 0x02, /**< The object is a semaphore. */
RT_Object_Class_Mutex = 0x03, /**< The object is a mutex. */
RT_Object_Class_Event = 0x04, /**< The object is a event. */
RT_Object_Class_MailBox = 0x05, /**< The object is a mail box. */
RT_Object_Class_MessageQueue = 0x06, /**< The object is a message queue. */
RT_Object_Class_MemHeap = 0x07, /**< The object is a memory heap. */
RT_Object_Class_MemPool = 0x08, /**< The object is a memory pool. */
RT_Object_Class_Device = 0x09, /**< The object is a device. */
RT_Object_Class_Timer = 0x0a, /**< The object is a timer. */
RT_Object_Class_Module = 0x0b, /**< The object is a module. */
RT_Object_Class_Unknown = 0x0c, /**< The object is unknown. */
RT_Object_Class_Static = 0x80 /**< The object is a static object. */
};
2)数据结构
struct rt_object
{
//对象名称,使用字符串表示,长度由RT_NAME_MAX决定
char name[RT_NAME_MAX];
//对象类型
rt_uint8_t type;
//对象状态
rt_uint8_t flag;
//侵入式链表,将该对象链接至容器中
rt_list_t list;
};
typedef struct rt_object *rt_object_t;
2.3 容器
内核中就是使用全局结构体数组(rt_object_information _object_container[])来定义容器。每一个容器都存放同一类型的对象,保存其信息,使用链表将所有对象链接起来方便管理。
1)数据结构
struct rt_object_information
{
//容器下的对象类型
enum rt_object_class_type type;
//链接容器下的所有对象
rt_list_t object_list;
//其对象大小
rt_size_t object_size;
};
2)容器定义 _object_container[]
//2.1) 容器是使用数组表示,下面是定义不同对象在数组中的序号
enum rt_object_info_type
{
RT_Object_Info_Thread = 0, /**< The object is a thread. */
#ifdef RT_USING_SEMAPHORE
RT_Object_Info_Semaphore, /**< The object is a semaphore. */
#endif
#ifdef RT_USING_MUTEX
RT_Object_Info_Mutex, /**< The object is a mutex. */
#endif
#ifdef RT_USING_EVENT
RT_Object_Info_Event, /**< The object is a event. */
#endif
#ifdef RT_USING_MAILBOX
RT_Object_Info_MailBox, /**< The object is a mail box. */
#endif
#ifdef RT_USING_MESSAGEQUEUE
RT_Object_Info_MessageQueue, /**< The object is a message queue. */
#endif
#ifdef RT_USING_MEMHEAP
RT_Object_Info_MemHeap, /**< The object is a memory heap */
#endif
#ifdef RT_USING_MEMPOOL
RT_Object_Info_MemPool, /**< The object is a memory pool. */
#endif
#ifdef RT_USING_DEVICE
RT_Object_Info_Device, /**< The object is a device */
#endif
RT_Object_Info_Timer, /**< The object is a timer. */
#ifdef RT_USING_MODULE
RT_Object_Info_Module, /**< The object is a module. */
#endif
RT_Object_Info_Unknown, /**< The object is unknown. */
};
//2.2) 每一个容器都存放同一类型的对象,比如_object_container[0]都是存放对象RT_Object_Class_Thread,再使用链表object_list将所有对象链接起来
#define _OBJ_CONTAINER_LIST_INIT(c)
{&(_object_container[c].object_list), &(_object_container[c].object_list)}
static struct rt_object_information _object_container[RT_Object_Info_Unknown] =
{
/* initialize object container - thread */
{RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Thread), sizeof(struct rt_thread)},
#ifdef RT_USING_SEMAPHORE
/* initialize object container - semaphore */
{RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Semaphore), sizeof(struct rt_semaphore)},
#endif
#ifdef RT_USING_MUTEX
/* initialize object container - mutex */
{RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Mutex), sizeof(struct rt_mutex)},
#endif
#ifdef RT_USING_EVENT
/* initialize object container - event */
{RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Event), sizeof(struct rt_event)},
#endif
#ifdef RT_USING_MAILBOX
/* initialize object container - mailbox */
{RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MailBox), sizeof(struct rt_mailbox)},
#endif
#ifdef RT_USING_MESSAGEQUEUE
/* initialize object container - message queue */
{RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MessageQueue), sizeof(struct rt_messagequeue)},
#endif
#ifdef RT_USING_MEMHEAP
/* initialize object container - memory heap */
{RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemHeap), sizeof(struct rt_memheap)},
#endif
#ifdef RT_USING_MEMPOOL
/* initialize object container - memory pool */
{RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemPool), sizeof(struct rt_mempool)},
#endif
#ifdef RT_USING_DEVICE
/* initialize object container - device */
{RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Device), sizeof(struct rt_device)},
#endif
/* initialize object container - timer */
{RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Timer), sizeof(struct rt_timer)},
#ifdef RT_USING_MODULE
/* initialize object container - module */
{RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Module), sizeof(struct rt_dlmodule)},
#endif
};
2.3 对象容器链接图
3 获取对象容器 rt_object_get_information()
struct rt_object_information* rt_object_get_information(enum rt_object_class_type type)
{
int index;
//遍历容器数组_object_container[],通过类型找到对象容器
for (index = 0; index < RT_Object_Info_Unknown; index ++)
if (_object_container[index].type == type)
return &_object_container[index];
return RT_NULL;
}
4 对象初始化 rt_object_init()
void rt_object_init(struct rt_object *object,
enum rt_object_class_type type,
const char *name)
{
//1 通过对象类型,找到容器
information = rt_object_get_information(type);
//2 遍历容器下的所有对象,如果和需初始化的对象一致,则进入断言
//PS:这个错误检查的作用???
for (node = information->object_list.next;
node != &(information->object_list);
node = node->next)
{
struct rt_object *obj;
obj = rt_list_entry(node, struct rt_object, list);
if (obj) /* skip warning when disable debug */
{
RT_ASSERT(obj != object);
}
}
//3 设置静态标记
object->type = type | RT_Object_Class_Static;
//4 设置对象名称
rt_strncpy(object->name, name, RT_NAME_MAX);
//5 将对象插入至容器链表末端
rt_list_insert_after(&(information->object_list), &(object->list));
}
5 对象删除 rt_object_delete()
void rt_object_delete(rt_object_t object)
{
//1 复位对象类型
object->type = RT_Object_Class_Null;
//2 将对象从容器链表中剔除
rt_list_remove(&(object->list));
//3 释放对象内存
RT_KERNEL_FREE(object);
}
6 查找对象rt_object_find()
rt_object_t rt_object_find(const char *name, rt_uint8_t type)
{
//1 通过类型找到对应容器
information = rt_object_get_information((enum rt_object_class_type)type);
//2 上下文检测,该函数不允许在中断中调用
RT_DEBUG_NOT_IN_INTERRUPT;
}
7 总结
- 内核中每一个数据结构都抽象为对象rt_object,包括:线程、信号量、互斥量、事件、邮箱、消息队列、内存堆、内存池、设备和定时器。
- 在实际使用过程中,对象属性是可以继承的
- 内核使用全局结构体数组维护容器,数组中每一个成员管理着所有相同类型的对象,方便对外随时查询各个对象信息
最后
以上就是疯狂电灯胆为你收集整理的RT-Thread分析-对象容器实现与作用1 前言2 相关数据结构3 获取对象容器 rt_object_get_information()4 对象初始化 rt_object_init()5 对象删除 rt_object_delete()6 查找对象rt_object_find()7 总结的全部内容,希望文章能够帮你解决RT-Thread分析-对象容器实现与作用1 前言2 相关数据结构3 获取对象容器 rt_object_get_information()4 对象初始化 rt_object_init()5 对象删除 rt_object_delete()6 查找对象rt_object_find()7 总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复