1 /* Ring definitions. */
2 #define	qr(a_type)							\
3 struct {								\
4 	a_type	*qre_next;						\
5 	a_type	*qre_prev;						\
6 }
7 
8 /* Ring functions. */
9 #define	qr_new(a_qr, a_field) do {					\
10 	(a_qr)->a_field.qre_next = (a_qr);				\
11 	(a_qr)->a_field.qre_prev = (a_qr);				\
12 } while (0)
13 
14 #define	qr_next(a_qr, a_field) ((a_qr)->a_field.qre_next)
15 
16 #define	qr_prev(a_qr, a_field) ((a_qr)->a_field.qre_prev)
17 
18 #define	qr_before_insert(a_qrelm, a_qr, a_field) do {			\
19 	(a_qr)->a_field.qre_prev = (a_qrelm)->a_field.qre_prev;		\
20 	(a_qr)->a_field.qre_next = (a_qrelm);				\
21 	(a_qr)->a_field.qre_prev->a_field.qre_next = (a_qr);		\
22 	(a_qrelm)->a_field.qre_prev = (a_qr);				\
23 } while (0)
24 
25 #define	qr_after_insert(a_qrelm, a_qr, a_field)				\
26     do									\
27     {									\
28 	(a_qr)->a_field.qre_next = (a_qrelm)->a_field.qre_next;		\
29 	(a_qr)->a_field.qre_prev = (a_qrelm);				\
30 	(a_qr)->a_field.qre_next->a_field.qre_prev = (a_qr);		\
31 	(a_qrelm)->a_field.qre_next = (a_qr);				\
32     } while (0)
33 
34 #define	qr_meld(a_qr_a, a_qr_b, a_field) do {				\
35 	void *t;							\
36 	(a_qr_a)->a_field.qre_prev->a_field.qre_next = (a_qr_b);	\
37 	(a_qr_b)->a_field.qre_prev->a_field.qre_next = (a_qr_a);	\
38 	t = (a_qr_a)->a_field.qre_prev;					\
39 	(a_qr_a)->a_field.qre_prev = (a_qr_b)->a_field.qre_prev;	\
40 	(a_qr_b)->a_field.qre_prev = t;					\
41 } while (0)
42 
43 /*
44  * qr_meld() and qr_split() are functionally equivalent, so there's no need to
45  * have two copies of the code.
46  */
47 #define	qr_split(a_qr_a, a_qr_b, a_field)				\
48 	qr_meld((a_qr_a), (a_qr_b), a_field)
49 
50 #define	qr_remove(a_qr, a_field) do {					\
51 	(a_qr)->a_field.qre_prev->a_field.qre_next			\
52 	    = (a_qr)->a_field.qre_next;					\
53 	(a_qr)->a_field.qre_next->a_field.qre_prev			\
54 	    = (a_qr)->a_field.qre_prev;					\
55 	(a_qr)->a_field.qre_next = (a_qr);				\
56 	(a_qr)->a_field.qre_prev = (a_qr);				\
57 } while (0)
58 
59 #define	qr_foreach(var, a_qr, a_field)					\
60 	for ((var) = (a_qr);						\
61 	    (var) != NULL;						\
62 	    (var) = (((var)->a_field.qre_next != (a_qr))		\
63 	    ? (var)->a_field.qre_next : NULL))
64 
65 #define	qr_reverse_foreach(var, a_qr, a_field)				\
66 	for ((var) = ((a_qr) != NULL) ? qr_prev(a_qr, a_field) : NULL;	\
67 	    (var) != NULL;						\
68 	    (var) = (((var) != (a_qr))					\
69 	    ? (var)->a_field.qre_prev : NULL))
70