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