list.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* list.h - the opkg package management system
  2. Tick Chen <tick@openmoko.com>
  3. Copyright (C) 2008 Openmoko Inc.
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 2, or (at
  7. your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. This is modified from Linux Kernel.
  13. */
  14. #ifndef _LINUX_LIST_H
  15. #define _LINUX_LIST_H
  16. struct list_head {
  17. struct list_head *next, *prev;
  18. };
  19. #define LIST_POISON1 ((struct list_head *) 0x00100100)
  20. #define LIST_POISON2 ((struct list_head *) 0x00200200)
  21. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  22. #define LIST_HEAD(name) \
  23. struct list_head name = LIST_HEAD_INIT(name)
  24. #define INIT_LIST_HEAD(ptr) do { \
  25. (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  26. } while (0)
  27. static inline void __list_add(struct list_head *newitem,
  28. struct list_head *prev, struct list_head *next)
  29. {
  30. next->prev = newitem;
  31. newitem->next = next;
  32. newitem->prev = prev;
  33. prev->next = newitem;
  34. }
  35. /**
  36. * list_add - add a new entry
  37. * @newitem: new entry to be added
  38. * @head: list head to add it after
  39. *
  40. * Insert a new entry after the specified head.
  41. * This is good for implementing stacks.
  42. */
  43. static inline void list_add(struct list_head *newitem, struct list_head *head)
  44. {
  45. __list_add(newitem, head, head->next);
  46. }
  47. /**
  48. * list_add_tail - add a new entry
  49. * @newitem: new entry to be added
  50. * @head: list head to add it before
  51. *
  52. * Insert a new entry before the specified head.
  53. * This is useful for implementing queues.
  54. */
  55. static inline void list_add_tail(struct list_head *newitem,
  56. struct list_head *head)
  57. {
  58. __list_add(newitem, head->prev, head);
  59. }
  60. /*
  61. * Delete a list entry by making the prev/next entries
  62. * point to each other.
  63. *
  64. * This is only for internal list manipulation where we know
  65. * the prev/next entries already!
  66. */
  67. static inline void __list_del(struct list_head *prev, struct list_head *next)
  68. {
  69. next->prev = prev;
  70. prev->next = next;
  71. }
  72. /**
  73. * list_del - deletes entry from list.
  74. * @entry: the element to delete from the list.
  75. * Note: list_empty on entry does not return true after this, the entry is
  76. * in an undefined state.
  77. */
  78. static inline void list_del(struct list_head *entry)
  79. {
  80. __list_del(entry->prev, entry->next);
  81. entry->next = LIST_POISON1;
  82. entry->prev = LIST_POISON2;
  83. }
  84. /**
  85. * list_del_init - deletes entry from list and reinitialize it.
  86. * @entry: the element to delete from the list.
  87. */
  88. static inline void list_del_init(struct list_head *entry)
  89. {
  90. __list_del(entry->prev, entry->next);
  91. INIT_LIST_HEAD(entry);
  92. }
  93. /**
  94. * list_move - delete from one list and add as another's head
  95. * @list: the entry to move
  96. * @head: the head that will precede our entry
  97. */
  98. static inline void list_move(struct list_head *list, struct list_head *head)
  99. {
  100. __list_del(list->prev, list->next);
  101. list_add(list, head);
  102. }
  103. /**
  104. * list_move_tail - delete from one list and add as another's tail
  105. * @list: the entry to move
  106. * @head: the head that will follow our entry
  107. */
  108. static inline void list_move_tail(struct list_head *list,
  109. struct list_head *head)
  110. {
  111. __list_del(list->prev, list->next);
  112. list_add_tail(list, head);
  113. }
  114. /**
  115. * list_empty - tests whether a list is empty
  116. * @head: the list to test.
  117. */
  118. static inline int list_empty(const struct list_head *head)
  119. {
  120. return head->next == head;
  121. }
  122. /**
  123. * list_empty_careful - tests whether a list is
  124. * empty _and_ checks that no other CPU might be
  125. * in the process of still modifying either member
  126. *
  127. * NOTE: using list_empty_careful() without synchronization
  128. * can only be safe if the only activity that can happen
  129. * to the list entry is list_del_init(). Eg. it cannot be used
  130. * if another CPU could re-list_add() it.
  131. *
  132. * @head: the list to test.
  133. */
  134. static inline int list_empty_careful(const struct list_head *head)
  135. {
  136. struct list_head *next = head->next;
  137. return (next == head) && (next == head->prev);
  138. }
  139. static inline void __list_splice(struct list_head *list, struct list_head *head)
  140. {
  141. struct list_head *first = list->next;
  142. struct list_head *last = list->prev;
  143. struct list_head *at = head->next;
  144. first->prev = head;
  145. head->next = first;
  146. last->next = at;
  147. at->prev = last;
  148. }
  149. /**
  150. * list_splice - join two lists
  151. * @list: the new list to add.
  152. * @head: the place to add it in the first list.
  153. */
  154. static inline void list_splice(struct list_head *list, struct list_head *head)
  155. {
  156. if (!list_empty(list))
  157. __list_splice(list, head);
  158. }
  159. /**
  160. * list_splice_init - join two lists and reinitialise the emptied list.
  161. * @list: the new list to add.
  162. * @head: the place to add it in the first list.
  163. *
  164. * The list at @list is reinitialised
  165. */
  166. static inline void list_splice_init(struct list_head *list,
  167. struct list_head *head)
  168. {
  169. if (!list_empty(list)) {
  170. __list_splice(list, head);
  171. INIT_LIST_HEAD(list);
  172. }
  173. }
  174. #define _offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  175. #define container_of(ptr, type, member) ({ \
  176. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  177. (type *)( (char *)__mptr - _offsetof(type,member) );})
  178. /**
  179. * list_entry - get the struct for this entry
  180. * @ptr: the &struct list_head pointer.
  181. * @type: the type of the struct this is embedded in.
  182. * @member: the name of the list_struct within the struct.
  183. */
  184. #define list_entry(ptr, type, member) \
  185. container_of(ptr, type, member)
  186. /**
  187. * list_for_each - iterate over a list
  188. * @pos: the &struct list_head to use as a loop counter.
  189. * @head: the head for your list.
  190. */
  191. #define list_for_each(pos, head) \
  192. for (pos = (head)->next; pos != (head); \
  193. pos = pos->next)
  194. /**
  195. * __list_for_each - iterate over a list
  196. * @pos: the &struct list_head to use as a loop counter.
  197. * @head: the head for your list.
  198. *
  199. * This variant differs from list_for_each() in that it's the
  200. * simplest possible list iteration code, no prefetching is done.
  201. * Use this for code that knows the list to be very short (empty
  202. * or 1 entry) most of the time.
  203. */
  204. #define __list_for_each(pos, head) \
  205. for (pos = (head)->next; pos != (head); pos = pos->next)
  206. /**
  207. * list_for_each_prev - iterate over a list backwards
  208. * @pos: the &struct list_head to use as a loop counter.
  209. * @head: the head for your list.
  210. */
  211. #define list_for_each_prev(pos, head) \
  212. for (pos = (head)->prev; pos != (head); \
  213. pos = pos->prev)
  214. /**
  215. * list_for_each_safe - iterate over a list safe against removal of list entry
  216. * @pos: the &struct list_head to use as a loop counter.
  217. * @n: another &struct list_head to use as temporary storage
  218. * @head: the head for your list.
  219. */
  220. #define list_for_each_safe(pos, n, head) \
  221. for (pos = (head)->next, n = pos->next; pos != (head); \
  222. pos = n, n = pos->next)
  223. /**
  224. * list_for_each_entry - iterate over list of given type
  225. * @pos: the type * to use as a loop counter.
  226. * @head: the head for your list.
  227. * @member: the name of the list_struct within the struct.
  228. */
  229. #define list_for_each_entry(pos, head, member) \
  230. for (pos = list_entry((head)->next, typeof(*pos), member); \
  231. &pos->member != (head); \
  232. pos = list_entry(pos->member.next, typeof(*pos), member))
  233. /**
  234. * list_for_each_entry_reverse - iterate backwards over list of given type.
  235. * @pos: the type * to use as a loop counter.
  236. * @head: the head for your list.
  237. * @member: the name of the list_struct within the struct.
  238. */
  239. #define list_for_each_entry_reverse(pos, head, member) \
  240. for (pos = list_entry((head)->prev, typeof(*pos), member); \
  241. &pos->member != (head); \
  242. pos = list_entry(pos->member.prev, typeof(*pos), member))
  243. /**
  244. * list_prepare_entry - prepare a pos entry for use as a start point in
  245. * list_for_each_entry_continue
  246. * @pos: the type * to use as a start point
  247. * @head: the head of the list
  248. * @member: the name of the list_struct within the struct.
  249. */
  250. #define list_prepare_entry(pos, head, member) \
  251. ((pos) ? : list_entry(head, typeof(*pos), member))
  252. /**
  253. * list_for_each_entry_continue - iterate over list of given type
  254. * continuing after existing point
  255. * @pos: the type * to use as a loop counter.
  256. * @head: the head for your list.
  257. * @member: the name of the list_struct within the struct.
  258. */
  259. #define list_for_each_entry_continue(pos, head, member) \
  260. for (pos = list_entry(pos->member.next, typeof(*pos), member); \
  261. &pos->member != (head); \
  262. pos = list_entry(pos->member.next, typeof(*pos), member))
  263. /**
  264. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  265. * @pos: the type * to use as a loop counter.
  266. * @n: another type * to use as temporary storage
  267. * @head: the head for your list.
  268. * @member: the name of the list_struct within the struct.
  269. */
  270. #define list_for_each_entry_safe(pos, n, head, member) \
  271. for (pos = list_entry((head)->next, typeof(*pos), member), \
  272. n = list_entry(pos->member.next, typeof(*pos), member); \
  273. &pos->member != (head); \
  274. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  275. #endif