linux数据结构

来源:互联网 发布:手机上能注册淘宝店铺 编辑:程序博客网 时间:2024/06/05 17:27

链表,队列,映射,二叉树等数据结构是程序设计中常用的数据结构。为了统一这些数据结构的操作接口,Linux内核开发者实现了一些标准的操作接口及实现(使用了大量的GNU扩展特性),以达到代码重用,开发者应该尽量使用这些标准接口,避免实现自己的再创造,虽然那样看起来很酷,很有劲。

 

有关链表

 

传统的双向链表实现方法是在链表元素中加入两个指针,然后用这些指针来构造双向链表。如下所示

[cpp] view plaincopy
  1. struct node{  
  2.     value_type value;  
  3.     struct element *prev;  
  4.     struct element *next;  
  5. }node, *list_head;  

其示意图如下:NULL为空指针。

 

如果将双向链表中的首尾两个元素进行链接,则会形成循环双向链表。示意图如下:

 

由上可以看出,如果想得到链表中某个指定节点,必须要遍历链表。所以,对于那些需要随机存取的数据,尽量使用数组,而不是链表,当然也可以配合一个哈希表来使用链表,有兴趣的同志可以看看。

 

上面的实现方法没有问题,但是对于内核来说,如果每个内核对象都采用这种方法,那么要为每个结构体添加相应代码,还要实现其链表操作函数。这样很麻烦,而且也不能达到代码复用以及提供统一的接口。所以内核开发者采用了另外一种巧妙的方法:声明list_head这么一个结构,然后只要嵌入这么一个数据结构就可以实现双向链表了。

[c-sharp] view plaincopy
  1. struct list_head{  
  2.     struct list_head *next;  
  3.     struct list_head *prev;  
  4. };  


假设你想以链表形式存储自己的课程与成绩,则可以采用下面的形式

[c-sharp] view plaincopy
  1. struct cource_score{  
  2.     int num;  
  3.     int score;  
  4.     struct list_head list;  
  5. };  

这样,就利用成员变量list将所有的链表节点连接起来,当然,一般还要设置一个头结点。


除此之外,开发者还提供了一些函数和宏用于链表操作。如使用container_of()可以通过list成员获得course_score结构体的地址。

[c-sharp] view plaincopy
  1. #define container_of(ptr, type, member) ({                                   /  
  2.                const typeof( ((type *)0)->member ) *_mptr = (ptr);      /  
  3.                (type *)( (char *)_mptr - offsetof(type, member) ) ;})  
  4.  
  5. #define offsetof(type, member) ((ssize_t)&(type *)0)->member)  
  6.  
  7. #define list_entry(ptr, type, member) /  
  8.               constainer_of(ptr, type, member)  

宏container_of使用了GNU扩展。分析一下这个宏定义,首先注意ptr为指向容器结构体的指针,type为容器结构体的类型,而member则是其内嵌的list_head成员变量的名称,如上例,type为course_score,而member则为list。

(type *)0将地址0转换为结构type的地址

(type *)0->member获取type机构体中member成员变量的偏移地址。由于容器结构体的地址为0,这就是member成员的偏移地址,所以宏offsetof也就是这个作用。

typeof(  ((type *)0->member)返回member成员的类型,然后将指针_mptr声明为该类型的指针,并赋值为ptr。

(type *)((char *)_mptr - offsetof(type, member));})则根据member成员的实际_mptr以及偏移量offsetof()则可以得到容器结构体的地址。

 

当有两种方法可以初始化链表,静态初始化和动态初始化。

 

 

[c-sharp] view plaincopy
  1. #define LIST_HEAD_INIT(name) {&(name),&name)}     
  2. #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)    
  3.  
  4. #define INIT_LIST_HEAD(ptr) do{/     
  5.         (ptr)->next = (ptr);(ptr)->next = (ptr);/    
  6. }while (0)    
  7.   
  8. static  inline void  INIT_LIST_HEAD(struct  list_head * list)    
  9. {    
  10.         list->next = list;    
  11.         list->prev = list;    
  12. }  

如果在编码时初始化链表,则可以使用宏LIST_HEAD_INIT,如上例中,则可以

