QUEUE(3)                       Library Routines                       QUEUE(3)




NAME

       SLIST_ENTRY,   SLIST_HEAD,  SLIST_INIT,  SLIST_INSERT_AFTER,  SLIST_IN
       SERT_HEAD, SLIST_REMOVE_HEAD, SLIST_REMOVE, STAILQ_ENTRY,  STAILQ_HEAD,
       STAILQ_INIT,    STAILQ_INSERT_AFTER,   STAILQ_INSERT_HEAD,   STAILQ_IN
       SERT_TAIL, STAILQ_REMOVE_HEAD,  STAILQ_REMOVE,  LIST_ENTRY,  LIST_HEAD,
       LIST_INIT,   LIST_INSERT_AFTER,  LIST_INSERT_BEFORE,  LIST_INSERT_HEAD,
       LIST_REMOVE, TAILQ_ENTRY, TAILQ_HEAD,  TAILQ_INIT,  TAILQ_INSERT_AFTER,
       TAILQ_INSERT_BEFORE,  TAILQ_INSERT_HEAD,  TAILQ_INSERT_TAIL,  TAILQ_RE
       MOVE, CIRCLEQ_ENTRY, CIRCLEQ_HEAD, CIRCLEQ_INIT,  CIRCLEQ_INSERT_AFTER,
       CIRCLEQ_INSERT_BEFORE,  CIRCLEQ_INSERT_HEAD,  CIRCLEQ_INSERT_TAIL, CIR
       CLEQ_REMOVE - implementations  of  singly-linked  lists,  singly-linked
       tail queues, lists, tail queues, and circular queues


SYNOPSIS

       #include <sys/queue.h>

       SLIST_ENTRY (TYPE);
       SLIST_HEAD (HEADNAME, TYPE);
       SLIST_INIT (SLIST_HEAD *head);
       SLIST_INSERT_AFTER (TYPE *listelm, TYPE *elm, SLIST_ENTRY NAME);
       SLIST_INSERT_HEAD (SLIST_HEAD *head, TYPE *elm, SLIST_ENTRY NAME);
       SLIST_REMOVE_HEAD (SLIST_HEAD *head, SLIST_ENTRY NAME);
       SLIST_REMOVE (SLIST_HEAD *head, TYPE *elm, TYPE, SLIST_ENTRY NAME);

       STAILQ_ENTRY (TYPE);
       STAILQ_HEAD (HEADNAME, TYPE);
       STAILQ_INIT (STAILQ_HEAD *head);
       STAILQ_INSERT_AFTER  (STAILQ_HEAD  *head,  TYPE  *listelm,  TYPE  *elm,
       STAILQ_ENTRY NAME);
       STAILQ_INSERT_HEAD (STAILQ_HEAD *head, TYPE *elm, STAILQ_ENTRY NAME);
       STAILQ_INSERT_TAIL (STAILQ_HEAD *head, TYPE *elm, STAILQ_ENTRY NAME);
       STAILQ_REMOVE_HEAD (STAILQ_HEAD *head, TYPE *elm, STAILQ_ENTRY NAME);
       STAILQ_REMOVE (STAILQ_HEAD *head, TYPE *elm, TYPE, STAILQ_ENTRY NAME);

       LIST_ENTRY (TYPE);
       LIST_HEAD (HEADNAME, TYPE);
       LIST_INIT (LIST_HEAD *head);
       LIST_INSERT_AFTER (TYPE *listelm, TYPE *elm, LIST_ENTRY NAME);
       LIST_INSERT_BEFORE (TYPE *listelm, TYPE *elm, LIST_ENTRY NAME);
       LIST_INSERT_HEAD (LIST_HEAD *head, TYPE *elm, LIST_ENTRY NAME);
       LIST_REMOVE (TYPE *elm, LIST_ENTRY NAME);

       TAILQ_ENTRY (TYPE);
       TAILQ_HEAD (HEADNAME, TYPE);
       TAILQ_INIT (TAILQ_HEAD *head);
       TAILQ_INSERT_AFTER  (TAILQ_HEAD  *head,  TYPE  *listelm,   TYPE   *elm,
       TAILQ_ENTRY NAME);
       TAILQ_INSERT_BEFORE (TYPE *listelm, TYPE *elm, TAILQ_ENTRY NAME);
       TAILQ_INSERT_HEAD (TAILQ_HEAD *head, TYPE *elm, TAILQ_ENTRY NAME);
       TAILQ_INSERT_TAIL (TAILQ_HEAD *head, TYPE *elm, TAILQ_ENTRY NAME);
       TAILQ_REMOVE (TAILQ_HEAD *head, TYPE *elm, TAILQ_ENTRY NAME);

       CIRCLEQ_ENTRY (TYPE);
       CIRCLEQ_HEAD (HEADNAME, TYPE);
       CIRCLEQ_INIT (CIRCLEQ_HEAD *head);
       CIRCLEQ_INSERT_AFTER  (CIRCLEQ_HEAD  *head,  TYPE  *listelm, TYPE *elm,
       CIRCLEQ_ENTRY NAME);
       CIRCLEQ_INSERT_BEFORE (CIRCLEQ_HEAD *head, TYPE  *listelm,  TYPE  *elm,
       CIRCLEQ_ENTRY NAME);
       CIRCLEQ_INSERT_HEAD   (CIRCLEQ_HEAD  *head,  TYPE  *elm,  CIRCLEQ_ENTRY
       NAME);
       CIRCLEQ_INSERT_TAIL  (CIRCLEQ_HEAD  *head,  TYPE  *elm,   CIRCLEQ_ENTRY
       NAME);
       CIRCLEQ_REMOVE (CIRCLEQ_HEAD *head, TYPE *elm, CIRCLEQ_ENTRY NAME);


