user_queue.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. * 3. 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. */
  30. #ifndef _USER_QUEUE_H_
  31. #define _USER_QUEUE_H_
  32. /*
  33. * This file defines four types of data structures: singly-linked lists,
  34. * singly-linked tail queues, lists and tail queues.
  35. *
  36. * A singly-linked list is headed by a single forward pointer. The elements
  37. * are singly linked for minimum space and pointer manipulation overhead at
  38. * the expense of O(n) removal for arbitrary elements. New elements can be
  39. * added to the list after an existing element or at the head of the list.
  40. * Elements being removed from the head of the list should use the explicit
  41. * macro for this purpose for optimum efficiency. A singly-linked list may
  42. * only be traversed in the forward direction. Singly-linked lists are ideal
  43. * for applications with large datasets and few or no removals or for
  44. * implementing a LIFO queue.
  45. *
  46. * A singly-linked tail queue is headed by a pair of pointers, one to the
  47. * head of the list and the other to the tail of the list. The elements are
  48. * singly linked for minimum space and pointer manipulation overhead at the
  49. * expense of O(n) removal for arbitrary elements. New elements can be added
  50. * to the list after an existing element, at the head of the list, or at the
  51. * end of the list. Elements being removed from the head of the tail queue
  52. * should use the explicit macro for this purpose for optimum efficiency.
  53. * A singly-linked tail queue may only be traversed in the forward direction.
  54. * Singly-linked tail queues are ideal for applications with large datasets
  55. * and few or no removals or for implementing a FIFO queue.
  56. *
  57. * A list is headed by a single forward pointer (or an array of forward
  58. * pointers for a hash table header). The elements are doubly linked
  59. * 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
  61. * or after an existing element or at the head of the list. A list
  62. * may only be traversed in the forward direction.
  63. *
  64. * A tail 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
  68. * after an existing element, at the head of the list, or at the end of
  69. * the list. A tail queue may be traversed in either direction.
  70. *
  71. * For details on the use of these macros, see the queue(3) manual page.
  72. *
  73. *
  74. * SLIST LIST STAILQ TAILQ
  75. * _HEAD + + + +
  76. * _HEAD_INITIALIZER + + + +
  77. * _ENTRY + + + +
  78. * _INIT + + + +
  79. * _EMPTY + + + +
  80. * _FIRST + + + +
  81. * _NEXT + + + +
  82. * _PREV - - - +
  83. * _LAST - - + +
  84. * _FOREACH + + + +
  85. * _FOREACH_SAFE + + + +
  86. * _FOREACH_REVERSE - - - +
  87. * _FOREACH_REVERSE_SAFE - - - +
  88. * _INSERT_HEAD + + + +
  89. * _INSERT_BEFORE - + - +
  90. * _INSERT_AFTER + + + +
  91. * _INSERT_TAIL - - + +
  92. * _CONCAT - - + +
  93. * _REMOVE_AFTER + - + -
  94. * _REMOVE_HEAD + - + -
  95. * _REMOVE + + + +
  96. * _SWAP + + + +
  97. *
  98. */
  99. #ifdef QUEUE_MACRO_DEBUG
  100. /* Store the last 2 places the queue element or head was altered */
  101. struct qm_trace {
  102. char * lastfile;
  103. int lastline;
  104. char * prevfile;
  105. int prevline;
  106. };
  107. #define TRACEBUF struct qm_trace trace;
  108. #define TRASHIT(x) do {(x) = (void *)-1;} while (0)
  109. #define QMD_SAVELINK(name, link) void **name = (void *)&(link)
  110. #define QMD_TRACE_HEAD(head) do { \
  111. (head)->trace.prevline = (head)->trace.lastline; \
  112. (head)->trace.prevfile = (head)->trace.lastfile; \
  113. (head)->trace.lastline = __LINE__; \
  114. (head)->trace.lastfile = __FILE__; \
  115. } while (0)
  116. #define QMD_TRACE_ELEM(elem) do { \
  117. (elem)->trace.prevline = (elem)->trace.lastline; \
  118. (elem)->trace.prevfile = (elem)->trace.lastfile; \
  119. (elem)->trace.lastline = __LINE__; \
  120. (elem)->trace.lastfile = __FILE__; \
  121. } while (0)
  122. #else
  123. #define QMD_TRACE_ELEM(elem)
  124. #define QMD_TRACE_HEAD(head)
  125. #define QMD_SAVELINK(name, link)
  126. #define TRACEBUF
  127. #define TRASHIT(x)
  128. #endif /* QUEUE_MACRO_DEBUG */
  129. /*
  130. * Singly-linked List declarations.
  131. */
  132. #define SLIST_HEAD(name, type) \
  133. struct name { \
  134. struct type *slh_first; /* first element */ \
  135. }
  136. #define SLIST_HEAD_INITIALIZER(head) \
  137. { NULL }
  138. #if defined(_WIN32)
  139. #if defined(SLIST_ENTRY)
  140. #undef SLIST_ENTRY
  141. #endif
  142. #endif
  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. #define SLIST_SWAP(head1, head2, type) do { \
  197. struct type *swap_first = SLIST_FIRST(head1); \
  198. SLIST_FIRST(head1) = SLIST_FIRST(head2); \
  199. SLIST_FIRST(head2) = swap_first; \
  200. } while (0)
  201. /*
  202. * Singly-linked Tail queue declarations.
  203. */
  204. #define STAILQ_HEAD(name, type) \
  205. struct name { \
  206. struct type *stqh_first;/* first element */ \
  207. struct type **stqh_last;/* addr of last next element */ \
  208. }
  209. #define STAILQ_HEAD_INITIALIZER(head) \
  210. { NULL, &(head).stqh_first }
  211. #define STAILQ_ENTRY(type) \
  212. struct { \
  213. struct type *stqe_next; /* next element */ \
  214. }
  215. /*
  216. * Singly-linked Tail queue functions.
  217. */
  218. #define STAILQ_CONCAT(head1, head2) do { \
  219. if (!STAILQ_EMPTY((head2))) { \
  220. *(head1)->stqh_last = (head2)->stqh_first; \
  221. (head1)->stqh_last = (head2)->stqh_last; \
  222. STAILQ_INIT((head2)); \
  223. } \
  224. } while (0)
  225. #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
  226. #define STAILQ_FIRST(head) ((head)->stqh_first)
  227. #define STAILQ_FOREACH(var, head, field) \
  228. for((var) = STAILQ_FIRST((head)); \
  229. (var); \
  230. (var) = STAILQ_NEXT((var), field))
  231. #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
  232. for ((var) = STAILQ_FIRST((head)); \
  233. (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
  234. (var) = (tvar))
  235. #define STAILQ_INIT(head) do { \
  236. STAILQ_FIRST((head)) = NULL; \
  237. (head)->stqh_last = &STAILQ_FIRST((head)); \
  238. } while (0)
  239. #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
  240. if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
  241. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  242. STAILQ_NEXT((tqelm), field) = (elm); \
  243. } while (0)
  244. #define STAILQ_INSERT_HEAD(head, elm, field) do { \
  245. if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
  246. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  247. STAILQ_FIRST((head)) = (elm); \
  248. } while (0)
  249. #define STAILQ_INSERT_TAIL(head, elm, field) do { \
  250. STAILQ_NEXT((elm), field) = NULL; \
  251. *(head)->stqh_last = (elm); \
  252. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  253. } while (0)
  254. #define STAILQ_LAST(head, type, field) \
  255. (STAILQ_EMPTY((head)) ? \
  256. NULL : \
  257. ((struct type *)(void *) \
  258. ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
  259. #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
  260. #define STAILQ_REMOVE(head, elm, type, field) do { \
  261. QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \
  262. if (STAILQ_FIRST((head)) == (elm)) { \
  263. STAILQ_REMOVE_HEAD((head), field); \
  264. } \
  265. else { \
  266. struct type *curelm = STAILQ_FIRST((head)); \
  267. while (STAILQ_NEXT(curelm, field) != (elm)) \
  268. curelm = STAILQ_NEXT(curelm, field); \
  269. STAILQ_REMOVE_AFTER(head, curelm, field); \
  270. } \
  271. TRASHIT(*oldnext); \
  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_REMOVE_HEAD(head, field) do { \
  279. if ((STAILQ_FIRST((head)) = \
  280. STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
  281. (head)->stqh_last = &STAILQ_FIRST((head)); \
  282. } while (0)
  283. #define STAILQ_SWAP(head1, head2, type) do { \
  284. struct type *swap_first = STAILQ_FIRST(head1); \
  285. struct type **swap_last = (head1)->stqh_last; \
  286. STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
  287. (head1)->stqh_last = (head2)->stqh_last; \
  288. STAILQ_FIRST(head2) = swap_first; \
  289. (head2)->stqh_last = swap_last; \
  290. if (STAILQ_EMPTY(head1)) \
  291. (head1)->stqh_last = &STAILQ_FIRST(head1); \
  292. if (STAILQ_EMPTY(head2)) \
  293. (head2)->stqh_last = &STAILQ_FIRST(head2); \
  294. } while (0)
  295. /*
  296. * List declarations.
  297. */
  298. #define LIST_HEAD(name, type) \
  299. struct name { \
  300. struct type *lh_first; /* first element */ \
  301. }
  302. #define LIST_HEAD_INITIALIZER(head) \
  303. { NULL }
  304. #define LIST_ENTRY(type) \
  305. struct { \
  306. struct type *le_next; /* next element */ \
  307. struct type **le_prev; /* address of previous next element */ \
  308. }
  309. /*
  310. * List functions.
  311. */
  312. #if defined(INVARIANTS)
  313. #define QMD_LIST_CHECK_HEAD(head, field) do { \
  314. if (LIST_FIRST((head)) != NULL && \
  315. LIST_FIRST((head))->field.le_prev != \
  316. &LIST_FIRST((head))) \
  317. panic("Bad list head %p first->prev != head", (void *)(head)); \
  318. } while (0)
  319. #define QMD_LIST_CHECK_NEXT(elm, field) do { \
  320. if (LIST_NEXT((elm), field) != NULL && \
  321. LIST_NEXT((elm), field)->field.le_prev != \
  322. &((elm)->field.le_next)) \
  323. panic("Bad link elm %p next->prev != elm", (void *)(elm)); \
  324. } while (0)
  325. #define QMD_LIST_CHECK_PREV(elm, field) do { \
  326. if (*(elm)->field.le_prev != (elm)) \
  327. panic("Bad link elm %p prev->next != elm", (void *)(elm)); \
  328. } while (0)
  329. #else
  330. #define QMD_LIST_CHECK_HEAD(head, field)
  331. #define QMD_LIST_CHECK_NEXT(elm, field)
  332. #define QMD_LIST_CHECK_PREV(elm, field)
  333. #endif /* (INVARIANTS) */
  334. #define LIST_EMPTY(head) ((head)->lh_first == NULL)
  335. #define LIST_FIRST(head) ((head)->lh_first)
  336. #define LIST_FOREACH(var, head, field) \
  337. for ((var) = LIST_FIRST((head)); \
  338. (var); \
  339. (var) = LIST_NEXT((var), field))
  340. #define LIST_FOREACH_SAFE(var, head, field, tvar) \
  341. for ((var) = LIST_FIRST((head)); \
  342. (var) && ((tvar) = LIST_NEXT((var), field), 1); \
  343. (var) = (tvar))
  344. #define LIST_INIT(head) do { \
  345. LIST_FIRST((head)) = NULL; \
  346. } while (0)
  347. #define LIST_INSERT_AFTER(listelm, elm, field) do { \
  348. QMD_LIST_CHECK_NEXT(listelm, field); \
  349. if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
  350. LIST_NEXT((listelm), field)->field.le_prev = \
  351. &LIST_NEXT((elm), field); \
  352. LIST_NEXT((listelm), field) = (elm); \
  353. (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
  354. } while (0)
  355. #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
  356. QMD_LIST_CHECK_PREV(listelm, field); \
  357. (elm)->field.le_prev = (listelm)->field.le_prev; \
  358. LIST_NEXT((elm), field) = (listelm); \
  359. *(listelm)->field.le_prev = (elm); \
  360. (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
  361. } while (0)
  362. #define LIST_INSERT_HEAD(head, elm, field) do { \
  363. QMD_LIST_CHECK_HEAD((head), field); \
  364. if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
  365. LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
  366. LIST_FIRST((head)) = (elm); \
  367. (elm)->field.le_prev = &LIST_FIRST((head)); \
  368. } while (0)
  369. #define LIST_NEXT(elm, field) ((elm)->field.le_next)
  370. #define LIST_REMOVE(elm, field) do { \
  371. QMD_SAVELINK(oldnext, (elm)->field.le_next); \
  372. QMD_SAVELINK(oldprev, (elm)->field.le_prev); \
  373. QMD_LIST_CHECK_NEXT(elm, field); \
  374. QMD_LIST_CHECK_PREV(elm, field); \
  375. if (LIST_NEXT((elm), field) != NULL) \
  376. LIST_NEXT((elm), field)->field.le_prev = \
  377. (elm)->field.le_prev; \
  378. *(elm)->field.le_prev = LIST_NEXT((elm), field); \
  379. TRASHIT(*oldnext); \
  380. TRASHIT(*oldprev); \
  381. } while (0)
  382. #define LIST_SWAP(head1, head2, type, field) do { \
  383. struct type *swap_tmp = LIST_FIRST((head1)); \
  384. LIST_FIRST((head1)) = LIST_FIRST((head2)); \
  385. LIST_FIRST((head2)) = swap_tmp; \
  386. if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
  387. swap_tmp->field.le_prev = &LIST_FIRST((head1)); \
  388. if ((swap_tmp = LIST_FIRST((head2))) != NULL) \
  389. swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
  390. } while (0)
  391. /*
  392. * Tail queue declarations.
  393. */
  394. #define TAILQ_HEAD(name, type) \
  395. struct name { \
  396. struct type *tqh_first; /* first element */ \
  397. struct type **tqh_last; /* addr of last next element */ \
  398. TRACEBUF \
  399. }
  400. #define TAILQ_HEAD_INITIALIZER(head) \
  401. { NULL, &(head).tqh_first }
  402. #define TAILQ_ENTRY(type) \
  403. struct { \
  404. struct type *tqe_next; /* next element */ \
  405. struct type **tqe_prev; /* address of previous next element */ \
  406. TRACEBUF \
  407. }
  408. /*
  409. * Tail queue functions.
  410. */
  411. #if defined(INVARIANTS)
  412. #define QMD_TAILQ_CHECK_HEAD(head, field) do { \
  413. if (!TAILQ_EMPTY(head) && \
  414. TAILQ_FIRST((head))->field.tqe_prev != \
  415. &TAILQ_FIRST((head))) \
  416. panic("Bad tailq head %p first->prev != head", (void *)(head)); \
  417. } while (0)
  418. #define QMD_TAILQ_CHECK_TAIL(head, field) do { \
  419. if (*(head)->tqh_last != NULL) \
  420. panic("Bad tailq NEXT(%p->tqh_last) != NULL", (void *)(head)); \
  421. } while (0)
  422. #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
  423. if (TAILQ_NEXT((elm), field) != NULL && \
  424. TAILQ_NEXT((elm), field)->field.tqe_prev != \
  425. &((elm)->field.tqe_next)) \
  426. panic("Bad link elm %p next->prev != elm", (void *)(elm)); \
  427. } while (0)
  428. #define QMD_TAILQ_CHECK_PREV(elm, field) do { \
  429. if (*(elm)->field.tqe_prev != (elm)) \
  430. panic("Bad link elm %p prev->next != elm", (void *)(elm)); \
  431. } while (0)
  432. #else
  433. #define QMD_TAILQ_CHECK_HEAD(head, field)
  434. #define QMD_TAILQ_CHECK_TAIL(head, headname)
  435. #define QMD_TAILQ_CHECK_NEXT(elm, field)
  436. #define QMD_TAILQ_CHECK_PREV(elm, field)
  437. #endif /* (INVARIANTS) */
  438. #define TAILQ_CONCAT(head1, head2, field) do { \
  439. if (!TAILQ_EMPTY(head2)) { \
  440. *(head1)->tqh_last = (head2)->tqh_first; \
  441. (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
  442. (head1)->tqh_last = (head2)->tqh_last; \
  443. TAILQ_INIT((head2)); \
  444. QMD_TRACE_HEAD(head1); \
  445. QMD_TRACE_HEAD(head2); \
  446. } \
  447. } while (0)
  448. #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
  449. #define TAILQ_FIRST(head) ((head)->tqh_first)
  450. #define TAILQ_FOREACH(var, head, field) \
  451. for ((var) = TAILQ_FIRST((head)); \
  452. (var); \
  453. (var) = TAILQ_NEXT((var), field))
  454. #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
  455. for ((var) = TAILQ_FIRST((head)); \
  456. (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
  457. (var) = (tvar))
  458. #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  459. for ((var) = TAILQ_LAST((head), headname); \
  460. (var); \
  461. (var) = TAILQ_PREV((var), headname, field))
  462. #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
  463. for ((var) = TAILQ_LAST((head), headname); \
  464. (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
  465. (var) = (tvar))
  466. #define TAILQ_INIT(head) do { \
  467. TAILQ_FIRST((head)) = NULL; \
  468. (head)->tqh_last = &TAILQ_FIRST((head)); \
  469. QMD_TRACE_HEAD(head); \
  470. } while (0)
  471. #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  472. QMD_TAILQ_CHECK_NEXT(listelm, field); \
  473. if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
  474. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  475. &TAILQ_NEXT((elm), field); \
  476. else { \
  477. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  478. QMD_TRACE_HEAD(head); \
  479. } \
  480. TAILQ_NEXT((listelm), field) = (elm); \
  481. (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
  482. QMD_TRACE_ELEM(&(elm)->field); \
  483. QMD_TRACE_ELEM(&listelm->field); \
  484. } while (0)
  485. #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  486. QMD_TAILQ_CHECK_PREV(listelm, field); \
  487. (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  488. TAILQ_NEXT((elm), field) = (listelm); \
  489. *(listelm)->field.tqe_prev = (elm); \
  490. (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
  491. QMD_TRACE_ELEM(&(elm)->field); \
  492. QMD_TRACE_ELEM(&listelm->field); \
  493. } while (0)
  494. #define TAILQ_INSERT_HEAD(head, elm, field) do { \
  495. QMD_TAILQ_CHECK_HEAD(head, field); \
  496. if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
  497. TAILQ_FIRST((head))->field.tqe_prev = \
  498. &TAILQ_NEXT((elm), field); \
  499. else \
  500. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  501. TAILQ_FIRST((head)) = (elm); \
  502. (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
  503. QMD_TRACE_HEAD(head); \
  504. QMD_TRACE_ELEM(&(elm)->field); \
  505. } while (0)
  506. #define TAILQ_INSERT_TAIL(head, elm, field) do { \
  507. QMD_TAILQ_CHECK_TAIL(head, field); \
  508. TAILQ_NEXT((elm), field) = NULL; \
  509. (elm)->field.tqe_prev = (head)->tqh_last; \
  510. *(head)->tqh_last = (elm); \
  511. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  512. QMD_TRACE_HEAD(head); \
  513. QMD_TRACE_ELEM(&(elm)->field); \
  514. } while (0)
  515. #define TAILQ_LAST(head, headname) \
  516. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  517. #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  518. #define TAILQ_PREV(elm, headname, field) \
  519. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  520. #define TAILQ_REMOVE(head, elm, field) do { \
  521. QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \
  522. QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \
  523. QMD_TAILQ_CHECK_NEXT(elm, field); \
  524. QMD_TAILQ_CHECK_PREV(elm, field); \
  525. if ((TAILQ_NEXT((elm), field)) != NULL) \
  526. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  527. (elm)->field.tqe_prev; \
  528. else { \
  529. (head)->tqh_last = (elm)->field.tqe_prev; \
  530. QMD_TRACE_HEAD(head); \
  531. } \
  532. *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
  533. TRASHIT(*oldnext); \
  534. TRASHIT(*oldprev); \
  535. QMD_TRACE_ELEM(&(elm)->field); \
  536. } while (0)
  537. #define TAILQ_SWAP(head1, head2, type, field) do { \
  538. struct type *swap_first = (head1)->tqh_first; \
  539. struct type **swap_last = (head1)->tqh_last; \
  540. (head1)->tqh_first = (head2)->tqh_first; \
  541. (head1)->tqh_last = (head2)->tqh_last; \
  542. (head2)->tqh_first = swap_first; \
  543. (head2)->tqh_last = swap_last; \
  544. if ((swap_first = (head1)->tqh_first) != NULL) \
  545. swap_first->field.tqe_prev = &(head1)->tqh_first; \
  546. else \
  547. (head1)->tqh_last = &(head1)->tqh_first; \
  548. if ((swap_first = (head2)->tqh_first) != NULL) \
  549. swap_first->field.tqe_prev = &(head2)->tqh_first; \
  550. else \
  551. (head2)->tqh_last = &(head2)->tqh_first; \
  552. } while (0)
  553. #endif /* !_SYS_QUEUE_H_ */