[c-sharp] view plaincopy
  1. struct course_score math = {  
  2.     num = 1;  
  3.     score = 60;  
  4.     list = LIST_HEAD_INIT(math.list);  
  5. };  

 

如果是运行时初始化链表,则可以使用宏INIT_LIST_HEAD或者内联函数INIT_LIST_HEAD来初始化,两者功能一样,只是内联函数提供了类型检测。如下所示

[c-sharp] view plaincopy
  1. struct course_score *math;  
  2. math = kmalloc(sizeof(struct course_score));  
  3. math->num = 1;  
  4. math->score = 60;  
  5. INIT_HEAD_LIST(&math->list);  

 


链表的其他操作包括添加、删除、合并、遍历等。

插入节点

[c-sharp] view plaincopy
  1. static  inline void  list_add(struct  list_head * new , struct  list_head * head)    
  2. {    
  3.        __list_add(new , head, head->next);    
  4. }    
  5.   
  6. static  inline void  list_add_tail(struct  list_head * new ,struct  list_head * head)    
  7. {    
  8.        __list_add(new , head->prev, head);    
  9. }   
  10.   
  11. struct  inline void  __list_add(struct  list_head * new , struct  list_head * prev, struct  list_head * next)    
  12. {    
  13.        next->prev = new ;    
  14.        new ->next = next;    
  15.        new ->prev = prev;    
  16.        prev->next = new ;    
  17. }  

插入操作有两种:表头插入和表尾插入。实际上,两种插入的方法是一样的,只是内部函数调用时,参数不同而已。

 

 

删除节点

[c-sharp] view plaincopy
  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.   
  7. static inline void list_del(struct list_head * entry)    
  8. {    
  9.         __list_del(entry->prev, entry->next);    
  10.         entry->next = LIST_POSITION;    
  11.         entry->prev = LIST_POSITION;    
  12. }    

 

对 LIST_POISON1,LIST_POISON2 的解释:

These are non-NULL pointers that will result in page faults under normal circumstances, used to verify that nobody uses  non-initialized list entries.

[c-sharp] view plaincopy
  1. #define LIST_POISON1  ((void *) 0x00100100)  
  2. #define LIST_POISON2  ((void *) 0x00200200)  

 

 

移动节点

[c-sharp] view plaincopy
  1. static inline void list_move(struct list_head * list, struct list_head * head)    
  2. {    
  3.     __list_del(list->prev, list->next);    
  4.     list_add(list, head);    
  5. }   
  6.   
  7. static inline void list_move_tail(struct list_head *  list, struct list_head *  head)  
  8. {  
  9.     __list_del(list->prev, list->next);  
  10.     list_add_tail(list, head);     
  11. }  

 

链表合并

[c-sharp] view plaincopy
  1. static inline void _list_splice(struct list_head * list, struct list_head * head)  
  2. {  
  3.     struct list_head * first = list->next;  
  4.     struct list_head * last = list->prev;  
  5.     struct list_head * at = head->next;  
  6.   
  7.     first->prev = head;  
  8.     head->next = first;  
  9.   
  10.     last->next = at;  
  11.     at->prev = last;  
  12. }  
  13.   
  14. static inline void list_splice(struct list_head * list, struct list_head * head)  
  15. {  
  16.     if(!list_empty(list))  
  17.         __list_splice(list, head);  
  18. }   
  

 

将一个非空链表插入到另外一个链表中。先做链表是否为空的检查,因为每个链表只有一个头节点,将空链表插入到另外一个链表中是没有意义的。但被插入的链表可以是空的。两个链表有两个头结点,在函数中要去掉一个头结点。

 

链表遍历

[c-sharp] view plaincopy
  1. #define list_for_each(pos, head)/  
  2.             for(pos = (head)->next; prefetch(pos->next), pos != (head);  
  3. static inline void prefetch(void *x)  
  4. {  
  5.     asm volatile("prefetch0 %0":  
  6.                                             :  
  7.                                          "m"(* unsigned long *)x));  
  8. }  
  9.  
  10. #define list_for_each_entry(pos, head, member)      /    
  11.        for(pos = list_entry((head)->next, typeof(*pos),member);  /    
  12.        prefetch(pos->member.next), &pos->member != (head);  /    
  13.        pos = list_entry(pos->member.next, typeof(*pos),member))   


