list.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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_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 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. (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
  350. /**
  351. * list_next_entry - get the next element in list
  352. * @pos: the type * to cursor
  353. * @member: the name of the list_head within the struct.
  354. */
  355. #define list_next_entry(pos, member) \
  356. list_entry((pos)->member.next, typeof(*(pos)), member)
  357. /**
  358. * list_prev_entry - get the prev element in list
  359. * @pos: the type * to cursor
  360. * @member: the name of the list_head within the struct.
  361. */
  362. #define list_prev_entry(pos, member) \
  363. list_entry((pos)->member.prev, typeof(*(pos)), member)
  364. /**
  365. * list_for_each - iterate over a list
  366. * @pos: the &struct list_head to use as a loop cursor.
  367. * @head: the head for your list.
  368. */
  369. #define list_for_each(pos, head) \
  370. for (pos = (head)->next; pos != (head); pos = pos->next)
  371. /**
  372. * list_for_each_prev - iterate over a list backwards
  373. * @pos: the &struct list_head to use as a loop cursor.
  374. * @head: the head for your list.
  375. */
  376. #define list_for_each_prev(pos, head) \
  377. for (pos = (head)->prev; pos != (head); pos = pos->prev)
  378. /**
  379. * list_for_each_safe - iterate over a list safe against removal of list entry
  380. * @pos: the &struct list_head to use as a loop cursor.
  381. * @n: another &struct list_head to use as temporary storage
  382. * @head: the head for your list.
  383. */
  384. #define list_for_each_safe(pos, n, head) \
  385. for (pos = (head)->next, n = pos->next; pos != (head); \
  386. pos = n, n = pos->next)
  387. /**
  388. * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
  389. * @pos: the &struct list_head to use as a loop cursor.
  390. * @n: another &struct list_head to use as temporary storage
  391. * @head: the head for your list.
  392. */
  393. #define list_for_each_prev_safe(pos, n, head) \
  394. for (pos = (head)->prev, n = pos->prev; \
  395. pos != (head); \
  396. pos = n, n = pos->prev)
  397. /**
  398. * list_for_each_entry - iterate over list of given type
  399. * @pos: the type * to use as a loop cursor.
  400. * @head: the head for your list.
  401. * @member: the name of the list_head within the struct.
  402. */
  403. #define list_for_each_entry(pos, head, member) \
  404. for (pos = list_first_entry(head, typeof(*pos), member); \
  405. &pos->member != (head); \
  406. pos = list_next_entry(pos, member))
  407. /**
  408. * list_for_each_entry_reverse - iterate backwards over list of given type.
  409. * @pos: the type * to use as a loop cursor.
  410. * @head: the head for your list.
  411. * @member: the name of the list_head within the struct.
  412. */
  413. #define list_for_each_entry_reverse(pos, head, member) \
  414. for (pos = list_last_entry(head, typeof(*pos), member); \
  415. &pos->member != (head); \
  416. pos = list_prev_entry(pos, member))
  417. /**
  418. * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
  419. * @pos: the type * to use as a start point
  420. * @head: the head of the list
  421. * @member: the name of the list_head within the struct.
  422. *
  423. * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
  424. */
  425. #define list_prepare_entry(pos, head, member) \
  426. ((pos) ? : list_entry(head, typeof(*pos), member))
  427. /**
  428. * list_for_each_entry_continue - continue iteration over list of given type
  429. * @pos: the type * to use as a loop cursor.
  430. * @head: the head for your list.
  431. * @member: the name of the list_head within the struct.
  432. *
  433. * Continue to iterate over list of given type, continuing after
  434. * the current position.
  435. */
  436. #define list_for_each_entry_continue(pos, head, member) \
  437. for (pos = list_next_entry(pos, member); \
  438. &pos->member != (head); \
  439. pos = list_next_entry(pos, member))
  440. /**
  441. * list_for_each_entry_continue_reverse - iterate backwards from the given point
  442. * @pos: the type * to use as a loop cursor.
  443. * @head: the head for your list.
  444. * @member: the name of the list_head within the struct.
  445. *
  446. * Start to iterate over list of given type backwards, continuing after
  447. * the current position.
  448. */
  449. #define list_for_each_entry_continue_reverse(pos, head, member) \
  450. for (pos = list_prev_entry(pos, member); \
  451. &pos->member != (head); \
  452. pos = list_prev_entry(pos, member))
  453. /**
  454. * list_for_each_entry_from - iterate over list of given type from the current point
  455. * @pos: the type * to use as a loop cursor.
  456. * @head: the head for your list.
  457. * @member: the name of the list_head within the struct.
  458. *
  459. * Iterate over list of given type, continuing from current position.
  460. */
  461. #define list_for_each_entry_from(pos, head, member) \
  462. for (; &pos->member != (head); \
  463. pos = list_next_entry(pos, member))
  464. /**
  465. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  466. * @pos: the type * to use as a loop cursor.
  467. * @n: another type * to use as temporary storage
  468. * @head: the head for your list.
  469. * @member: the name of the list_head within the struct.
  470. */
  471. #define list_for_each_entry_safe(pos, n, head, member) \
  472. for (pos = list_first_entry(head, typeof(*pos), member), \
  473. n = list_next_entry(pos, member); \
  474. &pos->member != (head); \
  475. pos = n, n = list_next_entry(n, member))
  476. /**
  477. * list_for_each_entry_safe_continue - continue list iteration safe against removal
  478. * @pos: the type * to use as a loop cursor.
  479. * @n: another type * to use as temporary storage
  480. * @head: the head for your list.
  481. * @member: the name of the list_head within the struct.
  482. *
  483. * Iterate over list of given type, continuing after current point,
  484. * safe against removal of list entry.
  485. */
  486. #define list_for_each_entry_safe_continue(pos, n, head, member) \
  487. for (pos = list_next_entry(pos, member), \
  488. n = list_next_entry(pos, member); \
  489. &pos->member != (head); \
  490. pos = n, n = list_next_entry(n, member))
  491. /**
  492. * list_for_each_entry_safe_from - iterate over list from current point safe against removal
  493. * @pos: the type * to use as a loop cursor.
  494. * @n: another type * to use as temporary storage
  495. * @head: the head for your list.
  496. * @member: the name of the list_head within the struct.
  497. *
  498. * Iterate over list of given type from current point, safe against
  499. * removal of list entry.
  500. */
  501. #define list_for_each_entry_safe_from(pos, n, head, member) \
  502. for (n = list_next_entry(pos, member); \
  503. &pos->member != (head); \
  504. pos = n, n = list_next_entry(n, member))
  505. /**
  506. * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
  507. * @pos: the type * to use as a loop cursor.
  508. * @n: another type * to use as temporary storage
  509. * @head: the head for your list.
  510. * @member: the name of the list_head within the struct.
  511. *
  512. * Iterate backwards over list of given type, safe against removal
  513. * of list entry.
  514. */
  515. #define list_for_each_entry_safe_reverse(pos, n, head, member) \
  516. for (pos = list_last_entry(head, typeof(*pos), member), \
  517. n = list_prev_entry(pos, member); \
  518. &pos->member != (head); \
  519. pos = n, n = list_prev_entry(n, member))
  520. /**
  521. * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
  522. * @pos: the loop cursor used in the list_for_each_entry_safe loop
  523. * @n: temporary storage used in list_for_each_entry_safe
  524. * @member: the name of the list_head within the struct.
  525. *
  526. * list_safe_reset_next is not safe to use in general if the list may be
  527. * modified concurrently (eg. the lock is dropped in the loop body). An
  528. * exception to this is if the cursor element (pos) is pinned in the list,
  529. * and list_safe_reset_next is called after re-taking the lock and before
  530. * completing the current iteration of the loop body.
  531. */
  532. #define list_safe_reset_next(pos, n, member) \
  533. n = list_next_entry(pos, member)
  534. /*
  535. * Double linked lists with a single pointer list head.
  536. * Mostly useful for hash tables where the two pointer list head is
  537. * too wasteful.
  538. * You lose the ability to access the tail in O(1).
  539. */
  540. #define HLIST_HEAD_INIT { .first = NULL }
  541. #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
  542. #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
  543. static inline void INIT_HLIST_NODE(struct hlist_node *h)
  544. {
  545. h->next = NULL;
  546. h->pprev = NULL;
  547. }
  548. static inline int hlist_unhashed(const struct hlist_node *h)
  549. {
  550. return !h->pprev;
  551. }
  552. static inline int hlist_empty(const struct hlist_head *h)
  553. {
  554. return !h->first;
  555. }
  556. static inline void __hlist_del(struct hlist_node *n)
  557. {
  558. struct hlist_node *next = n->next;
  559. struct hlist_node **pprev = n->pprev;
  560. WRITE_ONCE(*pprev, next);
  561. if (next)
  562. next->pprev = pprev;
  563. }
  564. static inline void hlist_del(struct hlist_node *n)
  565. {
  566. __hlist_del(n);
  567. n->next = LIST_POISON1;
  568. n->pprev = LIST_POISON2;
  569. }
  570. static inline void hlist_del_init(struct hlist_node *n)
  571. {
  572. if (!hlist_unhashed(n)) {
  573. __hlist_del(n);
  574. INIT_HLIST_NODE(n);
  575. }
  576. }
  577. static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
  578. {
  579. struct hlist_node *first = h->first;
  580. n->next = first;
  581. if (first)
  582. first->pprev = &n->next;
  583. h->first = n;
  584. n->pprev = &h->first;
  585. }
  586. /* next must be != NULL */
  587. static inline void hlist_add_before(struct hlist_node *n,
  588. struct hlist_node *next)
  589. {
  590. n->pprev = next->pprev;
  591. n->next = next;
  592. next->pprev = &n->next;
  593. *(n->pprev) = n;
  594. }
  595. static inline void hlist_add_behind(struct hlist_node *n,
  596. struct hlist_node *prev)
  597. {
  598. n->next = prev->next;
  599. prev->next = n;
  600. n->pprev = &prev->next;
  601. if (n->next)
  602. n->next->pprev = &n->next;
  603. }
  604. /* after that we'll appear to be on some hlist and hlist_del will work */
  605. static inline void hlist_add_fake(struct hlist_node *n)
  606. {
  607. n->pprev = &n->next;
  608. }
  609. static inline bool hlist_fake(struct hlist_node *h)
  610. {
  611. return h->pprev == &h->next;
  612. }
  613. /*
  614. * Move a list from one list head to another. Fixup the pprev
  615. * reference of the first entry if it exists.
  616. */
  617. static inline void hlist_move_list(struct hlist_head *old,
  618. struct hlist_head *new)
  619. {
  620. new->first = old->first;
  621. if (new->first)
  622. new->first->pprev = &new->first;
  623. old->first = NULL;
  624. }
  625. #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
  626. #define hlist_for_each(pos, head) \
  627. for (pos = (head)->first; pos ; pos = pos->next)
  628. #define hlist_for_each_safe(pos, n, head) \
  629. for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
  630. pos = n)
  631. #define hlist_entry_safe(ptr, type, member) \
  632. ({ typeof(ptr) ____ptr = (ptr); \
  633. ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
  634. })
  635. /**
  636. * hlist_for_each_entry - iterate over list of given type
  637. * @pos: the type * to use as a loop cursor.
  638. * @head: the head for your list.
  639. * @member: the name of the hlist_node within the struct.
  640. */
  641. #define hlist_for_each_entry(pos, head, member) \
  642. for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
  643. pos; \
  644. pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
  645. /**
  646. * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
  647. * @pos: the type * to use as a loop cursor.
  648. * @member: the name of the hlist_node within the struct.
  649. */
  650. #define hlist_for_each_entry_continue(pos, member) \
  651. for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
  652. pos; \
  653. pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
  654. /**
  655. * hlist_for_each_entry_from - iterate over a hlist continuing from current point
  656. * @pos: the type * to use as a loop cursor.
  657. * @member: the name of the hlist_node within the struct.
  658. */
  659. #define hlist_for_each_entry_from(pos, member) \
  660. for (; pos; \
  661. pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
  662. /**
  663. * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  664. * @pos: the type * to use as a loop cursor.
  665. * @n: another &struct hlist_node to use as temporary storage
  666. * @head: the head for your list.
  667. * @member: the name of the hlist_node within the struct.
  668. */
  669. #define hlist_for_each_entry_safe(pos, n, head, member) \
  670. for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
  671. pos && ({ n = pos->member.next; 1; }); \
  672. pos = hlist_entry_safe(n, typeof(*pos), member))
  673. /**
  674. * list_del_range - deletes range of entries from list.
  675. * @begin: first element in the range to delete from the list.
  676. * @end: last element in the range to delete from the list.
  677. * Note: list_empty on the range of entries does not return true after this,
  678. * the entries is in an undefined state.
  679. */
  680. static inline void list_del_range(struct list_head *begin,
  681. struct list_head *end)
  682. {
  683. begin->prev->next = end->next;
  684. end->next->prev = begin->prev;
  685. }
  686. /**
  687. * list_for_each_from - iterate over a list from one of its nodes
  688. * @pos: the &struct list_head to use as a loop cursor, from where to start
  689. * @head: the head for your list.
  690. */
  691. #define list_for_each_from(pos, head) \
  692. for (; pos != (head); pos = pos->next)
  693. #endif /* __TOOLS_LINUX_LIST_H */