list.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. #ifndef _LINUX_LIST_H
  2. #define _LINUX_LIST_H
  3. #include <linux/types.h>
  4. #include <linux/stddef.h>
  5. #include <linux/poison.h>
  6. #include <linux/const.h>
  7. #include <linux/kernel.h>
  8. /*
  9. * Simple doubly linked list implementation.
  10. *
  11. * Some of the internal functions ("__xxx") are useful when
  12. * manipulating whole lists rather than single entries, as
  13. * sometimes we already know the next/prev entries and we can
  14. * generate better code by using them directly rather than
  15. * using the generic single-entry routines.
  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. WRITE_ONCE(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. WRITE_ONCE(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. static inline void list_add(struct list_head *new, struct list_head *head)
  55. {
  56. __list_add(new, head, head->next);
  57. }
  58. /**
  59. * list_add_tail - add a new entry
  60. * @new: new entry to be added
  61. * @head: list head to add it before
  62. *
  63. * Insert a new entry before the specified head.
  64. * This is useful for implementing queues.
  65. */
  66. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  67. {
  68. __list_add(new, head->prev, head);
  69. }
  70. /*
  71. * Delete a list entry by making the prev/next entries
  72. * point to each other.
  73. *
  74. * This is only for internal list manipulation where we know
  75. * the prev/next entries already!
  76. */
  77. static inline void __list_del(struct list_head * prev, struct list_head * next)
  78. {
  79. next->prev = prev;
  80. WRITE_ONCE(prev->next, next);
  81. }
  82. /**
  83. * list_del - deletes entry from list.
  84. * @entry: the element to delete from the list.
  85. * Note: list_empty() on entry does not return true after this, the entry is
  86. * in an undefined state.
  87. */
  88. #ifndef CONFIG_DEBUG_LIST
  89. static inline void __list_del_entry(struct list_head *entry)
  90. {
  91. __list_del(entry->prev, entry->next);
  92. }
  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_entry(struct list_head *entry);
  101. extern void list_del(struct list_head *entry);
  102. #endif
  103. /**
  104. * list_replace - replace old entry by new one
  105. * @old : the element to be replaced
  106. * @new : the new element to insert
  107. *
  108. * If @old was empty, it will be overwritten.
  109. */
  110. static inline void list_replace(struct list_head *old,
  111. struct list_head *new)
  112. {
  113. new->next = old->next;
  114. new->next->prev = new;
  115. new->prev = old->prev;
  116. new->prev->next = new;
  117. }
  118. static inline void list_replace_init(struct list_head *old,
  119. struct list_head *new)
  120. {
  121. list_replace(old, new);
  122. INIT_LIST_HEAD(old);
  123. }
  124. /**
  125. * list_del_init - deletes entry from list and reinitialize it.
  126. * @entry: the element to delete from the list.
  127. */
  128. static inline void list_del_init(struct list_head *entry)
  129. {
  130. __list_del_entry(entry);
  131. INIT_LIST_HEAD(entry);
  132. }
  133. /**
  134. * list_move - delete from one list and add as another's head
  135. * @list: the entry to move
  136. * @head: the head that will precede our entry
  137. */
  138. static inline void list_move(struct list_head *list, struct list_head *head)
  139. {
  140. __list_del_entry(list);
  141. list_add(list, head);
  142. }
  143. /**
  144. * list_move_tail - delete from one list and add as another's tail
  145. * @list: the entry to move
  146. * @head: the head that will follow our entry
  147. */
  148. static inline void list_move_tail(struct list_head *list,
  149. struct list_head *head)
  150. {
  151. __list_del_entry(list);
  152. list_add_tail(list, head);
  153. }
  154. /**
  155. * list_is_last - tests whether @list is the last entry in list @head
  156. * @list: the entry to test
  157. * @head: the head of the list
  158. */
  159. static inline int list_is_last(const struct list_head *list,
  160. const struct list_head *head)
  161. {
  162. return list->next == head;
  163. }
  164. /**
  165. * list_empty - tests whether a list is empty
  166. * @head: the list to test.
  167. */
  168. static inline int list_empty(const struct list_head *head)
  169. {
  170. return READ_ONCE(head->next) == head;
  171. }
  172. /**
  173. * list_empty_careful - tests whether a list is empty and not being modified
  174. * @head: the list to test
  175. *
  176. * Description:
  177. * tests whether a list is empty _and_ checks that no other CPU might be
  178. * in the process of modifying either member (next or prev)
  179. *
  180. * NOTE: using list_empty_careful() without synchronization
  181. * can only be safe if the only activity that can happen
  182. * to the list entry is list_del_init(). Eg. it cannot be used
  183. * if another CPU could re-list_add() it.
  184. */
  185. static inline int list_empty_careful(const struct list_head *head)
  186. {
  187. struct list_head *next = head->next;
  188. return (next == head) && (next == head->prev);
  189. }
  190. /**
  191. * list_rotate_left - rotate the list to the left
  192. * @head: the head of the list
  193. */
  194. static inline void list_rotate_left(struct list_head *head)
  195. {
  196. struct list_head *first;
  197. if (!list_empty(head)) {
  198. first = head->next;
  199. list_move_tail(first, head);
  200. }
  201. }
  202. /**
  203. * list_is_singular - tests whether a list has just one entry.
  204. * @head: the list to test.
  205. */
  206. static inline int list_is_singular(const struct list_head *head)
  207. {
  208. return !list_empty(head) && (head->next == head->prev);
  209. }
  210. static inline void __list_cut_position(struct list_head *list,
  211. struct list_head *head, struct list_head *entry)
  212. {
  213. struct list_head *new_first = entry->next;
  214. list->next = head->next;
  215. list->next->prev = list;
  216. list->prev = entry;
  217. entry->next = list;
  218. head->next = new_first;
  219. new_first->prev = head;
  220. }
  221. /**
  222. * list_cut_position - cut a list into two
  223. * @list: a new list to add all removed entries
  224. * @head: a list with entries
  225. * @entry: an entry within head, could be the head itself
  226. * and if so we won't cut the list
  227. *
  228. * This helper moves the initial part of @head, up to and
  229. * including @entry, from @head to @list. You should
  230. * pass on @entry an element you know is on @head. @list
  231. * should be an empty list or a list you do not care about
  232. * losing its data.
  233. *
  234. */
  235. static inline void list_cut_position(struct list_head *list,
  236. struct list_head *head, struct list_head *entry)
  237. {
  238. if (list_empty(head))
  239. return;
  240. if (list_is_singular(head) &&
  241. (head->next != entry && head != entry))
  242. return;
  243. if (entry == head)
  244. INIT_LIST_HEAD(list);
  245. else
  246. __list_cut_position(list, head, entry);
  247. }
  248. static inline void __list_splice(const struct list_head *list,
  249. struct list_head *prev,
  250. struct list_head *next)
  251. {
  252. struct list_head *first = list->next;
  253. struct list_head *last = list->prev;
  254. first->prev = prev;
  255. prev->next = first;
  256. last->next = next;
  257. next->prev = last;
  258. }
  259. /**
  260. * list_splice - join two lists, this is designed for stacks
  261. * @list: the new list to add.
  262. * @head: the place to add it in the first list.
  263. */
  264. static inline void list_splice(const struct list_head *list,
  265. struct list_head *head)
  266. {
  267. if (!list_empty(list))
  268. __list_splice(list, head, head->next);
  269. }
  270. /**
  271. * list_splice_tail - join two lists, each list being a queue
  272. * @list: the new list to add.
  273. * @head: the place to add it in the first list.
  274. */
  275. static inline void list_splice_tail(struct list_head *list,
  276. struct list_head *head)
  277. {
  278. if (!list_empty(list))
  279. __list_splice(list, head->prev, head);
  280. }
  281. /**
  282. * list_splice_init - join two lists and reinitialise the emptied list.
  283. * @list: the new list to add.
  284. * @head: the place to add it in the first list.
  285. *
  286. * The list at @list is reinitialised
  287. */
  288. static inline void list_splice_init(struct list_head *list,
  289. struct list_head *head)
  290. {
  291. if (!list_empty(list)) {
  292. __list_splice(list, head, head->next);
  293. INIT_LIST_HEAD(list);
  294. }
  295. }
  296. /**
  297. * list_splice_tail_init - join two lists and reinitialise the emptied list
  298. * @list: the new list to add.
  299. * @head: the place to add it in the first list.
  300. *
  301. * Each of the lists is a queue.
  302. * The list at @list is reinitialised
  303. */
  304. static inline void list_splice_tail_init(struct list_head *list,
  305. struct list_head *head)
  306. {
  307. if (!list_empty(list)) {
  308. __list_splice(list, head->prev, head);
  309. INIT_LIST_HEAD(list);
  310. }
  311. }
  312. /**
  313. * list_entry - get the struct for this entry
  314. * @ptr: the &struct list_head pointer.
  315. * @type: the type of the struct this is embedded in.
  316. * @member: the name of the list_head within the struct.
  317. */
  318. #define list_entry(ptr, type, member) \
  319. container_of(ptr, type, member)
  320. /**
  321. * list_first_entry - get the first element from a list
  322. * @ptr: the list head to take the element from.
  323. * @type: the type of the struct this is embedded in.
  324. * @member: the name of the list_head within the struct.
  325. *
  326. * Note, that list is expected to be not empty.
  327. */
  328. #define list_first_entry(ptr, type, member) \
  329. list_entry((ptr)->next, type, member)
  330. /**
  331. * list_last_entry - get the last element from a list
  332. * @ptr: the list head to take the element from.
  333. * @type: the type of the struct this is embedded in.
  334. * @member: the name of the list_head within the struct.
  335. *
  336. * Note, that list is expected to be not empty.
  337. */
  338. #define list_last_entry(ptr, type, member) \
  339. list_entry((ptr)->prev, type, member)
  340. /**
  341. * list_first_entry_or_null - get the first element from a list
  342. * @ptr: the list head to take the element from.
  343. * @type: the type of the struct this is embedded in.
  344. * @member: the name of the list_head within the struct.
  345. *
  346. * Note that if the list is empty, it returns NULL.
  347. */
  348. #define list_first_entry_or_null(ptr, type, member) ({ \
  349. struct list_head *head__ = (ptr); \
  350. struct list_head *pos__ = READ_ONCE(head__->next); \
  351. pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
  352. })
  353. /**
  354. * list_next_entry - get the next element in list
  355. * @pos: the type * to cursor
  356. * @member: the name of the list_head within the struct.
  357. */
  358. #define list_next_entry(pos, member) \
  359. list_entry((pos)->member.next, typeof(*(pos)), member)
  360. /**
  361. * list_prev_entry - get the prev element in list
  362. * @pos: the type * to cursor
  363. * @member: the name of the list_head within the struct.
  364. */
  365. #define list_prev_entry(pos, member) \
  366. list_entry((pos)->member.prev, typeof(*(pos)), member)
  367. /**
  368. * list_for_each - iterate over a list
  369. * @pos: the &struct list_head to use as a loop cursor.
  370. * @head: the head for your list.
  371. */
  372. #define list_for_each(pos, head) \
  373. for (pos = (head)->next; pos != (head); pos = pos->next)
  374. /**
  375. * list_for_each_prev - iterate over a list backwards
  376. * @pos: the &struct list_head to use as a loop cursor.
  377. * @head: the head for your list.
  378. */
  379. #define list_for_each_prev(pos, head) \
  380. for (pos = (head)->prev; pos != (head); pos = pos->prev)
  381. /**
  382. * list_for_each_safe - iterate over a list safe against removal of list entry
  383. * @pos: the &struct list_head to use as a loop cursor.
  384. * @n: another &struct list_head to use as temporary storage
  385. * @head: the head for your list.
  386. */
  387. #define list_for_each_safe(pos, n, head) \
  388. for (pos = (head)->next, n = pos->next; pos != (head); \
  389. pos = n, n = pos->next)
  390. /**
  391. * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
  392. * @pos: the &struct list_head to use as a loop cursor.
  393. * @n: another &struct list_head to use as temporary storage
  394. * @head: the head for your list.
  395. */
  396. #define list_for_each_prev_safe(pos, n, head) \
  397. for (pos = (head)->prev, n = pos->prev; \
  398. pos != (head); \
  399. pos = n, n = pos->prev)
  400. /**
  401. * list_for_each_entry - iterate over list of given type
  402. * @pos: the type * to use as a loop cursor.
  403. * @head: the head for your list.
  404. * @member: the name of the list_head within the struct.
  405. */
  406. #define list_for_each_entry(pos, head, member) \
  407. for (pos = list_first_entry(head, typeof(*pos), member); \
  408. &pos->member != (head); \
  409. pos = list_next_entry(pos, member))
  410. /**
  411. * list_for_each_entry_reverse - iterate backwards over list of given type.
  412. * @pos: the type * to use as a loop cursor.
  413. * @head: the head for your list.
  414. * @member: the name of the list_head within the struct.
  415. */
  416. #define list_for_each_entry_reverse(pos, head, member) \
  417. for (pos = list_last_entry(head, typeof(*pos), member); \
  418. &pos->member != (head); \
  419. pos = list_prev_entry(pos, member))
  420. /**
  421. * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
  422. * @pos: the type * to use as a start point
  423. * @head: the head of the list
  424. * @member: the name of the list_head within the struct.
  425. *
  426. * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
  427. */
  428. #define list_prepare_entry(pos, head, member) \
  429. ((pos) ? : list_entry(head, typeof(*pos), member))
  430. /**
  431. * list_for_each_entry_continue - continue iteration over list of given type
  432. * @pos: the type * to use as a loop cursor.
  433. * @head: the head for your list.
  434. * @member: the name of the list_head within the struct.
  435. *
  436. * Continue to iterate over list of given type, continuing after
  437. * the current position.
  438. */
  439. #define list_for_each_entry_continue(pos, head, member) \
  440. for (pos = list_next_entry(pos, member); \
  441. &pos->member != (head); \
  442. pos = list_next_entry(pos, member))
  443. /**
  444. * list_for_each_entry_continue_reverse - iterate backwards from the given point
  445. * @pos: the type * to use as a loop cursor.
  446. * @head: the head for your list.
  447. * @member: the name of the list_head within the struct.
  448. *
  449. * Start to iterate over list of given type backwards, continuing after
  450. * the current position.
  451. */
  452. #define list_for_each_entry_continue_reverse(pos, head, member) \
  453. for (pos = list_prev_entry(pos, member); \
  454. &pos->member != (head); \
  455. pos = list_prev_entry(pos, member))
  456. /**
  457. * list_for_each_entry_from - iterate over list of given type from the current point
  458. * @pos: the type * to use as a loop cursor.
  459. * @head: the head for your list.
  460. * @member: the name of the list_head within the struct.
  461. *
  462. * Iterate over list of given type, continuing from current position.
  463. */
  464. #define list_for_each_entry_from(pos, head, member) \
  465. for (; &pos->member != (head); \
  466. pos = list_next_entry(pos, member))
  467. /**
  468. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  469. * @pos: the type * to use as a loop cursor.
  470. * @n: another type * to use as temporary storage
  471. * @head: the head for your list.
  472. * @member: the name of the list_head within the struct.
  473. */
  474. #define list_for_each_entry_safe(pos, n, head, member) \
  475. for (pos = list_first_entry(head, typeof(*pos), member), \
  476. n = list_next_entry(pos, member); \
  477. &pos->member != (head); \
  478. pos = n, n = list_next_entry(n, member))
  479. /**
  480. * list_for_each_entry_safe_continue - continue list iteration safe against removal
  481. * @pos: the type * to use as a loop cursor.
  482. * @n: another type * to use as temporary storage
  483. * @head: the head for your list.
  484. * @member: the name of the list_head within the struct.
  485. *
  486. * Iterate over list of given type, continuing after current point,
  487. * safe against removal of list entry.
  488. */
  489. #define list_for_each_entry_safe_continue(pos, n, head, member) \
  490. for (pos = list_next_entry(pos, member), \
  491. n = list_next_entry(pos, member); \
  492. &pos->member != (head); \
  493. pos = n, n = list_next_entry(n, member))
  494. /**
  495. * list_for_each_entry_safe_from - iterate over list from current point safe against removal
  496. * @pos: the type * to use as a loop cursor.
  497. * @n: another type * to use as temporary storage
  498. * @head: the head for your list.
  499. * @member: the name of the list_head within the struct.
  500. *
  501. * Iterate over list of given type from current point, safe against
  502. * removal of list entry.
  503. */
  504. #define list_for_each_entry_safe_from(pos, n, head, member) \
  505. for (n = list_next_entry(pos, member); \
  506. &pos->member != (head); \
  507. pos = n, n = list_next_entry(n, member))
  508. /**
  509. * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
  510. * @pos: the type * to use as a loop cursor.
  511. * @n: another type * to use as temporary storage
  512. * @head: the head for your list.
  513. * @member: the name of the list_head within the struct.
  514. *
  515. * Iterate backwards over list of given type, safe against removal
  516. * of list entry.
  517. */
  518. #define list_for_each_entry_safe_reverse(pos, n, head, member) \
  519. for (pos = list_last_entry(head, typeof(*pos), member), \
  520. n = list_prev_entry(pos, member); \
  521. &pos->member != (head); \
  522. pos = n, n = list_prev_entry(n, member))
  523. /**
  524. * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
  525. * @pos: the loop cursor used in the list_for_each_entry_safe loop
  526. * @n: temporary storage used in list_for_each_entry_safe
  527. * @member: the name of the list_head within the struct.
  528. *
  529. * list_safe_reset_next is not safe to use in general if the list may be
  530. * modified concurrently (eg. the lock is dropped in the loop body). An
  531. * exception to this is if the cursor element (pos) is pinned in the list,
  532. * and list_safe_reset_next is called after re-taking the lock and before
  533. * completing the current iteration of the loop body.
  534. */
  535. #define list_safe_reset_next(pos, n, member) \
  536. n = list_next_entry(pos, member)
  537. /*
  538. * Double linked lists with a single pointer list head.
  539. * Mostly useful for hash tables where the two pointer list head is
  540. * too wasteful.
  541. * You lose the ability to access the tail in O(1).
  542. */
  543. #define HLIST_HEAD_INIT { .first = NULL }
  544. #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
  545. #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
  546. static inline void INIT_HLIST_NODE(struct hlist_node *h)
  547. {
  548. h->next = NULL;
  549. h->pprev = NULL;
  550. }
  551. static inline int hlist_unhashed(const struct hlist_node *h)
  552. {
  553. return !h->pprev;
  554. }
  555. static inline int hlist_empty(const struct hlist_head *h)
  556. {
  557. return !READ_ONCE(h->first);
  558. }
  559. static inline void __hlist_del(struct hlist_node *n)
  560. {
  561. struct hlist_node *next = n->next;
  562. struct hlist_node **pprev = n->pprev;
  563. WRITE_ONCE(*pprev, next);
  564. if (next)
  565. next->pprev = pprev;
  566. }
  567. static inline void hlist_del(struct hlist_node *n)
  568. {
  569. __hlist_del(n);
  570. n->next = LIST_POISON1;
  571. n->pprev = LIST_POISON2;
  572. }
  573. static inline void hlist_del_init(struct hlist_node *n)
  574. {
  575. if (!hlist_unhashed(n)) {
  576. __hlist_del(n);
  577. INIT_HLIST_NODE(n);
  578. }
  579. }
  580. static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
  581. {
  582. struct hlist_node *first = h->first;
  583. n->next = first;
  584. if (first)
  585. first->pprev = &n->next;
  586. WRITE_ONCE(h->first, n);
  587. n->pprev = &h->first;
  588. }
  589. /* next must be != NULL */
  590. static inline void hlist_add_before(struct hlist_node *n,
  591. struct hlist_node *next)
  592. {
  593. n->pprev = next->pprev;
  594. n->next = next;
  595. next->pprev = &n->next;
  596. WRITE_ONCE(*(n->pprev), n);
  597. }
  598. static inline void hlist_add_behind(struct hlist_node *n,
  599. struct hlist_node *prev)
  600. {
  601. n->next = prev->next;
  602. WRITE_ONCE(prev->next, n);
  603. n->pprev = &prev->next;
  604. if (n->next)
  605. n->next->pprev = &n->next;
  606. }
  607. /* after that we'll appear to be on some hlist and hlist_del will work */
  608. static inline void hlist_add_fake(struct hlist_node *n)
  609. {
  610. n->pprev = &n->next;
  611. }
  612. static inline bool hlist_fake(struct hlist_node *h)
  613. {
  614. return h->pprev == &h->next;
  615. }
  616. /*
  617. * Check whether the node is the only node of the head without
  618. * accessing head:
  619. */
  620. static inline bool
  621. hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
  622. {
  623. return !n->next && n->pprev == &h->first;
  624. }
  625. /*
  626. * Move a list from one list head to another. Fixup the pprev
  627. * reference of the first entry if it exists.
  628. */
  629. static inline void hlist_move_list(struct hlist_head *old,
  630. struct hlist_head *new)
  631. {
  632. new->first = old->first;
  633. if (new->first)
  634. new->first->pprev = &new->first;
  635. old->first = NULL;
  636. }
  637. #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
  638. #define hlist_for_each(pos, head) \
  639. for (pos = (head)->first; pos ; pos = pos->next)
  640. #define hlist_for_each_safe(pos, n, head) \
  641. for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
  642. pos = n)
  643. #define hlist_entry_safe(ptr, type, member) \
  644. ({ typeof(ptr) ____ptr = (ptr); \
  645. ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
  646. })
  647. /**
  648. * hlist_for_each_entry - iterate over list of given type
  649. * @pos: the type * to use as a loop cursor.
  650. * @head: the head for your list.
  651. * @member: the name of the hlist_node within the struct.
  652. */
  653. #define hlist_for_each_entry(pos, head, member) \
  654. for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
  655. pos; \
  656. pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
  657. /**
  658. * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
  659. * @pos: the type * to use as a loop cursor.
  660. * @member: the name of the hlist_node within the struct.
  661. */
  662. #define hlist_for_each_entry_continue(pos, member) \
  663. for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
  664. pos; \
  665. pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
  666. /**
  667. * hlist_for_each_entry_from - iterate over a hlist continuing from current point
  668. * @pos: the type * to use as a loop cursor.
  669. * @member: the name of the hlist_node within the struct.
  670. */
  671. #define hlist_for_each_entry_from(pos, member) \
  672. for (; pos; \
  673. pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
  674. /**
  675. * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  676. * @pos: the type * to use as a loop cursor.
  677. * @n: another &struct hlist_node to use as temporary storage
  678. * @head: the head for your list.
  679. * @member: the name of the hlist_node within the struct.
  680. */
  681. #define hlist_for_each_entry_safe(pos, n, head, member) \
  682. for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
  683. pos && ({ n = pos->member.next; 1; }); \
  684. pos = hlist_entry_safe(n, typeof(*pos), member))
  685. #endif