list.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (C) 2002 Free Software Foundation, Inc.
  3. * (originally part of the GNU C Library and Userspace RCU)
  4. * Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  5. *
  6. * Copyright (C) 2009 Pierre-Marc Fournier
  7. * Conversion to RCU list.
  8. * Copyright (C) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, see
  22. * <http://www.gnu.org/licenses/>.
  23. */
  24. #ifndef LIST_H
  25. #define LIST_H 1
  26. /*
  27. * The definitions of this file are adopted from those which can be
  28. * found in the Linux kernel headers to enable people familiar with the
  29. * latter find their way in these sources as well.
  30. */
  31. /* Basic type for the double-link list. */
  32. struct list_head {
  33. struct list_head *next, *prev;
  34. };
  35. /* avoid conflicts with BSD-only sys/queue.h */
  36. #undef LIST_HEAD
  37. /* Define a variable with the head and tail of the list. */
  38. #define LIST_HEAD(name) \
  39. struct list_head name = { &(name), &(name) }
  40. /* Initialize a new list head. */
  41. #define INIT_LIST_HEAD(ptr) \
  42. (ptr)->next = (ptr)->prev = (ptr)
  43. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  44. /* Add new element at the head of the list. */
  45. static inline void list_add(struct list_head *newp, struct list_head *head)
  46. {
  47. head->next->prev = newp;
  48. newp->next = head->next;
  49. newp->prev = head;
  50. head->next = newp;
  51. }
  52. /* Add new element at the tail of the list. */
  53. static inline void list_add_tail(struct list_head *newp, struct list_head *head)
  54. {
  55. head->prev->next = newp;
  56. newp->next = head;
  57. newp->prev = head->prev;
  58. head->prev = newp;
  59. }
  60. /* Remove element from list. */
  61. static inline void __list_del(struct list_head *prev, struct list_head *next)
  62. {
  63. next->prev = prev;
  64. prev->next = next;
  65. }
  66. /* Remove element from list. */
  67. static inline void list_del(struct list_head *elem)
  68. {
  69. __list_del(elem->prev, elem->next);
  70. }
  71. /* Remove element from list, initializing the element's list pointers. */
  72. static inline void list_del_init(struct list_head *elem)
  73. {
  74. list_del(elem);
  75. INIT_LIST_HEAD(elem);
  76. }
  77. /* Delete from list, add to another list as head. */
  78. static inline void list_move(struct list_head *elem, struct list_head *head)
  79. {
  80. __list_del(elem->prev, elem->next);
  81. list_add(elem, head);
  82. }
  83. /* Replace an old entry. */
  84. static inline void list_replace(struct list_head *old, struct list_head *newp)
  85. {
  86. newp->next = old->next;
  87. newp->prev = old->prev;
  88. newp->prev->next = newp;
  89. newp->next->prev = newp;
  90. }
  91. /* Join two lists. */
  92. static inline void list_splice(struct list_head *add, struct list_head *head)
  93. {
  94. /* Do nothing if the list which gets added is empty. */
  95. if (add != add->next) {
  96. add->next->prev = head;
  97. add->prev->next = head->next;
  98. head->next->prev = add->prev;
  99. head->next = add->next;
  100. }
  101. }
  102. /* Get typed element from list at a given position. */
  103. #define list_entry(ptr, type, member) \
  104. ((type *) ((char *) (ptr) - offsetof(type, member)))
  105. /* Get first entry from a list. */
  106. #define list_first_entry(ptr, type, member) \
  107. list_entry((ptr)->next, type, member)
  108. /* Iterate forward over the elements of the list. */
  109. #define list_for_each(pos, head) \
  110. for (pos = (head)->next; pos != (head); pos = pos->next)
  111. /*
  112. * Iterate forward over the elements list. The list elements can be
  113. * removed from the list while doing this.
  114. */
  115. #define list_for_each_safe(pos, p, head) \
  116. for (pos = (head)->next, p = pos->next; \
  117. pos != (head); \
  118. pos = p, p = pos->next)
  119. /* Iterate backward over the elements of the list. */
  120. #define list_for_each_prev(pos, head) \
  121. for (pos = (head)->prev; pos != (head); pos = pos->prev)
  122. /*
  123. * Iterate backwards over the elements list. The list elements can be
  124. * removed from the list while doing this.
  125. */
  126. #define list_for_each_prev_safe(pos, p, head) \
  127. for (pos = (head)->prev, p = pos->prev; \
  128. pos != (head); \
  129. pos = p, p = pos->prev)
  130. static inline int list_empty(struct list_head *head)
  131. {
  132. return head == head->next;
  133. }
  134. static inline void list_replace_init(struct list_head *old,
  135. struct list_head *newp)
  136. {
  137. struct list_head *head = old->next;
  138. list_del(old);
  139. list_add_tail(newp, head);
  140. INIT_LIST_HEAD(old);
  141. }
  142. /*
  143. * This is exactly the same as a normal list_head, except that it can be
  144. * declared volatile (e.g., if you have a list that may be accessed from signal
  145. * handlers).
  146. */
  147. struct volatile_list_head {
  148. volatile struct volatile_list_head *next, *prev;
  149. };
  150. #define VOLATILE_LIST_HEAD(name) \
  151. volatile struct volatile_list_head name = { &(name), &(name) }
  152. static inline void __volatile_list_del(volatile struct volatile_list_head *prev,
  153. volatile struct volatile_list_head *next)
  154. {
  155. next->prev = prev;
  156. prev->next = next;
  157. }
  158. static inline void volatile_list_del(volatile struct volatile_list_head *elem)
  159. {
  160. __volatile_list_del(elem->prev, elem->next);
  161. }
  162. static inline int volatile_list_empty(volatile struct volatile_list_head *head)
  163. {
  164. return head == head->next;
  165. }
  166. static inline void volatile_list_add(volatile struct volatile_list_head *newp,
  167. volatile struct volatile_list_head *head)
  168. {
  169. head->next->prev = newp;
  170. newp->next = head->next;
  171. newp->prev = head;
  172. head->next = newp;
  173. }
  174. #endif /* LIST_H */