qemu-queue.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. #include "qemu-barrier.h" /* for smp_wmb() */
  75. /*
  76. * List definitions.
  77. */
  78. #define QLIST_HEAD(name, type) \
  79. struct name { \
  80. struct type *lh_first; /* first element */ \
  81. }
  82. #define QLIST_HEAD_INITIALIZER(head) \
  83. { NULL }
  84. #define QLIST_ENTRY(type) \
  85. struct { \
  86. struct type *le_next; /* next element */ \
  87. struct type **le_prev; /* address of previous next element */ \
  88. }
  89. /*
  90. * List functions.
  91. */
  92. #define QLIST_INIT(head) do { \
  93. (head)->lh_first = NULL; \
  94. } while (/*CONSTCOND*/0)
  95. #define QLIST_INSERT_AFTER(listelm, elm, field) do { \
  96. if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
  97. (listelm)->field.le_next->field.le_prev = \
  98. &(elm)->field.le_next; \
  99. (listelm)->field.le_next = (elm); \
  100. (elm)->field.le_prev = &(listelm)->field.le_next; \
  101. } while (/*CONSTCOND*/0)
  102. #define QLIST_INSERT_BEFORE(listelm, elm, field) do { \
  103. (elm)->field.le_prev = (listelm)->field.le_prev; \
  104. (elm)->field.le_next = (listelm); \
  105. *(listelm)->field.le_prev = (elm); \
  106. (listelm)->field.le_prev = &(elm)->field.le_next; \
  107. } while (/*CONSTCOND*/0)
  108. #define QLIST_INSERT_HEAD(head, elm, field) do { \
  109. if (((elm)->field.le_next = (head)->lh_first) != NULL) \
  110. (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
  111. (head)->lh_first = (elm); \
  112. (elm)->field.le_prev = &(head)->lh_first; \
  113. } while (/*CONSTCOND*/0)
  114. #define QLIST_INSERT_HEAD_RCU(head, elm, field) do { \
  115. (elm)->field.le_prev = &(head)->lh_first; \
  116. (elm)->field.le_next = (head)->lh_first; \
  117. smp_wmb(); /* fill elm before linking it */ \
  118. if ((head)->lh_first != NULL) { \
  119. (head)->lh_first->field.le_prev = &(elm)->field.le_next; \
  120. } \
  121. (head)->lh_first = (elm); \
  122. smp_wmb(); \
  123. } while (/* CONSTCOND*/0)
  124. #define QLIST_REMOVE(elm, field) do { \
  125. if ((elm)->field.le_next != NULL) \
  126. (elm)->field.le_next->field.le_prev = \
  127. (elm)->field.le_prev; \
  128. *(elm)->field.le_prev = (elm)->field.le_next; \
  129. } while (/*CONSTCOND*/0)
  130. #define QLIST_FOREACH(var, head, field) \
  131. for ((var) = ((head)->lh_first); \
  132. (var); \
  133. (var) = ((var)->field.le_next))
  134. #define QLIST_FOREACH_SAFE(var, head, field, next_var) \
  135. for ((var) = ((head)->lh_first); \
  136. (var) && ((next_var) = ((var)->field.le_next), 1); \
  137. (var) = (next_var))
  138. /*
  139. * List access methods.
  140. */
  141. #define QLIST_EMPTY(head) ((head)->lh_first == NULL)
  142. #define QLIST_FIRST(head) ((head)->lh_first)
  143. #define QLIST_NEXT(elm, field) ((elm)->field.le_next)
  144. /*
  145. * Simple queue definitions.
  146. */
  147. #define QSIMPLEQ_HEAD(name, type) \
  148. struct name { \
  149. struct type *sqh_first; /* first element */ \
  150. struct type **sqh_last; /* addr of last next element */ \
  151. }
  152. #define QSIMPLEQ_HEAD_INITIALIZER(head) \
  153. { NULL, &(head).sqh_first }
  154. #define QSIMPLEQ_ENTRY(type) \
  155. struct { \
  156. struct type *sqe_next; /* next element */ \
  157. }
  158. /*
  159. * Simple queue functions.
  160. */
  161. #define QSIMPLEQ_INIT(head) do { \
  162. (head)->sqh_first = NULL; \
  163. (head)->sqh_last = &(head)->sqh_first; \
  164. } while (/*CONSTCOND*/0)
  165. #define QSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
  166. if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
  167. (head)->sqh_last = &(elm)->field.sqe_next; \
  168. (head)->sqh_first = (elm); \
  169. } while (/*CONSTCOND*/0)
  170. #define QSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
  171. (elm)->field.sqe_next = NULL; \
  172. *(head)->sqh_last = (elm); \
  173. (head)->sqh_last = &(elm)->field.sqe_next; \
  174. } while (/*CONSTCOND*/0)
  175. #define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  176. if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL) \
  177. (head)->sqh_last = &(elm)->field.sqe_next; \
  178. (listelm)->field.sqe_next = (elm); \
  179. } while (/*CONSTCOND*/0)
  180. #define QSIMPLEQ_REMOVE_HEAD(head, field) do { \
  181. if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
  182. (head)->sqh_last = &(head)->sqh_first; \
  183. } while (/*CONSTCOND*/0)
  184. #define QSIMPLEQ_REMOVE(head, elm, type, field) do { \
  185. if ((head)->sqh_first == (elm)) { \
  186. QSIMPLEQ_REMOVE_HEAD((head), field); \
  187. } else { \
  188. struct type *curelm = (head)->sqh_first; \
  189. while (curelm->field.sqe_next != (elm)) \
  190. curelm = curelm->field.sqe_next; \
  191. if ((curelm->field.sqe_next = \
  192. curelm->field.sqe_next->field.sqe_next) == NULL) \
  193. (head)->sqh_last = &(curelm)->field.sqe_next; \
  194. } \
  195. } while (/*CONSTCOND*/0)
  196. #define QSIMPLEQ_FOREACH(var, head, field) \
  197. for ((var) = ((head)->sqh_first); \
  198. (var); \
  199. (var) = ((var)->field.sqe_next))
  200. #define QSIMPLEQ_FOREACH_SAFE(var, head, field, next) \
  201. for ((var) = ((head)->sqh_first); \
  202. (var) && ((next = ((var)->field.sqe_next)), 1); \
  203. (var) = (next))
  204. #define QSIMPLEQ_CONCAT(head1, head2) do { \
  205. if (!QSIMPLEQ_EMPTY((head2))) { \
  206. *(head1)->sqh_last = (head2)->sqh_first; \
  207. (head1)->sqh_last = (head2)->sqh_last; \
  208. QSIMPLEQ_INIT((head2)); \
  209. } \
  210. } while (/*CONSTCOND*/0)
  211. #define QSIMPLEQ_LAST(head, type, field) \
  212. (QSIMPLEQ_EMPTY((head)) ? \
  213. NULL : \
  214. ((struct type *)(void *) \
  215. ((char *)((head)->sqh_last) - offsetof(struct type, field))))
  216. /*
  217. * Simple queue access methods.
  218. */
  219. #define QSIMPLEQ_EMPTY(head) ((head)->sqh_first == NULL)
  220. #define QSIMPLEQ_FIRST(head) ((head)->sqh_first)
  221. #define QSIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
  222. /*
  223. * Tail queue definitions.
  224. */
  225. #define Q_TAILQ_HEAD(name, type, qual) \
  226. struct name { \
  227. qual type *tqh_first; /* first element */ \
  228. qual type *qual *tqh_last; /* addr of last next element */ \
  229. }
  230. #define QTAILQ_HEAD(name, type) Q_TAILQ_HEAD(name, struct type,)
  231. #define QTAILQ_HEAD_INITIALIZER(head) \
  232. { NULL, &(head).tqh_first }
  233. #define Q_TAILQ_ENTRY(type, qual) \
  234. struct { \
  235. qual type *tqe_next; /* next element */ \
  236. qual type *qual *tqe_prev; /* address of previous next element */\
  237. }
  238. #define QTAILQ_ENTRY(type) Q_TAILQ_ENTRY(struct type,)
  239. /*
  240. * Tail queue functions.
  241. */
  242. #define QTAILQ_INIT(head) do { \
  243. (head)->tqh_first = NULL; \
  244. (head)->tqh_last = &(head)->tqh_first; \
  245. } while (/*CONSTCOND*/0)
  246. #define QTAILQ_INSERT_HEAD(head, elm, field) do { \
  247. if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  248. (head)->tqh_first->field.tqe_prev = \
  249. &(elm)->field.tqe_next; \
  250. else \
  251. (head)->tqh_last = &(elm)->field.tqe_next; \
  252. (head)->tqh_first = (elm); \
  253. (elm)->field.tqe_prev = &(head)->tqh_first; \
  254. } while (/*CONSTCOND*/0)
  255. #define QTAILQ_INSERT_TAIL(head, elm, field) do { \
  256. (elm)->field.tqe_next = NULL; \
  257. (elm)->field.tqe_prev = (head)->tqh_last; \
  258. *(head)->tqh_last = (elm); \
  259. (head)->tqh_last = &(elm)->field.tqe_next; \
  260. } while (/*CONSTCOND*/0)
  261. #define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  262. if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  263. (elm)->field.tqe_next->field.tqe_prev = \
  264. &(elm)->field.tqe_next; \
  265. else \
  266. (head)->tqh_last = &(elm)->field.tqe_next; \
  267. (listelm)->field.tqe_next = (elm); \
  268. (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  269. } while (/*CONSTCOND*/0)
  270. #define QTAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  271. (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  272. (elm)->field.tqe_next = (listelm); \
  273. *(listelm)->field.tqe_prev = (elm); \
  274. (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  275. } while (/*CONSTCOND*/0)
  276. #define QTAILQ_REMOVE(head, elm, field) do { \
  277. if (((elm)->field.tqe_next) != NULL) \
  278. (elm)->field.tqe_next->field.tqe_prev = \
  279. (elm)->field.tqe_prev; \
  280. else \
  281. (head)->tqh_last = (elm)->field.tqe_prev; \
  282. *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  283. } while (/*CONSTCOND*/0)
  284. #define QTAILQ_FOREACH(var, head, field) \
  285. for ((var) = ((head)->tqh_first); \
  286. (var); \
  287. (var) = ((var)->field.tqe_next))
  288. #define QTAILQ_FOREACH_SAFE(var, head, field, next_var) \
  289. for ((var) = ((head)->tqh_first); \
  290. (var) && ((next_var) = ((var)->field.tqe_next), 1); \
  291. (var) = (next_var))
  292. #define QTAILQ_FOREACH_REVERSE(var, head, headname, field) \
  293. for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \
  294. (var); \
  295. (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
  296. /*
  297. * Tail queue access methods.
  298. */
  299. #define QTAILQ_EMPTY(head) ((head)->tqh_first == NULL)
  300. #define QTAILQ_FIRST(head) ((head)->tqh_first)
  301. #define QTAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  302. #define QTAILQ_LAST(head, headname) \
  303. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  304. #define QTAILQ_PREV(elm, headname, field) \
  305. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  306. /*
  307. * Circular queue definitions.
  308. */
  309. #define QCIRCLEQ_HEAD(name, type) \
  310. struct name { \
  311. struct type *cqh_first; /* first element */ \
  312. struct type *cqh_last; /* last element */ \
  313. }
  314. #define QCIRCLEQ_HEAD_INITIALIZER(head) \
  315. { (void *)&head, (void *)&head }
  316. #define QCIRCLEQ_ENTRY(type) \
  317. struct { \
  318. struct type *cqe_next; /* next element */ \
  319. struct type *cqe_prev; /* previous element */ \
  320. }
  321. /*
  322. * Circular queue functions.
  323. */
  324. #define QCIRCLEQ_INIT(head) do { \
  325. (head)->cqh_first = (void *)(head); \
  326. (head)->cqh_last = (void *)(head); \
  327. } while (/*CONSTCOND*/0)
  328. #define QCIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  329. (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  330. (elm)->field.cqe_prev = (listelm); \
  331. if ((listelm)->field.cqe_next == (void *)(head)) \
  332. (head)->cqh_last = (elm); \
  333. else \
  334. (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  335. (listelm)->field.cqe_next = (elm); \
  336. } while (/*CONSTCOND*/0)
  337. #define QCIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  338. (elm)->field.cqe_next = (listelm); \
  339. (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  340. if ((listelm)->field.cqe_prev == (void *)(head)) \
  341. (head)->cqh_first = (elm); \
  342. else \
  343. (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  344. (listelm)->field.cqe_prev = (elm); \
  345. } while (/*CONSTCOND*/0)
  346. #define QCIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  347. (elm)->field.cqe_next = (head)->cqh_first; \
  348. (elm)->field.cqe_prev = (void *)(head); \
  349. if ((head)->cqh_last == (void *)(head)) \
  350. (head)->cqh_last = (elm); \
  351. else \
  352. (head)->cqh_first->field.cqe_prev = (elm); \
  353. (head)->cqh_first = (elm); \
  354. } while (/*CONSTCOND*/0)
  355. #define QCIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  356. (elm)->field.cqe_next = (void *)(head); \
  357. (elm)->field.cqe_prev = (head)->cqh_last; \
  358. if ((head)->cqh_first == (void *)(head)) \
  359. (head)->cqh_first = (elm); \
  360. else \
  361. (head)->cqh_last->field.cqe_next = (elm); \
  362. (head)->cqh_last = (elm); \
  363. } while (/*CONSTCOND*/0)
  364. #define QCIRCLEQ_REMOVE(head, elm, field) do { \
  365. if ((elm)->field.cqe_next == (void *)(head)) \
  366. (head)->cqh_last = (elm)->field.cqe_prev; \
  367. else \
  368. (elm)->field.cqe_next->field.cqe_prev = \
  369. (elm)->field.cqe_prev; \
  370. if ((elm)->field.cqe_prev == (void *)(head)) \
  371. (head)->cqh_first = (elm)->field.cqe_next; \
  372. else \
  373. (elm)->field.cqe_prev->field.cqe_next = \
  374. (elm)->field.cqe_next; \
  375. } while (/*CONSTCOND*/0)
  376. #define QCIRCLEQ_FOREACH(var, head, field) \
  377. for ((var) = ((head)->cqh_first); \
  378. (var) != (const void *)(head); \
  379. (var) = ((var)->field.cqe_next))
  380. #define QCIRCLEQ_FOREACH_REVERSE(var, head, field) \
  381. for ((var) = ((head)->cqh_last); \
  382. (var) != (const void *)(head); \
  383. (var) = ((var)->field.cqe_prev))
  384. /*
  385. * Circular queue access methods.
  386. */
  387. #define QCIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
  388. #define QCIRCLEQ_FIRST(head) ((head)->cqh_first)
  389. #define QCIRCLEQ_LAST(head) ((head)->cqh_last)
  390. #define QCIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
  391. #define QCIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
  392. #define QCIRCLEQ_LOOP_NEXT(head, elm, field) \
  393. (((elm)->field.cqe_next == (void *)(head)) \
  394. ? ((head)->cqh_first) \
  395. : (elm->field.cqe_next))
  396. #define QCIRCLEQ_LOOP_PREV(head, elm, field) \
  397. (((elm)->field.cqe_prev == (void *)(head)) \
  398. ? ((head)->cqh_last) \
  399. : (elm->field.cqe_prev))
  400. #endif /* !QEMU_SYS_QUEUE_H_ */