qemu-queue.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 singly-linked lists, lists, simple
  5. * queues, and tail 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: singly-linked lists,
  41. * lists, simple queues, and tail queues.
  42. *
  43. * A singly-linked list is headed by a single forward pointer. The
  44. * elements are singly linked for minimum space and pointer manipulation
  45. * overhead at the expense of O(n) removal for arbitrary elements. New
  46. * elements can be added to the list after an existing element or at the
  47. * head of the list. Elements being removed from the head of the list
  48. * should use the explicit macro for this purpose for optimum
  49. * efficiency. A singly-linked list may only be traversed in the forward
  50. * direction. Singly-linked lists are ideal for applications with large
  51. * datasets and few or no removals or for implementing a LIFO queue.
  52. *
  53. * A list is headed by a single forward pointer (or an array of forward
  54. * pointers for a hash table header). The elements are doubly linked
  55. * so that an arbitrary element can be removed without a need to
  56. * traverse the list. New elements can be added to the list before
  57. * or after an existing element or at the head of the list. A list
  58. * may only be traversed in the forward direction.
  59. *
  60. * A simple queue is headed by a pair of pointers, one the head of the
  61. * list and the other to the tail of the list. The elements are singly
  62. * linked to save space, so elements can only be removed from the
  63. * head of the list. New elements can be added to the list after
  64. * an existing element, at the head of the list, or at the end of the
  65. * list. A simple queue may only be traversed in the forward direction.
  66. *
  67. * A tail queue is headed by a pair of pointers, one to the head of the
  68. * list and the other to the tail of the list. The elements are doubly
  69. * linked so that an arbitrary element can be removed without a need to
  70. * traverse the list. New elements can be added to the list before or
  71. * after an existing element, at the head of the list, or at the end of
  72. * the list. A tail queue may be traversed in either direction.
  73. *
  74. * For details on the use of these macros, see the queue(3) manual page.
  75. */
  76. #include "qemu-barrier.h" /* for smp_wmb() */
  77. /*
  78. * List definitions.
  79. */
  80. #define QLIST_HEAD(name, type) \
  81. struct name { \
  82. struct type *lh_first; /* first element */ \
  83. }
  84. #define QLIST_HEAD_INITIALIZER(head) \
  85. { NULL }
  86. #define QLIST_ENTRY(type) \
  87. struct { \
  88. struct type *le_next; /* next element */ \
  89. struct type **le_prev; /* address of previous next element */ \
  90. }
  91. /*
  92. * List functions.
  93. */
  94. #define QLIST_INIT(head) do { \
  95. (head)->lh_first = NULL; \
  96. } while (/*CONSTCOND*/0)
  97. #define QLIST_INSERT_AFTER(listelm, elm, field) do { \
  98. if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
  99. (listelm)->field.le_next->field.le_prev = \
  100. &(elm)->field.le_next; \
  101. (listelm)->field.le_next = (elm); \
  102. (elm)->field.le_prev = &(listelm)->field.le_next; \
  103. } while (/*CONSTCOND*/0)
  104. #define QLIST_INSERT_BEFORE(listelm, elm, field) do { \
  105. (elm)->field.le_prev = (listelm)->field.le_prev; \
  106. (elm)->field.le_next = (listelm); \
  107. *(listelm)->field.le_prev = (elm); \
  108. (listelm)->field.le_prev = &(elm)->field.le_next; \
  109. } while (/*CONSTCOND*/0)
  110. #define QLIST_INSERT_HEAD(head, elm, field) do { \
  111. if (((elm)->field.le_next = (head)->lh_first) != NULL) \
  112. (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
  113. (head)->lh_first = (elm); \
  114. (elm)->field.le_prev = &(head)->lh_first; \
  115. } while (/*CONSTCOND*/0)
  116. #define QLIST_INSERT_HEAD_RCU(head, elm, field) do { \
  117. (elm)->field.le_prev = &(head)->lh_first; \
  118. (elm)->field.le_next = (head)->lh_first; \
  119. smp_wmb(); /* fill elm before linking it */ \
  120. if ((head)->lh_first != NULL) { \
  121. (head)->lh_first->field.le_prev = &(elm)->field.le_next; \
  122. } \
  123. (head)->lh_first = (elm); \
  124. smp_wmb(); \
  125. } while (/* CONSTCOND*/0)
  126. #define QLIST_REMOVE(elm, field) do { \
  127. if ((elm)->field.le_next != NULL) \
  128. (elm)->field.le_next->field.le_prev = \
  129. (elm)->field.le_prev; \
  130. *(elm)->field.le_prev = (elm)->field.le_next; \
  131. } while (/*CONSTCOND*/0)
  132. #define QLIST_FOREACH(var, head, field) \
  133. for ((var) = ((head)->lh_first); \
  134. (var); \
  135. (var) = ((var)->field.le_next))
  136. #define QLIST_FOREACH_SAFE(var, head, field, next_var) \
  137. for ((var) = ((head)->lh_first); \
  138. (var) && ((next_var) = ((var)->field.le_next), 1); \
  139. (var) = (next_var))
  140. /*
  141. * List access methods.
  142. */
  143. #define QLIST_EMPTY(head) ((head)->lh_first == NULL)
  144. #define QLIST_FIRST(head) ((head)->lh_first)
  145. #define QLIST_NEXT(elm, field) ((elm)->field.le_next)
  146. /*
  147. * Singly-linked List definitions.
  148. */
  149. #define QSLIST_HEAD(name, type) \
  150. struct name { \
  151. struct type *slh_first; /* first element */ \
  152. }
  153. #define QSLIST_HEAD_INITIALIZER(head) \
  154. { NULL }
  155. #define QSLIST_ENTRY(type) \
  156. struct { \
  157. struct type *sle_next; /* next element */ \
  158. }
  159. /*
  160. * Singly-linked List functions.
  161. */
  162. #define QSLIST_INIT(head) do { \
  163. (head)->slh_first = NULL; \
  164. } while (/*CONSTCOND*/0)
  165. #define QSLIST_INSERT_AFTER(slistelm, elm, field) do { \
  166. (elm)->field.sle_next = (slistelm)->field.sle_next; \
  167. (slistelm)->field.sle_next = (elm); \
  168. } while (/*CONSTCOND*/0)
  169. #define QSLIST_INSERT_HEAD(head, elm, field) do { \
  170. (elm)->field.sle_next = (head)->slh_first; \
  171. (head)->slh_first = (elm); \
  172. } while (/*CONSTCOND*/0)
  173. #define QSLIST_REMOVE_HEAD(head, field) do { \
  174. (head)->slh_first = (head)->slh_first->field.sle_next; \
  175. } while (/*CONSTCOND*/0)
  176. #define QSLIST_REMOVE_AFTER(slistelm, field) do { \
  177. (slistelm)->field.sle_next = \
  178. QSLIST_NEXT(QSLIST_NEXT((slistelm), field), field); \
  179. } while (/*CONSTCOND*/0)
  180. #define QSLIST_FOREACH(var, head, field) \
  181. for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
  182. #define QSLIST_FOREACH_SAFE(var, head, field, tvar) \
  183. for ((var) = QSLIST_FIRST((head)); \
  184. (var) && ((tvar) = QSLIST_NEXT((var), field), 1); \
  185. (var) = (tvar))
  186. /*
  187. * Singly-linked List access methods.
  188. */
  189. #define QSLIST_EMPTY(head) ((head)->slh_first == NULL)
  190. #define QSLIST_FIRST(head) ((head)->slh_first)
  191. #define QSLIST_NEXT(elm, field) ((elm)->field.sle_next)
  192. /*
  193. * Simple queue definitions.
  194. */
  195. #define QSIMPLEQ_HEAD(name, type) \
  196. struct name { \
  197. struct type *sqh_first; /* first element */ \
  198. struct type **sqh_last; /* addr of last next element */ \
  199. }
  200. #define QSIMPLEQ_HEAD_INITIALIZER(head) \
  201. { NULL, &(head).sqh_first }
  202. #define QSIMPLEQ_ENTRY(type) \
  203. struct { \
  204. struct type *sqe_next; /* next element */ \
  205. }
  206. /*
  207. * Simple queue functions.
  208. */
  209. #define QSIMPLEQ_INIT(head) do { \
  210. (head)->sqh_first = NULL; \
  211. (head)->sqh_last = &(head)->sqh_first; \
  212. } while (/*CONSTCOND*/0)
  213. #define QSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
  214. if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
  215. (head)->sqh_last = &(elm)->field.sqe_next; \
  216. (head)->sqh_first = (elm); \
  217. } while (/*CONSTCOND*/0)
  218. #define QSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
  219. (elm)->field.sqe_next = NULL; \
  220. *(head)->sqh_last = (elm); \
  221. (head)->sqh_last = &(elm)->field.sqe_next; \
  222. } while (/*CONSTCOND*/0)
  223. #define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  224. if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL) \
  225. (head)->sqh_last = &(elm)->field.sqe_next; \
  226. (listelm)->field.sqe_next = (elm); \
  227. } while (/*CONSTCOND*/0)
  228. #define QSIMPLEQ_REMOVE_HEAD(head, field) do { \
  229. if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
  230. (head)->sqh_last = &(head)->sqh_first; \
  231. } while (/*CONSTCOND*/0)
  232. #define QSIMPLEQ_REMOVE(head, elm, type, field) do { \
  233. if ((head)->sqh_first == (elm)) { \
  234. QSIMPLEQ_REMOVE_HEAD((head), field); \
  235. } else { \
  236. struct type *curelm = (head)->sqh_first; \
  237. while (curelm->field.sqe_next != (elm)) \
  238. curelm = curelm->field.sqe_next; \
  239. if ((curelm->field.sqe_next = \
  240. curelm->field.sqe_next->field.sqe_next) == NULL) \
  241. (head)->sqh_last = &(curelm)->field.sqe_next; \
  242. } \
  243. } while (/*CONSTCOND*/0)
  244. #define QSIMPLEQ_FOREACH(var, head, field) \
  245. for ((var) = ((head)->sqh_first); \
  246. (var); \
  247. (var) = ((var)->field.sqe_next))
  248. #define QSIMPLEQ_FOREACH_SAFE(var, head, field, next) \
  249. for ((var) = ((head)->sqh_first); \
  250. (var) && ((next = ((var)->field.sqe_next)), 1); \
  251. (var) = (next))
  252. #define QSIMPLEQ_CONCAT(head1, head2) do { \
  253. if (!QSIMPLEQ_EMPTY((head2))) { \
  254. *(head1)->sqh_last = (head2)->sqh_first; \
  255. (head1)->sqh_last = (head2)->sqh_last; \
  256. QSIMPLEQ_INIT((head2)); \
  257. } \
  258. } while (/*CONSTCOND*/0)
  259. #define QSIMPLEQ_LAST(head, type, field) \
  260. (QSIMPLEQ_EMPTY((head)) ? \
  261. NULL : \
  262. ((struct type *)(void *) \
  263. ((char *)((head)->sqh_last) - offsetof(struct type, field))))
  264. /*
  265. * Simple queue access methods.
  266. */
  267. #define QSIMPLEQ_EMPTY(head) ((head)->sqh_first == NULL)
  268. #define QSIMPLEQ_FIRST(head) ((head)->sqh_first)
  269. #define QSIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
  270. /*
  271. * Tail queue definitions.
  272. */
  273. #define Q_TAILQ_HEAD(name, type, qual) \
  274. struct name { \
  275. qual type *tqh_first; /* first element */ \
  276. qual type *qual *tqh_last; /* addr of last next element */ \
  277. }
  278. #define QTAILQ_HEAD(name, type) Q_TAILQ_HEAD(name, struct type,)
  279. #define QTAILQ_HEAD_INITIALIZER(head) \
  280. { NULL, &(head).tqh_first }
  281. #define Q_TAILQ_ENTRY(type, qual) \
  282. struct { \
  283. qual type *tqe_next; /* next element */ \
  284. qual type *qual *tqe_prev; /* address of previous next element */\
  285. }
  286. #define QTAILQ_ENTRY(type) Q_TAILQ_ENTRY(struct type,)
  287. /*
  288. * Tail queue functions.
  289. */
  290. #define QTAILQ_INIT(head) do { \
  291. (head)->tqh_first = NULL; \
  292. (head)->tqh_last = &(head)->tqh_first; \
  293. } while (/*CONSTCOND*/0)
  294. #define QTAILQ_INSERT_HEAD(head, elm, field) do { \
  295. if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  296. (head)->tqh_first->field.tqe_prev = \
  297. &(elm)->field.tqe_next; \
  298. else \
  299. (head)->tqh_last = &(elm)->field.tqe_next; \
  300. (head)->tqh_first = (elm); \
  301. (elm)->field.tqe_prev = &(head)->tqh_first; \
  302. } while (/*CONSTCOND*/0)
  303. #define QTAILQ_INSERT_TAIL(head, elm, field) do { \
  304. (elm)->field.tqe_next = NULL; \
  305. (elm)->field.tqe_prev = (head)->tqh_last; \
  306. *(head)->tqh_last = (elm); \
  307. (head)->tqh_last = &(elm)->field.tqe_next; \
  308. } while (/*CONSTCOND*/0)
  309. #define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  310. if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  311. (elm)->field.tqe_next->field.tqe_prev = \
  312. &(elm)->field.tqe_next; \
  313. else \
  314. (head)->tqh_last = &(elm)->field.tqe_next; \
  315. (listelm)->field.tqe_next = (elm); \
  316. (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  317. } while (/*CONSTCOND*/0)
  318. #define QTAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  319. (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  320. (elm)->field.tqe_next = (listelm); \
  321. *(listelm)->field.tqe_prev = (elm); \
  322. (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  323. } while (/*CONSTCOND*/0)
  324. #define QTAILQ_REMOVE(head, elm, field) do { \
  325. if (((elm)->field.tqe_next) != NULL) \
  326. (elm)->field.tqe_next->field.tqe_prev = \
  327. (elm)->field.tqe_prev; \
  328. else \
  329. (head)->tqh_last = (elm)->field.tqe_prev; \
  330. *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  331. } while (/*CONSTCOND*/0)
  332. #define QTAILQ_FOREACH(var, head, field) \
  333. for ((var) = ((head)->tqh_first); \
  334. (var); \
  335. (var) = ((var)->field.tqe_next))
  336. #define QTAILQ_FOREACH_SAFE(var, head, field, next_var) \
  337. for ((var) = ((head)->tqh_first); \
  338. (var) && ((next_var) = ((var)->field.tqe_next), 1); \
  339. (var) = (next_var))
  340. #define QTAILQ_FOREACH_REVERSE(var, head, headname, field) \
  341. for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \
  342. (var); \
  343. (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
  344. /*
  345. * Tail queue access methods.
  346. */
  347. #define QTAILQ_EMPTY(head) ((head)->tqh_first == NULL)
  348. #define QTAILQ_FIRST(head) ((head)->tqh_first)
  349. #define QTAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  350. #define QTAILQ_LAST(head, headname) \
  351. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  352. #define QTAILQ_PREV(elm, headname, field) \
  353. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  354. #endif /* !QEMU_SYS_QUEUE_H_ */