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