Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1906289
  • 博文数量: 217
  • 博客积分: 4362
  • 博客等级: 上校
  • 技术积分: 4180
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-20 09:31
文章分类

全部博文(217)

文章存档

2017年(1)

2015年(2)

2014年(2)

2013年(6)

2012年(42)

2011年(119)

2010年(28)

2009年(17)

分类: C/C++

2011-08-25 21:05:12

自己把list.h源文件的代码一下部分做了手脚,下次如果在你的程序当中有用到list或者是hlist的时候,你就可以把我修改之后的内容复制一下,命名为list.h,然后在你的用户程序中写着这么一句
#include "list.h"
这样的话,你就可以很好的对内核链表进行使用了。

改动的地方(代码中的红色部分):
1)
#define LIST_POISON1  (void *) 0x0
#define LIST_POISON2  (void *) 0x0

LIST_POISON1和LIST_POISON2在内核中定义的是一个不能访问的地址,这里我把地址宏定义成了上面的东东。

2)
#define prefetch(x)

对于这个宏函数,主要是提高预取的效率,具体我也没有研究透,在这里我把它定义成了一个空。

3)
#define container_of(ptr, type, member) ({ \
const typeof(((type *)0)->member ) *__mptr = (ptr); \
(type *)((char *)__mptr - ((size_t) &((type *)0)->member));})

对于这个是一个难点,就是根据结构体中的某个成员地址,得到这个结构体变量的首地址。这段代码很经典,强烈建议大家把这段代码用到自己所写的程序当中,不仅仅是只在这里使用。

