linked-list.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #ifndef __LIST_H__
  2. #define __LIST_H__
  3. /*
  4. * These are non-NULL pointers that will result in page faults
  5. * under normal circumstances, used to verify that nobody uses
  6. * non-initialized list entries.
  7. */
  8. #define LIST_POISON1 ((void*) 0x00100100)
  9. #define LIST_POISON2 ((void*) 0x00200200)
  10. #ifndef offsetof
  11. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE*)0)->MEMBER)
  12. #endif
  13. #define container_of(ptr, type, member) ( { \
  14. const typeof(((type*)0)->member ) *__mptr = (ptr); \
  15. (type*)((char*)__mptr - offsetof(type,member) );})
  16. /*
  17. * Simple doubly linked list implementation.
  18. *
  19. * Some of the internal functions ("__xxx") are useful when
  20. * manipulating whole lists rather than single entries, as
  21. * sometimes we already know the next/prev entries and we can
  22. * generate better code by using them directly rather than
  23. * using the generic single-entry routines.
  24. */
  25. struct list_head {
  26. struct list_head *next, *prev;
  27. };
  28. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  29. #define LIST_HEAD(name) \
  30. struct list_head name = LIST_HEAD_INIT(name)
  31. #define INIT_LIST_HEAD(ptr) do { \
  32. (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  33. } while (0)
  34. /*
  35. * Insert a new entry between two known consecutive entries.
  36. *
  37. * This is only for internal list manipulation where we know
  38. * the prev/next entries already!
  39. */
  40. static inline void __list_add(struct list_head *new,
  41. struct list_head *prev,
  42. struct list_head *next)
  43. {
  44. next->prev = new;
  45. new->next = next;
  46. new->prev = prev;
  47. prev->next = new;
  48. }
  49. /**
  50. * list_add - add a new entry
  51. * @new: new entry to be added
  52. * @head: list head to add it after
  53. *
  54. * Insert a new entry after the specified head.
  55. * This is good for implementing stacks.
  56. */
  57. static inline void list_add(struct list_head *new, struct list_head *head)
  58. {
  59. __list_add(new, head, head->next);
  60. }
  61. /**
  62. * list_add_tail - add a new entry
  63. * @new: new entry to be added
  64. * @head: list head to add it before
  65. *
  66. * Insert a new entry before the specified head.
  67. * This is useful for implementing queues.
  68. */
  69. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  70. {
  71. __list_add(new, head->prev, head);
  72. }
  73. /*
  74. * Delete a list entry by making the prev/next entries
  75. * point to each other.
  76. *
  77. * This is only for internal list manipulation where we know
  78. * the prev/next entries already!
  79. */
  80. static inline void __list_del(struct list_head * prev, struct list_head * next)
  81. {
  82. next->prev = prev;
  83. prev->next = next;
  84. }
  85. /**
  86. * list_del - deletes entry from list.
  87. * @entry: the element to delete from the list.
  88. * Note: list_empty on entry does not return true after this, the entry is
  89. * in an undefined state.
  90. */
  91. static inline void list_del(struct list_head *entry)
  92. {
  93. __list_del(entry->prev, entry->next);
  94. entry->next = LIST_POISON1;
  95. entry->prev = LIST_POISON2;
  96. }
  97. /**
  98. * list_del_init - deletes entry from list and reinitialize it.
  99. * @entry: the element to delete from the list.
  100. */
  101. static inline void list_del_init(struct list_head *entry) {
  102. __list_del(entry->prev, entry->next);
  103. INIT_LIST_HEAD(entry);
  104. }
  105. /**
  106. * list_move - delete from one list and add as another's head
  107. * @list: the entry to move
  108. * @head: the head that will precede our entry
  109. */
  110. static inline void list_move(struct list_head *list, struct list_head *head) {
  111. __list_del(list->prev, list->next);
  112. list_add(list, head);
  113. }
  114. /**
  115. * list_move_tail - delete from one list and add as another's tail
  116. * @list: the entry to move
  117. * @head: the head that will follow our entry
  118. */
  119. static inline void list_move_tail(struct list_head *list, struct list_head *head) {
  120. __list_del(list->prev, list->next);
  121. list_add_tail(list, head);
  122. }
  123. /**
  124. * list_empty - tests whether a list is empty
  125. * @head: the list to test.
  126. */
  127. static inline int list_empty(const struct list_head *head)
  128. {
  129. return head->next == head;
  130. }
  131. /**
  132. * list_empty_careful - tests whether a list is
  133. * empty _and_ checks that no other CPU might be
  134. * in the process of still modifying either member
  135. *
  136. * NOTE: using list_empty_careful() without synchronization
  137. * can only be safe if the only activity that can happen
  138. * to the list entry is list_del_init(). Eg. it cannot be used
  139. * if another CPU could re-list_add() it.
  140. *
  141. * @head: the list to test.
  142. */
  143. static inline int list_empty_careful(const struct list_head *head)
  144. {
  145. struct list_head *next = head->next;
  146. return (next == head) && (next == head->prev);
  147. }
  148. static inline void __list_splice(struct list_head *list,
  149. struct list_head *head)
  150. {
  151. struct list_head *first = list->next;
  152. struct list_head *last = list->prev;
  153. struct list_head *at = head->next;
  154. first->prev = head;
  155. head->next = first;
  156. last->next = at;
  157. at->prev = last;
  158. }
  159. /**
  160. * list_splice - join two lists
  161. * @list: the new list to add.
  162. * @head: the place to add it in the first list.
  163. */
  164. static inline void list_splice(struct list_head *list, struct list_head *head)
  165. {
  166. if (!list_empty(list))
  167. __list_splice(list, head);
  168. }
  169. /**
  170. * list_splice_init - join two lists and reinitialise the emptied list.
  171. * @list: the new list to add.
  172. * @head: the place to add it in the first list.
  173. *
  174. * The list at @list is reinitialised
  175. */
  176. static inline void list_splice_init(struct list_head *list,
  177. struct list_head *head)
  178. {
  179. if (!list_empty(list)) {
  180. __list_splice(list, head);
  181. INIT_LIST_HEAD(list);
  182. }
  183. }
  184. static inline int list_swap(struct list_head *a, struct list_head *b, struct list_head *list)
  185. {
  186. if (a->next == list || b->prev == list)
  187. return -1;
  188. list_del(a);
  189. list_add(a, b);
  190. return 0;
  191. }
  192. /**
  193. * list_entry - get the struct for this entry
  194. * @ptr: the &struct list_head pointer.
  195. * @type: the type of the struct this is embedded in.
  196. * @member: the name of the list_struct within the struct.
  197. */
  198. #define list_entry(ptr, type, member) \
  199. container_of(ptr, type, member)
  200. /**
  201. * list_for_each - iterate over a list
  202. * @pos: the &struct list_head to use as a loop counter.
  203. * @head: the head for your list.
  204. */
  205. #define list_for_each(pos, head) \
  206. for (pos = (head)->next; pos != (head); pos = pos->next)
  207. /**
  208. * list_for_each_prev - iterate over a list backwards
  209. * @pos: the &struct list_head to use as a loop counter.
  210. * @head: the head for your list.
  211. */
  212. #define list_for_each_prev(pos, head) \
  213. for (pos = (head)->prev; pos != (head); pos = pos->prev)
  214. /**
  215. * list_for_each_safe - iterate over a list safe against removal of list entry
  216. * @pos: the &struct list_head to use as a loop counter.
  217. * @n: another &struct list_head to use as temporary storage
  218. * @head: the head for your list.
  219. */
  220. #define list_for_each_safe(pos, n, head) \
  221. for (pos = (head)->next, n = pos->next; pos != (head); pos = n, n = pos->next)
  222. /**
  223. * list_for_each_entry - iterate over list of given type
  224. * @pos: the type * to use as a loop counter.
  225. * @head: the head for your list.
  226. * @member: the name of the list_struct within the struct.
  227. */
  228. #define list_for_each_entry(pos, head, member) \
  229. for (pos = list_entry((head)->next, typeof(*pos), member); &pos->member != (head); \
  230. pos = list_entry(pos->member.next, typeof(*pos), member))
  231. /**
  232. * list_for_each_entry_reverse - iterate backwards over list of given type.
  233. * @pos: the type * to use as a loop counter.
  234. * @head: the head for your list.
  235. * @member: the name of the list_struct within the struct.
  236. */
  237. #define list_for_each_entry_reverse(pos, head, member) \
  238. for (pos = list_entry((head)->prev, typeof(*pos), member); &pos->member != (head); \
  239. pos = list_entry(pos->member.prev, typeof(*pos), member))
  240. /**
  241. * list_prepare_entry - prepare a pos entry for use as a start point in
  242. * list_for_each_entry_continue
  243. * @pos: the type * to use as a start point
  244. * @head: the head of the list
  245. * @member: the name of the list_struct within the struct.
  246. */
  247. #define list_prepare_entry(pos, head, member) \
  248. ((pos) ? : list_entry(head, typeof(*pos), member))
  249. /**
  250. * list_for_each_entry_continue - iterate over list of given type
  251. * continuing after existing point
  252. * @pos: the type * to use as a loop counter.
  253. * @head: the head for your list.
  254. * @member: the name of the list_struct within the struct.
  255. */
  256. #define list_for_each_entry_continue(pos, head, member) \
  257. for (pos = list_entry(pos->member.next, typeof(*pos), member); &pos->member != (head); \
  258. pos = list_entry(pos->member.next, typeof(*pos), member))
  259. /**
  260. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  261. * @pos: the type * to use as a loop counter.
  262. * @n: another type * to use as temporary storage
  263. * @head: the head for your list.
  264. * @member: the name of the list_struct within the struct.
  265. */
  266. #define list_for_each_entry_safe(pos, n, head, member) \
  267. for (pos = list_entry((head)->next, typeof(*pos), member), \
  268. n = list_entry(pos->member.next, typeof(*pos), member); \
  269. &pos->member != (head); \
  270. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  271. /**
  272. * list_for_each_entry_reverse_safe - iterate over list of given type safe against removal of list entry
  273. * @pos: the type * to use as a loop counter.
  274. * @n: another type * to use as temporary storage
  275. * @head: the head for your list.
  276. * @member: the name of the list_struct within the struct.
  277. */
  278. #define list_for_each_entry_reverse_safe(pos, n, head, member) \
  279. for (pos = list_entry((head)->prev, typeof(*pos), member), \
  280. n = list_entry(pos->member.prev, typeof(*pos), member); \
  281. &pos->member != (head); \
  282. pos = n, n = list_entry(n->member.prev, typeof(*n), member))
  283. #endif /* __LIST_H__ */