list.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. struct list_head {
  42. struct list_head *next;
  43. struct list_head *prev;
  44. };
  45. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  46. #undef LIST_HEAD
  47. #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
  48. static inline void
  49. INIT_LIST_HEAD(struct list_head *list)
  50. {
  51. list->next = list->prev = list;
  52. }
  53. static inline bool
  54. list_empty(const struct list_head *head)
  55. {
  56. return (head->next == head);
  57. }
  58. static inline bool
  59. list_is_first(const struct list_head *list,
  60. const struct list_head *head)
  61. {
  62. return list->prev == head;
  63. }
  64. static inline bool
  65. list_is_last(const struct list_head *list,
  66. const struct list_head *head)
  67. {
  68. return list->next == head;
  69. }
  70. static inline void
  71. _list_del(struct list_head *entry)
  72. {
  73. entry->next->prev = entry->prev;
  74. entry->prev->next = entry->next;
  75. }
  76. static inline void
  77. list_del(struct list_head *entry)
  78. {
  79. _list_del(entry);
  80. entry->next = entry->prev = NULL;
  81. }
  82. static inline void
  83. _list_add(struct list_head *_new, struct list_head *prev,
  84. struct list_head *next)
  85. {
  86. next->prev = _new;
  87. _new->next = next;
  88. _new->prev = prev;
  89. prev->next = _new;
  90. }
  91. static inline void
  92. list_del_init(struct list_head *entry)
  93. {
  94. _list_del(entry);
  95. INIT_LIST_HEAD(entry);
  96. }
  97. #define list_entry(ptr, type, field) container_of(ptr, type, field)
  98. #define list_first_entry(ptr, type, field) list_entry((ptr)->next, type, field)
  99. #define list_last_entry(ptr, type, field) list_entry((ptr)->prev, type, field)
  100. #define list_for_each(p, head) \
  101. for (p = (head)->next; p != (head); p = p->next)
  102. #define list_for_each_safe(p, n, head) \
  103. for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
  104. #define list_for_each_entry(p, h, field) \
  105. for (p = list_first_entry(h, __typeof__(*p), field); &p->field != (h); \
  106. p = list_entry(p->field.next, __typeof__(*p), field))
  107. #define list_for_each_entry_safe(p, n, h, field) \
  108. for (p = list_first_entry(h, __typeof__(*p), field), \
  109. n = list_entry(p->field.next, __typeof__(*p), field); &p->field != (h);\
  110. p = n, n = list_entry(n->field.next, __typeof__(*n), field))
  111. #define list_for_each_entry_reverse(p, h, field) \
  112. for (p = list_last_entry(h, __typeof__(*p), field); &p->field != (h); \
  113. p = list_entry(p->field.prev, __typeof__(*p), field))
  114. #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
  115. #define list_for_each_prev_safe(p, n, h) for (p = (h)->prev, n = p->prev; p != (h); p = n, n = p->prev)
  116. static inline void
  117. list_add(struct list_head *_new, struct list_head *head)
  118. {
  119. _list_add(_new, head, head->next);
  120. }
  121. static inline void
  122. list_add_tail(struct list_head *_new, struct list_head *head)
  123. {
  124. _list_add(_new, head->prev, head);
  125. }
  126. static inline void
  127. list_move(struct list_head *list, struct list_head *head)
  128. {
  129. _list_del(list);
  130. list_add(list, head);
  131. }
  132. static inline void
  133. list_move_tail(struct list_head *entry, struct list_head *head)
  134. {
  135. _list_del(entry);
  136. list_add_tail(entry, head);
  137. }
  138. static inline void
  139. _list_splice(const struct list_head *list, struct list_head *prev,
  140. struct list_head *next)
  141. {
  142. struct list_head *first;
  143. struct list_head *last;
  144. if (list_empty(list))
  145. return;
  146. first = list->next;
  147. last = list->prev;
  148. first->prev = prev;
  149. prev->next = first;
  150. last->next = next;
  151. next->prev = last;
  152. }
  153. static inline void
  154. list_splice(const struct list_head *list, struct list_head *head)
  155. {
  156. _list_splice(list, head, head->next);
  157. }
  158. static inline void
  159. list_splice_tail(struct list_head *list, struct list_head *head)
  160. {
  161. _list_splice(list, head->prev, head);
  162. }
  163. static inline void
  164. list_splice_init(struct list_head *list, struct list_head *head)
  165. {
  166. _list_splice(list, head, head->next);
  167. INIT_LIST_HEAD(list);
  168. }
  169. static inline void
  170. list_splice_tail_init(struct list_head *list, struct list_head *head)
  171. {
  172. _list_splice(list, head->prev, head);
  173. INIT_LIST_HEAD(list);
  174. }
  175. #endif /* _LINUX_LIST_H_ */