list.h 23 KB

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