circ-queue.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 1991, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * @(#)queue.h 8.5 (Berkeley) 8/20/94
  34. * $FreeBSD: ports/misc/44bsd-more/files/queue.h,v 1.1 2001/01/06 03:41:36 hoek Exp $
  35. */
  36. /*
  37. * Circular queue definitions.
  38. */
  39. #define CIRCLEQ_HEAD(name, type) \
  40. struct name { \
  41. struct type *cqh_first; /* first element */ \
  42. struct type *cqh_last; /* last element */ \
  43. }
  44. #define CIRCLEQ_ENTRY(type) \
  45. struct { \
  46. struct type *cqe_next; /* next element */ \
  47. struct type *cqe_prev; /* previous element */ \
  48. }
  49. /*
  50. * Circular queue functions.
  51. */
  52. #define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
  53. #define CIRCLEQ_FIRST(head) ((head)->cqh_first)
  54. #define CIRCLEQ_FOREACH(var, head, field) \
  55. for((var) = (head)->cqh_first; \
  56. (var) != (void *)(head); \
  57. (var) = (var)->field.cqe_next)
  58. #define CIRCLEQ_FOREACH_REVERSE(var, head, field) \
  59. for((var) = (head)->cqh_last; \
  60. (var) != (void *)(head); \
  61. (var) = (var)->field.cqe_prev)
  62. #define CIRCLEQ_INIT(head) do { \
  63. (head)->cqh_first = (void *)(head); \
  64. (head)->cqh_last = (void *)(head); \
  65. } while (0)
  66. #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  67. (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  68. (elm)->field.cqe_prev = (listelm); \
  69. if ((listelm)->field.cqe_next == (void *)(head)) \
  70. (head)->cqh_last = (elm); \
  71. else \
  72. (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  73. (listelm)->field.cqe_next = (elm); \
  74. } while (0)
  75. #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  76. (elm)->field.cqe_next = (listelm); \
  77. (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  78. if ((listelm)->field.cqe_prev == (void *)(head)) \
  79. (head)->cqh_first = (elm); \
  80. else \
  81. (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  82. (listelm)->field.cqe_prev = (elm); \
  83. } while (0)
  84. #define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  85. (elm)->field.cqe_next = (head)->cqh_first; \
  86. (elm)->field.cqe_prev = (void *)(head); \
  87. if ((head)->cqh_last == (void *)(head)) \
  88. (head)->cqh_last = (elm); \
  89. else \
  90. (head)->cqh_first->field.cqe_prev = (elm); \
  91. (head)->cqh_first = (elm); \
  92. } while (0)
  93. #define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  94. (elm)->field.cqe_next = (void *)(head); \
  95. (elm)->field.cqe_prev = (head)->cqh_last; \
  96. if ((head)->cqh_first == (void *)(head)) \
  97. (head)->cqh_first = (elm); \
  98. else \
  99. (head)->cqh_last->field.cqe_next = (elm); \
  100. (head)->cqh_last = (elm); \
  101. } while (0)
  102. #define CIRCLEQ_LAST(head) ((head)->cqh_last)
  103. #define CIRCLEQ_NEXT(elm,field) ((elm)->field.cqe_next)
  104. #define CIRCLEQ_PREV(elm,field) ((elm)->field.cqe_prev)
  105. #define CIRCLEQ_REMOVE(head, elm, field) do { \
  106. if ((elm)->field.cqe_next == (void *)(head)) \
  107. (head)->cqh_last = (elm)->field.cqe_prev; \
  108. else \
  109. (elm)->field.cqe_next->field.cqe_prev = \
  110. (elm)->field.cqe_prev; \
  111. if ((elm)->field.cqe_prev == (void *)(head)) \
  112. (head)->cqh_first = (elm)->field.cqe_next; \
  113. else \
  114. (elm)->field.cqe_prev->field.cqe_next = \
  115. (elm)->field.cqe_next; \
  116. } while (0)