Chinaunix首页 | 论坛 | 博客
  • 博客访问: 894715
  • 博文数量: 119
  • 博客积分: 2493
  • 博客等级: 大尉
  • 技术积分: 2363
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-03 14:00
文章分类

全部博文(119)

文章存档

2013年(19)

2012年(100)

分类: LINUX

2012-08-01 16:02:13

    linux内核中有很多用的很经典的数据结构,链表就算其中之一,还有队列,
哈希查找,红黑树查找等等,链表的设计很经典,就连很多开发内核的黑客们
都觉得内核中链表的设计是他们引以自豪的一部分。我觉得内核链表的好主要
体现为两点,1是可扩展性,2是封装。可扩展性肯定是必须的,内核一直都是
在发展中的,所以代码都不能写成死代码,要方便修改和追加。将链表常见的
操作都进行封装,使用者只关注接口,不需关注实现。分析内核中的链表我们
可以做些什么呢?我觉得可以将其复用到用户态编程中,以后在用户态下编程
就不需要写一些关于链表的代码了,直接将内核中list.h中的代码拷贝过来用。
也可以整理出my_list.h,在以后的用户态编程中直接将其包含到C文件中。
-------------------------------------------------------------------------------------------
1,链表的创建和初始化。
     内核的链表一般都是双向循环链表,学过数据结构的人都知道,双向循环
链表的效率是最高的,找头节点,尾节点,直接前驱,直接后继时间复杂度都是O(1),而使用单链表,单向循环链表或其他形式的链表是不能完成的。
其实内存中开辟出一个头节点然后将其初始化好就完成了链表的创建。

  1. static inline void INIT_LIST_HEAD(struct list_head *list)
  2. {
  3.     list->next = list;
  4.     list->prev = list;
  5. }
开辟好头节点后,然后将结构体中的next域和prev域都指向自己即OK。
例如:(用户态下一个例子)

  1. struct stu {
  2.     int num;
  3.     char name[20];
  4.     struct list_head list;
  5. };
  1.    
  2. struct stu *head = (struct stu *)malloc(sizeof(struct stu));
  3. if (head == NULL) {
  4.     printf("file,%s line,%d:malloc error!\n",__FILE__,__LINE__);
  5.     exit(1);
  6. }
  7. INIT_LIST_HEAD(&head->list);
上面的代码就完成了一个双向循环链表的创建。图示如下:

-------------------------------------------------------------------------------------------
2.链表的节点的插入。上面建立的只是一个空链表,肯定要向链表中插入一些
节点,插入的方式很多,下面主要分析list_add_tail函数,该函数实现的是
“尾插”。

  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.       prev->next = new;
  9. }

  10. /**
  11.  * list_add_tail - add a new entry
  12.  * @new: new entry to be added
  13.  * @head: list head to add it before
  14.  *
  15.  * Insert a new entry before the specified head.
  16.  * This is useful for implementing queues.
  17.  */
  18.  static inline void list_add_tail(struct list_head *new, struct list_head *head)
  19.  {
  20.       __list_add(new, head->prev, head);
  21.  }
其实,双向循环链表插入节点很简单,也不需要辅助指针,就几个指针变来变去的,画个图就清楚了
如果光靠脑子还不好想。示意图如下:



-------------------------------------------------------------------------------------------
3,链表的遍历。
      遍历就是使用线性表的方式吧链表上的每个节点都走一遍,内核中定
义了一个宏来实现。
  1. #define list_for_each(pos, head) \
  2.      for (pos = (head)->next; prefetch(pos->next), pos != (head); \
  3.              pos = pos->next)
可以看出,使用了辅助指针pos,pos是从第一节点开始的,并没有访问头节点,
直到pos到达头节点指针head的时候结束。

  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_struct within the struct.
  6.   */
  7. #define list_entry(ptr, type, member) \
  8.        container_of(ptr, type, member)
  1. #ifndef container_of
  2. /**
  3.  * container_of - cast a member of a structure out to the containing structure
  4.  *
  5.  * @ptr: the pointer to the member.
  6.  * @type: the type of the container struct this is embedded in.
  7.  * @member: the name of the member within the struct.
  8.  *
  9.  */
  10. #define container_of(ptr, type, member) ({ \
  11.          const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  12.           (type *)( (char *)__mptr - offsetof(type,member) );})
  13. #endif
list_entry主要是实现从内层结构体返回到外层结构体,就拿上面的struct stu
结构体来说吧,现在假如知道struct stu内部list结构体的指针pos,如何来获得
该结构体对应的struct stu的结构体指针?这儿主要是使用container_of这个宏
来实现的,该宏主要是用了一个技巧,主要是求出内层结构体字段到外层的偏移量,计算的方式是,将外层结构体的地址设置为0,然后就有,该字段的地址 = 
该字段到外层结构体的偏移量。


更详细的解释见:

示意图如下:
遍历的时候都是遍历pos,然后再由pos返回到pnode。然后就可以访问
strudent中的任何一个字段了。

------------------------------------------------------------------------------------------
4,链表的释放。
     由于链表的节点都是动态开辟的空间,必须要释放,不然容易造成内存泄漏。
 释放也是一个节点一个节点的释放,故也需要遍历链表。  

  1. /**
  2.  * list_for_each_safe - iterate over a list safe against removal of list entry
  3.  * @pos: the &struct list_head to use as a loop cursor.
  4.  * @n: another &struct list_head to use as temporary storage
  5.  * @head: the head for your list.
  6.  */
  7. #define list_for_each_safe(pos, n, head) \
  8.     for (pos = (head)->next, n = pos->next; pos != (head); \
  9.         pos = n, n = pos->next)
该遍历链表和一般的链表遍历不一样,这时候是释放整条链表,必须要重
新引入一个辅助指针n,如果使用list_for_each就会出错。
  1. static inline void __list_del(struct list_head * prev, struct list_head * next)
  2. {
  3.      next->prev = prev;
  4.      prev->next = next;
  5. }


  6. static inline void list_del(struct list_head *entry)
  7. {
  8.      __list_del(entry->prev, entry->next);
  9.      entry->next = LIST_POISON1;
  10.      entry->prev = LIST_POISON2;
  11. }
list_del将会将该节点与外界的“联系”切断,然后就可以使用free释放了
(如果是内核态就用kfree或vfree)。

最后将会只剩下头节点,再将头节点释放,将完成了整条链表的释放。
-------------------------------------------------------------------------------------------
5,用户态使用内核链表的实例:(完整代码)
 kernel_list_for_user.rar  

--------------------------------------------------------------------------------------------
6,参考:  


阅读(16541) | 评论(4) | 转发(18) |
给主人留下些什么吧!~~

Otto05562015-10-31 16:18:55

stu.c文件中最后部分:
//---------------------------------------------
...
list_for_each_safe(pos,n,&head->list) {
 list_del(pos);
 pnode = list_entry(pos,struct stu,list);
 printf(\"num %d has removed from the list!\\n\",pnode->num);
}free(pnode);

free(head);
return 0;
//--------------------------
free(pnode);是不是要放到里面,不然内存会泄漏

zhe_wang2013-06-10 16:55:02

kangear:不过你提取的list.h来自由哪个版本内核的呢?

内核版本中的list.h变化不大。我用的是2.6.39系列的。

回复 | 举报

kangear2013-06-10 10:29:58

不过你提取的list.h来自由哪个版本内核的呢?

kangear2013-06-10 10:21:51

Good,链表就是要会用,就用内核中的就OK了,这个确实是好的。