附录:
自己修改的list.h头文件。
  1. #ifndef _LINUX_LIST_H
  2. #define _LINUX_LIST_H

  3. #define LIST_POISON1 (void *) 0x0
  4. #define LIST_POISON2 (void *) 0x0
  5. #define prefetch(x) NULL

  6. #define container_of(ptr, type, member) ({    \
  7.     const typeof(((type *)0)->member ) *__mptr = (ptr); \
  8.     (type *)((char *)__mptr - ((size_t) &((type *)0)->member));})

  9. /*
  10.  * Simple doubly linked list implementation.
  11.  *
  12.  * Some of the internal functions ("__xxx") are useful when
  13.  * manipulating whole lists rather than single entries, as
  14.  * sometimes we already know the next/prev entries and we can
  15.  * generate better code by using them directly rather than
  16.  * using the generic single-entry routines.
  17.  */

  18. struct list_head {
  19.     struct list_head *next, *prev;
  20. };

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

  22. #define LIST_HEAD(name) \
  23.     struct list_head name = LIST_HEAD_INIT(name)

  24. static inline void INIT_LIST_HEAD(struct list_head *list)
  25. {
  26.     list->next = list;
  27.     list->prev = list;
  28. }

  29. /*
  30.  * Insert a new entry between two known consecutive entries.
  31.  *
  32.  * This is only for internal list manipulation where we know
  33.  * the prev/next entries
  34.  */
  35. #ifndef CONFIG_DEBUG_LIST
  36. static inline void __list_add(struct list_head *new,
  37.              struct list_head *prev,
  38.              struct list_head *next)
  39. {
  40.     next->prev = new;
  41.     new->next = next;
  42.     new->prev = prev;
  43.     prev->next = new;
  44. }
  45. #else
  46. extern void __list_add(struct list_head *new,
  47.              struct list_head *prev,
  48.              struct list_head *next);
  49. #endif

  50. /**
  51.  * list_add - add a new entry
  52.  * @new: new entry to be added
  53.  * @head: list head to add it after
  54.  *
  55.  * Insert a new entry after the specified head.
  56.  * This is good for implementing stacks.
  57.  */
  58. static inline void list_add(struct list_head *new, struct list_head *head)
  59. {
  60.     __list_add(new, head, head->next);
  61. }


  62. /**
  63.  * list_add_tail - add a new entry
  64.  * @new: new entry to be added
  65.  * @head: list head to add it before
  66.  *
  67.  * Insert a new entry before the specified head.
  68.  * This is useful for implementing queues.
  69.  */
  70. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  71. {
  72.     __list_add(new, head->prev, head);
  73. }

  74. /*
  75.  * Delete a list entry by making the prev/next entries
  76.  * point to each other.
  77.  *
  78.  * This is only for internal list manipulation where we know
  79.  * the prev/next entries
  80.  */
  81. static inline void __list_del(struct list_head * prev, struct list_head * next)
  82. {
  83.     next->prev = prev;
  84.     prev->next = next;
  85. }

  86. /**
  87.  * list_del - deletes entry from list.
  88.  * @entry: the element to delete from the list.
  89.  * Note: list_empty() on entry does not return true after this, the entry is
  90.  * in an undefined state.
  91.  */
  92. #ifndef CONFIG_DEBUG_LIST
  93. static inline void list_del(struct list_head *entry)
  94. {
  95.     __list_del(entry->prev, entry->next);
  96.     entry->next = LIST_POISON1;
  97.     entry->prev = LIST_POISON2;
  98. }
  99. #else
  100. extern void list_del(struct list_head *entry);
  101. #endif

  102. /**
  103.  * list_replace - replace old entry by new one
  104.  * @old : the element to be replaced
  105.  * @new : the new element to insert
  106.  *
  107.  * If @old was empty, it will be overwritten.
  108.  */
  109. static inline void list_replace(struct list_head *old,
  110.                 struct list_head *new)
  111. {
  112.     new->next = old->next;
  113.     new->next->prev = new;
  114.     new->prev = old->prev;
  115.     new->prev->next = new;
  116. }

  117. static inline void list_replace_init(struct list_head *old,
  118.                     struct list_head *new)
  119. {
  120.     list_replace(old, new);
  121.     INIT_LIST_HEAD(old);
  122. }

  123. /**
  124.  * list_del_init - deletes entry from list and reinitialize it.
  125.  * @entry: the element to delete from the list.
  126.  */
  127. static inline void list_del_init(struct list_head *entry)
  128. {
  129.     __list_del(entry->prev, entry->next);
  130.     INIT_LIST_HEAD(entry);
  131. }

  132. /**
  133.  * list_move - delete from one list and add as another's head
  134.  * @list: the entry to move
  135.  * @head: the head that will precede our entry
  136.  */
  137. static inline void list_move(struct list_head *list, struct list_head *head)
  138. {
  139.     __list_del(list->prev, list->next);
  140.     list_add(list, head);
  141. }

  142. /**
  143.  * list_move_tail - delete from one list and add as another's tail
  144.  * @list: the entry to move
  145.  * @head: the head that will follow our entry
  146.  */
  147. static inline void list_move_tail(struct list_head *list,
  148.                  struct list_head *head)
  149. {
  150.     __list_del(list->prev, list->next);
  151.     list_add_tail(list, head);
  152. }

  153. /**
  154.  * list_is_last - tests whether @list is the last entry in list @head
  155.  * @list: the entry to test
  156.  * @head: the head of the list
  157.  */
  158. static inline int list_is_last(const struct list_head *list,
  159.                 const struct list_head *head)
  160. {
  161.     return list->next == head;
  162. }

  163. /**
  164.  * list_empty - tests whether a list is empty
  165.  * @head: the list to test.
  166.  */
  167. static inline int list_empty(const struct list_head *head)
  168. {
  169.     return head->next == head;
  170. }

  171. /**
  172.  * list_empty_careful - tests whether a list is empty and not being modified
  173.  * @head: the list to test
  174.  *
  175.  * Description:
  176.  * tests whether a list is empty _and_ checks that no other CPU might be
  177.  * in the process of modifying either member (next or prev)
  178.  *
  179.  * NOTE: using list_empty_careful() without synchronization
  180.  * can only be safe if the only activity that can happen
  181.  * to the list entry is list_del_init(). Eg. it cannot be used
  182.  * if another CPU could re-list_add() it.
  183.  */
  184. static inline int list_empty_careful(const struct list_head *head)
  185. {
  186.     struct list_head *next = head->next;
  187.     return (next == head) && (next == head->prev);
  188. }

  189. /**
  190.  * list_rotate_left - rotate the list to the left
  191.  * @head: the head of the list
  192.  */
  193. static inline void list_rotate_left(struct list_head *head)
  194. {
  195.     struct list_head *first;

  196.     if (!list_empty(head)) {
  197.         first = head->next;
  198.         list_move_tail(first, head);
  199.     }
  200. }

  201. /**
  202.  * list_is_singular - tests whether a list has just one entry.
  203.  * @head: the list to test.
  204.  */
  205. static inline int list_is_singular(const struct list_head *head)
  206. {
  207.     return !list_empty(head) && (head->next == head->prev);
  208. }

  209. static inline void __list_cut_position(struct list_head *list,
  210.         struct list_head *head, struct list_head *entry)
  211. {
  212.     struct list_head *new_first = entry->next;
  213.     list->next = head->next;
  214.     list->next->prev = list;
  215.     list->prev = entry;
  216.     entry->next = list;
  217.     head->next = new_first;
  218.     new_first->prev = head;
  219. }

  220. /**
  221.  * list_cut_position - cut a list into two
  222.  * @list: a new list to add all removed entries
  223.  * @head: a list with entries
  224.  * @entry: an entry within head, could be the head itself
  225.  *    and if so we won't cut the list
  226.  *
  227.  * This helper moves the initial part of @head, up to and
  228.  * including @entry, from @head to @list. You should
  229.  * pass on @entry an element you know is on @head. @list
  230.  * should be an empty list or a list you do not care about
  231.  * losing its data.
  232.  *
  233.  */
  234. static inline void list_cut_position(struct list_head *list,
  235.         struct list_head *head, struct list_head *entry)
  236. {
  237.     if (list_empty(head))
  238.         return;
  239.     if (list_is_singular(head) &&
  240.         (head->next != entry && head != entry))
  241.         return;
  242.     if (entry == head)
  243.         INIT_LIST_HEAD(list);
  244.     else
  245.         __list_cut_position(list, head, entry);
  246. }

  247. static inline void __list_splice(const struct list_head *list,
  248.                  struct list_head *prev,
  249.                  struct list_head *next)
  250. {
  251.     struct list_head *first = list->next;
  252.     struct list_head *last = list->prev;

  253.     first->prev = prev;
  254.     prev->next = first;

  255.     last->next = next;
  256.     next->prev = last;
  257. }

  258. /**
  259.  * list_splice - join two lists, this is designed for stacks
  260.  * @list: the new list to add.
  261.  * @head: the place to add it in the first list.
  262.  */
  263. static inline void list_splice(const struct list_head *list,
  264.                 struct list_head *head)
  265. {
  266.     if (!list_empty(list))
  267.         __list_splice(list, head, head->next);
  268. }

  269. /**
  270.  * list_splice_tail - join two lists, each list being a queue
  271.  * @list: the new list to add.
  272.  * @head: the place to add it in the first list.
  273.  */
  274. static inline void list_splice_tail(struct list_head *list,
  275.                 struct list_head *head)
  276. {
  277.     if (!list_empty(list))
  278.         __list_splice(list, head->prev, head);
  279. }

  280. /**
  281.  * list_splice_init - join two lists and reinitialise the emptied list.
  282.  * @list: the new list to add.
  283.  * @head: the place to add it in the first list.
  284.  *
  285.  * The list at @list is reinitialised
  286.  */
  287. static inline void list_splice_init(struct list_head *list,
  288.                  struct list_head *head)
  289. {
  290.     if (!list_empty(list)) {
  291.         __list_splice(list, head, head->next);
  292.         INIT_LIST_HEAD(list);
  293.     }
  294. }

  295. /**
  296.  * list_splice_tail_init - join two lists and reinitialise the emptied list
  297.  * @list: the new list to add.
  298.  * @head: the place to add it in the first list.
  299.  *
  300.  * Each of the lists is a queue.
  301.  * The list at @list is reinitialised
  302.  */
  303. static inline void list_splice_tail_init(struct list_head *list,
  304.                      struct list_head *head)
  305. {
  306.     if (!list_empty(list)) {
  307.         __list_splice(list, head->prev, head);
  308.         INIT_LIST_HEAD(list);
  309.     }
  310. }

  311. /**
  312.  * list_entry - get the struct for this entry
  313.  * @ptr:    the &struct list_head pointer.
  314.  * @type:    the type of the struct this is embedded in.
  315.  * @member:    the name of the list_struct within the struct.
  316.  */
  317. #define list_entry(ptr, type, member) \
  318.     container_of(ptr, type, member)

  319. /**
  320.  * list_first_entry - get the first element from a list
  321.  * @ptr:    the list head to take the element from.
  322.  * @type:    the type of the struct this is embedded in.
  323.  * @member:    the name of the list_struct within the struct.
  324.  *
  325.  * Note, that list is expected to be not empty.
  326.  */
  327. #define list_first_entry(ptr, type, member) \
  328.     list_entry((ptr)->next, type, member)

  329. /**
  330.  * list_for_each    -    iterate over a list
  331.  * @pos:    the &struct list_head to use as a loop cursor.
  332.  * @head:    the head for your list.
  333.  */
  334. #define list_for_each(pos, head) \
  335.     for (pos = (head)->next; prefetch(pos->next), pos != (head); \
  336.             pos = pos->next)

  337. /**
  338.  * __list_for_each    -    iterate over a list
  339.  * @pos:    the &struct list_head to use as a loop cursor.
  340.  * @head:    the head for your list.
  341.  *
  342.  * This variant differs from list_for_each() in that it's the
  343.  * simplest possible list iteration code, no prefetching is done.
  344.  * Use this for code that knows the list to be very short (empty
  345.  * or 1 entry) most of the time.
  346.  */
  347. #define __list_for_each(pos, head) \
  348.     for (pos = (head)->next; pos != (head); pos = pos->next)

  349. /**
  350.  * list_for_each_prev    -    iterate over a list backwards
  351.  * @pos:    the &struct list_head to use as a loop cursor.
  352.  * @head:    the head for your list.
  353.  */
  354. #define list_for_each_prev(pos, head) \
  355.     for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
  356.             pos = pos->prev)

  357. /**
  358.  * list_for_each_safe - iterate over a list safe against removal of list entry
  359.  * @pos:    the &struct list_head to use as a loop cursor.
  360.  * @n:        another &struct list_head to use as temporary storage
  361.  * @head:    the head for your list.
  362.  */
  363. #define list_for_each_safe(pos, n, head) \
  364.     for (pos = (head)->next, n = pos->next; pos != (head); \
  365.         pos = n, n = pos->next)

  366. /**
  367.  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
  368.  * @pos:    the &struct list_head to use as a loop cursor.
  369.  * @n:        another &struct list_head to use as temporary storage
  370.  * @head:    the head for your list.
  371.  */
  372. #define list_for_each_prev_safe(pos, n, head) \
  373.     for (pos = (head)->prev, n = pos->prev; \
  374.      prefetch(pos->prev), pos != (head); \
  375.      pos = n, n = pos->prev)

  376. /**
  377.  * list_for_each_entry    -    iterate over list of given type
  378.  * @pos:    the type * to use as a loop cursor.
  379.  * @head:    the head for your list.
  380.  * @member:    the name of the list_struct within the struct.
  381.  */
  382. #define list_for_each_entry(pos, head, member)                \
  383.     for (pos = list_entry((head)->next, typeof(*pos), member);    \
  384.      prefetch(pos->member.next), &pos->member != (head);     \
  385.      pos = list_entry(pos->member.next, typeof(*pos), member))

  386. /**
  387.  * list_for_each_entry_reverse - iterate backwards over list of given type.
  388.  * @pos:    the type * to use as a loop cursor.
  389.  * @head:    the head for your list.
  390.  * @member:    the name of the list_struct within the struct.
  391.  */
  392. #define list_for_each_entry_reverse(pos, head, member)            \
  393.     for (pos = list_entry((head)->prev, typeof(*pos), member);    \
  394.      prefetch(pos->member.prev), &pos->member != (head);     \
  395.      pos = list_entry(pos->member.prev, typeof(*pos), member))

  396. /**
  397.  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
  398.  * @pos:    the type * to use as a start point
  399.  * @head:    the head of the list
  400.  * @member:    the name of the list_struct within the struct.
  401.  *
  402.  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
  403.  */
  404. #define list_prepare_entry(pos, head, member) \
  405.     ((pos) ? : list_entry(head, typeof(*pos), member))

  406. /**
  407.  * list_for_each_entry_continue - continue iteration over list of given type
  408.  * @pos:    the type * to use as a loop cursor.
  409.  * @head:    the head for your list.
  410.  * @member:    the name of the list_struct within the struct.
  411.  *
  412.  * Continue to iterate over list of given type, continuing after
  413.  * the current position.
  414.  */
  415. #define list_for_each_entry_continue(pos, head, member)         \
  416.     for (pos = list_entry(pos->member.next, typeof(*pos), member);    \
  417.      prefetch(pos->member.next), &pos->member != (head);    \
  418.      pos = list_entry(pos->member.next, typeof(*pos), member))

  419. /**
  420.  * list_for_each_entry_continue_reverse - iterate backwards from the given point
  421.  * @pos:    the type * to use as a loop cursor.
  422.  * @head:    the head for your list.
  423.  * @member:    the name of the list_struct within the struct.
  424.  *
  425.  * Start to iterate over list of given type backwards, continuing after
  426.  * the current position.
  427.  */
  428. #define list_for_each_entry_continue_reverse(pos, head, member)        \
  429.     for (pos = list_entry(pos->member.prev, typeof(*pos), member);    \
  430.      prefetch(pos->member.prev), &pos->member != (head);    \
  431.      pos = list_entry(pos->member.prev, typeof(*pos), member))

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

  443. /**
  444.  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  445.  * @pos:    the type * to use as a loop cursor.
  446.  * @n:        another type * to use as temporary storage
  447.  * @head:    the head for your list.
  448.  * @member:    the name of the list_struct within the struct.
  449.  */
  450. #define list_for_each_entry_safe(pos, n, head, member)            \
  451.     for (pos = list_entry((head)->next, typeof(*pos), member),    \
  452.         n = list_entry(pos->member.next, typeof(*pos), member);    \
  453.      &pos->member != (head);                     \
  454.      pos = n, n = list_entry(n->member.next, typeof(*n), member))

  455. /**
  456.  * list_for_each_entry_safe_continue - continue list iteration safe against removal
  457.  * @pos:    the type * to use as a loop cursor.
  458.  * @n:        another type * to use as temporary storage
  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 after current point,
  463.  * safe against removal of list entry.
  464.  */
  465. #define list_for_each_entry_safe_continue(pos, n, head, member)         \
  466.     for (pos = list_entry(pos->member.next, typeof(*pos), member),         \
  467.         n = list_entry(pos->member.next, typeof(*pos), member);        \
  468.      &pos->member != (head);                        \
  469.      pos = n, n = list_entry(n->member.next, typeof(*n), member))

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

  484. /**
  485.  * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
  486.  * @pos:    the type * to use as a loop cursor.
  487.  * @n:        another type * to use as temporary storage
  488.  * @head:    the head for your list.
  489.  * @member:    the name of the list_struct within the struct.
  490.  *
  491.  * Iterate backwards over list of given type, safe against removal
  492.  * of list entry.
  493.  */
  494. #define list_for_each_entry_safe_reverse(pos, n, head, member)        \
  495.     for (pos = list_entry((head)->prev, typeof(*pos), member),    \
  496.         n = list_entry(pos->member.prev, typeof(*pos), member);    \
  497.      &pos->member != (head);                     \
  498.      pos = n, n = list_entry(n->member.prev, typeof(*n), member))

  499. /**
  500.  * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
  501.  * @pos:    the loop cursor used in the list_for_each_entry_safe loop
  502.  * @n:        temporary storage used in list_for_each_entry_safe
  503.  * @member:    the name of the list_struct within the struct.
  504.  *
  505.  * list_safe_reset_next is not safe to use in general if the list may be
  506.  * modified concurrently (eg. the lock is dropped in the loop body). An
  507.  * exception to this is if the cursor element (pos) is pinned in the list,
  508.  * and list_safe_reset_next is called after re-taking the lock and before
  509.  * completing the current iteration of the loop body.
  510.  */
  511. #define list_safe_reset_next(pos, n, member)                \
  512.     n = list_entry(pos->member.next, typeof(*pos), member)

  513. /*
  514.  * Double linked lists with a single pointer list head.
  515.  * Mostly useful for hash tables where the two pointer list head is
  516.  * too wasteful.
  517.  * You lose the ability to access the tail in O(1).
  518.  */

  519. struct hlist_head {
  520.     struct hlist_node *first;
  521. };

  522. struct hlist_node {
  523.     struct hlist_node *next, **pprev;
  524. };

  525. #define HLIST_HEAD_INIT { .first = NULL }
  526. #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
  527. #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
  528. static inline void INIT_HLIST_NODE(struct hlist_node *h)
  529. {
  530.     h->next = NULL;
  531.     h->pprev = NULL;
  532. }

  533. static inline int hlist_unhashed(const struct hlist_node *h)
  534. {
  535.     return !h->pprev;
  536. }

  537. static inline int hlist_empty(const struct hlist_head *h)
  538. {
  539.     return !h->first;
  540. }

  541. static inline void __hlist_del(struct hlist_node *n)
  542. {
  543.     struct hlist_node *next = n->next;
  544.     struct hlist_node **pprev = n->pprev;
  545.     *pprev = next;
  546.     if (next)
  547.         next->pprev = pprev;
  548. }

  549. static inline void hlist_del(struct hlist_node *n)
  550. {
  551.     __hlist_del(n);
  552.     n->next = LIST_POISON1;
  553.     n->pprev = LIST_POISON2;
  554. }

  555. static inline void hlist_del_init(struct hlist_node *n)
  556. {
  557.     if (!hlist_unhashed(n)) {
  558.         __hlist_del(n);
  559.         INIT_HLIST_NODE(n);
  560.     }
  561. }

  562. static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
  563. {
  564.     struct hlist_node *first = h->first;
  565.     n->next = first;
  566.     if (first)
  567.         first->pprev = &n->next;
  568.     h->first = n;
  569.     n->pprev = &h->first;
  570. }

  571. /* next must be != NULL */
  572. static inline void hlist_add_before(struct hlist_node *n,
  573.                     struct hlist_node *next)
  574. {
  575.     n->pprev = next->pprev;
  576.     n->next = next;
  577.     next->pprev = &n->next;
  578.     *(n->pprev) = n;
  579. }

  580. static inline void hlist_add_after(struct hlist_node *n,
  581.                     struct hlist_node *next)
  582. {
  583.     next->next = n->next;
  584.     n->next = next;
  585.     next->pprev = &n->next;

  586.     if(next->next)
  587.         next->next->pprev = &next->next;
  588. }

  589. /*
  590.  * Move a list from one list head to another. Fixup the pprev
  591.  * reference of the first entry if it exists.
  592.  */
  593. static inline void hlist_move_list(struct hlist_head *old,
  594.                  struct hlist_head *new)
  595. {
  596.     new->first = old->first;
  597.     if (new->first)
  598.         new->first->pprev = &new->first;
  599.     old->first = NULL;
  600. }

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

  602. #define hlist_for_each(pos, head) \
  603.     for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
  604.      pos = pos->next)

  605. #define hlist_for_each_safe(pos, n, head) \
  606.     for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
  607.      pos = n)

  608. /**
  609.  * hlist_for_each_entry    - iterate over list of given type
  610.  * @tpos:    the type * to use as a loop cursor.
  611.  * @pos:    the &struct hlist_node to use as a loop cursor.
  612.  * @head:    the head for your list.
  613.  * @member:    the name of the hlist_node within the struct.
  614.  */
  615. #define hlist_for_each_entry(tpos, pos, head, member)             \
  616.     for (pos = (head)->first;                     \
  617.      pos && ({ prefetch(pos->next); 1;}) &&             \
  618.         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  619.      pos = pos->next)

  620. /**
  621.  * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
  622.  * @tpos:    the type * to use as a loop cursor.
  623.  * @pos:    the &struct hlist_node to use as a loop cursor.
  624.  * @member:    the name of the hlist_node within the struct.
  625.  */
  626. #define hlist_for_each_entry_continue(tpos, pos, member)         \
  627.     for (pos = (pos)->next;                         \
  628.      pos && ({ prefetch(pos->next); 1;}) &&             \
  629.         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  630.      pos = pos->next)

  631. /**
  632.  * hlist_for_each_entry_from - iterate over a hlist continuing from current point
  633.  * @tpos:    the type * to use as a loop cursor.
  634.  * @pos:    the &struct hlist_node to use as a loop cursor.
  635.  * @member:    the name of the hlist_node within the struct.
  636.  */
  637. #define hlist_for_each_entry_from(tpos, pos, member)             \
  638.     for (; pos && ({ prefetch(pos->next); 1;}) &&             \
  639.         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  640.      pos = pos->next)

  641. /**
  642.  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  643.  * @tpos:    the type * to use as a loop cursor.
  644.  * @pos:    the &struct hlist_node to use as a loop cursor.
  645.  * @n:        another &struct hlist_node to use as temporary storage
  646.  * @head:    the head for your list.
  647.  * @member:    the name of the hlist_node within the struct.
  648.  */
  649. #define hlist_for_each_entry_safe(tpos, pos, n, head, member)          \
  650.     for (pos = (head)->first;                     \
  651.      pos && ({ n = pos->next; 1; }) &&                  \
  652.         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  653.      pos = n)

  654. #endif
阅读(2676) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

sunjiangang-ok2011-08-26 11:32:39

sygspjlsj: 指针ptr指向结构体type中的成员member;通过指针ptr,返回结构体type的起始地址

#define container_of(ptr, type, member) ({                  \
    const t.....
谢谢~

sygspjlsj2011-08-26 11:12:18

指针ptr指向结构体type中的成员member;通过指针ptr,返回结构体type的起始地址

#define container_of(ptr, type, member) ({                  \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
1.ptr为物理地址,其类型和member类型一致,最终使用typeof( ((type *)0)->member )