DESCRIPTION

       These  macros  define  and  operate  on  five types of data structures:
       singly-linked lists, singly-linked tail queues, lists, tail queues, and
       circular queues.  All five structures support the following functional‐
       ity:

              Insertion of a new entry at the head of the list.
              Insertion of a new entry after any element in the list.
              O(1) removal of an entry from the head of the list.
              O(n) removal of any entry in the list.
              Forward traversal through the list.

       Singly-linked lists are the simplest of the five  data  structures  and
       support  only  the  above functionality.  Singly-linked lists are ideal
       for applications with large datasets and few or no removals, or for im‐
       plementing a LIFO queue.

       Singly-linked tail queues add the following functionality:

              Entries can be added at the end of a list.

       However:
              All list insertions must specify the head of the list.
              Each head entry requires two pointers rather than one.
              Code  size  is  about  15%  greater and operations run about 20%
              slower than singly-linked lists.

       Singly-linked tailqs are ideal for applications with large datasets and
       few or no removals, or for implementing a FIFO queue.

       All  doubly  linked  types  of data structures (lists, tail queues, and
       circle queues) additionally allow:

              Insertion of a new entry before any element in the list.
              O(1) removal of any entry in the list.
       However:
              Each elements requires two pointers rather than one.
              Code size and execution time of operations (except for  removal)
              is about twice that of the singly-linked data-structures.

       Linked  lists are the simplest of the doubly linked data structures and
       support only the above functionality over singly-linked lists.

       Tail queues add the following functionality:

              Entries can be added at the end of a list.
       However:
              All list insertions and removals must specify the  head  of  the
              list.
              Each head entry requires two pointers rather than one.
              Code  size  is  about  15%  greater and operations run about 20%
              slower than singly-linked lists.

       Circular queues add the following functionality:

              Entries can be added at the end of a list.
              They may be traversed backwards, from tail to head.
       However:
              All list insertions and removals must specify the  head  of  the
              list.
              Each head entry requires two pointers rather than one.
              The termination condition for traversal is more complex.
              Code  size  is  about  40%  greater and operations run about 45%
              slower than lists.

       In the macro definitions, TYPE is the name of a user defined structure,
       that  must  contain a field of type SLIST_ENTRY, STAILQ_ENTRY, LIST_EN
       TRY, TAILQ_ENTRY, or CIRCLEQ_ENTRY, named NAME The argument HEADNAME is
       the  name  of  a user defined structure that must be declared using the
       macros SLIST_HEAD, STAILQ_HEAD, LIST_HEAD, TAILQ_HEAD, or CIRCLEQ_HEAD.
       See  the examples below for further explanation of how these macros are
       used.


SINGLY-LINKED LISTS

       A singly-linked list is headed by a structure defined by the SLIST_HEAD
       macro.   This  structure contains a single pointer to the first element
       on the list.  The elements are singly  linked  for  minimum  space  and
       pointer  manipulation overhead at the expense of O(n) removal for arbi‐
       trary elements.  New elements can be added to the list after an  exist‐
       ing element or at the head of the list.  An SLIST_HEAD structure is de‐
       clared as follows:

              SLIST_HEAD(HEADNAME, TYPE) head;

       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 pointer to the
       head of the list can later be declared as:

              struct HEADNAME *headp;

       (The names head and headp are user selectable.)

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

       The macro SLIST_INIT initializes the list referenced by head.

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

       The macro SLIST_INSERT_AFTER inserts the new element elm after the ele‐
       ment listelm.

       The  macro  SLIST_REMOVE_HEAD  removes the element elm from the head of
       the list.  For optimum efficiency, elements being removed from the head
       of  the  list  should  explicitly use this macro instead of the generic
       SLIST_REMOVE macro.

       The macro SLIST_REMOVE removes the element elm from the list.


