QUEUE man page on BSDi

Man page or keyword search:  
man Server   6284 pages
apropos Keyword Search (all sections)
Output format
BSDi logo
[printable version]

QUEUE(3)		    BSD Programmer's Manual		      QUEUE(3)

NAME
     queue - implementations of simple list queues, tail queues, and circular
     queues

DESCRIPTION
     These macros define and operate on three types of data structures: lists,
     tail queues, and circular queues.	All three structures support the fol-
     lowing functionality:
	   1.	Insertion of a new entry at the head of the list.
	   2.	Insertion of a new entry after any element in the list.
	   3.	Insertion of a new entry before any element in the list.
	   4.	Removal of any entry in the list.
	   5.	Forward traversal through the list.

     Lists are the simplest of the three data structures and support only the
     above functionality.

     Tail queues add the following functionality:
	   1.	Entries can be added at the end of a list.
	   2.	They may be traversed backwards, from tail to head.

     However:
	   1.	All list insertions and removals must specify the head of the
		list.
	   2.	Each head entry requires two pointers rather than one.
	   3.	Reverse traversal is 50% slower than circular queues.
	   4.	Code size is about 15% greater and operations run about 20%
		slower than lists.

     Circular queues add the following functionality:
	   1.	Entries can be added at the end of a list.
	   2.	They may be traversed backwards, from tail to head.

     However:
	   1.	All list insertions and removals must specify the head of the
		list.
	   2.	Each head entry requires two pointers rather than one.
	   3.	The termination condition for traversal is more complex.
	   4.	Code size is about 40% greater and operations run about 45%
		slower than lists.

LIST SYNOPSIS
     #include <stddef.h>
     #include <sys/queue.h>

     LIST_END(LIST_HEAD *headp);
     LIST_ENTRY(TYPE);
     LIST_FIRST(LIST_HEAD *headp);
     LIST_HEAD(HEADNAME, TYPE);
     LIST_INIT(LIST_HEAD *headp);
     LIST_INSERT_AFTER(LIST_ENTRY *listelm, TYPE *elm, LIST_ENTRY NAME);
     LIST_INSERT_BEFORE(LIST_ENTRY *listelm, TYPE *elm, LIST_ENTRY NAME);
     LIST_INSERT_HEAD(LIST_HEAD *headp, TYPE *elm, LIST_ENTRY NAME);
     LIST_NEXT(TYPE *elm, LIST_ENTRY NAME);
     LIST_REMOVE(TYPE *elm, LIST_ENTRY NAME);

     A list is headed by a structure defined by the LIST_HEAD macro.  This
     structure contains a single pointer to the first element on the list.
     The elements are doubly linked so that an arbitrary element can be re-
     moved without traversing the list.	 New elements can be added to the list
     after or before an existing element or at the head of the list.  A
     LIST_HEAD structure is declared as follows:

	   LIST_HEAD(HEADNAME, TYPE);

     where HEADNAME is the name of the structure to be defined, and TYPE is
     the type of the elements to be linked into the list.  A structure repre-
     senting the head of the list, or a pointer to the head of the list can
     later be declared as follows:

	   struct HEADNAME head;
	   struct HEADNAME *headp;

     where the names head and headp are user selectable.

     In the macro arguments, TYPE is the name of a user defined structure,
     that must contain a field named NAME, of type LIST_ENTRY.

     The LIST_END macro returns the pointer value indicating the end of the
     list.  Note that the parameter to the LIST_END macro that points to the
     head of the list is unused and may be specified as NULL. The LIST_END
     macro has a parameter only to keep it consistent with the CIRCLEQ_END
     macro.

     The LIST_ENTRY macro declares a structure that connects the elements in
     the list.

     The LIST_FIRST macro returns a pointer to the first element of the list.
     When this pointer is the same as the pointer returned by the LIST_END
     macro the list is empty.

     The LIST_INIT macro initializes the list referenced by headp.

     The LIST_INSERT_HEAD macro inserts the new element elm at the head of the
     list.

     The LIST_INSERT_AFTER macro inserts the new element elm after the element
     listelm.

     The LIST_INSERT_BEFORE macro inserts the new element elm before the ele-
     ment listelm.

     The LIST_NEXT macro returns a pointer to the next element in the list.
     Upon reaching the end of the list this pointer will be the same as the
     pointer returned by the LIST_END macro.

     The LIST_REMOVE macro removes the element elm from the list.

