2
0

qemu-queue.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* $NetBSD: queue.h,v 1.52 2009/04/20 09:56:08 mschuett Exp $ */
  2. /*
  3. * Qemu version: Copy from netbsd, removed debug code, removed some of
  4. * the implementations. Left in lists, simple queues, tail queues and
  5. * circular queues.
  6. */
  7. /*
  8. * Copyright (c) 1991, 1993
  9. * The Regents of the University of California. All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * @(#)queue.h 8.5 (Berkeley) 8/20/94
  36. */
  37. #ifndef QEMU_SYS_QUEUE_H_
  38. #define QEMU_SYS_QUEUE_H_
  39. /*
  40. * This file defines four types of data structures:
  41. * lists, simple queues, tail queues, and circular queues.
  42. *
  43. * A list is headed by a single forward pointer (or an array of forward
  44. * pointers for a hash table header). The elements are doubly linked
  45. * so that an arbitrary element can be removed without a need to
  46. * traverse the list. New elements can be added to the list before
  47. * or after an existing element or at the head of the list. A list
  48. * may only be traversed in the forward direction.
  49. *
  50. * A simple queue is headed by a pair of pointers, one the head of the
  51. * list and the other to the tail of the list. The elements are singly
  52. * linked to save space, so elements can only be removed from the
  53. * head of the list. New elements can be added to the list after
  54. * an existing element, at the head of the list, or at the end of the
  55. * list. A simple queue may only be traversed in the forward direction.
  56. *
  57. * A tail queue is headed by a pair of pointers, one to the head of the
  58. * list and the other to the tail of the list. The elements are doubly
  59. * linked so that an arbitrary element can be removed without a need to
  60. * traverse the list. New elements can be added to the list before or
  61. * after an existing element, at the head of the list, or at the end of
  62. * the list. A tail queue may be traversed in either direction.
  63. *
  64. * A circle queue is headed by a pair of pointers, one to the head of the
  65. * list and the other to the tail of the list. The elements are doubly
  66. * linked so that an arbitrary element can be removed without a need to
  67. * traverse the list. New elements can be added to the list before or after
  68. * an existing element, at the head of the list, or at the end of the list.
  69. * A circle queue may be traversed in either direction, but has a more
  70. * complex end of list detection.
  71. *
  72. * For details on the use of these macros, see the queue(3) manual page.
  73. */
  74. /*
  75. * List definitions.
  76. */
  77. #define QLIST_HEAD(name, type) \
  78. struct name { \
  79. struct type *lh_first; /* first element */ \
  80. }
  81. #define QLIST_HEAD_INITIALIZER(head) \
  82. { NULL }
  83. #define QLIST_ENTRY(type) \
  84. struct { \
  85. struct type *le_next; /* next element */ \
  86. struct type **le_prev; /* address of previous next element */ \
  87. }
  88. /*
  89. * List functions.
  90. */
  91. #define QLIST_INIT(head) do { \
  92. (head)->lh_first = NULL; \
  93. } while (/*CONSTCOND*/0)
  94. #define QLIST_INSERT_AFTER(listelm, elm, field) do { \
  95. if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
  96. (listelm)->field.le_next->field.le_prev = \
  97. &(elm)->field.le_next; \
  98. (listelm)->field.le_next = (elm); \
  99. (elm)->field.le_prev = &(listelm)->field.le_next; \
  100. } while (/*CONSTCOND*/0)
  101. #define QLIST_INSERT_BEFORE(listelm, elm, field) do { \
  102. (elm)->field.le_prev = (listelm)->field.le_prev; \
  103. (elm)->field.le_next = (listelm); \
  104. *(listelm)->field.le_prev = (elm); \
  105. (listelm)->field.le_prev = &(elm)->field.le_next; \
  106. } while (/*CONSTCOND*/0)
  107. #define QLIST_INSERT_HEAD(head, elm, field) do { \
  108. if (((elm)->field.le_next = (head)->lh_first) != NULL) \
  109. (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
  110. (head)->lh_first = (elm); \
  111. (elm)->field.le_prev = &(head)->lh_first; \
  112. } while (/*CONSTCOND*/0)
  113. #define QLIST_REMOVE(elm, field) do { \
  114. if ((elm)->field.le_next != NULL) \
  115. (elm)->field.le_next->field.le_prev = \
  116. (elm)->field.le_prev; \
  117. *(elm)->field.le_prev = (elm)->field.le_next; \
  118. } while (/*CONSTCOND*/0)
  119. #define QLIST_FOREACH(var, head, field) \
  120. for ((var) = ((head)->lh_first); \
  121. (var); \
  122. (var) = ((var)->field.le_next))
  123. #define QLIST_FOREACH_SAFE(var, head, field, next_var) \
  124. for ((var) = ((head)->lh_first); \
  125. (var) && ((next_var) = ((var)->field.le_next), 1); \
  126. (var) = (next_var))
  127. /*
  128. * List access methods.
  129. */
  130. #define QLIST_EMPTY(head) ((head)->lh_first == NULL)
  131. #define QLIST_FIRST(head) ((head)->lh_first)
  132. #define QLIST_NEXT(elm, field) ((elm)->field.le_next)
  133. /*
  134. * Simple queue definitions.
  135. */
  136. #define QSIMPLEQ_HEAD(name, type) \
  137. struct name { \
  138. struct type *sqh_first; /* first element */ \
  139. struct type **sqh_last; /* addr of last next element */ \
  140. }
  141. #define QSIMPLEQ_HEAD_INITIALIZER(head) \
  142. { NULL, &(head).sqh_first }
  143. #define QSIMPLEQ_ENTRY(type) \
  144. struct { \
  145. struct type *sqe_next; /* next element */ \
  146. }
  147. /*
  148. * Simple queue functions.
  149. */
  150. #define QSIMPLEQ_INIT(head) do { \
  151. (head)->sqh_first = NULL; \
  152. (head)->sqh_last = &(head)->sqh_first; \
  153. } while (/*CONSTCOND*/0)
  154. #define QSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
  155. if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
  156. (head)->sqh_last = &(elm)->field.sqe_next; \
  157. (head)->sqh_first = (elm); \
  158. } while (/*CONSTCOND*/0)
  159. #define QSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
  160. (elm)->field.sqe_next = NULL; \
  161. *(head)->sqh_last = (elm); \
  162. (head)->sqh_last = &(elm)->field.sqe_next; \
  163. } while (/*CONSTCOND*/0)
  164. #define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  165. if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL) \
  166. (head)->sqh_last = &(elm)->field.sqe_next; \
  167. (listelm)->field.sqe_next = (elm); \
  168. } while (/*CONSTCOND*/0)
  169. #define QSIMPLEQ_REMOVE_HEAD(head, field) do { \
  170. if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
  171. (head)->sqh_last = &(head)->sqh_first; \
  172. } while (/*CONSTCOND*/0)
  173. #define QSIMPLEQ_REMOVE(head, elm, type, field) do { \
  174. if ((head)->sqh_first == (elm)) { \
  175. QSIMPLEQ_REMOVE_HEAD((head), field); \
  176. } else { \
  177. struct type *curelm = (head)->sqh_first; \
  178. while (curelm->field.sqe_next != (elm)) \
  179. curelm = curelm->field.sqe_next; \
  180. if ((curelm->field.sqe_next = \
  181. curelm->field.sqe_next->field.sqe_next) == NULL) \
  182. (head)->sqh_last = &(curelm)->field.sqe_next; \
  183. } \
  184. } while (/*CONSTCOND*/0)
  185. #define QSIMPLEQ_FOREACH(var, head, field) \
  186. for ((var) = ((head)->sqh_first); \
  187. (var); \
  188. (var) = ((var)->field.sqe_next))
  189. #define QSIMPLEQ_FOREACH_SAFE(var, head, field, next) \
  190. for ((var) = ((head)->sqh_first); \
  191. (var) && ((next = ((var)->field.sqe_next)), 1); \
  192. (var) = (next))
  193. #define QSIMPLEQ_CONCAT(head1, head2) do { \
  194. if (!QSIMPLEQ_EMPTY((head2))) { \
  195. *(head1)->sqh_last = (head2)->sqh_first; \
  196. (head1)->sqh_last = (head2)->sqh_last; \
  197. QSIMPLEQ_INIT((head2)); \
  198. } \
  199. } while (/*CONSTCOND*/0)
  200. #define QSIMPLEQ_LAST(head, type, field) \
  201. (QSIMPLEQ_EMPTY((head)) ? \
  202. NULL : \
  203. ((struct type *)(void *) \
  204. ((char *)((head)->sqh_last) - offsetof(struct type, field))))
  205. /*
  206. * Simple queue access methods.
  207. */
  208. #define QSIMPLEQ_EMPTY(head) ((head)->sqh_first == NULL)
  209. #define QSIMPLEQ_FIRST(head) ((head)->sqh_first)
  210. #define QSIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
  211. /*
  212. * Tail queue definitions.
  213. */
  214. #define Q_TAILQ_HEAD(name, type, qual) \
  215. struct name { \
  216. qual type *tqh_first; /* first element */ \
  217. qual type *qual *tqh_last; /* addr of last next element */ \
  218. }
  219. #define QTAILQ_HEAD(name, type) Q_TAILQ_HEAD(name, struct type,)
  220. #define QTAILQ_HEAD_INITIALIZER(head) \
  221. { NULL, &(head).tqh_first }
  222. #define Q_TAILQ_ENTRY(type, qual) \
  223. struct { \
  224. qual type *tqe_next; /* next element */ \
  225. qual type *qual *tqe_prev; /* address of previous next element */\
  226. }
  227. #define QTAILQ_ENTRY(type) Q_TAILQ_ENTRY(struct type,)
  228. /*
  229. * Tail queue functions.
  230. */
  231. #define QTAILQ_INIT(head) do { \
  232. (head)->tqh_first = NULL; \
  233. (head)->tqh_last = &(head)->tqh_first; \
  234. } while (/*CONSTCOND*/0)
  235. #define QTAILQ_INSERT_HEAD(head, elm, field) do { \
  236. if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  237. (head)->tqh_first->field.tqe_prev = \
  238. &(elm)->field.tqe_next; \
  239. else \
  240. (head)->tqh_last = &(elm)->field.tqe_next; \
  241. (head)->tqh_first = (elm); \
  242. (elm)->field.tqe_prev = &(head)->tqh_first; \
  243. } while (/*CONSTCOND*/0)
  244. #define QTAILQ_INSERT_TAIL(head, elm, field) do { \
  245. (elm)->field.tqe_next = NULL; \
  246. (elm)->field.tqe_prev = (head)->tqh_last; \
  247. *(head)->tqh_last = (elm); \
  248. (head)->tqh_last = &(elm)->field.tqe_next; \
  249. } while (/*CONSTCOND*/0)
  250. #define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  251. if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  252. (elm)->field.tqe_next->field.tqe_prev = \
  253. &(elm)->field.tqe_next; \
  254. else \
  255. (head)->tqh_last = &(elm)->field.tqe_next; \
  256. (listelm)->field.tqe_next = (elm); \
  257. (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  258. } while (/*CONSTCOND*/0)
  259. #define QTAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  260. (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  261. (elm)->field.tqe_next = (listelm); \
  262. *(listelm)->field.tqe_prev = (elm); \
  263. (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  264. } while (/*CONSTCOND*/0)
  265. #define QTAILQ_REMOVE(head, elm, field) do { \
  266. if (((elm)->field.tqe_next) != NULL) \
  267. (elm)->field.tqe_next->field.tqe_prev = \
  268. (elm)->field.tqe_prev; \
  269. else \
  270. (head)->tqh_last = (elm)->field.tqe_prev; \
  271. *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  272. } while (/*CONSTCOND*/0)
  273. #define QTAILQ_FOREACH(var, head, field) \
  274. for ((var) = ((head)->tqh_first); \
  275. (var); \
  276. (var) = ((var)->field.tqe_next))
  277. #define QTAILQ_FOREACH_SAFE(var, head, field, next_var) \
  278. for ((var) = ((head)->tqh_first); \
  279. (var) && ((next_var) = ((var)->field.tqe_next), 1); \
  280. (var) = (next_var))
  281. #define QTAILQ_FOREACH_REVERSE(var, head, headname, field) \
  282. for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \
  283. (var); \
  284. (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
  285. /*
  286. * Tail queue access methods.
  287. */
  288. #define QTAILQ_EMPTY(head) ((head)->tqh_first == NULL)
  289. #define QTAILQ_FIRST(head) ((head)->tqh_first)
  290. #define QTAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  291. #define QTAILQ_LAST(head, headname) \
  292. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  293. #define QTAILQ_PREV(elm, headname, field) \
  294. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  295. /*
  296. * Circular queue definitions.
  297. */
  298. #define QCIRCLEQ_HEAD(name, type) \
  299. struct name { \
  300. struct type *cqh_first; /* first element */ \
  301. struct type *cqh_last; /* last element */ \
  302. }
  303. #define QCIRCLEQ_HEAD_INITIALIZER(head) \
  304. { (void *)&head, (void *)&head }
  305. #define QCIRCLEQ_ENTRY(type) \
  306. struct { \
  307. struct type *cqe_next; /* next element */ \
  308. struct type *cqe_prev; /* previous element */ \
  309. }
  310. /*
  311. * Circular queue functions.
  312. */
  313. #define QCIRCLEQ_INIT(head) do { \
  314. (head)->cqh_first = (void *)(head); \
  315. (head)->cqh_last = (void *)(head); \
  316. } while (/*CONSTCOND*/0)
  317. #define QCIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  318. (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  319. (elm)->field.cqe_prev = (listelm); \
  320. if ((listelm)->field.cqe_next == (void *)(head)) \
  321. (head)->cqh_last = (elm); \
  322. else \
  323. (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  324. (listelm)->field.cqe_next = (elm); \
  325. } while (/*CONSTCOND*/0)
  326. #define QCIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  327. (elm)->field.cqe_next = (listelm); \
  328. (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  329. if ((listelm)->field.cqe_prev == (void *)(head)) \
  330. (head)->cqh_first = (elm); \
  331. else \
  332. (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  333. (listelm)->field.cqe_prev = (elm); \
  334. } while (/*CONSTCOND*/0)
  335. #define QCIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  336. (elm)->field.cqe_next = (head)->cqh_first; \
  337. (elm)->field.cqe_prev = (void *)(head); \
  338. if ((head)->cqh_last == (void *)(head)) \
  339. (head)->cqh_last = (elm); \
  340. else \
  341. (head)->cqh_first->field.cqe_prev = (elm); \
  342. (head)->cqh_first = (elm); \
  343. } while (/*CONSTCOND*/0)
  344. #define QCIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  345. (elm)->field.cqe_next = (void *)(head); \
  346. (elm)->field.cqe_prev = (head)->cqh_last; \
  347. if ((head)->cqh_first == (void *)(head)) \
  348. (head)->cqh_first = (elm); \
  349. else \
  350. (head)->cqh_last->field.cqe_next = (elm); \
  351. (head)->cqh_last = (elm); \
  352. } while (/*CONSTCOND*/0)
  353. #define QCIRCLEQ_REMOVE(head, elm, field) do { \
  354. if ((elm)->field.cqe_next == (void *)(head)) \
  355. (head)->cqh_last = (elm)->field.cqe_prev; \
  356. else \
  357. (elm)->field.cqe_next->field.cqe_prev = \
  358. (elm)->field.cqe_prev; \
  359. if ((elm)->field.cqe_prev == (void *)(head)) \
  360. (head)->cqh_first = (elm)->field.cqe_next; \
  361. else \
  362. (elm)->field.cqe_prev->field.cqe_next = \
  363. (elm)->field.cqe_next; \
  364. } while (/*CONSTCOND*/0)
  365. #define QCIRCLEQ_FOREACH(var, head, field) \
  366. for ((var) = ((head)->cqh_first); \
  367. (var) != (const void *)(head); \
  368. (var) = ((var)->field.cqe_next))
  369. #define QCIRCLEQ_FOREACH_REVERSE(var, head, field) \
  370. for ((var) = ((head)->cqh_last); \
  371. (var) != (const void *)(head); \
  372. (var) = ((var)->field.cqe_prev))
  373. /*
  374. * Circular queue access methods.
  375. */
  376. #define QCIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
  377. #define QCIRCLEQ_FIRST(head) ((head)->cqh_first)
  378. #define QCIRCLEQ_LAST(head) ((head)->cqh_last)
  379. #define QCIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
  380. #define QCIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
  381. #define QCIRCLEQ_LOOP_NEXT(head, elm, field) \
  382. (((elm)->field.cqe_next == (void *)(head)) \
  383. ? ((head)->cqh_first) \
  384. : (elm->field.cqe_next))
  385. #define QCIRCLEQ_LOOP_PREV(head, elm, field) \
  386. (((elm)->field.cqe_prev == (void *)(head)) \
  387. ? ((head)->cqh_last) \
  388. : (elm->field.cqe_prev))
  389. #endif /* !QEMU_SYS_QUEUE_H_ */