list.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #ifndef _LINUX_LIST_H
  2. #define _LINUX_LIST_H
  3. #ifndef ARCH_HAS_PREFETCH
  4. #define ARCH_HAS_PREFETCH
  5. static inline void prefetch(const void *x) {;}
  6. #endif
  7. /*
  8. * Simple doubly linked list implementation.
  9. *
  10. * Some of the internal functions ("__xxx") are useful when
  11. * manipulating whole lists rather than single entries, as
  12. * sometimes we already know the next/prev entries and we can
  13. * generate better code by using them directly rather than
  14. * using the generic single-entry routines.
  15. */
  16. struct list_head {
  17. struct list_head *next, *prev;
  18. };
  19. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  20. #define LIST_HEAD(name) \
  21. struct list_head name = LIST_HEAD_INIT(name)
  22. #define INIT_LIST_HEAD(ptr) do { \
  23. (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  24. } while (0)
  25. /*
  26. * Insert a new entry between two known consecutive entries.
  27. *
  28. * This is only for internal list manipulation where we know
  29. * the prev/next entries already!
  30. */
  31. static inline void __list_add(struct list_head *new,
  32. struct list_head *prev,
  33. struct list_head *next)
  34. {
  35. next->prev = new;
  36. new->next = next;
  37. new->prev = prev;
  38. prev->next = new;
  39. }
  40. /**
  41. * list_add - add a new entry
  42. * @new: new entry to be added
  43. * @head: list head to add it after
  44. *
  45. * Insert a new entry after the specified head.
  46. * This is good for implementing stacks.
  47. */
  48. static inline void list_add(struct list_head *new, struct list_head *head)
  49. {
  50. __list_add(new, head, head->next);
  51. }
  52. /**
  53. * list_add_tail - add a new entry
  54. * @new: new entry to be added
  55. * @head: list head to add it before
  56. *
  57. * Insert a new entry before the specified head.
  58. * This is useful for implementing queues.
  59. */
  60. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  61. {
  62. __list_add(new, head->prev, head);
  63. }
  64. /*
  65. * Delete a list entry by making the prev/next entries
  66. * point to each other.
  67. *
  68. * This is only for internal list manipulation where we know
  69. * the prev/next entries already!
  70. */
  71. static inline void __list_del(struct list_head *prev, struct list_head *next)
  72. {
  73. next->prev = prev;
  74. prev->next = next;
  75. }
  76. /**
  77. * list_del - deletes entry from list.
  78. * @entry: the element to delete from the list.
  79. * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
  80. */
  81. static inline void list_del(struct list_head *entry)
  82. {
  83. __list_del(entry->prev, entry->next);
  84. entry->next = (void *) 0;
  85. entry->prev = (void *) 0;
  86. }
  87. /**
  88. * list_del_init - deletes entry from list and reinitialize it.
  89. * @entry: the element to delete from the list.
  90. */
  91. static inline void list_del_init(struct list_head *entry)
  92. {
  93. __list_del(entry->prev, entry->next);
  94. INIT_LIST_HEAD(entry);
  95. }
  96. /**
  97. * list_move - delete from one list and add as another's head
  98. * @list: the entry to move
  99. * @head: the head that will precede our entry
  100. */
  101. static inline void list_move(struct list_head *list, struct list_head *head)
  102. {
  103. __list_del(list->prev, list->next);
  104. list_add(list, head);
  105. }
  106. /**
  107. * list_move_tail - delete from one list and add as another's tail
  108. * @list: the entry to move
  109. * @head: the head that will follow our entry
  110. */
  111. static inline void list_move_tail(struct list_head *list,
  112. struct list_head *head)
  113. {
  114. __list_del(list->prev, list->next);
  115. list_add_tail(list, head);
  116. }
  117. /**
  118. * list_empty - tests whether a list is empty
  119. * @head: the list to test.
  120. */
  121. static inline int list_empty(struct list_head *head)
  122. {
  123. return head->next == head;
  124. }
  125. static inline void __list_splice(struct list_head *list,
  126. struct list_head *head)
  127. {
  128. struct list_head *first = list->next;
  129. struct list_head *last = list->prev;
  130. struct list_head *at = head->next;
  131. first->prev = head;
  132. head->next = first;
  133. last->next = at;
  134. at->prev = last;
  135. }
  136. /**
  137. * list_splice - join two lists
  138. * @list: the new list to add.
  139. * @head: the place to add it in the first list.
  140. */
  141. static inline void list_splice(struct list_head *list, struct list_head *head)
  142. {
  143. if (!list_empty(list))
  144. __list_splice(list, head);
  145. }
  146. /**
  147. * list_splice_init - join two lists and reinitialise the emptied list.
  148. * @list: the new list to add.
  149. * @head: the place to add it in the first list.
  150. *
  151. * The list at @list is reinitialised
  152. */
  153. static inline void list_splice_init(struct list_head *list,
  154. struct list_head *head)
  155. {
  156. if (!list_empty(list)) {
  157. __list_splice(list, head);
  158. INIT_LIST_HEAD(list);
  159. }
  160. }
  161. /**
  162. * list_entry - get the struct for this entry
  163. * @ptr: the &struct list_head pointer.
  164. * @type: the type of the struct this is embedded in.
  165. * @member: the name of the list_struct within the struct.
  166. */
  167. #define list_entry(ptr, type, member) \
  168. ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
  169. /**
  170. * list_for_each - iterate over a list
  171. * @pos: the &struct list_head to use as a loop counter.
  172. * @head: the head for your list.
  173. */
  174. #define list_for_each(pos, head) \
  175. for (pos = (head)->next, prefetch(pos->next); pos != (head); \
  176. pos = pos->next, prefetch(pos->next))
  177. /**
  178. * list_for_each_prev - iterate over a list backwards
  179. * @pos: the &struct list_head to use as a loop counter.
  180. * @head: the head for your list.
  181. */
  182. #define list_for_each_prev(pos, head) \
  183. for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
  184. pos = pos->prev, prefetch(pos->prev))
  185. /**
  186. * list_for_each_safe - iterate over a list safe against removal of list entry
  187. * @pos: the &struct list_head to use as a loop counter.
  188. * @n: another &struct list_head to use as temporary storage
  189. * @head: the head for your list.
  190. */
  191. #define list_for_each_safe(pos, n, head) \
  192. for (pos = (head)->next, n = pos->next; pos != (head); \
  193. pos = n, n = pos->next)
  194. /**
  195. * list_for_each_entry - iterate over list of given type
  196. * @pos: the type * to use as a loop counter.
  197. * @head: the head for your list.
  198. * @member: the name of the list_struct within the struct.
  199. */
  200. #define list_for_each_entry(pos, head, member) \
  201. for (pos = list_entry((head)->next, typeof(*pos), member), \
  202. prefetch(pos->member.next); \
  203. &pos->member != (head); \
  204. pos = list_entry(pos->member.next, typeof(*pos), member), \
  205. prefetch(pos->member.next))
  206. /**
  207. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  208. * @pos: the type * to use as a loop counter.
  209. * @n: another type * to use as temporary storage
  210. * @head: the head for your list.
  211. * @member: the name of the list_struct within the struct.
  212. */
  213. #define list_for_each_entry_safe(pos, n, head, member) \
  214. for (pos = list_entry((head)->next, typeof(*pos), member), \
  215. n = list_entry(pos->member.next, typeof(*pos), member); \
  216. &pos->member != (head); \
  217. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  218. /**
  219. * list_for_each_entry_continue - iterate over list of given type
  220. * continuing after existing point
  221. * @pos: the type * to use as a loop counter.
  222. * @head: the head for your list.
  223. * @member: the name of the list_struct within the struct.
  224. */
  225. #define list_for_each_entry_continue(pos, head, member) \
  226. for (pos = list_entry(pos->member.next, typeof(*pos), member), \
  227. prefetch(pos->member.next); \
  228. &pos->member != (head); \
  229. pos = list_entry(pos->member.next, typeof(*pos), member), \
  230. prefetch(pos->member.next))
  231. #endif