LIST EXAMPLE
     LIST_HEAD(listhead, entry) head;
     struct listhead *headp;			     /* List head. */
     struct entry {
	     ...
	     LIST_ENTRY(entry) entries;		     /* List. */
	     ...
     } *n1, *n2, *np;

     headp = &head;
     LIST_INIT(headp);				     /* Initialize the list. */

     n1 = malloc(sizeof(struct entry));		     /* Insert at head. */
     LIST_INSERT_HEAD(headp, n1, entries);

     n2 = malloc(sizeof(struct entry));		     /* Insert after element. */
     LIST_INSERT_AFTER(n1, n2, entries);

     n2 = malloc(sizeof(struct entry));		     /* Insert before element. */
     LIST_INSERT_BEFORE(n1, n2, entries);
						     /* Forward traversal. */
     for (np = LIST_FIRST(headp); np != LIST_END(NULL);
	 np = LIST_NEXT(np, entries))
	     np-> ...
     while (LIST_FIRST(headp) != LIST_END(NULL))     /* Empty list. */
	     LIST_REMOVE(LIST_FIRST(headp), entries);

TAIL QUEUE SYNOPSIS
     #include <stddef.h>
     #include <sys/queue.h>

     TAILQ_END(TAILQ_HEAD *headp);
     TAILQ_ENTRY(TYPE);
     TAILQ_FIRST(TAILQ_HEAD *headp);
     TAILQ_HEAD(HEADNAME, TYPE);
     TAILQ_INIT(TAILQ_HEAD *headp);
     TAILQ_INSERT_AFTER(TAILQ_HEAD *headp, TYPE *listelm, TYPE *elm, TAILQ_EN-
     TRY NAME);
     TAILQ_INSERT_BEFORE(TAILQ_HEAD *headp, TYPE *listelm, TYPE *elm,
     TAILQ_ENTRY NAME);
     TAILQ_INSERT_HEAD(TAILQ_HEAD *headp, TYPE *elm, TAILQ_ENTRY NAME);
     TAILQ_INSERT_TAIL(TAILQ_HEAD *headp, TYPE *elm, TAILQ_ENTRY NAME);
     TAILQ_NEXT(TYPE *elm, TAILQ_ENTRY NAME);
     TAILQ_PREV(TYPE *elm, HEADNAME, TAILQ_ENTRY NAME);
     TAILQ_LAST(TAILQ_HEAD *headp, HEADNAME);
     TAILQ_REMOVE(TAILQ_HEAD *headp, TYPE *elm, TAILQ_ENTRY NAME);

     A tail queue is headed by a structure defined by the TAILQ_HEAD macro.
     This structure contains a pair of pointers, one to the first element in
     the tail queue and the other to the last element in the tail queue.  The
     elements are doubly linked so that an arbitrary element can be removed
     without traversing the tail queue.	 New elements can be added to the tail
     queue after an existing element, before an existing element, at the head
     of the tail queue, or at the end of the tail queue.  A TAILQ_HEAD struc-
     ture is declared as follows:

	   TAILQ_HEAD(HEADNAME, TYPE);

     where HEADNAME is the name of the structure to be defined, and TYPE is
     the type of the elements to be linked into the list.  A structure repre-
     senting the head of the list, or a pointer to the head of the list can
     later be declared as follows:

	   struct HEADNAME head;
	   struct HEADNAME *headp;

     where the names head and headp are user selectable.

     In the macro arguments, TYPE is the name of a user defined structure,
     that must contain a field named NAME, of type TAILQ_ENTRY.

     The TAILQ_END macro returns the pointer value indicating the end of the
     tail queue.  Note that the parameter to the TAILQ_END macro that points
     to the head of the list is unused and may be specified as NULL. The
     TAILQ_END macro has a parameter only to keep it consistent with the
     CIRCLEQ_END macro.

     The TAILQ_ENTRY macro declares a structure that connects the elements in
     the tail queue.

     The TAILQ_FIRST macro returns a pointer to the first element of the tail
     queue.  When this pointer is the same as the pointer returned by the
     TAILQ_END macro the tail queue is empty.

     The TAILQ_INIT macro initializes the tail queue referenced by headp.

     The TAILQ_INSERT_AFTER macro inserts the new element elm after the ele-
     ment listelm.

     The TAILQ_INSERT_BEFORE macro inserts the new element elm before the ele-
     ment listelm.

     The TAILQ_INSERT_HEAD macro inserts the new element elm at the head of
     the tail queue.

     The TAILQ_INSERT_TAIL macro inserts the new element elm at the end of the
     tail queue.

     The TAILQ_LAST macro returns a pointer to the last element of the tail
     queue.  When this pointer is the same as the pointer returned by the
     TAILQ_END macro the tail queue is empty.

     The TAILQ_NEXT macro returns a pointer to the next element in the tail
     queue.  Upon reaching the end of the tail queue this pointer will be the
     same as the pointer returned by the TAILQ_END macro.

     The TAILQ_PREV macro returns a pointer to the previous element in the
     tail queue.  Upon reaching the end of the tail queue this pointer will be
     the same as the pointer returned by the TAILQ_END macro.

     The TAILQ_REMOVE macro removes the element elm from the tail queue.

