queue.h

00001 /*  $Id: queue.h,v 1.1 2006/11/20 13:36:12 tho Exp $ */
00002 /*  $OpenBSD: queue.h,v 1.22 2001/06/23 04:39:35 angelos Exp $  */
00003 /*  $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $   */
00004 
00005 #ifndef _U_QUEUE_H_
00006 #define _U_QUEUE_H_
00007 
00008 /* A list is headed by a single forward pointer (or an array of forward
00009  * pointers for a hash table header). The elements are doubly linked so that
00010  * an arbitrary element can be removed without a need to traverse the list. 
00011  * New elements can be added to the list before or after an existing element 
00012  * or at the head of the list. A list may only be traversed in the forward 
00013  * direction. */
00014 
00015 #define LIST_HEAD(name, type)                                           \
00016 struct name {                                                           \
00017         struct type *lh_first;  /* first element */                     \
00018 }
00019 
00020 #define LIST_HEAD_INITIALIZER(head)                                     \
00021         { NULL }
00022 
00023 #define LIST_ENTRY(type)                                                \
00024 struct {                                                                \
00025         struct type *le_next;   /* next element */                      \
00026         struct type **le_prev;  /* address of previous next element */  \
00027 }
00028 
00029 #define LIST_FIRST(head)                ((head)->lh_first)
00030 #define LIST_END(head)                  NULL
00031 #define LIST_EMPTY(head)                (LIST_FIRST(head) == LIST_END(head))
00032 #define LIST_NEXT(elm, field)           ((elm)->field.le_next)
00033 
00034 #define LIST_FOREACH(var, head, field)                                  \
00035         for((var) = LIST_FIRST(head);                                   \
00036             (var)!= LIST_END(head);                                     \
00037             (var) = LIST_NEXT(var, field))
00038 
00039 #define LIST_INIT(head) do {                                            \
00040         LIST_FIRST(head) = LIST_END(head);                              \
00041 } while (0)
00042 
00043 #define LIST_INSERT_AFTER(listelm, elm, field) do {                     \
00044         if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)  \
00045                 (listelm)->field.le_next->field.le_prev =               \
00046                     &(elm)->field.le_next;                              \
00047         (listelm)->field.le_next = (elm);                               \
00048         (elm)->field.le_prev = &(listelm)->field.le_next;               \
00049 } while (0)
00050 
00051 #define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \
00052         (elm)->field.le_prev = (listelm)->field.le_prev;                \
00053         (elm)->field.le_next = (listelm);                               \
00054         *(listelm)->field.le_prev = (elm);                              \
00055         (listelm)->field.le_prev = &(elm)->field.le_next;               \
00056 } while (0)
00057 
00058 #define LIST_INSERT_HEAD(head, elm, field) do {                         \
00059         if (((elm)->field.le_next = (head)->lh_first) != NULL)          \
00060                 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
00061         (head)->lh_first = (elm);                                       \
00062         (elm)->field.le_prev = &(head)->lh_first;                       \
00063 } while (0)
00064 
00065 #define LIST_REMOVE(elm, field) do {                                    \
00066         if ((elm)->field.le_next != NULL)                               \
00067                 (elm)->field.le_next->field.le_prev =                   \
00068                     (elm)->field.le_prev;                               \
00069         *(elm)->field.le_prev = (elm)->field.le_next;                   \
00070 } while (0)
00071 
00072 #define LIST_REPLACE(elm, elm2, field) do {                             \
00073         if (((elm2)->field.le_next = (elm)->field.le_next) != NULL)     \
00074                 (elm2)->field.le_next->field.le_prev =                  \
00075                     &(elm2)->field.le_next;                             \
00076         (elm2)->field.le_prev = (elm)->field.le_prev;                   \
00077         *(elm2)->field.le_prev = (elm2);                                \
00078 } while (0)
00079 
00080 
00081 /* A tail queue is headed by a pair of pointers, one to the head of the
00082  * list and the other to the tail of the list. The elements are doubly
00083  * linked so that an arbitrary element can be removed without a need to
00084  * traverse the list. New elements can be added to the list after
00085  * an existing element, at the head of the list, or at the end of the
00086  * list. A tail queue may only be traversed in the forward direction. */
00087 
00088 /* Tail queue definitions. */
00089 #define TAILQ_HEAD(name, type)                                              \
00090 struct name {                                                               \
00091         struct type *tqh_first;     /* first element */                     \
00092         struct type **tqh_last;     /* addr of last next element */         \
00093 }
00094 
00095 #define TAILQ_ENTRY(type)                                                   \
00096 struct {                                                                    \
00097         struct type *tqe_next;      /* next element */                      \
00098         struct type **tqe_prev;     /* address of previous next element */  \
00099 }
00100 
00101 /* Tail queue functions. */
00102 #define TAILQ_INIT(head) {                                                  \
00103         (head)->tqh_first = NULL;                                           \
00104         (head)->tqh_last = &(head)->tqh_first;                              \
00105 }
00106 
00107 #define TAILQ_INSERT_HEAD(head, elm, field) {                               \
00108         if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)            \
00109                 (elm)->field.tqe_next->field.tqe_prev =                     \
00110                     &(elm)->field.tqe_next;                                 \
00111         else                                                                \
00112                 (head)->tqh_last = &(elm)->field.tqe_next;                  \
00113         (head)->tqh_first = (elm);                                          \
00114         (elm)->field.tqe_prev = &(head)->tqh_first;                         \
00115 }
00116 
00117 #define TAILQ_INSERT_TAIL(head, elm, field) {                               \
00118         (elm)->field.tqe_next = NULL;                                       \
00119         (elm)->field.tqe_prev = (head)->tqh_last;                           \
00120         *(head)->tqh_last = (elm);                                          \
00121         (head)->tqh_last = &(elm)->field.tqe_next;                          \
00122 }
00123 
00124 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) {                     \
00125         if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)    \
00126                 (elm)->field.tqe_next->field.tqe_prev =                     \
00127                     &(elm)->field.tqe_next;                                 \
00128         else                                                                \
00129                 (head)->tqh_last = &(elm)->field.tqe_next;                  \
00130         (listelm)->field.tqe_next = (elm);                                  \
00131         (elm)->field.tqe_prev = &(listelm)->field.tqe_next;                 \
00132 }
00133 
00134 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do {                       \
00135         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;                  \
00136         (elm)->field.tqe_next = (listelm);                                  \
00137         *(listelm)->field.tqe_prev = (elm);                                 \
00138         (listelm)->field.tqe_prev = &(elm)->field.tqe_next;                 \
00139 } while (0)
00140 
00141 #define TAILQ_REMOVE(head, elm, field) {                                    \
00142         if (((elm)->field.tqe_next) != NULL)                                \
00143                 (elm)->field.tqe_next->field.tqe_prev =                     \
00144                     (elm)->field.tqe_prev;                                  \
00145         else                                                                \
00146                 (head)->tqh_last = (elm)->field.tqe_prev;                   \
00147         *(elm)->field.tqe_prev = (elm)->field.tqe_next;                     \
00148 }
00149 
00150 
00151 /* A circle queue is headed by a pair of pointers, one to the head of the
00152  * list and the other to the tail of the list. The elements are doubly
00153  * linked so that an arbitrary element can be removed without a need to
00154  * traverse the list. New elements can be added to the list before or after
00155  * an existing element, at the head of the list, or at the end of the list.
00156  * A circle queue may be traversed in either direction, but has a more
00157  * complex end of list detection. */
00158 
00159 /* Circular queue definitions. */
00160 #define CIRCLEQ_HEAD(name, type)                                            \
00161 struct name {                                                               \
00162         struct type *cqh_first;               /* first element */           \
00163         struct type *cqh_last;                /* last element */            \
00164 }
00165 
00166 #define CIRCLEQ_ENTRY(type)                                                 \
00167 struct {                                                                    \
00168         struct type *cqe_next;                /* next element */            \
00169         struct type *cqe_prev;                /* previous element */        \
00170 }
00171 
00172 /* Circular queue functions. */
00173 #define CIRCLEQ_INIT(head) {                                                \
00174         (head)->cqh_first = (void *)(head);                                 \
00175         (head)->cqh_last = (void *)(head);                                  \
00176 }
00177 
00178 #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) {                   \
00179         (elm)->field.cqe_next = (listelm)->field.cqe_next;                  \
00180         (elm)->field.cqe_prev = (listelm);                                  \
00181         if ((listelm)->field.cqe_next == (void *)(head))                    \
00182                 (head)->cqh_last = (elm);                                   \
00183         else                                                                \
00184                 (listelm)->field.cqe_next->field.cqe_prev = (elm);          \
00185         (listelm)->field.cqe_next = (elm);                                  \
00186 }
00187 
00188 #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) {                  \
00189         (elm)->field.cqe_next = (listelm);                                  \
00190         (elm)->field.cqe_prev = (listelm)->field.cqe_prev;                  \
00191         if ((listelm)->field.cqe_prev == (void *)(head))                    \
00192                 (head)->cqh_first = (elm);                                  \
00193         else                                                                \
00194                 (listelm)->field.cqe_prev->field.cqe_next = (elm);          \
00195         (listelm)->field.cqe_prev = (elm);                                  \
00196 }
00197 
00198 #define CIRCLEQ_INSERT_HEAD(head, elm, field) {                             \
00199         (elm)->field.cqe_next = (head)->cqh_first;                          \
00200         (elm)->field.cqe_prev = (void *)(head);                             \
00201         if ((head)->cqh_last == (void *)(head))                             \
00202                 (head)->cqh_last = (elm);                                   \
00203         else                                                                \
00204                 (head)->cqh_first->field.cqe_prev = (elm);                  \
00205         (head)->cqh_first = (elm);                                          \
00206 }
00207 
00208 #define CIRCLEQ_INSERT_TAIL(head, elm, field) {                             \
00209         (elm)->field.cqe_next = (void *)(head);                             \
00210         (elm)->field.cqe_prev = (head)->cqh_last;                           \
00211         if ((head)->cqh_first == (void *)(head))                            \
00212                 (head)->cqh_first = (elm);                                  \
00213         else                                                                \
00214                 (head)->cqh_last->field.cqe_next = (elm);                   \
00215         (head)->cqh_last = (elm);                                           \
00216 }
00217 
00218 #define CIRCLEQ_REMOVE(head, elm, field) {                                  \
00219         if ((elm)->field.cqe_next == (void *)(head))                        \
00220                 (head)->cqh_last = (elm)->field.cqe_prev;                   \
00221         else                                                                \
00222                 (elm)->field.cqe_next->field.cqe_prev =                     \
00223                     (elm)->field.cqe_prev;                                  \
00224         if ((elm)->field.cqe_prev == (void *)(head))                        \
00225                 (head)->cqh_first = (elm)->field.cqe_next;                  \
00226         else                                                                \
00227                 (elm)->field.cqe_prev->field.cqe_next =                     \
00228                     (elm)->field.cqe_next;                                  \
00229 }
00230 
00231 #ifndef LIST_ENTRY_NULL
00232 #define LIST_ENTRY_NULL { NULL, NULL }
00233 #endif
00234 
00235 #ifndef LIST_FOREACH_SAFE
00236 #define LIST_FOREACH_SAFE(var, head, field, tvar)                       \
00237         for ((var) = LIST_FIRST((head));                                \
00238             (var) && ((tvar) = LIST_NEXT((var), field), 1);             \
00239             (var) = (tvar))
00240 #endif
00241 
00242 #ifndef LIST_FIRST
00243 #define LIST_FIRST(head)    ((head)->lh_first)
00244 #endif 
00245 
00246 #ifndef TAILQ_ENTRY_NULL
00247 #define TAILQ_ENTRY_NULL { NULL, NULL }
00248 #endif
00249 
00250 #ifndef TAILQ_FIRST
00251 #define TAILQ_FIRST(head)   ((head)->tqh_first)
00252 #endif 
00253 
00254 #ifndef TAILQ_END
00255 #define TAILQ_END(head)     NULL
00256 #endif
00257 
00258 #ifndef TAILQ_NEXT
00259 #define TAILQ_NEXT(elm, field)  ((elm)->field.tqe_next)
00260 #endif
00261 
00262 #ifndef TAILQ_LAST
00263 #define TAILQ_LAST(head, headname)                                          \
00264         (*(((struct headname *)((head)->tqh_last))->tqh_last))
00265 #endif
00266 
00267 #ifndef TAILQ_PREV
00268 #define TAILQ_PREV(elm, headname, field)                                    \
00269         (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
00270 #endif
00271 
00272 #ifndef TAILQ_EMPTY
00273 #define TAILQ_EMPTY(head)                                                   \
00274         (TAILQ_FIRST(head) == TAILQ_END(head))
00275 #endif
00276 
00277 #ifndef TAILQ_FOREACH
00278 #define TAILQ_FOREACH(var, head, field)                                     \
00279         for((var) = TAILQ_FIRST(head);                                      \
00280             (var) != TAILQ_END(head);                                       \
00281             (var) = TAILQ_NEXT(var, field))
00282 #endif
00283 
00284 #ifndef TAILQ_FOREACH_SAFE
00285 #define TAILQ_FOREACH_SAFE(var, head, field, tvar)                      \
00286         for ((var) = TAILQ_FIRST((head));                               \
00287             (var) && ((tvar) = TAILQ_NEXT((var), field), 1);            \
00288             (var) = (tvar))
00289           
00290 #endif
00291 #ifndef TAILQ_FOREACH_REVERSE
00292 #define TAILQ_FOREACH_REVERSE(var, head, headname, field)               \
00293         for ((var) = TAILQ_LAST((head), headname);                      \
00294             (var);                                                      \
00295             (var) = TAILQ_PREV((var), headname, field))
00296 
00297 #endif
00298 
00299 #ifndef TAILQ_FOREACH_REVERSE_SAFE
00300 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar)    \
00301         for ((var) = TAILQ_LAST((head), headname);                      \
00302             (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1);  \
00303             (var) = (tvar))
00304 #endif
00305 
00306 #endif /* !_U_QUEUE_H_ */

←Products
© 2005-2012 - KoanLogic S.r.l. - All rights reserved