Linux 内核 hlist 详解

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

和hlist相关的数据结构有两个:hlist_head 和 hlist_node

  1. //hash桶的头结点  
  2. struct hlist_head {  
  3.     struct hlist_node *first;//指向每一个hash桶的第一个结点的指针  
  4. };  
  5. //hash桶的普通结点  
  6. struct hlist_node {  
  7.     struct hlist_node *next;//指向下一个结点的指针  
  8.     struct hlist_node **pprev;//指向上一个结点的next指针的地址  
  9. };  

结构如下图所示:


hlist_head结构体只有一个域,即first。 first指针指向该hlist链表的第一个节点。
hlist_node结构体有两个域,next 和pprev。 next指针很容易理解,它指向下个hlist_node结点,倘若该节点是链表的最后一个节点,next指向NULL。
pprev是一个二级指针, 它指向前一个节点的next指针的地址
。为什么我们需要这样一个指针呢?它的好处是什么?
在回答这个问题之前,我们先研究另一个问题:为什么哈希表的实现需要两个不同的数据结构?
哈希表的目的是为了方便快速的查找,所以哈希表中hash桶的数量通常比较大,否则“冲突”的概率会非常大,这样也就失去了哈希表的意义。如何做到既能维护一张大表,又能不使用过多的内存呢?就只能从数据结构上下功夫了。所以对于哈希表的每个hash桶,它的结构体中只存放一个指针,解决了占用空间的问题。现在又出现了另一个问题:数据结构不一致。显然,如果hlist_node采用传统的next,prev指针,对于第一个节点和后面其他节点的处理会不一致。这样并不优雅,而且效率上也有损失。
hlist_node巧妙地将pprev指向上一个节点的next指针的地址,由于hlist_head的first域指向的结点类型和hlist_node指向的下一个结点的结点类型相同,这样就解决了通用性!

如果要删除hash桶对应链表中的第一个普通结点

对应的程序代码如下:

  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;//保留待删除的第一个结点的pprev域(即头结点first域的地址),此时 pprev = &first   
  5.  *pprev = next;   
  6.  /* 
  7.  因为pprev = &first,所以*pprev = next,相当于 first = next 
  8.  即将hash桶的头结点指针指向原来的第二个结点,如上图中的黑线1 
  9.  */  
  10.  if (next) //如果第二个结点不为空  
  11.   next->pprev = pprev;//将第二个结点的pprev域设置为头结点first域的地址,如上图中的黑线2   
  12. }  

如果要删除hash桶对应链表中的非第一个结点


对应的程序代码如下:

  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;//获取待删除结点的pprev域   
  5.  *pprev = next; //修改待删除结点的pprev域,逻辑上使待删除结点的前驱结点指向待删除结点的后继结点,如上图中的黑线1  
  6.   
  7.  if (next) //如果待删除结点的下一个普通结点不为空  
  8.       next->pprev = pprev;//设置下一个结点的pprev域,如上图中的黑线2,保持hlist的结构   
  9. }  

可以看到删除第一个普通结点和删除非第一个普通结点的代码是一样的。

下面再来看看如果hlist_node中包含两个分别指向前驱结点和后继结点的指针

很明显删除hash桶对应链表中的非第一个普通结点,只需要如下两行代码:

  1. n->next->prev = n->prev;  
  2. n->prev->next = n->next;  

可是,如果是删除的hash桶对应链表中的第一个普通结点:
此时 n->prev->next = n->next 就会出问题,因为hash桶的表头结点没有next域
所以,明显在这种情况下删除hash桶对应链表的第一个普通结点和非第一个普通结点的代码是不一样的。
同样的情况也存在于插入操作。

附一张在hash桶头结点之后,插入第一个普通结点的图:


在遍历上,如果使用hlist_hode, list_node指针进行遍历,两者过程大致相似。

  1. #define list_for_each(pos, head) \  
  2.         for (pos = (head)->next; prefetch(pos->next), pos != (head); \  
  3.                 pos = pos->next)  
  4.    
  5. #define hlist_for_each(pos, head) \  
  6.         for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \  
  7.              pos = pos->next)  

