Linux内核数据结构——链表
来源:互联网 发布:北京北大软件上班 编辑:程序博客网 时间:2024/05/24 03:54
目录
- 目录
- 简介
- 单向链表
- 双向链表
- 环形链表
- Linux内核中的链表实现
- offsetof
- container_of
- container_of 第一部分
- container_of 第二部分
- 链表初始化
- 向链表中增加一个节点
- 删除节点
- 移动节点
- 判断链表是否为空
- 遍历链表
- Demo测试
- mlisth
- mlistc
- 执行结果
简介
最近在学习Android Binder驱动程序实现的时候,发现里面的数据结构用到了struct list_head。而我google发现struct list_head是Linux内核实现的链表数据结构。本身我就对数据结构这块比较感兴趣,正好借这个机会学习一下Linux内核中链表的实现。(ps:主要是堆积代码,自我学习哈,大家吐槽请轻轻吐)。
链表是一种常用的组织有序数据的数据结构,它通过指针将一系列数据节点连接成一条数据链,是线性表的一种重要实现方式。链表和的静态数据的不同之处在于,它所包含的元素都是动态创建并插入链表的,在编译时不必知道具体需要创建多少个元素。另外,也因为链表中每个元素的创建时间各不相同,所以它们在内存中无须占用连续内存区。正是因为链表中的元素不连续存放,所以各元素需要通过某种方式被连接在一起。于是,每个元素都包含一个指向下一个元素的指针,当有元素加入链表或从链表中删除元素元素时,只需要简单的调整节点指针就可以了。
由于链表是一种非常基本的数据结构,我先简单介绍一下大学数据结构课本中是如何描述这种数据结构的。
单向链表
可以用一种最简单的数据结构来表示这样一个链表:
struct list_element { void *data; /*有效数据*/ struct list_element *next; /*指向下一个元素的指针*/};
下图描述了一个单链表结构:
双向链表
在有些链表中,每个元素还包含一个指向前一个元素的指针,因为它们可以同时向前或者向后相互连接,所以这种链表被称作双向链表。表示双向链表的数据结构如下:
struct list_element { void* data; /*有效数据*/ struct list_element *next; /*指向下一个元素的指针*/ struct list_element *prev; /*指向上一个元素的指针*/};
下图描述了一个双向链表结构体:
环形链表
通常情况下,因为链表中最后一个元素不再有下一个元素,所以将链表尾元素中的后指针设置为NULL,以此证明它是链表中的最后一个元素。但是,在有些链表中,末尾元素并不指向特殊值,相反,它指回链表的首元素。并且,在环形双向链表中,首节点的前驱节点指向尾节点。
因为环形双向链表提供了最大的灵活性,所以Linux内核标准的链表就是环形双向链表。
Linux内核中的链表实现
在介绍Linux内核链表的源码实现之前,我们先来学习一下两个非常重要的宏:offsetof和container_of。
offsetof
它定义在include/linux/stddef.h中,其源码如下:
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
解释一下这个宏的运行原理:
- ((TYPE *) 0):将0转型为TYPE类型的指针。
- ((TYPE *)0)->MEMBER:访问结构中的数据成员。
- &((TYPE *)0)->MEMBER:取出数据成员的地址。
- (size_t)&((TYPE )0)->MEMBER:结果转换类型。巧妙之处在于将0转换为(TYPE ),以内存空间首地址0作为起始地址,则成员地址自然为偏移地址。
所以这个宏的作用是:计算TYPE类型中MEMBER成员的内存偏移量。
container_of
它定义在include/lunux/kernel.h中,其源码如下:
/** * 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)); })
这里需要简单的解释一下container_of宏。这个宏的作用很简单,就是通过一个容器(结构体)中某个成员的指针得到指向这个容器(结构体)的指针。这个宏的实现代码只有两行,我们逐行的分析一下container_of宏的实现。
container_of 第一部分
const typeof(((type *)0)->member)* __mptr = (ptr);
这里需要解释几点:
- typeof是GNU C对标准C的扩展,它的作用是根据变量获取变量类型。
- typeof获取了type结构体的member成员的变量类型,然后定义了一个指向该变量类型的指针__mptr,并将实际结构体该成员变量的指针的值赋值给__mptr。
- 通过这行代码,临时变量__mptr中保存了type结构体成员变量member在内存中分配的地址值。
container_of 第二部分
(type*)((char*)__mptr - offsetof(type, member));
这块代码是利用__mptr的地址,减去type结构体成员member相对于结构体首地址的偏移地址,得到的自然是结构体首地址,即该结构体指针。
链表初始化
Linux内核实现链表的方式可以说是独树一帜。简介中提到的链表数据结构,都是需要在具体数据的结构体内部增加一个指向数据的next(或者prev)节点指针,才能串联在链表中。但是,Linux的内核实现方式与众不同,它不是将整个数据结构塞入链表,而是将链表节点塞入数据结构。
链表代码在头文件include/linux/types.h中声明,其数据结构很简单:
struct list_head { struct list_head *next, *prev;};
next是指向下一个链表节点,prev是指向前一个链表节点。然而,这里似乎看不出它们有多大的作用。到底什么才是链表存储的具体内容呢?其实关键在于理解list_head结构是如何使用的。
接下来介绍一下链表头节点初始化的源码,定义在include/linux/list.h中:
/** * Simple doubly linked list implementation. * * Some of the internal functions ("__xxx") are useful when * manipulating whole lists rather than single entries, as * sometimes we already know the next/prev entries and we can * generate better code by using them directly rather than * using the generic single-entry routines. */#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;}
LIST_HEAD宏创建一个链表头节点,并用LIST_HEAD_INIT宏对头节点进行赋值,使得头节点的前驱指针和后继指针均指向自己。
INIT_LIST_HEAD函数也是对链表头节点进行初始化,使得头节点list的前驱和后继指针均指向自己。
向链表中增加一个节点
/** * Insert a new entry between two known consecutive entries. * * This is only for internal list manipulation where we know * the prev/next entries already! */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 * * 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_tail - add a new entry * @new: new entry to be added * @head: list head to add it before * * Insert a new entry before the specified head. * This is useful for implementing queues. */static inline void list_add_tail(struct list_head *new, struct list_head *head){ __list_add(new, head->prev, head);}
插入节点分为从链表头部插入list_add和从链表尾部插入list_add_tail,这两个函数均是通过调用__list_add函数来实现的。
删除节点
/** * 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;}/** * list_del - deletes entry from list. * @entry: the element to delete from the list. * Note: list_empty() on entry does not return true after this, the entry is * in an undefined state. */static inline void list_del(struct list_head *entry){ __list_del(entry->prev, entry->next); entry->next = LIST_POISON1; entry->prev = LIST_POISON2;}
需要注意的是:该函数并没有接收list的头节点,它只是接受一个特定的需要删除的节点,并修改该特定节点的前后指针,这样该节点就从链表中删除了。
移动节点
/** * list_move - delete from one list and add as another's head * @list: the entry to move * @head: the head that will precede our entry */static inline void list_move(struct list_head *list, struct list_head *head){ __list_del(list); list_add(list, head);}
该函数从链表中先移除list节点,然后将其加入到一个以head节点为头节点的链表中。
/** * list_move_tail - delete from one list and add as another's tail * @list: the entry to move * @head: the head that will follow our entry */static inline void list_move_tail(struct list_head *list, struct list_head *head){ __list_del(list); list_add_tail(list, head);}
该函数从一个链表中移除list项,然后将其加入到以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;}
这个函数用来判断链表是否为空。
遍历链表
前面介绍了内核中如何初始化、插入、删除一个链表节点。但是,如果无法访问链表中的数据,那这些操作就没有任何意义。链表仅仅是一个能够包含重要数据的容器,我们必须利用链表移动并访问包含我们数据的结构体。
注意:链表操作的时间复杂度为O(1),遍历链表的复杂度为O(n),n是链表所包含的元素数目。
遍历链表最简单的方法是使用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_head类型参数,第一个参数用来指向当前项,这是一个你必须提供的临时变量,第二个参数是需要遍历的链表的头节点。每次遍历中,第一个参数在链表中不断移动指向下一个元素,直到链表中的所有元素都被访问为止。
但是,这里一个指向链表结构的指针通常是无用的,我们所需要的是一个指向包含list_head的结构体的指针。这时,我们就可以用之前讨论的container_of方法,来获取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_struct within the struct. */#define list_entry(ptr, type, member) \ container_of(ptr, type, member)/** * 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. */#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))
Demo测试
这里我也编写一个简单的链表使用程序,参考了Linux 内核链表的实现。
mlist.h
#define POISON_POINTER_DELTA 0#define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)#define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)#define offsetof(type, member) \ (size_t)(&((type*)0)->member)#define container_of(ptr, type, member) ({ \ const typeof(((type *)0)->member)* __mptr = (ptr); \ (type *)((char *)__mptr - offsetof(type, member)); })struct list_head{ struct list_head *next, *prev;};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->prev = LIST_POISON1; entry->next = LIST_POISON2;}static inline void list_move(struct list_head *list, struct list_head *head){ list_del(list); list_add(list, head);}static inline void list_move_tail(struct list_head *list, struct list_head *head){ list_del(list); list_add_tail(list, head);}static inline int list_empty(const struct list_head *head){ return head->next == head;}#define list_entry(ptr, type, member) \ container_of(ptr, type, member)#define list_first_entry(ptr, type, member) \ list_entry((ptr)->next, type, member)#define list_for_each(pos, head) \ for(pos = (head)->next; pos != (head); pos = pos->next)
mlist.c
#include <stdio.h>#include <string.h>#include <stdlib.h>#include "mlist.h"struct fox { int tail_length; int weight; struct list_head list_node;};struct fox* create_fox(int length, int weight){ struct fox* tmp_fox = (struct fox*)malloc(sizeof(struct fox)); if (tmp_fox == NULL) { return NULL; } tmp_fox->tail_length = length; tmp_fox->weight = weight; return tmp_fox;}static inline void for_each_fox(const struct list_head* head){ struct list_head *pos; struct fox *tmp_fox; list_for_each(pos, head) { tmp_fox = list_entry(pos, struct fox, list_node); printf("fox info: %d --- %d\n", tmp_fox->tail_length, tmp_fox->weight); }}int main(){ struct fox * fox_list = (struct fox *)malloc(sizeof(struct fox)); struct fox * fox; if (fox_list == NULL) { printf("malloc failed!"); return -1; } // 初始化链表头节点 struct list_head *head = &(fox_list->list_node); INIT_LIST_HEAD(head); printf("初始化头节点完成!\n"); // 插入4个fox fox = create_fox(100, 100); list_add_tail(&(fox->list_node), head); fox = create_fox(105, 105); list_add_tail(&(fox->list_node), head); fox = create_fox(110, 110); list_add_tail(&(fox->list_node), head); fox = create_fox(115, 115); list_add_tail(&(fox->list_node), head); printf("插入4个狐狸节点\n"); // 遍历 for_each_fox(head); // 移动节点 list_move_tail(head->next, head); printf("将第一个节点移动到末尾\n"); for_each_fox(head); // 删除节点 list_del(head->next); printf("删除当前第一个节点\n"); for_each_fox(head); return 0;}
执行结果
执行结果如图所示:
- Linux内核数据结构—链表
- Linux内核数据结构——链表
- linux内核数据结构—kfifo
- Linux内核常见数据结构及操作——链表
- 内核数据结构——链表
- linux内核数据结构---链表(1)
- Linux内核之数据结构--链表
- Linux内核常见数据结构及操作——HASH表
- 有趣的数据结构——Linux内核中的链表
- Linux内核常用数据结构——顺序表之哈希表
- 有趣的数据结构——Linux内核中的链表
- 有趣的数据结构——Linux内核中的链表
- 《Linux内核设计与实现》——内部数据结构
- linux 内核常用数据结构及算法——container_of
- Linux内核工程导论——数据结构:树
- Linux内核工程导论——数据结构:其他
- Linux内核的一种数据结构——task_struct
- 关于linux内核的数据结构——list_head
- Java NIO系列教程(十二) Java NIO与IO
- 取出数组中的重复数字 和 不重复的数量和去重
- CSS框架Less
- Windows7下安装scipy
- scrapy抓取网页信息插入MySQL
- Linux内核数据结构——链表
- Spring MVC防止数据重复提交
- 百度云平台使用说明
- Apache2 Nginx配置多域名
- 北大ACM试题1005
- linux下绑定host
- PKI技术原理(收集 整理 归纳)
- mac 10.10.1 下要用android-ndk-r10c-macosx版本
- Qt编译MySql驱动