queue.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl>
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #ifndef QUEUE_H_
  16. #define QUEUE_H_
  17. #include <stddef.h>
  18. #define uv__queue_data(pointer, type, field) \
  19. ((type*) ((char*) (pointer) - offsetof(type, field)))
  20. #define uv__queue_foreach(q, h) \
  21. for ((q) = (h)->next; (q) != (h); (q) = (q)->next)
  22. static inline void uv__queue_init(struct uv__queue* q) {
  23. q->next = q;
  24. q->prev = q;
  25. }
  26. static inline int uv__queue_empty(const struct uv__queue* q) {
  27. return q == q->next;
  28. }
  29. static inline struct uv__queue* uv__queue_head(const struct uv__queue* q) {
  30. return q->next;
  31. }
  32. static inline struct uv__queue* uv__queue_next(const struct uv__queue* q) {
  33. return q->next;
  34. }
  35. static inline void uv__queue_add(struct uv__queue* h, struct uv__queue* n) {
  36. h->prev->next = n->next;
  37. n->next->prev = h->prev;
  38. h->prev = n->prev;
  39. h->prev->next = h;
  40. }
  41. static inline void uv__queue_split(struct uv__queue* h,
  42. struct uv__queue* q,
  43. struct uv__queue* n) {
  44. n->prev = h->prev;
  45. n->prev->next = n;
  46. n->next = q;
  47. h->prev = q->prev;
  48. h->prev->next = h;
  49. q->prev = n;
  50. }
  51. static inline void uv__queue_move(struct uv__queue* h, struct uv__queue* n) {
  52. if (uv__queue_empty(h))
  53. uv__queue_init(n);
  54. else
  55. uv__queue_split(h, h->next, n);
  56. }
  57. static inline void uv__queue_insert_head(struct uv__queue* h,
  58. struct uv__queue* q) {
  59. q->next = h->next;
  60. q->prev = h;
  61. q->next->prev = q;
  62. h->next = q;
  63. }
  64. static inline void uv__queue_insert_tail(struct uv__queue* h,
  65. struct uv__queue* q) {
  66. q->next = h;
  67. q->prev = h->prev;
  68. q->prev->next = q;
  69. h->prev = q;
  70. }
  71. static inline void uv__queue_remove(struct uv__queue* q) {
  72. q->prev->next = q->next;
  73. q->next->prev = q->prev;
  74. }
  75. #endif /* QUEUE_H_ */