Chinaunix首页 | 论坛 | 博客
  • 博客访问: 757866
  • 博文数量: 144
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1150
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-17 14:32
个人简介

小公司研发总监,既当司令也当兵!

文章分类

全部博文(144)

分类: LINUX

2018-08-25 22:54:52

list.rar

点击(此处)折叠或打开

  1. #ifndef _USER_LIST_H
  2. #define _USER_LIST_H

  3. struct list_head {
  4.     struct list_head *next, *prev;
  5. };

  6. /*
  7.  * Architectures might want to move the poison pointer offset
  8.  * into some well-recognized area such as 0xdead000000000000,
  9.  * that is also not mappable by user-space exploits:
  10.  */
  11. #define POISON_POINTER_DELTA 0

  12. /*
  13.  * These are non-NULL pointers that will result in page faults
  14.  * under normal circumstances, used to verify that nobody uses
  15.  * non-initialized list entries.
  16.  */
  17. #define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)
  18. #define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)


  19. #ifndef offsetof
  20. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  21. #endif


  22. /**
  23.  * container_of - cast a member of a structure out to the containing structure
  24.  * @ptr:    the pointer to the member.
  25.  * @type:    the type of the container struct this is embedded in.
  26.  * @member:    the name of the member within the struct.
  27.  *
  28.  */
  29. #define container_of(ptr, type, member) ({            \
  30.     const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
  31.     (type *)( (char *)__mptr - offsetof(type,member) );})


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

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

  42. #define LIST_HEAD(name) \
  43.     struct list_head name = LIST_HEAD_INIT(name)

  44. static inline void INIT_LIST_HEAD(struct list_head *list)
  45. {
  46.     list->next = list;
  47.     list->prev = list;
  48. }

  49. static inline void __list_add(struct list_head *new,
  50.                             struct list_head *prev,
  51.                             struct list_head *next)
  52. {
  53.     next->prev = new;
  54.     new->next = next;
  55.     new->prev = prev;
  56.     prev->next = new;
  57. }


  58. /**
  59.  * list_add - add a new entry
  60.  * @new: new entry to be added
  61.  * @head: list head to add it after
  62.  *
  63.  * Insert a new entry after the specified head.
  64.  * This is good for implementing stacks.
  65.  */
  66. static inline void list_add(struct list_head *new, struct list_head *head)
  67. {
  68.     __list_add(new, head, head->next);
  69. }


  70. /**
  71.  * list_add_tail - add a new entry
  72.  * @new: new entry to be added
  73.  * @head: list head to add it before
  74.  *
  75.  * Insert a new entry before the specified head.
  76.  * This is useful for implementing queues.
  77.  */
  78. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  79. {
  80.     __list_add(new, head->prev, head);
  81. }

  82. /*
  83.  * Delete a list entry by making the prev/next entries
  84.  * point to each other.
  85.  *
  86.  * This is only for internal list manipulation where we know
  87.  * the prev/next entries
  88.  */
  89. static inline void __list_del(struct list_head * prev, struct list_head * next)
  90. {
  91.     next->prev = prev;
  92.     prev->next = next;
  93. }

  94. /**
  95.  * list_del - deletes entry from list.
  96.  * @entry: the element to delete from the list.
  97.  * Note: list_empty() on entry does not return true after this, the entry is
  98.  * in an undefined state.
  99.  */

  100. static inline void __list_del_entry(struct list_head *entry)
  101. {
  102.     __list_del(entry->prev, entry->next);
  103. }

  104. static inline void list_del(struct list_head *entry)
  105. {
  106.     __list_del(entry->prev, entry->next);
  107.     entry->next = LIST_POISON1;
  108.     entry->prev = LIST_POISON2;
  109. }

  110. /**
  111.  * list_replace - replace old entry by new one
  112.  * @old : the element to be replaced
  113.  * @new : the new element to insert
  114.  *
  115.  * If @old was empty, it will be overwritten.
  116.  */
  117. static inline void list_replace(struct list_head *old,
  118.                 struct list_head *new)
  119. {
  120.     new->next = old->next;
  121.     new->next->prev = new;
  122.     new->prev = old->prev;
  123.     new->prev->next = new;
  124. }

  125. static inline void list_replace_init(struct list_head *old,
  126.                     struct list_head *new)
  127. {
  128.     list_replace(old, new);
  129.     INIT_LIST_HEAD(old);
  130. }

  131. /**
  132.  * list_del_init - deletes entry from list and reinitialize it.
  133.  * @entry: the element to delete from the list.
  134.  */
  135. static inline void list_del_init(struct list_head *entry)
  136. {
  137.     __list_del_entry(entry);
  138.     INIT_LIST_HEAD(entry);
  139. }

  140. /**
  141.  * list_move - delete from one list and add as another's head
  142.  * @list: the entry to move
  143.  * @head: the head that will precede our entry
  144.  */
  145. static inline void list_move(struct list_head *list, struct list_head *head)
  146. {
  147.     __list_del_entry(list);
  148.     list_add(list, head);
  149. }

  150. /**
  151.  * list_move_tail - delete from one list and add as another's tail
  152.  * @list: the entry to move
  153.  * @head: the head that will follow our entry
  154.  */
  155. static inline void list_move_tail(struct list_head *list,
  156.                  struct list_head *head)
  157. {
  158.     __list_del_entry(list);
  159.     list_add_tail(list, head);
  160. }

  161. /**
  162.  * list_is_last - tests whether @list is the last entry in list @head
  163.  * @list: the entry to test
  164.  * @head: the head of the list
  165.  */
  166. static inline int list_is_last(const struct list_head *list,
  167.                 const struct list_head *head)
  168. {
  169.     return list->next == head;
  170. }

  171. /**
  172.  * list_empty - tests whether a list is empty
  173.  * @head: the list to test.
  174.  */
  175. static inline int list_empty(const struct list_head *head)
  176. {
  177.     return head->next == head;
  178. }

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

  197. /**
  198.  * list_rotate_left - rotate the list to the left
  199.  * @head: the head of the list
  200.  */
  201. static inline void list_rotate_left(struct list_head *head)
  202. {
  203.     struct list_head *first;

  204.     if (!list_empty(head)) {
  205.         first = head->next;
  206.         list_move_tail(first, head);
  207.     }
  208. }

  209. /**
  210.  * list_is_singular - tests whether a list has just one entry.
  211.  * @head: the list to test.
  212.  */
  213. static inline int list_is_singular(const struct list_head *head)
  214. {
  215.     return !list_empty(head) && (head->next == head->prev);
  216. }

  217. static inline void __list_cut_position(struct list_head *list,
  218.         struct list_head *head, struct list_head *entry)
  219. {
  220.     struct list_head *new_first = entry->next;
  221.     list->next = head->next;
  222.     list->next->prev = list;
  223.     list->prev = entry;
  224.     entry->next = list;
  225.     head->next = new_first;
  226.     new_first->prev = head;
  227. }

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

  255. static inline void __list_splice(const struct list_head *list,
  256.                  struct list_head *prev,
  257.                  struct list_head *next)
  258. {
  259.     struct list_head *first = list->next;
  260.     struct list_head *last = list->prev;

  261.     first->prev = prev;
  262.     prev->next = first;

  263.     last->next = next;
  264.     next->prev = last;
  265. }

  266. /**
  267.  * list_splice - join two lists, this is designed for stacks
  268.  * @list: the new list to add.
  269.  * @head: the place to add it in the first list.
  270.  */
  271. static inline void list_splice(const struct list_head *list,
  272.                 struct list_head *head)
  273. {
  274.     if (!list_empty(list))
  275.         __list_splice(list, head, head->next);
  276. }

  277. /**
  278.  * list_splice_tail - join two lists, each list being a queue
  279.  * @list: the new list to add.
  280.  * @head: the place to add it in the first list.
  281.  */
  282. static inline void list_splice_tail(struct list_head *list,
  283.                 struct list_head *head)
  284. {
  285.     if (!list_empty(list))
  286.         __list_splice(list, head->prev, head);
  287. }

  288. /**
  289.  * list_splice_init - join two lists and reinitialise the emptied list.
  290.  * @list: the new list to add.
  291.  * @head: the place to add it in the first list.
  292.  *
  293.  * The list at @list is reinitialised
  294.  */
  295. static inline void list_splice_init(struct list_head *list,
  296.                  struct list_head *head)
  297. {
  298.     if (!list_empty(list)) {
  299.         __list_splice(list, head, head->next);
  300.         INIT_LIST_HEAD(list);
  301.     }
  302. }

  303. /**
  304.  * list_splice_tail_init - join two lists and reinitialise the emptied list
  305.  * @list: the new list to add.
  306.  * @head: the place to add it in the first list.
  307.  *
  308.  * Each of the lists is a queue.
  309.  * The list at @list is reinitialised
  310.  */
  311. static inline void list_splice_tail_init(struct list_head *list,
  312.                      struct list_head *head)
  313. {
  314.     if (!list_empty(list)) {
  315.         __list_splice(list, head->prev, head);
  316.         INIT_LIST_HEAD(list);
  317.     }
  318. }

  319. /**
  320.  * list_entry - get the struct for this entry
  321.  * @ptr:    the &struct list_head pointer.
  322.  * @type:    the type of the struct this is embedded in.
  323.  * @member:    the name of the list_struct within the struct.
  324.  */
  325. #define list_entry(ptr, type, member) \
  326.     container_of(ptr, type, member)

  327. /**
  328.  * list_first_entry - get the first element from a list
  329.  * @ptr:    the list head to take the element from.
  330.  * @type:    the type of the struct this is embedded in.
  331.  * @member:    the name of the list_struct within the struct.
  332.  *
  333.  * Note, that list is expected to be not empty.
  334.  */
  335. #define list_first_entry(ptr, type, member) \
  336.     list_entry((ptr)->next, type, member)

  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. #define list_for_each(pos, head) \
  343.     for (pos = (head)->next; pos != (head); pos = pos->next)

  344. /**
  345.  * __list_for_each    -    iterate over a list
  346.  * @pos:    the &struct list_head to use as a loop cursor.
  347.  * @head:    the head for your list.
  348.  *
  349.  * This variant doesn't differ from list_for_each() any more.
  350.  * We don't do prefetching in either case.
  351.  */
  352. #define __list_for_each(pos, head) \
  353.     for (pos = (head)->next; pos != (head); pos = pos->next)

  354. /**
  355.  * list_for_each_prev    -    iterate over a list backwards
  356.  * @pos:    the &struct list_head to use as a loop cursor.
  357.  * @head:    the head for your list.
  358.  */
  359. #define list_for_each_prev(pos, head) \
  360.     for (pos = (head)->prev; pos != (head); pos = pos->prev)

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

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

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

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

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

  410. /**
  411.  * list_for_each_entry_continue - continue iteration 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.  * Continue to iterate over list of given type, continuing after
  417.  * the current position.
  418.  */
  419. #define list_for_each_entry_continue(pos, head, member)         \
  420.     for (pos = list_entry(pos->member.next, typeof(*pos), member);    \
  421.      &pos->member != (head);    \
  422.      pos = list_entry(pos->member.next, typeof(*pos), member))

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

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

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

  459. /**
  460.  * list_for_each_entry_safe_continue - continue list iteration safe against removal
  461.  * @pos:    the type * to use as a loop cursor.
  462.  * @n:        another type * to use as temporary storage
  463.  * @head:    the head for your list.
  464.  * @member:    the name of the list_struct within the struct.
  465.  *
  466.  * Iterate over list of given type, continuing after current point,
  467.  * safe against removal of list entry.
  468.  */
  469. #define list_for_each_entry_safe_continue(pos, n, head, member)         \
  470.     for (pos = list_entry(pos->member.next, typeof(*pos), member),         \
  471.         n = list_entry(pos->member.next, typeof(*pos), member);        \
  472.      &pos->member != (head);                        \
  473.      pos = n, n = list_entry(n->member.next, typeof(*n), member))

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

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

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

  517. #endif


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