list.h 19 KB

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