Linux Kernel数据结构:链表

来源:互联网 发布:php websocket server 编辑:程序博客网 时间:2024/05/20 06:07

Linux Kernel数据结构:链表

原创 2016年10月20日 22:58:25

数据结构中链表是 节点中包含数据 , kernel中的链表是链表包含在数据结构中

内核链表的优势

尽可能的代码重用,将大堆的链表设计变为一个链表操作就可以搞定,总结起来可以为可扩展性,封装性。在数据结构的中的链表一般情况下都是一个节点中包含数据域和指针域,数据域用于存储数据信息,指针域用于连接下一个节点,不过这样的话有一个弊端就是当我设计一个学生信息链表时,我需要写一套关于这个链表的操作函数,假设我再设计一个老师信息的链表时,我又要写一套老师信息链表的操作函数,这样如果很多的话,做起来非常费劲。kernel中的链表设计就巧妙的避开了这个弊端。

链表的构造

如果需要构造某个结构的链表,则在这个结构中定义一个类型为list_head的指针成员,通过这个成员将每个结构连接起来,形成链表,通过通用的链表函数来进行操作。有点可想而知,这个通用的链表操作函数可以搞定所有的链表,实现了代码的重用。如果想得到对应结构的指针,可以使用list_entry计算出来。

比如我这边有一个单纯的结构,我想让他组成一个链表。

[cpp] view plain copy
  1. struct mystruct {  
  2.      int data ;  
  3. } ;  
在结构体内部增加一个list_head, list_head为
[cpp] view plain copy
  1. struct list_head {  
  2.     struct list_head *next, *prev;  
  3. };  
组合之后为
[cpp] view plain copy
  1. struct mystruct {  
  2.      int data ;  
  3.      struct list_head mylist ;  
  4. } ;  
初始化第一个变量
[cpp] view plain copy
  1. struct mystruct first = {  
  2.      .data = 10,  
  3.      .mylist = LIST_HEAD_INIT(first.mylist)  
  4. } ;  
data = 10 , LIST_HEAD_INIT 实际上是让这个mylist的变量自己指向自己了.
[cpp] view plain copy
  1. #define LIST_HEAD_INIT(name) { &(name), &(name) }   
接下来再加入第二个变量second ,data = 20
[cpp] view plain copy
  1. struct mystruct second ;  
  2. second.data = 20 ;  
  3. INIT_LIST_HEAD( & second.mylist ) ;  
INIT_LIST_HEAD初始化指向自己
[cpp] view plain copy
  1. static inline void INIT_LIST_HEAD(struct list_head *list)  
  2. {  
  3.     WRITE_ONCE(list->next, list);  
  4.     list->prev = list;  
  5. }  
以上我们声明并初始化了两个data,然后这个地方我们需要一个头list_head
[cpp] view plain copy
  1. LIST_HEAD(mylinkedlist) ;  
[cpp] view plain copy
  1. #define LIST_HEAD(name) \  
  2.     struct list_head name = LIST_HEAD_INIT(name)  

链表节点初始化之后如下图


链表的插入

看下如何将first,second插入到链表mylinkedlist中

[cpp] view plain copy
  1. list_add ( &first.mylist , &mylinkedlist ) ;  
  2. list_add ( &second.mylist , &mylinkedlist ) ;  
看下list_add的操作:
[cpp] view plain copy
  1. /** 
  2.  * list_add - add a new entry 
  3.  * @new: new entry to be added 
  4.  * @head: list head to add it after 
  5.  * 
  6.  * Insert a new entry after the specified head. 
  7.  * This is good for implementing stacks. 
  8.  */  
  9. static inline void list_add(struct list_head *newstruct list_head *head)  
  10. {  
  11.     __list_add(new, head, head->next);  
  12. }  
__list_add
[cpp] view plain copy
  1. static inline void __list_add(struct list_head *new,  
  2.                   struct list_head *prev,  
  3.                   struct list_head *next)  
  4. {  
  5.     next->prev = new;  
  6.     new->next = next;  
  7.     new->prev = prev;  
  8.     WRITE_ONCE(prev->next, new);  
  9. }  
可以使用一个图来表示:


这里list_add是从表头插入的,kernel也提供了一种从表尾插入的方式

