torcs - 1.2.0

Main Page   Modules   Compound List   File List   Compound Members   File Members   Related Pages  

torcs/src/libs/tgf/queue.h

00001 /*      $OpenBSD: queue.h,v 1.3 2001/01/26 11:34:05 niklas Exp $        */
00002 /*      $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $       */
00003 
00004 /*
00005  * Copyright (c) 1991, 1993
00006  *      The Regents of the University of California.  All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. All advertising materials mentioning features or use of this software
00017  *    must display the following acknowledgement:
00018  *      This product includes software developed by the University of
00019  *      California, Berkeley and its contributors.
00020  * 4. Neither the name of the University nor the names of its contributors
00021  *    may be used to endorse or promote products derived from this software
00022  *    without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00025  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00027  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00030  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00031  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00033  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00034  * SUCH DAMAGE.
00035  *
00036  *      @(#)queue.h     8.5 (Berkeley) 8/20/94
00037  */
00038 
00039 #ifndef _SYS_QUEUE_H_
00040 #define _SYS_QUEUE_H_
00041 
00042 #ifndef NULL
00043 #define NULL ((void *)0)
00044 #endif
00045 
00046 /*
00047  * This file defines four types of data structures: lists, simple queues,
00048  * tail queues, and circular queues.
00049  *
00050  * A list is headed by a single forward pointer (or an array of forward
00051  * pointers for a hash table header). The elements are doubly linked
00052  * so that an arbitrary element can be removed without a need to
00053  * traverse the list. New elements can be added to the list before
00054  * or after an existing element or at the head of the list. A list
00055  * may only be traversed in the forward direction.
00056  *
00057  * A simple queue is headed by a pair of pointers, one the head of the
00058  * list and the other to the tail of the list. The elements are singly
00059  * linked to save space, so only elements can only be removed from the
00060  * head of the list. New elements can be added to the list before or after
00061  * an existing element, at the head of the list, or at the end of the
00062  * list. A simple queue may only be traversed in the forward direction.
00063  *
00064  * A tail queue is headed by a pair of pointers, one to the head of the
00065  * list and the other to the tail of the list. The elements are doubly
00066  * linked so that an arbitrary element can be removed without a need to
00067  * traverse the list. New elements can be added to the list before or
00068  * after an existing element, at the head of the list, or at the end of
00069  * the list. A tail queue may be traversed in either direction.
00070  *
00071  * A circle queue is headed by a pair of pointers, one to the head of the
00072  * list and the other to the tail of the list. The elements are doubly
00073  * linked so that an arbitrary element can be removed without a need to
00074  * traverse the list. New elements can be added to the list before or after
00075  * an existing element, at the head of the list, or at the end of the list.
00076  * A circle queue may be traversed in either direction, but has a more
00077  * complex end of list detection.
00078  *
00079  * For details on the use of these macros, see the queue(3) manual page.
00080  */
00081 
00082 /*
00083  * List definitions.
00084  */
00085 #define LIST_HEAD(name, type)                                           \
00086 typedef struct name {                                                   \
00087         struct type *lh_first;  /* first element */                     \
00088 } t ## name
00089 
00090 #define LIST_ENTRY(type)                                                \
00091 struct {                                                                \
00092         struct type *le_next;   /* next element */                      \
00093         struct type **le_prev;  /* address of previous next element */  \
00094 }
00095 
00096 #define LIST_FIRST(head)                ((head)->lh_first)
00097 #define LIST_NEXT(elm, field)           ((elm)->field.le_next)
00098 #define LIST_END(head)                  NULL
00099 
00100 /*
00101  * List functions.
00102  */
00103 #define LIST_INIT(head) do {                                            \
00104         (head)->lh_first = NULL;                                        \
00105 } while (0)
00106 
00107 #define LIST_INIT_ENTRY(elm, field) do {        \
00108   (elm)->field.le_next = 0;                     \
00109   (elm)->field.le_prev = 0;                     \
00110 } while (0)
00111 
00112 #define LIST_INSERT_AFTER(listelm, elm, field) do {                     \
00113         if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)  \
00114                 (listelm)->field.le_next->field.le_prev =               \
00115                     &(elm)->field.le_next;                              \
00116         (listelm)->field.le_next = (elm);                               \
00117         (elm)->field.le_prev = &(listelm)->field.le_next;               \
00118 } while (0)
00119 
00120 #define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \
00121         (elm)->field.le_prev = (listelm)->field.le_prev;                \
00122         (elm)->field.le_next = (listelm);                               \
00123         *(listelm)->field.le_prev = (elm);                              \
00124         (listelm)->field.le_prev = &(elm)->field.le_next;               \
00125 } while (0)
00126 
00127 #define LIST_INSERT_HEAD(head, elm, field) do {                         \
00128         if (((elm)->field.le_next = (head)->lh_first) != NULL)          \
00129                 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
00130         (head)->lh_first = (elm);                                       \
00131         (elm)->field.le_prev = &(head)->lh_first;                       \
00132 } while (0)
00133 
00134 #ifdef USE_DEBUG
00135 #define LIST_REMOVE(elm, field) do {                            \
00136         if ((elm)->field.le_prev)                               \
00137           {                                                     \
00138             if ((elm)->field.le_next != NULL)                   \
00139                 (elm)->field.le_next->field.le_prev =           \
00140                     (elm)->field.le_prev;                       \
00141             *(elm)->field.le_prev = (elm)->field.le_next;       \
00142           }                                                     \
00143         (elm)->field.le_next = NULL;                            \
00144         (elm)->field.le_prev = NULL;                            \
00145 } while (0)
00146 #else /* USE_DEBUG */
00147 #define LIST_REMOVE(elm, field) do {                            \
00148         if ((elm)->field.le_prev)                               \
00149           {                                                     \
00150             if ((elm)->field.le_next != NULL)                   \
00151                 (elm)->field.le_next->field.le_prev =           \
00152                     (elm)->field.le_prev;                       \
00153             *(elm)->field.le_prev = (elm)->field.le_next;       \
00154           }                                                     \
00155 } while (0)
00156 #endif /* USE_DEBUG */
00157 
00158 /*
00159  * Simple queue definitions.
00160  */
00161 #define SIMPLEQ_HEAD(name, type)                                        \
00162 struct name {                                                           \
00163         struct type *sqh_first; /* first element */                     \
00164         struct type **sqh_last; /* addr of last next element */         \
00165 }
00166 
00167 #define SIMPLEQ_ENTRY(type)                                             \
00168 struct {                                                                \
00169         struct type *sqe_next;  /* next element */                      \
00170 }
00171 
00172 /*
00173  * Simple queue functions.
00174  */
00175 #define SIMPLEQ_INIT(head) do {                                         \
00176         (head)->sqh_first = NULL;                                       \
00177         (head)->sqh_last = &(head)->sqh_first;                          \
00178 } while (0)
00179 
00180 #define SIMPLEQ_INSERT_HEAD(head, elm, field) do {                      \
00181         if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)        \
00182                 (head)->sqh_last = &(elm)->field.sqe_next;              \
00183         (head)->sqh_first = (elm);                                      \
00184 } while (0)
00185 
00186 #define SIMPLEQ_INSERT_TAIL(head, elm, field) do {                      \
00187         (elm)->field.sqe_next = NULL;                                   \
00188         *(head)->sqh_last = (elm);                                      \
00189         (head)->sqh_last = &(elm)->field.sqe_next;                      \
00190 } while (0)
00191 
00192 #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {            \
00193         if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
00194                 (head)->sqh_last = &(elm)->field.sqe_next;              \
00195         (listelm)->field.sqe_next = (elm);                              \
00196 } while (0)
00197 
00198 #define SIMPLEQ_REMOVE_HEAD(head, elm, field) do {                      \
00199         if (((head)->sqh_first = (elm)->field.sqe_next) == NULL)        \
00200                 (head)->sqh_last = &(head)->sqh_first;                  \
00201 } while (0)
00202 
00203 /*
00204  * Tail queue definitions.
00205  */
00206 #define TAILQ_HEAD(name, type)                                          \
00207 typedef struct name {                                                   \
00208         struct type *tqh_first; /* first element */                     \
00209         struct type **tqh_last; /* addr of last next element */         \
00210 } t ## name
00211 
00212 #define TAILQ_ENTRY(type)                                               \
00213 struct {                                                                \
00214         struct type *tqe_next;  /* next element */                      \
00215         struct type **tqe_prev; /* address of previous next element */  \
00216 }
00217 
00218 
00219 #define TAILQ_FIRST(head)               ((head)->tqh_first)
00220 #define TAILQ_NEXT(elm, field)          ((elm)->field.tqe_next)
00221 #define TAILQ_END(head)                 NULL
00222 #define TAILQ_LAST(head, headname) \
00223         (*(((struct headname *)((head)->tqh_last))->tqh_last))
00224 #define TAILQ_PREV(elm, headname, field) \
00225         (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
00226 
00227 /*
00228  * Tail queue functions.
00229  */
00230 #define TAILQ_INIT(head) do {                                           \
00231         (head)->tqh_first = NULL;                                       \
00232         (head)->tqh_last = &(head)->tqh_first;                          \
00233 } while (0)
00234 
00235 #define TAILQ_INIT_ENTRY(elm, field) do {       \
00236   (elm)->field.tqe_next = 0;                    \
00237   (elm)->field.tqe_prev = 0;                    \
00238 } while (0)
00239 
00240 #define TAILQ_INSERT_HEAD(head, elm, field) do {                        \
00241         if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)        \
00242                 (head)->tqh_first->field.tqe_prev =                     \
00243                     &(elm)->field.tqe_next;                             \
00244         else                                                            \
00245                 (head)->tqh_last = &(elm)->field.tqe_next;              \
00246         (head)->tqh_first = (elm);                                      \
00247         (elm)->field.tqe_prev = &(head)->tqh_first;                     \
00248 } while (0)
00249 
00250 #define TAILQ_INSERT_TAIL(head, elm, field) do {                        \
00251         (elm)->field.tqe_next = NULL;                                   \
00252         (elm)->field.tqe_prev = (head)->tqh_last;                       \
00253         *(head)->tqh_last = (elm);                                      \
00254         (head)->tqh_last = &(elm)->field.tqe_next;                      \
00255 } while (0)
00256 
00257 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {              \
00258         if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
00259                 (elm)->field.tqe_next->field.tqe_prev =                 \
00260                     &(elm)->field.tqe_next;                             \
00261         else                                                            \
00262                 (head)->tqh_last = &(elm)->field.tqe_next;              \
00263         (listelm)->field.tqe_next = (elm);                              \
00264         (elm)->field.tqe_prev = &(listelm)->field.tqe_next;             \
00265 } while (0)
00266 
00267 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do {                   \
00268         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
00269         (elm)->field.tqe_next = (listelm);                              \
00270         *(listelm)->field.tqe_prev = (elm);                             \
00271         (listelm)->field.tqe_prev = &(elm)->field.tqe_next;             \
00272 } while (0)
00273 
00274 #define TAILQ_REMOVE(head, elm, field) do {                             \
00275         if (((elm)->field.tqe_next) != NULL)                            \
00276                 (elm)->field.tqe_next->field.tqe_prev =                 \
00277                     (elm)->field.tqe_prev;                              \
00278         else                                                            \
00279                 (head)->tqh_last = (elm)->field.tqe_prev;               \
00280         *(elm)->field.tqe_prev = (elm)->field.tqe_next;                 \
00281 } while (0)
00282 
00283 /*
00284  * Circular queue definitions.
00285  */
00286 #define CIRCLEQ_HEAD(name, type)                                        \
00287 typedef struct name {                                                   \
00288         struct type *cqh_first;         /* first element */             \
00289         struct type *cqh_last;          /* last element */              \
00290 } t ## name
00291 
00292 #define CIRCLEQ_ENTRY(type)                                             \
00293 struct {                                                                \
00294         struct type *cqe_next;          /* next element */              \
00295         struct type *cqe_prev;          /* previous element */          \
00296 }
00297 
00298 #define CIRCLEQ_FIRST(head)             ((head)->cqh_first)
00299 #define CIRCLEQ_LAST(head)              ((head)->cqh_last)
00300 #define CIRCLEQ_END(head)               ((void *)(head))
00301 #define CIRCLEQ_NEXT(elm, field)        ((elm)->field.cqe_next)
00302 #define CIRCLEQ_PREV(elm, field)        ((elm)->field.cqe_prev)
00303 
00304 /*
00305  * Circular queue functions.
00306  */
00307 #define CIRCLEQ_INIT(head, type) do {                                           \
00308         (head)->cqh_first = (struct type *)(head);                              \
00309         (head)->cqh_last = (struct type *)(head);                               \
00310 } while (0)
00311 
00312 #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field, type) do {              \
00313         (elm)->field.cqe_next = (listelm)->field.cqe_next;              \
00314         (elm)->field.cqe_prev = (listelm);                              \
00315         if ((listelm)->field.cqe_next == (struct type *)(head))         \
00316                 (head)->cqh_last = (elm);                               \
00317         else                                                            \
00318                 (listelm)->field.cqe_next->field.cqe_prev = (elm);      \
00319         (listelm)->field.cqe_next = (elm);                              \
00320 } while (0)
00321 
00322 #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field, type) do {             \
00323         (elm)->field.cqe_next = (listelm);                              \
00324         (elm)->field.cqe_prev = (listelm)->field.cqe_prev;              \
00325         if ((listelm)->field.cqe_prev == (struct type *)(head))         \
00326                 (head)->cqh_first = (elm);                              \
00327         else                                                            \
00328                 (listelm)->field.cqe_prev->field.cqe_next = (elm);      \
00329         (listelm)->field.cqe_prev = (elm);                              \
00330 } while (0)
00331 
00332 #define CIRCLEQ_INSERT_HEAD(head, elm, field, type) do {                        \
00333         (elm)->field.cqe_next = (head)->cqh_first;                      \
00334         (elm)->field.cqe_prev = (struct type *)(head);                          \
00335         if ((head)->cqh_last == (struct type *)(head))                          \
00336                 (head)->cqh_last = (elm);                               \
00337         else                                                            \
00338                 (head)->cqh_first->field.cqe_prev = (elm);              \
00339         (head)->cqh_first = (elm);                                      \
00340 } while (0)
00341 
00342 #define CIRCLEQ_INSERT_TAIL(head, elm, field, type) do {                        \
00343         (elm)->field.cqe_next = (struct type *)(head);                          \
00344         (elm)->field.cqe_prev = (head)->cqh_last;                       \
00345         if ((head)->cqh_first == (struct type *)(head))                 \
00346                 (head)->cqh_first = (elm);                              \
00347         else                                                            \
00348                 (head)->cqh_last->field.cqe_next = (elm);               \
00349         (head)->cqh_last = (elm);                                       \
00350 } while (0)
00351 
00352 #define CIRCLEQ_REMOVE(head, elm, field, type) do {                             \
00353         if ((elm)->field.cqe_next == (struct type *)(head))                     \
00354                 (head)->cqh_last = (elm)->field.cqe_prev;               \
00355         else                                                            \
00356                 (elm)->field.cqe_next->field.cqe_prev =                 \
00357                     (elm)->field.cqe_prev;                              \
00358         if ((elm)->field.cqe_prev == (struct type *)(head))                     \
00359                 (head)->cqh_first = (elm)->field.cqe_next;              \
00360         else                                                            \
00361                 (elm)->field.cqe_prev->field.cqe_next =                 \
00362                     (elm)->field.cqe_next;                              \
00363 } while (0)
00364 #endif  /* !_SYS_QUEUE_H_ */

Generated at Fri Apr 4 00:29:54 2003 for torcs by doxygen 1.3-rc1 written by Dimitri van Heesch, © 1997-1999
TORCS © Eric Espié 1999, 2002.