prefetch为预取函数,提前预取下一指令,能提高程序执行速度。





队列


队列也是一种链表,只是针对队列的操作只能是从队尾插入,从队首删除。在操作系统中有很多这种数据结构的用武之地,一般是一个进程产生数据,另外一个进程处理数据,如Linux中网络数据包的处理,进程之间使用管道通信等,都是这种情况。Linux内核中队列称作kfifo,其对应的源文件时kernel/kfifo.c,<linux/kfifo.h>中包含了其声明。

[c-sharp] view plaincopy
  1. struct __kfifo {  
  2.     unsigned int    in;  
  3.     unsigned int    out;  
  4.     unsigned int    mask;  
  5.     unsigned int    esize;  
  6.     void        *data;  
  7. };  
  8.  
  9.  
  10. #define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) /  
  11.     union { /  
  12.         struct __kfifo    kfifo; /  
  13.         datatype    *type; /  
  14.         char        (*rectype)[recsize]; /  
  15.         ptrtype        *ptr; /  
  16.         const ptrtype    *ptr_const; /  
  17.     }  
  18.  
  19.  
  20. #define __STRUCT_KFIFO_PTR(type, recsize, ptrtype) /  
  21. { /  
  22.     __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); /  
  23.     type        buf[0]; /  
  24. }  
  25.   
  26.   
  27. struct kfifo __STRUCT_KFIFO_PTR(unsigned char, 0, void);  


kfifo提供了两种操作,入队(in)和出队(out),为了记录下一次出队或者入队的位置,kfifo维护了两个变量in和out。入队操作会将数据拷贝至队列中,具体位置由in确定,然后根据数据大小更新in,标识下一入队发生的位置。出队的操作与之类似。当in和out相等时,队列为空,此时不能执行出队操作。当in等于队列长度时,不能执行入队操作。


和其他内核对象一样,定义并初始化队列也有静态和动态两种方式。

动态方法

[c-sharp] view plaincopy
  1. static inline int __must_check  
  2. __kfifo_int_must_check_helper(int val)  
  3. {  
  4.     return val;  
  5. }  
  6.  
  7.  
  8. #define kfifo_alloc(fifo, size, gfp_mask) /  
  9. __kfifo_int_must_check_helper( /  
  10. ({ /  
  11.     typeof((fifo) + 1) __tmp = (fifo); /  
  12.     struct __kfifo *__kfifo = &__tmp->kfifo; /  
  13.     __is_kfifo_ptr(__tmp) ? /  
  14.     __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : /  
  15.     -EINVAL; /  
  16. }) /  
  17. )  

这个函数创建并初始化一个大小为size的队列。gfp_mask指定内存分配方式,可以取值GFP_KERNEL,GFP_ATOMIC,当在进程上下文分配内存,使用GFP_KERNEL,此时,允许kmalloc函数因为等待内存页释放而睡眠。如果,在中断上下文中分配内存,使用GFP_ATOMIC,此时kmalloc不能睡眠,此时可能由于内存不足导致分配失败。


如果,你想自己分配队列空间,可以使用下面这个函数。

[c-sharp] view plaincopy
  1. #define kfifo_init(fifo, buffer, size) /  
  2. ({ /  
  3.     typeof((fifo) + 1) __tmp = (fifo); /  
  4.     struct __kfifo *__kfifo = &__tmp->kfifo; /  
  5.     __is_kfifo_ptr(__tmp) ? /  
  6.     __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : /  
  7.     -EINVAL; /  
  8. })  

这个函数创建并初始化kfifo,这个kfifo使用buffer指向的大小为size的区域作为队列节点存储区域。注意,size必须为2的n次方,即size = 2n


静态方法

DECLARE_KFIFO(name, size);

INIT_KFIFO(name);



入队

但kfifo成功创建后,就可以想队列尾部放入数据