TAIL QUEUE EXAMPLE
     TAILQ_HEAD(tailhead, entry) head;
     struct tailhead *headp;	     /* Tail queue head. */
     struct entry {
	     ...
	     TAILQ_ENTRY(entry) entries;     /* Tail queue. */
	     ...
     } *n1, *n2, *np;

     headp = &head;
     TAILQ_INIT(headp);			     /* Initialize the tailq. */

     n1 = malloc(sizeof(struct entry));	     /* Insert at head. */
     TAILQ_INSERT_HEAD(headp, n1, entries);

     n1 = malloc(sizeof(struct entry));	     /* Insert at tail. */
     TAILQ_INSERT_TAIL(headp, n1, entries);

     n2 = malloc(sizeof(struct entry));	     /* Insert after element. */
     TAILQ_INSERT_AFTER(headp, n1, n2, entries);

     n2 = malloc(sizeof(struct entry));	     /* Insert before element. */
     TAILQ_INSERT_BEFORE(headp, n1, n2, entries);
					     /* Forward traversal. */
     for (np = TAILQ_FIRST(headp); np != TAILQ_END(NULL);
	 np = TAILQ_NEXT(np, entries))
	     np-> ...
					     /* Reverse traversal. */
     for (np = TAILQ_LAST(headp, tailhead); np != TAILQ_END(NULL);
	 np = TAILQ_PREV(np, tailhead, entries))
	     np-> ...
					     /* Empty list. */
     while (TAILQ_FIRST(headp) != TAILQ_END(NULL))
	     TAILQ_REMOVE(headp, TAILQ_FIRST(headp), entries);

