rtthread 链表操作的的几个 API
/* 将节点 n 插入到节点 l 的后面,如果 l 为头节点,则插入到链表头部 */
rt_inline void rt_list_insert_after(rt_list_t *l, rt_list_t *n)
{
l->next->prev = n;
n->next = l->next;
l->next = n;
n->prev = l;
}
/* 将节点 n 插入到节点 l 的前面,如果 l 为头节点,则插入到链表尾部,头的前面就是结尾 */
rt_inline void rt_list_insert_before(rt_list_t *l, rt_list_t *n)
{
l->prev->next = n;
n->prev = l->prev;
l->prev = n;
n->next = l;
}
/* 从 n 所在的链表中移除该节点 */
rt_inline void rt_list_remove(rt_list_t *n)
{
n->next->prev = n->prev;
n->prev->next = n->next;
n->next = n->prev = n;
}
/* 判断该链表是否为空 */
rt_inline int rt_list_isempty(const rt_list_t *l)
{
return l->next == l;
}
/* 获取链表上节点个数 */
rt_inline unsigned int rt_list_len(const rt_list_t *l)
{
unsigned int len = 0;
const rt_list_t *p = l;
while (p->next != l)
{
p = p->next;
len ++;
}
return len;
}
/* 遍历 head 链表上所有节点 */
#define rt_list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
rtthread 实用的几个宏
/* 通过 node 地址返回 node 所在结构体地址 */
define rt_list_entry(node, type, member) \
rt_container_of(node, type, member)
/* 搭配使用效果更佳 ,遍历链表上所有节点,并通过这个节点拿到节点所在结构体*/
rt_list_t *node = RT_NULL;
rt_list_for_each(node, &xxx_list)
{
struct xxx_struct *obj;
obj = rt_list_entry(node, struct xxx_struct, obj_list);
if (obj) /* skip warning when disable debug */
{
obj->xxxx // 操作结构体成员
}
}