1/*
2 * List definitions.
3 */
4#define	ql_head(a_type)							\
5struct {								\
6	a_type *qlh_first;						\
7}
8
9#define	ql_head_initializer(a_head) {NULL}
10
11#define	ql_elm(a_type)	qr(a_type)
12
13/* List functions. */
14#define	ql_new(a_head) do {						\
15	(a_head)->qlh_first = NULL;					\
16} while (0)
17
18#define	ql_elm_new(a_elm, a_field) qr_new((a_elm), a_field)
19
20#define	ql_first(a_head) ((a_head)->qlh_first)
21
22#define	ql_last(a_head, a_field)					\
23	((ql_first(a_head) != NULL)					\
24	    ? qr_prev(ql_first(a_head), a_field) : NULL)
25
26#define	ql_next(a_head, a_elm, a_field)					\
27	((ql_last(a_head, a_field) != (a_elm))				\
28	    ? qr_next((a_elm), a_field)	: NULL)
29
30#define	ql_prev(a_head, a_elm, a_field)					\
31	((ql_first(a_head) != (a_elm)) ? qr_prev((a_elm), a_field)	\
32				       : NULL)
33
34#define	ql_before_insert(a_head, a_qlelm, a_elm, a_field) do {		\
35	qr_before_insert((a_qlelm), (a_elm), a_field);			\
36	if (ql_first(a_head) == (a_qlelm)) {				\
37		ql_first(a_head) = (a_elm);				\
38	}								\
39} while (0)
40
41#define	ql_after_insert(a_qlelm, a_elm, a_field)			\
42	qr_after_insert((a_qlelm), (a_elm), a_field)
43
44#define	ql_head_insert(a_head, a_elm, a_field) do {			\
45	if (ql_first(a_head) != NULL) {					\
46		qr_before_insert(ql_first(a_head), (a_elm), a_field);	\
47	}								\
48	ql_first(a_head) = (a_elm);					\
49} while (0)
50
51#define	ql_tail_insert(a_head, a_elm, a_field) do {			\
52	if (ql_first(a_head) != NULL) {					\
53		qr_before_insert(ql_first(a_head), (a_elm), a_field);	\
54	}								\
55	ql_first(a_head) = qr_next((a_elm), a_field);			\
56} while (0)
57
58#define	ql_remove(a_head, a_elm, a_field) do {				\
59	if (ql_first(a_head) == (a_elm)) {				\
60		ql_first(a_head) = qr_next(ql_first(a_head), a_field);	\
61	}								\
62	if (ql_first(a_head) != (a_elm)) {				\
63		qr_remove((a_elm), a_field);				\
64	} else {							\
65		ql_first(a_head) = NULL;				\
66	}								\
67} while (0)
68
69#define	ql_head_remove(a_head, a_type, a_field) do {			\
70	a_type *t = ql_first(a_head);					\
71	ql_remove((a_head), t, a_field);				\
72} while (0)
73
74#define	ql_tail_remove(a_head, a_type, a_field) do {			\
75	a_type *t = ql_last(a_head, a_field);				\
76	ql_remove((a_head), t, a_field);				\
77} while (0)
78
79#define	ql_foreach(a_var, a_head, a_field)				\
80	qr_foreach((a_var), ql_first(a_head), a_field)
81
82#define	ql_reverse_foreach(a_var, a_head, a_field)			\
83	qr_reverse_foreach((a_var), ql_first(a_head), a_field)
84