elist.h 6.9 KB

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