OSDN Git Service

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