linux内核链表list_head的原理与使用

一般的链表

一般的链表如下,它是将数据结构塞入链表,这样的话,每一种数据结构都需要重新定义。

struct student_node {
	char name[20];
	int age;
	struct student_node *next;
	struct student_node *prev;
}

Linux内核中的链表

内核版本:4.1.15, 在目录include/linux/types.h中定义了list_head结构:

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

可以看到,很简单,Linux不是将数据结构塞入链表,而是将链表塞入数据结构。

list_head主要的操作

在目录include/linux/types.h中定义了list_head的操作函数:

static inline void INIT_LIST_HEAD(struct list_head *list) //初始化一个链表头,
{													      //将其中的next和prev都指向自己
	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);
}

#define container_of(ptr, type, member) ({                      \ 
	const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
	(type *)( (char *)__mptr - offsetof(type,member) );})

#define list_entry(ptr, type, member) \ 
	container_of(ptr, type, member)

#define list_for_each_entry(pos, head, member)				\     //遍历head
	for (pos = list_entry((head)->next, typeof(*pos), member);	\
	     &pos->member != (head); 	\
	     pos = list_entry(pos->member.next, typeof(*pos), member))

内核链表的使用

struct student {
    char name[20];
    int age;
    struct list_head list;
};

int main()
{
    //struct list_head stu_head = {&stu_head, &stu_head};
    struct list_head stu_head;
    INIT_LIST_HEAD(&stu_head);

    struct student *stu1;
    stu1 = malloc(sizeof(*stu1));
    strcpy(stu1->name, "jack");
    stu1->age = 20;

    struct student *stu2;
    stu2 = malloc(sizeof(*stu2));
    strcpy(stu2->name, "bob");
    stu2->age = 30;

    struct student *stu3;
    stu3 = malloc(sizeof(*stu3));
    strcpy(stu3->name, "bobc");
    stu3->age = 31;

    list_add(&stu1->list, &stu_head);
    list_add(&stu2->list, &stu_head);
    list_add(&stu3->list, &stu_head);

    struct student *stu;
    list_for_each_entry(stu, &stu_head, list) {
        printf("stu->name=%s\n",stu->name);
    }
	return 0;
}

输出如下:

stu->name=bobc
stu->name=bob
stu->name=jack

可以看到,linux内核中链表操作的都是list_head结构,那么它是怎样通过list_head来访问对应的数据结构里的成员的呢?

Liunx内核链表的原理

从上面的代码可以知道,遍历是通过list_for_each_entry宏来实现的,该宏是一个for循环,可以使用gcc的预编译命令来查看list_for_each_entry展开后的代码:

gcc -E -o main.i main.c

查看main.i中宏的位置,展开后如下:

    struct student *stu;
    for (stu = ({ const typeof( ((typeof(*stu) *)0)->list ) *__mptr = ((&stu_head)->next); (typeof(*stu) *)( (char *)__mptr - ((size_t) &((typeof(*stu) *)0)->list) );}); &stu->list != (&stu_head); stu = ({ const typeof( ((typeof(*stu) *)0)->list ) *__mptr = (stu->list.next); (typeof(*stu) *)( (char *)__mptr - ((size_t) &((typeof(*stu) *)0)->list) );})) {
        printf("stu->name=%s\n",stu->name);
    }

展开后分为for循环中的三个部分:

stu = ({ const typeof( ((typeof(*stu) *)0)->list ) *__mptr = ((&stu_head)->next); (typeof(*stu) *)( (char *)__mptr - ((size_t) &((typeof(*stu) *)0)->list) );})
&stu->list != (&stu_head);
stu = ({ const typeof( ((typeof(*stu) *)0)->list ) *__mptr = (stu->list.next); (typeof(*stu) *)( (char *)__mptr - ((size_t) &((typeof(*stu) *)0)->list) );})

分别对应for语句中的三份代码,可以看到,遍历的终止条件是&stu->list != (&stu_head),如果&stu->list = (&stu_head),说明遍历到头节点了,循环结束,下面简化一下第一份代码:

const list_head *__mptr = ((&stu_head)->next);
(struct student *)( (char *)__mptr - ((size_t) &((struct student *)0)->list);

可以看到, 最终stu的值是:

(struct student *)( (char *)__mptr - ((size_t) &((struct student *)0)->list);

将第一步细化实现如下:

    /*手动实现从list_head访问student*/
    struct list_head *mptr = stu_head.next;
    unsigned long long offset = (unsigned long long)(&(((struct student *)0)->list));
    mptr = (unsigned long long)mptr - offset;
    stu = (struct student *)mptr;

    printf("stu = %s %p\n", stu->name, stu);
    printf("stu1 = %p\n", stu1);
    printf("stu2 = %p\n", stu2);
    printf("stu3 = %p\n", stu3);

输出如下:

stu = bobc 0x55d8023972c0
stu1 = 0x55d802397260
stu2 = 0x55d802397290
stu3 = 0x55d8023972c0

可以看到,stu最终等于stu3,因为stu3最后一个插入。画了一份草图便于理解:
在这里插入图片描述
可以看到,linux能够实现从list_head访问到student主要是使用了offset,offset是student结构体的起始地址到list_head的大小,再使用list_head的地址减去这段大小即可得到该结构体的起始地址,从而访问结构体成员。

代码链接

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值