sys-queue.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /* $OpenBSD: queue.h,v 1.45 2018/07/12 14:22:54 sashan Exp $ */
  2. /* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
  3. /*
  4. * Copyright (c) 1991, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * @(#)queue.h 8.5 (Berkeley) 8/20/94
  32. */
  33. /* OPENBSD ORIGINAL: sys/sys/queue.h */
  34. #ifndef _FAKE_QUEUE_H_
  35. #define _FAKE_QUEUE_H_
  36. /*
  37. * Require for OS/X and other platforms that have old/broken/incomplete
  38. * <sys/queue.h>.
  39. */
  40. #undef CIRCLEQ_EMPTY
  41. #undef CIRCLEQ_END
  42. #undef CIRCLEQ_ENTRY
  43. #undef CIRCLEQ_FIRST
  44. #undef CIRCLEQ_FOREACH
  45. #undef CIRCLEQ_FOREACH_REVERSE
  46. #undef CIRCLEQ_HEAD
  47. #undef CIRCLEQ_HEAD_INITIALIZER
  48. #undef CIRCLEQ_INIT
  49. #undef CIRCLEQ_INSERT_AFTER
  50. #undef CIRCLEQ_INSERT_BEFORE
  51. #undef CIRCLEQ_INSERT_HEAD
  52. #undef CIRCLEQ_INSERT_TAIL
  53. #undef CIRCLEQ_LAST
  54. #undef CIRCLEQ_NEXT
  55. #undef CIRCLEQ_PREV
  56. #undef CIRCLEQ_REMOVE
  57. #undef CIRCLEQ_REPLACE
  58. #undef LIST_EMPTY
  59. #undef LIST_END
  60. #undef LIST_ENTRY
  61. #undef LIST_FIRST
  62. #undef LIST_FOREACH
  63. #undef LIST_FOREACH_SAFE
  64. #undef LIST_HEAD
  65. #undef LIST_HEAD_INITIALIZER
  66. #undef LIST_INIT
  67. #undef LIST_INSERT_AFTER
  68. #undef LIST_INSERT_BEFORE
  69. #undef LIST_INSERT_HEAD
  70. #undef LIST_NEXT
  71. #undef LIST_REMOVE
  72. #undef LIST_REPLACE
  73. #undef SIMPLEQ_CONCAT
  74. #undef SIMPLEQ_EMPTY
  75. #undef SIMPLEQ_END
  76. #undef SIMPLEQ_ENTRY
  77. #undef SIMPLEQ_FIRST
  78. #undef SIMPLEQ_FOREACH
  79. #undef SIMPLEQ_FOREACH_SAFE
  80. #undef SIMPLEQ_HEAD
  81. #undef SIMPLEQ_HEAD_INITIALIZER
  82. #undef SIMPLEQ_INIT
  83. #undef SIMPLEQ_INSERT_AFTER
  84. #undef SIMPLEQ_INSERT_HEAD
  85. #undef SIMPLEQ_INSERT_TAIL
  86. #undef SIMPLEQ_NEXT
  87. #undef SIMPLEQ_REMOVE_AFTER
  88. #undef SIMPLEQ_REMOVE_HEAD
  89. #undef SLIST_EMPTY
  90. #undef SLIST_END
  91. #undef SLIST_ENTRY
  92. #undef SLIST_FIRST
  93. #undef SLIST_FOREACH
  94. #undef SLIST_FOREACH_PREVPTR
  95. #undef SLIST_FOREACH_SAFE
  96. #undef SLIST_HEAD
  97. #undef SLIST_HEAD_INITIALIZER
  98. #undef SLIST_INIT
  99. #undef SLIST_INSERT_AFTER
  100. #undef SLIST_INSERT_HEAD
  101. #undef SLIST_NEXT
  102. #undef SLIST_REMOVE
  103. #undef SLIST_REMOVE_AFTER
  104. #undef SLIST_REMOVE_HEAD
  105. #undef SLIST_REMOVE_NEXT
  106. #undef TAILQ_CONCAT
  107. #undef TAILQ_EMPTY
  108. #undef TAILQ_END
  109. #undef TAILQ_ENTRY
  110. #undef TAILQ_FIRST
  111. #undef TAILQ_FOREACH
  112. #undef TAILQ_FOREACH_REVERSE
  113. #undef TAILQ_FOREACH_REVERSE_SAFE
  114. #undef TAILQ_FOREACH_SAFE
  115. #undef TAILQ_HEAD
  116. #undef TAILQ_HEAD_INITIALIZER
  117. #undef TAILQ_INIT
  118. #undef TAILQ_INSERT_AFTER
  119. #undef TAILQ_INSERT_BEFORE
  120. #undef TAILQ_INSERT_HEAD
  121. #undef TAILQ_INSERT_TAIL
  122. #undef TAILQ_LAST
  123. #undef TAILQ_NEXT
  124. #undef TAILQ_PREV
  125. #undef TAILQ_REMOVE
  126. #undef TAILQ_REPLACE
  127. /*
  128. * This file defines five types of data structures: singly-linked lists,
  129. * lists, simple queues, tail queues and XOR simple queues.
  130. *
  131. *
  132. * A singly-linked list is headed by a single forward pointer. The elements
  133. * are singly linked for minimum space and pointer manipulation overhead at
  134. * the expense of O(n) removal for arbitrary elements. New elements can be
  135. * added to the list after an existing element or at the head of the list.
  136. * Elements being removed from the head of the list should use the explicit
  137. * macro for this purpose for optimum efficiency. A singly-linked list may
  138. * only be traversed in the forward direction. Singly-linked lists are ideal
  139. * for applications with large datasets and few or no removals or for
  140. * implementing a LIFO queue.
  141. *
  142. * A list is headed by a single forward pointer (or an array of forward
  143. * pointers for a hash table header). The elements are doubly linked
  144. * so that an arbitrary element can be removed without a need to
  145. * traverse the list. New elements can be added to the list before
  146. * or after an existing element or at the head of the list. A list
  147. * may only be traversed in the forward direction.
  148. *
  149. * A simple queue is headed by a pair of pointers, one to the head of the
  150. * list and the other to the tail of the list. The elements are singly
  151. * linked to save space, so elements can only be removed from the
  152. * head of the list. New elements can be added to the list before or after
  153. * an existing element, at the head of the list, or at the end of the
  154. * list. A simple queue may only be traversed in the forward direction.
  155. *
  156. * A tail queue is headed by a pair of pointers, one to the head of the
  157. * list and the other to the tail of the list. The elements are doubly
  158. * linked so that an arbitrary element can be removed without a need to
  159. * traverse the list. New elements can be added to the list before or
  160. * after an existing element, at the head of the list, or at the end of
  161. * the list. A tail queue may be traversed in either direction.
  162. *
  163. * An XOR simple queue is used in the same way as a regular simple queue.
  164. * The difference is that the head structure also includes a "cookie" that
  165. * is XOR'd with the queue pointer (first, last or next) to generate the
  166. * real pointer value.
  167. *
  168. * For details on the use of these macros, see the queue(3) manual page.
  169. */
  170. #if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC))
  171. #define _Q_INVALID ((void *)-1)
  172. #define _Q_INVALIDATE(a) (a) = _Q_INVALID
  173. #else
  174. #define _Q_INVALIDATE(a)
  175. #endif
  176. /*
  177. * Singly-linked List definitions.
  178. */
  179. #define SLIST_HEAD(name, type) \
  180. struct name { \
  181. struct type *slh_first; /* first element */ \
  182. }
  183. #define SLIST_HEAD_INITIALIZER(head) \
  184. { NULL }
  185. #define SLIST_ENTRY(type) \
  186. struct { \
  187. struct type *sle_next; /* next element */ \
  188. }
  189. /*
  190. * Singly-linked List access methods.
  191. */
  192. #define SLIST_FIRST(head) ((head)->slh_first)
  193. #define SLIST_END(head) NULL
  194. #define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
  195. #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  196. #define SLIST_FOREACH(var, head, field) \
  197. for((var) = SLIST_FIRST(head); \
  198. (var) != SLIST_END(head); \
  199. (var) = SLIST_NEXT(var, field))
  200. #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
  201. for ((var) = SLIST_FIRST(head); \
  202. (var) && ((tvar) = SLIST_NEXT(var, field), 1); \
  203. (var) = (tvar))
  204. /*
  205. * Singly-linked List functions.
  206. */
  207. #define SLIST_INIT(head) { \
  208. SLIST_FIRST(head) = SLIST_END(head); \
  209. }
  210. #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  211. (elm)->field.sle_next = (slistelm)->field.sle_next; \
  212. (slistelm)->field.sle_next = (elm); \
  213. } while (0)
  214. #define SLIST_INSERT_HEAD(head, elm, field) do { \
  215. (elm)->field.sle_next = (head)->slh_first; \
  216. (head)->slh_first = (elm); \
  217. } while (0)
  218. #define SLIST_REMOVE_AFTER(elm, field) do { \
  219. (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
  220. } while (0)
  221. #define SLIST_REMOVE_HEAD(head, field) do { \
  222. (head)->slh_first = (head)->slh_first->field.sle_next; \
  223. } while (0)
  224. #define SLIST_REMOVE(head, elm, type, field) do { \
  225. if ((head)->slh_first == (elm)) { \
  226. SLIST_REMOVE_HEAD((head), field); \
  227. } else { \
  228. struct type *curelm = (head)->slh_first; \
  229. \
  230. while (curelm->field.sle_next != (elm)) \
  231. curelm = curelm->field.sle_next; \
  232. curelm->field.sle_next = \
  233. curelm->field.sle_next->field.sle_next; \
  234. } \
  235. _Q_INVALIDATE((elm)->field.sle_next); \
  236. } while (0)
  237. /*
  238. * List definitions.
  239. */
  240. #define LIST_HEAD(name, type) \
  241. struct name { \
  242. struct type *lh_first; /* first element */ \
  243. }
  244. #define LIST_HEAD_INITIALIZER(head) \
  245. { NULL }
  246. #define LIST_ENTRY(type) \
  247. struct { \
  248. struct type *le_next; /* next element */ \
  249. struct type **le_prev; /* address of previous next element */ \
  250. }
  251. /*
  252. * List access methods.
  253. */
  254. #define LIST_FIRST(head) ((head)->lh_first)
  255. #define LIST_END(head) NULL
  256. #define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head))
  257. #define LIST_NEXT(elm, field) ((elm)->field.le_next)
  258. #define LIST_FOREACH(var, head, field) \
  259. for((var) = LIST_FIRST(head); \
  260. (var)!= LIST_END(head); \
  261. (var) = LIST_NEXT(var, field))
  262. #define LIST_FOREACH_SAFE(var, head, field, tvar) \
  263. for ((var) = LIST_FIRST(head); \
  264. (var) && ((tvar) = LIST_NEXT(var, field), 1); \
  265. (var) = (tvar))
  266. /*
  267. * List functions.
  268. */
  269. #define LIST_INIT(head) do { \
  270. LIST_FIRST(head) = LIST_END(head); \
  271. } while (0)
  272. #define LIST_INSERT_AFTER(listelm, elm, field) do { \
  273. if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
  274. (listelm)->field.le_next->field.le_prev = \
  275. &(elm)->field.le_next; \
  276. (listelm)->field.le_next = (elm); \
  277. (elm)->field.le_prev = &(listelm)->field.le_next; \
  278. } while (0)
  279. #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
  280. (elm)->field.le_prev = (listelm)->field.le_prev; \
  281. (elm)->field.le_next = (listelm); \
  282. *(listelm)->field.le_prev = (elm); \
  283. (listelm)->field.le_prev = &(elm)->field.le_next; \
  284. } while (0)
  285. #define LIST_INSERT_HEAD(head, elm, field) do { \
  286. if (((elm)->field.le_next = (head)->lh_first) != NULL) \
  287. (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
  288. (head)->lh_first = (elm); \
  289. (elm)->field.le_prev = &(head)->lh_first; \
  290. } while (0)
  291. #define LIST_REMOVE(elm, field) do { \
  292. if ((elm)->field.le_next != NULL) \
  293. (elm)->field.le_next->field.le_prev = \
  294. (elm)->field.le_prev; \
  295. *(elm)->field.le_prev = (elm)->field.le_next; \
  296. _Q_INVALIDATE((elm)->field.le_prev); \
  297. _Q_INVALIDATE((elm)->field.le_next); \
  298. } while (0)
  299. #define LIST_REPLACE(elm, elm2, field) do { \
  300. if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
  301. (elm2)->field.le_next->field.le_prev = \
  302. &(elm2)->field.le_next; \
  303. (elm2)->field.le_prev = (elm)->field.le_prev; \
  304. *(elm2)->field.le_prev = (elm2); \
  305. _Q_INVALIDATE((elm)->field.le_prev); \
  306. _Q_INVALIDATE((elm)->field.le_next); \
  307. } while (0)
  308. /*
  309. * Simple queue definitions.
  310. */
  311. #define SIMPLEQ_HEAD(name, type) \
  312. struct name { \
  313. struct type *sqh_first; /* first element */ \
  314. struct type **sqh_last; /* addr of last next element */ \
  315. }
  316. #define SIMPLEQ_HEAD_INITIALIZER(head) \
  317. { NULL, &(head).sqh_first }
  318. #define SIMPLEQ_ENTRY(type) \
  319. struct { \
  320. struct type *sqe_next; /* next element */ \
  321. }
  322. /*
  323. * Simple queue access methods.
  324. */
  325. #define SIMPLEQ_FIRST(head) ((head)->sqh_first)
  326. #define SIMPLEQ_END(head) NULL
  327. #define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
  328. #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
  329. #define SIMPLEQ_FOREACH(var, head, field) \
  330. for((var) = SIMPLEQ_FIRST(head); \
  331. (var) != SIMPLEQ_END(head); \
  332. (var) = SIMPLEQ_NEXT(var, field))
  333. #define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
  334. for ((var) = SIMPLEQ_FIRST(head); \
  335. (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1); \
  336. (var) = (tvar))
  337. /*
  338. * Simple queue functions.
  339. */
  340. #define SIMPLEQ_INIT(head) do { \
  341. (head)->sqh_first = NULL; \
  342. (head)->sqh_last = &(head)->sqh_first; \
  343. } while (0)
  344. #define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
  345. if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
  346. (head)->sqh_last = &(elm)->field.sqe_next; \
  347. (head)->sqh_first = (elm); \
  348. } while (0)
  349. #define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
  350. (elm)->field.sqe_next = NULL; \
  351. *(head)->sqh_last = (elm); \
  352. (head)->sqh_last = &(elm)->field.sqe_next; \
  353. } while (0)
  354. #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  355. if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
  356. (head)->sqh_last = &(elm)->field.sqe_next; \
  357. (listelm)->field.sqe_next = (elm); \
  358. } while (0)
  359. #define SIMPLEQ_REMOVE_HEAD(head, field) do { \
  360. if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
  361. (head)->sqh_last = &(head)->sqh_first; \
  362. } while (0)
  363. #define SIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
  364. if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) \
  365. == NULL) \
  366. (head)->sqh_last = &(elm)->field.sqe_next; \
  367. } while (0)
  368. #define SIMPLEQ_CONCAT(head1, head2) do { \
  369. if (!SIMPLEQ_EMPTY((head2))) { \
  370. *(head1)->sqh_last = (head2)->sqh_first; \
  371. (head1)->sqh_last = (head2)->sqh_last; \
  372. SIMPLEQ_INIT((head2)); \
  373. } \
  374. } while (0)
  375. /*
  376. * XOR Simple queue definitions.
  377. */
  378. #define XSIMPLEQ_HEAD(name, type) \
  379. struct name { \
  380. struct type *sqx_first; /* first element */ \
  381. struct type **sqx_last; /* addr of last next element */ \
  382. unsigned long sqx_cookie; \
  383. }
  384. #define XSIMPLEQ_ENTRY(type) \
  385. struct { \
  386. struct type *sqx_next; /* next element */ \
  387. }
  388. /*
  389. * XOR Simple queue access methods.
  390. */
  391. #define XSIMPLEQ_XOR(head, ptr) ((__typeof(ptr))((head)->sqx_cookie ^ \
  392. (unsigned long)(ptr)))
  393. #define XSIMPLEQ_FIRST(head) XSIMPLEQ_XOR(head, ((head)->sqx_first))
  394. #define XSIMPLEQ_END(head) NULL
  395. #define XSIMPLEQ_EMPTY(head) (XSIMPLEQ_FIRST(head) == XSIMPLEQ_END(head))
  396. #define XSIMPLEQ_NEXT(head, elm, field) XSIMPLEQ_XOR(head, ((elm)->field.sqx_next))
  397. #define XSIMPLEQ_FOREACH(var, head, field) \
  398. for ((var) = XSIMPLEQ_FIRST(head); \
  399. (var) != XSIMPLEQ_END(head); \
  400. (var) = XSIMPLEQ_NEXT(head, var, field))
  401. #define XSIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
  402. for ((var) = XSIMPLEQ_FIRST(head); \
  403. (var) && ((tvar) = XSIMPLEQ_NEXT(head, var, field), 1); \
  404. (var) = (tvar))
  405. /*
  406. * XOR Simple queue functions.
  407. */
  408. #define XSIMPLEQ_INIT(head) do { \
  409. arc4random_buf(&(head)->sqx_cookie, sizeof((head)->sqx_cookie)); \
  410. (head)->sqx_first = XSIMPLEQ_XOR(head, NULL); \
  411. (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
  412. } while (0)
  413. #define XSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
  414. if (((elm)->field.sqx_next = (head)->sqx_first) == \
  415. XSIMPLEQ_XOR(head, NULL)) \
  416. (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
  417. (head)->sqx_first = XSIMPLEQ_XOR(head, (elm)); \
  418. } while (0)
  419. #define XSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
  420. (elm)->field.sqx_next = XSIMPLEQ_XOR(head, NULL); \
  421. *(XSIMPLEQ_XOR(head, (head)->sqx_last)) = XSIMPLEQ_XOR(head, (elm)); \
  422. (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
  423. } while (0)
  424. #define XSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  425. if (((elm)->field.sqx_next = (listelm)->field.sqx_next) == \
  426. XSIMPLEQ_XOR(head, NULL)) \
  427. (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
  428. (listelm)->field.sqx_next = XSIMPLEQ_XOR(head, (elm)); \
  429. } while (0)
  430. #define XSIMPLEQ_REMOVE_HEAD(head, field) do { \
  431. if (((head)->sqx_first = XSIMPLEQ_XOR(head, \
  432. (head)->sqx_first)->field.sqx_next) == XSIMPLEQ_XOR(head, NULL)) \
  433. (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
  434. } while (0)
  435. #define XSIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
  436. if (((elm)->field.sqx_next = XSIMPLEQ_XOR(head, \
  437. (elm)->field.sqx_next)->field.sqx_next) \
  438. == XSIMPLEQ_XOR(head, NULL)) \
  439. (head)->sqx_last = \
  440. XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
  441. } while (0)
  442. /*
  443. * Tail queue definitions.
  444. */
  445. #define TAILQ_HEAD(name, type) \
  446. struct name { \
  447. struct type *tqh_first; /* first element */ \
  448. struct type **tqh_last; /* addr of last next element */ \
  449. }
  450. #define TAILQ_HEAD_INITIALIZER(head) \
  451. { NULL, &(head).tqh_first }
  452. #define TAILQ_ENTRY(type) \
  453. struct { \
  454. struct type *tqe_next; /* next element */ \
  455. struct type **tqe_prev; /* address of previous next element */ \
  456. }
  457. /*
  458. * Tail queue access methods.
  459. */
  460. #define TAILQ_FIRST(head) ((head)->tqh_first)
  461. #define TAILQ_END(head) NULL
  462. #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  463. #define TAILQ_LAST(head, headname) \
  464. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  465. /* XXX */
  466. #define TAILQ_PREV(elm, headname, field) \
  467. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  468. #define TAILQ_EMPTY(head) \
  469. (TAILQ_FIRST(head) == TAILQ_END(head))
  470. #define TAILQ_FOREACH(var, head, field) \
  471. for((var) = TAILQ_FIRST(head); \
  472. (var) != TAILQ_END(head); \
  473. (var) = TAILQ_NEXT(var, field))
  474. #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
  475. for ((var) = TAILQ_FIRST(head); \
  476. (var) != TAILQ_END(head) && \
  477. ((tvar) = TAILQ_NEXT(var, field), 1); \
  478. (var) = (tvar))
  479. #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  480. for((var) = TAILQ_LAST(head, headname); \
  481. (var) != TAILQ_END(head); \
  482. (var) = TAILQ_PREV(var, headname, field))
  483. #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
  484. for ((var) = TAILQ_LAST(head, headname); \
  485. (var) != TAILQ_END(head) && \
  486. ((tvar) = TAILQ_PREV(var, headname, field), 1); \
  487. (var) = (tvar))
  488. /*
  489. * Tail queue functions.
  490. */
  491. #define TAILQ_INIT(head) do { \
  492. (head)->tqh_first = NULL; \
  493. (head)->tqh_last = &(head)->tqh_first; \
  494. } while (0)
  495. #define TAILQ_INSERT_HEAD(head, elm, field) do { \
  496. if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  497. (head)->tqh_first->field.tqe_prev = \
  498. &(elm)->field.tqe_next; \
  499. else \
  500. (head)->tqh_last = &(elm)->field.tqe_next; \
  501. (head)->tqh_first = (elm); \
  502. (elm)->field.tqe_prev = &(head)->tqh_first; \
  503. } while (0)
  504. #define TAILQ_INSERT_TAIL(head, elm, field) do { \
  505. (elm)->field.tqe_next = NULL; \
  506. (elm)->field.tqe_prev = (head)->tqh_last; \
  507. *(head)->tqh_last = (elm); \
  508. (head)->tqh_last = &(elm)->field.tqe_next; \
  509. } while (0)
  510. #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  511. if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  512. (elm)->field.tqe_next->field.tqe_prev = \
  513. &(elm)->field.tqe_next; \
  514. else \
  515. (head)->tqh_last = &(elm)->field.tqe_next; \
  516. (listelm)->field.tqe_next = (elm); \
  517. (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  518. } while (0)
  519. #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  520. (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  521. (elm)->field.tqe_next = (listelm); \
  522. *(listelm)->field.tqe_prev = (elm); \
  523. (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  524. } while (0)
  525. #define TAILQ_REMOVE(head, elm, field) do { \
  526. if (((elm)->field.tqe_next) != NULL) \
  527. (elm)->field.tqe_next->field.tqe_prev = \
  528. (elm)->field.tqe_prev; \
  529. else \
  530. (head)->tqh_last = (elm)->field.tqe_prev; \
  531. *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  532. _Q_INVALIDATE((elm)->field.tqe_prev); \
  533. _Q_INVALIDATE((elm)->field.tqe_next); \
  534. } while (0)
  535. #define TAILQ_REPLACE(head, elm, elm2, field) do { \
  536. if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
  537. (elm2)->field.tqe_next->field.tqe_prev = \
  538. &(elm2)->field.tqe_next; \
  539. else \
  540. (head)->tqh_last = &(elm2)->field.tqe_next; \
  541. (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
  542. *(elm2)->field.tqe_prev = (elm2); \
  543. _Q_INVALIDATE((elm)->field.tqe_prev); \
  544. _Q_INVALIDATE((elm)->field.tqe_next); \
  545. } while (0)
  546. #define TAILQ_CONCAT(head1, head2, field) do { \
  547. if (!TAILQ_EMPTY(head2)) { \
  548. *(head1)->tqh_last = (head2)->tqh_first; \
  549. (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
  550. (head1)->tqh_last = (head2)->tqh_last; \
  551. TAILQ_INIT((head2)); \
  552. } \
  553. } while (0)
  554. #endif /* !_SYS_QUEUE_H_ */