Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28309
  • 博文数量: 8
  • 博客积分: 210
  • 博客等级: 入伍新兵
  • 技术积分: 105
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-23 12:46
个人简介

ψ(° Д °;)ψ

文章分类

全部博文(8)

文章存档

2013年(2)

2012年(2)

2011年(4)

我的朋友

分类: LINUX

2012-02-04 18:56:16

linux内核链表提供了一个非常经典的方法。与平时C入门书籍中使用的链表不同,内核链表在任何地方完全通用,不用再对每一种数据分别定义struct。

1. 普通链表
  1. // 数据
  2. typedef struct data
  3. {
  4.     /* some data below */
        // ......
  5. }DATA;


  6. struct list
  7. {
  8.     struct list *prev, *next;
  9.     DATA data;
  10. };

2. 内核链表
  1. // 内核链表指针
  2. struct list_head
  3. {
  4.     struct list_head *prev, *next;
  5. };

  6. // 数据
  7. typedef struct data
  8. {
  9.     struct list_head head;
  10.     /* some data below */
  11.     // ......
  12. }DATA;
从上可以看出,普通链表是把数据域定义在链表节点内,这样的弊端很明显。而内核链表是在数据域中插入链表针节节点,这样无论数据域怎么变,链表的使用还是照旧不会有变化。

3. 在取数据时,说白了就是通过指链表针节点地址反推出当前链表地址。具体实现如下
  1. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  2. #define container_of(ptr, type, member) ({            \
  3.     const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
  4.     (type *)( (char *)__mptr - offsetof(type,member) );})
传入链表指针地址ptr减去ptr相对于整个链表节点的偏移量,就得到了起始地址,于是链表取出来了。