SINGLY-LINKED LIST EXAMPLE

       SLIST_HEAD(slisthead, entry) head;
       struct slisthead *headp;      /* Singly-linked List head. */
       struct entry {
            ...
            SLIST_ENTRY(entry) entries;   /* Singly-linked List. */
            ...
       } *n1, *n2, *n3, *np;

       SLIST_INIT(&head);            /* Initialize the list. */

       n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
       SLIST_INSERT_HEAD(&head, n1, entries);

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

       SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
       free(n2);

       n3 = head.slh_first;
       SLIST_REMOVE_HEAD(&head, entries); /* Deletion. */
       free(n3);

                                /* Forward traversal. */
       for (np = head.slh_first; np != NULL; np = np->entries.sle_next)
            np-> ...

       while (head.slh_first != NULL) {   /* List Deletion. */
            n1 = head.slh_first;
            SLIST_REMOVE_HEAD(&head, entries);
            free(n1);
       }


SINGLY-LINKED TAIL QUEUES

       A singly-linked tail queue is headed by  a  structure  defined  by  the
       STAILQ_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 singly linked for minimum space
       and pointer manipulation overhead at the expense of  O(n)  removal  for
       arbitrary  elements.  New elements can be added to the tail queue after
       an existing element, at the head of the tail queue, or at  the  end  of
       the tail queue.  A STAILQ_HEAD structure is declared as follows:

              STAILQ_HEAD(HEADNAME, TYPE) head;

       where  HEADNAME is the name of the structure to be defined, and TYPE is
       the type of the elements to be linked into the tail queue.   A  pointer
       to the head of the tail queue can later be declared as:

              struct HEADNAME *headp;

       (The names head and headp are user selectable.)

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

       The macro STAILQ_INIT initializes the tail queue referenced by head.

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

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

       The macro STAILQ_INSERT_AFTER inserts the new element elm after the el‐
       ement listelm.

       The  macro  STAILQ_REMOVE_HEAD removes the element elm from the head of
       the tail queue.  For optimum efficiency, elements  being  removed  from
       the head of the tail queue should use this macro explicitly rather than
       the generic STAILQ_REMOVE macro.

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


SINGLY-LINKED TAIL QUEUE EXAMPLE

       STAILQ_HEAD(stailhead, entry) head;
       struct stailhead *headp;      /* Singly-linked tail queue head. */
       struct entry {
            ...
            STAILQ_ENTRY(entry) entries;  /* Tail queue. */
            ...
       } *n1, *n2, *n3, *np;

       STAILQ_INIT(&head);           /* Initialize the queue. */

       n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
       STAILQ_INSERT_HEAD(&head, n1, entries);

       n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
       STAILQ_INSERT_TAIL(&head, n1, entries);

       n2 = malloc(sizeof(struct entry)); /* Insert after. */
       STAILQ_INSERT_AFTER(&head, n1, n2, entries);

                                /* Deletion. */
       STAILQ_REMOVE(&head, n2, entry, entries);
       free(n2);

                                /* Deletion from the head */
       n3 = head.stqh_first;
       STAILQ_REMOVE_HEAD(&head, entries);
       free(n3);

                                /* Forward traversal. */
       for (np = head.stqh_first; np != NULL; np = np->entries.stqe_next)
            np-> ...
                                /* TailQ Deletion. */
       while (head.stqh_first != NULL) {
            n1 = head.stqh_first;
            TAILQ_REMOVE_HEAD(&head, entries);
            free(n1);
       }
                                /* Faster TailQ Deletion. */
       n1 = head.stqh_first;
       while (n1 != NULL) {
            n2 = n1->entries.stqe_next;
            free(n1);
            n1 = n2;
       }
       STAILQ_INIT(&head);


