一.链表头结构体
struct list_head {
struct list_head *next, *prev;
};
二.初始化链表头
也就是把list的next和prew指针指向自己
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
三.添加链表项
通用的添加链表项函数
函数执行前
static inline void __list_add(struct list_head *new,struct list_head *prev, struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
执行后
在链表头部添加链表项
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
在链表尾部添加链表项
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
四.删除链表项
函数执行前
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
执行后
链表中删除链表项
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next); //从链表中脱开链表项
entry->next = LIST_POISON1; //移除链表项的next指针
entry->prev = LIST_POISON2; //移除链表项的prev指针
}
五.删除链表头
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
六.一些list相关的宏
1.list_entry
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
2.list_first_entry
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)