[c-sharp] view plaincopy
  1. #define    kfifo_in(fifo, buf, n) /  
  2. ({ /  
  3.     typeof((fifo) + 1) __tmp = (fifo); /  
  4.     typeof((buf) + 1) __buf = (buf); /  
  5.     unsigned long __n = (n); /  
  6.     const size_t __recsize = sizeof(*__tmp->rectype); /  
  7.     struct __kfifo *__kfifo = &__tmp->kfifo; /  
  8.     if (0) { /  
  9.         typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); /  
  10.         __dummy = (typeof(__buf))NULL; /  
  11.     } /  
  12.     (__recsize) ?/  
  13.     __kfifo_in_r(__kfifo, __buf, __n, __recsize) : /  
  14.     __kfifo_in(__kfifo, __buf, __n); /  
  15. })  

这个函数将buf开始的n个字符插入队列。这里是尽最大努力的拷贝,也就是说如果空间不足,拷贝的大小就是可用空间可容纳的大小。


出队

[c-sharp] view plaincopy
  1. #define    kfifo_out(fifo, buf, n) /  
  2. __kfifo_uint_must_check_helper( /  
  3. ({ /  
  4.     typeof((fifo) + 1) __tmp = (fifo); /  
  5.     typeof((buf) + 1) __buf = (buf); /  
  6.     unsigned long __n = (n); /  
  7.     const size_t __recsize = sizeof(*__tmp->rectype); /  
  8.     struct __kfifo *__kfifo = &__tmp->kfifo; /  
  9.     if (0) { /  
  10.         typeof(__tmp->ptr) __dummy = NULL; /  
  11.         __buf = __dummy; /  
  12.     } /  
  13.     (__recsize) ?/  
  14.     __kfifo_out_r(__kfifo, __buf, __n, __recsize) : /  
  15.     __kfifo_out(__kfifo, __buf, __n); /  
  16. }) /  
  17. )  

这个函数从队列中读出长度为n的数据,然后放入以buf表示的缓冲区中。出队意味着以后数据已不在队列中,你也可以调用函数kfifo_out_peek来读取数据而不从队列中删除这些数据。对于队列的操作需要注意的就是同步的问题,因为这等效于读者和写者问题。有兴趣可以看看内核中的实现。


其他队列的操作还有很多,代码文件位于linux/kfifo.c以及include/linux/kfifo.h中。




1. 前言


 


本文介绍linux内核中一些常用的数据结构和操作。


 


2. 双向链表(list)


 


linux内核中的双向链表通过结构 struct list_head来将各个节点连接起来,此结构会作为链表元素结构中的一个参数:


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


 


链表头的初始化,注意,结构中的指针为NULL并不是初始化,而是指向自身才是初始化,如果只是按普通情况下的置为NULL,而不是指向自身,系统会崩溃,这是一个容易犯的错误:


 


#define LIST_HEAD_INIT(name) { &(name), &(name) }


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


#define INIT_LIST_HEAD(ptr) do { \
 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)


 


最常用的链表操作:


插入到链表头:
void list_add(struct list_head *new, struct list_head *head);


 


插入到链表尾:
void list_add_tail(struct list_head *new, struct list_head *head);


 


删除链表节点:
void list_del(struct list_head *entry);


 


将节点移动到另一链表:
void list_move(struct list_head *list, struct list_head *head);


 


将节点移动到链表尾:
void list_move_tail(struct list_head *list,struct list_head *head);


 


判断链表是否为空,返回1为空,0非空
int list_empty(struct list_head *head);


 


把两个链表拼接起来:
void list_splice(struct list_head *list, struct list_head *head);


 


取得节点指针:
#define list_entry(ptr, type, member) \
 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))


 


遍历链表中每个节点:
#define list_for_each(pos, head) \
 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
         pos = pos->next, prefetch(pos->next))


 


逆向循环链表中每个节点:
#define list_for_each_prev(pos, head) \
 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
         pos = pos->prev, prefetch(pos->prev))


 


举例:


 


LISH_HEAD(mylist);


 


struct my_list{
 struct list_head list;
 int data;
};


 


static int ini_list(void)
{
 struct my_list *p;
 int i;
 for(i=0; i<100; i++){
  p="kmalloc"(sizeof(struct my_list), GFP_KERNEL);
  list_add(&p->list, &mylist);
 }
}



