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