list.h 22 KB

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