在内存中形成如下结构的一个双向链表:


 


  +---------------------------------------------------------------+
  |                                                               |
  |  mylist         99            98                     0        |
  |  +----+    +---------+    +---------+           +---------+   |
  +->|next|--->|list.next|--->|list.next|--->...--->|list.next|---+
     |----|    |---------|    |---------|           |---------|
  +--|prev|<---|list.prev|<---|list.prev|<---...<---|list.prev|<--+
  |  +----+    |---------|    |---------|           |---------|   |
  |            |  data   |    |  data   |           |  data   |   |
  |            +---------+    +---------+           +---------+   |
  |                                                               |
  +---------------------------------------------------------------+


 


知道了链表头就能遍历整个链表,如果是用list_add()插入新节点的话,从链表头的next方向看是一个堆栈型。


 


从链表中删除节点很容易:


static void del_item(struct my_list *p)
{
 list_del(&p->list, &mylist);
 kfree(p);
}


 


最重要的宏是list_entry,这个宏的思路是根据链表元素结构中链表头结构list_head的地址推算出链表元素结构的实际地址:


 


#define list_entry(ptr, type, member) \
 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))


 


ptr是链表元素结构(如struct my_list)中链表头结构list_head的地址
member是链表元素结构(如struct my_list)中链表头结构list_head参数的名称
type是链表元素结构类型(如struct my_list)


计算原理是根据链表头结构list_head的地址减去其在链表元素结构中的偏移位置而得到链表元素结构的地址。


 


例如:


static void print_list(void)
{
 struct list_head *cur;
 struct my_list *p;


 list_for_each(cur, &mylist){
  p="list"_entry(cur, struct my_list, list);
  printk("data=%d\n", p->data);
 }
}


 


优点:


这样就可以用相同的数据处理方式来描述所有双向链表,不用再单独为各个链表编写各种编辑函数。


 


缺点:
1) 链表头中元素置为NULL不是初始化,与普通习惯不同;
2) 仍然需要单独编写各自的删除整个链表的函数,不能统一处理,因为不能保证所有链表元素结构中链表头结构list_head的偏移地址都是相同的,当然如果把链表头结构list_head都作为链表元素结构的第一个参数,就可以用统一的删除整个链表的函数。




linux内核hlist分析

在Linux内核中,hlist(哈希链表)使用非常广泛。本文将对其数据结构和核心函数进行分析。

和hlist相关的数据结构有两个(1)hlist_head (2)hlist_node

[cpp] view plaincopy
  1. struct hlist_head {  
  2.         struct hlist_node *first;  
  3. };  
  4.   
  5. struct hlist_node {  
  6.         struct hlist_node *next, **pprev;  
  7. };  
顾名思义, hlist_head表示哈希表的头结点。 哈希表中每一个entry(hlist_head)所对应的都是一个链表(hlist),该链表的结点由hlist_node表示。

 hlist_head结构体只有一个域,即first。 first指针指向该hlist链表的第一个节点。

hlist_node结构体有两个域,next 和pprev。 next指针很容易理解,它指向下个hlist_node结点,倘若该节点是链表的最后一个节点,next指向NULL。

pprev是一个二级指针, 它指向前一个节点的next指针。为什么我们需要这样一个指针呢?它的好处是什么?

在回答这个问题之前,我们先研究另一个问题:为什么散列表的实现需要两个不同的数据结构?

散列表的目的是为了方便快速的查找,所以散列表通常是一个比较大的数组,否则“冲突”的概率会非常大, 这样也就失去了散列表的意义。如何做到既能维护一张大表,又能不使用过多的内存呢?就只能从数据结构上下功夫了。所以对于散列表的每个entry,它的结构体中只存放一个指针,解决了占用空间的问题。现在又出现了另一个问题:数据结构不一致。显然,如果hlist_node采用传统的next,prev指针, 对于第一个节点和后面其他节点的处理会不一致。这样并不优雅,而且效率上也有损失。

hlist_node巧妙地将pprev指向上一个节点的next指针的地址,由于hlist_head和hlist_node指向的下一个节点的指针类型相同,这样就解决了通用性!