[cpp] view plain copy
  1. /** 
  2.  * list_add_tail - add a new entry 
  3.  * @new: new entry to be added 
  4.  * @head: list head to add it before 
  5.  * 
  6.  * Insert a new entry before the specified head. 
  7.  * This is useful for implementing queues. 
  8.  */  
  9. static inline void list_add_tail(struct list_head *newstruct list_head *head)  
  10. {  
  11.     __list_add(new, head->prev, head);  
  12. }  

遍历

将链表上所有的元素都走一遍,内核中也提供一个宏来实现这个功能,从下面的宏可以看出, pos指向的是第一个元素,如果pos不等于head(如果等于head的话,应该是到了表尾的时候了),获取到这个节点之后,可以进行操作,操作完毕之后移到下一个节点。

[cpp] view plain copy
  1. /** 
  2.  * list_for_each    -   iterate over a list 
  3.  * @pos:    the &struct list_head to use as a loop cursor. 
  4.  * @head:   the head for your list. 
  5.  */  
  6. #define list_for_each(pos, head) \  
  7.     for (pos = (head)->next; pos != (head); pos = pos->next)  
从上面看出实际上我们拿到的都是struct list_head的位置,并没有拿到数据域,如何拿到数据域,也就是整个struct的指针,kernel也给出了接口
[cpp] view plain copy
  1. /** 
  2.  * list_entry - get the struct for this entry 
  3.  * @ptr:    the &struct list_head pointer. 
  4.  * @type:   the type of the struct this is embedded in. 
  5.  * @member: the name of the list_head within the struct. 
  6.  */  
  7. #define list_entry(ptr, type, member) \  
  8.     container_of(ptr, type, member)  
container_of就是通过数据结果中的一个元素计算出数据的第一个元素的地址
[cpp] view plain copy
  1. /** 
  2.  * container_of - cast a member of a structure out to the containing structure 
  3.  * @ptr:    the pointer to the member. 
  4.  * @type:   the type of the container struct this is embedded in. 
  5.  * @member: the name of the member within the struct. 
  6.  * 
  7.  */  
  8. #define container_of(ptr, type, member) ({          \  
  9.     const typeof( ((type *)0)->member ) *__mptr = (ptr); \  
  10.     (type *)( (char *)__mptr - offsetof(type,member) );})  

  1.首先定义一个临时的数据类型(通过typeof( ((type *)0)->member )获得)与ptr相同的指针变量__mptr,然后用它来保存ptr的值。

  2.用(char *)__mptr减去member在结构体中的偏移量,得到的值就是整个结构体变量的首地址(整个宏的返回值就是这个首地址)。

获取到数据域,就可以获得到数据信息了,如下面,但因出每一个节点的data值。
[cpp] view plain copy
  1. struct list_head *position = NULL ;   
  2. struct mystruct  *datastructureptr  = NULL ;   
  3. list_for_each ( position , & mylinkedlist )   
  4.     {   
  5.          datastructureptr = list_entry ( position, struct mystruct , mylist );   
  6.          printk ("data  =  %d\n" , datastructureptr->data );   
  7.     }  
kernel同时也提供了这样一个比较简单的宏
[cpp] view plain copy
  1. /** 
  2.  * list_for_each_entry  -   iterate over list of given type 
  3.  * @pos:    the type * to use as a loop cursor. 
  4.  * @head:   the head for your list. 
  5.  * @member: the name of the list_head within the struct. 
  6.  */  
  7. #define list_for_each_entry(pos, head, member)              \  
  8.     for (pos = list_first_entry(head, typeof(*pos), member);    \  
  9.          &pos->member != (head);                 \  
  10.          pos = list_next_entry(pos, member))  

链表的释放

释放就比较简单了,首先要就是断掉prev和next的联系,让二者关联起来。

[cpp] view plain copy
  1. static inline void list_del(struct list_head *entry)  
  2. {  
  3.     __list_del(entry->prev, entry->next);  
  4.     entry->next = LIST_POISON1;  
  5.     entry->prev = LIST_POISON2;  
  6. }  
[cpp] view plain copy
  1. /* 
  2.  * Delete a list entry by making the prev/next entries 
  3.  * point to each other. 
  4.  * 
  5.  * This is only for internal list manipulation where we know 
  6.  * the prev/next entries already! 
  7.  */  
  8. static inline void __list_del(struct list_head * prev, struct list_head * next)  
  9. {  
  10.     next->prev = prev;  
  11.     WRITE_ONCE(prev->next, next);  
  12. }  

当然链表还提供了很多相关的接口,实现在kernelxx/include/linux/list.h中,可以参阅。

原创粉丝点击