list.h 18 KB

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