include/linux/list.h
找到;include/linux/types.h
找到;struct list_head {
struct list_head *next, *prev;
};
// 静态初始化
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
// 调用INIT_LIST_HEAD来初始化, **WRITE_ONCE**这个后面我们专门介绍
static inline void INIT_LIST_HEAD(struct list_head *list)
{
WRITE_ONCE(list->next, list);
list->prev = list;
}
new
插入到prev
和next
中, 这个函数是下面在头部和尾部插入的实现基础static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
if (!__list_add_valid(new, prev, next))
return;
// 前后向指针的改写赋值
next->prev = new;
new->next = next;
new->prev = prev;
WRITE_ONCE(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;
WRITE_ONCE(prev->next, next);
}
entry
static inline void __list_del_entry(struct list_head *entry)
{
if (!__list_del_entry_valid(entry))
return;
__list_del(entry->prev, entry->next);
}
都是指针的变换
static inline void list_replace(struct list_head *old,
struct list_head *new)
{
new->next = old->next;
new->next->prev = new;
new->prev = old->prev;
new->prev->next = new;
}
static inline void list_move(struct list_head *list, struct list_head *head)
{
// 从原处的list后摘掉
__list_del_entry(list);
// 添加到新链表的头部
list_add(list, head);
}
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
// 从原处的list后摘掉
__list_del_entry(list);
// 添加到新链表的队尾
list_add_tail(list, head);
}
list是新队列的head指针, 包括的元素从原head队列的第一个元素到entry, head队列仅包括余下的元素
static inline void __list_cut_position(struct list_head *list,
struct list_head *head, struct list_head *entry)
{
struct list_head *new_first = entry->next;
list->next = head->next;
list->next->prev = list;
list->prev = entry;
entry->next = list;
head->next = new_first;
new_first->prev = head;
}
static inline void __list_splice(const struct list_head *list,
struct list_head *prev,
struct list_head *next)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
first->prev = prev;
prev->next = first;
last->next = next;
next->prev = last;
}
static inline void list_splice(const struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head, head->next);
}
static inline void list_splice_tail(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head->prev, head);
}
list_entry
宏按之前说的, 这个list_head
都有要嵌入到用户定义的struct中,这个宏就是由这个list_head ptr
来获取当前所处的struct对象的指针, 用了linux的经典宏定义 container_of
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
typedef struct _Foo {
int data_;
struct list_head link;
} Foo;
int main(int argn, char* argv[]) {
LIST_HEAD(test_link);
Foo f1;
f1.data_ = 1;
LIST_HEAD_INIT(f1.link);
Foo f2;
f1.data_ = 2;
LIST_HEAD_INIT(f2.link);
list_add(&f1.link, &test_link)
list_add(&f2.link, &test_link)
struct Foo* pos;
list_for_each_entry(pos, &test_link, link) {
printf("%d\n", pos->data_);
}
return 0;
}