list.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef __LIST_H
  24. #define __LIST_H
  25. #include <sys/types.h>
  26. #define containerof(ptr, type, member) \
  27. ((type *)((addr_t)(ptr) - offsetof(type, member)))
  28. struct list_node {
  29. struct list_node *prev;
  30. struct list_node *next;
  31. };
  32. #define LIST_INITIAL_VALUE(list) { &(list), &(list) }
  33. static inline void list_initialize(struct list_node *list)
  34. {
  35. list->prev = list->next = list;
  36. }
  37. static inline void list_clear_node(struct list_node *item)
  38. {
  39. item->prev = item->next = 0;
  40. }
  41. static inline bool list_in_list(struct list_node *item)
  42. {
  43. if (item->prev == 0 && item->next == 0)
  44. return false;
  45. else
  46. return true;
  47. }
  48. static inline void list_add_head(struct list_node *list, struct list_node *item)
  49. {
  50. item->next = list->next;
  51. item->prev = list;
  52. list->next->prev = item;
  53. list->next = item;
  54. }
  55. #define list_add_after(entry, new_entry) list_add_head(entry, new_entry)
  56. static inline void list_add_tail(struct list_node *list, struct list_node *item)
  57. {
  58. item->prev = list->prev;
  59. item->next = list;
  60. list->prev->next = item;
  61. list->prev = item;
  62. }
  63. #define list_add_before(entry, new_entry) list_add_tail(entry, new_entry)
  64. static inline void list_delete(struct list_node *item)
  65. {
  66. item->next->prev = item->prev;
  67. item->prev->next = item->next;
  68. item->prev = item->next = 0;
  69. }
  70. static inline struct list_node* list_remove_head(struct list_node *list)
  71. {
  72. if(list->next != list) {
  73. struct list_node *item = list->next;
  74. list_delete(item);
  75. return item;
  76. } else {
  77. return NULL;
  78. }
  79. }
  80. #define list_remove_head_type(list, type, element) ({\
  81. struct list_node *__nod = list_remove_head(list);\
  82. type *__t;\
  83. if(__nod)\
  84. __t = containerof(__nod, type, element);\
  85. else\
  86. __t = (type *)0;\
  87. __t;\
  88. })
  89. static inline struct list_node* list_remove_tail(struct list_node *list)
  90. {
  91. if(list->prev != list) {
  92. struct list_node *item = list->prev;
  93. list_delete(item);
  94. return item;
  95. } else {
  96. return NULL;
  97. }
  98. }
  99. #define list_remove_tail_type(list, type, element) ({\
  100. struct list_node *__nod = list_remove_tail(list);\
  101. type *__t;\
  102. if(__nod)\
  103. __t = containerof(__nod, type, element);\
  104. else\
  105. __t = (type *)0;\
  106. __t;\
  107. })
  108. static inline struct list_node* list_peek_head(struct list_node *list)
  109. {
  110. if(list->next != list) {
  111. return list->next;
  112. } else {
  113. return NULL;
  114. }
  115. }
  116. #define list_peek_head_type(list, type, element) ({\
  117. struct list_node *__nod = list_peek_head(list);\
  118. type *__t;\
  119. if(__nod)\
  120. __t = containerof(__nod, type, element);\
  121. else\
  122. __t = (type *)0;\
  123. __t;\
  124. })
  125. static inline struct list_node* list_peek_tail(struct list_node *list)
  126. {
  127. if(list->prev != list) {
  128. return list->prev;
  129. } else {
  130. return NULL;
  131. }
  132. }
  133. #define list_peek_tail_type(list, type, element) ({\
  134. struct list_node *__nod = list_peek_tail(list);\
  135. type *__t;\
  136. if(__nod)\
  137. __t = containerof(__nod, type, element);\
  138. else\
  139. __t = (type *)0;\
  140. __t;\
  141. })
  142. static inline struct list_node* list_prev(struct list_node *list, struct list_node *item)
  143. {
  144. if(item->prev != list)
  145. return item->prev;
  146. else
  147. return NULL;
  148. }
  149. #define list_prev_type(list, item, type, element) ({\
  150. struct list_node *__nod = list_prev(list, item);\
  151. type *__t;\
  152. if(__nod)\
  153. __t = containerof(__nod, type, element);\
  154. else\
  155. __t = (type *)0;\
  156. __t;\
  157. })
  158. static inline struct list_node* list_prev_wrap(struct list_node *list, struct list_node *item)
  159. {
  160. if(item->prev != list)
  161. return item->prev;
  162. else if(item->prev->prev != list)
  163. return item->prev->prev;
  164. else
  165. return NULL;
  166. }
  167. #define list_prev_wrap_type(list, item, type, element) ({\
  168. struct list_node *__nod = list_prev_wrap(list, item);\
  169. type *__t;\
  170. if(__nod)\
  171. __t = containerof(__nod, type, element);\
  172. else\
  173. __t = (type *)0;\
  174. __t;\
  175. })
  176. static inline struct list_node* list_next(struct list_node *list, struct list_node *item)
  177. {
  178. if(item->next != list)
  179. return item->next;
  180. else
  181. return NULL;
  182. }
  183. #define list_next_type(list, item, type, element) ({\
  184. struct list_node *__nod = list_next(list, item);\
  185. type *__t;\
  186. if(__nod)\
  187. __t = containerof(__nod, type, element);\
  188. else\
  189. __t = (type *)0;\
  190. __t;\
  191. })
  192. static inline struct list_node* list_next_wrap(struct list_node *list, struct list_node *item)
  193. {
  194. if(item->next != list)
  195. return item->next;
  196. else if(item->next->next != list)
  197. return item->next->next;
  198. else
  199. return NULL;
  200. }
  201. #define list_next_wrap_type(list, item, type, element) ({\
  202. struct list_node *__nod = list_next_wrap(list, item);\
  203. type *__t;\
  204. if(__nod)\
  205. __t = containerof(__nod, type, element);\
  206. else\
  207. __t = (type *)0;\
  208. __t;\
  209. })
  210. // iterates over the list, node should be struct list_node*
  211. #define list_for_every(list, node) \
  212. for(node = (list)->next; node != (list); node = node->next)
  213. // iterates over the list in a safe way for deletion of current node
  214. // node and temp_node should be struct list_node*
  215. #define list_for_every_safe(list, node, temp_node) \
  216. for(node = (list)->next, temp_node = (node)->next;\
  217. node != (list);\
  218. node = temp_node, temp_node = (node)->next)
  219. // iterates over the list, entry should be the container structure type *
  220. #define list_for_every_entry(list, entry, type, member) \
  221. for((entry) = containerof((list)->next, type, member);\
  222. &(entry)->member != (list);\
  223. (entry) = containerof((entry)->member.next, type, member))
  224. // iterates over the list in a safe way for deletion of current node
  225. // entry and temp_entry should be the container structure type *
  226. #define list_for_every_entry_safe(list, entry, temp_entry, type, member) \
  227. for(entry = containerof((list)->next, type, member),\
  228. temp_entry = containerof((entry)->member.next, type, member);\
  229. &(entry)->member != (list);\
  230. entry = temp_entry, temp_entry = containerof((temp_entry)->member.next, type, member))
  231. static inline bool list_is_empty(struct list_node *list)
  232. {
  233. return (list->next == list) ? true : false;
  234. }
  235. #endif