链表宏定义
2026/4/26大约 3 分钟宏定义数据结构常量
链表相关定义
📚 本节导读
用途:双向链表和单向链表的宏定义
来源:kernel/include/os_list.h:351
一、初始化宏
1.1 OS_LIST_INIT
#define OS_LIST_INIT(name) \
{ \
&(name), &(name) \
}说明:初始化双向链表,仅作为右值使用
使用示例:
os_list_node_t list = OS_LIST_INIT(list);1.2 OS_SLIST_INIT
#define OS_SLIST_INIT(name) \
{ \
OS_NULL \
}说明:初始化单向链表,仅作为右值使用
使用示例:
os_slist_node_t slist = OS_SLIST_INIT(slist);二、链表节点操作宏
2.1 os_list_entry
#define os_list_entry(ptr, type, member) os_container_of(ptr, type, member)说明:通过链表节点指针获取包含它的结构体指针
参数:
ptr:struct os_list_node指针type:结构体类型member:结构体中的链表成员名称
使用示例:
struct my_struct
{
int data;
os_list_node_t list_node;
};
os_list_node_t *node = ...;
struct my_struct *entry = os_list_entry(node, struct my_struct, list_node);2.2 os_list_first_entry
#define os_list_first_entry(head, type, member) os_list_entry((head)->next, type, member)说明:获取链表的第一个节点对应的结构体
注意:链表不能为空
2.3 os_list_first_entry_or_null
#define os_list_first_entry_or_null(head, type, member) \
(!os_list_empty(head) ? os_list_first_entry(head, type, member) : OS_NULL)说明:获取链表的第一个节点对应的结构体,如果为空则返回 OS_NULL
2.4 os_list_tail_entry
#define os_list_tail_entry(head, type, member) os_list_entry(os_list_tail(head), type, member)说明:获取链表的最后一个节点对应的结构体
注意:链表不能为空
2.5 os_list_tail_entry_or_null
#define os_list_tail_entry_or_null(head, type, member) \
(!os_list_empty(head) ? os_list_tail_entry(head, type, member) : OS_NULL)说明:获取链表的最后一个节点对应的结构体,如果为空则返回 OS_NULL
三、双向链表遍历宏
3.1 os_list_for_each
#define os_list_for_each(pos, head) for ((pos) = (head)->next; (pos) != (head); (pos) = (pos)->next)说明:遍历链表
参数:
pos:循环计数器(struct os_list_node)head:链表头
使用示例:
os_list_node_t *pos;
os_list_for_each(pos, &head)
{
// 处理每个节点
}3.2 os_list_for_each_safe
#define os_list_for_each_safe(pos, n, head) \
for ((pos) = (head)->next, (n) = (pos)->next; (pos) != (head); (pos) = (n), (n) = (pos)->next)说明:安全遍历链表(在遍历时可以删除节点)
参数:
pos:循环计数器(struct os_list_node)n:临时存储(struct os_list_node)head:链表头
3.3 os_list_for_each_entry
#define os_list_for_each_entry(pos, head, type, member) \
for ((pos) = os_list_entry((head)->next, type, member); &(pos)->member != (head); \
(pos) = os_list_entry((pos)->member.next, type, member))说明:遍历指定类型的链表
3.4 os_list_for_each_entry_safe
#define os_list_for_each_entry_safe(pos, n, head, type, member) \
for ((pos) = os_list_entry((head)->next, type, member), (n) = os_list_entry(pos->member.next, type, member); \
&(pos)->member != (head); \
(pos) = (n), (n) = os_list_entry(n->member.next, type, member))说明:安全遍历指定类型的链表(在遍历时可以删除节点)
四、单向链表节点操作宏
4.1 os_slist_entry
#define os_slist_entry(ptr, type, member) os_container_of(ptr, type, member)说明:通过单向链表节点指针获取包含它的结构体指针
4.2 os_slist_first_entry
#define os_slist_first_entry(head, type, member) os_slist_entry((head)->next, type, member)说明:获取单向链表的第一个节点对应的结构体
4.3 os_slist_first_entry_or_null
#define os_slist_first_entry_or_null(head, type, member) \
(!os_slist_empty(head) ? os_slist_first_entry(head, type, member) : OS_NULL)说明:获取单向链表的第一个节点对应的结构体,如果为空则返回 OS_NULL
4.4 os_slist_tail_entry
#define os_slist_tail_entry(head, type, member) os_slist_entry(os_slist_tail(head), type, member)说明:获取单向链表的最后一个节点对应的结构体
4.5 os_slist_tail_entry_or_null
#define os_slist_tail_entry_or_null(head, type, member) \
(!os_slist_empty(head) ? os_slist_tail_entry(head, type, member) : OS_NULL)说明:获取单向链表的最后一个节点对应的结构体,如果为空则返回 OS_NULL
五、单向链表遍历宏
5.1 os_slist_for_each
#define os_slist_for_each(pos, head) for (pos = (head)->next; pos != OS_NULL; pos = pos->next)说明:遍历单向链表
5.2 os_slist_for_each_safe
#define os_slist_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = (pos != OS_NULL) ? pos->next : OS_NULL; pos != OS_NULL; \
pos = n, n = (pos != OS_NULL) ? pos->next : OS_NULL)说明:安全遍历单向链表(在遍历时可以删除节点)