You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

581 lines
29 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
4 years ago
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
4 years ago
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
4 years ago
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* $OpenBSD: queue.h,v 1.43 2015/12/28 19:38:40 millert 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. #ifndef _SYS_QUEUE_H_
  34. #define _SYS_QUEUE_H_
  35. /*
  36. * This file defines five types of data structures: singly-linked lists,
  37. * lists, simple queues, tail queues and XOR simple queues.
  38. *
  39. *
  40. * A singly-linked list is headed by a single forward pointer. The elements
  41. * are singly linked for minimum space and pointer manipulation overhead at
  42. * the expense of O(n) removal for arbitrary elements. New elements can be
  43. * added to the list after an existing element or at the head of the list.
  44. * Elements being removed from the head of the list should use the explicit
  45. * macro for this purpose for optimum efficiency. A singly-linked list may
  46. * only be traversed in the forward direction. Singly-linked lists are ideal
  47. * for applications with large datasets and few or no removals or for
  48. * implementing a LIFO queue.
  49. *
  50. * A list is headed by a single forward pointer (or an array of forward
  51. * pointers for a hash table header). The elements are doubly linked
  52. * so that an arbitrary element can be removed without a need to
  53. * traverse the list. New elements can be added to the list before
  54. * or after an existing element or at the head of the list. A list
  55. * may only be traversed in the forward direction.
  56. *
  57. * A simple queue is headed by a pair of pointers, one to the head of the
  58. * list and the other to the tail of the list. The elements are singly
  59. * linked to save space, so elements can only be removed from the
  60. * head of the list. New elements can be added to the list before or after
  61. * an existing element, at the head of the list, or at the end of the
  62. * list. A simple queue 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. * An XOR simple queue is used in the same way as a regular simple queue.
  72. * The difference is that the head structure also includes a "cookie" that
  73. * is XOR'd with the queue pointer (first, last or next) to generate the
  74. * real pointer value.
  75. *
  76. * For details on the use of these macros, see the queue(3) manual page.
  77. */
  78. #if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC))
  79. #define _Q_INVALIDATE(a) (a) = ((void *) -1)
  80. #else
  81. #define _Q_INVALIDATE(a)
  82. #endif
  83. /*
  84. * Singly-linked List definitions.
  85. */
  86. #define SLIST_HEAD(name, type) \
  87. struct name \
  88. { \
  89. struct type * slh_first; /* first element */ \
  90. }
  91. #define SLIST_HEAD_INITIALIZER(head) {NULL}
  92. #define SLIST_ENTRY(type) \
  93. struct \
  94. { \
  95. struct type * sle_next; /* next element */ \
  96. }
  97. /*
  98. * Singly-linked List access methods.
  99. */
  100. #define SLIST_FIRST(head) ((head)->slh_first)
  101. #define SLIST_END(head) NULL
  102. #define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
  103. #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  104. #define SLIST_FOREACH(var, head, field) for ((var) = SLIST_FIRST(head); (var) != SLIST_END(head); (var) = SLIST_NEXT(var, field))
  105. #define SLIST_FOREACH_SAFE(var, head, field, tvar) for ((var) = SLIST_FIRST(head); (var) && ((tvar) = SLIST_NEXT(var, field), 1); (var) = (tvar))
  106. /*
  107. * Singly-linked List functions.
  108. */
  109. #define SLIST_INIT(head) \
  110. { \
  111. SLIST_FIRST(head) = SLIST_END(head); \
  112. }
  113. #define SLIST_INSERT_AFTER(slistelm, elm, field) \
  114. do \
  115. { \
  116. (elm)->field.sle_next = (slistelm)->field.sle_next; \
  117. (slistelm)->field.sle_next = (elm); \
  118. } \
  119. while (0)
  120. #define SLIST_INSERT_HEAD(head, elm, field) \
  121. do \
  122. { \
  123. (elm)->field.sle_next = (head)->slh_first; \
  124. (head)->slh_first = (elm); \
  125. } \
  126. while (0)
  127. #define SLIST_REMOVE_AFTER(elm, field) \
  128. do \
  129. { \
  130. (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
  131. } \
  132. while (0)
  133. #define SLIST_REMOVE_HEAD(head, field) \
  134. do \
  135. { \
  136. (head)->slh_first = (head)->slh_first->field.sle_next; \
  137. } \
  138. while (0)
  139. #define SLIST_REMOVE(head, elm, type, field) \
  140. do \
  141. { \
  142. if ((head)->slh_first == (elm)) \
  143. { \
  144. SLIST_REMOVE_HEAD((head), field); \
  145. } \
  146. else \
  147. { \
  148. struct type * curelm = (head)->slh_first; \
  149. \
  150. while (curelm->field.sle_next != (elm)) \
  151. curelm = curelm->field.sle_next; \
  152. curelm->field.sle_next = curelm->field.sle_next->field.sle_next; \
  153. } \
  154. _Q_INVALIDATE((elm)->field.sle_next); \
  155. } \
  156. while (0)
  157. /*
  158. * List definitions.
  159. */
  160. #define LIST_HEAD(name, type) \
  161. struct name \
  162. { \
  163. struct type * lh_first; /* first element */ \
  164. }
  165. #define LIST_HEAD_INITIALIZER(head) {NULL}
  166. #define LIST_ENTRY(type) \
  167. struct \
  168. { \
  169. struct type * le_next; /* next element */ \
  170. struct type ** le_prev; /* address of previous next element */ \
  171. }
  172. /*
  173. * List access methods.
  174. */
  175. #define LIST_FIRST(head) ((head)->lh_first)
  176. #define LIST_END(head) NULL
  177. #define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head))
  178. #define LIST_NEXT(elm, field) ((elm)->field.le_next)
  179. #define LIST_FOREACH(var, head, field) for ((var) = LIST_FIRST(head); (var) != LIST_END(head); (var) = LIST_NEXT(var, field))
  180. #define LIST_FOREACH_SAFE(var, head, field, tvar) for ((var) = LIST_FIRST(head); (var) && ((tvar) = LIST_NEXT(var, field), 1); (var) = (tvar))
  181. /*
  182. * List functions.
  183. */
  184. #define LIST_INIT(head) \
  185. do \
  186. { \
  187. LIST_FIRST(head) = LIST_END(head); \
  188. } \
  189. while (0)
  190. #define LIST_INSERT_AFTER(listelm, elm, field) \
  191. do \
  192. { \
  193. if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
  194. (listelm)->field.le_next->field.le_prev = &(elm)->field.le_next; \
  195. (listelm)->field.le_next = (elm); \
  196. (elm)->field.le_prev = &(listelm)->field.le_next; \
  197. } \
  198. while (0)
  199. #define LIST_INSERT_BEFORE(listelm, elm, field) \
  200. do \
  201. { \
  202. (elm)->field.le_prev = (listelm)->field.le_prev; \
  203. (elm)->field.le_next = (listelm); \
  204. *(listelm)->field.le_prev = (elm); \
  205. (listelm)->field.le_prev = &(elm)->field.le_next; \
  206. } \
  207. while (0)
  208. #define LIST_INSERT_HEAD(head, elm, field) \
  209. do \
  210. { \
  211. if (((elm)->field.le_next = (head)->lh_first) != NULL) \
  212. (head)->lh_first->field.le_prev = &(elm)->field.le_next; \
  213. (head)->lh_first = (elm); \
  214. (elm)->field.le_prev = &(head)->lh_first; \
  215. } \
  216. while (0)
  217. #define LIST_REMOVE(elm, field) \
  218. do \
  219. { \
  220. if ((elm)->field.le_next != NULL) \
  221. (elm)->field.le_next->field.le_prev = (elm)->field.le_prev; \
  222. *(elm)->field.le_prev = (elm)->field.le_next; \
  223. _Q_INVALIDATE((elm)->field.le_prev); \
  224. _Q_INVALIDATE((elm)->field.le_next); \
  225. } \
  226. while (0)
  227. #define LIST_REPLACE(elm, elm2, field) \
  228. do \
  229. { \
  230. if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
  231. (elm2)->field.le_next->field.le_prev = &(elm2)->field.le_next; \
  232. (elm2)->field.le_prev = (elm)->field.le_prev; \
  233. *(elm2)->field.le_prev = (elm2); \
  234. _Q_INVALIDATE((elm)->field.le_prev); \
  235. _Q_INVALIDATE((elm)->field.le_next); \
  236. } \
  237. while (0)
  238. /*
  239. * Simple queue definitions.
  240. */
  241. #define SIMPLEQ_HEAD(name, type) \
  242. struct name \
  243. { \
  244. struct type * sqh_first; /* first element */ \
  245. struct type ** sqh_last; /* addr of last next element */ \
  246. }
  247. #define SIMPLEQ_HEAD_INITIALIZER(head) {NULL, &(head).sqh_first}
  248. #define SIMPLEQ_ENTRY(type) \
  249. struct \
  250. { \
  251. struct type * sqe_next; /* next element */ \
  252. }
  253. /*
  254. * Simple queue access methods.
  255. */
  256. #define SIMPLEQ_FIRST(head) ((head)->sqh_first)
  257. #define SIMPLEQ_END(head) NULL
  258. #define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
  259. #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
  260. #define SIMPLEQ_FOREACH(var, head, field) for ((var) = SIMPLEQ_FIRST(head); (var) != SIMPLEQ_END(head); (var) = SIMPLEQ_NEXT(var, field))
  261. #define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) for ((var) = SIMPLEQ_FIRST(head); (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1); (var) = (tvar))
  262. /*
  263. * Simple queue functions.
  264. */
  265. #define SIMPLEQ_INIT(head) \
  266. do \
  267. { \
  268. (head)->sqh_first = NULL; \
  269. (head)->sqh_last = &(head)->sqh_first; \
  270. } \
  271. while (0)
  272. #define SIMPLEQ_INSERT_HEAD(head, elm, field) \
  273. do \
  274. { \
  275. if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
  276. (head)->sqh_last = &(elm)->field.sqe_next; \
  277. (head)->sqh_first = (elm); \
  278. } \
  279. while (0)
  280. #define SIMPLEQ_INSERT_TAIL(head, elm, field) \
  281. do \
  282. { \
  283. (elm)->field.sqe_next = NULL; \
  284. *(head)->sqh_last = (elm); \
  285. (head)->sqh_last = &(elm)->field.sqe_next; \
  286. } \
  287. while (0)
  288. #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) \
  289. do \
  290. { \
  291. if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL) \
  292. (head)->sqh_last = &(elm)->field.sqe_next; \
  293. (listelm)->field.sqe_next = (elm); \
  294. } \
  295. while (0)
  296. #define SIMPLEQ_REMOVE_HEAD(head, field) \
  297. do \
  298. { \
  299. if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
  300. (head)->sqh_last = &(head)->sqh_first; \
  301. } \
  302. while (0)
  303. #define SIMPLEQ_REMOVE_AFTER(head, elm, field) \
  304. do \
  305. { \
  306. if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) == NULL) \
  307. (head)->sqh_last = &(elm)->field.sqe_next; \
  308. } \
  309. while (0)
  310. #define SIMPLEQ_CONCAT(head1, head2) \
  311. do \
  312. { \
  313. if (! SIMPLEQ_EMPTY((head2))) \
  314. { \
  315. *(head1)->sqh_last = (head2)->sqh_first; \
  316. (head1)->sqh_last = (head2)->sqh_last; \
  317. SIMPLEQ_INIT((head2)); \
  318. } \
  319. } \
  320. while (0)
  321. /*
  322. * XOR Simple queue definitions.
  323. */
  324. #define XSIMPLEQ_HEAD(name, type) \
  325. struct name \
  326. { \
  327. struct type * sqx_first; /* first element */ \
  328. struct type ** sqx_last; /* addr of last next element */ \
  329. unsigned long sqx_cookie; \
  330. }
  331. #define XSIMPLEQ_ENTRY(type) \
  332. struct \
  333. { \
  334. struct type * sqx_next; /* next element */ \
  335. }
  336. /*
  337. * XOR Simple queue access methods.
  338. */
  339. #define XSIMPLEQ_XOR(head, ptr) ((__typeof(ptr)) ((head)->sqx_cookie ^ (unsigned long) (ptr)))
  340. #define XSIMPLEQ_FIRST(head) XSIMPLEQ_XOR(head, ((head)->sqx_first))
  341. #define XSIMPLEQ_END(head) NULL
  342. #define XSIMPLEQ_EMPTY(head) (XSIMPLEQ_FIRST(head) == XSIMPLEQ_END(head))
  343. #define XSIMPLEQ_NEXT(head, elm, field) XSIMPLEQ_XOR(head, ((elm)->field.sqx_next))
  344. #define XSIMPLEQ_FOREACH(var, head, field) for ((var) = XSIMPLEQ_FIRST(head); (var) != XSIMPLEQ_END(head); (var) = XSIMPLEQ_NEXT(head, var, field))
  345. #define XSIMPLEQ_FOREACH_SAFE(var, head, field, tvar) for ((var) = XSIMPLEQ_FIRST(head); (var) && ((tvar) = XSIMPLEQ_NEXT(head, var, field), 1); (var) = (tvar))
  346. /*
  347. * XOR Simple queue functions.
  348. */
  349. #define XSIMPLEQ_INIT(head) \
  350. do \
  351. { \
  352. arc4random_buf(&(head)->sqx_cookie, sizeof((head)->sqx_cookie)); \
  353. (head)->sqx_first = XSIMPLEQ_XOR(head, NULL); \
  354. (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
  355. } \
  356. while (0)
  357. #define XSIMPLEQ_INSERT_HEAD(head, elm, field) \
  358. do \
  359. { \
  360. if (((elm)->field.sqx_next = (head)->sqx_first) == XSIMPLEQ_XOR(head, NULL)) \
  361. (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
  362. (head)->sqx_first = XSIMPLEQ_XOR(head, (elm)); \
  363. } \
  364. while (0)
  365. #define XSIMPLEQ_INSERT_TAIL(head, elm, field) \
  366. do \
  367. { \
  368. (elm)->field.sqx_next = XSIMPLEQ_XOR(head, NULL); \
  369. *(XSIMPLEQ_XOR(head, (head)->sqx_last)) = XSIMPLEQ_XOR(head, (elm)); \
  370. (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
  371. } \
  372. while (0)
  373. #define XSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) \
  374. do \
  375. { \
  376. if (((elm)->field.sqx_next = (listelm)->field.sqx_next) == XSIMPLEQ_XOR(head, NULL)) \
  377. (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
  378. (listelm)->field.sqx_next = XSIMPLEQ_XOR(head, (elm)); \
  379. } \
  380. while (0)
  381. #define XSIMPLEQ_REMOVE_HEAD(head, field) \
  382. do \
  383. { \
  384. if (((head)->sqx_first = XSIMPLEQ_XOR(head, (head)->sqx_first)->field.sqx_next) == XSIMPLEQ_XOR(head, NULL)) \
  385. (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
  386. } \
  387. while (0)
  388. #define XSIMPLEQ_REMOVE_AFTER(head, elm, field) \
  389. do \
  390. { \
  391. if (((elm)->field.sqx_next = XSIMPLEQ_XOR(head, (elm)->field.sqx_next)->field.sqx_next) == XSIMPLEQ_XOR(head, NULL)) \
  392. (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
  393. } \
  394. while (0)
  395. /*
  396. * Tail queue definitions.
  397. */
  398. #define TAILQ_HEAD(name, type) \
  399. struct name \
  400. { \
  401. struct type * tqh_first; /* first element */ \
  402. struct type ** tqh_last; /* addr of last next element */ \
  403. }
  404. #define TAILQ_HEAD_INITIALIZER(head) {NULL, &(head).tqh_first}
  405. #define TAILQ_ENTRY(type) \
  406. struct \
  407. { \
  408. struct type * tqe_next; /* next element */ \
  409. struct type ** tqe_prev; /* address of previous next element */ \
  410. }
  411. /*
  412. * Tail queue access methods.
  413. */
  414. #define TAILQ_FIRST(head) ((head)->tqh_first)
  415. #define TAILQ_END(head) NULL
  416. #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  417. #define TAILQ_LAST(head, headname) (*(((struct headname *) ((head)->tqh_last))->tqh_last))
  418. /* XXX */
  419. #define TAILQ_PREV(elm, headname, field) (*(((struct headname *) ((elm)->field.tqe_prev))->tqh_last))
  420. #define TAILQ_EMPTY(head) (TAILQ_FIRST(head) == TAILQ_END(head))
  421. #define TAILQ_FOREACH(var, head, field) for ((var) = TAILQ_FIRST(head); (var) != TAILQ_END(head); (var) = TAILQ_NEXT(var, field))
  422. #define TAILQ_FOREACH_SAFE(var, head, field, tvar) for ((var) = TAILQ_FIRST(head); (var) != TAILQ_END(head) && ((tvar) = TAILQ_NEXT(var, field), 1); (var) = (tvar))
  423. #define TAILQ_FOREACH_REVERSE(var, head, headname, field) for ((var) = TAILQ_LAST(head, headname); (var) != TAILQ_END(head); (var) = TAILQ_PREV(var, headname, field))
  424. #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) for ((var) = TAILQ_LAST(head, headname); (var) != TAILQ_END(head) && ((tvar) = TAILQ_PREV(var, headname, field), 1); (var) = (tvar))
  425. /*
  426. * Tail queue functions.
  427. */
  428. #define TAILQ_INIT(head) \
  429. do \
  430. { \
  431. (head)->tqh_first = NULL; \
  432. (head)->tqh_last = &(head)->tqh_first; \
  433. } \
  434. while (0)
  435. #define TAILQ_INSERT_HEAD(head, elm, field) \
  436. do \
  437. { \
  438. if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  439. (head)->tqh_first->field.tqe_prev = &(elm)->field.tqe_next; \
  440. else \
  441. (head)->tqh_last = &(elm)->field.tqe_next; \
  442. (head)->tqh_first = (elm); \
  443. (elm)->field.tqe_prev = &(head)->tqh_first; \
  444. } \
  445. while (0)
  446. #define TAILQ_INSERT_TAIL(head, elm, field) \
  447. do \
  448. { \
  449. (elm)->field.tqe_next = NULL; \
  450. (elm)->field.tqe_prev = (head)->tqh_last; \
  451. *(head)->tqh_last = (elm); \
  452. (head)->tqh_last = &(elm)->field.tqe_next; \
  453. } \
  454. while (0)
  455. #define TAILQ_INSERT_AFTER(head, listelm, elm, field) \
  456. do \
  457. { \
  458. if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL) \
  459. (elm)->field.tqe_next->field.tqe_prev = &(elm)->field.tqe_next; \
  460. else \
  461. (head)->tqh_last = &(elm)->field.tqe_next; \
  462. (listelm)->field.tqe_next = (elm); \
  463. (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  464. } \
  465. while (0)
  466. #define TAILQ_INSERT_BEFORE(listelm, elm, field) \
  467. do \
  468. { \
  469. (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  470. (elm)->field.tqe_next = (listelm); \
  471. *(listelm)->field.tqe_prev = (elm); \
  472. (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  473. } \
  474. while (0)
  475. #define TAILQ_REMOVE(head, elm, field) \
  476. do \
  477. { \
  478. if (((elm)->field.tqe_next) != NULL) \
  479. (elm)->field.tqe_next->field.tqe_prev = (elm)->field.tqe_prev; \
  480. else \
  481. (head)->tqh_last = (elm)->field.tqe_prev; \
  482. *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  483. _Q_INVALIDATE((elm)->field.tqe_prev); \
  484. _Q_INVALIDATE((elm)->field.tqe_next); \
  485. } \
  486. while (0)
  487. #define TAILQ_REPLACE(head, elm, elm2, field) \
  488. do \
  489. { \
  490. if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
  491. (elm2)->field.tqe_next->field.tqe_prev = &(elm2)->field.tqe_next; \
  492. else \
  493. (head)->tqh_last = &(elm2)->field.tqe_next; \
  494. (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
  495. *(elm2)->field.tqe_prev = (elm2); \
  496. _Q_INVALIDATE((elm)->field.tqe_prev); \
  497. _Q_INVALIDATE((elm)->field.tqe_next); \
  498. } \
  499. while (0)
  500. #define TAILQ_CONCAT(head1, head2, field) \
  501. do \
  502. { \
  503. if (! TAILQ_EMPTY(head2)) \
  504. { \
  505. *(head1)->tqh_last = (head2)->tqh_first; \
  506. (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
  507. (head1)->tqh_last = (head2)->tqh_last; \
  508. TAILQ_INIT((head2)); \
  509. } \
  510. } \
  511. while (0)
  512. #endif /* !_SYS_QUEUE_H_ */