下面我们再来看一看hlist_node这样设计之后,插入 删除这些基本操作会有什么不一样。

[cpp] view plaincopy
  1. static inline void __hlist_del(struct hlist_node *n)  
  2. {  
  3.         struct hlist_node *next = n->next;  
  4.         struct hlist_node **pprev = n->pprev;  
  5.         *pprev = next;  
  6.         if (next)  
  7.                 next->pprev = pprev;  
  8. }  
__hlist_del用于删除节点n。

首先获取n的下一个节点next, n->pprev指向n的前一个节点的next指针的地址, 这样×pprev就代表n前一个节点的下一个节点(现在即n本身),第三行代码*pprev=next;就将n的前一个节点和下一个节点关联起来了。至此,n节点的前一个节点的关联工作就完成了,现在再来完成下一个节点的关联工作。如果n是链表的最后一个节点,那么n->next即为空, 则无需任何操作,否则,next->pprev = pprev。

给链表增加一个节点需要考虑两个条件:(1)是否为链表的首个节点(2)普通节点。

[cpp] view plaincopy
  1. static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)  
  2.  {  
  3.          struct hlist_node *first = h->first;  
  4.          n->next = first;  
  5.          if (first)  
  6.                  first->pprev = &n->next;  
  7.          h->first = n;  
  8.          n->pprev = &h->first;  
  9.  }  
首先讨论条件(1)。

first = h->first; 获取当前链表的首个节点;

n->next = fist;  将n作为链表的首个节点,让first往后靠;

先来看最后一行 n->pprev - &h->first; 将n的pprev指向hlist_head的first指针,至此关于节点n的关联工作就做完了。

再来看倒数第二行 h->first = n; 将节点h的关联工作做完;

最后我们再来看原先的第一个节点的关联工作,对于它来说,仅仅需要更新一下pprev的关联信息: first->pprev = &n->next;

接下来讨论条件(2)。 这里也包括两种情况:a)插在当前节点的前面b)插在当前节点的后面

[cpp] view plaincopy
  1. /* next must be != NULL */  
  2.  static inline void hlist_add_before(struct hlist_node *n,  
  3.                                          struct hlist_node *next)  
  4.  {  
  5.          n->pprev = next->pprev;  
  6.          n->next = next;  
  7.          next->pprev = &n->next;  
  8.          *(n->pprev) = n;  
  9.  }  

先讨论情况a) 将节点n 插到next之前  (n是新插入的节点)

还是一个一个节点的搞定(一共三个节点), 先搞定节点n

n->pprev = next->prev;   将 next 的pprev 赋值给n->pprev  n取代next的位置

n->next = next;   将next作为n的下一个节点, 至此节点n的关联动作完成。

next->pprev = &n->next; next的关联动作完成。

*(n->pprev) = n;   n->pprev表示n的前一个节点的next指针; *(n->pprev)则表示n的前一个节点next指针所指向下一个节点的内容, 这里将n赋值给它,正好完成它的关联工作。

[cpp] view plaincopy
  1. static inline void hlist_add_after(struct hlist_node *n,  
  2.                                          struct hlist_node *next)  
  3.  {  
  4.          next->next = n->next;  
  5.          n->next = next;  
  6.          next->pprev = &n->next;  
  7.    
  8.          if(next->next)  
  9.                  next->next->pprev  = &next->next;  
  10.  }  
再来看情况b) 将结点next插入到n之后 (next是新插入的节点)

具体步骤就不分析了。 应该也很容易。

下面我还要介绍一个函数:

[cpp] view plaincopy
  1. static inline int hlist_unhashed(const struct hlist_node *h)  
  2.  {  
  3.          return !h->pprev;  
  4.  }  
这个函数的目的是判断该节点是否已经存在hash表中。这里处理得很巧妙。 判断前一个节点的next指向的地址是否为空。


最后我们看一个具体的例子,Linux内核是如何管理pid的。(正好和上一篇介绍pid的文章相呼应:))   基于内核3.0.3

内核初始化时要调用pidhash_init()创建哈希表。 该函数会在 start_kernel()函数里被调用(init/main.c Line 509)

