list.h 17 KB

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