qemu/include/qemu/queue.h
<<
>>
Prefs
   1/*      $NetBSD: queue.h,v 1.52 2009/04/20 09:56:08 mschuett Exp $ */
   2
   3/*
   4 * QEMU version: Copy from netbsd, removed debug code, removed some of
   5 * the implementations.  Left in singly-linked lists, lists, simple
   6 * queues, and tail queues.
   7 */
   8
   9/*
  10 * Copyright (c) 1991, 1993
  11 *      The Regents of the University of California.  All rights reserved.
  12 *
  13 * Redistribution and use in source and binary forms, with or without
  14 * modification, are permitted provided that the following conditions
  15 * are met:
  16 * 1. Redistributions of source code must retain the above copyright
  17 *    notice, this list of conditions and the following disclaimer.
  18 * 2. Redistributions in binary form must reproduce the above copyright
  19 *    notice, this list of conditions and the following disclaimer in the
  20 *    documentation and/or other materials provided with the distribution.
  21 * 3. Neither the name of the University nor the names of its contributors
  22 *    may be used to endorse or promote products derived from this software
  23 *    without specific prior written permission.
  24 *
  25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35 * SUCH DAMAGE.
  36 *
  37 *      @(#)queue.h     8.5 (Berkeley) 8/20/94
  38 */
  39
  40#ifndef QEMU_SYS_QUEUE_H_
  41#define QEMU_SYS_QUEUE_H_
  42
  43/*
  44 * This file defines four types of data structures: singly-linked lists,
  45 * lists, simple queues, and tail queues.
  46 *
  47 * A singly-linked list is headed by a single forward pointer. The
  48 * elements are singly linked for minimum space and pointer manipulation
  49 * overhead at the expense of O(n) removal for arbitrary elements. New
  50 * elements can be added to the list after an existing element or at the
  51 * head of the list.  Elements being removed from the head of the list
  52 * should use the explicit macro for this purpose for optimum
  53 * efficiency. A singly-linked list may only be traversed in the forward
  54 * direction.  Singly-linked lists are ideal for applications with large
  55 * datasets and few or no removals or for implementing a LIFO queue.
  56 *
  57 * A list is headed by a single forward pointer (or an array of forward
  58 * pointers for a hash table header). The elements are doubly linked
  59 * so that an arbitrary element can be removed without a need to
  60 * traverse the list. New elements can be added to the list before
  61 * or after an existing element or at the head of the list. A list
  62 * may only be traversed in the forward direction.
  63 *
  64 * A simple queue is headed by a pair of pointers, one the head of the
  65 * list and the other to the tail of the list. The elements are singly
  66 * linked to save space, so elements can only be removed from the
  67 * head of the list. New elements can be added to the list after
  68 * an existing element, at the head of the list, or at the end of the
  69 * list. A simple queue may only be traversed in the forward direction.
  70 *
  71 * A tail queue is headed by a pair of pointers, one to the head of the
  72 * list and the other to the tail of the list. The elements are doubly
  73 * linked so that an arbitrary element can be removed without a need to
  74 * traverse the list. New elements can be added to the list before or
  75 * after an existing element, at the head of the list, or at the end of
  76 * the list. A tail queue may be traversed in either direction.
  77 *
  78 * For details on the use of these macros, see the queue(3) manual page.
  79 */
  80
  81#include "qemu/atomic.h" /* for smp_wmb() */
  82
  83/*
  84 * List definitions.
  85 */
  86#define QLIST_HEAD(name, type)                                          \
  87struct name {                                                           \
  88        struct type *lh_first;  /* first element */                     \
  89}
  90
  91#define QLIST_HEAD_INITIALIZER(head)                                    \
  92        { NULL }
  93
  94#define QLIST_ENTRY(type)                                               \
  95struct {                                                                \
  96        struct type *le_next;   /* next element */                      \
  97        struct type **le_prev;  /* address of previous next element */  \
  98}
  99
 100/*
 101 * List functions.
 102 */
 103#define QLIST_INIT(head) do {                                           \
 104        (head)->lh_first = NULL;                                        \
 105} while (/*CONSTCOND*/0)
 106
 107#define QLIST_SWAP(dstlist, srclist, field) do {                        \
 108        void *tmplist;                                                  \
 109        tmplist = (srclist)->lh_first;                                  \
 110        (srclist)->lh_first = (dstlist)->lh_first;                      \
 111        if ((srclist)->lh_first != NULL) {                              \
 112            (srclist)->lh_first->field.le_prev = &(srclist)->lh_first;  \
 113        }                                                               \
 114        (dstlist)->lh_first = tmplist;                                  \
 115        if ((dstlist)->lh_first != NULL) {                              \
 116            (dstlist)->lh_first->field.le_prev = &(dstlist)->lh_first;  \
 117        }                                                               \
 118} while (/*CONSTCOND*/0)
 119
 120#define QLIST_FIX_HEAD_PTR(head, field) do {                            \
 121        if ((head)->lh_first != NULL) {                                 \
 122            (head)->lh_first->field.le_prev = &(head)->lh_first;        \
 123        }                                                               \
 124} while (/*CONSTCOND*/0)
 125
 126#define QLIST_INSERT_AFTER(listelm, elm, field) do {                    \
 127        if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)  \
 128                (listelm)->field.le_next->field.le_prev =               \
 129                    &(elm)->field.le_next;                              \
 130        (listelm)->field.le_next = (elm);                               \
 131        (elm)->field.le_prev = &(listelm)->field.le_next;               \
 132} while (/*CONSTCOND*/0)
 133
 134#define QLIST_INSERT_BEFORE(listelm, elm, field) do {                   \
 135        (elm)->field.le_prev = (listelm)->field.le_prev;                \
 136        (elm)->field.le_next = (listelm);                               \
 137        *(listelm)->field.le_prev = (elm);                              \
 138        (listelm)->field.le_prev = &(elm)->field.le_next;               \
 139} while (/*CONSTCOND*/0)
 140
 141#define QLIST_INSERT_HEAD(head, elm, field) do {                        \
 142        if (((elm)->field.le_next = (head)->lh_first) != NULL)          \
 143                (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
 144        (head)->lh_first = (elm);                                       \
 145        (elm)->field.le_prev = &(head)->lh_first;                       \
 146} while (/*CONSTCOND*/0)
 147
 148#define QLIST_REMOVE(elm, field) do {                                   \
 149        if ((elm)->field.le_next != NULL)                               \
 150                (elm)->field.le_next->field.le_prev =                   \
 151                    (elm)->field.le_prev;                               \
 152        *(elm)->field.le_prev = (elm)->field.le_next;                   \
 153} while (/*CONSTCOND*/0)
 154
 155#define QLIST_FOREACH(var, head, field)                                 \
 156        for ((var) = ((head)->lh_first);                                \
 157                (var);                                                  \
 158                (var) = ((var)->field.le_next))
 159
 160#define QLIST_FOREACH_SAFE(var, head, field, next_var)                  \
 161        for ((var) = ((head)->lh_first);                                \
 162                (var) && ((next_var) = ((var)->field.le_next), 1);      \
 163                (var) = (next_var))
 164
 165/*
 166 * List access methods.
 167 */
 168#define QLIST_EMPTY(head)                ((head)->lh_first == NULL)
 169#define QLIST_FIRST(head)                ((head)->lh_first)
 170#define QLIST_NEXT(elm, field)           ((elm)->field.le_next)
 171
 172
 173/*
 174 * Singly-linked List definitions.
 175 */
 176#define QSLIST_HEAD(name, type)                                          \
 177struct name {                                                           \
 178        struct type *slh_first; /* first element */                     \
 179}
 180
 181#define QSLIST_HEAD_INITIALIZER(head)                                    \
 182        { NULL }
 183
 184#define QSLIST_ENTRY(type)                                               \
 185struct {                                                                \
 186        struct type *sle_next;  /* next element */                      \
 187}
 188
 189/*
 190 * Singly-linked List functions.
 191 */
 192#define QSLIST_INIT(head) do {                                           \
 193        (head)->slh_first = NULL;                                       \
 194} while (/*CONSTCOND*/0)
 195
 196#define QSLIST_INSERT_AFTER(slistelm, elm, field) do {                   \
 197        (elm)->field.sle_next = (slistelm)->field.sle_next;             \
 198        (slistelm)->field.sle_next = (elm);                             \
 199} while (/*CONSTCOND*/0)
 200
 201#define QSLIST_INSERT_HEAD(head, elm, field) do {                        \
 202        (elm)->field.sle_next = (head)->slh_first;                       \
 203        (head)->slh_first = (elm);                                       \
 204} while (/*CONSTCOND*/0)
 205
 206#define QSLIST_INSERT_HEAD_ATOMIC(head, elm, field) do {                     \
 207        typeof(elm) save_sle_next;                                           \
 208        do {                                                                 \
 209            save_sle_next = (elm)->field.sle_next = (head)->slh_first;       \
 210        } while (atomic_cmpxchg(&(head)->slh_first, save_sle_next, (elm)) != \
 211                 save_sle_next);                                             \
 212} while (/*CONSTCOND*/0)
 213
 214#define QSLIST_MOVE_ATOMIC(dest, src) do {                               \
 215        (dest)->slh_first = atomic_xchg(&(src)->slh_first, NULL);        \
 216} while (/*CONSTCOND*/0)
 217
 218#define QSLIST_REMOVE_HEAD(head, field) do {                             \
 219        (head)->slh_first = (head)->slh_first->field.sle_next;          \
 220} while (/*CONSTCOND*/0)
 221
 222#define QSLIST_REMOVE_AFTER(slistelm, field) do {                        \
 223        (slistelm)->field.sle_next =                                    \
 224            QSLIST_NEXT(QSLIST_NEXT((slistelm), field), field);           \
 225} while (/*CONSTCOND*/0)
 226
 227#define QSLIST_FOREACH(var, head, field)                                 \
 228        for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
 229
 230#define QSLIST_FOREACH_SAFE(var, head, field, tvar)                      \
 231        for ((var) = QSLIST_FIRST((head));                               \
 232            (var) && ((tvar) = QSLIST_NEXT((var), field), 1);            \
 233            (var) = (tvar))
 234
 235/*
 236 * Singly-linked List access methods.
 237 */
 238#define QSLIST_EMPTY(head)       ((head)->slh_first == NULL)
 239#define QSLIST_FIRST(head)       ((head)->slh_first)
 240#define QSLIST_NEXT(elm, field)  ((elm)->field.sle_next)
 241
 242
 243/*
 244 * Simple queue definitions.
 245 */
 246#define QSIMPLEQ_HEAD(name, type)                                       \
 247struct name {                                                           \
 248    struct type *sqh_first;    /* first element */                      \
 249    struct type **sqh_last;    /* addr of last next element */          \
 250}
 251
 252#define QSIMPLEQ_HEAD_INITIALIZER(head)                                 \
 253    { NULL, &(head).sqh_first }
 254
 255#define QSIMPLEQ_ENTRY(type)                                            \
 256struct {                                                                \
 257    struct type *sqe_next;    /* next element */                        \
 258}
 259
 260/*
 261 * Simple queue functions.
 262 */
 263#define QSIMPLEQ_INIT(head) do {                                        \
 264    (head)->sqh_first = NULL;                                           \
 265    (head)->sqh_last = &(head)->sqh_first;                              \
 266} while (/*CONSTCOND*/0)
 267
 268#define QSIMPLEQ_INSERT_HEAD(head, elm, field) do {                     \
 269    if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)            \
 270        (head)->sqh_last = &(elm)->field.sqe_next;                      \
 271    (head)->sqh_first = (elm);                                          \
 272} while (/*CONSTCOND*/0)
 273
 274#define QSIMPLEQ_INSERT_TAIL(head, elm, field) do {                     \
 275    (elm)->field.sqe_next = NULL;                                       \
 276    *(head)->sqh_last = (elm);                                          \
 277    (head)->sqh_last = &(elm)->field.sqe_next;                          \
 278} while (/*CONSTCOND*/0)
 279
 280#define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {           \
 281    if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)    \
 282        (head)->sqh_last = &(elm)->field.sqe_next;                      \
 283    (listelm)->field.sqe_next = (elm);                                  \
 284} while (/*CONSTCOND*/0)
 285
 286#define QSIMPLEQ_REMOVE_HEAD(head, field) do {                          \
 287    if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
 288        (head)->sqh_last = &(head)->sqh_first;                          \
 289} while (/*CONSTCOND*/0)
 290
 291#define QSIMPLEQ_SPLIT_AFTER(head, elm, field, removed) do {            \
 292    QSIMPLEQ_INIT(removed);                                             \
 293    if (((removed)->sqh_first = (head)->sqh_first) != NULL) {           \
 294        if (((head)->sqh_first = (elm)->field.sqe_next) == NULL) {      \
 295            (head)->sqh_last = &(head)->sqh_first;                      \
 296        }                                                               \
 297        (removed)->sqh_last = &(elm)->field.sqe_next;                   \
 298        (elm)->field.sqe_next = NULL;                                   \
 299    }                                                                   \
 300} while (/*CONSTCOND*/0)
 301
 302#define QSIMPLEQ_REMOVE(head, elm, type, field) do {                    \
 303    if ((head)->sqh_first == (elm)) {                                   \
 304        QSIMPLEQ_REMOVE_HEAD((head), field);                            \
 305    } else {                                                            \
 306        struct type *curelm = (head)->sqh_first;                        \
 307        while (curelm->field.sqe_next != (elm))                         \
 308            curelm = curelm->field.sqe_next;                            \
 309        if ((curelm->field.sqe_next =                                   \
 310            curelm->field.sqe_next->field.sqe_next) == NULL)            \
 311                (head)->sqh_last = &(curelm)->field.sqe_next;           \
 312    }                                                                   \
 313} while (/*CONSTCOND*/0)
 314
 315#define QSIMPLEQ_FOREACH(var, head, field)                              \
 316    for ((var) = ((head)->sqh_first);                                   \
 317        (var);                                                          \
 318        (var) = ((var)->field.sqe_next))
 319
 320#define QSIMPLEQ_FOREACH_SAFE(var, head, field, next)                   \
 321    for ((var) = ((head)->sqh_first);                                   \
 322        (var) && ((next = ((var)->field.sqe_next)), 1);                 \
 323        (var) = (next))
 324
 325#define QSIMPLEQ_CONCAT(head1, head2) do {                              \
 326    if (!QSIMPLEQ_EMPTY((head2))) {                                     \
 327        *(head1)->sqh_last = (head2)->sqh_first;                        \
 328        (head1)->sqh_last = (head2)->sqh_last;                          \
 329        QSIMPLEQ_INIT((head2));                                         \
 330    }                                                                   \
 331} while (/*CONSTCOND*/0)
 332
 333#define QSIMPLEQ_LAST(head, type, field)                                \
 334    (QSIMPLEQ_EMPTY((head)) ?                                           \
 335        NULL :                                                          \
 336            ((struct type *)(void *)                                    \
 337        ((char *)((head)->sqh_last) - offsetof(struct type, field))))
 338
 339/*
 340 * Simple queue access methods.
 341 */
 342#define QSIMPLEQ_EMPTY(head)        ((head)->sqh_first == NULL)
 343#define QSIMPLEQ_FIRST(head)        ((head)->sqh_first)
 344#define QSIMPLEQ_NEXT(elm, field)   ((elm)->field.sqe_next)
 345
 346
 347/*
 348 * Tail queue definitions.
 349 */
 350#define Q_TAILQ_HEAD(name, type, qual)                                  \
 351struct name {                                                           \
 352        qual type *tqh_first;           /* first element */             \
 353        qual type *qual *tqh_last;      /* addr of last next element */ \
 354}
 355#define QTAILQ_HEAD(name, type)  Q_TAILQ_HEAD(name, struct type,)
 356
 357#define QTAILQ_HEAD_INITIALIZER(head)                                   \
 358        { NULL, &(head).tqh_first }
 359
 360#define Q_TAILQ_ENTRY(type, qual)                                       \
 361struct {                                                                \
 362        qual type *tqe_next;            /* next element */              \
 363        qual type *qual *tqe_prev;      /* address of previous next element */\
 364}
 365#define QTAILQ_ENTRY(type)       Q_TAILQ_ENTRY(struct type,)
 366
 367/*
 368 * Tail queue functions.
 369 */
 370#define QTAILQ_INIT(head) do {                                          \
 371        (head)->tqh_first = NULL;                                       \
 372        (head)->tqh_last = &(head)->tqh_first;                          \
 373} while (/*CONSTCOND*/0)
 374
 375#define QTAILQ_INSERT_HEAD(head, elm, field) do {                       \
 376        if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)        \
 377                (head)->tqh_first->field.tqe_prev =                     \
 378                    &(elm)->field.tqe_next;                             \
 379        else                                                            \
 380                (head)->tqh_last = &(elm)->field.tqe_next;              \
 381        (head)->tqh_first = (elm);                                      \
 382        (elm)->field.tqe_prev = &(head)->tqh_first;                     \
 383} while (/*CONSTCOND*/0)
 384
 385#define QTAILQ_INSERT_TAIL(head, elm, field) do {                       \
 386        (elm)->field.tqe_next = NULL;                                   \
 387        (elm)->field.tqe_prev = (head)->tqh_last;                       \
 388        *(head)->tqh_last = (elm);                                      \
 389        (head)->tqh_last = &(elm)->field.tqe_next;                      \
 390} while (/*CONSTCOND*/0)
 391
 392#define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do {             \
 393        if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
 394                (elm)->field.tqe_next->field.tqe_prev =                 \
 395                    &(elm)->field.tqe_next;                             \
 396        else                                                            \
 397                (head)->tqh_last = &(elm)->field.tqe_next;              \
 398        (listelm)->field.tqe_next = (elm);                              \
 399        (elm)->field.tqe_prev = &(listelm)->field.tqe_next;             \
 400} while (/*CONSTCOND*/0)
 401
 402#define QTAILQ_INSERT_BEFORE(listelm, elm, field) do {                  \
 403        (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
 404        (elm)->field.tqe_next = (listelm);                              \
 405        *(listelm)->field.tqe_prev = (elm);                             \
 406        (listelm)->field.tqe_prev = &(elm)->field.tqe_next;             \
 407} while (/*CONSTCOND*/0)
 408
 409#define QTAILQ_REMOVE(head, elm, field) do {                            \
 410        if (((elm)->field.tqe_next) != NULL)                            \
 411                (elm)->field.tqe_next->field.tqe_prev =                 \
 412                    (elm)->field.tqe_prev;                              \
 413        else                                                            \
 414                (head)->tqh_last = (elm)->field.tqe_prev;               \
 415        *(elm)->field.tqe_prev = (elm)->field.tqe_next;                 \
 416} while (/*CONSTCOND*/0)
 417
 418#define QTAILQ_FOREACH(var, head, field)                                \
 419        for ((var) = ((head)->tqh_first);                               \
 420                (var);                                                  \
 421                (var) = ((var)->field.tqe_next))
 422
 423#define QTAILQ_FOREACH_SAFE(var, head, field, next_var)                 \
 424        for ((var) = ((head)->tqh_first);                               \
 425                (var) && ((next_var) = ((var)->field.tqe_next), 1);     \
 426                (var) = (next_var))
 427
 428#define QTAILQ_FOREACH_REVERSE(var, head, headname, field)              \
 429        for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last));    \
 430                (var);                                                  \
 431                (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
 432
 433/*
 434 * Tail queue access methods.
 435 */
 436#define QTAILQ_EMPTY(head)               ((head)->tqh_first == NULL)
 437#define QTAILQ_FIRST(head)               ((head)->tqh_first)
 438#define QTAILQ_NEXT(elm, field)          ((elm)->field.tqe_next)
 439
 440#define QTAILQ_LAST(head, headname) \
 441        (*(((struct headname *)((head)->tqh_last))->tqh_last))
 442#define QTAILQ_PREV(elm, headname, field) \
 443        (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
 444
 445#endif  /* !QEMU_SYS_QUEUE_H_ */
 446