CIRCULAR QUEUE SYNOPSIS
     #include <stddef.h>
     #include <sys/queue.h>

     CIRCLEQ_END(CIRCLEQ_HEAD *headp);
     CIRCLEQ_ENTRY(TYPE);
     CIRCLEQ_FIRST(CIRCLEQ_HEAD *headp);
     CIRCLEQ_HEAD(HEADNAME, TYPE);
     CIRCLEQ_INIT(CIRCLEQ_HEAD *headp);
     CIRCLEQ_INSERT_AFTER(CIRCLEQ_HEAD *headp, TYPE *listelm, TYPE *elm, CIR-
     CLEQ_ENTRY NAME);
     CIRCLEQ_INSERT_BEFORE(CIRCLEQ_HEAD *headp, TYPE *listelm, TYPE *elm, CIR-
     CLEQ_ENTRY NAME);
     CIRCLEQ_INSERT_HEAD(CIRCLEQ_HEAD *headp, TYPE *elm, CIRCLEQ_ENTRY NAME);
     CIRCLEQ_INSERT_TAIL(CIRCLEQ_HEAD *headp, TYPE *elm, CIRCLEQ_ENTRY NAME);
     CIRCLEQ_NEXT(TYPE *elm, CIRCLEQ_ENTRY NAME);
     CIRCLEQ_PREV(TYPE *elm, CIRCLEQ_ENTRY NAME);
     CIRCLEQ_LAST(CIRCLEQ_HEAD *headp);
     CIRCLEQ_REMOVE(CIRCLEQ_HEAD *headp, TYPE *elm, CIRCLEQ_ENTRY NAME);

     A circular queue is headed by a structure defined by the CIRCLEQ_HEAD
     macro.  This structure contains a pair of pointers, one to the first ele-
     ment in the circular queue and the other to the last element in the cir-
     cular queue.  The elements are doubly linked so that an arbitrary element
     can be removed without traversing the queue.  New elements can be added
     to the queue after an existing element, before an existing element, at
     the head of the queue, or at the end of the queue.	 A CIRCLEQ_HEAD struc-
     ture is declared as follows:

	   CIRCLEQ_HEAD(HEADNAME, TYPE);

     where HEADNAME is the name of the structure to be defined, and TYPE is
     the type of the elements to be linked into the list.  A structure repre-
     senting the head of the list, or a pointer to the head of the list can
     later be declared as follows:

	   struct HEADNAME head;
	   struct HEADNAME *headp;

     where the names head and headp are user selectable.

     In the macro arguments, TYPE is the name of a user defined structure,
     that must contain a field named NAME, of type CIRCLEQ_ENTRY.

     The CIRCLEQ_END macro returns the pointer value indicating the end of the
     circle queue.

     The CIRCLEQ_ENTRY macro declares a structure that connects the elements
     in the circular queue.

     The CIRCLEQ_FIRST macro returns a pointer to the first element of the
     circle queue.  When this pointer is the same as the pointer returned by
     the CIRCLEQ_END macro the circle queue is empty.

     The CIRCLEQ_INIT macro initializes the circular queue referenced by
     headp.

     The CIRCLEQ_INSERT_AFTER macro inserts the new element elm after the ele-
     ment listelm.

     The CIRCLEQ_INSERT_BEFORE macro inserts the new element elm before the
     element listelm.

     The CIRCLEQ_INSERT_HEAD macro inserts the new element elm at the head of
     the circular queue.

     The CIRCLEQ_INSERT_TAIL macro inserts the new element elm at the end of
     the circular queue.

     The CIRCLEQ_LAST macro returns a pointer to the last element of the cir-
     cle queue.	 When this pointer is the same as the pointer returned by the
     CIRCLEQ_END macro the circle queue is empty.

     The CIRCLEQ_NEXT macro returns a pointer to the next element in the cir-
     cle queue.	 Upon reaching the end of the circle queue this pointer will
     be the same as the pointer returned by the CIRCLEQ_END macro.

     The CIRCLEQ_PREV macro returns a pointer to the previous element in the
     circle queue.  Upon reaching the end of the circle queue this pointer
     will be the same as the pointer returned by the CIRCLEQ_END macro.

     The CIRCLEQ_REMOVE macro removes the element elm from the circular queue.

CIRCULAR QUEUE EXAMPLE
     CIRCLEQ_HEAD(circleq, entry) head;
     struct circleq *headp;		     /* Circular queue head. */
     struct entry {
	     ...
	     CIRCLEQ_ENTRY entries;	     /* Circular queue. */
	     ...
     } *n1, *n2, *np;

     headp = &head;
     CIRCLEQ_INIT(headp);		     /* Initialize the circleq. */

     n1 = malloc(sizeof(struct entry));	     /* Insert at head. */
     CIRCLEQ_INSERT_HEAD(headp, n1, entries);

     n1 = malloc(sizeof(struct entry));	     /* Insert at tail. */
     CIRCLEQ_INSERT_TAIL(headp, n1, entries);

     n2 = malloc(sizeof(struct entry));	     /* Insert after element. */
     CIRCLEQ_INSERT_AFTER(headp, n1, n2, entries);

     n2 = malloc(sizeof(struct entry));	     /* Insert before element. */
     CIRCLEQ_INSERT_BEFORE(headp, n1, n2, entries);
					     /* Forward traversal. */
     for (np = CIRCLEQ_FIRST(headp); np != CIRCLEQ_END(headp);
	 np = CIRCLEQ_NEXT(np, entries))
	     np-> ...
					     /* Reverse traversal. */
     for (np = CIRCLEQ_LAST(headp); np != CIRCLEQ_END(headp);
	 np = CIRCLEQ_PREV(np, entries))
	     np-> ...
					     /* Empty list. */
     while (CIRCLEQ_FIRST(headp) != CIRCLEQ_END(headp))
	     CIRCLEQ_REMOVE(headp, CIRCLEQ_FIRST(headp), entries);

HISTORY
     The queue functions first appeared in 4.4BSD.

4th Berkeley Distribution	August 20, 1994				     6
[top]

List of man pages available for BSDi

Copyright (c) for man pages and the logo by the respective OS vendor.

For those who want to learn more, the polarhome community provides shell access and support.

[legal] [privacy] [GNU] [policy] [cookies] [netiquette] [sponsors] [FAQ]
Tweet
Polarhome, production since 1999.
Member of Polarhome portal.
Based on Fawad Halim's script.
....................................................................
Vote for polarhome
Free Shell Accounts :: the biggest list on the net