xref: /csrg-svn/share/man/man3/queue.3 (revision 65120)
1*65120Smckusick.\" Copyright (c) 1993 The Regents of the University of California.
2*65120Smckusick.\" All rights reserved.
3*65120Smckusick.\"
4*65120Smckusick.\" %sccs.include.redist.roff%
5*65120Smckusick.\"
6*65120Smckusick.\"	@(#)queue.3	8.1 (Berkeley) 12/13/93
7*65120Smckusick.\"
8*65120Smckusick.Dd ""
9*65120Smckusick.Dt QUEUE 3
10*65120Smckusick.Os BSD 4
11*65120Smckusick.Sh NAME
12*65120Smckusick.Nm LIST_ENTRY ,
13*65120Smckusick.Nm LIST_HEAD ,
14*65120Smckusick.Nm LIST_INIT ,
15*65120Smckusick.Nm LIST_INSERT_AFTER ,
16*65120Smckusick.Nm LIST_INSERT_HEAD ,
17*65120Smckusick.Nm LIST_REMOVE ,
18*65120Smckusick.Nm TAILQ_ENTRY ,
19*65120Smckusick.Nm TAILQ_HEAD ,
20*65120Smckusick.Nm TAILQ_INIT ,
21*65120Smckusick.Nm TAILQ_INSERT_AFTER ,
22*65120Smckusick.Nm TAILQ_INSERT_HEAD ,
23*65120Smckusick.Nm TAILQ_INSERT_TAIL ,
24*65120Smckusick.Nm TAILQ_REMOVE ,
25*65120Smckusick.Nm CIRCLEQ_ENTRY ,
26*65120Smckusick.Nm CIRCLEQ_HEAD ,
27*65120Smckusick.Nm CIRCLEQ_INIT ,
28*65120Smckusick.Nm CIRCLEQ_INSERT_AFTER ,
29*65120Smckusick.Nm CIRCLEQ_INSERT_BEFORE ,
30*65120Smckusick.Nm CIRCLEQ_INSERT_HEAD ,
31*65120Smckusick.Nm CIRCLEQ_INSERT_TAIL ,
32*65120Smckusick.Nm CIRCLEQ_REMOVE
33*65120Smckusick.Nd implementations of lists, tail queues, and circular queues
34*65120Smckusick.Sh SYNOPSIS
35*65120Smckusick.Fd #include <sys/queue.h>
36*65120Smckusick.sp
37*65120Smckusick.Fn LIST_ENTRY "TYPE"
38*65120Smckusick.Fn LIST_HEAD "HEADNAME" "TYPE"
39*65120Smckusick.Fn LIST_INIT "LIST_HEAD *head"
40*65120Smckusick.Fn LIST_INSERT_AFTER "LIST_ENTRY *listelm" "TYPE *elm" "LIST_ENTRY NAME"
41*65120Smckusick.Fn LIST_INSERT_HEAD "LIST_HEAD *head" "TYPE *elm" "LIST_ENTRY NAME"
42*65120Smckusick.Fn LIST_REMOVE "TYPE *elm" "LIST_ENTRY NAME"
43*65120Smckusick.sp
44*65120Smckusick.Fn TAILQ_ENTRY "TYPE"
45*65120Smckusick.Fn TAILQ_HEAD "HEADNAME" "TYPE"
46*65120Smckusick.Fn TAILQ_INIT "TAILQ_HEAD *head"
47*65120Smckusick.Fn TAILQ_INSERT_AFTER "TAILQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "TAILQ_ENTRY NAME"
48*65120Smckusick.Fn TAILQ_INSERT_HEAD "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
49*65120Smckusick.Fn TAILQ_INSERT_TAIL "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
50*65120Smckusick.Fn TAILQ_REMOVE "TAILQ_HEAD *head" "TYPE *elm" "TAILQ_ENTRY NAME"
51*65120Smckusick.sp
52*65120Smckusick.Fn CIRCLEQ_ENTRY "TYPE"
53*65120Smckusick.Fn CIRCLEQ_HEAD "HEADNAME" "TYPE"
54*65120Smckusick.Fn CIRCLEQ_INIT "CIRCLEQ_HEAD *head"
55*65120Smckusick.Fn CIRCLEQ_INSERT_AFTER "CIRCLEQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
56*65120Smckusick.Fn CIRCLEQ_INSERT_BEFORE "CIRCLEQ_HEAD *head" "TYPE *listelm" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
57*65120Smckusick.Fn CIRCLEQ_INSERT_HEAD "CIRCLEQ_HEAD *head" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
58*65120Smckusick.Fn CIRCLEQ_INSERT_TAIL "CIRCLEQ_HEAD *head" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
59*65120Smckusick.Fn CIRCLEQ_REMOVE "CIRCLEQ_HEAD *head" "TYPE *elm" "CIRCLEQ_ENTRY NAME"
60*65120Smckusick.Sh DESCRIPTION
61*65120SmckusickThese macros define and operate on three types of data structures:
62*65120Smckusicklists, tail queues, and circular queues.
63*65120SmckusickAll three structures support the following functionality:
64*65120Smckusick.Bl -enum -compact -offset indent
65*65120Smckusick.It
66*65120SmckusickInsertion of a new entry at the head of the list.
67*65120Smckusick.It
68*65120SmckusickInsertion of a new entry after any element in the list.
69*65120Smckusick.It
70*65120SmckusickRemoval of any entry in the list.
71*65120Smckusick.It
72*65120SmckusickForward traversal through the list.
73*65120Smckusick.El
74*65120Smckusick.Pp
75*65120SmckusickLists are the simplest of the three data structures and support
76*65120Smckusickonly the above functionality.
77*65120Smckusick.Pp
78*65120SmckusickTail queues add the following functionality:
79*65120Smckusick.Bl -enum -compact -offset indent
80*65120Smckusick.It
81*65120SmckusickEntries can be added at the end of a list.
82*65120Smckusick.El
83*65120SmckusickHowever:
84*65120Smckusick.Bl -enum -compact -offset indent
85*65120Smckusick.It
86*65120SmckusickAll list insertions and removals must specify the head of the list.
87*65120Smckusick.It
88*65120SmckusickEach head entry requires two pointers rather than one.
89*65120Smckusick.It
90*65120SmckusickCode size is about 15% greater and operations run about 20% slower
91*65120Smckusickthan lists.
92*65120Smckusick.El
93*65120Smckusick.Pp
94*65120SmckusickCircular queues add the following functionality:
95*65120Smckusick.Bl -enum -compact -offset indent
96*65120Smckusick.It
97*65120SmckusickEntries can be added at the end of a list.
98*65120Smckusick.It
99*65120SmckusickEntries can be added before another entry.
100*65120Smckusick.It
101*65120SmckusickThey may be traversed backwards, from tail to head.
102*65120Smckusick.El
103*65120SmckusickHowever:
104*65120Smckusick.Bl -enum -compact -offset indent
105*65120Smckusick.It
106*65120SmckusickAll list insertions and removals must specify the head of the list.
107*65120Smckusick.It
108*65120SmckusickEach head entry requires two pointers rather than one.
109*65120Smckusick.It
110*65120SmckusickThe termination condition for traversal is more complex.
111*65120Smckusick.It
112*65120SmckusickCode size is about 40% greater and operations run about 45% slower
113*65120Smckusickthan lists.
114*65120Smckusick.El
115*65120Smckusick.Pp
116*65120SmckusickIn the macro definitions,
117*65120Smckusick.Fa TYPE
118*65120Smckusickis the name of a user defined structure,
119*65120Smckusickthat must contain a field of type
120*65120Smckusick.Li LIST_ENTRY ,
121*65120Smckusick.Li TAILQ_ENTRY ,
122*65120Smckusickor
123*65120Smckusick.Li CIRCLEQ_ENTRY ,
124*65120Smckusicknamed
125*65120Smckusick.Fa NAME .
126*65120SmckusickThe argument
127*65120Smckusick.Fa HEADNAME
128*65120Smckusickis the name of a user defined structure that must be declared
129*65120Smckusickusing the macros
130*65120Smckusick.Li LIST_HEAD ,
131*65120Smckusick.Li TAILQ_HEAD ,
132*65120Smckusickor
133*65120Smckusick.Li CIRCLEQ_HEAD .
134*65120SmckusickSee the examples below for further explanation of how these
135*65120Smckusickmacros are used.
136*65120Smckusick.Sh LISTS
137*65120SmckusickA list is headed by a structure defined by the
138*65120Smckusick.Nm LIST_HEAD
139*65120Smckusickmacro.
140*65120SmckusickThis structure contains a single pointer to the first element
141*65120Smckusickon the list.
142*65120SmckusickThe elements are doubly linked so that an arbitrary element can be
143*65120Smckusickremoved without traversing the list.
144*65120SmckusickNew elements can be added to the list after an existing element or
145*65120Smckusickat the head of the list.
146*65120SmckusickA
147*65120Smckusick.Fa LIST_HEAD
148*65120Smckusickstructure is declared as follows:
149*65120Smckusick.Bd -literal -offset indent
150*65120SmckusickLIST_HEAD(HEADNAME, TYPE) head;
151*65120Smckusick.Ed
152*65120Smckusick.sp
153*65120Smckusickwhere
154*65120Smckusick.Fa HEADNAME
155*65120Smckusickis the name of the structure to be defined, and
156*65120Smckusick.Fa TYPE
157*65120Smckusickis the type of the elements to be linked into the list.
158*65120SmckusickA pointer to the head of the list can later be declared as:
159*65120Smckusick.Bd -literal -offset indent
160*65120Smckusickstruct HEADNAME *headp;
161*65120Smckusick.Ed
162*65120Smckusick.sp
163*65120Smckusick(The names
164*65120Smckusick.Li head
165*65120Smckusickand
166*65120Smckusick.Li headp
167*65120Smckusickare user selectable.)
168*65120Smckusick.Pp
169*65120SmckusickThe macro
170*65120Smckusick.Nm LIST_ENTRY
171*65120Smckusickdeclares a structure that connects the elements in
172*65120Smckusickthe list.
173*65120Smckusick.Pp
174*65120SmckusickThe macro
175*65120Smckusick.Nm LIST_INIT
176*65120Smckusickinitializes the list referenced by
177*65120Smckusick.Fa head .
178*65120Smckusick.Pp
179*65120SmckusickThe macro
180*65120Smckusick.Nm LIST_INSERT_HEAD
181*65120Smckusickinserts the new element
182*65120Smckusick.Fa elm
183*65120Smckusickat the head of the list.
184*65120Smckusick.Pp
185*65120SmckusickThe macro
186*65120Smckusick.Nm LIST_INSERT_AFTER
187*65120Smckusickinserts the new element
188*65120Smckusick.Fa elm
189*65120Smckusickafter the element
190*65120Smckusick.Fa listelm .
191*65120Smckusick.Pp
192*65120SmckusickThe macro
193*65120Smckusick.Nm LIST_REMOVE
194*65120Smckusickremoves the element
195*65120Smckusick.Fa elm
196*65120Smckusickfrom the list.
197*65120Smckusick.Sh LIST EXAMPLE
198*65120Smckusick.Bd -literal
199*65120SmckusickLIST_HEAD(listhead, entry) head;
200*65120Smckusickstruct listhead *headp;		/* List head. */
201*65120Smckusickstruct entry {
202*65120Smckusick	...
203*65120Smckusick	LIST_ENTRY(entry) entries;	/* List. */
204*65120Smckusick	...
205*65120Smckusick} *n1, *n2, *np;
206*65120Smckusick
207*65120SmckusickLIST_INIT(&head);			/* Initialize the list. */
208*65120Smckusick
209*65120Smckusickn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
210*65120SmckusickLIST_INSERT_HEAD(&head, n1, entries);
211*65120Smckusick
212*65120Smckusickn2 = malloc(sizeof(struct entry));	/* Insert after. */
213*65120SmckusickLIST_INSERT_AFTER(n1, n2, entries);
214*65120Smckusick					/* Forward traversal. */
215*65120Smckusickfor (np = head.lh_first; np != NULL; np = np->entries.le_next)
216*65120Smckusick	np-> ...
217*65120Smckusick
218*65120Smckusickwhile (head.lh_first != NULL)		/* Delete. */
219*65120Smckusick	LIST_REMOVE(head.lh_first, entries);
220*65120Smckusick.Ed
221*65120Smckusick.Sh TAIL QUEUES
222*65120SmckusickA tail queue is headed by a structure defined by the
223*65120Smckusick.Nm TAILQ_HEAD
224*65120Smckusickmacro.
225*65120SmckusickThis structure contains a pair of pointers,
226*65120Smckusickone to the first element in the tail queue and the other to
227*65120Smckusickthe last element in the tail queue.
228*65120SmckusickThe elements are doubly linked so that an arbitrary element can be
229*65120Smckusickremoved without traversing the tail queue.
230*65120SmckusickNew elements can be added to the tail queue after an existing element,
231*65120Smckusickat the head of the tail queue, or at the end of the tail queue.
232*65120SmckusickA
233*65120Smckusick.Fa TAILQ_HEAD
234*65120Smckusickstructure is declared as follows:
235*65120Smckusick.Bd -literal -offset indent
236*65120SmckusickTAILQ_HEAD(HEADNAME, TYPE) head;
237*65120Smckusick.Ed
238*65120Smckusick.sp
239*65120Smckusickwhere
240*65120Smckusick.Li HEADNAME
241*65120Smckusickis the name of the structure to be defined, and
242*65120Smckusick.Li TYPE
243*65120Smckusickis the type of the elements to be linked into the tail queue.
244*65120SmckusickA pointer to the head of the tail queue can later be declared as:
245*65120Smckusick.Bd -literal -offset indent
246*65120Smckusickstruct HEADNAME *headp;
247*65120Smckusick.Ed
248*65120Smckusick.sp
249*65120Smckusick(The names
250*65120Smckusick.Li head
251*65120Smckusickand
252*65120Smckusick.Li headp
253*65120Smckusickare user selectable.)
254*65120Smckusick.Pp
255*65120SmckusickThe macro
256*65120Smckusick.Nm TAILQ_ENTRY
257*65120Smckusickdeclares a structure that connects the elements in
258*65120Smckusickthe tail queue.
259*65120Smckusick.Pp
260*65120SmckusickThe macro
261*65120Smckusick.Nm TAILQ_INIT
262*65120Smckusickinitializes the tail queue referenced by
263*65120Smckusick.Fa head .
264*65120Smckusick.Pp
265*65120SmckusickThe macro
266*65120Smckusick.Nm TAILQ_INSERT_HEAD
267*65120Smckusickinserts the new element
268*65120Smckusick.Fa elm
269*65120Smckusickat the head of the tail queue.
270*65120Smckusick.Pp
271*65120SmckusickThe macro
272*65120Smckusick.Nm TAILQ_INSERT_TAIL
273*65120Smckusickinserts the new element
274*65120Smckusick.Fa elm
275*65120Smckusickat the end of the tail queue.
276*65120Smckusick.Pp
277*65120SmckusickThe macro
278*65120Smckusick.Nm TAILQ_INSERT_AFTER
279*65120Smckusickinserts the new element
280*65120Smckusick.Fa elm
281*65120Smckusickafter the element
282*65120Smckusick.Fa listelm .
283*65120Smckusick.Pp
284*65120SmckusickThe macro
285*65120Smckusick.Nm TAILQ_REMOVE
286*65120Smckusickremoves the element
287*65120Smckusick.Fa elm
288*65120Smckusickfrom the tail queue.
289*65120Smckusick.Sh TAIL QUEUE EXAMPLE
290*65120Smckusick.Bd -literal
291*65120SmckusickTAILQ_HEAD(tailhead, entry) head;
292*65120Smckusickstruct tailhead *headp;		/* Tail queue head. */
293*65120Smckusickstruct entry {
294*65120Smckusick	...
295*65120Smckusick	TAILQ_ENTRY(entry) entries;	/* Tail queue. */
296*65120Smckusick	...
297*65120Smckusick} *n1, *n2, *np;
298*65120Smckusick
299*65120SmckusickTAILQ_INIT(&head);			/* Initialize the queue. */
300*65120Smckusick
301*65120Smckusickn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
302*65120SmckusickTAILQ_INSERT_HEAD(&head, n1, entries);
303*65120Smckusick
304*65120Smckusickn1 = malloc(sizeof(struct entry));	/* Insert at the tail. */
305*65120SmckusickTAILQ_INSERT_TAIL(&head, n1, entries);
306*65120Smckusick
307*65120Smckusickn2 = malloc(sizeof(struct entry));	/* Insert after. */
308*65120SmckusickTAILQ_INSERT_AFTER(&head, n1, n2, entries);
309*65120Smckusick					/* Forward traversal. */
310*65120Smckusickfor (np = head.tqh_first; np != NULL; np = np->entries.tqe_next)
311*65120Smckusick	np-> ...
312*65120Smckusick					/* Delete. */
313*65120Smckusickwhile (head.tqh_first != NULL)
314*65120Smckusick	TAILQ_REMOVE(&head, head.tqh_first, entries);
315*65120Smckusick.Ed
316*65120Smckusick.Sh CIRCULAR QUEUES
317*65120SmckusickA circular queue is headed by a structure defined by the
318*65120Smckusick.Nm CIRCLEQ_HEAD
319*65120Smckusickmacro.
320*65120SmckusickThis structure contains a pair of pointers,
321*65120Smckusickone to the first element in the circular queue and the other to the
322*65120Smckusicklast element in the circular queue.
323*65120SmckusickThe elements are doubly linked so that an arbitrary element can be
324*65120Smckusickremoved without traversing the queue.
325*65120SmckusickNew elements can be added to the queue after an existing element,
326*65120Smckusickbefore an existing element, at the head of the queue, or at the end
327*65120Smckusickof the queue.
328*65120SmckusickA
329*65120Smckusick.Fa CIRCLEQ_HEAD
330*65120Smckusickstructure is declared as follows:
331*65120Smckusick.Bd -literal -offset indent
332*65120SmckusickCIRCLEQ_HEAD(HEADNAME, TYPE) head;
333*65120Smckusick.Ed
334*65120Smckusick.sp
335*65120Smckusickwhere
336*65120Smckusick.Li HEADNAME
337*65120Smckusickis the name of the structure to be defined, and
338*65120Smckusick.Li TYPE
339*65120Smckusickis the type of the elements to be linked into the circular queue.
340*65120SmckusickA pointer to the head of the circular queue can later be declared as:
341*65120Smckusick.Bd -literal -offset indent
342*65120Smckusickstruct HEADNAME *headp;
343*65120Smckusick.Ed
344*65120Smckusick.sp
345*65120Smckusick(The names
346*65120Smckusick.Li head
347*65120Smckusickand
348*65120Smckusick.Li headp
349*65120Smckusickare user selectable.)
350*65120Smckusick.Pp
351*65120SmckusickThe macro
352*65120Smckusick.Nm CIRCLEQ_ENTRY
353*65120Smckusickdeclares a structure that connects the elements in
354*65120Smckusickthe circular queue.
355*65120Smckusick.Pp
356*65120SmckusickThe macro
357*65120Smckusick.Nm CIRCLEQ_INIT
358*65120Smckusickinitializes the circular queue referenced by
359*65120Smckusick.Fa head .
360*65120Smckusick.Pp
361*65120SmckusickThe macro
362*65120Smckusick.Nm CIRCLEQ_INSERT_HEAD
363*65120Smckusickinserts the new element
364*65120Smckusick.Fa elm
365*65120Smckusickat the head of the circular queue.
366*65120Smckusick.Pp
367*65120SmckusickThe macro
368*65120Smckusick.Nm CIRCLEQ_INSERT_TAIL
369*65120Smckusickinserts the new element
370*65120Smckusick.Fa elm
371*65120Smckusickat the end of the circular queue.
372*65120Smckusick.Pp
373*65120SmckusickThe macro
374*65120Smckusick.Nm CIRCLEQ_INSERT_AFTER
375*65120Smckusickinserts the new element
376*65120Smckusick.Fa elm
377*65120Smckusickafter the element
378*65120Smckusick.Fa listelm .
379*65120Smckusick.Pp
380*65120SmckusickThe macro
381*65120Smckusick.Nm CIRCLEQ_INSERT_BEFORE
382*65120Smckusickinserts the new element
383*65120Smckusick.Fa elm
384*65120Smckusickbefore the element
385*65120Smckusick.Fa listelm .
386*65120Smckusick.Pp
387*65120SmckusickThe macro
388*65120Smckusick.Nm CIRCLEQ_REMOVE
389*65120Smckusickremoves the element
390*65120Smckusick.Fa elm
391*65120Smckusickfrom the circular queue.
392*65120Smckusick.Sh CIRCULAR QUEUE EXAMPLE
393*65120Smckusick.Bd -literal
394*65120SmckusickCIRCLEQ_HEAD(circleq, entry) head;
395*65120Smckusickstruct circleq *headp;			/* Circular queue head. */
396*65120Smckusickstruct entry {
397*65120Smckusick	...
398*65120Smckusick	CIRCLEQ_ENTRY entries;		/* Circular queue. */
399*65120Smckusick	...
400*65120Smckusick} *n1, *n2, *np;
401*65120Smckusick
402*65120SmckusickCIRCLEQ_INIT(&head);			/* Initialize the circular queue. */
403*65120Smckusick
404*65120Smckusickn1 = malloc(sizeof(struct entry));	/* Insert at the head. */
405*65120SmckusickCIRCLEQ_INSERT_HEAD(&head, n1, entries);
406*65120Smckusick
407*65120Smckusickn1 = malloc(sizeof(struct entry));	/* Insert at the tail. */
408*65120SmckusickCIRCLEQ_INSERT_TAIL(&head, n1, entries);
409*65120Smckusick
410*65120Smckusickn2 = malloc(sizeof(struct entry));	/* Insert after. */
411*65120SmckusickCIRCLEQ_INSERT_AFTER(&head, n1, n2, entries);
412*65120Smckusick
413*65120Smckusickn2 = malloc(sizeof(struct entry));	/* Insert before. */
414*65120SmckusickCIRCLEQ_INSERT_BEFORE(&head, n1, n2, entries);
415*65120Smckusick					/* Forward traversal. */
416*65120Smckusickfor (np = head.cqh_first; np != (void *)&head; np = np->entries.cqe_next)
417*65120Smckusick	np-> ...
418*65120Smckusick					/* Reverse traversal. */
419*65120Smckusickfor (np = head.cqh_last; np != (void *)&head; np = np->entries.cqe_prev)
420*65120Smckusick	np-> ...
421*65120Smckusick					/* Delete. */
422*65120Smckusickwhile (head.cqh_first != (void *)&head)
423*65120Smckusick	CIRCLEQ_REMOVE(&head, head.cqh_first, entries);
424*65120Smckusick.Ed
425*65120Smckusick.Sh HISTORY
426*65120SmckusickThe
427*65120Smckusick.Nm queue
428*65120Smckusickfunctions first appeared in 4.4BSD.
429