queue.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*-
  2. * Copyright (c) 1991, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * @(#)queue.h 8.5 (Berkeley) 8/20/94
  30. * $FreeBSD$
  31. */
  32. #ifndef _SYS_QUEUE_H_
  33. #define _SYS_QUEUE_H_
  34. #include <sys/cdefs.h>
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /*
  39. * This file defines four types of data structures: singly-linked lists,
  40. * singly-linked tail queues, lists and tail queues.
  41. *
  42. * A singly-linked list is headed by a single forward pointer. The elements
  43. * are singly linked for minimum space and pointer manipulation overhead at
  44. * the expense of O(n) removal for arbitrary elements. New elements can be
  45. * added to the list after an existing element or at the head of the list.
  46. * Elements being removed from the head of the list should use the explicit
  47. * macro for this purpose for optimum efficiency. A singly-linked list may
  48. * only be traversed in the forward direction. Singly-linked lists are ideal
  49. * for applications with large datasets and few or no removals or for
  50. * implementing a LIFO queue.
  51. *
  52. * A singly-linked tail queue is headed by a pair of pointers, one to the
  53. * head of the list and the other to the tail of the list. The elements are
  54. * singly linked for minimum space and pointer manipulation overhead at the
  55. * expense of O(n) removal for arbitrary elements. New elements can be added
  56. * to the list after an existing element, at the head of the list, or at the
  57. * end of the list. Elements being removed from the head of the tail queue
  58. * should use the explicit macro for this purpose for optimum efficiency.
  59. * A singly-linked tail queue may only be traversed in the forward direction.
  60. * Singly-linked tail queues are ideal for applications with large datasets
  61. * and few or no removals or for implementing a FIFO queue.
  62. *
  63. * A list is headed by a single forward pointer (or an array of forward
  64. * pointers for a hash table header). The elements are doubly linked
  65. * so that an arbitrary element can be removed without a need to
  66. * traverse the list. New elements can be added to the list before
  67. * or after an existing element or at the head of the list. A list
  68. * may only be traversed in the forward direction.
  69. *
  70. * A tail queue is headed by a pair of pointers, one to the head of the
  71. * list and the other to the tail of the list. The elements are doubly
  72. * linked so that an arbitrary element can be removed without a need to
  73. * traverse the list. New elements can be added to the list before or
  74. * after an existing element, at the head of the list, or at the end of
  75. * the list. A tail queue may be traversed in either direction.
  76. *
  77. * For details on the use of these macros, see the queue(3) manual page.
  78. *
  79. *
  80. * SLIST LIST STAILQ TAILQ
  81. * _HEAD + + + +
  82. * _HEAD_INITIALIZER + + + +
  83. * _ENTRY + + + +
  84. * _INIT + + + +
  85. * _EMPTY + + + +
  86. * _FIRST + + + +
  87. * _NEXT + + + +
  88. * _PREV - - - +
  89. * _LAST - - + +
  90. * _FOREACH + + + +
  91. * _FOREACH_SAFE + + + +
  92. * _FOREACH_REVERSE - - - +
  93. * _FOREACH_REVERSE_SAFE - - - +
  94. * _INSERT_HEAD + + + +
  95. * _INSERT_BEFORE - + - +
  96. * _INSERT_AFTER + + + +
  97. * _INSERT_TAIL - - + +
  98. * _CONCAT - - + +
  99. * _REMOVE_AFTER + - + -
  100. * _REMOVE_HEAD + - + -
  101. * _REMOVE + + + +
  102. *
  103. */
  104. #ifdef QUEUE_MACRO_DEBUG
  105. /* Store the last 2 places the queue element or head was altered */
  106. struct qm_trace {
  107. char * lastfile;
  108. int lastline;
  109. char * prevfile;
  110. int prevline;
  111. };
  112. #define TRACEBUF struct qm_trace trace;
  113. #define TRASHIT(x) do {(x) = (void *)-1;} while (0)
  114. #define QMD_SAVELINK(name, link) void **name = (void *)&(link)
  115. #define QMD_TRACE_HEAD(head) do { \
  116. (head)->trace.prevline = (head)->trace.lastline; \
  117. (head)->trace.prevfile = (head)->trace.lastfile; \
  118. (head)->trace.lastline = __LINE__; \
  119. (head)->trace.lastfile = __FILE__; \
  120. } while (0)
  121. #define QMD_TRACE_ELEM(elem) do { \
  122. (elem)->trace.prevline = (elem)->trace.lastline; \
  123. (elem)->trace.prevfile = (elem)->trace.lastfile; \
  124. (elem)->trace.lastline = __LINE__; \
  125. (elem)->trace.lastfile = __FILE__; \
  126. } while (0)
  127. #else
  128. #define QMD_TRACE_ELEM(elem)
  129. #define QMD_TRACE_HEAD(head)
  130. #define QMD_SAVELINK(name, link)
  131. #define TRACEBUF
  132. #define TRASHIT(x)
  133. #endif /* QUEUE_MACRO_DEBUG */
  134. /*
  135. * Singly-linked List declarations.
  136. */
  137. #define SLIST_HEAD(name, type) \
  138. struct name { \
  139. struct type *slh_first; /* first element */ \
  140. }
  141. #define SLIST_HEAD_INITIALIZER(head) \
  142. { NULL }
  143. #define SLIST_ENTRY(type) \
  144. struct { \
  145. struct type *sle_next; /* next element */ \
  146. }
  147. /*
  148. * Singly-linked List functions.
  149. */
  150. #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
  151. #define SLIST_FIRST(head) ((head)->slh_first)
  152. #define SLIST_FOREACH(var, head, field) \
  153. for ((var) = SLIST_FIRST((head)); \
  154. (var); \
  155. (var) = SLIST_NEXT((var), field))
  156. #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
  157. for ((var) = SLIST_FIRST((head)); \
  158. (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
  159. (var) = (tvar))
  160. #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  161. for ((varp) = &SLIST_FIRST((head)); \
  162. ((var) = *(varp)) != NULL; \
  163. (varp) = &SLIST_NEXT((var), field))
  164. #define SLIST_INIT(head) do { \
  165. SLIST_FIRST((head)) = NULL; \
  166. } while (0)
  167. #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  168. SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
  169. SLIST_NEXT((slistelm), field) = (elm); \
  170. } while (0)
  171. #define SLIST_INSERT_HEAD(head, elm, field) do { \
  172. SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
  173. SLIST_FIRST((head)) = (elm); \
  174. } while (0)
  175. #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  176. #define SLIST_REMOVE(head, elm, type, field) do { \
  177. QMD_SAVELINK(oldnext, (elm)->field.sle_next); \
  178. if (SLIST_FIRST((head)) == (elm)) { \
  179. SLIST_REMOVE_HEAD((head), field); \
  180. } \
  181. else { \
  182. struct type *curelm = SLIST_FIRST((head)); \
  183. while (SLIST_NEXT(curelm, field) != (elm)) \
  184. curelm = SLIST_NEXT(curelm, field); \
  185. SLIST_REMOVE_AFTER(curelm, field); \
  186. } \
  187. TRASHIT(*oldnext); \
  188. } while (0)
  189. #define SLIST_REMOVE_AFTER(elm, field) do { \
  190. SLIST_NEXT(elm, field) = \
  191. SLIST_NEXT(SLIST_NEXT(elm, field), field); \
  192. } while (0)
  193. #define SLIST_REMOVE_HEAD(head, field) do { \
  194. SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
  195. } while (0)
  196. /*
  197. * Singly-linked Tail queue declarations.
  198. */
  199. #define STAILQ_HEAD(name, type) \
  200. struct name { \
  201. struct type *stqh_first;/* first element */ \
  202. struct type **stqh_last;/* addr of last next element */ \
  203. }
  204. #define STAILQ_HEAD_INITIALIZER(head) \
  205. { NULL, &(head).stqh_first }
  206. #define STAILQ_ENTRY(type) \
  207. struct { \
  208. struct type *stqe_next; /* next element */ \
  209. }
  210. /*
  211. * Singly-linked Tail queue functions.
  212. */
  213. #define STAILQ_CONCAT(head1, head2) do { \
  214. if (!STAILQ_EMPTY((head2))) { \
  215. *(head1)->stqh_last = (head2)->stqh_first; \
  216. (head1)->stqh_last = (head2)->stqh_last; \
  217. STAILQ_INIT((head2)); \
  218. } \
  219. } while (0)
  220. #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
  221. #define STAILQ_FIRST(head) ((head)->stqh_first)
  222. #define STAILQ_FOREACH(var, head, field) \
  223. for((var) = STAILQ_FIRST((head)); \
  224. (var); \
  225. (var) = STAILQ_NEXT((var), field))
  226. #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
  227. for ((var) = STAILQ_FIRST((head)); \
  228. (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
  229. (var) = (tvar))
  230. #define STAILQ_INIT(head) do { \
  231. STAILQ_FIRST((head)) = NULL; \
  232. (head)->stqh_last = &STAILQ_FIRST((head)); \
  233. } while (0)
  234. #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
  235. if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
  236. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  237. STAILQ_NEXT((tqelm), field) = (elm); \
  238. } while (0)
  239. #define STAILQ_INSERT_HEAD(head, elm, field) do { \
  240. if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
  241. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  242. STAILQ_FIRST((head)) = (elm); \
  243. } while (0)
  244. #define STAILQ_INSERT_TAIL(head, elm, field) do { \
  245. STAILQ_NEXT((elm), field) = NULL; \
  246. *(head)->stqh_last = (elm); \
  247. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  248. } while (0)
  249. #define STAILQ_LAST(head, type, field) \
  250. (STAILQ_EMPTY((head)) ? \
  251. NULL : \
  252. ((struct type *)(void *) \
  253. ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
  254. #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
  255. #define STAILQ_REMOVE(head, elm, type, field) do { \
  256. QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \
  257. if (STAILQ_FIRST((head)) == (elm)) { \
  258. STAILQ_REMOVE_HEAD((head), field); \
  259. } \
  260. else { \
  261. struct type *curelm = STAILQ_FIRST((head)); \
  262. while (STAILQ_NEXT(curelm, field) != (elm)) \
  263. curelm = STAILQ_NEXT(curelm, field); \
  264. STAILQ_REMOVE_AFTER(head, curelm, field); \
  265. } \
  266. TRASHIT(*oldnext); \
  267. } while (0)
  268. #define STAILQ_REMOVE_HEAD(head, field) do { \
  269. if ((STAILQ_FIRST((head)) = \
  270. STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
  271. (head)->stqh_last = &STAILQ_FIRST((head)); \
  272. } while (0)
  273. #define STAILQ_REMOVE_AFTER(head, elm, field) do { \
  274. if ((STAILQ_NEXT(elm, field) = \
  275. STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
  276. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  277. } while (0)
  278. #define STAILQ_SWAP(head1, head2, type) do { \
  279. struct type *swap_first = STAILQ_FIRST(head1); \
  280. struct type **swap_last = (head1)->stqh_last; \
  281. STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
  282. (head1)->stqh_last = (head2)->stqh_last; \
  283. STAILQ_FIRST(head2) = swap_first; \
  284. (head2)->stqh_last = swap_last; \
  285. if (STAILQ_EMPTY(head1)) \
  286. (head1)->stqh_last = &STAILQ_FIRST(head1); \
  287. if (STAILQ_EMPTY(head2)) \
  288. (head2)->stqh_last = &STAILQ_FIRST(head2); \
  289. } while (0)
  290. #define STAILQ_INSERT_CHAIN_HEAD(head, elm_chead, elm_ctail, field) do { \
  291. if ((STAILQ_NEXT(elm_ctail, field) = STAILQ_FIRST(head)) == NULL ) { \
  292. (head)->stqh_last = &STAILQ_NEXT(elm_ctail, field); \
  293. } \
  294. STAILQ_FIRST(head) = (elm_chead); \
  295. } while (0)
  296. /*
  297. * List declarations.
  298. */
  299. #define LIST_HEAD(name, type) \
  300. struct name { \
  301. struct type *lh_first; /* first element */ \
  302. }
  303. #define LIST_HEAD_INITIALIZER(head) \
  304. { NULL }
  305. #define LIST_ENTRY(type) \
  306. struct { \
  307. struct type *le_next; /* next element */ \
  308. struct type **le_prev; /* address of previous next element */ \
  309. }
  310. /*
  311. * List functions.
  312. */
  313. #if (defined(_KERNEL) && defined(INVARIANTS))
  314. #define QMD_LIST_CHECK_HEAD(head, field) do { \
  315. if (LIST_FIRST((head)) != NULL && \
  316. LIST_FIRST((head))->field.le_prev != \
  317. &LIST_FIRST((head))) \
  318. panic("Bad list head %p first->prev != head", (head)); \
  319. } while (0)
  320. #define QMD_LIST_CHECK_NEXT(elm, field) do { \
  321. if (LIST_NEXT((elm), field) != NULL && \
  322. LIST_NEXT((elm), field)->field.le_prev != \
  323. &((elm)->field.le_next)) \
  324. panic("Bad link elm %p next->prev != elm", (elm)); \
  325. } while (0)
  326. #define QMD_LIST_CHECK_PREV(elm, field) do { \
  327. if (*(elm)->field.le_prev != (elm)) \
  328. panic("Bad link elm %p prev->next != elm", (elm)); \
  329. } while (0)
  330. #else
  331. #define QMD_LIST_CHECK_HEAD(head, field)
  332. #define QMD_LIST_CHECK_NEXT(elm, field)
  333. #define QMD_LIST_CHECK_PREV(elm, field)
  334. #endif /* (_KERNEL && INVARIANTS) */
  335. #define LIST_EMPTY(head) ((head)->lh_first == NULL)
  336. #define LIST_FIRST(head) ((head)->lh_first)
  337. #define LIST_FOREACH(var, head, field) \
  338. for ((var) = LIST_FIRST((head)); \
  339. (var); \
  340. (var) = LIST_NEXT((var), field))
  341. #define LIST_FOREACH_SAFE(var, head, field, tvar) \
  342. for ((var) = LIST_FIRST((head)); \
  343. (var) && ((tvar) = LIST_NEXT((var), field), 1); \
  344. (var) = (tvar))
  345. #define LIST_INIT(head) do { \
  346. LIST_FIRST((head)) = NULL; \
  347. } while (0)
  348. #define LIST_INSERT_AFTER(listelm, elm, field) do { \
  349. QMD_LIST_CHECK_NEXT(listelm, field); \
  350. if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
  351. LIST_NEXT((listelm), field)->field.le_prev = \
  352. &LIST_NEXT((elm), field); \
  353. LIST_NEXT((listelm), field) = (elm); \
  354. (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
  355. } while (0)
  356. #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
  357. QMD_LIST_CHECK_PREV(listelm, field); \
  358. (elm)->field.le_prev = (listelm)->field.le_prev; \
  359. LIST_NEXT((elm), field) = (listelm); \
  360. *(listelm)->field.le_prev = (elm); \
  361. (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
  362. } while (0)
  363. #define LIST_INSERT_HEAD(head, elm, field) do { \
  364. QMD_LIST_CHECK_HEAD((head), field); \
  365. if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
  366. LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
  367. LIST_FIRST((head)) = (elm); \
  368. (elm)->field.le_prev = &LIST_FIRST((head)); \
  369. } while (0)
  370. #define LIST_NEXT(elm, field) ((elm)->field.le_next)
  371. #define LIST_REMOVE(elm, field) do { \
  372. QMD_SAVELINK(oldnext, (elm)->field.le_next); \
  373. QMD_SAVELINK(oldprev, (elm)->field.le_prev); \
  374. QMD_LIST_CHECK_NEXT(elm, field); \
  375. QMD_LIST_CHECK_PREV(elm, field); \
  376. if (LIST_NEXT((elm), field) != NULL) \
  377. LIST_NEXT((elm), field)->field.le_prev = \
  378. (elm)->field.le_prev; \
  379. *(elm)->field.le_prev = LIST_NEXT((elm), field); \
  380. TRASHIT(*oldnext); \
  381. TRASHIT(*oldprev); \
  382. } while (0)
  383. #define LIST_SWAP(head1, head2, type, field) do { \
  384. struct type *swap_tmp = LIST_FIRST((head1)); \
  385. LIST_FIRST((head1)) = LIST_FIRST((head2)); \
  386. LIST_FIRST((head2)) = swap_tmp; \
  387. if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
  388. swap_tmp->field.le_prev = &LIST_FIRST((head1)); \
  389. if ((swap_tmp = LIST_FIRST((head2))) != NULL) \
  390. swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
  391. } while (0)
  392. /*
  393. * Tail queue declarations.
  394. */
  395. #define TAILQ_HEAD(name, type) \
  396. struct name { \
  397. struct type *tqh_first; /* first element */ \
  398. struct type **tqh_last; /* addr of last next element */ \
  399. TRACEBUF \
  400. }
  401. #define TAILQ_HEAD_INITIALIZER(head) \
  402. { NULL, &(head).tqh_first }
  403. #define TAILQ_ENTRY(type) \
  404. struct { \
  405. struct type *tqe_next; /* next element */ \
  406. struct type **tqe_prev; /* address of previous next element */ \
  407. TRACEBUF \
  408. }
  409. /*
  410. * Tail queue functions.
  411. */
  412. #if (defined(_KERNEL) && defined(INVARIANTS))
  413. #define QMD_TAILQ_CHECK_HEAD(head, field) do { \
  414. if (!TAILQ_EMPTY(head) && \
  415. TAILQ_FIRST((head))->field.tqe_prev != \
  416. &TAILQ_FIRST((head))) \
  417. panic("Bad tailq head %p first->prev != head", (head)); \
  418. } while (0)
  419. #define QMD_TAILQ_CHECK_TAIL(head, field) do { \
  420. if (*(head)->tqh_last != NULL) \
  421. panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \
  422. } while (0)
  423. #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
  424. if (TAILQ_NEXT((elm), field) != NULL && \
  425. TAILQ_NEXT((elm), field)->field.tqe_prev != \
  426. &((elm)->field.tqe_next)) \
  427. panic("Bad link elm %p next->prev != elm", (elm)); \
  428. } while (0)
  429. #define QMD_TAILQ_CHECK_PREV(elm, field) do { \
  430. if (*(elm)->field.tqe_prev != (elm)) \
  431. panic("Bad link elm %p prev->next != elm", (elm)); \
  432. } while (0)
  433. #else
  434. #define QMD_TAILQ_CHECK_HEAD(head, field)
  435. #define QMD_TAILQ_CHECK_TAIL(head, headname)
  436. #define QMD_TAILQ_CHECK_NEXT(elm, field)
  437. #define QMD_TAILQ_CHECK_PREV(elm, field)
  438. #endif /* (_KERNEL && INVARIANTS) */
  439. #define TAILQ_CONCAT(head1, head2, field) do { \
  440. if (!TAILQ_EMPTY(head2)) { \
  441. *(head1)->tqh_last = (head2)->tqh_first; \
  442. (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
  443. (head1)->tqh_last = (head2)->tqh_last; \
  444. TAILQ_INIT((head2)); \
  445. QMD_TRACE_HEAD(head1); \
  446. QMD_TRACE_HEAD(head2); \
  447. } \
  448. } while (0)
  449. #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
  450. #define TAILQ_FIRST(head) ((head)->tqh_first)
  451. #define TAILQ_FOREACH(var, head, field) \
  452. for ((var) = TAILQ_FIRST((head)); \
  453. (var); \
  454. (var) = TAILQ_NEXT((var), field))
  455. #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
  456. for ((var) = TAILQ_FIRST((head)); \
  457. (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
  458. (var) = (tvar))
  459. #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  460. for ((var) = TAILQ_LAST((head), headname); \
  461. (var); \
  462. (var) = TAILQ_PREV((var), headname, field))
  463. #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
  464. for ((var) = TAILQ_LAST((head), headname); \
  465. (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
  466. (var) = (tvar))
  467. #define TAILQ_INIT(head) do { \
  468. TAILQ_FIRST((head)) = NULL; \
  469. (head)->tqh_last = &TAILQ_FIRST((head)); \
  470. QMD_TRACE_HEAD(head); \
  471. } while (0)
  472. #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  473. QMD_TAILQ_CHECK_NEXT(listelm, field); \
  474. if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
  475. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  476. &TAILQ_NEXT((elm), field); \
  477. else { \
  478. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  479. QMD_TRACE_HEAD(head); \
  480. } \
  481. TAILQ_NEXT((listelm), field) = (elm); \
  482. (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
  483. QMD_TRACE_ELEM(&(elm)->field); \
  484. QMD_TRACE_ELEM(&listelm->field); \
  485. } while (0)
  486. #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  487. QMD_TAILQ_CHECK_PREV(listelm, field); \
  488. (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  489. TAILQ_NEXT((elm), field) = (listelm); \
  490. *(listelm)->field.tqe_prev = (elm); \
  491. (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
  492. QMD_TRACE_ELEM(&(elm)->field); \
  493. QMD_TRACE_ELEM(&listelm->field); \
  494. } while (0)
  495. #define TAILQ_INSERT_HEAD(head, elm, field) do { \
  496. QMD_TAILQ_CHECK_HEAD(head, field); \
  497. if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
  498. TAILQ_FIRST((head))->field.tqe_prev = \
  499. &TAILQ_NEXT((elm), field); \
  500. else \
  501. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  502. TAILQ_FIRST((head)) = (elm); \
  503. (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
  504. QMD_TRACE_HEAD(head); \
  505. QMD_TRACE_ELEM(&(elm)->field); \
  506. } while (0)
  507. #define TAILQ_INSERT_TAIL(head, elm, field) do { \
  508. QMD_TAILQ_CHECK_TAIL(head, field); \
  509. TAILQ_NEXT((elm), field) = NULL; \
  510. (elm)->field.tqe_prev = (head)->tqh_last; \
  511. *(head)->tqh_last = (elm); \
  512. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  513. QMD_TRACE_HEAD(head); \
  514. QMD_TRACE_ELEM(&(elm)->field); \
  515. } while (0)
  516. #define TAILQ_LAST(head, headname) \
  517. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  518. #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  519. #define TAILQ_PREV(elm, headname, field) \
  520. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  521. #define TAILQ_REMOVE(head, elm, field) do { \
  522. QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \
  523. QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \
  524. QMD_TAILQ_CHECK_NEXT(elm, field); \
  525. QMD_TAILQ_CHECK_PREV(elm, field); \
  526. if ((TAILQ_NEXT((elm), field)) != NULL) \
  527. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  528. (elm)->field.tqe_prev; \
  529. else { \
  530. (head)->tqh_last = (elm)->field.tqe_prev; \
  531. QMD_TRACE_HEAD(head); \
  532. } \
  533. *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
  534. TRASHIT(*oldnext); \
  535. TRASHIT(*oldprev); \
  536. QMD_TRACE_ELEM(&(elm)->field); \
  537. } while (0)
  538. #define TAILQ_SWAP(head1, head2, type, field) do { \
  539. struct type *swap_first = (head1)->tqh_first; \
  540. struct type **swap_last = (head1)->tqh_last; \
  541. (head1)->tqh_first = (head2)->tqh_first; \
  542. (head1)->tqh_last = (head2)->tqh_last; \
  543. (head2)->tqh_first = swap_first; \
  544. (head2)->tqh_last = swap_last; \
  545. if ((swap_first = (head1)->tqh_first) != NULL) \
  546. swap_first->field.tqe_prev = &(head1)->tqh_first; \
  547. else \
  548. (head1)->tqh_last = &(head1)->tqh_first; \
  549. if ((swap_first = (head2)->tqh_first) != NULL) \
  550. swap_first->field.tqe_prev = &(head2)->tqh_first; \
  551. else \
  552. (head2)->tqh_last = &(head2)->tqh_first; \
  553. } while (0)
  554. #ifdef __cplusplus
  555. }
  556. #endif
  557. #endif /* !_SYS_QUEUE_H_ */