如果使用其寄生结构的指针进行遍历,则hlistlist也略有不同,hlist在遍历时需要一个指向hlist_node的临时指针,该指针的引入,一是为了遍历,而list的遍历在list_entry的参数中实现了,更主要的目的在于判断结束,因为hlist最后一个节点的nextNULL,只有hlist_node指向NULL时才算结束,而这个NULL不包含在任何寄生结构内,不能通过tpos->member的方式访问到,故临时变量pos的引入时必须的。

  1. #define list_for_each_entry(pos, head, member) \  
  2.         for (pos = list_entry((head)->next, typeof(*pos), member); \  
  3.              prefetch(pos->member.next), &pos->member != (head); \  
  4.              pos = list_entry(pos->member.next, typeof(*pos), member))  
  5.    
  6. #define hlist_for_each_entry(tpos, pos, head, member) \  
  7.         for (pos = (head)->first; \  
  8.              pos && ({ prefetch(pos->next); 1;}) && \  
  9.                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \  
  10.              pos = pos->next)  

另外,listhlist的遍历都实现了safe版本,因在遍历时,没有任何特别的东西来阻止对链表执行删除操作(通常在使用链表时使用锁来保护并发访问)。安全版本的遍历函数使用临时存放的方法使得检索链表时能不被删除操作所影响。

  1. #define list_for_each_safe(pos, n, head) \  
  2.         for (pos = (head)->next, n = pos->next; pos != (head); \  
  3.                 pos = n, n = pos->next)  
  4.    
  5. #define hlist_for_each_safe(pos, n, head) \  
  6.         for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \  
  7.              pos = n)  

