ql.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2002 Jason Evans <jasone@canonware.com>.
  4. * Copyright (C) 2015-2018 Mark Straver <moonchild@palemoon.org>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice(s), this list of conditions and the following disclaimer
  11. * unmodified other than the allowable addition of one or more
  12. * copyright notices.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice(s), this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  25. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  27. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  28. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. ******************************************************************************/
  31. /*
  32. * List definitions.
  33. */
  34. #define ql_head(a_type) \
  35. struct { \
  36. a_type *qlh_first; \
  37. }
  38. #define ql_head_initializer(a_head) {NULL}
  39. #define ql_elm(a_type) qr(a_type)
  40. /* List functions. */
  41. #define ql_new(a_head) do { \
  42. (a_head)->qlh_first = NULL; \
  43. } while (0)
  44. #define ql_elm_new(a_elm, a_field) qr_new((a_elm), a_field)
  45. #define ql_first(a_head) ((a_head)->qlh_first)
  46. #define ql_last(a_head, a_field) \
  47. ((ql_first(a_head) != NULL) \
  48. ? qr_prev(ql_first(a_head), a_field) : NULL)
  49. #define ql_next(a_head, a_elm, a_field) \
  50. ((ql_last(a_head, a_field) != (a_elm)) \
  51. ? qr_next((a_elm), a_field) : NULL)
  52. #define ql_prev(a_head, a_elm, a_field) \
  53. ((ql_first(a_head) != (a_elm)) ? qr_prev((a_elm), a_field) \
  54. : NULL)
  55. #define ql_before_insert(a_head, a_qlelm, a_elm, a_field) do { \
  56. qr_before_insert((a_qlelm), (a_elm), a_field); \
  57. if (ql_first(a_head) == (a_qlelm)) { \
  58. ql_first(a_head) = (a_elm); \
  59. } \
  60. } while (0)
  61. #define ql_after_insert(a_qlelm, a_elm, a_field) \
  62. qr_after_insert((a_qlelm), (a_elm), a_field)
  63. #define ql_head_insert(a_head, a_elm, a_field) do { \
  64. if (ql_first(a_head) != NULL) { \
  65. qr_before_insert(ql_first(a_head), (a_elm), a_field); \
  66. } \
  67. ql_first(a_head) = (a_elm); \
  68. } while (0)
  69. #define ql_tail_insert(a_head, a_elm, a_field) do { \
  70. if (ql_first(a_head) != NULL) { \
  71. qr_before_insert(ql_first(a_head), (a_elm), a_field); \
  72. } \
  73. ql_first(a_head) = qr_next((a_elm), a_field); \
  74. } while (0)
  75. #define ql_remove(a_head, a_elm, a_field) do { \
  76. if (ql_first(a_head) == (a_elm)) { \
  77. ql_first(a_head) = qr_next(ql_first(a_head), a_field); \
  78. } \
  79. if (ql_first(a_head) != (a_elm)) { \
  80. qr_remove((a_elm), a_field); \
  81. } else { \
  82. ql_first(a_head) = NULL; \
  83. } \
  84. } while (0)
  85. #define ql_head_remove(a_head, a_type, a_field) do { \
  86. a_type *t = ql_first(a_head); \
  87. ql_remove((a_head), t, a_field); \
  88. } while (0)
  89. #define ql_tail_remove(a_head, a_type, a_field) do { \
  90. a_type *t = ql_last(a_head, a_field); \
  91. ql_remove((a_head), t, a_field); \
  92. } while (0)
  93. #define ql_foreach(a_var, a_head, a_field) \
  94. qr_foreach((a_var), ql_first(a_head), a_field)
  95. #define ql_reverse_foreach(a_var, a_head, a_field) \
  96. qr_reverse_foreach((a_var), ql_first(a_head), a_field)