]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/queue.3
Make the standard indent for code samples, shell session
[thirdparty/man-pages.git] / man3 / queue.3
1 .\" Copyright (c) 1993
2 .\" The Regents of the University of California. All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\" @(#)queue.3 8.2 (Berkeley) 1/24/94
33 .\"
34 .\" hch, 2002-03-25
35 .\" 2007-12-08, mtk, Converted from mdoc to man macros
36 .\"
37 .TH QUEUE 3 2007-12-08 "Linux" "Linux Programmer's Manual"
38 .SH NAME
39 LIST_ENTRY, LIST_HEAD, LIST_INIT, LIST_INSERT_AFTER, \
40 LIST_INSERT_HEAD, LIST_REMOVE, TAILQ_ENTRY, TAILQ_HEAD, \
41 TAILQ_INIT, TAILQ_INSERT_AFTER, TAILQ_INSERT_HEAD, TAILQ_INSERT_TAIL, \
42 TAILQ_REMOVE, CIRCLEQ_ENTRY, CIRCLEQ_HEAD, CIRCLEQ_INIT, \
43 CIRCLEQ_INSERT_AFTER, CIRCLEQ_INSERT_BEFORE, \
44 CIRCLEQ_INSERT_HEAD, CIRCLEQ_INSERT_TAIL, \
45 CIRCLEQ_REMOVE \- implementations of lists, tail queues, and circular queues
46 .SH SYNOPSIS
47 .nf
48 .B #include <sys/queue.h>
49
50 .BI "LIST_ENTRY(" TYPE );
51 .BI "LIST_HEAD(" HEADNAME ", " TYPE );
52 .BI "LIST_INIT(LIST_HEAD *" head );
53 .BI "LIST_INSERT_AFTER(LIST_ENTRY *" listelm ", "
54 .BI " TYPE *" elm ", LIST_ENTRY " NAME );
55 .BI "LIST_INSERT_HEAD(LIST_HEAD *" head ", "
56 .BI " TYPE *" elm ", LIST_ENTRY " NAME );
57 .BI "LIST_REMOVE(TYPE *" elm ", LIST_ENTRY " NAME );
58
59 .BI "TAILQ_ENTRY(" TYPE );
60 .BI "TAILQ_HEAD("HEADNAME ", " TYPE );
61 .BI "TAILQ_INIT(TAILQ_HEAD *" head );
62 .BI "TAILQ_INSERT_AFTER(TAILQ_HEAD *" head ", TYPE *" listelm ", "
63 .BI " TYPE *" elm ", TAILQ_ENTRY " NAME );
64 .BI "TAILQ_INSERT_HEAD(TAILQ_HEAD *" head ", "
65 .BI " TYPE *" elm ", TAILQ_ENTRY " NAME );
66 .BI "TAILQ_INSERT_TAIL(TAILQ_HEAD *" head ", "
67 .BI " TYPE *" elm ", TAILQ_ENTRY " NAME );
68 .BI "TAILQ_REMOVE(TAILQ_HEAD *" head ", TYPE *" elm ", TAILQ_ENTRY " NAME );
69
70 .BI CIRCLEQ_ENTRY( TYPE );
71 .BI "CIRCLEQ_HEAD(" HEADNAME ", " TYPE );
72 .BI "CIRCLEQ_INIT(CIRCLEQ_HEAD *" head );
73 .BI "CIRCLEQ_INSERT_AFTER(CIRCLEQ_HEAD *" head ", TYPE *" listelm ", "
74 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
75 .BI "CIRCLEQ_INSERT_BEFORE(CIRCLEQ_HEAD *" head ", TYPE *" listelm ", "
76 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
77 .BI "CIRCLEQ_INSERT_HEAD(CIRCLEQ_HEAD *" head ", "
78 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
79 .BI "CIRCLEQ_INSERT_TAIL(CIRCLEQ_HEAD *" head ", "
80 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
81 .BI "CIRCLEQ_REMOVE(CIRCLEQ_HEAD *" head ", "
82 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
83 .fi
84 .SH DESCRIPTION
85 These macros define and operate on three types of data structures:
86 lists, tail queues, and circular queues.
87 All three structures support the following functionality:
88 .RS
89 .IP 1. 4
90 Insertion of a new entry at the head of the list.
91 .IP 2.
92 Insertion of a new entry after any element in the list.
93 .IP 3.
94 Removal of any entry in the list.
95 .IP 4.
96 Forward traversal through the list.
97 .RE
98 .PP
99 Lists are the simplest of the three data structures and support
100 only the above functionality.
101
102 Tail queues add the following functionality:
103 .RS
104 .IP 1. 4
105 Entries can be added at the end of a list.
106 .RE
107 .PP
108 However:
109 .RS
110 .IP 1. 4
111 All list insertions and removals must specify the head of the list.
112 .IP 2.
113 Each head entry requires two pointers rather than one.
114 .IP 3.
115 Code size is about 15% greater and operations run about 20% slower
116 than lists.
117 .RE
118 .PP
119 Circular queues add the following functionality:
120 .RS
121 .IP 1. 4
122 Entries can be added at the end of a list.
123 .IP 2.
124 Entries can be added before another entry.
125 .IP 3.
126 They may be traversed backwards, from tail to head.
127 .RE
128 .PP
129 However:
130 .RS
131 .IP 1. 4
132 All list insertions and removals must specify the head of the list.
133 .IP 2.
134 Each head entry requires two pointers rather than one.
135 .IP 3.
136 The termination condition for traversal is more complex.
137 .IP 4.
138 Code size is about 40% greater and operations run about 45% slower
139 than lists.
140 .RE
141 .PP
142 In the macro definitions,
143 .I TYPE
144 is the name of a user defined structure,
145 that must contain a field of type
146 .IR "LIST_ENTRY" ,
147 .IR "TAILQ_ENTRY" ,
148 or
149 .IR "CIRCLEQ_ENTRY" ,
150 named
151 .IR NAME .
152 The argument
153 .I HEADNAME
154 is the name of a user defined structure that must be declared
155 using the macros
156 .IR "LIST_HEAD" ,
157 .IR "TAILQ_HEAD" ,
158 or
159 .IR "CIRCLEQ_HEAD" .
160 See the examples below for further explanation of how these
161 macros are used.
162 .SS Lists
163 A list is headed by a structure defined by the
164 LIST_HEAD macro.
165 This structure contains a single pointer to the first element
166 on the list.
167 The elements are doubly linked so that an arbitrary element can be
168 removed without traversing the list.
169 New elements can be added to the list after an existing element or
170 at the head of the list.
171 A
172 .I LIST_HEAD
173 structure is declared as follows:
174 .in +4n
175 .nf
176
177 LIST_HEAD(HEADNAME, TYPE) head;
178 .fi
179 .in
180 .sp
181 where
182 .I HEADNAME
183 is the name of the structure to be defined, and
184 .I TYPE
185 is the type of the elements to be linked into the list.
186 A pointer to the head of the list can later be declared as:
187 .in +4n
188 .nf
189
190 struct HEADNAME *headp;
191 .fi
192 .in
193 .sp
194 (The names
195 .IR "head"
196 and
197 .IR "headp"
198 are user selectable.)
199 .sp
200 The macro
201 .B LIST_ENTRY
202 declares a structure that connects the elements in
203 the list.
204 .sp
205 The macro
206 .B LIST_INIT
207 initializes the list referenced by
208 .IR head .
209 .sp
210 The macro
211 .B LIST_INSERT_HEAD
212 inserts the new element
213 .I elm
214 at the head of the list.
215 .sp
216 The macro
217 .B LIST_INSERT_AFTER
218 inserts the new element
219 .I elm
220 after the element
221 .IR listelm .
222 .sp
223 The macro
224 .B LIST_REMOVE
225 removes the element
226 .I elm
227 from the list.
228 .SS List Example
229 .nf
230 LIST_HEAD(listhead, entry) head;
231 struct listhead *headp; /* List head. */
232 struct entry {
233 ...
234 LIST_ENTRY(entry) entries; /* List. */
235 ...
236 } *n1, *n2, *np;
237
238 LIST_INIT(&head); /* Initialize the list. */
239
240 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
241 LIST_INSERT_HEAD(&head, n1, entries);
242
243 n2 = malloc(sizeof(struct entry)); /* Insert after. */
244 LIST_INSERT_AFTER(n1, n2, entries);
245 /* Forward traversal. */
246 for (np = head.lh_first; np != NULL; np = np\->entries.le_next)
247 np\-> ...
248
249 while (head.lh_first != NULL) /* Delete. */
250 LIST_REMOVE(head.lh_first, entries);
251 .fi
252 .SS Tail Queues
253 A tail queue is headed by a structure defined by the
254 TAILQ_HEAD macro.
255 This structure contains a pair of pointers,
256 one to the first element in the tail queue and the other to
257 the last element in the tail queue.
258 The elements are doubly linked so that an arbitrary element can be
259 removed without traversing the tail queue.
260 New elements can be added to the tail queue after an existing element,
261 at the head of the tail queue, or at the end of the tail queue.
262 A
263 .I TAILQ_HEAD
264 structure is declared as follows:
265 .in +4n
266 .nf
267
268 TAILQ_HEAD(HEADNAME, TYPE) head;
269 .fi
270 .in
271 .PP
272 where
273 .IR "HEADNAME"
274 is the name of the structure to be defined, and
275 .IR "TYPE"
276 is the type of the elements to be linked into the tail queue.
277 A pointer to the head of the tail queue can later be declared as:
278 .in +4n
279 .nf
280
281 struct HEADNAME *headp;
282 .fi
283 .in
284 .sp
285 (The names
286 .IR "head"
287 and
288 .IR "headp"
289 are user selectable.)
290 .sp
291 The macro
292 .B TAILQ_ENTRY
293 declares a structure that connects the elements in
294 the tail queue.
295 .sp
296 The macro
297 .B TAILQ_INIT
298 initializes the tail queue referenced by
299 .IR head .
300 .sp
301 The macro
302 .B TAILQ_INSERT_HEAD
303 inserts the new element
304 .I elm
305 at the head of the tail queue.
306 .sp
307 The macro
308 .B TAILQ_INSERT_TAIL
309 inserts the new element
310 .I elm
311 at the end of the tail queue.
312 .sp
313 The macro
314 .B TAILQ_INSERT_AFTER
315 inserts the new element
316 .I elm
317 after the element
318 .IR listelm .
319 .sp
320 The macro
321 .B TAILQ_REMOVE
322 removes the element
323 .I elm
324 from the tail queue.
325 .SS Tail Queue Example
326 .nf
327 TAILQ_HEAD(tailhead, entry) head;
328 struct tailhead *headp; /* Tail queue head. */
329 struct entry {
330 ...
331 TAILQ_ENTRY(entry) entries; /* Tail queue. */
332 ...
333 } *n1, *n2, *np;
334
335 TAILQ_INIT(&head); /* Initialize the queue. */
336
337 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
338 TAILQ_INSERT_HEAD(&head, n1, entries);
339
340 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
341 TAILQ_INSERT_TAIL(&head, n1, entries);
342
343 n2 = malloc(sizeof(struct entry)); /* Insert after. */
344 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
345 /* Forward traversal. */
346 for (np = head.tqh_first; np != NULL; np = np->entries.tqe_next)
347 np-> ...
348 /* Delete. */
349 while (head.tqh_first != NULL)
350 TAILQ_REMOVE(&head, head.tqh_first, entries);
351 .fi
352 .SS Circular Queues
353 A circular queue is headed by a structure defined by the
354 CIRCLEQ_HEAD macro.
355 This structure contains a pair of pointers,
356 one to the first element in the circular queue and the other to the
357 last element in the circular queue.
358 The elements are doubly linked so that an arbitrary element can be
359 removed without traversing the queue.
360 New elements can be added to the queue after an existing element,
361 before an existing element, at the head of the queue, or at the end
362 of the queue.
363 A
364 .I CIRCLEQ_HEAD
365 structure is declared as follows:
366 .in +4n
367 .nf
368
369 CIRCLEQ_HEAD(HEADNAME, TYPE) head;
370 .fi
371 .in
372 .sp
373 where
374 .IR "HEADNAME"
375 is the name of the structure to be defined, and
376 .IR "TYPE"
377 is the type of the elements to be linked into the circular queue.
378 A pointer to the head of the circular queue can later be declared as:
379 .in +4n
380 .nf
381
382 struct HEADNAME *headp;
383 .fi
384 .in
385 .sp
386 (The names
387 .IR "head"
388 and
389 .IR "headp"
390 are user selectable.)
391 .sp
392 The macro
393 .B CIRCLEQ_ENTRY
394 declares a structure that connects the elements in
395 the circular queue.
396 .sp
397 The macro
398 .B CIRCLEQ_INIT
399 initializes the circular queue referenced by
400 .IR head .
401 .sp
402 The macro
403 .B CIRCLEQ_INSERT_HEAD
404 inserts the new element
405 .I elm
406 at the head of the circular queue.
407 .sp
408 The macro
409 .B CIRCLEQ_INSERT_TAIL
410 inserts the new element
411 .I elm
412 at the end of the circular queue.
413 .sp
414 The macro
415 .B CIRCLEQ_INSERT_AFTER
416 inserts the new element
417 .I elm
418 after the element
419 .IR listelm .
420 .sp
421 The macro
422 .B CIRCLEQ_INSERT_BEFORE
423 inserts the new element
424 .I elm
425 before the element
426 .IR listelm .
427 .sp
428 The macro
429 .B CIRCLEQ_REMOVE
430 removes the element
431 .I elm
432 from the circular queue.
433 .SS Circular Queue Example
434 .nf
435 CIRCLEQ_HEAD(circleq, entry) head;
436 struct circleq *headp; /* Circular queue head. */
437 struct entry {
438 ...
439 CIRCLEQ_ENTRY(entry) entries; /* Circular queue. */
440 ...
441 } *n1, *n2, *np;
442
443 CIRCLEQ_INIT(&head); /* Initialize the circular queue. */
444
445 n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
446 CIRCLEQ_INSERT_HEAD(&head, n1, entries);
447
448 n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
449 CIRCLEQ_INSERT_TAIL(&head, n1, entries);
450
451 n2 = malloc(sizeof(struct entry)); /* Insert after. */
452 CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries);
453
454 n2 = malloc(sizeof(struct entry)); /* Insert before. */
455 CIRCLEQ_INSERT_BEFORE(&head, n1, n2, entries);
456 /* Forward traversal. */
457 for (np = head.cqh_first; np != (void *)&head;
458 np = np->entries.cqe_next)
459 np-> ...
460 /* Reverse traversal. */
461 for (np = head.cqh_last; np != (void *)&head; np = np->entries.cqe_prev)
462 np-> ...
463 /* Delete. */
464 while (head.cqh_first != (void *)&head)
465 CIRCLEQ_REMOVE(&head, head.cqh_first, entries);
466 .fi
467 .SH "CONFORMING TO"
468 Not in POSIX.1-2001.
469 Present on the BSDs.
470 The
471 queue functions first appeared in
472 4.4BSD.