compat-queue.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * <sys/queue.h> implementation for systems that don't have it.
  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. * $FreeBSD: src/sys/sys/queue.h,v 1.32.2.4 2001/03/31 03:33:39 hsu Exp $
  33. */
  34. #ifndef SYS_QUEUE_H
  35. #define SYS_QUEUE_H
  36. /*
  37. * This file defines four types of data structures: singly-linked lists,
  38. * singly-linked tail queues, lists and tail queues.
  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 singly-linked tail queue is headed by a pair of pointers, one to the
  51. * head of the list and the other to the tail of the list. The elements are
  52. * singly linked for minimum space and pointer manipulation overhead at the
  53. * expense of O(n) removal for arbitrary elements. New elements can be added
  54. * to the list after an existing element, at the head of the list, or at the
  55. * end of the list. Elements being removed from the head of the tail queue
  56. * should use the explicit macro for this purpose for optimum efficiency.
  57. * A singly-linked tail queue may only be traversed in the forward direction.
  58. * Singly-linked tail queues are ideal for applications with large datasets
  59. * and few or no removals or for implementing a FIFO queue.
  60. *
  61. * A list is headed by a single forward pointer (or an array of forward
  62. * pointers for a hash table header). The elements are doubly linked
  63. * so that an arbitrary element can be removed without a need to
  64. * traverse the list. New elements can be added to the list before
  65. * or after an existing element or at the head of the list. A list
  66. * may only be traversed in the forward direction.
  67. *
  68. * A tail queue is headed by a pair of pointers, one to the head of the
  69. * list and the other to the tail of the list. The elements are doubly
  70. * linked so that an arbitrary element can be removed without a need to
  71. * traverse the list. New elements can be added to the list before or
  72. * after an existing element, at the head of the list, or at the end of
  73. * the list. A tail queue may be traversed in either direction.
  74. *
  75. * For details on the use of these macros, see the queue(3) manual page.
  76. *
  77. *
  78. * SLIST LIST STAILQ TAILQ
  79. * _HEAD + + + +
  80. * _HEAD_INITIALIZER + + + +
  81. * _ENTRY + + + +
  82. * _INIT + + + +
  83. * _EMPTY + + + +
  84. * _FIRST + + + +
  85. * _NEXT + + + +
  86. * _PREV - - - +
  87. * _LAST - - + +
  88. * _FOREACH + + + +
  89. * _FOREACH_SAFE + + + +
  90. * _FOREACH_REVERSE - - - +
  91. * _FOREACH_REVERSE_SAFE - - - +
  92. * _INSERT_HEAD + + + +
  93. * _INSERT_BEFORE - + - +
  94. * _INSERT_AFTER + + + +
  95. * _INSERT_TAIL - - + +
  96. * _CONCAT - - + +
  97. * _REMOVE_HEAD + - + -
  98. * _REMOVE + + + +
  99. *
  100. */
  101. /*
  102. * Singly-linked List declarations.
  103. */
  104. #define SLIST_HEAD(name, type) \
  105. struct name { \
  106. struct type *slh_first; /* first element */ \
  107. }
  108. #define SLIST_HEAD_INITIALIZER(head) \
  109. { NULL }
  110. #define SLIST_ENTRY(type) \
  111. struct { \
  112. struct type *sle_next; /* next element */ \
  113. }
  114. /*
  115. * Singly-linked List functions.
  116. */
  117. #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
  118. #define SLIST_FIRST(head) ((head)->slh_first)
  119. #define SLIST_FOREACH(var, head, field) \
  120. for ((var) = SLIST_FIRST((head)); \
  121. (var); \
  122. (var) = SLIST_NEXT((var), field))
  123. #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
  124. for ((var) = SLIST_FIRST((head)); \
  125. (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
  126. (var) = (tvar))
  127. #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  128. for ((varp) = &SLIST_FIRST((head)); \
  129. ((var) = *(varp)) != NULL; \
  130. (varp) = &SLIST_NEXT((var), field))
  131. #define SLIST_INIT(head) do { \
  132. SLIST_FIRST((head)) = NULL; \
  133. } while (0)
  134. #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  135. SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
  136. SLIST_NEXT((slistelm), field) = (elm); \
  137. } while (0)
  138. #define SLIST_INSERT_HEAD(head, elm, field) do { \
  139. SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
  140. SLIST_FIRST((head)) = (elm); \
  141. } while (0)
  142. #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  143. #define SLIST_REMOVE(head, elm, type, field) do { \
  144. if (SLIST_FIRST((head)) == (elm)) { \
  145. SLIST_REMOVE_HEAD((head), field); \
  146. } \
  147. else { \
  148. struct type *curelm = SLIST_FIRST((head)); \
  149. while (SLIST_NEXT(curelm, field) != (elm)) \
  150. curelm = SLIST_NEXT(curelm, field); \
  151. SLIST_NEXT(curelm, field) = \
  152. SLIST_NEXT(SLIST_NEXT(curelm, field), field); \
  153. } \
  154. } while (0)
  155. #define SLIST_REMOVE_HEAD(head, field) do { \
  156. SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
  157. } while (0)
  158. /*
  159. * Singly-linked Tail queue declarations.
  160. */
  161. #define STAILQ_HEAD(name, type) \
  162. struct name { \
  163. struct type *stqh_first;/* first element */ \
  164. struct type **stqh_last;/* addr of last next element */ \
  165. }
  166. #define STAILQ_HEAD_INITIALIZER(head) \
  167. { NULL, &(head).stqh_first }
  168. #define STAILQ_ENTRY(type) \
  169. struct { \
  170. struct type *stqe_next; /* next element */ \
  171. }
  172. /*
  173. * Singly-linked Tail queue functions.
  174. */
  175. #define STAILQ_CONCAT(head1, head2) do { \
  176. if (!STAILQ_EMPTY((head2))) { \
  177. *(head1)->stqh_last = (head2)->stqh_first; \
  178. (head1)->stqh_last = (head2)->stqh_last; \
  179. STAILQ_INIT((head2)); \
  180. } \
  181. } while (0)
  182. #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
  183. #define STAILQ_FIRST(head) ((head)->stqh_first)
  184. #define STAILQ_FOREACH(var, head, field) \
  185. for((var) = STAILQ_FIRST((head)); \
  186. (var); \
  187. (var) = STAILQ_NEXT((var), field))
  188. #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
  189. for ((var) = STAILQ_FIRST((head)); \
  190. (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
  191. (var) = (tvar))
  192. #define STAILQ_INIT(head) do { \
  193. STAILQ_FIRST((head)) = NULL; \
  194. (head)->stqh_last = &STAILQ_FIRST((head)); \
  195. } while (0)
  196. #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
  197. if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
  198. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  199. STAILQ_NEXT((tqelm), field) = (elm); \
  200. } while (0)
  201. #define STAILQ_INSERT_HEAD(head, elm, field) do { \
  202. if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
  203. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  204. STAILQ_FIRST((head)) = (elm); \
  205. } while (0)
  206. #define STAILQ_INSERT_TAIL(head, elm, field) do { \
  207. STAILQ_NEXT((elm), field) = NULL; \
  208. *(head)->stqh_last = (elm); \
  209. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  210. } while (0)
  211. #define STAILQ_LAST(head, type, field) \
  212. (STAILQ_EMPTY((head)) ? \
  213. NULL : \
  214. ((struct type *) \
  215. ((char *)((head)->stqh_last) - offsetof(struct type, field))))
  216. #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
  217. #define STAILQ_REMOVE(head, elm, type, field) do { \
  218. if (STAILQ_FIRST((head)) == (elm)) { \
  219. STAILQ_REMOVE_HEAD((head), field); \
  220. } \
  221. else { \
  222. struct type *curelm = STAILQ_FIRST((head)); \
  223. while (STAILQ_NEXT(curelm, field) != (elm)) \
  224. curelm = STAILQ_NEXT(curelm, field); \
  225. if ((STAILQ_NEXT(curelm, field) = \
  226. STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
  227. (head)->stqh_last = &STAILQ_NEXT((curelm), field);\
  228. } \
  229. } while (0)
  230. #define STAILQ_REMOVE_HEAD(head, field) do { \
  231. if ((STAILQ_FIRST((head)) = \
  232. STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
  233. (head)->stqh_last = &STAILQ_FIRST((head)); \
  234. } while (0)
  235. #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
  236. if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
  237. (head)->stqh_last = &STAILQ_FIRST((head)); \
  238. } while (0)
  239. /*
  240. * List declarations.
  241. */
  242. #define LIST_HEAD(name, type) \
  243. struct name { \
  244. struct type *lh_first; /* first element */ \
  245. }
  246. #define LIST_HEAD_INITIALIZER(head) \
  247. { NULL }
  248. #define LIST_ENTRY(type) \
  249. struct { \
  250. struct type *le_next; /* next element */ \
  251. struct type **le_prev; /* address of previous next element */ \
  252. }
  253. /*
  254. * List functions.
  255. */
  256. #define LIST_EMPTY(head) ((head)->lh_first == NULL)
  257. #define LIST_FIRST(head) ((head)->lh_first)
  258. #define LIST_FOREACH(var, head, field) \
  259. for ((var) = LIST_FIRST((head)); \
  260. (var); \
  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. #define LIST_INIT(head) do { \
  267. LIST_FIRST((head)) = NULL; \
  268. } while (0)
  269. #define LIST_INSERT_AFTER(listelm, elm, field) do { \
  270. if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
  271. LIST_NEXT((listelm), field)->field.le_prev = \
  272. &LIST_NEXT((elm), field); \
  273. LIST_NEXT((listelm), field) = (elm); \
  274. (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
  275. } while (0)
  276. #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
  277. (elm)->field.le_prev = (listelm)->field.le_prev; \
  278. LIST_NEXT((elm), field) = (listelm); \
  279. *(listelm)->field.le_prev = (elm); \
  280. (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
  281. } while (0)
  282. #define LIST_INSERT_HEAD(head, elm, field) do { \
  283. if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
  284. LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
  285. LIST_FIRST((head)) = (elm); \
  286. (elm)->field.le_prev = &LIST_FIRST((head)); \
  287. } while (0)
  288. #define LIST_NEXT(elm, field) ((elm)->field.le_next)
  289. #define LIST_REMOVE(elm, field) do { \
  290. if (LIST_NEXT((elm), field) != NULL) \
  291. LIST_NEXT((elm), field)->field.le_prev = \
  292. (elm)->field.le_prev; \
  293. *(elm)->field.le_prev = LIST_NEXT((elm), field); \
  294. } while (0)
  295. /*
  296. * Tail queue declarations.
  297. */
  298. #define TAILQ_HEAD(name, type) \
  299. struct name { \
  300. struct type *tqh_first; /* first element */ \
  301. struct type **tqh_last; /* addr of last next element */ \
  302. }
  303. #define TAILQ_HEAD_INITIALIZER(head) \
  304. { NULL, &(head).tqh_first }
  305. #define TAILQ_ENTRY(type) \
  306. struct { \
  307. struct type *tqe_next; /* next element */ \
  308. struct type **tqe_prev; /* address of previous next element */ \
  309. }
  310. /*
  311. * Tail queue functions.
  312. */
  313. #define TAILQ_CONCAT(head1, head2, field) do { \
  314. if (!TAILQ_EMPTY(head2)) { \
  315. *(head1)->tqh_last = (head2)->tqh_first; \
  316. (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
  317. (head1)->tqh_last = (head2)->tqh_last; \
  318. TAILQ_INIT((head2)); \
  319. } \
  320. } while (0)
  321. #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
  322. #define TAILQ_FIRST(head) ((head)->tqh_first)
  323. #define TAILQ_FOREACH(var, head, field) \
  324. for ((var) = TAILQ_FIRST((head)); \
  325. (var); \
  326. (var) = TAILQ_NEXT((var), field))
  327. #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
  328. for ((var) = TAILQ_FIRST((head)); \
  329. (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
  330. (var) = (tvar))
  331. #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  332. for ((var) = TAILQ_LAST((head), headname); \
  333. (var); \
  334. (var) = TAILQ_PREV((var), headname, field))
  335. #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
  336. for ((var) = TAILQ_LAST((head), headname); \
  337. (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
  338. (var) = (tvar))
  339. #define TAILQ_INIT(head) do { \
  340. TAILQ_FIRST((head)) = NULL; \
  341. (head)->tqh_last = &TAILQ_FIRST((head)); \
  342. } while (0)
  343. #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  344. if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
  345. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  346. &TAILQ_NEXT((elm), field); \
  347. else { \
  348. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  349. } \
  350. TAILQ_NEXT((listelm), field) = (elm); \
  351. (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
  352. } while (0)
  353. #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  354. (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  355. TAILQ_NEXT((elm), field) = (listelm); \
  356. *(listelm)->field.tqe_prev = (elm); \
  357. (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
  358. } while (0)
  359. #define TAILQ_INSERT_HEAD(head, elm, field) do { \
  360. if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
  361. TAILQ_FIRST((head))->field.tqe_prev = \
  362. &TAILQ_NEXT((elm), field); \
  363. else \
  364. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  365. TAILQ_FIRST((head)) = (elm); \
  366. (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
  367. } while (0)
  368. #define TAILQ_INSERT_TAIL(head, elm, field) do { \
  369. TAILQ_NEXT((elm), field) = NULL; \
  370. (elm)->field.tqe_prev = (head)->tqh_last; \
  371. *(head)->tqh_last = (elm); \
  372. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  373. } while (0)
  374. #define TAILQ_LAST(head, headname) \
  375. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  376. #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  377. #define TAILQ_PREV(elm, headname, field) \
  378. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  379. #define TAILQ_REMOVE(head, elm, field) do { \
  380. if ((TAILQ_NEXT((elm), field)) != NULL) \
  381. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  382. (elm)->field.tqe_prev; \
  383. else { \
  384. (head)->tqh_last = (elm)->field.tqe_prev; \
  385. } \
  386. *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
  387. } while (0)
  388. #endif /* !SYS_QUEUE_H */