list.h 17 KB

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