附上linux内核中与hlist相关的完整代码:

  1. //hash桶的头结点   
  2. struct hlist_head {   
  3.      struct hlist_node *first;//指向每一个hash桶的第一个结点的指针   
  4. };   
  5. //hash桶的普通结点   
  6. struct hlist_node {   
  7.  struct hlist_node *next;//指向下一个结点的指针   
  8.  struct hlist_node **pprev;//指向上一个结点的next指针的地址   
  9. };   
  10. //以下三种方法都是初始化hash桶的头结点   
  11. #define HLIST_HEAD_INIT { .first = NULL }   
  12. #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }   
  13. #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)   
  14.   
  15. //初始化hash桶的普通结点   
  16. static inline void INIT_HLIST_NODE(struct hlist_node *h)   
  17. {   
  18.  h->next = NULL;   
  19.  h->pprev = NULL;   
  20. }   
  21.   
  22. //判断一个结点是否已经存在于hash桶中   
  23. static inline int hlist_unhashed(const struct hlist_node *h)   
  24. {   
  25.      return !h->pprev;   
  26. }   
  27.   
  28. //判断一个hash桶是否为空   
  29. static inline int hlist_empty(const struct hlist_head *h)   
  30. {   
  31.      return !h->first;   
  32. }   
  33.   
  34. static inline void __hlist_del(struct hlist_node *n)   
  35. {   
  36.  struct hlist_node *next = n->next;//获取指向待删除结点的下一个结点的指针   
  37.  struct hlist_node **pprev = n->pprev;//保留待删除结点的pprev域   
  38.  *pprev = next;//修改待删除结点的pprev域,逻辑上使待删除结点的前驱结点指向待删除结点的后继结点   
  39.  if (next)   
  40.   next->pprev = pprev;//设置待删除结点的下一个结点的pprev域,保持hlist的结构   
  41. }   
  42.   
  43. static inline void hlist_del(struct hlist_node *n)   
  44. {   
  45.  __hlist_del(n);//删除结点之后,需要将其next域和pprev域设置为无用值   
  46.  n->next = LIST_POISON1;   
  47.  n->pprev = LIST_POISON2;   
  48. }   
  49.   
  50. static inline void hlist_del_init(struct hlist_node *n)   
  51. {   
  52.  if (!hlist_unhashed(n))   
  53.  {   
  54.   __hlist_del(n);   
  55.   INIT_HLIST_NODE(n);   
  56.  }   
  57. }   
  58.   
  59. //将普通结点n插入到头结点h对应的hash桶的第一个结点的位置   
  60. static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)   
  61. {   
  62.  struct hlist_node *first = h->first;   
  63.  n->next = first;   
  64.  if (first)   
  65.   first->pprev = &n->next;   
  66.  h->first = n;   
  67.  n->pprev = &h->first;   
  68. }   
  69.   
  70. /* next must be != NULL */   
  71. //在next结点之前插入结点n,即使next结点是hash桶中的第一个结点也可以   
  72. static inline void hlist_add_before(struct hlist_node *n, struct hlist_node *next)   
  73. {   
  74.  n->pprev = next->pprev;   
  75.  n->next = next;   
  76.  next->pprev = &n->next;   
  77.  *(n->pprev) = n;   
  78. }   
  79.   
  80. //在结点n之后插入结点next   
  81. static inline void hlist_add_after(struct hlist_node *n, struct hlist_node *next)   
  82. {   
  83.  next->next = n->next;   
  84.  n->next = next;   
  85.  next->pprev = &n->next;   
  86.   
  87.  if(next->next)   
  88.   next->next->pprev = &next->next;   
  89. }   
  90.   
  91. /*  
  92.  * Move a list from one list head to another. Fixup the pprev  
  93.  * reference of the first entry if it exists.  
  94.  */   
  95. static inline void hlist_move_list(struct hlist_head *old, struct hlist_head *new)   
  96. {   
  97.  new->first = old->first;   
  98.  if (new->first)   
  99.   new->first->pprev = &new->first;   
  100.  old->first = NULL;   
  101. }   
  102.   
  103. //通过一个结构体内部一个成员的地址获取结构体的首地址   
  104. #define hlist_entry(ptr, type, member) container_of(ptr,type,member)   
  105.   
  106. #define hlist_for_each(pos, head) \   
  107.  for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \   
  108.       pos = pos->next)   
  109.   
  110. #define hlist_for_each_safe(pos, n, head) \   
  111.  for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \   
  112.       pos = n)   
  113.   
  114. /**  
  115.  * hlist_for_each_entry - iterate over list of given type  
  116.  * @tpos:   the type * to use as a loop cursor.  
  117.  * @pos:    the &struct hlist_node to use as a loop cursor.  
  118.  * @head:   the head for your list.  
  119.  * @member: the name of the hlist_node within the struct.  
  120.  */   
  121. #define hlist_for_each_entry(tpos, pos, head, member)    \   
  122.  for (pos = (head)->first;    \   
  123.       pos && ({ prefetch(pos->next); 1;}) &&  \   
  124.   ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \   
  125.       pos = pos->next)   
  126.   
  127. /**  
  128.  * hlist_for_each_entry_continue - iterate over a hlist continuing after current point  
  129.  * @tpos:   the type * to use as a loop cursor.  
  130.  * @pos:    the &struct hlist_node to use as a loop cursor.  
  131.  * @member: the name of the hlist_node within the struct.  
  132.  */   
  133. #define hlist_for_each_entry_continue(tpos, pos, member)     \   
  134.  for (pos = (pos)->next;  \   
  135.       pos && ({ prefetch(pos->next); 1;}) &&  \   
  136.   ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \   
  137.       pos = pos->next)   
  138.   
  139. /**  
  140.  * hlist_for_each_entry_from - iterate over a hlist continuing from current point  
  141.  * @tpos:   the type * to use as a loop cursor.  
  142.  * @pos:    the &struct hlist_node to use as a loop cursor.  
  143.  * @member: the name of the hlist_node within the struct.  
  144.  */   
  145. #define hlist_for_each_entry_from(tpos, pos, member)     \   
  146.  for (; pos && ({ prefetch(pos->next); 1;}) &&    \   
  147.   ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \   
  148.       pos = pos->next)   
  149.   
  150. /**  
  151.  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry  
  152.  * @tpos:   the type * to use as a loop cursor.  
  153.  * @pos:    the &struct hlist_node to use as a loop cursor.  
  154.  * @n:   another &struct hlist_node to use as temporary storage  
  155.  * @head:   the head for your list.  
  156.  * @member: the name of the hlist_node within the struct.  
  157.  */   
  158. #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \   
  159.  for (pos = (head)->first;    \   
  160.       pos && ({ n = pos->next; 1; }) && \   
  161.   ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \   
  162.       pos = n) 

猜你喜欢

转载自blog.csdn.net/xie0812/article/details/52927383