0
点赞
收藏
分享

微信扫一扫

Linux内核源码中最常见的数据结构之【list_head】

Linux内核源码中最常见的数据结构之【list_head】

1. 定义

​list_head​​可以说是Linux内核使用的最多的数据结构之一了,它让开发人员能以双向链表的形式快速将当前结构链接起来,同时对链表进行基本操作。其定义如下:

struct list_head {
struct list_head *next, *prev;
};

使用方法就是将​​list_head​​以成员变量的形式添加到其他数据结构中。

Linux内核源码中最常见的数据结构之【list_head】_数据结构

假设现有一群学生在排队

struct student{
char *name;
int age;
struct list_node others;
}

获取同学S其前面一位同学的方式为​​S->others.next​​,是不是很简单?

但是你可能会问,​​S->others.next​​​获取的是前一位同学的​​others​​成员变量,那还是不能访问前一位同学自身结构啊?

问得好,我们接着介绍…

#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})

​offsetof​​​宏定义,获取​​TYPE​​​类型结构体中​​MEMBER​​​成员的偏移量,PS:​​NULL​​​指针的地址为​​0x0000​

​container_of​​也是Linux内核中常用的宏定义之一,其作用就是通过某个结构体的成员变量获取该结构体本身

  • ​ptr​​指向该成员的指针
  • ​type​​为包含该成员的结构体类型
  • ​member​​为该成员在结构体中的名字

在上述定义中

​typeof( ((type *)0)->member )​​​这个语句中,先将0隐式转化为​​NULL​​​再显式类型转化为​​type​​​类型,再通过​​typeof​​​获取​​type​​​类型结构体中名字叫做​​member​​的成员变量的数据类型

​const typeof( ((type *)0)->member ) *__mptr​​​ 定义了一个​​member​​数据类型的指针

​(char *)__mptr​​​获取​​__mptr​​在内存中的地址

​(char *)__mptr - offsetof(type,member)​​获取该结构体首地址

​(type *)( (char *)__mptr - offsetof(type,member) )​​​显式类型转化为​​type​​类型,任务完成!

现在通过​​container_of(S->others.next, struct student, others)​​就可以获取前一位同学的结构体啦

2. 相关方法

对于双向链表,除了上述基本操作,还包括初始化链表、添加节点、删除节点、更新节点、遍历链表等操作。这些方法也都被定义在内核中。

  1. 初始化链表
//创建头节点
#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)

static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
  1. 添加节点
/*
* 在两个已知的连续节点之间添加新节点
*/
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;
}

/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
* 在指定的头节点之后插入新的节点
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}


/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
* 在特定的头节点之前插入新的节点
* 对用于实现队列非常有用
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
  1. 删除节点
/*
* 删除两个已知节点之间的节点
*/
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}

static inline void __list_del_entry(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
}

static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
  1. 更新节点
/**
* list_replace - replace old entry by new one
* @old : the element to be replaced
* @new : the new element to insert
*
* If @old was empty, it will be overwritten.
*/
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;
}
  1. 遍历链表
/**
* 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 - 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)

/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
* 遍历list_node成员所在的结构体类型
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)

#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
  1. 判断是否是尾节点,判断链表是否为空
/**
* list_is_last - tests whether @list is the last entry in list @head
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_last(const struct list_head *list,
const struct list_head *head)
{
return list->next == head;
}

/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}

3. 使用案例

#include <stdio.h>

struct student{
char* name;
int age;
struct list_head others;
};

int main(void)
{
//学生排队,队列为lines
LIST_HEAD(lines);
struct student s1 = {"s1", 10};
struct student s2 = {"s2", 9};
struct student s3 = {"s3", 11};

//将s1加入到队伍中
list_add_tail(&s1.others, &lines);
//将s2加入到队伍中
list_add_tail(&s2.others, &lines);
//将s3加入到队伍中
list_add_tail(&s3.others, &lines);

struct student *cursor;
printf("lines: \n");
list_for_each_entry(cursor, &lines, others){
printf("%s -> ", cursor->name);
}
printf("\n");

//s4这个人19岁,不讲武德,插队
struct student s4 = {"s4", 19};
list_add(&s4.others, &lines);
printf("lines: \n");
list_for_each_entry(cursor, &lines, others){
printf("%s -> ", cursor->name);
}
printf("\n");

//老师来了,把s4提出队伍
list_del(&s4.others);
printf("lines: \n");
list_for_each_entry(cursor, &lines, others){
printf("%s -> ", cursor->name);
}
printf("\n");

return 0;
}

输出结果

//三个学生排队
lines:
s1 -> s2 -> s3 ->
//s4不讲武德,插队
lines:
s4 -> s1 -> s2 -> s3 ->
//s4被踢出队伍
lines:
s1 -> s2 -> s3 ->


举报

相关推荐

0 条评论