list.h 13 KB

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