queue.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. *
  14. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * @(#)queue.h 8.5 (Berkeley) 8/20/94
  27. * $FreeBSD: src/sys/sys/queue.h,v 1.38 2000/05/26 02:06:56 jake Exp $
  28. */
  29. #ifndef _SYS_QUEUE_H_
  30. #define _SYS_QUEUE_H_
  31. /*
  32. * This file defines five types of data structures: singly-linked lists,
  33. * singly-linked tail queues, lists, tail queues, and circular queues.
  34. *
  35. * A singly-linked list is headed by a single forward pointer. The elements
  36. * are singly linked for minimum space and pointer manipulation overhead at
  37. * the expense of O(n) removal for arbitrary elements. New elements can be
  38. * added to the list after an existing element or at the head of the list.
  39. * Elements being removed from the head of the list should use the explicit
  40. * macro for this purpose for optimum efficiency. A singly-linked list may
  41. * only be traversed in the forward direction. Singly-linked lists are ideal
  42. * for applications with large datasets and few or no removals or for
  43. * implementing a LIFO queue.
  44. *
  45. * A singly-linked tail queue is headed by a pair of pointers, one to the
  46. * head of the list and the other to the tail of the list. The elements are
  47. * singly linked for minimum space and pointer manipulation overhead at the
  48. * expense of O(n) removal for arbitrary elements. New elements can be added
  49. * to the list after an existing element, at the head of the list, or at the
  50. * end of the list. Elements being removed from the head of the tail queue
  51. * should use the explicit macro for this purpose for optimum efficiency.
  52. * A singly-linked tail queue may only be traversed in the forward direction.
  53. * Singly-linked tail queues are ideal for applications with large datasets
  54. * and few or no removals or for implementing a FIFO queue.
  55. *
  56. * A list is headed by a single forward pointer (or an array of forward
  57. * pointers for a hash table header). The elements are doubly linked
  58. * so that an arbitrary element can be removed without a need to
  59. * traverse the list. New elements can be added to the list before
  60. * or after an existing element or at the head of the list. A list
  61. * may only be traversed in the forward direction.
  62. *
  63. * A tail queue is headed by a pair of pointers, one to the head of the
  64. * list and the other to the tail of the list. The elements are doubly
  65. * linked so that an arbitrary element can be removed without a need to
  66. * traverse the list. New elements can be added to the list before or
  67. * after an existing element, at the head of the list, or at the end of
  68. * the list. A tail queue may be traversed in either direction.
  69. *
  70. * A circle queue is headed by a pair of pointers, one to the head of the
  71. * list and the other to the tail of the list. The elements are doubly
  72. * linked so that an arbitrary element can be removed without a need to
  73. * traverse the list. New elements can be added to the list before or after
  74. * an existing element, at the head of the list, or at the end of the list.
  75. * A circle queue may be traversed in either direction, but has a more
  76. * complex end of list detection.
  77. *
  78. * For details on the use of these macros, see the queue(3) manual page.
  79. *
  80. *
  81. * SLIST LIST STAILQ TAILQ CIRCLEQ
  82. * _HEAD + + + + +
  83. * _HEAD_INITIALIZER + + + + +
  84. * _ENTRY + + + + +
  85. * _INIT + + + + +
  86. * _EMPTY + + + + +
  87. * _FIRST + + + + +
  88. * _NEXT + + + + +
  89. * _PREV - - - + +
  90. * _LAST - - + + +
  91. * _FOREACH + + + + +
  92. * _FOREACH_REVERSE - - - + +
  93. * _INSERT_HEAD + + + + +
  94. * _INSERT_BEFORE - + - + +
  95. * _INSERT_AFTER + + + + +
  96. * _INSERT_TAIL - - + + +
  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_INIT(head) do { \
  124. SLIST_FIRST((head)) = NULL; \
  125. } while (0)
  126. #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  127. SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
  128. SLIST_NEXT((slistelm), field) = (elm); \
  129. } while (0)
  130. #define SLIST_INSERT_HEAD(head, elm, field) do { \
  131. SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
  132. SLIST_FIRST((head)) = (elm); \
  133. } while (0)
  134. #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  135. #define SLIST_REMOVE(head, elm, type, field) do { \
  136. if (SLIST_FIRST((head)) == (elm)) { \
  137. SLIST_REMOVE_HEAD((head), field); \
  138. } \
  139. else { \
  140. struct type *curelm = SLIST_FIRST((head)); \
  141. while (SLIST_NEXT(curelm, field) != (elm)) \
  142. curelm = SLIST_NEXT(curelm, field); \
  143. SLIST_NEXT(curelm, field) = \
  144. SLIST_NEXT(SLIST_NEXT(curelm, field), field); \
  145. } \
  146. } while (0)
  147. #define SLIST_REMOVE_HEAD(head, field) do { \
  148. SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
  149. } while (0)
  150. /*
  151. * Singly-linked Tail queue declarations.
  152. */
  153. #define STAILQ_HEAD(name, type) \
  154. struct name { \
  155. struct type *stqh_first;/* first element */ \
  156. struct type **stqh_last;/* addr of last next element */ \
  157. }
  158. #define STAILQ_HEAD_INITIALIZER(head) \
  159. { NULL, &(head).stqh_first }
  160. #define STAILQ_ENTRY(type) \
  161. struct { \
  162. struct type *stqe_next; /* next element */ \
  163. }
  164. /*
  165. * Singly-linked Tail queue functions.
  166. */
  167. #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
  168. #define STAILQ_FIRST(head) ((head)->stqh_first)
  169. #define STAILQ_FOREACH(var, head, field) \
  170. for((var) = STAILQ_FIRST((head)); \
  171. (var); \
  172. (var) = STAILQ_NEXT((var), field))
  173. #define STAILQ_INIT(head) do { \
  174. STAILQ_FIRST((head)) = NULL; \
  175. (head)->stqh_last = &STAILQ_FIRST((head)); \
  176. } while (0)
  177. #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
  178. if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
  179. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  180. STAILQ_NEXT((tqelm), field) = (elm); \
  181. } while (0)
  182. #define STAILQ_INSERT_HEAD(head, elm, field) do { \
  183. if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
  184. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  185. STAILQ_FIRST((head)) = (elm); \
  186. } while (0)
  187. #define STAILQ_INSERT_TAIL(head, elm, field) do { \
  188. STAILQ_NEXT((elm), field) = NULL; \
  189. STAILQ_LAST((head)) = (elm); \
  190. (head)->stqh_last = &STAILQ_NEXT((elm), field); \
  191. } while (0)
  192. #define STAILQ_LAST(head) (*(head)->stqh_last)
  193. #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
  194. #define STAILQ_REMOVE(head, elm, type, field) do { \
  195. if (STAILQ_FIRST((head)) == (elm)) { \
  196. STAILQ_REMOVE_HEAD(head, field); \
  197. } \
  198. else { \
  199. struct type *curelm = STAILQ_FIRST((head)); \
  200. while (STAILQ_NEXT(curelm, field) != (elm)) \
  201. curelm = STAILQ_NEXT(curelm, field); \
  202. if ((STAILQ_NEXT(curelm, field) = \
  203. STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
  204. (head)->stqh_last = &STAILQ_NEXT((curelm), field);\
  205. } \
  206. } while (0)
  207. #define STAILQ_REMOVE_HEAD(head, field) do { \
  208. if ((STAILQ_FIRST((head)) = \
  209. STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
  210. (head)->stqh_last = &STAILQ_FIRST((head)); \
  211. } while (0)
  212. #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
  213. if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
  214. (head)->stqh_last = &STAILQ_FIRST((head)); \
  215. } while (0)
  216. /*
  217. * List declarations.
  218. */
  219. #define BSD_LIST_HEAD(name, type) \
  220. struct name { \
  221. struct type *lh_first; /* first element */ \
  222. }
  223. #define LIST_HEAD_INITIALIZER(head) \
  224. { NULL }
  225. #define LIST_ENTRY(type) \
  226. struct { \
  227. struct type *le_next; /* next element */ \
  228. struct type **le_prev; /* address of previous next element */ \
  229. }
  230. /*
  231. * List functions.
  232. */
  233. #define LIST_EMPTY(head) ((head)->lh_first == NULL)
  234. #define LIST_FIRST(head) ((head)->lh_first)
  235. #define LIST_FOREACH(var, head, field) \
  236. for ((var) = LIST_FIRST((head)); \
  237. (var); \
  238. (var) = LIST_NEXT((var), field))
  239. #define LIST_INIT(head) do { \
  240. LIST_FIRST((head)) = NULL; \
  241. } while (0)
  242. #define LIST_INSERT_AFTER(listelm, elm, field) do { \
  243. if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
  244. LIST_NEXT((listelm), field)->field.le_prev = \
  245. &LIST_NEXT((elm), field); \
  246. LIST_NEXT((listelm), field) = (elm); \
  247. (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
  248. } while (0)
  249. #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
  250. (elm)->field.le_prev = (listelm)->field.le_prev; \
  251. LIST_NEXT((elm), field) = (listelm); \
  252. *(listelm)->field.le_prev = (elm); \
  253. (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
  254. } while (0)
  255. #define LIST_INSERT_HEAD(head, elm, field) do { \
  256. if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
  257. LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
  258. LIST_FIRST((head)) = (elm); \
  259. (elm)->field.le_prev = &LIST_FIRST((head)); \
  260. } while (0)
  261. #define LIST_NEXT(elm, field) ((elm)->field.le_next)
  262. #define LIST_REMOVE(elm, field) do { \
  263. if (LIST_NEXT((elm), field) != NULL) \
  264. LIST_NEXT((elm), field)->field.le_prev = \
  265. (elm)->field.le_prev; \
  266. *(elm)->field.le_prev = LIST_NEXT((elm), field); \
  267. } while (0)
  268. /*
  269. * Tail queue declarations.
  270. */
  271. #define TAILQ_HEAD(name, type) \
  272. struct name { \
  273. struct type *tqh_first; /* first element */ \
  274. struct type **tqh_last; /* addr of last next element */ \
  275. }
  276. #define TAILQ_HEAD_INITIALIZER(head) \
  277. { NULL, &(head).tqh_first }
  278. #define TAILQ_ENTRY(type) \
  279. struct { \
  280. struct type *tqe_next; /* next element */ \
  281. struct type **tqe_prev; /* address of previous next element */ \
  282. }
  283. /*
  284. * Tail queue functions.
  285. */
  286. #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
  287. #define TAILQ_FIRST(head) ((head)->tqh_first)
  288. #define TAILQ_FOREACH(var, head, field) \
  289. for ((var) = TAILQ_FIRST((head)); \
  290. (var); \
  291. (var) = TAILQ_NEXT((var), field))
  292. #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  293. for ((var) = TAILQ_LAST((head), headname); \
  294. (var); \
  295. (var) = TAILQ_PREV((var), headname, field))
  296. #define TAILQ_INIT(head) do { \
  297. TAILQ_FIRST((head)) = NULL; \
  298. (head)->tqh_last = &TAILQ_FIRST((head)); \
  299. } while (0)
  300. #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  301. if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
  302. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  303. &TAILQ_NEXT((elm), field); \
  304. else \
  305. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  306. TAILQ_NEXT((listelm), field) = (elm); \
  307. (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
  308. } while (0)
  309. #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  310. (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  311. TAILQ_NEXT((elm), field) = (listelm); \
  312. *(listelm)->field.tqe_prev = (elm); \
  313. (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
  314. } while (0)
  315. #define TAILQ_INSERT_HEAD(head, elm, field) do { \
  316. if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
  317. TAILQ_FIRST((head))->field.tqe_prev = \
  318. &TAILQ_NEXT((elm), field); \
  319. else \
  320. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  321. TAILQ_FIRST((head)) = (elm); \
  322. (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
  323. } while (0)
  324. #define TAILQ_INSERT_TAIL(head, elm, field) do { \
  325. TAILQ_NEXT((elm), field) = NULL; \
  326. (elm)->field.tqe_prev = (head)->tqh_last; \
  327. *(head)->tqh_last = (elm); \
  328. (head)->tqh_last = &TAILQ_NEXT((elm), field); \
  329. } while (0)
  330. #define TAILQ_LAST(head, headname) \
  331. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  332. #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  333. #define TAILQ_PREV(elm, headname, field) \
  334. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  335. #define TAILQ_REMOVE(head, elm, field) do { \
  336. if ((TAILQ_NEXT((elm), field)) != NULL) \
  337. TAILQ_NEXT((elm), field)->field.tqe_prev = \
  338. (elm)->field.tqe_prev; \
  339. else \
  340. (head)->tqh_last = (elm)->field.tqe_prev; \
  341. *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
  342. } while (0)
  343. /*
  344. * Circular queue declarations.
  345. */
  346. #define CIRCLEQ_HEAD(name, type) \
  347. struct name { \
  348. struct type *cqh_first; /* first element */ \
  349. struct type *cqh_last; /* last element */ \
  350. }
  351. #define CIRCLEQ_HEAD_INITIALIZER(head) \
  352. { (void *)&(head), (void *)&(head) }
  353. #define CIRCLEQ_ENTRY(type) \
  354. struct { \
  355. struct type *cqe_next; /* next element */ \
  356. struct type *cqe_prev; /* previous element */ \
  357. }
  358. /*
  359. * Circular queue functions.
  360. */
  361. #define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
  362. #define CIRCLEQ_FIRST(head) ((head)->cqh_first)
  363. #define CIRCLEQ_FOREACH(var, head, field) \
  364. for ((var) = CIRCLEQ_FIRST((head)); \
  365. (var) != (void *)(head); \
  366. (var) = CIRCLEQ_NEXT((var), field))
  367. #define CIRCLEQ_FOREACH_REVERSE(var, head, field) \
  368. for ((var) = CIRCLEQ_LAST((head)); \
  369. (var) != (void *)(head); \
  370. (var) = CIRCLEQ_PREV((var), field))
  371. #define CIRCLEQ_INIT(head) do { \
  372. CIRCLEQ_FIRST((head)) = (void *)(head); \
  373. CIRCLEQ_LAST((head)) = (void *)(head); \
  374. } while (0)
  375. #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  376. CIRCLEQ_NEXT((elm), field) = CIRCLEQ_NEXT((listelm), field); \
  377. CIRCLEQ_PREV((elm), field) = (listelm); \
  378. if (CIRCLEQ_NEXT((listelm), field) == (void *)(head)) \
  379. CIRCLEQ_LAST((head)) = (elm); \
  380. else \
  381. CIRCLEQ_PREV(CIRCLEQ_NEXT((listelm), field), field) = (elm);\
  382. CIRCLEQ_NEXT((listelm), field) = (elm); \
  383. } while (0)
  384. #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  385. CIRCLEQ_NEXT((elm), field) = (listelm); \
  386. CIRCLEQ_PREV((elm), field) = CIRCLEQ_PREV((listelm), field); \
  387. if (CIRCLEQ_PREV((listelm), field) == (void *)(head)) \
  388. CIRCLEQ_FIRST((head)) = (elm); \
  389. else \
  390. CIRCLEQ_NEXT(CIRCLEQ_PREV((listelm), field), field) = (elm);\
  391. CIRCLEQ_PREV((listelm), field) = (elm); \
  392. } while (0)
  393. #define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  394. CIRCLEQ_NEXT((elm), field) = CIRCLEQ_FIRST((head)); \
  395. CIRCLEQ_PREV((elm), field) = (void *)(head); \
  396. if (CIRCLEQ_LAST((head)) == (void *)(head)) \
  397. CIRCLEQ_LAST((head)) = (elm); \
  398. else \
  399. CIRCLEQ_PREV(CIRCLEQ_FIRST((head)), field) = (elm); \
  400. CIRCLEQ_FIRST((head)) = (elm); \
  401. } while (0)
  402. #define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  403. CIRCLEQ_NEXT((elm), field) = (void *)(head); \
  404. CIRCLEQ_PREV((elm), field) = CIRCLEQ_LAST((head)); \
  405. if (CIRCLEQ_FIRST((head)) == (void *)(head)) \
  406. CIRCLEQ_FIRST((head)) = (elm); \
  407. else \
  408. CIRCLEQ_NEXT(CIRCLEQ_LAST((head)), field) = (elm); \
  409. CIRCLEQ_LAST((head)) = (elm); \
  410. } while (0)
  411. #define CIRCLEQ_LAST(head) ((head)->cqh_last)
  412. #define CIRCLEQ_NEXT(elm,field) ((elm)->field.cqe_next)
  413. #define CIRCLEQ_PREV(elm,field) ((elm)->field.cqe_prev)
  414. #define CIRCLEQ_REMOVE(head, elm, field) do { \
  415. if (CIRCLEQ_NEXT((elm), field) == (void *)(head)) \
  416. CIRCLEQ_LAST((head)) = CIRCLEQ_PREV((elm), field); \
  417. else \
  418. CIRCLEQ_PREV(CIRCLEQ_NEXT((elm), field), field) = \
  419. CIRCLEQ_PREV((elm), field); \
  420. if (CIRCLEQ_PREV((elm), field) == (void *)(head)) \
  421. CIRCLEQ_FIRST((head)) = CIRCLEQ_NEXT((elm), field); \
  422. else \
  423. CIRCLEQ_NEXT(CIRCLEQ_PREV((elm), field), field) = \
  424. CIRCLEQ_NEXT((elm), field); \
  425. } while (0)
  426. #endif /* !_SYS_QUEUE_H_ */