DESCRIPTION

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

1

Insertion of a new entry at the head of the list.

2

Insertion of a new entry before or after any element in the
list.

3

Removal of any entry in the list.

4

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 implementing
a LIFO queue.

Simple queues add the following functionality:

1

Entries can be added at the end of a list.

However:

1

Entries may not be added before any element in the list.

2

All list insertions and removals must specify the head of the
list.

3

Each head entry requires two pointers rather than one.

Simple queues are ideal for applications with large datasets and few or
no removals, or for implementing a FIFO queue.

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:

1

Entries can be added at the end of a list.

However:

1

All list insertions and removals, except insertion before
another element, must specify the head of the list.

2

Each head entry requires two pointers rather than one.

3

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.

In the macro definitions, TYPE is the name of a user defined structure,
that must contain a field of type GG_LIST_ENTRY, GG_SIMPLEQ_ENTRY,
GG_SLIST_ENTRY, GG_TAILQ_ENTRY, or GG_CIRCLEQ_ENTRY, named NAME. The
argument HEADNAME is the name of a user defined structure that must be
declared using the macros GG_LIST_HEAD, GG_SIMPLEQ_HEAD, GG_SLIST_HEAD,
GG_TAILQ_HEAD, or GG_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 arbitrary elements.
New elements can be added to the list after an existing element
or at the head of the list. An GG_SLIST_HEAD structure is declared as follows:

GG_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 GG_SLIST_HEAD_INITIALIZER evaluates to an initializer for the list
head.

The macro GG_SLIST_EMPTY evaluates to true if there are no elements in the list.

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

The macro GG_SLIST_FIRST returns the first element in the list or NULL if
the list is empty.

The macro GG_SLIST_FOREACH traverses the list referenced by head in the forward
direction, assigning each element in turn to var.

The macro GG_SLIST_INIT initializes the list referenced by head.

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

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

The macro GG_SLIST_NEXT returns the next element in the list.

The macro GG_SLIST_REMOVE removes the element elm from the list.

The macro GG_SLIST_REMOVE_HEAD removes the first element 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
GG_SLIST_REMOVE macro.

SIMPLE QUEUES

A simple queue is headed by a structure defined by the GG_SIMPLEQ_HEAD
macro. This structure contains a pair of pointers, one to the first
element in the simple queue and the other to the last element in the simple
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 queue after an existing element,
at the head of the queue, or at the end of the queue. A GG_SIMPLEQ_HEAD
structure is declared as follows:

GG_SIMPLEQ_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 simple queue. A pointer
to the head of the simple queue can later be declared as:

struct HEADNAME *headp;

(The names head and headp are user selectable.)

The macro GG_SIMPLEQ_ENTRYk declares a structure that connects the elements
in the simple queue.

The macro GG_SIMPLEQ_HEAD_INITIALIZER provides a value which can be used to
initialize a simple queue head at compile time, and is used at the point
that the simple queue head variable is declared, like:

struct HEADNAME head = GG_SIMPLEQ_HEAD_INITIALIZER(head);

The macro GG_SIMPLEQ_INIT initializes the simple queue referenced by head.

The macro GG_SIMPLEQ_INSERT_HEAD inserts the new element elm at the head of
the simple queue.

The macro GG_SIMPLEQ_INSERT_TAIL inserts the new element elm at the end of
the simple queue.

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

The macro GG_SIMPLEQ_REMOVE removes elm from the simple queue.

The macro GG_SIMPLEQ_REMOVE_HEAD removes the first element from the head of
the simple queue. For optimum efficiency, elements being removed from
the head of the queue should explicitly use this macro instead of the
generic GG_SIMPLQ_REMOVE macro.

The macro GG_SIMPLEQ_EMPTY return true if the simple queue head has no elements.

The macro GG_SIMPLEQ_FIRST returns the first element of the simple queue
head.

The macro GG_SIMPLEQ_FOREACH traverses the tail queue referenced by head in
the forward direction, assigning each element in turn to var.

LISTS

A list is headed by a structure defined by the GG_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
removed 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:

GG_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 GG_LIST_ENTRY declares a structure that connects the elements in
the list.

The macro GG_LIST_HEAD_INITIALIZER provides a value which can be used to
initialize a list head at compile time, and is used at the point that the
list head variable is declared, like:

struct HEADNAME head = GG_LIST_HEAD_INITIALIZER(head);

The macro GG_LIST_INIT initializes the list referenced by head.

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

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

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

The macro GG_LIST_REMOVE removes the element elm from the list.

The macro GG_LIST_EMPTY return true if the list head has no elements.

The macro GG_LIST_FIRST returns the first element of the list head.

The macro GG_LIST_FOREACH traverses the list referenced by head in the forward
direction, assigning each element in turn to var.

TAIL QUEUES

A tail queue is headed by a structure defined by the GG_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
queue after an existing element, before an existing element, at the head
of the queue, or at the end the queue. A GG_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 GG_TAILQ_ENTRY declares a structure that connects the elements in
the tail queue.

The macro GG_TAILQ_HEAD_INITIALIZER provides a value which can be used to
initialize a tail queue head at compile time, and is used at the point
that the tail queue head variable is declared, like:

struct HEADNAME head = GG_TAILQ_HEAD_INITIALIZER(head);

The macro GG_TAILQ_INIT initializes the tail queue referenced by head.

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

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

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

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

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

The macro GG_TAILQ_EMPTY return true if the tail queue head has no elements.

The macro GG_TAILQ_FIRST returns the first element of the tail queue head.

The macro GG_TAILQ_FOREACH traverses the tail queue referenced by head in
the forward direction, assigning each element in turn to var.

The macro GG_TAILQ_FOREACH_REVERSE traverses the tail queue referenced by
head in the reverse direction, assigning each element in turn to var.

CIRCULAR QUEUES

A circular queue is headed by a structure defined by the GG_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 element, at
the head of the queue, or at the end of the queue. A GG_CIRCLEQ_HEAD
structure is declared as follows:

GG_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 GG_CIRCLEQ_ENTRY declares a structure that connects the elements
in the circular queue.

The macro GG_CIRCLEQ_HEAD_INITIALIZER provides a value which can be used to
initialize a circular queue head at compile time, and is used at the
point that the circular queue head variable is declared, like:

struct HEADNAME head = GG_CIRCLEQ_HEAD_INITIALIZER(head);

The macro GG_CIRCLEQ_INIT initializes the circular queue referenced by head.

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

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

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

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

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

The macro GG_CIRCLEQ_EMPTY return true if the circular queue head has no
elements.

The macro GG_CIRCLEQ_FIRST returns the first element of the circular queue
head.

The macro GG_CICRLEQ_FOREACH traverses the circle queue referenced by head
in the forward direction, assigning each element in turn to var.

The macro GG_CICRLEQ_FOREACH_REVERSE traverses the circle queue referenced
by head in the reverse direction, assigning each element in turn to var.

The macro GG_CIRCLEQ_LAST returns the last element of the circular queue
head.