list.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*-
  2. * Copyright (c) 2011 Felix Fietkau <nbd@openwrt.org>
  3. * Copyright (c) 2010 Isilon Systems, Inc.
  4. * Copyright (c) 2010 iX Systems, Inc.
  5. * Copyright (c) 2010 Panasas, Inc.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice unmodified, this list of conditions, and the following
  13. * disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef _LINUX_LIST_H_
  30. #define _LINUX_LIST_H_
  31. #include <stddef.h>
  32. #include <stdbool.h>
  33. #define prefetch(x)
  34. #ifndef container_of
  35. #define container_of(ptr, type, member) \
  36. ({ \
  37. const __typeof__(((type *) NULL)->member) *__mptr = (ptr); \
  38. (type *) ((char *) __mptr - offsetof(type, member)); \
  39. })
  40. #endif
  41. #ifndef container_of_safe
  42. #define container_of_safe(ptr, type, member) \
  43. ({ \
  44. const __typeof__(((type *) NULL)->member) *__mptr = (ptr); \
  45. __mptr ? (type *)((char *) __mptr - offsetof(type, member)) : NULL; \
  46. })
  47. #endif
  48. struct list_head {
  49. struct list_head *next;
  50. struct list_head *prev;
  51. };
  52. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  53. #undef LIST_HEAD
  54. #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
  55. static inline void
  56. INIT_LIST_HEAD(struct list_head *list)
  57. {
  58. list->next = list->prev = list;
  59. }
  60. static inline bool
  61. list_empty(const struct list_head *head)
  62. {
  63. return (head->next == head);
  64. }
  65. static inline bool
  66. list_is_first(const struct list_head *list,
  67. const struct list_head *head)
  68. {
  69. return list->prev == head;
  70. }
  71. static inline bool
  72. list_is_last(const struct list_head *list,
  73. const struct list_head *head)
  74. {
  75. return list->next == head;
  76. }
  77. static inline void
  78. _list_del(struct list_head *entry)
  79. {
  80. entry->next->prev = entry->prev;
  81. entry->prev->next = entry->next;
  82. }
  83. static inline void
  84. list_del(struct list_head *entry)
  85. {
  86. _list_del(entry);
  87. entry->next = entry->prev = NULL;
  88. }
  89. static inline void
  90. _list_add(struct list_head *_new, struct list_head *prev,
  91. struct list_head *next)
  92. {
  93. next->prev = _new;
  94. _new->next = next;
  95. _new->prev = prev;
  96. prev->next = _new;
  97. }
  98. static inline void
  99. list_del_init(struct list_head *entry)
  100. {
  101. _list_del(entry);
  102. INIT_LIST_HEAD(entry);
  103. }
  104. #define list_entry(ptr, type, field) container_of(ptr, type, field)
  105. #define list_first_entry(ptr, type, field) list_entry((ptr)->next, type, field)
  106. #define list_last_entry(ptr, type, field) list_entry((ptr)->prev, type, field)
  107. #define list_next_entry(pos, member) list_entry((pos)->member.next, typeof(*(pos)), member)
  108. #define list_entry_is_h(p, h, field) (&p->field == (h))
  109. #define list_for_each(p, head) \
  110. for (p = (head)->next; p != (head); p = p->next)
  111. #define list_for_each_safe(p, n, head) \
  112. for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
  113. #define list_for_each_entry(p, h, field) \
  114. for (p = list_first_entry(h, __typeof__(*p), field); &p->field != (h); \
  115. p = list_entry(p->field.next, __typeof__(*p), field))
  116. #define list_for_each_entry_continue(p, h, field) \
  117. for (p = list_next_entry(p, field); \
  118. !list_entry_is_h(p, h, field); \
  119. p = list_next_entry(p, field))
  120. #define list_for_each_entry_continue_reverse(p, h, field) \
  121. for (p = list_prev_entry(p, field); \
  122. !list_entry_is_h(p, h, field); \
  123. p = list_prev_entry(p, field))
  124. #define list_for_each_entry_safe(p, n, h, field) \
  125. for (p = list_first_entry(h, __typeof__(*p), field), \
  126. n = list_entry(p->field.next, __typeof__(*p), field); &p->field != (h);\
  127. p = n, n = list_entry(n->field.next, __typeof__(*n), field))
  128. #define list_for_each_entry_reverse(p, h, field) \
  129. for (p = list_last_entry(h, __typeof__(*p), field); &p->field != (h); \
  130. p = list_entry(p->field.prev, __typeof__(*p), field))
  131. #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
  132. #define list_for_each_prev_safe(p, n, h) for (p = (h)->prev, n = p->prev; p != (h); p = n, n = p->prev)
  133. static inline void
  134. list_add(struct list_head *_new, struct list_head *head)
  135. {
  136. _list_add(_new, head, head->next);
  137. }
  138. static inline void
  139. list_add_tail(struct list_head *_new, struct list_head *head)
  140. {
  141. _list_add(_new, head->prev, head);
  142. }
  143. static inline void
  144. list_move(struct list_head *list, struct list_head *head)
  145. {
  146. _list_del(list);
  147. list_add(list, head);
  148. }
  149. static inline void
  150. list_move_tail(struct list_head *entry, struct list_head *head)
  151. {
  152. _list_del(entry);
  153. list_add_tail(entry, head);
  154. }
  155. static inline void
  156. _list_splice(const struct list_head *list, struct list_head *prev,
  157. struct list_head *next)
  158. {
  159. struct list_head *first;
  160. struct list_head *last;
  161. if (list_empty(list))
  162. return;
  163. first = list->next;
  164. last = list->prev;
  165. first->prev = prev;
  166. prev->next = first;
  167. last->next = next;
  168. next->prev = last;
  169. }
  170. static inline void
  171. list_splice(const struct list_head *list, struct list_head *head)
  172. {
  173. _list_splice(list, head, head->next);
  174. }
  175. static inline void
  176. list_splice_tail(struct list_head *list, struct list_head *head)
  177. {
  178. _list_splice(list, head->prev, head);
  179. }
  180. static inline void
  181. list_splice_init(struct list_head *list, struct list_head *head)
  182. {
  183. _list_splice(list, head, head->next);
  184. INIT_LIST_HEAD(list);
  185. }
  186. static inline void
  187. list_splice_tail_init(struct list_head *list, struct list_head *head)
  188. {
  189. _list_splice(list, head->prev, head);
  190. INIT_LIST_HEAD(list);
  191. }
  192. #endif /* _LINUX_LIST_H_ */