tor_queue.txt 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. Below follows the manpage for tor_queue.h, as included with OpenBSD's
  2. sys/queue.h. License follows at the end of the file.
  3. ======================================================================
  4. QUEUE(3) OpenBSD Programmer's Manual QUEUE(3)
  5. NAME
  6. SLIST_ENTRY, SLIST_HEAD, SLIST_HEAD_INITIALIZER, SLIST_FIRST, SLIST_NEXT,
  7. SLIST_END, SLIST_EMPTY, SLIST_FOREACH, SLIST_FOREACH_SAFE, SLIST_INIT,
  8. SLIST_INSERT_AFTER, SLIST_INSERT_HEAD, SLIST_REMOVE_AFTER,
  9. SLIST_REMOVE_HEAD, SLIST_REMOVE, LIST_ENTRY, LIST_HEAD,
  10. LIST_HEAD_INITIALIZER, LIST_FIRST, LIST_NEXT, LIST_END, LIST_EMPTY,
  11. LIST_FOREACH, LIST_FOREACH_SAFE, LIST_INIT, LIST_INSERT_AFTER,
  12. LIST_INSERT_BEFORE, LIST_INSERT_HEAD, LIST_REMOVE, LIST_REPLACE,
  13. SIMPLEQ_ENTRY, SIMPLEQ_HEAD, SIMPLEQ_HEAD_INITIALIZER, SIMPLEQ_FIRST,
  14. SIMPLEQ_NEXT, SIMPLEQ_END, SIMPLEQ_EMPTY, SIMPLEQ_FOREACH,
  15. SIMPLEQ_FOREACH_SAFE, SIMPLEQ_INIT, SIMPLEQ_INSERT_AFTER,
  16. SIMPLEQ_INSERT_HEAD, SIMPLEQ_INSERT_TAIL, SIMPLEQ_REMOVE_AFTER,
  17. SIMPLEQ_REMOVE_HEAD, TAILQ_ENTRY, TAILQ_HEAD, TAILQ_HEAD_INITIALIZER,
  18. TAILQ_FIRST, TAILQ_NEXT, TAILQ_END, TAILQ_LAST, TAILQ_PREV, TAILQ_EMPTY,
  19. TAILQ_FOREACH, TAILQ_FOREACH_SAFE, TAILQ_FOREACH_REVERSE,
  20. TAILQ_FOREACH_REVERSE_SAFE, TAILQ_INIT, TAILQ_INSERT_AFTER,
  21. TAILQ_INSERT_BEFORE, TAILQ_INSERT_HEAD, TAILQ_INSERT_TAIL, TAILQ_REMOVE,
  22. TAILQ_REPLACE, CIRCLEQ_ENTRY, CIRCLEQ_HEAD, CIRCLEQ_HEAD_INITIALIZER,
  23. CIRCLEQ_FIRST, CIRCLEQ_LAST, CIRCLEQ_END, CIRCLEQ_NEXT, CIRCLEQ_PREV,
  24. CIRCLEQ_EMPTY, CIRCLEQ_FOREACH, CIRCLEQ_FOREACH_SAFE,
  25. CIRCLEQ_FOREACH_REVERSE_SAFE, CIRCLEQ_INIT, CIRCLEQ_INSERT_AFTER,
  26. CIRCLEQ_INSERT_BEFORE, CIRCLEQ_INSERT_HEAD, CIRCLEQ_INSERT_TAIL,
  27. CIRCLEQ_REMOVE, CIRCLEQ_REPLACE - implementations of singly-linked lists,
  28. doubly-linked lists, simple queues, tail queues, and circular queues
  29. SYNOPSIS
  30. #include <sys/queue.h>
  31. SLIST_ENTRY(TYPE);
  32. SLIST_HEAD(HEADNAME, TYPE);
  33. SLIST_HEAD_INITIALIZER(SLIST_HEAD head);
  34. struct TYPE *
  35. SLIST_FIRST(SLIST_HEAD *head);
  36. struct TYPE *
  37. SLIST_NEXT(struct TYPE *listelm, SLIST_ENTRY NAME);
  38. struct TYPE *
  39. SLIST_END(SLIST_HEAD *head);
  40. int
  41. SLIST_EMPTY(SLIST_HEAD *head);
  42. SLIST_FOREACH(VARNAME, SLIST_HEAD *head, SLIST_ENTRY NAME);
  43. SLIST_FOREACH_SAFE(VARNAME, SLIST_HEAD *head, SLIST_ENTRY
  44. NAME, TEMP_VARNAME);
  45. void
  46. SLIST_INIT(SLIST_HEAD *head);
  47. void
  48. SLIST_INSERT_AFTER(struct TYPE *listelm, struct TYPE *elm, SLIST_ENTRY
  49. NAME);
  50. void
  51. SLIST_INSERT_HEAD(SLIST_HEAD *head, struct TYPE *elm, SLIST_ENTRY NAME);
  52. void
  53. SLIST_REMOVE_AFTER(struct TYPE *elm, SLIST_ENTRY NAME);
  54. void
  55. SLIST_REMOVE_HEAD(SLIST_HEAD *head, SLIST_ENTRY NAME);
  56. void
  57. SLIST_REMOVE(SLIST_HEAD *head, struct TYPE *elm, TYPE, SLIST_ENTRY NAME);
  58. LIST_ENTRY(TYPE);
  59. LIST_HEAD(HEADNAME, TYPE);
  60. LIST_HEAD_INITIALIZER(LIST_HEAD head);
  61. struct TYPE *
  62. LIST_FIRST(LIST_HEAD *head);
  63. struct TYPE *
  64. LIST_NEXT(struct TYPE *listelm, LIST_ENTRY NAME);
  65. struct TYPE *
  66. LIST_END(LIST_HEAD *head);
  67. int
  68. LIST_EMPTY(LIST_HEAD *head);
  69. LIST_FOREACH(VARNAME, LIST_HEAD *head, LIST_ENTRY NAME);
  70. LIST_FOREACH_SAFE(VARNAME, LIST_HEAD *head, LIST_ENTRY
  71. NAME, TEMP_VARNAME);
  72. void
  73. LIST_INIT(LIST_HEAD *head);
  74. void
  75. LIST_INSERT_AFTER(struct TYPE *listelm, struct TYPE *elm, LIST_ENTRY
  76. NAME);
  77. void
  78. LIST_INSERT_BEFORE(struct TYPE *listelm, struct TYPE *elm, LIST_ENTRY
  79. NAME);
  80. void
  81. LIST_INSERT_HEAD(LIST_HEAD *head, struct TYPE *elm, LIST_ENTRY NAME);
  82. void
  83. LIST_REMOVE(struct TYPE *elm, LIST_ENTRY NAME);
  84. void
  85. LIST_REPLACE(struct TYPE *elm, struct TYPE *elm2, LIST_ENTRY NAME);
  86. SIMPLEQ_ENTRY(TYPE);
  87. SIMPLEQ_HEAD(HEADNAME, TYPE);
  88. SIMPLEQ_HEAD_INITIALIZER(SIMPLEQ_HEAD head);
  89. struct TYPE *
  90. SIMPLEQ_FIRST(SIMPLEQ_HEAD *head);
  91. struct TYPE *
  92. SIMPLEQ_NEXT(struct TYPE *listelm, SIMPLEQ_ENTRY NAME);
  93. struct TYPE *
  94. SIMPLEQ_END(SIMPLEQ_HEAD *head);
  95. int
  96. SIMPLEQ_EMPTY(SIMPLEQ_HEAD *head);
  97. SIMPLEQ_FOREACH(VARNAME, SIMPLEQ_HEAD *head, SIMPLEQ_ENTRY NAME);
  98. SIMPLEQ_FOREACH_SAFE(VARNAME, SIMPLEQ_HEAD *head, SIMPLEQ_ENTRY
  99. NAME, TEMP_VARNAME);
  100. void
  101. SIMPLEQ_INIT(SIMPLEQ_HEAD *head);
  102. void
  103. SIMPLEQ_INSERT_AFTER(SIMPLEQ_HEAD *head, struct TYPE *listelm, struct
  104. TYPE *elm, SIMPLEQ_ENTRY NAME);
  105. void
  106. SIMPLEQ_INSERT_HEAD(SIMPLEQ_HEAD *head, struct TYPE *elm, SIMPLEQ_ENTRY
  107. NAME);
  108. void
  109. SIMPLEQ_INSERT_TAIL(SIMPLEQ_HEAD *head, struct TYPE *elm, SIMPLEQ_ENTRY
  110. NAME);
  111. void
  112. SIMPLEQ_REMOVE_AFTER(SIMPLEQ_HEAD *head, struct TYPE *elm, SIMPLEQ_ENTRY
  113. NAME);
  114. void
  115. SIMPLEQ_REMOVE_HEAD(SIMPLEQ_HEAD *head, SIMPLEQ_ENTRY NAME);
  116. TAILQ_ENTRY(TYPE);
  117. TAILQ_HEAD(HEADNAME, TYPE);
  118. TAILQ_HEAD_INITIALIZER(TAILQ_HEAD head);
  119. struct TYPE *
  120. TAILQ_FIRST(TAILQ_HEAD *head);
  121. struct TYPE *
  122. TAILQ_NEXT(struct TYPE *listelm, TAILQ_ENTRY NAME);
  123. struct TYPE *
  124. TAILQ_END(TAILQ_HEAD *head);
  125. struct TYPE *
  126. TAILQ_LAST(TAILQ_HEAD *head, HEADNAME NAME);
  127. struct TYPE *
  128. TAILQ_PREV(struct TYPE *listelm, HEADNAME NAME, TAILQ_ENTRY NAME);
  129. int
  130. TAILQ_EMPTY(TAILQ_HEAD *head);
  131. TAILQ_FOREACH(VARNAME, TAILQ_HEAD *head, TAILQ_ENTRY NAME);
  132. TAILQ_FOREACH_SAFE(VARNAME, TAILQ_HEAD *head, TAILQ_ENTRY
  133. NAME, TEMP_VARNAME);
  134. TAILQ_FOREACH_REVERSE(VARNAME, TAILQ_HEAD *head, HEADNAME, TAILQ_ENTRY
  135. NAME);
  136. TAILQ_FOREACH_REVERSE_SAFE(VARNAME, TAILQ_HEAD
  137. *head, HEADNAME, TAILQ_ENTRY NAME, TEMP_VARNAME);
  138. void
  139. TAILQ_INIT(TAILQ_HEAD *head);
  140. void
  141. TAILQ_INSERT_AFTER(TAILQ_HEAD *head, struct TYPE *listelm, struct TYPE
  142. *elm, TAILQ_ENTRY NAME);
  143. void
  144. TAILQ_INSERT_BEFORE(struct TYPE *listelm, struct TYPE *elm, TAILQ_ENTRY
  145. NAME);
  146. void
  147. TAILQ_INSERT_HEAD(TAILQ_HEAD *head, struct TYPE *elm, TAILQ_ENTRY NAME);
  148. void
  149. TAILQ_INSERT_TAIL(TAILQ_HEAD *head, struct TYPE *elm, TAILQ_ENTRY NAME);
  150. void
  151. TAILQ_REMOVE(TAILQ_HEAD *head, struct TYPE *elm, TAILQ_ENTRY NAME);
  152. void
  153. TAILQ_REPLACE(TAILQ_HEAD *head, struct TYPE *elm, struct TYPE
  154. *elm2, TAILQ_ENTRY NAME);
  155. CIRCLEQ_ENTRY(TYPE);
  156. CIRCLEQ_HEAD(HEADNAME, TYPE);
  157. CIRCLEQ_HEAD_INITIALIZER(CIRCLEQ_HEAD head);
  158. struct TYPE *
  159. CIRCLEQ_FIRST(CIRCLEQ_HEAD *head);
  160. struct TYPE *
  161. CIRCLEQ_LAST(CIRCLEQ_HEAD *head);
  162. struct TYPE *
  163. CIRCLEQ_END(CIRCLEQ_HEAD *head);
  164. struct TYPE *
  165. CIRCLEQ_NEXT(struct TYPE *listelm, CIRCLEQ_ENTRY NAME);
  166. struct TYPE *
  167. CIRCLEQ_PREV(struct TYPE *listelm, CIRCLEQ_ENTRY NAME);
  168. int
  169. CIRCLEQ_EMPTY(CIRCLEQ_HEAD *head);
  170. CIRCLEQ_FOREACH(VARNAME, CIRCLEQ_HEAD *head, CIRCLEQ_ENTRY NAME);
  171. CIRCLEQ_FOREACH_SAFE(VARNAME, CIRCLEQ_HEAD *head, CIRCLEQ_ENTRY
  172. NAME, TEMP_VARNAME);
  173. CIRCLEQ_FOREACH_REVERSE(VARNAME, CIRCLEQ_HEAD *head, CIRCLEQ_ENTRY NAME);
  174. CIRCLEQ_FOREACH_REVERSE_SAFE(VARNAME, CIRCLEQ_HEAD *head, CIRCLEQ_ENTRY
  175. NAME, TEMP_VARNAME);
  176. void
  177. CIRCLEQ_INIT(CIRCLEQ_HEAD *head);
  178. void
  179. CIRCLEQ_INSERT_AFTER(CIRCLEQ_HEAD *head, struct TYPE *listelm, struct
  180. TYPE *elm, CIRCLEQ_ENTRY NAME);
  181. void
  182. CIRCLEQ_INSERT_BEFORE(CIRCLEQ_HEAD *head, struct TYPE *listelm, struct
  183. TYPE *elm, CIRCLEQ_ENTRY NAME);
  184. void
  185. CIRCLEQ_INSERT_HEAD(CIRCLEQ_HEAD *head, struct TYPE *elm, CIRCLEQ_ENTRY
  186. NAME);
  187. void
  188. CIRCLEQ_INSERT_TAIL(CIRCLEQ_HEAD *head, struct TYPE *elm, CIRCLEQ_ENTRY
  189. NAME);
  190. void
  191. CIRCLEQ_REMOVE(CIRCLEQ_HEAD *head, struct TYPE *elm, CIRCLEQ_ENTRY NAME);
  192. void
  193. CIRCLEQ_REPLACE(CIRCLEQ_HEAD *head, struct TYPE *elm, struct TYPE
  194. *elm2, CIRCLEQ_ENTRY NAME);
  195. DESCRIPTION
  196. These macros define and operate on five types of data structures: singly-
  197. linked lists, simple queues, lists, tail queues, and circular queues.
  198. All five structures support the following functionality:
  199. 1. Insertion of a new entry at the head of the list.
  200. 2. Insertion of a new entry after any element in the list.
  201. 3. Removal of an entry from the head of the list.
  202. 4. Forward traversal through the list.
  203. Singly-linked lists are the simplest of the five data structures and
  204. support only the above functionality. Singly-linked lists are ideal for
  205. applications with large datasets and few or no removals, or for
  206. implementing a LIFO queue.
  207. Simple queues add the following functionality:
  208. 1. Entries can be added at the end of a list.
  209. However:
  210. 1. All list insertions must specify the head of the list.
  211. 2. Each head entry requires two pointers rather than one.
  212. 3. Code size is about 15% greater and operations run about 20%
  213. slower than singly-linked lists.
  214. Simple queues are ideal for applications with large datasets and few or
  215. no removals, or for implementing a FIFO queue.
  216. All doubly linked types of data structures (lists, tail queues, and
  217. circle queues) additionally allow:
  218. 1. Insertion of a new entry before any element in the list.
  219. 2. Removal of any entry in the list.
  220. However:
  221. 1. Each element requires two pointers rather than one.
  222. 2. Code size and execution time of operations (except for
  223. removal) is about twice that of the singly-linked data-
  224. structures.
  225. Lists are the simplest of the doubly linked data structures and support
  226. only the above functionality over singly-linked lists.
  227. Tail queues add the following functionality:
  228. 1. Entries can be added at the end of a list.
  229. 2. They may be traversed backwards, at a cost.
  230. However:
  231. 1. All list insertions and removals must specify the head of the
  232. list.
  233. 2. Each head entry requires two pointers rather than one.
  234. 3. Code size is about 15% greater and operations run about 20%
  235. slower than singly-linked lists.
  236. Circular queues add the following functionality:
  237. 1. Entries can be added at the end of a list.
  238. 2. They may be traversed backwards, from tail to head.
  239. However:
  240. 1. All list insertions and removals must specify the head of the
  241. list.
  242. 2. Each head entry requires two pointers rather than one.
  243. 3. The termination condition for traversal is more complex.
  244. 4. Code size is about 40% greater and operations run about 45%
  245. slower than lists.
  246. In the macro definitions, TYPE is the name tag of a user defined
  247. structure that must contain a field of type SLIST_ENTRY, LIST_ENTRY,
  248. SIMPLEQ_ENTRY, TAILQ_ENTRY, or CIRCLEQ_ENTRY, named NAME. The argument
  249. HEADNAME is the name tag of a user defined structure that must be
  250. declared using the macros SLIST_HEAD(), LIST_HEAD(), SIMPLEQ_HEAD(),
  251. TAILQ_HEAD(), or CIRCLEQ_HEAD(). See the examples below for further
  252. explanation of how these macros are used.
  253. SINGLY-LINKED LISTS
  254. A singly-linked list is headed by a structure defined by the SLIST_HEAD()
  255. macro. This structure contains a single pointer to the first element on
  256. the list. The elements are singly linked for minimum space and pointer
  257. manipulation overhead at the expense of O(n) removal for arbitrary
  258. elements. New elements can be added to the list after an existing
  259. element or at the head of the list. A SLIST_HEAD structure is declared
  260. as follows:
  261. SLIST_HEAD(HEADNAME, TYPE) head;
  262. where HEADNAME is the name of the structure to be defined, and struct
  263. TYPE is the type of the elements to be linked into the list. A pointer
  264. to the head of the list can later be declared as:
  265. struct HEADNAME *headp;
  266. (The names head and headp are user selectable.)
  267. The HEADNAME facility is often not used, leading to the following bizarre
  268. code:
  269. SLIST_HEAD(, TYPE) head, *headp;
  270. The SLIST_ENTRY() macro declares a structure that connects the elements
  271. in the list.
  272. The SLIST_INIT() macro initializes the list referenced by head.
  273. The list can also be initialized statically by using the
  274. SLIST_HEAD_INITIALIZER() macro like this:
  275. SLIST_HEAD(HEADNAME, TYPE) head = SLIST_HEAD_INITIALIZER(head);
  276. The SLIST_INSERT_HEAD() macro inserts the new element elm at the head of
  277. the list.
  278. The SLIST_INSERT_AFTER() macro inserts the new element elm after the
  279. element listelm.
  280. The SLIST_REMOVE_HEAD() macro removes the first element of the list
  281. pointed by head.
  282. The SLIST_REMOVE_AFTER() macro removes the list element immediately
  283. following elm.
  284. The SLIST_REMOVE() macro removes the element elm of the list pointed by
  285. head.
  286. The SLIST_FIRST() and SLIST_NEXT() macros can be used to traverse the
  287. list:
  288. for (np = SLIST_FIRST(&head); np != NULL; np = SLIST_NEXT(np, NAME))
  289. Or, for simplicity, one can use the SLIST_FOREACH() macro:
  290. SLIST_FOREACH(np, head, NAME)
  291. The macro SLIST_FOREACH_SAFE() traverses the list referenced by head in a
  292. forward direction, assigning each element in turn to var. However,
  293. unlike SLIST_FOREACH() it is permitted to remove var as well as free it
  294. from within the loop safely without interfering with the traversal.
  295. The SLIST_EMPTY() macro should be used to check whether a simple list is
  296. empty.
  297. SINGLY-LINKED LIST EXAMPLE
  298. SLIST_HEAD(listhead, entry) head;
  299. struct entry {
  300. ...
  301. SLIST_ENTRY(entry) entries; /* Simple list. */
  302. ...
  303. } *n1, *n2, *np;
  304. SLIST_INIT(&head); /* Initialize simple list. */
  305. n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
  306. SLIST_INSERT_HEAD(&head, n1, entries);
  307. n2 = malloc(sizeof(struct entry)); /* Insert after. */
  308. SLIST_INSERT_AFTER(n1, n2, entries);
  309. SLIST_FOREACH(np, &head, entries) /* Forward traversal. */
  310. np-> ...
  311. while (!SLIST_EMPTY(&head)) { /* Delete. */
  312. n1 = SLIST_FIRST(&head);
  313. SLIST_REMOVE_HEAD(&head, entries);
  314. free(n1);
  315. }
  316. LISTS
  317. A list is headed by a structure defined by the LIST_HEAD() macro. This
  318. structure contains a single pointer to the first element on the list.
  319. The elements are doubly linked so that an arbitrary element can be
  320. removed without traversing the list. New elements can be added to the
  321. list after an existing element, before an existing element, or at the
  322. head of the list. A LIST_HEAD structure is declared as follows:
  323. LIST_HEAD(HEADNAME, TYPE) head;
  324. where HEADNAME is the name of the structure to be defined, and struct
  325. TYPE is the type of the elements to be linked into the list. A pointer
  326. to the head of the list can later be declared as:
  327. struct HEADNAME *headp;
  328. (The names head and headp are user selectable.)
  329. The HEADNAME facility is often not used, leading to the following bizarre
  330. code:
  331. LIST_HEAD(, TYPE) head, *headp;
  332. The LIST_ENTRY() macro declares a structure that connects the elements in
  333. the list.
  334. The LIST_INIT() macro initializes the list referenced by head.
  335. The list can also be initialized statically by using the
  336. LIST_HEAD_INITIALIZER() macro like this:
  337. LIST_HEAD(HEADNAME, TYPE) head = LIST_HEAD_INITIALIZER(head);
  338. The LIST_INSERT_HEAD() macro inserts the new element elm at the head of
  339. the list.
  340. The LIST_INSERT_AFTER() macro inserts the new element elm after the
  341. element listelm.
  342. The LIST_INSERT_BEFORE() macro inserts the new element elm before the
  343. element listelm.
  344. The LIST_REMOVE() macro removes the element elm from the list.
  345. The LIST_REPLACE() macro replaces the list element elm with the new
  346. element elm2.
  347. The LIST_FIRST() and LIST_NEXT() macros can be used to traverse the list:
  348. for (np = LIST_FIRST(&head); np != NULL; np = LIST_NEXT(np, NAME))
  349. Or, for simplicity, one can use the LIST_FOREACH() macro:
  350. LIST_FOREACH(np, head, NAME)
  351. The macro LIST_FOREACH_SAFE() traverses the list referenced by head in a
  352. forward direction, assigning each element in turn to var. However,
  353. unlike LIST_FOREACH() it is permitted to remove var as well as free it
  354. from within the loop safely without interfering with the traversal.
  355. The LIST_EMPTY() macro should be used to check whether a list is empty.
  356. LIST EXAMPLE
  357. LIST_HEAD(listhead, entry) head;
  358. struct entry {
  359. ...
  360. LIST_ENTRY(entry) entries; /* List. */
  361. ...
  362. } *n1, *n2, *np;
  363. LIST_INIT(&head); /* Initialize list. */
  364. n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
  365. LIST_INSERT_HEAD(&head, n1, entries);
  366. n2 = malloc(sizeof(struct entry)); /* Insert after. */
  367. LIST_INSERT_AFTER(n1, n2, entries);
  368. n2 = malloc(sizeof(struct entry)); /* Insert before. */
  369. LIST_INSERT_BEFORE(n1, n2, entries);
  370. /* Forward traversal. */
  371. LIST_FOREACH(np, &head, entries)
  372. np-> ...
  373. while (!LIST_EMPTY(&head)) /* Delete. */
  374. n1 = LIST_FIRST(&head);
  375. LIST_REMOVE(n1, entries);
  376. free(n1);
  377. }
  378. SIMPLE QUEUES
  379. A simple queue is headed by a structure defined by the SIMPLEQ_HEAD()
  380. macro. This structure contains a pair of pointers, one to the first
  381. element in the simple queue and the other to the last element in the
  382. simple queue. The elements are singly linked. New elements can be added
  383. to the queue after an existing element, at the head of the queue or at
  384. the tail of the queue. A SIMPLEQ_HEAD structure is declared as follows:
  385. SIMPLEQ_HEAD(HEADNAME, TYPE) head;
  386. where HEADNAME is the name of the structure to be defined, and struct
  387. TYPE is the type of the elements to be linked into the queue. A pointer
  388. to the head of the queue can later be declared as:
  389. struct HEADNAME *headp;
  390. (The names head and headp are user selectable.)
  391. The SIMPLEQ_ENTRY() macro declares a structure that connects the elements
  392. in the queue.
  393. The SIMPLEQ_INIT() macro initializes the queue referenced by head.
  394. The queue can also be initialized statically by using the
  395. SIMPLEQ_HEAD_INITIALIZER() macro like this:
  396. SIMPLEQ_HEAD(HEADNAME, TYPE) head = SIMPLEQ_HEAD_INITIALIZER(head);
  397. The SIMPLEQ_INSERT_AFTER() macro inserts the new element elm after the
  398. element listelm.
  399. The SIMPLEQ_INSERT_HEAD() macro inserts the new element elm at the head
  400. of the queue.
  401. The SIMPLEQ_INSERT_TAIL() macro inserts the new element elm at the end of
  402. the queue.
  403. The SIMPLEQ_REMOVE_AFTER() macro removes the queue element immediately
  404. following elm.
  405. The SIMPLEQ_REMOVE_HEAD() macro removes the first element from the queue.
  406. The SIMPLEQ_FIRST() and SIMPLEQ_NEXT() macros can be used to traverse the
  407. queue. The SIMPLEQ_FOREACH() is used for queue traversal:
  408. SIMPLEQ_FOREACH(np, head, NAME)
  409. The macro SIMPLEQ_FOREACH_SAFE() traverses the queue referenced by head
  410. in a forward direction, assigning each element in turn to var. However,
  411. unlike SIMPLEQ_FOREACH() it is permitted to remove var as well as free it
  412. from within the loop safely without interfering with the traversal.
  413. The SIMPLEQ_EMPTY() macro should be used to check whether a list is
  414. empty.
  415. SIMPLE QUEUE EXAMPLE
  416. SIMPLEQ_HEAD(listhead, entry) head = SIMPLEQ_HEAD_INITIALIZER(head);
  417. struct entry {
  418. ...
  419. SIMPLEQ_ENTRY(entry) entries; /* Simple queue. */
  420. ...
  421. } *n1, *n2, *np;
  422. n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
  423. SIMPLEQ_INSERT_HEAD(&head, n1, entries);
  424. n2 = malloc(sizeof(struct entry)); /* Insert after. */
  425. SIMPLEQ_INSERT_AFTER(&head, n1, n2, entries);
  426. n2 = malloc(sizeof(struct entry)); /* Insert at the tail. */
  427. SIMPLEQ_INSERT_TAIL(&head, n2, entries);
  428. /* Forward traversal. */
  429. SIMPLEQ_FOREACH(np, &head, entries)
  430. np-> ...
  431. /* Delete. */
  432. while (!SIMPLEQ_EMPTY(&head)) {
  433. n1 = SIMPLEQ_FIRST(&head);
  434. SIMPLEQ_REMOVE_HEAD(&head, entries);
  435. free(n1);
  436. }
  437. TAIL QUEUES
  438. A tail queue is headed by a structure defined by the TAILQ_HEAD() macro.
  439. This structure contains a pair of pointers, one to the first element in
  440. the tail queue and the other to the last element in the tail queue. The
  441. elements are doubly linked so that an arbitrary element can be removed
  442. without traversing the tail queue. New elements can be added to the
  443. queue after an existing element, before an existing element, at the head
  444. of the queue, or at the end of the queue. A TAILQ_HEAD structure is
  445. declared as follows:
  446. TAILQ_HEAD(HEADNAME, TYPE) head;
  447. where HEADNAME is the name of the structure to be defined, and struct
  448. TYPE is the type of the elements to be linked into the tail queue. A
  449. pointer to the head of the tail queue can later be declared as:
  450. struct HEADNAME *headp;
  451. (The names head and headp are user selectable.)
  452. The TAILQ_ENTRY() macro declares a structure that connects the elements
  453. in the tail queue.
  454. The TAILQ_INIT() macro initializes the tail queue referenced by head.
  455. The tail queue can also be initialized statically by using the
  456. TAILQ_HEAD_INITIALIZER() macro.
  457. The TAILQ_INSERT_HEAD() macro inserts the new element elm at the head of
  458. the tail queue.
  459. The TAILQ_INSERT_TAIL() macro inserts the new element elm at the end of
  460. the tail queue.
  461. The TAILQ_INSERT_AFTER() macro inserts the new element elm after the
  462. element listelm.
  463. The TAILQ_INSERT_BEFORE() macro inserts the new element elm before the
  464. element listelm.
  465. The TAILQ_REMOVE() macro removes the element elm from the tail queue.
  466. The TAILQ_REPLACE() macro replaces the list element elm with the new
  467. element elm2.
  468. TAILQ_FOREACH() and TAILQ_FOREACH_REVERSE() are used for traversing a
  469. tail queue. TAILQ_FOREACH() starts at the first element and proceeds
  470. towards the last. TAILQ_FOREACH_REVERSE() starts at the last element and
  471. proceeds towards the first.
  472. TAILQ_FOREACH(np, &head, NAME)
  473. TAILQ_FOREACH_REVERSE(np, &head, HEADNAME, NAME)
  474. The macros TAILQ_FOREACH_SAFE() and TAILQ_FOREACH_REVERSE_SAFE() traverse
  475. the list referenced by head in a forward or reverse direction
  476. respectively, assigning each element in turn to var. However, unlike
  477. their unsafe counterparts, they permit both the removal of var as well as
  478. freeing it from within the loop safely without interfering with the
  479. traversal.
  480. The TAILQ_FIRST(), TAILQ_NEXT(), TAILQ_LAST() and TAILQ_PREV() macros can
  481. be used to manually traverse a tail queue or an arbitrary part of one.
  482. The TAILQ_EMPTY() macro should be used to check whether a tail queue is
  483. empty.
  484. TAIL QUEUE EXAMPLE
  485. TAILQ_HEAD(tailhead, entry) head;
  486. struct entry {
  487. ...
  488. TAILQ_ENTRY(entry) entries; /* Tail queue. */
  489. ...
  490. } *n1, *n2, *np;
  491. TAILQ_INIT(&head); /* Initialize queue. */
  492. n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
  493. TAILQ_INSERT_HEAD(&head, n1, entries);
  494. n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
  495. TAILQ_INSERT_TAIL(&head, n1, entries);
  496. n2 = malloc(sizeof(struct entry)); /* Insert after. */
  497. TAILQ_INSERT_AFTER(&head, n1, n2, entries);
  498. n2 = malloc(sizeof(struct entry)); /* Insert before. */
  499. TAILQ_INSERT_BEFORE(n1, n2, entries);
  500. /* Forward traversal. */
  501. TAILQ_FOREACH(np, &head, entries)
  502. np-> ...
  503. /* Manual forward traversal. */
  504. for (np = n2; np != NULL; np = TAILQ_NEXT(np, entries))
  505. np-> ...
  506. /* Delete. */
  507. while ((np = TAILQ_FIRST(&head))) {
  508. TAILQ_REMOVE(&head, np, entries);
  509. free(np);
  510. }
  511. CIRCULAR QUEUES
  512. A circular queue is headed by a structure defined by the CIRCLEQ_HEAD()
  513. macro. This structure contains a pair of pointers, one to the first
  514. element in the circular queue and the other to the last element in the
  515. circular queue. The elements are doubly linked so that an arbitrary
  516. element can be removed without traversing the queue. New elements can be
  517. added to the queue after an existing element, before an existing element,
  518. at the head of the queue, or at the end of the queue. A CIRCLEQ_HEAD
  519. structure is declared as follows:
  520. CIRCLEQ_HEAD(HEADNAME, TYPE) head;
  521. where HEADNAME is the name of the structure to be defined, and struct
  522. TYPE is the type of the elements to be linked into the circular queue. A
  523. pointer to the head of the circular queue can later be declared as:
  524. struct HEADNAME *headp;
  525. (The names head and headp are user selectable.)
  526. The CIRCLEQ_ENTRY() macro declares a structure that connects the elements
  527. in the circular queue.
  528. The CIRCLEQ_INIT() macro initializes the circular queue referenced by
  529. head.
  530. The circular queue can also be initialized statically by using the
  531. CIRCLEQ_HEAD_INITIALIZER() macro.
  532. The CIRCLEQ_INSERT_HEAD() macro inserts the new element elm at the head
  533. of the circular queue.
  534. The CIRCLEQ_INSERT_TAIL() macro inserts the new element elm at the end of
  535. the circular queue.
  536. The CIRCLEQ_INSERT_AFTER() macro inserts the new element elm after the
  537. element listelm.
  538. The CIRCLEQ_INSERT_BEFORE() macro inserts the new element elm before the
  539. element listelm.
  540. The CIRCLEQ_REMOVE() macro removes the element elm from the circular
  541. queue.
  542. The CIRCLEQ_REPLACE() macro replaces the list element elm with the new
  543. element elm2.
  544. The CIRCLEQ_FIRST(), CIRCLEQ_LAST(), CIRCLEQ_END(), CIRCLEQ_NEXT() and
  545. CIRCLEQ_PREV() macros can be used to traverse a circular queue. The
  546. CIRCLEQ_FOREACH() is used for circular queue forward traversal:
  547. CIRCLEQ_FOREACH(np, head, NAME)
  548. The CIRCLEQ_FOREACH_REVERSE() macro acts like CIRCLEQ_FOREACH() but
  549. traverses the circular queue backwards.
  550. The macros CIRCLEQ_FOREACH_SAFE() and CIRCLEQ_FOREACH_REVERSE_SAFE()
  551. traverse the list referenced by head in a forward or reverse direction
  552. respectively, assigning each element in turn to var. However, unlike
  553. their unsafe counterparts, they permit both the removal of var as well as
  554. freeing it from within the loop safely without interfering with the
  555. traversal.
  556. The CIRCLEQ_EMPTY() macro should be used to check whether a circular
  557. queue is empty.
  558. CIRCULAR QUEUE EXAMPLE
  559. CIRCLEQ_HEAD(circleq, entry) head;
  560. struct entry {
  561. ...
  562. CIRCLEQ_ENTRY(entry) entries; /* Circular queue. */
  563. ...
  564. } *n1, *n2, *np;
  565. CIRCLEQ_INIT(&head); /* Initialize circular queue. */
  566. n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
  567. CIRCLEQ_INSERT_HEAD(&head, n1, entries);
  568. n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
  569. CIRCLEQ_INSERT_TAIL(&head, n1, entries);
  570. n2 = malloc(sizeof(struct entry)); /* Insert after. */
  571. CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries);
  572. n2 = malloc(sizeof(struct entry)); /* Insert before. */
  573. CIRCLEQ_INSERT_BEFORE(&head, n1, n2, entries);
  574. /* Forward traversal. */
  575. CIRCLEQ_FOREACH(np, &head, entries)
  576. np-> ...
  577. /* Reverse traversal. */
  578. CIRCLEQ_FOREACH_REVERSE(np, &head, entries)
  579. np-> ...
  580. /* Delete. */
  581. while (!CIRCLEQ_EMPTY(&head)) {
  582. n1 = CIRCLEQ_FIRST(&head);
  583. CIRCLEQ_REMOVE(&head, n1, entries);
  584. free(n1);
  585. }
  586. NOTES
  587. It is an error to assume the next and previous fields are preserved after
  588. an element has been removed from a list or queue. Using any macro
  589. (except the various forms of insertion) on an element removed from a list
  590. or queue is incorrect. An example of erroneous usage is removing the
  591. same element twice.
  592. The SLIST_END(), LIST_END(), SIMPLEQ_END() and TAILQ_END() macros are
  593. provided for symmetry with CIRCLEQ_END(). They expand to NULL and don't
  594. serve any useful purpose.
  595. Trying to free a list in the following way is a common error:
  596. LIST_FOREACH(var, head, entry)
  597. free(var);
  598. free(head);
  599. Since var is free'd, the FOREACH macros refer to a pointer that may have
  600. been reallocated already. A similar situation occurs when the current
  601. element is deleted from the list. In cases like these the data
  602. structure's FOREACH_SAFE macros should be used instead.
  603. HISTORY
  604. The queue functions first appeared in 4.4BSD.
  605. OpenBSD 5.0 April 11, 2012 OpenBSD 5.0
  606. ======================================================================
  607. .\" $OpenBSD: queue.3,v 1.56 2012/04/11 13:29:14 naddy Exp $
  608. .\" $NetBSD: queue.3,v 1.4 1995/07/03 00:25:36 mycroft Exp $
  609. .\"
  610. .\" Copyright (c) 1993 The Regents of the University of California.
  611. .\" All rights reserved.
  612. .\"
  613. .\" Redistribution and use in source and binary forms, with or without
  614. .\" modification, are permitted provided that the following conditions
  615. .\" are met:
  616. .\" 1. Redistributions of source code must retain the above copyright
  617. .\" notice, this list of conditions and the following disclaimer.
  618. .\" 2. Redistributions in binary form must reproduce the above copyright
  619. .\" notice, this list of conditions and the following disclaimer in the
  620. .\" documentation and/or other materials provided with the distribution.
  621. .\" 3. Neither the name of the University nor the names of its contributors
  622. .\" may be used to endorse or promote products derived from this software
  623. .\" without specific prior written permission.
  624. .\"
  625. .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  626. .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  627. .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  628. .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  629. .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  630. .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  631. .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  632. .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  633. .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  634. .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  635. .\" SUCH DAMAGE.