都说linux内核中list_head很经典,今晚来一探究竟。好困啊,手臂还软,代码都码不动了,可我不能睡,写完这个就去玩。
1. 先看结构体定义:
这个结构体定义在include/linux/types.h中
struct list_head {
struct list_head *next, *prev;
};
第一次见到这个结构体,愣了一下,这个结构体只有前后指针,怎么存内容啊?原来不是直接用它,而是自己定义一个宿主结构体,将list_head结构体作为宿主结构体的成员来使用。借助linux内核中提供的list_head一系列函数来快速构建自己的双向循环链表。
操作函数定义在include/linux/list.h中
2. 创建list_head头结点:
使用LIST_HEAD(name),LIST_HEAD_INIT都可以。
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
#define LIST_HEAD_INIT(name) { &(name), &(name) }
3.添加一个节点
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
__list_add()
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;
}
4.删除一个节点
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
__list_del()
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
5.根据list_head成员找到宿主结构体地址
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
6.遍历list_head链表
list_for_each(),在这个函数中不能执行删除操作
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
list_for_each_safe(),在这个函数中可以执行删除操作。
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
7.遍历宿主结构体list_for_each_entry_safe()
/**
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
小例子
#include <linux/init.h>
#include <linux/module.h>
#include <linux/list.h>
#include <linux/string.h>
struct Student
{
char name[20];
int age;
struct list_head list;
};
void my_head_list_fun(void)//还必须带有void阿
{
struct Student hy =
{
.name = "hy",
.age = 25,
};
struct Student hd =
{
.name = "hd",
.age = 26,
};
struct Student ym =
{
.name = "ym",
.age = 25,
};
struct list_head head = LIST_HEAD_INIT(head);
list_add(&hy.list,&head);
list_add(&hd.list,&head);
list_add(&ym.list,&head);
struct list_head *pos = NULL;
struct Student *student = NULL;
list_for_each(pos,&head)
{
student = list_entry(pos,struct Student,list);
printk("name = %s,age = %d\n",student->name,student->age);
}
printk("---------------\n");
//删除一个节点试试
struct list_head *temp = NULL;
list_for_each_safe(pos,temp,&head)
{
student = list_entry(pos,struct Student,list);
if(strcmp(student->name,"ym")==0)
{
list_del(&student->list);
}
}
list_for_each(pos,&head)
{
student = list_entry(pos,struct Student,list);
printk("name = %s,age = %d\n",student->name,student->age);
}
}
static int head_list_init(void)
{
printk("head_list_init.\n");
my_head_list_fun();
return 0;
}
void head_list_exit(void)
{
printk("head_list_exit.\n");
}
module_init(head_list_init);
module_exit(head_list_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("hy");
还有很多其他有用的函数,这里没有写,等用到的时候去查即可。
参考:
https://www.jianshu.com/p/8d4135b8aa72