list.h 21 KB

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