list.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /* GPL v2, adapted from the Linux kernel */
  2. #ifndef _LINUX_LIST_H
  3. #define _LINUX_LIST_H
  4. #include <stddef.h>
  5. /**
  6. * container_of - cast a member of a structure out to the containing structure
  7. * @ptr: the pointer to the member.
  8. * @type: the type of the container struct this is embedded in.
  9. * @member: the name of the member within the struct.
  10. *
  11. */
  12. #ifndef container_of
  13. #define container_of(ptr, type, member) ( \
  14. (type *)( (char *)ptr - offsetof(type,member) ))
  15. #endif
  16. /*
  17. * Simple doubly linked list implementation.
  18. *
  19. * Some of the internal functions ("__xxx") are useful when
  20. * manipulating whole lists rather than single entries, as
  21. * sometimes we already know the next/prev entries and we can
  22. * generate better code by using them directly rather than
  23. * using the generic single-entry routines.
  24. */
  25. struct list_head {
  26. struct list_head *next, *prev;
  27. };
  28. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  29. #define LIST_HEAD(name) \
  30. struct list_head name = LIST_HEAD_INIT(name)
  31. static inline void INIT_LIST_HEAD(struct list_head *list)
  32. {
  33. list->next = list;
  34. list->prev = list;
  35. }
  36. /*
  37. * Insert a new entry between two known consecutive entries.
  38. *
  39. * This is only for internal list manipulation where we know
  40. * the prev/next entries already!
  41. */
  42. static inline void __list_add(struct list_head *new,
  43. struct list_head *prev,
  44. struct list_head *next)
  45. {
  46. next->prev = new;
  47. new->next = next;
  48. new->prev = prev;
  49. prev->next = new;
  50. }
  51. /**
  52. * list_add - add a new entry
  53. * @new: new entry to be added
  54. * @head: list head to add it after
  55. *
  56. * Insert a new entry after the specified head.
  57. * This is good for implementing stacks.
  58. */
  59. static inline void list_add(struct list_head *new, struct list_head *head)
  60. {
  61. __list_add(new, head, head->next);
  62. }
  63. /**
  64. * list_add_tail - add a new entry
  65. * @new: new entry to be added
  66. * @head: list head to add it before
  67. *
  68. * Insert a new entry before the specified head.
  69. * This is useful for implementing queues.
  70. */
  71. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  72. {
  73. __list_add(new, head->prev, head);
  74. }
  75. /*
  76. * Delete a list entry by making the prev/next entries
  77. * point to each other.
  78. *
  79. * This is only for internal list manipulation where we know
  80. * the prev/next entries already!
  81. */
  82. static inline void __list_del(struct list_head * prev, struct list_head * next)
  83. {
  84. next->prev = prev;
  85. prev->next = next;
  86. }
  87. /**
  88. * list_del - deletes entry from list.
  89. * @entry: the element to delete from the list.
  90. * Note: list_empty() on entry does not return true after this, the entry is
  91. * in an undefined state.
  92. */
  93. static inline void list_del(struct list_head *entry)
  94. {
  95. __list_del(entry->prev, entry->next);
  96. entry->next = NULL;
  97. entry->prev = NULL;
  98. }
  99. /**
  100. * list_replace - replace old entry by new one
  101. * @old : the element to be replaced
  102. * @new : the new element to insert
  103. *
  104. * If @old was empty, it will be overwritten.
  105. */
  106. static inline void list_replace(struct list_head *old,
  107. struct list_head *new)
  108. {
  109. new->next = old->next;
  110. new->next->prev = new;
  111. new->prev = old->prev;
  112. new->prev->next = new;
  113. }
  114. static inline void list_replace_init(struct list_head *old,
  115. struct list_head *new)
  116. {
  117. list_replace(old, new);
  118. INIT_LIST_HEAD(old);
  119. }
  120. /**
  121. * list_del_init - deletes entry from list and reinitialize it.
  122. * @entry: the element to delete from the list.
  123. */
  124. static inline void list_del_init(struct list_head *entry)
  125. {
  126. __list_del(entry->prev, entry->next);
  127. INIT_LIST_HEAD(entry);
  128. }
  129. /**
  130. * list_move - delete from one list and add as another's head
  131. * @list: the entry to move
  132. * @head: the head that will precede our entry
  133. */
  134. static inline void list_move(struct list_head *list, struct list_head *head)
  135. {
  136. __list_del(list->prev, list->next);
  137. list_add(list, head);
  138. }
  139. /**
  140. * list_move_tail - delete from one list and add as another's tail
  141. * @list: the entry to move
  142. * @head: the head that will follow our entry
  143. */
  144. static inline void list_move_tail(struct list_head *list,
  145. struct list_head *head)
  146. {
  147. __list_del(list->prev, list->next);
  148. list_add_tail(list, head);
  149. }
  150. /**
  151. * list_is_last - tests whether @list is the last entry in list @head
  152. * @list: the entry to test
  153. * @head: the head of the list
  154. */
  155. static inline int list_is_last(const struct list_head *list,
  156. const struct list_head *head)
  157. {
  158. return list->next == head;
  159. }
  160. /**
  161. * list_empty - tests whether a list is empty
  162. * @head: the list to test.
  163. */
  164. static inline int list_empty(const struct list_head *head)
  165. {
  166. return head->next == head;
  167. }
  168. /**
  169. * list_empty_careful - tests whether a list is empty and not being modified
  170. * @head: the list to test
  171. *
  172. * Description:
  173. * tests whether a list is empty _and_ checks that no other CPU might be
  174. * in the process of modifying either member (next or prev)
  175. *
  176. * NOTE: using list_empty_careful() without synchronization
  177. * can only be safe if the only activity that can happen
  178. * to the list entry is list_del_init(). Eg. it cannot be used
  179. * if another CPU could re-list_add() it.
  180. */
  181. static inline int list_empty_careful(const struct list_head *head)
  182. {
  183. struct list_head *next = head->next;
  184. return (next == head) && (next == head->prev);
  185. }
  186. static inline void __list_splice(struct list_head *list,
  187. struct list_head *head)
  188. {
  189. struct list_head *first = list->next;
  190. struct list_head *last = list->prev;
  191. struct list_head *at = head->next;
  192. first->prev = head;
  193. head->next = first;
  194. last->next = at;
  195. at->prev = last;
  196. }
  197. /**
  198. * list_splice - join two lists
  199. * @list: the new list to add.
  200. * @head: the place to add it in the first list.
  201. */
  202. static inline void list_splice(struct list_head *list, struct list_head *head)
  203. {
  204. if (!list_empty(list))
  205. __list_splice(list, head);
  206. }
  207. /**
  208. * list_splice_init - join two lists and reinitialise the emptied list.
  209. * @list: the new list to add.
  210. * @head: the place to add it in the first list.
  211. *
  212. * The list at @list is reinitialised
  213. */
  214. static inline void list_splice_init(struct list_head *list,
  215. struct list_head *head)
  216. {
  217. if (!list_empty(list)) {
  218. __list_splice(list, head);
  219. INIT_LIST_HEAD(list);
  220. }
  221. }
  222. /**
  223. * list_entry - get the struct for this entry
  224. * @ptr: the &struct list_head pointer.
  225. * @type: the type of the struct this is embedded in.
  226. * @member: the name of the list_struct within the struct.
  227. */
  228. #define list_entry(ptr, type, member) \
  229. container_of(ptr, type, member)
  230. /**
  231. * list_first_entry - get the first element from a list
  232. * @ptr: the list head to take the element from.
  233. * @type: the type of the struct this is embedded in.
  234. * @member: the name of the list_struct within the struct.
  235. *
  236. * Note, that list is expected to be not empty.
  237. */
  238. #define list_first_entry(ptr, type, member) \
  239. list_entry((ptr)->next, type, member)
  240. /**
  241. * list_for_each - iterate over a list
  242. * @pos: the &struct list_head to use as a loop cursor.
  243. * @head: the head for your list.
  244. */
  245. #define list_for_each(pos, head) \
  246. for (pos = (head)->next; pos != (head); \
  247. pos = pos->next)
  248. /**
  249. * __list_for_each - iterate over a list
  250. * @pos: the &struct list_head to use as a loop cursor.
  251. * @head: the head for your list.
  252. *
  253. * This variant differs from list_for_each() in that it's the
  254. * simplest possible list iteration code, no prefetching is done.
  255. * Use this for code that knows the list to be very short (empty
  256. * or 1 entry) most of the time.
  257. */
  258. #define __list_for_each(pos, head) \
  259. for (pos = (head)->next; pos != (head); pos = pos->next)
  260. /**
  261. * list_for_each_prev - iterate over a list backwards
  262. * @pos: the &struct list_head to use as a loop cursor.
  263. * @head: the head for your list.
  264. */
  265. #define list_for_each_prev(pos, head) \
  266. for (pos = (head)->prev; pos != (head); \
  267. pos = pos->prev)
  268. /**
  269. * list_for_each_safe - iterate over a list safe against removal of list entry
  270. * @pos: the &struct list_head to use as a loop cursor.
  271. * @n: another &struct list_head to use as temporary storage
  272. * @head: the head for your list.
  273. */
  274. #define list_for_each_safe(pos, n, head) \
  275. for (pos = (head)->next, n = pos->next; pos != (head); \
  276. pos = n, n = pos->next)
  277. /**
  278. * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
  279. * @pos: the &struct list_head to use as a loop cursor.
  280. * @n: another &struct list_head to use as temporary storage
  281. * @head: the head for your list.
  282. */
  283. #define list_for_each_prev_safe(pos, n, head) \
  284. for (pos = (head)->prev, n = pos->prev; \
  285. pos != (head); \
  286. pos = n, n = pos->prev)
  287. /**
  288. * list_for_each_entry - iterate over list of given type
  289. * @pos: the type * to use as a loop cursor.
  290. * @head: the head for your list.
  291. * @member: the name of the list_struct within the struct.
  292. */
  293. #define list_for_each_entry(pos, head, member) \
  294. for (pos = list_entry((head)->next, typeof(*pos), member); \
  295. &pos->member != (head); \
  296. pos = list_entry(pos->member.next, typeof(*pos), member))
  297. /**
  298. * list_for_each_entry_reverse - iterate backwards over list of given type.
  299. * @pos: the type * to use as a loop cursor.
  300. * @head: the head for your list.
  301. * @member: the name of the list_struct within the struct.
  302. */
  303. #define list_for_each_entry_reverse(pos, head, member) \
  304. for (pos = list_entry((head)->prev, typeof(*pos), member); \
  305. &pos->member != (head); \
  306. pos = list_entry(pos->member.prev, typeof(*pos), member))
  307. /**
  308. * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
  309. * @pos: the type * to use as a start point
  310. * @head: the head of the list
  311. * @member: the name of the list_struct within the struct.
  312. *
  313. * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
  314. */
  315. #define list_prepare_entry(pos, head, member) \
  316. ((pos) ? : list_entry(head, typeof(*pos), member))
  317. /**
  318. * list_for_each_entry_continue - continue iteration over list of given type
  319. * @pos: the type * to use as a loop cursor.
  320. * @head: the head for your list.
  321. * @member: the name of the list_struct within the struct.
  322. *
  323. * Continue to iterate over list of given type, continuing after
  324. * the current position.
  325. */
  326. #define list_for_each_entry_continue(pos, head, member) \
  327. for (pos = list_entry(pos->member.next, typeof(*pos), member); \
  328. &pos->member != (head); \
  329. pos = list_entry(pos->member.next, typeof(*pos), member))
  330. /**
  331. * list_for_each_entry_continue_reverse - iterate backwards from the given point
  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. * Start to iterate over list of given type backwards, continuing after
  337. * the current position.
  338. */
  339. #define list_for_each_entry_continue_reverse(pos, head, member) \
  340. for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
  341. &pos->member != (head); \
  342. pos = list_entry(pos->member.prev, 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 = NULL;
  450. n->pprev = NULL;
  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; pos = pos->next)
  489. #define hlist_for_each_safe(pos, n, head) \
  490. for (pos = (head)->first; pos; pos = n)
  491. /**
  492. * hlist_for_each_entry - iterate over list of given type
  493. * @tpos: the type * to use as a loop cursor.
  494. * @pos: the &struct hlist_node to use as a loop cursor.
  495. * @head: the head for your list.
  496. * @member: the name of the hlist_node within the struct.
  497. */
  498. #define hlist_for_each_entry(tpos, pos, head, member) \
  499. for (pos = (head)->first; pos && \
  500. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  501. pos = pos->next)
  502. /**
  503. * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
  504. * @tpos: the type * to use as a loop cursor.
  505. * @pos: the &struct hlist_node to use as a loop cursor.
  506. * @member: the name of the hlist_node within the struct.
  507. */
  508. #define hlist_for_each_entry_continue(tpos, pos, member) \
  509. for (pos = (pos)->next; pos && \
  510. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  511. pos = pos->next)
  512. /**
  513. * hlist_for_each_entry_from - iterate over a hlist continuing from current point
  514. * @tpos: the type * to use as a loop cursor.
  515. * @pos: the &struct hlist_node to use as a loop cursor.
  516. * @member: the name of the hlist_node within the struct.
  517. */
  518. #define hlist_for_each_entry_from(tpos, pos, member) \
  519. for (; pos && \
  520. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  521. pos = pos->next)
  522. /**
  523. * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  524. * @tpos: the type * to use as a loop cursor.
  525. * @pos: the &struct hlist_node to use as a loop cursor.
  526. * @n: another &struct hlist_node to use as temporary storage
  527. * @head: the head for your list.
  528. * @member: the name of the hlist_node within the struct.
  529. */
  530. #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
  531. for (pos = (head)->first; \
  532. pos && ({ n = pos->next; 1; }) && \
  533. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  534. pos = n)
  535. #endif