Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

queue.h 30KB

il y a 3 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*-
  2. * Copyright (c) 1991, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * @(#)queue.h 8.5 (Berkeley) 8/20/94
  30. * $FreeBSD$
  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 be traversed in either 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. * Below is a summary of implemented functions where:
  77. * + means the macro is available
  78. * - means the macro is not available
  79. * s means the macro is available but is slow (runs in O(n) time)
  80. *
  81. * SLIST LIST STAILQ TAILQ
  82. * _HEAD + + + +
  83. * _CLASS_HEAD + + + +
  84. * _HEAD_INITIALIZER + + + +
  85. * _ENTRY + + + +
  86. * _CLASS_ENTRY + + + +
  87. * _INIT + + + +
  88. * _EMPTY + + + +
  89. * _FIRST + + + +
  90. * _NEXT + + + +
  91. * _PREV - + - +
  92. * _LAST - - + +
  93. * _LAST_FAST - - - +
  94. * _FOREACH + + + +
  95. * _FOREACH_FROM + + + +
  96. * _FOREACH_SAFE + + + +
  97. * _FOREACH_FROM_SAFE + + + +
  98. * _FOREACH_REVERSE - - - +
  99. * _FOREACH_REVERSE_FROM - - - +
  100. * _FOREACH_REVERSE_SAFE - - - +
  101. * _FOREACH_REVERSE_FROM_SAFE - - - +
  102. * _INSERT_HEAD + + + +
  103. * _INSERT_BEFORE - + - +
  104. * _INSERT_AFTER + + + +
  105. * _INSERT_TAIL - - + +
  106. * _CONCAT s s + +
  107. * _REMOVE_AFTER + - + -
  108. * _REMOVE_HEAD + - + -
  109. * _REMOVE s + s +
  110. * _SWAP + + + +
  111. *
  112. */
  113. #ifdef QUEUE_MACRO_DEBUG
  114. #warn Use QUEUE_MACRO_DEBUG_TRACE and/or QUEUE_MACRO_DEBUG_TRASH
  115. #define QUEUE_MACRO_DEBUG_TRACE
  116. #define QUEUE_MACRO_DEBUG_TRASH
  117. #endif
  118. #ifdef QUEUE_MACRO_DEBUG_TRACE
  119. /* Store the last 2 places the queue element or head was altered */
  120. struct qm_trace {
  121. unsigned long lastline;
  122. unsigned long prevline;
  123. const char *lastfile;
  124. const char *prevfile;
  125. };
  126. #define TRACEBUF struct qm_trace trace;
  127. #define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } ,
  128. #define QMD_TRACE_HEAD(head) do { \
  129. (head)->trace.prevline = (head)->trace.lastline; \
  130. (head)->trace.prevfile = (head)->trace.lastfile; \
  131. (head)->trace.lastline = __LINE__; \
  132. (head)->trace.lastfile = __FILE__; \
  133. } while (0)
  134. #define QMD_TRACE_ELEM(elem) do { \
  135. (elem)->trace.prevline = (elem)->trace.lastline; \
  136. (elem)->trace.prevfile = (elem)->trace.lastfile; \
  137. (elem)->trace.lastline = __LINE__; \
  138. (elem)->trace.lastfile = __FILE__; \
  139. } while (0)
  140. #else /* !QUEUE_MACRO_DEBUG_TRACE */
  141. #define QMD_TRACE_ELEM(elem)
  142. #define QMD_TRACE_HEAD(head)
  143. #define TRACEBUF
  144. #define TRACEBUF_INITIALIZER
  145. #endif /* QUEUE_MACRO_DEBUG_TRACE */
  146. #ifdef QUEUE_MACRO_DEBUG_TRASH
  147. #define TRASHIT(x) do {(x) = (void *)-1;} while (0)
  148. #define QMD_IS_TRASHED(x) ((x) == (void *)(intptr_t)-1)
  149. #else /* !QUEUE_MACRO_DEBUG_TRASH */
  150. #define TRASHIT(x)
  151. #define QMD_IS_TRASHED(x) 0
  152. #endif /* QUEUE_MACRO_DEBUG_TRASH */
  153. #if defined(QUEUE_MACRO_DEBUG_TRACE) || defined(QUEUE_MACRO_DEBUG_TRASH)
  154. #define QMD_SAVELINK(name, link) void **name = (void *)&(link)
  155. #else /* !QUEUE_MACRO_DEBUG_TRACE && !QUEUE_MACRO_DEBUG_TRASH */
  156. #define QMD_SAVELINK(name, link)
  157. #endif /* QUEUE_MACRO_DEBUG_TRACE || QUEUE_MACRO_DEBUG_TRASH */
  158. #ifdef __cplusplus
  159. /*
  160. * In C++ there can be structure lists and class lists:
  161. */
  162. #define QUEUE_TYPEOF(type) type
  163. #else
  164. #define QUEUE_TYPEOF(type) struct type
  165. #endif
  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_CLASS_HEAD(name, type) \
  174. struct name { \
  175. class type *slh_first; /* first element */ \
  176. }
  177. #define SLIST_HEAD_INITIALIZER(head) \
  178. { NULL }
  179. #define SLIST_ENTRY(type) \
  180. struct { \
  181. struct type *sle_next; /* next element */ \
  182. }
  183. #define SLIST_CLASS_ENTRY(type) \
  184. struct { \
  185. class type *sle_next; /* next element */ \
  186. }
  187. /*
  188. * Singly-linked List functions.
  189. */
  190. #if (defined(_KERNEL) && defined(INVARIANTS))
  191. #define QMD_SLIST_CHECK_PREVPTR(prevp, elm) do { \
  192. if (*(prevp) != (elm)) \
  193. panic("Bad prevptr *(%p) == %p != %p", \
  194. (prevp), *(prevp), (elm)); \
  195. } while (0)
  196. #else
  197. #define QMD_SLIST_CHECK_PREVPTR(prevp, elm)
  198. #endif
  199. #define SLIST_CONCAT(head1, head2, type, field) do { \
  200. QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head1); \
  201. if (curelm == NULL) { \
  202. if ((SLIST_FIRST(head1) = SLIST_FIRST(head2)) != NULL) \
  203. SLIST_INIT(head2); \
  204. } else if (SLIST_FIRST(head2) != NULL) { \
  205. while (SLIST_NEXT(curelm, field) != NULL) \
  206. curelm = SLIST_NEXT(curelm, field); \
  207. SLIST_NEXT(curelm, field) = SLIST_FIRST(head2); \
  208. SLIST_INIT(head2); \
  209. } \
  210. } while (0)
  211. #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
  212. #define SLIST_FIRST(head) ((head)->slh_first)
  213. #define SLIST_FOREACH(var, head, field) \
  214. for ((var) = SLIST_FIRST((head)); \
  215. (var); \
  216. (var) = SLIST_NEXT((var), field))
  217. #define SLIST_FOREACH_FROM(var, head, field) \
  218. for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
  219. (var); \
  220. (var) = SLIST_NEXT((var), field))
  221. #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
  222. for ((var) = SLIST_FIRST((head)); \
  223. (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
  224. (var) = (tvar))
  225. #define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
  226. for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
  227. (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
  228. (var) = (tvar))
  229. #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  230. for ((varp) = &SLIST_FIRST((head)); \
  231. ((var) = *(varp)) != NULL; \
  232. (varp) = &SLIST_NEXT((var), field))
  233. #define SLIST_INIT(head) do { \
  234. SLIST_FIRST((head)) = NULL; \
  235. } while (0)
  236. #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  237. SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
  238. SLIST_NEXT((slistelm), field) = (elm); \
  239. } while (0)
  240. #define SLIST_INSERT_HEAD(head, elm, field) do { \
  241. SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
  242. SLIST_FIRST((head)) = (elm); \
  243. } while (0)
  244. #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  245. #define SLIST_REMOVE(head, elm, type, field) do { \
  246. QMD_SAVELINK(oldnext, (elm)->field.sle_next); \
  247. if (SLIST_FIRST((head)) == (elm)) { \
  248. SLIST_REMOVE_HEAD((head), field); \
  249. } \
  250. else { \
  251. QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \
  252. while (SLIST_NEXT(curelm, field) != (elm)) \
  253. curelm = SLIST_NEXT(curelm, field); \
  254. SLIST_REMOVE_AFTER(curelm, field); \
  255. } \
  256. TRASHIT(*oldnext); \
  257. } while (0)
  258. #define SLIST_REMOVE_AFTER(elm, field) do { \
  259. SLIST_NEXT(elm, field) = \
  260. SLIST_NEXT(SLIST_NEXT(elm, field), field); \
  261. } while (0)
  262. #define SLIST_REMOVE_HEAD(head, field) do { \
  263. SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
  264. } while (0)
  265. #define SLIST_REMOVE_PREVPTR(prevp, elm, field) do { \
  266. QMD_SLIST_CHECK_PREVPTR(prevp, elm); \
  267. *(prevp) = SLIST_NEXT(elm, field); \
  268. TRASHIT((elm)->field.sle_next); \
  269. } while (0)
  270. #define SLIST_SWAP(head1, head2, type) do { \
  271. QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \
  272. SLIST_FIRST(head1) = SLIST_FIRST(head2); \
  273. SLIST_FIRST(head2) = swap_first; \
  274. } while (0)
  275. /*
  276. * Singly-linked Tail queue declarations.
  277. */
  278. #define STAILQ_HEAD(name, type) \
  279. struct name { \
  280. struct type *stqh_first;/* first element */ \
  281. struct type **stqh_last;/* addr of last next element */ \
  282. }
  283. #define STAILQ_CLASS_HEAD(name, type) \
  284. struct name { \
  285. class type *stqh_first; /* first element */ \
  286. class type **stqh_last; /* addr of last next element */ \
  287. }
  288. #define STAILQ_HEAD_INITIALIZER(head) \
  289. { NULL, &(head).stqh_first }
  290. #define STAILQ_ENTRY(type) \
  291. struct { \
  292. struct type *stqe_next; /* next element */ \
  293. }
  294. #define STAILQ_CLASS_ENTRY(type) \
  295. struct { \
  296. class type *stqe_next; /* next element */ \
  297. }
  298. /*
  299. * Singly-linked Tail queue functions.
  300. */
  301. #define STAILQ_CONCAT(head1, head2) do { \
  302. if (!STAILQ_EMPTY((head2))) { \
  303. *(head1)->stqh_last = (head2)->stqh_first; \
  304. (head1)->stqh_last = (head2)->stqh_last; \
  305. STAILQ_INIT((head2)); \
  306. } \
  307. } while (0)
  308. #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
  309. #define STAILQ_FIRST(head) ((head)->stqh_first)
  310. #define STAILQ_FOREACH(var, head, field) \
  311. for((var) = STAILQ_FIRST((head)); \
  312. (var); \
  313. (var) = STAILQ_NEXT((var), field))
  314. #define STAILQ_FOREACH_FROM(var, head, field) \
  315. for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
  316. (var); \
  317. (var) = STAILQ_NEXT((var), field))
  318. #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
  319. for ((var) = STAILQ_FIRST((head)); \
  320. (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
  321. (var) = (tvar))
  322. #define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
  323. for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
  324. (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
  325. (var) = (tvar))
  326. #define STAILQ_INIT(head) do { \
  327. STAILQ_FIRST((head)) = NULL; \
  328. (head)->stqh_last = &STAILQ_FIRST((head)); \
  329. } while (0)
  330. #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
  331. if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
  332. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  333. STAILQ_NEXT((tqelm), field) = (elm); \
  334. } while (0)
  335. #define STAILQ_INSERT_HEAD(head, elm, field) do { \
  336. if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
  337. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  338. STAILQ_FIRST((head)) = (elm); \
  339. } while (0)
  340. #define STAILQ_INSERT_TAIL(head, elm, field) do { \
  341. STAILQ_NEXT((elm), field) = NULL; \
  342. *(head)->stqh_last = (elm); \
  343. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  344. } while (0)
  345. #define STAILQ_LAST(head, type, field) \
  346. (STAILQ_EMPTY((head)) ? NULL : \
  347. __containerof((head)->stqh_last, \
  348. QUEUE_TYPEOF(type), field.stqe_next))
  349. #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
  350. #define STAILQ_REMOVE(head, elm, type, field) do { \
  351. QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \
  352. if (STAILQ_FIRST((head)) == (elm)) { \
  353. STAILQ_REMOVE_HEAD((head), field); \
  354. } \
  355. else { \
  356. QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \
  357. while (STAILQ_NEXT(curelm, field) != (elm)) \
  358. curelm = STAILQ_NEXT(curelm, field); \
  359. STAILQ_REMOVE_AFTER(head, curelm, field); \
  360. } \
  361. TRASHIT(*oldnext); \
  362. } while (0)
  363. #define STAILQ_REMOVE_AFTER(head, elm, field) do { \
  364. if ((STAILQ_NEXT(elm, field) = \
  365. STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
  366. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  367. } while (0)
  368. #define STAILQ_REMOVE_HEAD(head, field) do { \
  369. if ((STAILQ_FIRST((head)) = \
  370. STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
  371. (head)->stqh_last = &STAILQ_FIRST((head)); \
  372. } while (0)
  373. #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
  374. if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
  375. (head)->stqh_last = &STAILQ_FIRST((head)); \
  376. } while (0)
  377. #define STAILQ_SWAP(head1, head2, type) do { \
  378. QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \
  379. QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \
  380. STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
  381. (head1)->stqh_last = (head2)->stqh_last; \
  382. STAILQ_FIRST(head2) = swap_first; \
  383. (head2)->stqh_last = swap_last; \
  384. if (STAILQ_EMPTY(head1)) \
  385. (head1)->stqh_last = &STAILQ_FIRST(head1); \
  386. if (STAILQ_EMPTY(head2)) \
  387. (head2)->stqh_last = &STAILQ_FIRST(head2); \
  388. } while (0)
  389. /*
  390. * List declarations.
  391. */
  392. #define LIST_HEAD(name, type) \
  393. struct name { \
  394. struct type *lh_first; /* first element */ \
  395. }
  396. #define LIST_CLASS_HEAD(name, type) \
  397. struct name { \
  398. class type *lh_first; /* first element */ \
  399. }
  400. #define LIST_HEAD_INITIALIZER(head) \
  401. { NULL }
  402. #define LIST_ENTRY(type) \
  403. struct { \
  404. struct type *le_next; /* next element */ \
  405. struct type **le_prev; /* address of previous next element */ \
  406. }
  407. #define LIST_CLASS_ENTRY(type) \
  408. struct { \
  409. class type *le_next; /* next element */ \
  410. class type **le_prev; /* address of previous next element */ \
  411. }
  412. /*
  413. * List functions.
  414. */
  415. #if (defined(_KERNEL) && defined(INVARIANTS))
  416. /*
  417. * QMD_LIST_CHECK_HEAD(LIST_HEAD *head, LIST_ENTRY NAME)
  418. *
  419. * If the list is non-empty, validates that the first element of the list
  420. * points back at 'head.'
  421. */
  422. #define QMD_LIST_CHECK_HEAD(head, field) do { \
  423. if (LIST_FIRST((head)) != NULL && \
  424. LIST_FIRST((head))->field.le_prev != \
  425. &LIST_FIRST((head))) \
  426. panic("Bad list head %p first->prev != head", (head)); \
  427. } while (0)
  428. /*
  429. * QMD_LIST_CHECK_NEXT(TYPE *elm, LIST_ENTRY NAME)
  430. *
  431. * If an element follows 'elm' in the list, validates that the next element
  432. * points back at 'elm.'
  433. */
  434. #define QMD_LIST_CHECK_NEXT(elm, field) do { \
  435. if (LIST_NEXT((elm), field) != NULL && \
  436. LIST_NEXT((elm), field)->field.le_prev != \
  437. &((elm)->field.le_next)) \
  438. panic("Bad link elm %p next->prev != elm", (elm)); \
  439. } while (0)
  440. /*
  441. * QMD_LIST_CHECK_PREV(TYPE *elm, LIST_ENTRY NAME)
  442. *
  443. * Validates that the previous element (or head of the list) points to 'elm.'
  444. */
  445. #define QMD_LIST_CHECK_PREV(elm, field) do { \
  446. if (*(elm)->field.le_prev != (elm)) \
  447. panic("Bad link elm %p prev->next != elm", (elm)); \
  448. } while (0)
  449. #else
  450. #define QMD_LIST_CHECK_HEAD(head, field)
  451. #define QMD_LIST_CHECK_NEXT(elm, field)
  452. #define QMD_LIST_CHECK_PREV(elm, field)
  453. #endif /* (_KERNEL && INVARIANTS) */
  454. #define LIST_CONCAT(head1, head2, type, field) do { \
  455. QUEUE_TYPEOF(type) *curelm = LIST_FIRST(head1); \
  456. if (curelm == NULL) { \
  457. if ((LIST_FIRST(head1) = LIST_FIRST(head2)) != NULL) { \
  458. LIST_FIRST(head2)->field.le_prev = \
  459. &LIST_FIRST((head1)); \
  460. LIST_INIT(head2); \
  461. } \
  462. } else if (LIST_FIRST(head2) != NULL) { \
  463. while (LIST_NEXT(curelm, field) != NULL) \
  464. curelm = LIST_NEXT(curelm, field); \
  465. LIST_NEXT(curelm, field) = LIST_FIRST(head2); \
  466. LIST_FIRST(head2)->field.le_prev = &LIST_NEXT(curelm, field); \
  467. LIST_INIT(head2); \
  468. } \
  469. } while (0)
  470. #define LIST_EMPTY(head) ((head)->lh_first == NULL)
  471. #define LIST_FIRST(head) ((head)->lh_first)
  472. #define LIST_FOREACH(var, head, field) \
  473. for ((var) = LIST_FIRST((head)); \
  474. (var); \
  475. (var) = LIST_NEXT((var), field))
  476. #define LIST_FOREACH_FROM(var, head, field) \
  477. for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
  478. (var); \
  479. (var) = LIST_NEXT((var), field))
  480. #define LIST_FOREACH_SAFE(var, head, field, tvar) \
  481. for ((var) = LIST_FIRST((head)); \
  482. (var) && ((tvar) = LIST_NEXT((var), field), 1); \
  483. (var) = (tvar))
  484. #define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
  485. for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
  486. (var) && ((tvar) = LIST_NEXT((var), field), 1); \
  487. (var) = (tvar))
  488. #define LIST_INIT(head) do { \
  489. LIST_FIRST((head)) = NULL; \
  490. } while (0)
  491. #define LIST_INSERT_AFTER(listelm, elm, field) do { \
  492. QMD_LIST_CHECK_NEXT(listelm, field); \
  493. if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
  494. LIST_NEXT((listelm), field)->field.le_prev = \
  495. &LIST_NEXT((elm), field); \
  496. LIST_NEXT((listelm), field) = (elm); \
  497. (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
  498. } while (0)
  499. #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
  500. QMD_LIST_CHECK_PREV(listelm, field); \
  501. (elm)->field.le_prev = (listelm)->field.le_prev; \
  502. LIST_NEXT((elm), field) = (listelm); \
  503. *(listelm)->field.le_prev = (elm); \
  504. (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
  505. } while (0)
  506. #define LIST_INSERT_HEAD(head, elm, field) do { \
  507. QMD_LIST_CHECK_HEAD((head), field); \
  508. if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
  509. LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
  510. LIST_FIRST((head)) = (elm); \
  511. (elm)->field.le_prev = &LIST_FIRST((head)); \
  512. } while (0)
  513. #define LIST_NEXT(elm, field) ((elm)->field.le_next)
  514. #define LIST_PREV(elm, head, type, field) \
  515. ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \
  516. __containerof((elm)->field.le_prev, \
  517. QUEUE_TYPEOF(type), field.le_next))
  518. #define LIST_REMOVE(elm, field) do { \
  519. QMD_SAVELINK(oldnext, (elm)->field.le_next); \
  520. QMD_SAVELINK(oldprev, (elm)->field.le_prev); \
  521. QMD_LIST_CHECK_NEXT(elm, field); \
  522. QMD_LIST_CHECK_PREV(elm, field); \
  523. if (LIST_NEXT((elm), field) != NULL) \
  524. LIST_NEXT((elm), field)->field.le_prev = \
  525. (elm)->field.le_prev; \
  526. *(elm)->field.le_prev = LIST_NEXT((elm), field); \
  527. TRASHIT(*oldnext); \
  528. TRASHIT(*oldprev); \
  529. } while (0)
  530. #define LIST_SWAP(head1, head2, type, field) do { \
  531. QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \
  532. LIST_FIRST((head1)) = LIST_FIRST((head2)); \
  533. LIST_FIRST((head2)) = swap_tmp; \
  534. if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
  535. swap_tmp->field.le_prev = &LIST_FIRST((head1)); \
  536. if ((swap_tmp = LIST_FIRST((head2))) != NULL) \
  537. swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
  538. } while (0)
  539. /*
  540. * Tail queue declarations.
  541. */
  542. #define TAILQ_HEAD(name, type) \
  543. struct name { \
  544. struct type *tqh_first; /* first element */ \
  545. struct type **tqh_last; /* addr of last next element */ \
  546. TRACEBUF \
  547. }
  548. #define TAILQ_CLASS_HEAD(name, type) \
  549. struct name { \
  550. class type *tqh_first; /* first element */ \
  551. class type **tqh_last; /* addr of last next element */ \
  552. TRACEBUF \
  553. }
  554. #define TAILQ_HEAD_INITIALIZER(head) \
  555. { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER }
  556. #define TAILQ_ENTRY(type) \
  557. struct { \
  558. struct type *tqe_next; /* next element */ \
  559. struct type **tqe_prev; /* address of previous next element */ \
  560. TRACEBUF \
  561. }
  562. #define TAILQ_CLASS_ENTRY(type) \
  563. struct { \
  564. class type *tqe_next; /* next element */ \
  565. class type **tqe_prev; /* address of previous next element */ \
  566. TRACEBUF \
  567. }
  568. /*
  569. * Tail queue functions.
  570. */
  571. #if (defined(_KERNEL) && defined(INVARIANTS))
  572. /*
  573. * QMD_TAILQ_CHECK_HEAD(TAILQ_HEAD *head, TAILQ_ENTRY NAME)
  574. *
  575. * If the tailq is non-empty, validates that the first element of the tailq
  576. * points back at 'head.'
  577. */
  578. #define QMD_TAILQ_CHECK_HEAD(head, field) do { \
  579. if (!TAILQ_EMPTY(head) && \
  580. TAILQ_FIRST((head))->field.tqe_prev != \
  581. &TAILQ_FIRST((head))) \
  582. panic("Bad tailq head %p first->prev != head", (head)); \
  583. } while (0)
  584. /*
  585. * QMD_TAILQ_CHECK_TAIL(TAILQ_HEAD *head, TAILQ_ENTRY NAME)
  586. *
  587. * Validates that the tail of the tailq is a pointer to pointer to NULL.
  588. */
  589. #define QMD_TAILQ_CHECK_TAIL(head, field) do { \
  590. if (*(head)->tqh_last != NULL) \
  591. panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \
  592. } while (0)
  593. /*
  594. * QMD_TAILQ_CHECK_NEXT(TYPE *elm, TAILQ_ENTRY NAME)
  595. *
  596. * If an element follows 'elm' in the tailq, validates that the next element
  597. * points back at 'elm.'
  598. */
  599. #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
  600. if (TAILQ_NEXT((elm), field) != NULL && \
  601. TAILQ_NEXT((elm), field)->field.tqe_prev != \
  602. &((elm)->field.tqe_next)) \
  603. panic("Bad link elm %p next->prev != elm", (elm)); \
  604. } while (0)
  605. /*
  606. * QMD_TAILQ_CHECK_PREV(TYPE *elm, TAILQ_ENTRY NAME)
  607. *
  608. * Validates that the previous element (or head of the tailq) points to 'elm.'
  609. */
  610. #define QMD_TAILQ_CHECK_PREV(elm, field) do { \
  611. if (*(elm)->field.tqe_prev != (elm)) \
  612. panic("Bad link elm %p prev->next != elm", (elm)); \
  613. } while (0)
  614. #else
  615. #define QMD_TAILQ_CHECK_HEAD(head, field)
  616. #define QMD_TAILQ_CHECK_TAIL(head, headname)
  617. #define QMD_TAILQ_CHECK_NEXT(elm, field)
  618. #define QMD_TAILQ_CHECK_PREV(elm, field)
  619. #endif /* (_KERNEL && INVARIANTS) */
  620. #define TAILQ_CONCAT(head1, head2, field) do { \
  621. if (!TAILQ_EMPTY(head2)) { \
  622. *(head1)->tqh_last = (head2)->tqh_first; \
  623. (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
  624. (head1)->tqh_last = (head2)->tqh_last; \
  625. TAILQ_INIT((head2)); \
  626. QMD_TRACE_HEAD(head1); \
  627. QMD_TRACE_HEAD(head2); \
  628. } \
  629. } while (0)
  630. #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
  631. #define TAILQ_FIRST(head) ((head)->tqh_first)
  632. #define TAILQ_FOREACH(var, head, field) \
  633. for ((var) = TAILQ_FIRST((head)); \
  634. (var); \
  635. (var) = TAILQ_NEXT((var), field))
  636. #define TAILQ_FOREACH_FROM(var, head, field) \
  637. for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
  638. (var); \
  639. (var) = TAILQ_NEXT((var), field))
  640. #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
  641. for ((var) = TAILQ_FIRST((head)); \
  642. (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
  643. (var) = (tvar))
  644. #define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
  645. for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
  646. (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
  647. (var) = (tvar))
  648. #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  649. for ((var) = TAILQ_LAST((head), headname); \
  650. (var); \
  651. (var) = TAILQ_PREV((var), headname, field))
  652. #define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \
  653. for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
  654. (var); \
  655. (var) = TAILQ_PREV((var), headname, field))
  656. #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
  657. for ((var) = TAILQ_LAST((head), headname); \
  658. (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
  659. (var) = (tvar))
  660. #define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \
  661. for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
  662. (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
  663. (var) = (tvar))
  664. #define TAILQ_INIT(head) do { \
  665. TAILQ_FIRST((head)) = NULL; \
  666. (head)->tqh_last = &TAILQ_FIRST((head)); \
  667. QMD_TRACE_HEAD(head); \
  668. } while (0)
  669. #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  670. QMD_TAILQ_CHECK_NEXT(listelm, field); \
  671. if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
  672. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  673. &TAILQ_NEXT((elm), field); \
  674. else { \
  675. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  676. QMD_TRACE_HEAD(head); \
  677. } \
  678. TAILQ_NEXT((listelm), field) = (elm); \
  679. (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
  680. QMD_TRACE_ELEM(&(elm)->field); \
  681. QMD_TRACE_ELEM(&(listelm)->field); \
  682. } while (0)
  683. #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  684. QMD_TAILQ_CHECK_PREV(listelm, field); \
  685. (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  686. TAILQ_NEXT((elm), field) = (listelm); \
  687. *(listelm)->field.tqe_prev = (elm); \
  688. (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
  689. QMD_TRACE_ELEM(&(elm)->field); \
  690. QMD_TRACE_ELEM(&(listelm)->field); \
  691. } while (0)
  692. #define TAILQ_INSERT_HEAD(head, elm, field) do { \
  693. QMD_TAILQ_CHECK_HEAD(head, field); \
  694. if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
  695. TAILQ_FIRST((head))->field.tqe_prev = \
  696. &TAILQ_NEXT((elm), field); \
  697. else \
  698. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  699. TAILQ_FIRST((head)) = (elm); \
  700. (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
  701. QMD_TRACE_HEAD(head); \
  702. QMD_TRACE_ELEM(&(elm)->field); \
  703. } while (0)
  704. #define TAILQ_INSERT_TAIL(head, elm, field) do { \
  705. QMD_TAILQ_CHECK_TAIL(head, field); \
  706. TAILQ_NEXT((elm), field) = NULL; \
  707. (elm)->field.tqe_prev = (head)->tqh_last; \
  708. *(head)->tqh_last = (elm); \
  709. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  710. QMD_TRACE_HEAD(head); \
  711. QMD_TRACE_ELEM(&(elm)->field); \
  712. } while (0)
  713. #define TAILQ_LAST(head, headname) \
  714. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  715. /*
  716. * The FAST function is fast in that it causes no data access other
  717. * then the access to the head. The standard LAST function above
  718. * will cause a data access of both the element you want and
  719. * the previous element. FAST is very useful for instances when
  720. * you may want to prefetch the last data element.
  721. */
  722. #define TAILQ_LAST_FAST(head, type, field) \
  723. (TAILQ_EMPTY(head) ? NULL : __containerof((head)->tqh_last, QUEUE_TYPEOF(type), field.tqe_next))
  724. #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  725. #define TAILQ_PREV(elm, headname, field) \
  726. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  727. #define TAILQ_REMOVE(head, elm, field) do { \
  728. QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \
  729. QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \
  730. QMD_TAILQ_CHECK_NEXT(elm, field); \
  731. QMD_TAILQ_CHECK_PREV(elm, field); \
  732. if ((TAILQ_NEXT((elm), field)) != NULL) \
  733. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  734. (elm)->field.tqe_prev; \
  735. else { \
  736. (head)->tqh_last = (elm)->field.tqe_prev; \
  737. QMD_TRACE_HEAD(head); \
  738. } \
  739. *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
  740. TRASHIT(*oldnext); \
  741. TRASHIT(*oldprev); \
  742. QMD_TRACE_ELEM(&(elm)->field); \
  743. } while (0)
  744. #define TAILQ_SWAP(head1, head2, type, field) do { \
  745. QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \
  746. QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \
  747. (head1)->tqh_first = (head2)->tqh_first; \
  748. (head1)->tqh_last = (head2)->tqh_last; \
  749. (head2)->tqh_first = swap_first; \
  750. (head2)->tqh_last = swap_last; \
  751. if ((swap_first = (head1)->tqh_first) != NULL) \
  752. swap_first->field.tqe_prev = &(head1)->tqh_first; \
  753. else \
  754. (head1)->tqh_last = &(head1)->tqh_first; \
  755. if ((swap_first = (head2)->tqh_first) != NULL) \
  756. swap_first->field.tqe_prev = &(head2)->tqh_first; \
  757. else \
  758. (head2)->tqh_last = &(head2)->tqh_first; \
  759. } while (0)
  760. #ifdef _KERNEL
  761. /*
  762. * XXX insque() and remque() are an old way of handling certain queues.
  763. * They bogusly assumes that all queue heads look alike.
  764. */
  765. struct quehead {
  766. struct quehead *qh_link;
  767. struct quehead *qh_rlink;
  768. };
  769. #ifdef __GNUC__
  770. static __inline void
  771. insque(void *a, void *b)
  772. {
  773. struct quehead *element = (struct quehead *)a,
  774. *head = (struct quehead *)b;
  775. element->qh_link = head->qh_link;
  776. element->qh_rlink = head;
  777. head->qh_link = element;
  778. element->qh_link->qh_rlink = element;
  779. }
  780. static __inline void
  781. remque(void *a)
  782. {
  783. struct quehead *element = (struct quehead *)a;
  784. element->qh_link->qh_rlink = element->qh_rlink;
  785. element->qh_rlink->qh_link = element->qh_link;
  786. element->qh_rlink = 0;
  787. }
  788. #else /* !__GNUC__ */
  789. void insque(void *a, void *b);
  790. void remque(void *a);
  791. #endif /* __GNUC__ */
  792. #endif /* _KERNEL */
  793. #endif /* !_SYS_QUEUE_H_ */