2
0

sys-queue.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* $NetBSD: queue.h,v 1.45.14.1 2007/07/18 20:13:24 liamjfoy Exp $ */
  2. /*
  3. * Qemu version: Copy from netbsd, removed debug code, removed some of
  4. * the implementations. Left in lists, tail queues and circular queues.
  5. */
  6. /*
  7. * Copyright (c) 1991, 1993
  8. * The Regents of the University of California. All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * @(#)queue.h 8.5 (Berkeley) 8/20/94
  35. */
  36. #ifndef _SYS_QUEUE_H_
  37. #define _SYS_QUEUE_H_
  38. /*
  39. * This file defines three types of data structures:
  40. * lists, tail queues, and circular queues.
  41. *
  42. * A list is headed by a single forward pointer (or an array of forward
  43. * pointers for a hash table header). The elements are doubly linked
  44. * so that an arbitrary element can be removed without a need to
  45. * traverse the list. New elements can be added to the list before
  46. * or after an existing element or at the head of the list. A list
  47. * may only be traversed in the forward direction.
  48. *
  49. * A tail queue is headed by a pair of pointers, one to the head of the
  50. * list and the other to the tail of the list. The elements are doubly
  51. * linked so that an arbitrary element can be removed without a need to
  52. * traverse the list. New elements can be added to the list before or
  53. * after an existing element, at the head of the list, or at the end of
  54. * the list. A tail queue may be traversed in either direction.
  55. *
  56. * A circle queue is headed by a pair of pointers, one to the head of the
  57. * list and the other to the tail of the list. The elements are doubly
  58. * linked so that an arbitrary element can be removed without a need to
  59. * traverse the list. New elements can be added to the list before or after
  60. * an existing element, at the head of the list, or at the end of the list.
  61. * A circle queue may be traversed in either direction, but has a more
  62. * complex end of list detection.
  63. *
  64. * For details on the use of these macros, see the queue(3) manual page.
  65. */
  66. /*
  67. * List definitions.
  68. */
  69. #define LIST_HEAD(name, type) \
  70. struct name { \
  71. struct type *lh_first; /* first element */ \
  72. }
  73. #define LIST_HEAD_INITIALIZER(head) \
  74. { NULL }
  75. #define LIST_ENTRY(type) \
  76. struct { \
  77. struct type *le_next; /* next element */ \
  78. struct type **le_prev; /* address of previous next element */ \
  79. }
  80. /*
  81. * List functions.
  82. */
  83. #define LIST_INIT(head) do { \
  84. (head)->lh_first = NULL; \
  85. } while (/*CONSTCOND*/0)
  86. #define LIST_INSERT_AFTER(listelm, elm, field) do { \
  87. if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
  88. (listelm)->field.le_next->field.le_prev = \
  89. &(elm)->field.le_next; \
  90. (listelm)->field.le_next = (elm); \
  91. (elm)->field.le_prev = &(listelm)->field.le_next; \
  92. } while (/*CONSTCOND*/0)
  93. #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
  94. (elm)->field.le_prev = (listelm)->field.le_prev; \
  95. (elm)->field.le_next = (listelm); \
  96. *(listelm)->field.le_prev = (elm); \
  97. (listelm)->field.le_prev = &(elm)->field.le_next; \
  98. } while (/*CONSTCOND*/0)
  99. #define LIST_INSERT_HEAD(head, elm, field) do { \
  100. if (((elm)->field.le_next = (head)->lh_first) != NULL) \
  101. (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
  102. (head)->lh_first = (elm); \
  103. (elm)->field.le_prev = &(head)->lh_first; \
  104. } while (/*CONSTCOND*/0)
  105. #define LIST_REMOVE(elm, field) do { \
  106. if ((elm)->field.le_next != NULL) \
  107. (elm)->field.le_next->field.le_prev = \
  108. (elm)->field.le_prev; \
  109. *(elm)->field.le_prev = (elm)->field.le_next; \
  110. } while (/*CONSTCOND*/0)
  111. #define LIST_FOREACH(var, head, field) \
  112. for ((var) = ((head)->lh_first); \
  113. (var); \
  114. (var) = ((var)->field.le_next))
  115. /*
  116. * List access methods.
  117. */
  118. #define LIST_EMPTY(head) ((head)->lh_first == NULL)
  119. #define LIST_FIRST(head) ((head)->lh_first)
  120. #define LIST_NEXT(elm, field) ((elm)->field.le_next)
  121. /*
  122. * Tail queue definitions.
  123. */
  124. #define _TAILQ_HEAD(name, type, qual) \
  125. struct name { \
  126. qual type *tqh_first; /* first element */ \
  127. qual type *qual *tqh_last; /* addr of last next element */ \
  128. }
  129. #define TAILQ_HEAD(name, type) _TAILQ_HEAD(name, struct type,)
  130. #define TAILQ_HEAD_INITIALIZER(head) \
  131. { NULL, &(head).tqh_first }
  132. #define _TAILQ_ENTRY(type, qual) \
  133. struct { \
  134. qual type *tqe_next; /* next element */ \
  135. qual type *qual *tqe_prev; /* address of previous next element */\
  136. }
  137. #define TAILQ_ENTRY(type) _TAILQ_ENTRY(struct type,)
  138. /*
  139. * Tail queue functions.
  140. */
  141. #define TAILQ_INIT(head) do { \
  142. (head)->tqh_first = NULL; \
  143. (head)->tqh_last = &(head)->tqh_first; \
  144. } while (/*CONSTCOND*/0)
  145. #define TAILQ_INSERT_HEAD(head, elm, field) do { \
  146. if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  147. (head)->tqh_first->field.tqe_prev = \
  148. &(elm)->field.tqe_next; \
  149. else \
  150. (head)->tqh_last = &(elm)->field.tqe_next; \
  151. (head)->tqh_first = (elm); \
  152. (elm)->field.tqe_prev = &(head)->tqh_first; \
  153. } while (/*CONSTCOND*/0)
  154. #define TAILQ_INSERT_TAIL(head, elm, field) do { \
  155. (elm)->field.tqe_next = NULL; \
  156. (elm)->field.tqe_prev = (head)->tqh_last; \
  157. *(head)->tqh_last = (elm); \
  158. (head)->tqh_last = &(elm)->field.tqe_next; \
  159. } while (/*CONSTCOND*/0)
  160. #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  161. if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  162. (elm)->field.tqe_next->field.tqe_prev = \
  163. &(elm)->field.tqe_next; \
  164. else \
  165. (head)->tqh_last = &(elm)->field.tqe_next; \
  166. (listelm)->field.tqe_next = (elm); \
  167. (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  168. } while (/*CONSTCOND*/0)
  169. #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  170. (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  171. (elm)->field.tqe_next = (listelm); \
  172. *(listelm)->field.tqe_prev = (elm); \
  173. (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  174. } while (/*CONSTCOND*/0)
  175. #define TAILQ_REMOVE(head, elm, field) do { \
  176. if (((elm)->field.tqe_next) != NULL) \
  177. (elm)->field.tqe_next->field.tqe_prev = \
  178. (elm)->field.tqe_prev; \
  179. else \
  180. (head)->tqh_last = (elm)->field.tqe_prev; \
  181. *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  182. } while (/*CONSTCOND*/0)
  183. #define TAILQ_FOREACH(var, head, field) \
  184. for ((var) = ((head)->tqh_first); \
  185. (var); \
  186. (var) = ((var)->field.tqe_next))
  187. #define TAILQ_FOREACH_SAFE(var, head, field, next_var) \
  188. for ((var) = ((head)->tqh_first); \
  189. (var) && ((next_var) = ((var)->field.tqe_next), 1); \
  190. (var) = (next_var))
  191. #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  192. for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \
  193. (var); \
  194. (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
  195. /*
  196. * Tail queue access methods.
  197. */
  198. #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
  199. #define TAILQ_FIRST(head) ((head)->tqh_first)
  200. #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  201. #define TAILQ_LAST(head, headname) \
  202. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  203. #define TAILQ_PREV(elm, headname, field) \
  204. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  205. /*
  206. * Circular queue definitions.
  207. */
  208. #define CIRCLEQ_HEAD(name, type) \
  209. struct name { \
  210. struct type *cqh_first; /* first element */ \
  211. struct type *cqh_last; /* last element */ \
  212. }
  213. #define CIRCLEQ_HEAD_INITIALIZER(head) \
  214. { (void *)&head, (void *)&head }
  215. #define CIRCLEQ_ENTRY(type) \
  216. struct { \
  217. struct type *cqe_next; /* next element */ \
  218. struct type *cqe_prev; /* previous element */ \
  219. }
  220. /*
  221. * Circular queue functions.
  222. */
  223. #define CIRCLEQ_INIT(head) do { \
  224. (head)->cqh_first = (void *)(head); \
  225. (head)->cqh_last = (void *)(head); \
  226. } while (/*CONSTCOND*/0)
  227. #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  228. (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  229. (elm)->field.cqe_prev = (listelm); \
  230. if ((listelm)->field.cqe_next == (void *)(head)) \
  231. (head)->cqh_last = (elm); \
  232. else \
  233. (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  234. (listelm)->field.cqe_next = (elm); \
  235. } while (/*CONSTCOND*/0)
  236. #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  237. (elm)->field.cqe_next = (listelm); \
  238. (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  239. if ((listelm)->field.cqe_prev == (void *)(head)) \
  240. (head)->cqh_first = (elm); \
  241. else \
  242. (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  243. (listelm)->field.cqe_prev = (elm); \
  244. } while (/*CONSTCOND*/0)
  245. #define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  246. (elm)->field.cqe_next = (head)->cqh_first; \
  247. (elm)->field.cqe_prev = (void *)(head); \
  248. if ((head)->cqh_last == (void *)(head)) \
  249. (head)->cqh_last = (elm); \
  250. else \
  251. (head)->cqh_first->field.cqe_prev = (elm); \
  252. (head)->cqh_first = (elm); \
  253. } while (/*CONSTCOND*/0)
  254. #define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  255. (elm)->field.cqe_next = (void *)(head); \
  256. (elm)->field.cqe_prev = (head)->cqh_last; \
  257. if ((head)->cqh_first == (void *)(head)) \
  258. (head)->cqh_first = (elm); \
  259. else \
  260. (head)->cqh_last->field.cqe_next = (elm); \
  261. (head)->cqh_last = (elm); \
  262. } while (/*CONSTCOND*/0)
  263. #define CIRCLEQ_REMOVE(head, elm, field) do { \
  264. if ((elm)->field.cqe_next == (void *)(head)) \
  265. (head)->cqh_last = (elm)->field.cqe_prev; \
  266. else \
  267. (elm)->field.cqe_next->field.cqe_prev = \
  268. (elm)->field.cqe_prev; \
  269. if ((elm)->field.cqe_prev == (void *)(head)) \
  270. (head)->cqh_first = (elm)->field.cqe_next; \
  271. else \
  272. (elm)->field.cqe_prev->field.cqe_next = \
  273. (elm)->field.cqe_next; \
  274. } while (/*CONSTCOND*/0)
  275. #define CIRCLEQ_FOREACH(var, head, field) \
  276. for ((var) = ((head)->cqh_first); \
  277. (var) != (const void *)(head); \
  278. (var) = ((var)->field.cqe_next))
  279. #define CIRCLEQ_FOREACH_REVERSE(var, head, field) \
  280. for ((var) = ((head)->cqh_last); \
  281. (var) != (const void *)(head); \
  282. (var) = ((var)->field.cqe_prev))
  283. /*
  284. * Circular queue access methods.
  285. */
  286. #define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
  287. #define CIRCLEQ_FIRST(head) ((head)->cqh_first)
  288. #define CIRCLEQ_LAST(head) ((head)->cqh_last)
  289. #define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
  290. #define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
  291. #define CIRCLEQ_LOOP_NEXT(head, elm, field) \
  292. (((elm)->field.cqe_next == (void *)(head)) \
  293. ? ((head)->cqh_first) \
  294. : (elm->field.cqe_next))
  295. #define CIRCLEQ_LOOP_PREV(head, elm, field) \
  296. (((elm)->field.cqe_prev == (void *)(head)) \
  297. ? ((head)->cqh_last) \
  298. : (elm->field.cqe_prev))
  299. #endif /* !_SYS_QUEUE_H_ */