ngx-queue.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) Igor Sysoev
  3. */
  4. #ifndef NGX_QUEUE_H_INCLUDED_
  5. #define NGX_QUEUE_H_INCLUDED_
  6. typedef struct ngx_queue_s ngx_queue_t;
  7. struct ngx_queue_s {
  8. ngx_queue_t *prev;
  9. ngx_queue_t *next;
  10. };
  11. #define ngx_queue_init(q) \
  12. do { \
  13. (q)->prev = q; \
  14. (q)->next = q; \
  15. } \
  16. while (0)
  17. #define ngx_queue_empty(h) \
  18. (h == (h)->prev)
  19. #define ngx_queue_insert_head(h, x) \
  20. do { \
  21. (x)->next = (h)->next; \
  22. (x)->next->prev = x; \
  23. (x)->prev = h; \
  24. (h)->next = x; \
  25. } \
  26. while (0)
  27. #define ngx_queue_insert_after ngx_queue_insert_head
  28. #define ngx_queue_insert_tail(h, x) \
  29. do { \
  30. (x)->prev = (h)->prev; \
  31. (x)->prev->next = x; \
  32. (x)->next = h; \
  33. (h)->prev = x; \
  34. } \
  35. while (0)
  36. #define ngx_queue_head(h) \
  37. (h)->next
  38. #define ngx_queue_last(h) \
  39. (h)->prev
  40. #define ngx_queue_sentinel(h) \
  41. (h)
  42. #define ngx_queue_next(q) \
  43. (q)->next
  44. #define ngx_queue_prev(q) \
  45. (q)->prev
  46. #if defined(NGX_DEBUG)
  47. #define ngx_queue_remove(x) \
  48. do { \
  49. (x)->next->prev = (x)->prev; \
  50. (x)->prev->next = (x)->next; \
  51. (x)->prev = NULL; \
  52. (x)->next = NULL; \
  53. } \
  54. while (0)
  55. #else
  56. #define ngx_queue_remove(x) \
  57. do { \
  58. (x)->next->prev = (x)->prev; \
  59. (x)->prev->next = (x)->next; \
  60. } \
  61. while (0)
  62. #endif
  63. #define ngx_queue_split(h, q, n) \
  64. do { \
  65. (n)->prev = (h)->prev; \
  66. (n)->prev->next = n; \
  67. (n)->next = q; \
  68. (h)->prev = (q)->prev; \
  69. (h)->prev->next = h; \
  70. (q)->prev = n; \
  71. } \
  72. while (0)
  73. #define ngx_queue_add(h, n) \
  74. do { \
  75. (h)->prev->next = (n)->next; \
  76. (n)->next->prev = (h)->prev; \
  77. (h)->prev = (n)->prev; \
  78. (h)->prev->next = h; \
  79. } \
  80. while (0)
  81. #define ngx_queue_data(q, type, link) \
  82. ((type *) ((unsigned char *) q - offsetof(type, link)))
  83. #define ngx_queue_foreach(q, h) \
  84. for ((q) = ngx_queue_head(h); \
  85. (q) != ngx_queue_sentinel(h) && !ngx_queue_empty(h); \
  86. (q) = ngx_queue_next(q))
  87. #endif /* NGX_QUEUE_H_INCLUDED_ */