list.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #ifndef _LINUX_LIST_H
  2. #define _LINUX_LIST_H
  3. #define LIST_POISON1 ((void *) 0x00100100)
  4. #define LIST_POISON2 ((void *) 0x00200200)
  5. /*
  6. * Simple doubly linked list implementation.
  7. *
  8. * Some of the internal functions ("__xxx") are useful when
  9. * manipulating whole lists rather than single entries, as
  10. * sometimes we already know the next/prev entries and we can
  11. * generate better code by using them directly rather than
  12. * using the generic single-entry routines.
  13. */
  14. struct list_head {
  15. struct list_head *next, *prev;
  16. };
  17. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  18. #define LIST_HEAD(name) \
  19. struct list_head name = LIST_HEAD_INIT(name)
  20. static inline void INIT_LIST_HEAD(struct list_head *list)
  21. {
  22. list->next = list;
  23. list->prev = list;
  24. }
  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. #ifndef CONFIG_DEBUG_LIST
  32. static inline void __list_add(struct list_head *new,
  33. struct list_head *prev,
  34. struct list_head *next)
  35. {
  36. next->prev = new;
  37. new->next = next;
  38. new->prev = prev;
  39. prev->next = new;
  40. }
  41. #else
  42. extern void __list_add(struct list_head *new,
  43. struct list_head *prev,
  44. struct list_head *next);
  45. #endif
  46. /**
  47. * list_add - add a new entry
  48. * @new: new entry to be added
  49. * @head: list head to add it after
  50. *
  51. * Insert a new entry after the specified head.
  52. * This is good for implementing stacks.
  53. */
  54. #ifndef CONFIG_DEBUG_LIST
  55. static inline void list_add(struct list_head *new, struct list_head *head)
  56. {
  57. __list_add(new, head, head->next);
  58. }
  59. #else
  60. extern void list_add(struct list_head *new, struct list_head *head);
  61. #endif
  62. /**
  63. * list_add_tail - add a new entry
  64. * @new: new entry to be added
  65. * @head: list head to add it before
  66. *
  67. * Insert a new entry before the specified head.
  68. * This is useful for implementing queues.
  69. */
  70. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  71. {
  72. __list_add(new, head->prev, head);
  73. }
  74. /*
  75. * Delete a list entry by making the prev/next entries
  76. * point to each other.
  77. *
  78. * This is only for internal list manipulation where we know
  79. * the prev/next entries already!
  80. */
  81. static inline void __list_del(struct list_head * prev, struct list_head * next)
  82. {
  83. next->prev = prev;
  84. prev->next = next;
  85. }
  86. /**
  87. * list_del - deletes entry from list.
  88. * @entry: the element to delete from the list.
  89. * Note: list_empty on entry does not return true after this, the entry is
  90. * in an undefined state.
  91. */
  92. #ifndef CONFIG_DEBUG_LIST
  93. static inline void list_del(struct list_head *entry)
  94. {
  95. __list_del(entry->prev, entry->next);
  96. entry->next = LIST_POISON1;
  97. entry->prev = LIST_POISON2;
  98. }
  99. #else
  100. extern void list_del(struct list_head *entry);
  101. #endif
  102. /**
  103. * list_replace - replace old entry by new one
  104. * @old : the element to be replaced
  105. * @new : the new element to insert
  106. * Note: if 'old' was empty, it will be overwritten.
  107. */
  108. static inline void list_replace(struct list_head *old,
  109. struct list_head *new)
  110. {
  111. new->next = old->next;
  112. new->next->prev = new;
  113. new->prev = old->prev;
  114. new->prev->next = new;
  115. }
  116. static inline void list_replace_init(struct list_head *old,
  117. struct list_head *new)
  118. {
  119. list_replace(old, new);
  120. INIT_LIST_HEAD(old);
  121. }
  122. /**
  123. * list_del_init - deletes entry from list and reinitialize it.
  124. * @entry: the element to delete from the list.
  125. */
  126. static inline void list_del_init(struct list_head *entry)
  127. {
  128. __list_del(entry->prev, entry->next);
  129. INIT_LIST_HEAD(entry);
  130. }
  131. /**
  132. * list_move - delete from one list and add as another's head
  133. * @list: the entry to move
  134. * @head: the head that will precede our entry
  135. */
  136. static inline void list_move(struct list_head *list, struct list_head *head)
  137. {
  138. __list_del(list->prev, list->next);
  139. list_add(list, head);
  140. }
  141. /**
  142. * list_move_tail - delete from one list and add as another's tail
  143. * @list: the entry to move
  144. * @head: the head that will follow our entry
  145. */
  146. static inline void list_move_tail(struct list_head *list,
  147. struct list_head *head)
  148. {
  149. __list_del(list->prev, list->next);
  150. list_add_tail(list, head);
  151. }
  152. /**
  153. * list_is_last - tests whether @list is the last entry in list @head
  154. * @list: the entry to test
  155. * @head: the head of the list
  156. */
  157. static inline int list_is_last(const struct list_head *list,
  158. const struct list_head *head)
  159. {
  160. return list->next == head;
  161. }
  162. /**
  163. * list_empty - tests whether a list is empty
  164. * @head: the list to test.
  165. */
  166. static inline int list_empty(const struct list_head *head)
  167. {
  168. return head->next == head;
  169. }
  170. /**
  171. * list_empty_careful - tests whether a list is empty and not being modified
  172. * @head: the list to test
  173. *
  174. * Description:
  175. * tests whether a list is empty _and_ checks that no other CPU might be
  176. * in the process of modifying either member (next or prev)
  177. *
  178. * NOTE: using list_empty_careful() without synchronization
  179. * can only be safe if the only activity that can happen
  180. * to the list entry is list_del_init(). Eg. it cannot be used
  181. * if another CPU could re-list_add() it.
  182. */
  183. static inline int list_empty_careful(const struct list_head *head)
  184. {
  185. struct list_head *next = head->next;
  186. return (next == head) && (next == head->prev);
  187. }
  188. static inline void __list_splice(struct list_head *list,
  189. struct list_head *head)
  190. {
  191. struct list_head *first = list->next;
  192. struct list_head *last = list->prev;
  193. struct list_head *at = head->next;
  194. first->prev = head;
  195. head->next = first;
  196. last->next = at;
  197. at->prev = last;
  198. }
  199. /**
  200. * list_splice - join two lists
  201. * @list: the new list to add.
  202. * @head: the place to add it in the first list.
  203. */
  204. static inline void list_splice(struct list_head *list, struct list_head *head)
  205. {
  206. if (!list_empty(list))
  207. __list_splice(list, head);
  208. }
  209. /**
  210. * list_splice_init - join two lists and reinitialise the emptied list.
  211. * @list: the new list to add.
  212. * @head: the place to add it in the first list.
  213. *
  214. * The list at @list is reinitialised
  215. */
  216. static inline void list_splice_init(struct list_head *list,
  217. struct list_head *head)
  218. {
  219. if (!list_empty(list)) {
  220. __list_splice(list, head);
  221. INIT_LIST_HEAD(list);
  222. }
  223. }
  224. /**
  225. * list_entry - get the struct for this entry
  226. * @ptr: the &struct list_head pointer.
  227. * @type: the type of the struct this is embedded in.
  228. * @member: the name of the list_struct within the struct.
  229. */
  230. #define list_entry(ptr, type, member) \
  231. container_of(ptr, type, member)
  232. /**
  233. * list_for_each - iterate over a list
  234. * @pos: the &struct list_head to use as a loop cursor.
  235. * @head: the head for your list.
  236. */
  237. #define list_for_each(pos, head) \
  238. for (pos = (head)->next; prefetch(pos->next), pos != (head); \
  239. pos = pos->next)
  240. /**
  241. * __list_for_each - iterate over a list
  242. * @pos: the &struct list_head to use as a loop cursor.
  243. * @head: the head for your list.
  244. *
  245. * This variant differs from list_for_each() in that it's the
  246. * simplest possible list iteration code, no prefetching is done.
  247. * Use this for code that knows the list to be very short (empty
  248. * or 1 entry) most of the time.
  249. */
  250. #define __list_for_each(pos, head) \
  251. for (pos = (head)->next; pos != (head); pos = pos->next)
  252. /**
  253. * list_for_each_prev - iterate over a list backwards
  254. * @pos: the &struct list_head to use as a loop cursor.
  255. * @head: the head for your list.
  256. */
  257. #define list_for_each_prev(pos, head) \
  258. for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
  259. pos = pos->prev)
  260. /**
  261. * list_for_each_safe - iterate over a list safe against removal of list entry
  262. * @pos: the &struct list_head to use as a loop cursor.
  263. * @n: another &struct list_head to use as temporary storage
  264. * @head: the head for your list.
  265. */
  266. #define list_for_each_safe(pos, n, head) \
  267. for (pos = (head)->next, n = pos->next; pos != (head); \
  268. pos = n, n = pos->next)
  269. /**
  270. * list_for_each_entry - iterate over list of given type
  271. * @pos: the type * to use as a loop cursor.
  272. * @head: the head for your list.
  273. * @member: the name of the list_struct within the struct.
  274. */
  275. #define list_for_each_entry(pos, head, member) \
  276. for (pos = list_entry((head)->next, typeof(*pos), member); \
  277. prefetch(pos->member.next), &pos->member != (head); \
  278. pos = list_entry(pos->member.next, typeof(*pos), member))
  279. /**
  280. * list_for_each_entry_reverse - iterate backwards over list of given type.
  281. * @pos: the type * to use as a loop cursor.
  282. * @head: the head for your list.
  283. * @member: the name of the list_struct within the struct.
  284. */
  285. #define list_for_each_entry_reverse(pos, head, member) \
  286. for (pos = list_entry((head)->prev, typeof(*pos), member); \
  287. prefetch(pos->member.prev), &pos->member != (head); \
  288. pos = list_entry(pos->member.prev, typeof(*pos), member))
  289. /**
  290. * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue
  291. * @pos: the type * to use as a start point
  292. * @head: the head of the list
  293. * @member: the name of the list_struct within the struct.
  294. *
  295. * Prepares a pos entry for use as a start point in list_for_each_entry_continue.
  296. */
  297. #define list_prepare_entry(pos, head, member) \
  298. ((pos) ? : list_entry(head, typeof(*pos), member))
  299. /**
  300. * list_for_each_entry_continue - continue iteration over list of given type
  301. * @pos: the type * to use as a loop cursor.
  302. * @head: the head for your list.
  303. * @member: the name of the list_struct within the struct.
  304. *
  305. * Continue to iterate over list of given type, continuing after
  306. * the current position.
  307. */
  308. #define list_for_each_entry_continue(pos, head, member) \
  309. for (pos = list_entry(pos->member.next, typeof(*pos), member); \
  310. prefetch(pos->member.next), &pos->member != (head); \
  311. pos = list_entry(pos->member.next, typeof(*pos), member))
  312. /**
  313. * list_for_each_entry_from - iterate over list of given type from the current point
  314. * @pos: the type * to use as a loop cursor.
  315. * @head: the head for your list.
  316. * @member: the name of the list_struct within the struct.
  317. *
  318. * Iterate over list of given type, continuing from current position.
  319. */
  320. #define list_for_each_entry_from(pos, head, member) \
  321. for (; prefetch(pos->member.next), &pos->member != (head); \
  322. pos = list_entry(pos->member.next, typeof(*pos), member))
  323. /**
  324. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  325. * @pos: the type * to use as a loop cursor.
  326. * @n: another type * to use as temporary storage
  327. * @head: the head for your list.
  328. * @member: the name of the list_struct within the struct.
  329. */
  330. #define list_for_each_entry_safe(pos, n, head, member) \
  331. for (pos = list_entry((head)->next, typeof(*pos), member), \
  332. n = list_entry(pos->member.next, typeof(*pos), member); \
  333. &pos->member != (head); \
  334. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  335. /**
  336. * list_for_each_entry_safe_continue
  337. * @pos: the type * to use as a loop cursor.
  338. * @n: another type * to use as temporary storage
  339. * @head: the head for your list.
  340. * @member: the name of the list_struct within the struct.
  341. *
  342. * Iterate over list of given type, continuing after current point,
  343. * safe against removal of list entry.
  344. */
  345. #define list_for_each_entry_safe_continue(pos, n, head, member) \
  346. for (pos = list_entry(pos->member.next, typeof(*pos), member), \
  347. n = list_entry(pos->member.next, typeof(*pos), member); \
  348. &pos->member != (head); \
  349. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  350. /**
  351. * list_for_each_entry_safe_from
  352. * @pos: the type * to use as a loop cursor.
  353. * @n: another type * to use as temporary storage
  354. * @head: the head for your list.
  355. * @member: the name of the list_struct within the struct.
  356. *
  357. * Iterate over list of given type from current point, safe against
  358. * removal of list entry.
  359. */
  360. #define list_for_each_entry_safe_from(pos, n, head, member) \
  361. for (n = list_entry(pos->member.next, typeof(*pos), member); \
  362. &pos->member != (head); \
  363. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  364. /**
  365. * list_for_each_entry_safe_reverse
  366. * @pos: the type * to use as a loop cursor.
  367. * @n: another type * to use as temporary storage
  368. * @head: the head for your list.
  369. * @member: the name of the list_struct within the struct.
  370. *
  371. * Iterate backwards over list of given type, safe against removal
  372. * of list entry.
  373. */
  374. #define list_for_each_entry_safe_reverse(pos, n, head, member) \
  375. for (pos = list_entry((head)->prev, typeof(*pos), member), \
  376. n = list_entry(pos->member.prev, typeof(*pos), member); \
  377. &pos->member != (head); \
  378. pos = n, n = list_entry(n->member.prev, typeof(*n), member))
  379. #endif