1 /*
2  * %CopyrightBegin%
3  *
4  * Copyright Ericsson AB 2013-2016. All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * %CopyrightEnd%
19  */
20 
21 #ifndef ERL_UTIL_QUEUE_H_
22 #define ERL_UTIL_QUEUE_H_
23 
24 #define erts_circleq_head(Q)     ((Q)->next)
25 #define erts_circleq_tail(Q)     ((Q)->prev)
26 #define erts_circleq_next(Q)     ((Q)->next)
27 #define erts_circleq_prev(Q)     ((Q)->prev)
28 #define erts_circleq_is_empty(Q) ((Q)->next == (void *)(Q))
29 
30 #define erts_circleq_remove(N)        \
31     do {                              \
32         (N)->next->prev = (N)->prev;  \
33         (N)->prev->next = (N)->next;  \
34         (N)->next = (N);              \
35         (N)->prev = (N);              \
36     } while(0)
37 
38 #define erts_circleq_pop_head(Q, N)   \
39     do {                              \
40 	(N) = (Q)->next;              \
41         (N)->next->prev = (N)->prev;  \
42         (N)->prev->next = (N)->next;  \
43         (N)->next = (N);              \
44         (N)->prev = (N);              \
45     } while(0)
46 
47 #define erts_circleq_pop_tail(Q, N)   \
48     do {                              \
49 	(N) = (Q)->prev;              \
50         (N)->next->prev = (N)->prev;  \
51         (N)->prev->next = (N)->next;  \
52         (N)->next = (N);              \
53         (N)->prev = (N);              \
54     } while(0)
55 
56 #define erts_circleq_push_head(Q, N)  \
57     do {                              \
58         (N)->next = (Q)->next;        \
59         (N)->prev = (void *)(Q);      \
60         (Q)->next->prev = (N);        \
61         (Q)->next = (N);              \
62     } while(0)
63 
64 #define erts_circleq_push_tail(Q, N)  \
65     do {                              \
66         (N)->prev = (Q)->prev;        \
67         (N)->next = (void *)(Q);      \
68         (Q)->prev->next = (N);        \
69         (Q)->prev = (N);              \
70     } while(0)
71 
72 #define erts_circleq_foreach(V, Q) \
73     for ((V) = (Q)->next; (V) != (const void *)(Q); (V) = (V)->next)
74 
75 #define erts_circleq_foreach_reverse(V, Q) \
76     for ((V) = (Q)->prev; (V) != (const void *)(Q); (V) = (V)->prev)
77 
78 #endif
79