list.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copied from the Linux kernel source tree, version 2.6.0-test1.
  3. *
  4. * Licensed under the GPL v2 as per the whole kernel source tree.
  5. *
  6. */
  7. #ifndef _LIST_H
  8. #define _LIST_H
  9. #ifndef __GNUC__
  10. # error "Need GNU GCC"
  11. #endif
  12. #define typeof __typeof__
  13. #define offsetof(type, member) __builtin_offsetof (type, member)
  14. /**
  15. * container_of - cast a member of a structure out to the containing structure
  16. *
  17. * @ptr: the pointer to the member.
  18. * @type: the type of the container struct this is embedded in.
  19. * @member: the name of the member within the struct.
  20. *
  21. */
  22. #define container_of(ptr, type, member) ({ \
  23. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  24. (type *)( (char *)__mptr - offsetof(type,member) );})
  25. /*
  26. * These are non-NULL pointers that will result in page faults
  27. * under normal circumstances, used to verify that nobody uses
  28. * non-initialized list entries.
  29. */
  30. #define LIST_POISON1 ((void *) 0x00100100)
  31. #define LIST_POISON2 ((void *) 0x00200200)
  32. /*
  33. * Simple doubly linked list implementation.
  34. *
  35. * Some of the internal functions ("__xxx") are useful when
  36. * manipulating whole lists rather than single entries, as
  37. * sometimes we already know the next/prev entries and we can
  38. * generate better code by using them directly rather than
  39. * using the generic single-entry routines.
  40. */
  41. struct list_head {
  42. struct list_head *next, *prev;
  43. };
  44. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  45. #define LIST_HEAD(name) \
  46. struct list_head name = LIST_HEAD_INIT(name)
  47. #define INIT_LIST_HEAD(ptr) do { \
  48. (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  49. } while (0)
  50. /*
  51. * Insert a new entry between two known consecutive entries.
  52. *
  53. * This is only for internal list manipulation where we know
  54. * the prev/next entries already!
  55. */
  56. static inline void __list_add(struct list_head *new,
  57. struct list_head *prev,
  58. struct list_head *next)
  59. {
  60. next->prev = new;
  61. new->next = next;
  62. new->prev = prev;
  63. prev->next = new;
  64. }
  65. /**
  66. * list_add - add a new entry
  67. * @new: new entry to be added
  68. * @head: list head to add it after
  69. *
  70. * Insert a new entry after the specified head.
  71. * This is good for implementing stacks.
  72. */
  73. static inline void list_add(struct list_head *new, struct list_head *head)
  74. {
  75. __list_add(new, head, head->next);
  76. }
  77. /**
  78. * list_add_tail - add a new entry
  79. * @new: new entry to be added
  80. * @head: list head to add it before
  81. *
  82. * Insert a new entry before the specified head.
  83. * This is useful for implementing queues.
  84. */
  85. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  86. {
  87. __list_add(new, head->prev, head);
  88. }
  89. /*
  90. * Delete a list entry by making the prev/next entries
  91. * point to each other.
  92. *
  93. * This is only for internal list manipulation where we know
  94. * the prev/next entries already!
  95. */
  96. static inline void __list_del(struct list_head * prev, struct list_head * next)
  97. {
  98. next->prev = prev;
  99. prev->next = next;
  100. }
  101. /**
  102. * list_del - deletes entry from list.
  103. * @entry: the element to delete from the list.
  104. * Note: list_empty on entry does not return true after this, the entry is
  105. * in an undefined state.
  106. */
  107. static inline void list_del(struct list_head *entry)
  108. {
  109. __list_del(entry->prev, entry->next);
  110. entry->next = LIST_POISON1;
  111. entry->prev = LIST_POISON2;
  112. }
  113. /**
  114. * list_del_init - deletes entry from list and reinitialize it.
  115. * @entry: the element to delete from the list.
  116. */
  117. static inline void list_del_init(struct list_head *entry)
  118. {
  119. __list_del(entry->prev, entry->next);
  120. INIT_LIST_HEAD(entry);
  121. }
  122. /**
  123. * list_move - delete from one list and add as another's head
  124. * @list: the entry to move
  125. * @head: the head that will precede our entry
  126. */
  127. static inline void list_move(struct list_head *list, struct list_head *head)
  128. {
  129. __list_del(list->prev, list->next);
  130. list_add(list, head);
  131. }
  132. /**
  133. * list_move_tail - delete from one list and add as another's tail
  134. * @list: the entry to move
  135. * @head: the head that will follow our entry
  136. */
  137. static inline void list_move_tail(struct list_head *list,
  138. struct list_head *head)
  139. {
  140. __list_del(list->prev, list->next);
  141. list_add_tail(list, head);
  142. }
  143. /**
  144. * list_empty - tests whether a list is empty
  145. * @head: the list to test.
  146. */
  147. static inline int list_empty(const struct list_head *head)
  148. {
  149. return head->next == head;
  150. }
  151. static inline void __list_splice(struct list_head *list,
  152. struct list_head *head)
  153. {
  154. struct list_head *first = list->next;
  155. struct list_head *last = list->prev;
  156. struct list_head *at = head->next;
  157. first->prev = head;
  158. head->next = first;
  159. last->next = at;
  160. at->prev = last;
  161. }
  162. /**
  163. * list_splice - join two lists
  164. * @list: the new list to add.
  165. * @head: the place to add it in the first list.
  166. */
  167. static inline void list_splice(struct list_head *list, struct list_head *head)
  168. {
  169. if (!list_empty(list))
  170. __list_splice(list, head);
  171. }
  172. /**
  173. * list_splice_init - join two lists and reinitialise the emptied list.
  174. * @list: the new list to add.
  175. * @head: the place to add it in the first list.
  176. *
  177. * The list at @list is reinitialised
  178. */
  179. static inline void list_splice_init(struct list_head *list,
  180. struct list_head *head)
  181. {
  182. if (!list_empty(list)) {
  183. __list_splice(list, head);
  184. INIT_LIST_HEAD(list);
  185. }
  186. }
  187. /**
  188. * list_entry - get the struct for this entry
  189. * @ptr: the &struct list_head pointer.
  190. * @type: the type of the struct this is embedded in.
  191. * @member: the name of the list_struct within the struct.
  192. */
  193. #define list_entry(ptr, type, member) \
  194. container_of(ptr, type, member)
  195. /**
  196. * list_for_each - iterate over a list
  197. * @pos: the &struct list_head to use as a loop counter.
  198. * @head: the head for your list.
  199. */
  200. #define list_for_each(pos, head) \
  201. for (pos = (head)->next; pos != (head); \
  202. pos = pos->next)
  203. /**
  204. * __list_for_each - iterate over a list
  205. * @pos: the &struct list_head to use as a loop counter.
  206. * @head: the head for your list.
  207. *
  208. * This variant differs from list_for_each() in that it's the
  209. * simplest possible list iteration code.
  210. * Use this for code that knows the list to be very short (empty
  211. * or 1 entry) most of the time.
  212. */
  213. #define __list_for_each(pos, head) \
  214. for (pos = (head)->next; pos != (head); pos = pos->next)
  215. /**
  216. * list_for_each_prev - iterate over a list backwards
  217. * @pos: the &struct list_head to use as a loop counter.
  218. * @head: the head for your list.
  219. */
  220. #define list_for_each_prev(pos, head) \
  221. for (pos = (head)->prev; pos != (head); pos = pos->prev)
  222. /**
  223. * list_for_each_safe - iterate over a list safe against removal of list entry
  224. * @pos: the &struct list_head to use as a loop counter.
  225. * @n: another &struct list_head to use as temporary storage
  226. * @head: the head for your list.
  227. */
  228. #define list_for_each_safe(pos, n, head) \
  229. for (pos = (head)->next, n = pos->next; pos != (head); \
  230. pos = n, n = pos->next)
  231. /**
  232. * list_for_each_entry - iterate over list of given type
  233. * @pos: the type * to use as a loop counter.
  234. * @head: the head for your list.
  235. * @member: the name of the list_struct within the struct.
  236. */
  237. #define list_for_each_entry(pos, head, member) \
  238. for (pos = list_entry((head)->next, typeof(*pos), member); \
  239. &pos->member != (head); \
  240. pos = list_entry(pos->member.next, typeof(*pos), member))
  241. /**
  242. * list_for_each_entry_reverse - iterate backwards over list of given type.
  243. * @pos: the type * to use as a loop counter.
  244. * @head: the head for your list.
  245. * @member: the name of the list_struct within the struct.
  246. */
  247. #define list_for_each_entry_reverse(pos, head, member) \
  248. for (pos = list_entry((head)->prev, typeof(*pos), member); \
  249. &pos->member != (head); \
  250. pos = list_entry(pos->member.prev, typeof(*pos), member))
  251. /**
  252. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  253. * @pos: the type * to use as a loop counter.
  254. * @n: another type * to use as temporary storage
  255. * @head: the head for your list.
  256. * @member: the name of the list_struct within the struct.
  257. */
  258. #define list_for_each_entry_safe(pos, n, head, member) \
  259. for (pos = list_entry((head)->next, typeof(*pos), member), \
  260. n = list_entry(pos->member.next, typeof(*pos), member); \
  261. &pos->member != (head); \
  262. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  263. #endif /* _LIST_H */