LISTS

       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 an existing element, before an existing element, or  at  the
       head of the list.  A LIST_HEAD structure is declared as follows:

              LIST_HEAD(HEADNAME, TYPE) head;

       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 pointer to  the
       head of the list can later be declared as:

              struct HEADNAME *headp;

       (The names head and headp are user selectable.)

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

       The macro LIST_INIT initializes the list referenced by head.

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

       The  macro LIST_INSERT_AFTER inserts the new element elm after the ele‐
       ment listelm.

       The macro LIST_INSERT_BEFORE inserts the new element elm before the el‐
       ement listelm.

       The macro LIST_REMOVE 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, *n3, *np;

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

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

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

       n3 = malloc(sizeof(struct entry)); /* Insert before. */
       LIST_INSERT_BEFORE(n2, n3, entries);

       LIST_REMOVE(n2, entries);          /* Deletion. */
       free(n2);

                                /* Forward traversal. */
       for (np = head.lh_first; np != NULL; np = np->entries.le_next)
            np-> ...

       while (head.lh_first != NULL) {         /* List Deletion. */
            n1 = head.lh_first;
            LIST_REMOVE(n1, entries);
            free(n1);
       }

       n1 = head.lh_first;           /* Faster List Delete. */
       while (n1 != NULL) {
            n2 = n1->entires.le_next;
            free(n1);
            n1 = n2;
       }
       LIST_INIT(&head);


TAIL QUEUES

       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  re‐
       moved  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 structure is declared as follows:

              TAILQ_HEAD(HEADNAME, TYPE) head;

       where HEADNAME is the name of the structure to be defined, and TYPE  is
       the  type  of the elements to be linked into the tail queue.  A pointer
       to the head of the tail queue can later be declared as:

              struct HEADNAME *headp;

       (The names head and headp are user selectable.)

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

       The macro TAILQ_INIT initializes the tail queue referenced by head.

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

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

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

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

       The macro TAILQ_REMOVE 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, *n3, *np;

       TAILQ_INIT(&head);            /* Initialize the queue. */

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

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

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

       n3 = malloc(sizeof(struct entry)); /* Insert before. */
       TAILQ_INSERT_BEFORE(n2, n3, entries);

       TAILQ_REMOVE(&head, n2, entries);  /* Deletion. */
       free(n2);
                                /* Forward traversal. */
       for (np = head.tqh_first; np != NULL; np = np->entries.tqe_next)
            np-> ...
                                /* TailQ Deletion. */
       while (head.tqh_first != NULL) {
            n1 = head.tqh_first;
            TAILQ_REMOVE(&head, head.tqh_first, entries);
            free(n1);
       }
                                /* Faster TailQ Deletion. */
       n1 = head.tqh_first;
       while (n1 != NULL) {
            n2 = n1->entries.tqe_next;
            free(n1);
            n1 = n2;
       }
       TAILQ_INIT(&head);


CIRCULAR QUEUES

       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
       element  in the circular queue and the other to the last element in the
       circular 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 el‐
       ement,  at  the  head of the queue, or at the end of the queue.  A CIR
       CLEQ_HEAD structure is declared as follows:

              CIRCLEQ_HEAD(HEADNAME, TYPE) head;

       where HEADNAME is the name of the structure to be defined, and TYPE  is
       the  type  of  the  elements  to  be linked into the circular queue.  A
       pointer to the head of the circular queue can later be declared as:

              struct HEADNAME *headp;

       (The names head and headp are user selectable.)

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

       The  macro  CIRCLEQ_INIT  initializes  the circular queue referenced by
       head.

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

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

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

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

       The macro CIRCLEQ_REMOVE 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;

       CIRCLEQ_INIT(&head);               /* Initialize the circular queue. */

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

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

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

       n2 = malloc(sizeof(struct entry)); /* Insert before. */
       CIRCLEQ_INSERT_BEFORE(&head, n1, n2, entries);

       CIRCLEQ_REMOVE(&head, n1, entries);     /* Deletion. */
       free(n1);
                                /* Forward traversal. */
       for (np = head.cqh_first; np != (void *)&head; np = np->entries.cqe_next)
            np-> ...
                                /* Reverse traversal. */
       for (np = head.cqh_last; np != (void *)&head; np = np->entries.cqe_prev)
            np-> ...
                                /* CircleQ Deletion. */
       while (head.cqh_first != (void *)&head) {
            n1 = head.cqh_first;
            CIRCLEQ_REMOVE(&head, head.cqh_first, entries);
            free(n1);
       }
                                /* Faster CircleQ Deletion. */
       n1 = head.cqh_first;
       while (n1 != (void *)&head) {
            n2 = n1->entries.cqh_next;
            free(n1);
            n1 = n2;
       }
       CIRCLEQ_INIT(&head);


HISTORY

       The queue functions first appeared in 4.4BSD.



GNO                               11 May 1997                         QUEUE(3)

Man(1) output converted with man2html