/filesystems/unixfs/common/darwin/queue.h

http://macfuse.googlecode.com/ · C Header · 716 lines · 444 code · 102 blank · 170 comment · 117 complexity · 6813e680d10c0ddf323e15d5fecd04b2 MD5 · raw file

  1. /*
  2. * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
  3. *
  4. * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apple Public Source License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. The rights granted to you under the License
  10. * may not be used to create, or enable the creation or redistribution of,
  11. * unlawful or unlicensed copies of an Apple operating system, or to
  12. * circumvent, violate, or enable the circumvention or violation of, any
  13. * terms of an Apple operating system software license agreement.
  14. *
  15. * Please obtain a copy of the License at
  16. * http://www.opensource.apple.com/apsl/ and read it before using this file.
  17. *
  18. * The Original Code and all software distributed under the License are
  19. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  20. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  21. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  23. * Please see the License for the specific language governing rights and
  24. * limitations under the License.
  25. *
  26. * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  27. */
  28. /*-
  29. * Copyright (c) 1991, 1993
  30. * The Regents of the University of California. All rights reserved.
  31. *
  32. * Redistribution and use in source and binary forms, with or without
  33. * modification, are permitted provided that the following conditions
  34. * are met:
  35. * 1. Redistributions of source code must retain the above copyright
  36. * notice, this list of conditions and the following disclaimer.
  37. * 2. Redistributions in binary form must reproduce the above copyright
  38. * notice, this list of conditions and the following disclaimer in the
  39. * documentation and/or other materials provided with the distribution.
  40. * 4. Neither the name of the University nor the names of its contributors
  41. * may be used to endorse or promote products derived from this software
  42. * without specific prior written permission.
  43. *
  44. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  45. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  48. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  49. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  50. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  51. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  52. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  53. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  54. * SUCH DAMAGE.
  55. *
  56. * @(#)queue.h 8.5 (Berkeley) 8/20/94
  57. */
  58. #ifndef _SYS_QUEUE_H_
  59. #define _SYS_QUEUE_H_
  60. /*
  61. * This file defines five types of data structures: singly-linked lists,
  62. * singly-linked tail queues, lists, tail queues, and circular queues.
  63. *
  64. * A singly-linked list is headed by a single forward pointer. The elements
  65. * are singly linked for minimum space and pointer manipulation overhead at
  66. * the expense of O(n) removal for arbitrary elements. New elements can be
  67. * added to the list after an existing element or at the head of the list.
  68. * Elements being removed from the head of the list should use the explicit
  69. * macro for this purpose for optimum efficiency. A singly-linked list may
  70. * only be traversed in the forward direction. Singly-linked lists are ideal
  71. * for applications with large datasets and few or no removals or for
  72. * implementing a LIFO queue.
  73. *
  74. * A singly-linked tail queue is headed by a pair of pointers, one to the
  75. * head of the list and the other to the tail of the list. The elements are
  76. * singly linked for minimum space and pointer manipulation overhead at the
  77. * expense of O(n) removal for arbitrary elements. New elements can be added
  78. * to the list after an existing element, at the head of the list, or at the
  79. * end of the list. Elements being removed from the head of the tail queue
  80. * should use the explicit macro for this purpose for optimum efficiency.
  81. * A singly-linked tail queue may only be traversed in the forward direction.
  82. * Singly-linked tail queues are ideal for applications with large datasets
  83. * and few or no removals or for implementing a FIFO queue.
  84. *
  85. * A list is headed by a single forward pointer (or an array of forward
  86. * pointers for a hash table header). The elements are doubly linked
  87. * so that an arbitrary element can be removed without a need to
  88. * traverse the list. New elements can be added to the list before
  89. * or after an existing element or at the head of the list. A list
  90. * may only be traversed in the forward direction.
  91. *
  92. * A tail queue is headed by a pair of pointers, one to the head of the
  93. * list and the other to the tail of the list. The elements are doubly
  94. * linked so that an arbitrary element can be removed without a need to
  95. * traverse the list. New elements can be added to the list before or
  96. * after an existing element, at the head of the list, or at the end of
  97. * the list. A tail queue may be traversed in either direction.
  98. *
  99. * A circle queue is headed by a pair of pointers, one to the head of the
  100. * list and the other to the tail of the list. The elements are doubly
  101. * linked so that an arbitrary element can be removed without a need to
  102. * traverse the list. New elements can be added to the list before or after
  103. * an existing element, at the head of the list, or at the end of the list.
  104. * A circle queue may be traversed in either direction, but has a more
  105. * complex end of list detection.
  106. * Note that circle queues are deprecated, because, as the removal log
  107. * in FreeBSD states, "CIRCLEQs are a disgrace to everything Knuth taught
  108. * us in Volume 1 Chapter 2. [...] Use TAILQ instead, it provides the same
  109. * functionality." Code using them will continue to compile, but they
  110. * are no longer documented on the man page.
  111. *
  112. * For details on the use of these macros, see the queue(3) manual page.
  113. *
  114. *
  115. * SLIST LIST STAILQ TAILQ CIRCLEQ
  116. * _HEAD + + + + +
  117. * _HEAD_INITIALIZER + + + + -
  118. * _ENTRY + + + + +
  119. * _INIT + + + + +
  120. * _EMPTY + + + + +
  121. * _FIRST + + + + +
  122. * _NEXT + + + + +
  123. * _PREV - - - + +
  124. * _LAST - - + + +
  125. * _FOREACH + + + + +
  126. * _FOREACH_SAFE + + + + -
  127. * _FOREACH_REVERSE - - - + -
  128. * _FOREACH_REVERSE_SAFE - - - + -
  129. * _INSERT_HEAD + + + + +
  130. * _INSERT_BEFORE - + - + +
  131. * _INSERT_AFTER + + + + +
  132. * _INSERT_TAIL - - + + +
  133. * _CONCAT - - + + -
  134. * _REMOVE_HEAD + - + - -
  135. * _REMOVE + + + + +
  136. *
  137. */
  138. #ifdef QUEUE_MACRO_DEBUG
  139. /* Store the last 2 places the queue element or head was altered */
  140. struct qm_trace {
  141. char * lastfile;
  142. int lastline;
  143. char * prevfile;
  144. int prevline;
  145. };
  146. #define TRACEBUF struct qm_trace trace;
  147. #define TRASHIT(x) do {(x) = (void *)-1;} while (0)
  148. #define QMD_TRACE_HEAD(head) do { \
  149. (head)->trace.prevline = (head)->trace.lastline; \
  150. (head)->trace.prevfile = (head)->trace.lastfile; \
  151. (head)->trace.lastline = __LINE__; \
  152. (head)->trace.lastfile = __FILE__; \
  153. } while (0)
  154. #define QMD_TRACE_ELEM(elem) do { \
  155. (elem)->trace.prevline = (elem)->trace.lastline; \
  156. (elem)->trace.prevfile = (elem)->trace.lastfile; \
  157. (elem)->trace.lastline = __LINE__; \
  158. (elem)->trace.lastfile = __FILE__; \
  159. } while (0)
  160. #else
  161. #define QMD_TRACE_ELEM(elem)
  162. #define QMD_TRACE_HEAD(head)
  163. #define TRACEBUF
  164. #define TRASHIT(x)
  165. #endif /* QUEUE_MACRO_DEBUG */
  166. /*
  167. * Singly-linked List declarations.
  168. */
  169. #define SLIST_HEAD(name, type) \
  170. struct name { \
  171. struct type *slh_first; /* first element */ \
  172. }
  173. #define SLIST_HEAD_INITIALIZER(head) \
  174. { NULL }
  175. #define SLIST_ENTRY(type) \
  176. struct { \
  177. struct type *sle_next; /* next element */ \
  178. }
  179. /*
  180. * Singly-linked List functions.
  181. */
  182. #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
  183. #define SLIST_FIRST(head) ((head)->slh_first)
  184. #define SLIST_FOREACH(var, head, field) \
  185. for ((var) = SLIST_FIRST((head)); \
  186. (var); \
  187. (var) = SLIST_NEXT((var), field))
  188. #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
  189. for ((var) = SLIST_FIRST((head)); \
  190. (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
  191. (var) = (tvar))
  192. #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  193. for ((varp) = &SLIST_FIRST((head)); \
  194. ((var) = *(varp)) != NULL; \
  195. (varp) = &SLIST_NEXT((var), field))
  196. #define SLIST_INIT(head) do { \
  197. SLIST_FIRST((head)) = NULL; \
  198. } while (0)
  199. #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  200. SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
  201. SLIST_NEXT((slistelm), field) = (elm); \
  202. } while (0)
  203. #define SLIST_INSERT_HEAD(head, elm, field) do { \
  204. SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
  205. SLIST_FIRST((head)) = (elm); \
  206. } while (0)
  207. #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  208. #define SLIST_REMOVE(head, elm, type, field) do { \
  209. if (SLIST_FIRST((head)) == (elm)) { \
  210. SLIST_REMOVE_HEAD((head), field); \
  211. } \
  212. else { \
  213. struct type *curelm = SLIST_FIRST((head)); \
  214. while (SLIST_NEXT(curelm, field) != (elm)) \
  215. curelm = SLIST_NEXT(curelm, field); \
  216. SLIST_NEXT(curelm, field) = \
  217. SLIST_NEXT(SLIST_NEXT(curelm, field), field); \
  218. } \
  219. TRASHIT((elm)->field.sle_next); \
  220. } while (0)
  221. #define SLIST_REMOVE_HEAD(head, field) do { \
  222. SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
  223. } while (0)
  224. /*
  225. * Singly-linked Tail queue declarations.
  226. */
  227. #define STAILQ_HEAD(name, type) \
  228. struct name { \
  229. struct type *stqh_first;/* first element */ \
  230. struct type **stqh_last;/* addr of last next element */ \
  231. }
  232. #define STAILQ_HEAD_INITIALIZER(head) \
  233. { NULL, &(head).stqh_first }
  234. #define STAILQ_ENTRY(type) \
  235. struct { \
  236. struct type *stqe_next; /* next element */ \
  237. }
  238. /*
  239. * Singly-linked Tail queue functions.
  240. */
  241. #define STAILQ_CONCAT(head1, head2) do { \
  242. if (!STAILQ_EMPTY((head2))) { \
  243. *(head1)->stqh_last = (head2)->stqh_first; \
  244. (head1)->stqh_last = (head2)->stqh_last; \
  245. STAILQ_INIT((head2)); \
  246. } \
  247. } while (0)
  248. #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
  249. #define STAILQ_FIRST(head) ((head)->stqh_first)
  250. #define STAILQ_FOREACH(var, head, field) \
  251. for((var) = STAILQ_FIRST((head)); \
  252. (var); \
  253. (var) = STAILQ_NEXT((var), field))
  254. #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
  255. for ((var) = STAILQ_FIRST((head)); \
  256. (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
  257. (var) = (tvar))
  258. #define STAILQ_INIT(head) do { \
  259. STAILQ_FIRST((head)) = NULL; \
  260. (head)->stqh_last = &STAILQ_FIRST((head)); \
  261. } while (0)
  262. #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
  263. if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
  264. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  265. STAILQ_NEXT((tqelm), field) = (elm); \
  266. } while (0)
  267. #define STAILQ_INSERT_HEAD(head, elm, field) do { \
  268. if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
  269. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  270. STAILQ_FIRST((head)) = (elm); \
  271. } while (0)
  272. #define STAILQ_INSERT_TAIL(head, elm, field) do { \
  273. STAILQ_NEXT((elm), field) = NULL; \
  274. *(head)->stqh_last = (elm); \
  275. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  276. } while (0)
  277. #define STAILQ_LAST(head, type, field) \
  278. (STAILQ_EMPTY((head)) ? \
  279. NULL : \
  280. ((struct type *)(void *) \
  281. ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
  282. #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
  283. #define STAILQ_REMOVE(head, elm, type, field) do { \
  284. if (STAILQ_FIRST((head)) == (elm)) { \
  285. STAILQ_REMOVE_HEAD((head), field); \
  286. } \
  287. else { \
  288. struct type *curelm = STAILQ_FIRST((head)); \
  289. while (STAILQ_NEXT(curelm, field) != (elm)) \
  290. curelm = STAILQ_NEXT(curelm, field); \
  291. if ((STAILQ_NEXT(curelm, field) = \
  292. STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
  293. (head)->stqh_last = &STAILQ_NEXT((curelm), field);\
  294. } \
  295. TRASHIT((elm)->field.stqe_next); \
  296. } while (0)
  297. #define STAILQ_REMOVE_HEAD(head, field) do { \
  298. if ((STAILQ_FIRST((head)) = \
  299. STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
  300. (head)->stqh_last = &STAILQ_FIRST((head)); \
  301. } while (0)
  302. #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
  303. if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
  304. (head)->stqh_last = &STAILQ_FIRST((head)); \
  305. } while (0)
  306. /*
  307. * List declarations.
  308. */
  309. #define LIST_HEAD(name, type) \
  310. struct name { \
  311. struct type *lh_first; /* first element */ \
  312. }
  313. #define LIST_HEAD_INITIALIZER(head) \
  314. { NULL }
  315. #define LIST_ENTRY(type) \
  316. struct { \
  317. struct type *le_next; /* next element */ \
  318. struct type **le_prev; /* address of previous next element */ \
  319. }
  320. /*
  321. * List functions.
  322. */
  323. #if (defined(_KERNEL) && defined(INVARIANTS)) || defined(QUEUE_MACRO_DEBUG)
  324. #define QMD_LIST_CHECK_HEAD(head, field) do { \
  325. if (LIST_FIRST((head)) != NULL && \
  326. LIST_FIRST((head))->field.le_prev != \
  327. &LIST_FIRST((head))) \
  328. panic("Bad list head %p first->prev != head", (head)); \
  329. } while (0)
  330. #define QMD_LIST_CHECK_NEXT(elm, field) do { \
  331. if (LIST_NEXT((elm), field) != NULL && \
  332. LIST_NEXT((elm), field)->field.le_prev != \
  333. &((elm)->field.le_next)) \
  334. panic("Bad link elm %p next->prev != elm", (elm)); \
  335. } while (0)
  336. #define QMD_LIST_CHECK_PREV(elm, field) do { \
  337. if (*(elm)->field.le_prev != (elm)) \
  338. panic("Bad link elm %p prev->next != elm", (elm)); \
  339. } while (0)
  340. #else
  341. #define QMD_LIST_CHECK_HEAD(head, field)
  342. #define QMD_LIST_CHECK_NEXT(elm, field)
  343. #define QMD_LIST_CHECK_PREV(elm, field)
  344. #endif /* (_KERNEL && INVARIANTS) || QUEUE_MACRO_DEBUG */
  345. #define LIST_EMPTY(head) ((head)->lh_first == NULL)
  346. #define LIST_FIRST(head) ((head)->lh_first)
  347. #define LIST_FOREACH(var, head, field) \
  348. for ((var) = LIST_FIRST((head)); \
  349. (var); \
  350. (var) = LIST_NEXT((var), field))
  351. #define LIST_FOREACH_SAFE(var, head, field, tvar) \
  352. for ((var) = LIST_FIRST((head)); \
  353. (var) && ((tvar) = LIST_NEXT((var), field), 1); \
  354. (var) = (tvar))
  355. #define LIST_INIT(head) do { \
  356. LIST_FIRST((head)) = NULL; \
  357. } while (0)
  358. #define LIST_INSERT_AFTER(listelm, elm, field) do { \
  359. QMD_LIST_CHECK_NEXT(listelm, field); \
  360. if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
  361. LIST_NEXT((listelm), field)->field.le_prev = \
  362. &LIST_NEXT((elm), field); \
  363. LIST_NEXT((listelm), field) = (elm); \
  364. (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
  365. } while (0)
  366. #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
  367. QMD_LIST_CHECK_PREV(listelm, field); \
  368. (elm)->field.le_prev = (listelm)->field.le_prev; \
  369. LIST_NEXT((elm), field) = (listelm); \
  370. *(listelm)->field.le_prev = (elm); \
  371. (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
  372. } while (0)
  373. #define LIST_INSERT_HEAD(head, elm, field) do { \
  374. QMD_LIST_CHECK_HEAD((head), field); \
  375. if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
  376. LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
  377. LIST_FIRST((head)) = (elm); \
  378. (elm)->field.le_prev = &LIST_FIRST((head)); \
  379. } while (0)
  380. #define LIST_NEXT(elm, field) ((elm)->field.le_next)
  381. #define LIST_REMOVE(elm, field) do { \
  382. QMD_LIST_CHECK_NEXT(elm, field); \
  383. QMD_LIST_CHECK_PREV(elm, field); \
  384. if (LIST_NEXT((elm), field) != NULL) \
  385. LIST_NEXT((elm), field)->field.le_prev = \
  386. (elm)->field.le_prev; \
  387. *(elm)->field.le_prev = LIST_NEXT((elm), field); \
  388. TRASHIT((elm)->field.le_next); \
  389. TRASHIT((elm)->field.le_prev); \
  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. #define TAILQ_CONCAT(head1, head2, field) do { \
  412. if (!TAILQ_EMPTY(head2)) { \
  413. *(head1)->tqh_last = (head2)->tqh_first; \
  414. (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
  415. (head1)->tqh_last = (head2)->tqh_last; \
  416. TAILQ_INIT((head2)); \
  417. QMD_TRACE_HEAD(head1); \
  418. QMD_TRACE_HEAD(head2); \
  419. } \
  420. } while (0)
  421. #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
  422. #define TAILQ_FIRST(head) ((head)->tqh_first)
  423. #define TAILQ_FOREACH(var, head, field) \
  424. for ((var) = TAILQ_FIRST((head)); \
  425. (var); \
  426. (var) = TAILQ_NEXT((var), field))
  427. #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
  428. for ((var) = TAILQ_FIRST((head)); \
  429. (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
  430. (var) = (tvar))
  431. #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  432. for ((var) = TAILQ_LAST((head), headname); \
  433. (var); \
  434. (var) = TAILQ_PREV((var), headname, field))
  435. #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
  436. for ((var) = TAILQ_LAST((head), headname); \
  437. (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
  438. (var) = (tvar))
  439. #define TAILQ_INIT(head) do { \
  440. TAILQ_FIRST((head)) = NULL; \
  441. (head)->tqh_last = &TAILQ_FIRST((head)); \
  442. QMD_TRACE_HEAD(head); \
  443. } while (0)
  444. #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  445. if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
  446. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  447. &TAILQ_NEXT((elm), field); \
  448. else { \
  449. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  450. QMD_TRACE_HEAD(head); \
  451. } \
  452. TAILQ_NEXT((listelm), field) = (elm); \
  453. (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
  454. QMD_TRACE_ELEM(&(elm)->field); \
  455. QMD_TRACE_ELEM(&listelm->field); \
  456. } while (0)
  457. #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  458. (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  459. TAILQ_NEXT((elm), field) = (listelm); \
  460. *(listelm)->field.tqe_prev = (elm); \
  461. (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
  462. QMD_TRACE_ELEM(&(elm)->field); \
  463. QMD_TRACE_ELEM(&listelm->field); \
  464. } while (0)
  465. #define TAILQ_INSERT_HEAD(head, elm, field) do { \
  466. if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
  467. TAILQ_FIRST((head))->field.tqe_prev = \
  468. &TAILQ_NEXT((elm), field); \
  469. else \
  470. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  471. TAILQ_FIRST((head)) = (elm); \
  472. (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
  473. QMD_TRACE_HEAD(head); \
  474. QMD_TRACE_ELEM(&(elm)->field); \
  475. } while (0)
  476. #define TAILQ_INSERT_TAIL(head, elm, field) do { \
  477. TAILQ_NEXT((elm), field) = NULL; \
  478. (elm)->field.tqe_prev = (head)->tqh_last; \
  479. *(head)->tqh_last = (elm); \
  480. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  481. QMD_TRACE_HEAD(head); \
  482. QMD_TRACE_ELEM(&(elm)->field); \
  483. } while (0)
  484. #define TAILQ_LAST(head, headname) \
  485. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  486. #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  487. #define TAILQ_PREV(elm, headname, field) \
  488. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  489. #define TAILQ_REMOVE(head, elm, field) do { \
  490. if ((TAILQ_NEXT((elm), field)) != NULL) \
  491. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  492. (elm)->field.tqe_prev; \
  493. else { \
  494. (head)->tqh_last = (elm)->field.tqe_prev; \
  495. QMD_TRACE_HEAD(head); \
  496. } \
  497. *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
  498. TRASHIT((elm)->field.tqe_next); \
  499. TRASHIT((elm)->field.tqe_prev); \
  500. QMD_TRACE_ELEM(&(elm)->field); \
  501. } while (0)
  502. /*
  503. * Circular queue definitions.
  504. */
  505. #define CIRCLEQ_HEAD(name, type) \
  506. struct name { \
  507. struct type *cqh_first; /* first element */ \
  508. struct type *cqh_last; /* last element */ \
  509. }
  510. #define CIRCLEQ_ENTRY(type) \
  511. struct { \
  512. struct type *cqe_next; /* next element */ \
  513. struct type *cqe_prev; /* previous element */ \
  514. }
  515. /*
  516. * Circular queue functions.
  517. */
  518. #define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
  519. #define CIRCLEQ_FIRST(head) ((head)->cqh_first)
  520. #define CIRCLEQ_FOREACH(var, head, field) \
  521. for((var) = (head)->cqh_first; \
  522. (var) != (void *)(head); \
  523. (var) = (var)->field.cqe_next)
  524. #define CIRCLEQ_INIT(head) do { \
  525. (head)->cqh_first = (void *)(head); \
  526. (head)->cqh_last = (void *)(head); \
  527. } while (0)
  528. #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  529. (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  530. (elm)->field.cqe_prev = (listelm); \
  531. if ((listelm)->field.cqe_next == (void *)(head)) \
  532. (head)->cqh_last = (elm); \
  533. else \
  534. (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  535. (listelm)->field.cqe_next = (elm); \
  536. } while (0)
  537. #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  538. (elm)->field.cqe_next = (listelm); \
  539. (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  540. if ((listelm)->field.cqe_prev == (void *)(head)) \
  541. (head)->cqh_first = (elm); \
  542. else \
  543. (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  544. (listelm)->field.cqe_prev = (elm); \
  545. } while (0)
  546. #define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  547. (elm)->field.cqe_next = (head)->cqh_first; \
  548. (elm)->field.cqe_prev = (void *)(head); \
  549. if ((head)->cqh_last == (void *)(head)) \
  550. (head)->cqh_last = (elm); \
  551. else \
  552. (head)->cqh_first->field.cqe_prev = (elm); \
  553. (head)->cqh_first = (elm); \
  554. } while (0)
  555. #define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  556. (elm)->field.cqe_next = (void *)(head); \
  557. (elm)->field.cqe_prev = (head)->cqh_last; \
  558. if ((head)->cqh_first == (void *)(head)) \
  559. (head)->cqh_first = (elm); \
  560. else \
  561. (head)->cqh_last->field.cqe_next = (elm); \
  562. (head)->cqh_last = (elm); \
  563. } while (0)
  564. #define CIRCLEQ_LAST(head) ((head)->cqh_last)
  565. #define CIRCLEQ_NEXT(elm,field) ((elm)->field.cqe_next)
  566. #define CIRCLEQ_PREV(elm,field) ((elm)->field.cqe_prev)
  567. #define CIRCLEQ_REMOVE(head, elm, field) do { \
  568. if ((elm)->field.cqe_next == (void *)(head)) \
  569. (head)->cqh_last = (elm)->field.cqe_prev; \
  570. else \
  571. (elm)->field.cqe_next->field.cqe_prev = \
  572. (elm)->field.cqe_prev; \
  573. if ((elm)->field.cqe_prev == (void *)(head)) \
  574. (head)->cqh_first = (elm)->field.cqe_next; \
  575. else \
  576. (elm)->field.cqe_prev->field.cqe_next = \
  577. (elm)->field.cqe_next; \
  578. } while (0)
  579. #ifdef _KERNEL
  580. #if NOTFB31
  581. /*
  582. * XXX insque() and remque() are an old way of handling certain queues.
  583. * They bogusly assumes that all queue heads look alike.
  584. */
  585. struct quehead {
  586. struct quehead *qh_link;
  587. struct quehead *qh_rlink;
  588. };
  589. #ifdef __GNUC__
  590. static __inline void
  591. insque(void *a, void *b)
  592. {
  593. struct quehead *element = (struct quehead *)a,
  594. *head = (struct quehead *)b;
  595. element->qh_link = head->qh_link;
  596. element->qh_rlink = head;
  597. head->qh_link = element;
  598. element->qh_link->qh_rlink = element;
  599. }
  600. static __inline void
  601. remque(void *a)
  602. {
  603. struct quehead *element = (struct quehead *)a;
  604. element->qh_link->qh_rlink = element->qh_rlink;
  605. element->qh_rlink->qh_link = element->qh_link;
  606. element->qh_rlink = 0;
  607. }
  608. #else /* !__GNUC__ */
  609. void insque(void *a, void *b);
  610. void remque(void *a);
  611. #endif /* __GNUC__ */
  612. #endif
  613. #endif /* _KERNEL */
  614. #endif /* !_SYS_QUEUE_H_ */