queue.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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: src/sys/sys/queue.h,v 1.68 2006/10/24 11:20:29 ru Exp $
  31. */
  32. #ifndef _SYS_QUEUE_H_
  33. #define _SYS_QUEUE_H_
  34. #include <sys/cdefs.h>
  35. /*
  36. * This file defines four types of data structures: singly-linked lists,
  37. * singly-linked tail queues, lists and tail queues.
  38. *
  39. * A singly-linked list is headed by a single forward pointer. The elements
  40. * are singly linked for minimum space and pointer manipulation overhead at
  41. * the expense of O(n) removal for arbitrary elements. New elements can be
  42. * added to the list after an existing element or at the head of the list.
  43. * Elements being removed from the head of the list should use the explicit
  44. * macro for this purpose for optimum efficiency. A singly-linked list may
  45. * only be traversed in the forward direction. Singly-linked lists are ideal
  46. * for applications with large datasets and few or no removals or for
  47. * implementing a LIFO queue.
  48. *
  49. * A singly-linked tail queue is headed by a pair of pointers, one to the
  50. * head of the list and the other to the tail of the list. The elements are
  51. * singly linked for minimum space and pointer manipulation overhead at the
  52. * expense of O(n) removal for arbitrary elements. New elements can be added
  53. * to the list after an existing element, at the head of the list, or at the
  54. * end of the list. Elements being removed from the head of the tail queue
  55. * should use the explicit macro for this purpose for optimum efficiency.
  56. * A singly-linked tail queue may only be traversed in the forward direction.
  57. * Singly-linked tail queues are ideal for applications with large datasets
  58. * and few or no removals or for implementing a FIFO queue.
  59. *
  60. * A list is headed by a single forward pointer (or an array of forward
  61. * pointers for a hash table header). The elements are doubly linked
  62. * so that an arbitrary element can be removed without a need to
  63. * traverse the list. New elements can be added to the list before
  64. * or after an existing element or at the head of the list. A list
  65. * 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. *
  77. * SLIST LIST STAILQ TAILQ
  78. * _HEAD + + + +
  79. * _HEAD_INITIALIZER + + + +
  80. * _ENTRY + + + +
  81. * _INIT + + + +
  82. * _EMPTY + + + +
  83. * _FIRST + + + +
  84. * _NEXT + + + +
  85. * _PREV - - - +
  86. * _LAST - - + +
  87. * _FOREACH + + + +
  88. * _FOREACH_SAFE + + + +
  89. * _FOREACH_REVERSE - - - +
  90. * _FOREACH_REVERSE_SAFE - - - +
  91. * _INSERT_HEAD + + + +
  92. * _INSERT_BEFORE - + - +
  93. * _INSERT_AFTER + + + +
  94. * _INSERT_TAIL - - + +
  95. * _CONCAT - - + +
  96. * _REMOVE_HEAD + - + -
  97. * _REMOVE + + + +
  98. *
  99. */
  100. #ifdef QUEUE_MACRO_DEBUG
  101. /* Store the last 2 places the queue element or head was altered */
  102. struct qm_trace {
  103. char * lastfile;
  104. int lastline;
  105. char * prevfile;
  106. int prevline;
  107. };
  108. #define TRACEBUF struct qm_trace trace;
  109. #define TRASHIT(x) do {(x) = (void *)-1;} while (0)
  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 TRACEBUF
  126. #define TRASHIT(x)
  127. #endif /* QUEUE_MACRO_DEBUG */
  128. /*
  129. * Singly-linked List declarations.
  130. */
  131. #define SLIST_HEAD(name, type) \
  132. struct name { \
  133. struct type *slh_first; /* first element */ \
  134. }
  135. #define SLIST_HEAD_INITIALIZER(head) \
  136. { NULL }
  137. #define SLIST_ENTRY(type) \
  138. struct { \
  139. struct type *sle_next; /* next element */ \
  140. }
  141. /*
  142. * Singly-linked List functions.
  143. */
  144. #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
  145. #define SLIST_FIRST(head) ((head)->slh_first)
  146. #define SLIST_FOREACH(var, head, field) \
  147. for ((var) = SLIST_FIRST((head)); \
  148. (var); \
  149. (var) = SLIST_NEXT((var), field))
  150. #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
  151. for ((var) = SLIST_FIRST((head)); \
  152. (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
  153. (var) = (tvar))
  154. #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  155. for ((varp) = &SLIST_FIRST((head)); \
  156. ((var) = *(varp)) != NULL; \
  157. (varp) = &SLIST_NEXT((var), field))
  158. #define SLIST_INIT(head) do { \
  159. SLIST_FIRST((head)) = NULL; \
  160. } while (0)
  161. #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  162. SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
  163. SLIST_NEXT((slistelm), field) = (elm); \
  164. } while (0)
  165. #define SLIST_INSERT_HEAD(head, elm, field) do { \
  166. SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
  167. SLIST_FIRST((head)) = (elm); \
  168. } while (0)
  169. #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  170. #define SLIST_REMOVE(head, elm, type, field) do { \
  171. if (SLIST_FIRST((head)) == (elm)) { \
  172. SLIST_REMOVE_HEAD((head), field); \
  173. } \
  174. else { \
  175. struct type *curelm = SLIST_FIRST((head)); \
  176. while (SLIST_NEXT(curelm, field) != (elm)) \
  177. curelm = SLIST_NEXT(curelm, field); \
  178. SLIST_NEXT(curelm, field) = \
  179. SLIST_NEXT(SLIST_NEXT(curelm, field), field); \
  180. } \
  181. TRASHIT((elm)->field.sle_next); \
  182. } while (0)
  183. #define SLIST_REMOVE_HEAD(head, field) do { \
  184. SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
  185. } while (0)
  186. /*
  187. * Singly-linked Tail queue declarations.
  188. */
  189. #define STAILQ_HEAD(name, type) \
  190. struct name { \
  191. struct type *stqh_first;/* first element */ \
  192. struct type **stqh_last;/* addr of last next element */ \
  193. }
  194. #define STAILQ_HEAD_INITIALIZER(head) \
  195. { NULL, &(head).stqh_first }
  196. #define STAILQ_ENTRY(type) \
  197. struct { \
  198. struct type *stqe_next; /* next element */ \
  199. }
  200. /*
  201. * Singly-linked Tail queue functions.
  202. */
  203. #define STAILQ_CONCAT(head1, head2) do { \
  204. if (!STAILQ_EMPTY((head2))) { \
  205. *(head1)->stqh_last = (head2)->stqh_first; \
  206. (head1)->stqh_last = (head2)->stqh_last; \
  207. STAILQ_INIT((head2)); \
  208. } \
  209. } while (0)
  210. #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
  211. #define STAILQ_FIRST(head) ((head)->stqh_first)
  212. #define STAILQ_FOREACH(var, head, field) \
  213. for((var) = STAILQ_FIRST((head)); \
  214. (var); \
  215. (var) = STAILQ_NEXT((var), field))
  216. #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
  217. for ((var) = STAILQ_FIRST((head)); \
  218. (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
  219. (var) = (tvar))
  220. #define STAILQ_INIT(head) do { \
  221. STAILQ_FIRST((head)) = NULL; \
  222. (head)->stqh_last = &STAILQ_FIRST((head)); \
  223. } while (0)
  224. #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
  225. if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
  226. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  227. STAILQ_NEXT((tqelm), field) = (elm); \
  228. } while (0)
  229. #define STAILQ_INSERT_HEAD(head, elm, field) do { \
  230. if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
  231. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  232. STAILQ_FIRST((head)) = (elm); \
  233. } while (0)
  234. #define STAILQ_INSERT_TAIL(head, elm, field) do { \
  235. STAILQ_NEXT((elm), field) = NULL; \
  236. *(head)->stqh_last = (elm); \
  237. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  238. } while (0)
  239. #define STAILQ_LAST(head, type, field) \
  240. (STAILQ_EMPTY((head)) ? \
  241. NULL : \
  242. ((struct type *)(void *) \
  243. ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
  244. #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
  245. #define STAILQ_REMOVE(head, elm, type, field) do { \
  246. if (STAILQ_FIRST((head)) == (elm)) { \
  247. STAILQ_REMOVE_HEAD((head), field); \
  248. } \
  249. else { \
  250. struct type *curelm = STAILQ_FIRST((head)); \
  251. while (STAILQ_NEXT(curelm, field) != (elm)) \
  252. curelm = STAILQ_NEXT(curelm, field); \
  253. if ((STAILQ_NEXT(curelm, field) = \
  254. STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
  255. (head)->stqh_last = &STAILQ_NEXT((curelm), field);\
  256. } \
  257. TRASHIT((elm)->field.stqe_next); \
  258. } while (0)
  259. #define STAILQ_REMOVE_HEAD(head, field) do { \
  260. if ((STAILQ_FIRST((head)) = \
  261. STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
  262. (head)->stqh_last = &STAILQ_FIRST((head)); \
  263. } while (0)
  264. /*
  265. * List declarations.
  266. */
  267. #define LIST_HEAD(name, type) \
  268. struct name { \
  269. struct type *lh_first; /* first element */ \
  270. }
  271. #define LIST_HEAD_INITIALIZER(head) \
  272. { NULL }
  273. #define LIST_ENTRY(type) \
  274. struct { \
  275. struct type *le_next; /* next element */ \
  276. struct type **le_prev; /* address of previous next element */ \
  277. }
  278. /*
  279. * List functions.
  280. */
  281. #if (defined(_KERNEL) && defined(INVARIANTS))
  282. #define QMD_LIST_CHECK_HEAD(head, field) do { \
  283. if (LIST_FIRST((head)) != NULL && \
  284. LIST_FIRST((head))->field.le_prev != \
  285. &LIST_FIRST((head))) \
  286. panic("Bad list head %p first->prev != head", (head)); \
  287. } while (0)
  288. #define QMD_LIST_CHECK_NEXT(elm, field) do { \
  289. if (LIST_NEXT((elm), field) != NULL && \
  290. LIST_NEXT((elm), field)->field.le_prev != \
  291. &((elm)->field.le_next)) \
  292. panic("Bad link elm %p next->prev != elm", (elm)); \
  293. } while (0)
  294. #define QMD_LIST_CHECK_PREV(elm, field) do { \
  295. if (*(elm)->field.le_prev != (elm)) \
  296. panic("Bad link elm %p prev->next != elm", (elm)); \
  297. } while (0)
  298. #else
  299. #define QMD_LIST_CHECK_HEAD(head, field)
  300. #define QMD_LIST_CHECK_NEXT(elm, field)
  301. #define QMD_LIST_CHECK_PREV(elm, field)
  302. #endif /* (_KERNEL && INVARIANTS) */
  303. #define LIST_EMPTY(head) ((head)->lh_first == NULL)
  304. #define LIST_FIRST(head) ((head)->lh_first)
  305. #define LIST_FOREACH(var, head, field) \
  306. for ((var) = LIST_FIRST((head)); \
  307. (var); \
  308. (var) = LIST_NEXT((var), field))
  309. #define LIST_FOREACH_SAFE(var, head, field, tvar) \
  310. for ((var) = LIST_FIRST((head)); \
  311. (var) && ((tvar) = LIST_NEXT((var), field), 1); \
  312. (var) = (tvar))
  313. #define LIST_INIT(head) do { \
  314. LIST_FIRST((head)) = NULL; \
  315. } while (0)
  316. #define LIST_INSERT_AFTER(listelm, elm, field) do { \
  317. QMD_LIST_CHECK_NEXT(listelm, field); \
  318. if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
  319. LIST_NEXT((listelm), field)->field.le_prev = \
  320. &LIST_NEXT((elm), field); \
  321. LIST_NEXT((listelm), field) = (elm); \
  322. (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
  323. } while (0)
  324. #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
  325. QMD_LIST_CHECK_PREV(listelm, field); \
  326. (elm)->field.le_prev = (listelm)->field.le_prev; \
  327. LIST_NEXT((elm), field) = (listelm); \
  328. *(listelm)->field.le_prev = (elm); \
  329. (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
  330. } while (0)
  331. #define LIST_INSERT_HEAD(head, elm, field) do { \
  332. QMD_LIST_CHECK_HEAD((head), field); \
  333. if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
  334. LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
  335. LIST_FIRST((head)) = (elm); \
  336. (elm)->field.le_prev = &LIST_FIRST((head)); \
  337. } while (0)
  338. #define LIST_NEXT(elm, field) ((elm)->field.le_next)
  339. #define LIST_REMOVE(elm, field) do { \
  340. QMD_LIST_CHECK_NEXT(elm, field); \
  341. QMD_LIST_CHECK_PREV(elm, field); \
  342. if (LIST_NEXT((elm), field) != NULL) \
  343. LIST_NEXT((elm), field)->field.le_prev = \
  344. (elm)->field.le_prev; \
  345. *(elm)->field.le_prev = LIST_NEXT((elm), field); \
  346. TRASHIT((elm)->field.le_next); \
  347. TRASHIT((elm)->field.le_prev); \
  348. } while (0)
  349. /*
  350. * Tail queue declarations.
  351. */
  352. #define TAILQ_HEAD(name, type) \
  353. struct name { \
  354. struct type *tqh_first; /* first element */ \
  355. struct type **tqh_last; /* addr of last next element */ \
  356. TRACEBUF \
  357. }
  358. #define TAILQ_HEAD_INITIALIZER(head) \
  359. { NULL, &(head).tqh_first }
  360. #define TAILQ_ENTRY(type) \
  361. struct { \
  362. struct type *tqe_next; /* next element */ \
  363. struct type **tqe_prev; /* address of previous next element */ \
  364. TRACEBUF \
  365. }
  366. /*
  367. * Tail queue functions.
  368. */
  369. #if (defined(_KERNEL) && defined(INVARIANTS))
  370. #define QMD_TAILQ_CHECK_HEAD(head, field) do { \
  371. if (!TAILQ_EMPTY(head) && \
  372. TAILQ_FIRST((head))->field.tqe_prev != \
  373. &TAILQ_FIRST((head))) \
  374. panic("Bad tailq head %p first->prev != head", (head)); \
  375. } while (0)
  376. #define QMD_TAILQ_CHECK_TAIL(head, field) do { \
  377. if (*(head)->tqh_last != NULL) \
  378. panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \
  379. } while (0)
  380. #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
  381. if (TAILQ_NEXT((elm), field) != NULL && \
  382. TAILQ_NEXT((elm), field)->field.tqe_prev != \
  383. &((elm)->field.tqe_next)) \
  384. panic("Bad link elm %p next->prev != elm", (elm)); \
  385. } while (0)
  386. #define QMD_TAILQ_CHECK_PREV(elm, field) do { \
  387. if (*(elm)->field.tqe_prev != (elm)) \
  388. panic("Bad link elm %p prev->next != elm", (elm)); \
  389. } while (0)
  390. #else
  391. #define QMD_TAILQ_CHECK_HEAD(head, field)
  392. #define QMD_TAILQ_CHECK_TAIL(head, headname)
  393. #define QMD_TAILQ_CHECK_NEXT(elm, field)
  394. #define QMD_TAILQ_CHECK_PREV(elm, field)
  395. #endif /* (_KERNEL && INVARIANTS) */
  396. #define TAILQ_CONCAT(head1, head2, field) do { \
  397. if (!TAILQ_EMPTY(head2)) { \
  398. *(head1)->tqh_last = (head2)->tqh_first; \
  399. (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
  400. (head1)->tqh_last = (head2)->tqh_last; \
  401. TAILQ_INIT((head2)); \
  402. QMD_TRACE_HEAD(head1); \
  403. QMD_TRACE_HEAD(head2); \
  404. } \
  405. } while (0)
  406. #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
  407. #define TAILQ_FIRST(head) ((head)->tqh_first)
  408. #define TAILQ_FOREACH(var, head, field) \
  409. for ((var) = TAILQ_FIRST((head)); \
  410. (var); \
  411. (var) = TAILQ_NEXT((var), field))
  412. #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
  413. for ((var) = TAILQ_FIRST((head)); \
  414. (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
  415. (var) = (tvar))
  416. #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  417. for ((var) = TAILQ_LAST((head), headname); \
  418. (var); \
  419. (var) = TAILQ_PREV((var), headname, field))
  420. #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
  421. for ((var) = TAILQ_LAST((head), headname); \
  422. (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
  423. (var) = (tvar))
  424. #define TAILQ_INIT(head) do { \
  425. TAILQ_FIRST((head)) = NULL; \
  426. (head)->tqh_last = &TAILQ_FIRST((head)); \
  427. QMD_TRACE_HEAD(head); \
  428. } while (0)
  429. #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  430. QMD_TAILQ_CHECK_NEXT(listelm, field); \
  431. if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
  432. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  433. &TAILQ_NEXT((elm), field); \
  434. else { \
  435. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  436. QMD_TRACE_HEAD(head); \
  437. } \
  438. TAILQ_NEXT((listelm), field) = (elm); \
  439. (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
  440. QMD_TRACE_ELEM(&(elm)->field); \
  441. QMD_TRACE_ELEM(&listelm->field); \
  442. } while (0)
  443. #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  444. QMD_TAILQ_CHECK_PREV(listelm, field); \
  445. (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  446. TAILQ_NEXT((elm), field) = (listelm); \
  447. *(listelm)->field.tqe_prev = (elm); \
  448. (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
  449. QMD_TRACE_ELEM(&(elm)->field); \
  450. QMD_TRACE_ELEM(&listelm->field); \
  451. } while (0)
  452. #define TAILQ_INSERT_HEAD(head, elm, field) do { \
  453. QMD_TAILQ_CHECK_HEAD(head, field); \
  454. if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
  455. TAILQ_FIRST((head))->field.tqe_prev = \
  456. &TAILQ_NEXT((elm), field); \
  457. else \
  458. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  459. TAILQ_FIRST((head)) = (elm); \
  460. (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
  461. QMD_TRACE_HEAD(head); \
  462. QMD_TRACE_ELEM(&(elm)->field); \
  463. } while (0)
  464. #define TAILQ_INSERT_TAIL(head, elm, field) do { \
  465. QMD_TAILQ_CHECK_TAIL(head, field); \
  466. TAILQ_NEXT((elm), field) = NULL; \
  467. (elm)->field.tqe_prev = (head)->tqh_last; \
  468. *(head)->tqh_last = (elm); \
  469. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  470. QMD_TRACE_HEAD(head); \
  471. QMD_TRACE_ELEM(&(elm)->field); \
  472. } while (0)
  473. #define TAILQ_LAST(head, headname) \
  474. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  475. #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  476. #define TAILQ_PREV(elm, headname, field) \
  477. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  478. #define TAILQ_REMOVE(head, elm, field) do { \
  479. QMD_TAILQ_CHECK_NEXT(elm, field); \
  480. QMD_TAILQ_CHECK_PREV(elm, field); \
  481. if ((TAILQ_NEXT((elm), field)) != NULL) \
  482. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  483. (elm)->field.tqe_prev; \
  484. else { \
  485. (head)->tqh_last = (elm)->field.tqe_prev; \
  486. QMD_TRACE_HEAD(head); \
  487. } \
  488. *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
  489. TRASHIT((elm)->field.tqe_next); \
  490. TRASHIT((elm)->field.tqe_prev); \
  491. QMD_TRACE_ELEM(&(elm)->field); \
  492. } while (0)
  493. #ifdef _KERNEL
  494. /*
  495. * XXX insque() and remque() are an old way of handling certain queues.
  496. * They bogusly assumes that all queue heads look alike.
  497. */
  498. struct quehead {
  499. struct quehead *qh_link;
  500. struct quehead *qh_rlink;
  501. };
  502. #ifdef __CC_SUPPORTS___INLINE
  503. static __inline void
  504. insque(void *a, void *b)
  505. {
  506. struct quehead *element = (struct quehead *)a,
  507. *head = (struct quehead *)b;
  508. element->qh_link = head->qh_link;
  509. element->qh_rlink = head;
  510. head->qh_link = element;
  511. element->qh_link->qh_rlink = element;
  512. }
  513. static __inline void
  514. remque(void *a)
  515. {
  516. struct quehead *element = (struct quehead *)a;
  517. element->qh_link->qh_rlink = element->qh_rlink;
  518. element->qh_rlink->qh_link = element->qh_link;
  519. element->qh_rlink = 0;
  520. }
  521. #else /* !__CC_SUPPORTS___INLINE */
  522. void insque(void *a, void *b);
  523. void remque(void *a);
  524. #endif /* __CC_SUPPORTS___INLINE */
  525. #endif /* _KERNEL */
  526. #endif /* !_SYS_QUEUE_H_ */