4. 将内核源码中的list.h整理出来,在以后可以方便地使用,避免出错。
list.h
  1. #ifndef _LINUX_LIST_H
  2. #define _LINUX_LIST_H

  3. //#include <linux/types.h>
  4. //#include <linux/stddef.h>
  5. //#include <linux/poison.h>
  6. //#include <linux/const.h>

  7. #define LIST_POISON1    ((void *) 0x0)    //0x00100100)
  8. #define LIST_POISON2    ((void *) 0x0)    //0x00200200)

  9. /*
  10.  * stddef.h
  11.  */
  12. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

  13. /** kernel.h
  14.  * container_of - cast a member of a structure out to the containing structure
  15.  * @ptr:    the pointer to the member.
  16.  * @type:    the type of the container struct this is embedded in.
  17.  * @member:    the name of the member within the struct.
  18.  *
  19.  */
  20. #define container_of(ptr, type, member) ({            \
  21.     const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
  22.     (type *)( (char *)__mptr - offsetof(type,member) );})


  23. /*
  24.  * types.h
  25.  */
  26. struct list_head {
  27.     struct list_head *next, *prev;
  28. };

  29. struct hlist_head {
  30.     struct hlist_node *first;
  31. };

  32. struct hlist_node {
  33.     struct hlist_node *next, **pprev;
  34. };



  35. /*
  36.  * Simple doubly linked list implementation.
  37.  *
  38.  * Some of the internal functions ("__xxx") are useful when
  39.  * manipulating whole lists rather than single entries, as
  40.  * sometimes we already know the next/prev entries and we can
  41.  * generate better code by using them directly rather than
  42.  * using the generic single-entry routines.
  43.  */

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

  45. #define LIST_HEAD(name) \
  46.     struct list_head name = LIST_HEAD_INIT(name)

  47. static inline void INIT_LIST_HEAD(struct list_head *list)
  48. {
  49.     list->next = list;
  50.     list->prev = list;
  51. }

  52. /*
  53.  * Insert a new entry between two known consecutive entries.
  54.  *
  55.  * This is only for internal list manipulation where we know
  56.  * the prev/next entries
  57.  */
  58. #ifndef CONFIG_DEBUG_LIST
  59. static inline void __list_add(struct list_head *new,
  60.              struct list_head *prev,
  61.              struct list_head *next)
  62. {
  63.     next->prev = new;
  64.     new->next = next;
  65.     new->prev = prev;
  66.     prev->next = new;
  67. }
  68. #else
  69. extern void __list_add(struct list_head *new,
  70.              struct list_head *prev,
  71.              struct list_head *next);
  72. #endif

  73. /**
  74.  * list_add - add a new entry
  75.  * @new: new entry to be added
  76.  * @head: list head to add it after
  77.  *
  78.  * Insert a new entry after the specified head.
  79.  * This is good for implementing stacks.
  80.  */
  81. static inline void list_add(struct list_head *new, struct list_head *head)
  82. {
  83.     __list_add(new, head, head->next);
  84. }


  85. /**
  86.  * list_add_tail - add a new entry
  87.  * @new: new entry to be added
  88.  * @head: list head to add it before
  89.  *
  90.  * Insert a new entry before the specified head.
  91.  * This is useful for implementing queues.
  92.  */
  93. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  94. {
  95.     __list_add(new, head->prev, head);
  96. }

  97. /*
  98.  * Delete a list entry by making the prev/next entries
  99.  * point to each other.
  100.  *
  101.  * This is only for internal list manipulation where we know
  102.  * the prev/next entries
  103.  */
  104. static inline void __list_del(struct list_head * prev, struct list_head * next)
  105. {
  106.     next->prev = prev;
  107.     prev->next = next;
  108. }

  109. /**
  110.  * list_del - deletes entry from list.
  111.  * @entry: the element to delete from the list.
  112.  * Note: list_empty() on entry does not return true after this, the entry is
  113.  * in an undefined state.
  114.  */
  115. #ifndef CONFIG_DEBUG_LIST
  116. static inline void __list_del_entry(struct list_head *entry)
  117. {
  118.     __list_del(entry->prev, entry->next);
  119. }

  120. static inline void list_del(struct list_head *entry)
  121. {
  122.     __list_del(entry->prev, entry->next);
  123.     entry->next = LIST_POISON1;
  124.     entry->prev = LIST_POISON2;
  125. }
  126. #else
  127. extern void __list_del_entry(struct list_head *entry);
  128. extern void list_del(struct list_head *entry);
  129. #endif

  130. /**
  131.  * list_replace - replace old entry by new one
  132.  * @old : the element to be replaced
  133.  * @new : the new element to insert
  134.  *
  135.  * If @old was empty, it will be overwritten.
  136.  */
  137. static inline void list_replace(struct list_head *old,
  138.                 struct list_head *new)
  139. {
  140.     new->next = old->next;
  141.     new->next->prev = new;
  142.     new->prev = old->prev;
  143.     new->prev->next = new;
  144. }

  145. static inline void list_replace_init(struct list_head *old,
  146.                     struct list_head *new)
  147. {
  148.     list_replace(old, new);
  149.     INIT_LIST_HEAD(old);
  150. }

  151. /**
  152.  * list_del_init - deletes entry from list and reinitialize it.
  153.  * @entry: the element to delete from the list.
  154.  */
  155. static inline void list_del_init(struct list_head *entry)
  156. {
  157.     __list_del_entry(entry);
  158.     INIT_LIST_HEAD(entry);
  159. }

  160. /**
  161.  * list_move - delete from one list and add as another's head
  162.  * @list: the entry to move
  163.  * @head: the head that will precede our entry
  164.  */
  165. static inline void list_move(struct list_head *list, struct list_head *head)
  166. {
  167.     __list_del_entry(list);
  168.     list_add(list, head);
  169. }

  170. /**
  171.  * list_move_tail - delete from one list and add as another's tail
  172.  * @list: the entry to move
  173.  * @head: the head that will follow our entry
  174.  */
  175. static inline void list_move_tail(struct list_head *list,
  176.                  struct list_head *head)
  177. {
  178.     __list_del_entry(list);
  179.     list_add_tail(list, head);
  180. }

  181. /**
  182.  * list_is_last - tests whether @list is the last entry in list @head
  183.  * @list: the entry to test
  184.  * @head: the head of the list
  185.  */
  186. static inline int list_is_last(const struct list_head *list,
  187.                 const struct list_head *head)
  188. {
  189.     return list->next == head;
  190. }

  191. /**
  192.  * list_empty - tests whether a list is empty
  193.  * @head: the list to test.
  194.  */
  195. static inline int list_empty(const struct list_head *head)
  196. {
  197.     return head->next == head;
  198. }

  199. /**
  200.  * list_empty_careful - tests whether a list is empty and not being modified
  201.  * @head: the list to test
  202.  *
  203.  * Description:
  204.  * tests whether a list is empty _and_ checks that no other CPU might be
  205.  * in the process of modifying either member (next or prev)
  206.  *
  207.  * NOTE: using list_empty_careful() without synchronization
  208.  * can only be safe if the only activity that can happen
  209.  * to the list entry is list_del_init(). Eg. it cannot be used
  210.  * if another CPU could re-list_add() it.
  211.  */
  212. static inline int list_empty_careful(const struct list_head *head)
  213. {
  214.     struct list_head *next = head->next;
  215.     return (next == head) && (next == head->prev);
  216. }

  217. /**
  218.  * list_rotate_left - rotate the list to the left
  219.  * @head: the head of the list
  220.  */
  221. static inline void list_rotate_left(struct list_head *head)
  222. {
  223.     struct list_head *first;

  224.     if (!list_empty(head)) {
  225.         first = head->next;
  226.         list_move_tail(first, head);
  227.     }
  228. }

  229. /**
  230.  * list_is_singular - tests whether a list has just one entry.
  231.  * @head: the list to test.
  232.  */
  233. static inline int list_is_singular(const struct list_head *head)
  234. {
  235.     return !list_empty(head) && (head->next == head->prev);
  236. }

  237. static inline void __list_cut_position(struct list_head *list,
  238.         struct list_head *head, struct list_head *entry)
  239. {
  240.     struct list_head *new_first = entry->next;
  241.     list->next = head->next;
  242.     list->next->prev = list;
  243.     list->prev = entry;
  244.     entry->next = list;
  245.     head->next = new_first;
  246.     new_first->prev = head;
  247. }

  248. /**
  249.  * list_cut_position - cut a list into two
  250.  * @list: a new list to add all removed entries
  251.  * @head: a list with entries
  252.  * @entry: an entry within head, could be the head itself
  253.  *    and if so we won't cut the list
  254.  *
  255.  * This helper moves the initial part of @head, up to and
  256.  * including @entry, from @head to @list. You should
  257.  * pass on @entry an element you know is on @head. @list
  258.  * should be an empty list or a list you do not care about
  259.  * losing its data.
  260.  *
  261.  */
  262. static inline void list_cut_position(struct list_head *list,
  263.         struct list_head *head, struct list_head *entry)
  264. {
  265.     if (list_empty(head))
  266.         return;
  267.     if (list_is_singular(head) &&
  268.         (head->next != entry && head != entry))
  269.         return;
  270.     if (entry == head)
  271.         INIT_LIST_HEAD(list);
  272.     else
  273.         __list_cut_position(list, head, entry);
  274. }

  275. static inline void __list_splice(const struct list_head *list,
  276.                  struct list_head *prev,
  277.                  struct list_head *next)
  278. {
  279.     struct list_head *first = list->next;
  280.     struct list_head *last = list->prev;

  281.     first->prev = prev;
  282.     prev->next = first;

  283.     last->next = next;
  284.     next->prev = last;
  285. }

  286. /**
  287.  * list_splice - join two lists, this is designed for stacks
  288.  * @list: the new list to add.
  289.  * @head: the place to add it in the first list.
  290.  */
  291. static inline void list_splice(const struct list_head *list,
  292.                 struct list_head *head)
  293. {
  294.     if (!list_empty(list))
  295.         __list_splice(list, head, head->next);
  296. }

  297. /**
  298.  * list_splice_tail - join two lists, each list being a queue
  299.  * @list: the new list to add.
  300.  * @head: the place to add it in the first list.
  301.  */
  302. static inline void list_splice_tail(struct list_head *list,
  303.                 struct list_head *head)
  304. {
  305.     if (!list_empty(list))
  306.         __list_splice(list, head->prev, head);
  307. }

  308. /**
  309.  * list_splice_init - join two lists and reinitialise the emptied list.
  310.  * @list: the new list to add.
  311.  * @head: the place to add it in the first list.
  312.  *
  313.  * The list at @list is reinitialised
  314.  */
  315. static inline void list_splice_init(struct list_head *list,
  316.                  struct list_head *head)
  317. {
  318.     if (!list_empty(list)) {
  319.         __list_splice(list, head, head->next);
  320.         INIT_LIST_HEAD(list);
  321.     }
  322. }

  323. /**
  324.  * list_splice_tail_init - join two lists and reinitialise the emptied list
  325.  * @list: the new list to add.
  326.  * @head: the place to add it in the first list.
  327.  *
  328.  * Each of the lists is a queue.
  329.  * The list at @list is reinitialised
  330.  */
  331. static inline void list_splice_tail_init(struct list_head *list,
  332.                      struct list_head *head)
  333. {
  334.     if (!list_empty(list)) {
  335.         __list_splice(list, head->prev, head);
  336.         INIT_LIST_HEAD(list);
  337.     }
  338. }

  339. /**
  340.  * list_entry - get the struct for this entry
  341.  * @ptr:    the &struct list_head pointer.
  342.  * @type:    the type of the struct this is embedded in.
  343.  * @member:    the name of the list_struct within the struct.
  344.  */
  345. #define list_entry(ptr, type, member) \
  346.     container_of(ptr, type, member)

  347. /**
  348.  * list_first_entry - get the first element from a list
  349.  * @ptr:    the list head to take the element from.
  350.  * @type:    the type of the struct this is embedded in.
  351.  * @member:    the name of the list_struct within the struct.
  352.  *
  353.  * Note, that list is expected to be not empty.
  354.  */
  355. #define list_first_entry(ptr, type, member) \
  356.     list_entry((ptr)->next, type, member)

  357. /**
  358.  * list_for_each    -    iterate over a list
  359.  * @pos:    the &struct list_head to use as a loop cursor.
  360.  * @head:    the head for your list.
  361.  */
  362. #define list_for_each(pos, head) \
  363.     for (pos = (head)->next; pos != (head); pos = pos->next)

  364. /**
  365.  * __list_for_each    -    iterate over a list
  366.  * @pos:    the &struct list_head to use as a loop cursor.
  367.  * @head:    the head for your list.
  368.  *
  369.  * This variant doesn't differ from list_for_each() any more.
  370.  * We don't do prefetching in either case.
  371.  */
  372. #define __list_for_each(pos, head) \
  373.     for (pos = (head)->next; pos != (head); pos = pos->next)

  374. /**
  375.  * list_for_each_prev    -    iterate over a list backwards
  376.  * @pos:    the &struct list_head to use as a loop cursor.
  377.  * @head:    the head for your list.
  378.  */
  379. #define list_for_each_prev(pos, head) \
  380.     for (pos = (head)->prev; pos != (head); pos = pos->prev)

  381. /**
  382.  * list_for_each_safe - iterate over a list safe against removal of list entry
  383.  * @pos:    the &struct list_head to use as a loop cursor.
  384.  * @n:        another &struct list_head to use as temporary storage
  385.  * @head:    the head for your list.
  386.  */
  387. #define list_for_each_safe(pos, n, head) \
  388.     for (pos = (head)->next, n = pos->next; pos != (head); \
  389.         pos = n, n = pos->next)

  390. /**
  391.  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
  392.  * @pos:    the &struct list_head to use as a loop cursor.
  393.  * @n:        another &struct list_head to use as temporary storage
  394.  * @head:    the head for your list.
  395.  */
  396. #define list_for_each_prev_safe(pos, n, head) \
  397.     for (pos = (head)->prev, n = pos->prev; \
  398.      pos != (head); \
  399.      pos = n, n = pos->prev)

  400. /**
  401.  * list_for_each_entry    -    iterate over list of given type
  402.  * @pos:    the type * to use as a loop cursor.
  403.  * @head:    the head for your list.
  404.  * @member:    the name of the list_struct within the struct.
  405.  */
  406. #define list_for_each_entry(pos, head, member)                \
  407.     for (pos = list_entry((head)->next, typeof(*pos), member);    \
  408.      &pos->member != (head);     \
  409.      pos = list_entry(pos->member.next, typeof(*pos), member))

  410. /**
  411.  * list_for_each_entry_reverse - iterate backwards over list of given type.
  412.  * @pos:    the type * to use as a loop cursor.
  413.  * @head:    the head for your list.
  414.  * @member:    the name of the list_struct within the struct.
  415.  */
  416. #define list_for_each_entry_reverse(pos, head, member)            \
  417.     for (pos = list_entry((head)->prev, typeof(*pos), member);    \
  418.      &pos->member != (head);     \
  419.      pos = list_entry(pos->member.prev, typeof(*pos), member))

  420. /**
  421.  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
  422.  * @pos:    the type * to use as a start point
  423.  * @head:    the head of the list
  424.  * @member:    the name of the list_struct within the struct.
  425.  *
  426.  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
  427.  */
  428. #define list_prepare_entry(pos, head, member) \
  429.     ((pos) ? : list_entry(head, typeof(*pos), member))

  430. /**
  431.  * list_for_each_entry_continue - continue iteration over list of given type
  432.  * @pos:    the type * to use as a loop cursor.
  433.  * @head:    the head for your list.
  434.  * @member:    the name of the list_struct within the struct.
  435.  *
  436.  * Continue to iterate over list of given type, continuing after
  437.  * the current position.
  438.  */
  439. #define list_for_each_entry_continue(pos, head, member)         \
  440.     for (pos = list_entry(pos->member.next, typeof(*pos), member);    \
  441.      &pos->member != (head);    \
  442.      pos = list_entry(pos->member.next, typeof(*pos), member))

  443. /**
  444.  * list_for_each_entry_continue_reverse - iterate backwards from the given point
  445.  * @pos:    the type * to use as a loop cursor.
  446.  * @head:    the head for your list.
  447.  * @member:    the name of the list_struct within the struct.
  448.  *
  449.  * Start to iterate over list of given type backwards, continuing after
  450.  * the current position.
  451.  */
  452. #define list_for_each_entry_continue_reverse(pos, head, member)        \
  453.     for (pos = list_entry(pos->member.prev, typeof(*pos), member);    \
  454.      &pos->member != (head);    \
  455.      pos = list_entry(pos->member.prev, typeof(*pos), member))

  456. /**
  457.  * list_for_each_entry_from - iterate over list of given type from the current point
  458.  * @pos:    the type * to use as a loop cursor.
  459.  * @head:    the head for your list.
  460.  * @member:    the name of the list_struct within the struct.
  461.  *
  462.  * Iterate over list of given type, continuing from current position.
  463.  */
  464. #define list_for_each_entry_from(pos, head, member)             \
  465.     for (; &pos->member != (head);    \
  466.      pos = list_entry(pos->member.next, typeof(*pos), member))

  467. /**
  468.  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  469.  * @pos:    the type * to use as a loop cursor.
  470.  * @n:        another type * to use as temporary storage
  471.  * @head:    the head for your list.
  472.  * @member:    the name of the list_struct within the struct.
  473.  */
  474. #define list_for_each_entry_safe(pos, n, head, member)            \
  475.     for (pos = list_entry((head)->next, typeof(*pos), member),    \
  476.         n = list_entry(pos->member.next, typeof(*pos), member);    \
  477.      &pos->member != (head);                     \
  478.      pos = n, n = list_entry(n->member.next, typeof(*n), member))

  479. /**
  480.  * list_for_each_entry_safe_continue - continue list iteration safe against removal
  481.  * @pos:    the type * to use as a loop cursor.
  482.  * @n:        another type * to use as temporary storage
  483.  * @head:    the head for your list.
  484.  * @member:    the name of the list_struct within the struct.
  485.  *
  486.  * Iterate over list of given type, continuing after current point,
  487.  * safe against removal of list entry.
  488.  */
  489. #define list_for_each_entry_safe_continue(pos, n, head, member)         \
  490.     for (pos = list_entry(pos->member.next, typeof(*pos), member),         \
  491.         n = list_entry(pos->member.next, typeof(*pos), member);        \
  492.      &pos->member != (head);                        \
  493.      pos = n, n = list_entry(n->member.next, typeof(*n), member))

  494. /**
  495.  * list_for_each_entry_safe_from - iterate over list from current point safe against removal
  496.  * @pos:    the type * to use as a loop cursor.
  497.  * @n:        another type * to use as temporary storage
  498.  * @head:    the head for your list.
  499.  * @member:    the name of the list_struct within the struct.
  500.  *
  501.  * Iterate over list of given type from current point, safe against
  502.  * removal of list entry.
  503.  */
  504. #define list_for_each_entry_safe_from(pos, n, head, member)             \
  505.     for (n = list_entry(pos->member.next, typeof(*pos), member);        \
  506.      &pos->member != (head);                        \
  507.      pos = n, n = list_entry(n->member.next, typeof(*n), member))

  508. /**
  509.  * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
  510.  * @pos:    the type * to use as a loop cursor.
  511.  * @n:        another type * to use as temporary storage
  512.  * @head:    the head for your list.
  513.  * @member:    the name of the list_struct within the struct.
  514.  *
  515.  * Iterate backwards over list of given type, safe against removal
  516.  * of list entry.
  517.  */
  518. #define list_for_each_entry_safe_reverse(pos, n, head, member)        \
  519.     for (pos = list_entry((head)->prev, typeof(*pos), member),    \
  520.         n = list_entry(pos->member.prev, typeof(*pos), member);    \
  521.      &pos->member != (head);                     \
  522.      pos = n, n = list_entry(n->member.prev, typeof(*n), member))

  523. /**
  524.  * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
  525.  * @pos:    the loop cursor used in the list_for_each_entry_safe loop
  526.  * @n:        temporary storage used in list_for_each_entry_safe
  527.  * @member:    the name of the list_struct within the struct.
  528.  *
  529.  * list_safe_reset_next is not safe to use in general if the list may be
  530.  * modified concurrently (eg. the lock is dropped in the loop body). An
  531.  * exception to this is if the cursor element (pos) is pinned in the list,
  532.  * and list_safe_reset_next is called after re-taking the lock and before
  533.  * completing the current iteration of the loop body.
  534.  */
  535. #define list_safe_reset_next(pos, n, member)                \
  536.     n = list_entry(pos->member.next, typeof(*pos), member)

  537. /*
  538.  * Double linked lists with a single pointer list head.
  539.  * Mostly useful for hash tables where the two pointer list head is
  540.  * too wasteful.
  541.  * You lose the ability to access the tail in O(1).
  542.  */

  543. #define HLIST_HEAD_INIT { .first = NULL }
  544. #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
  545. #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
  546. static inline void INIT_HLIST_NODE(struct hlist_node *h)
  547. {
  548.     h->next = NULL;
  549.     h->pprev = NULL;
  550. }

  551. static inline int hlist_unhashed(const struct hlist_node *h)
  552. {
  553.     return !h->pprev;
  554. }

  555. static inline int hlist_empty(const struct hlist_head *h)
  556. {
  557.     return !h->first;
  558. }

  559. static inline void __hlist_del(struct hlist_node *n)
  560. {
  561.     struct hlist_node *next = n->next;
  562.     struct hlist_node **pprev = n->pprev;
  563.     *pprev = next;
  564.     if (next)
  565.         next->pprev = pprev;
  566. }

  567. static inline void hlist_del(struct hlist_node *n)
  568. {
  569.     __hlist_del(n);
  570.     n->next = LIST_POISON1;
  571.     n->pprev = LIST_POISON2;
  572. }

  573. static inline void hlist_del_init(struct hlist_node *n)
  574. {
  575.     if (!hlist_unhashed(n)) {
  576.         __hlist_del(n);
  577.         INIT_HLIST_NODE(n);
  578.     }
  579. }

  580. static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
  581. {
  582.     struct hlist_node *first = h->first;
  583.     n->next = first;
  584.     if (first)
  585.         first->pprev = &n->next;
  586.     h->first = n;
  587.     n->pprev = &h->first;
  588. }

  589. /* next must be != NULL */
  590. static inline void hlist_add_before(struct hlist_node *n,
  591.                     struct hlist_node *next)
  592. {
  593.     n->pprev = next->pprev;
  594.     n->next = next;
  595.     next->pprev = &n->next;
  596.     *(n->pprev) = n;
  597. }

  598. static inline void hlist_add_after(struct hlist_node *n,
  599.                     struct hlist_node *next)
  600. {
  601.     next->next = n->next;
  602.     n->next = next;
  603.     next->pprev = &n->next;

  604.     if(next->next)
  605.         next->next->pprev = &next->next;
  606. }

  607. /* after that we'll appear to be on some hlist and hlist_del will work */
  608. static inline void hlist_add_fake(struct hlist_node *n)
  609. {
  610.     n->pprev = &n->next;
  611. }

  612. /*
  613.  * Move a list from one list head to another. Fixup the pprev
  614.  * reference of the first entry if it exists.
  615.  */
  616. static inline void hlist_move_list(struct hlist_head *old,
  617.                  struct hlist_head *new)
  618. {
  619.     new->first = old->first;
  620.     if (new->first)
  621.         new->first->pprev = &new->first;
  622.     old->first = NULL;
  623. }

  624. #define hlist_entry(ptr, type, member) container_of(ptr,type,member)

  625. #define hlist_for_each(pos, head) \
  626.     for (pos = (head)->first; pos ; pos = pos->next)

  627. #define hlist_for_each_safe(pos, n, head) \
  628.     for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
  629.      pos = n)

  630. /**
  631.  * hlist_for_each_entry    - iterate over list of given type
  632.  * @tpos:    the type * to use as a loop cursor.
  633.  * @pos:    the &struct hlist_node to use as a loop cursor.
  634.  * @head:    the head for your list.
  635.  * @member:    the name of the hlist_node within the struct.
  636.  */
  637. #define hlist_for_each_entry(tpos, pos, head, member)             \
  638.     for (pos = (head)->first;                     \
  639.      pos &&                             \
  640.         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  641.      pos = pos->next)

  642. /**
  643.  * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
  644.  * @tpos:    the type * to use as a loop cursor.
  645.  * @pos:    the &struct hlist_node to use as a loop cursor.
  646.  * @member:    the name of the hlist_node within the struct.
  647.  */
  648. #define hlist_for_each_entry_continue(tpos, pos, member)         \
  649.     for (pos = (pos)->next;                         \
  650.      pos &&                             \
  651.         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  652.      pos = pos->next)

  653. /**
  654.  * hlist_for_each_entry_from - iterate over a hlist continuing from current point
  655.  * @tpos:    the type * to use as a loop cursor.
  656.  * @pos:    the &struct hlist_node to use as a loop cursor.
  657.  * @member:    the name of the hlist_node within the struct.
  658.  */
  659. #define hlist_for_each_entry_from(tpos, pos, member)             \
  660.     for (; pos &&                             \
  661.         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  662.      pos = pos->next)

  663. /**
  664.  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  665.  * @tpos:    the type * to use as a loop cursor.
  666.  * @pos:    the &struct hlist_node to use as a loop cursor.
  667.  * @n:        another &struct hlist_node to use as temporary storage
  668.  * @head:    the head for your list.
  669.  * @member:    the name of the hlist_node within the struct.
  670.  */
  671. #define hlist_for_each_entry_safe(tpos, pos, n, head, member)          \
  672.     for (pos = (head)->first;                     \
  673.      pos && ({ n = pos->next; 1; }) &&                  \
  674.         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  675.      pos = n)

  676. #endif

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