[cpp] view plaincopy
  1. void __init pidhash_init(void)  
  2.  {  
  3.          int i, pidhash_size;  
  4.    
  5.          pid_hash = alloc_large_system_hash("PID"sizeof(*pid_hash), 0, 18,  
  6.                                             HASH_EARLY | HASH_SMALL,  
  7.                                             &pidhash_shift, NULL, 4096);  
  8.          pidhash_size = 1 << pidhash_shift;  
  9.    
  10.          for (i = 0; i < pidhash_size; i++)  
  11.                  INIT_HLIST_HEAD(&pid_hash[i]);  
  12.  }  
从这个函数可以看到内核会在slab上分配一个大小为pidhash_size的数组,然后为每一个entry进行初始化(INIT_HLIST_HEAD)

在alloc_pid函数里

[cpp] view plaincopy
  1. struct pid *alloc_pid(struct pid_namespace *ns)  
  2.  {  
  3.          struct pid *pid;  
  4.          enum pid_type type;  
  5.          int i, nr;  
  6.          struct pid_namespace *tmp;  
  7.          struct upid *upid;  
  8.    
  9.          pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);  /×在slab上分配pid结构体×/  
  10.          if (!pid)  
  11.                  goto out;  
  12.    
  13.          tmp = ns;  
  14.          for (i = ns->level; i >= 0; i--) {       /×虽然这里是for循环,实际只会运行一次,因为现在只支持global namespace即ns->level=0×/  
  15.                  nr = alloc_pidmap(tmp);          /×在各级pid_namespace上寻找并分配pid的值×/  
  16.                  if (nr < 0)  
  17.                          goto out_free;  
  18.    
  19.                  pid->numbers[i].nr = nr;  
  20.                  pid->numbers[i].ns = tmp;  
  21.                  tmp = tmp->parent;  
  22.          }  
  23.    
  24.          get_pid_ns(ns);  
  25.          pid->level = ns->level;  
  26.          atomic_set(&pid->count, 1);  
  27.          for (type = 0; type < PIDTYPE_MAX; ++type)  
  28.                  INIT_HLIST_HEAD(&pid->tasks[type]);     
  29.    
  30.          upid = pid->numbers + ns->level;  
  31.          spin_lock_irq(&pidmap_lock);  
  32.          for ( ; upid >= pid->numbers; --upid)  
  33.                  hlist_add_head_rcu(&upid->pid_chain,  
  34.                                  &pid_hash[pid_hashfn(upid->nr, upid->ns)]);   /×将各级namespace中的upid插入pidhash的哈希表里×/  
  35.          spin_unlock_irq(&pidmap_lock);  
  36.    
  37.  out:  
  38.          return pid;  
  39.    
  40.  out_free:  
  41.          while (++i <= ns->level)  
  42.                  free_pidmap(pid->numbers + i);  
  43.    
  44.          kmem_cache_free(ns->pid_cachep, pid);  
  45.          pid = NULL;  
  46.          goto out;  
  47.  }  


inux中的hlist_head和hlist_node结构被用于hash表,具体定义如下:
struct hlist_head {
      struct hlist_node *first;
};
struct hlist_node {
      struct hlist_node *next, **pprev;
};
一般hash表都比较大,所以设计成单链表能节省几乎一半空间。这并不奇怪。奇怪的是hlist_node的pprev域。pprev域被设计成hlist_node的二级指针,而且指向前一个节点的next域(或者对于第一个节点,指向表头的first域)。为什么要设计成这样?
考虑普通的单链表,如下:
struct node {
      struct node *next;
};
对于这样的链表,在指定节点(a)后插入节点(b)很容易:b->next = a->next; a->next = b;
但是要在指定节点(a)前插入(b)则很麻烦:先通过while(i->next == a) i = i->next;得到a的前一个节点,然后再进行i->next = b; b->next = a;进行插入;这就花费一定的时间用来搜索a的前一个节点。
linux要节省这部分时间。分析一下,搜索的目的是修改上一个节点的next域。为了省去搜索,hlist_node便增加了pprev域指向上一个节点的next域。要修改上一个节点的next,可以同*pprev = ?快速的修改。
总结一下,引入pprv域是把单链表的前插操作最优。

原创粉丝点击