1/******************************************************************************
2 *
3 * Copyright (C) 2002 Jason Evans <jasone@FreeBSD.org>.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice(s), this list of conditions and the following disclaimer
11 *    unmodified other than the allowable addition of one or more
12 *    copyright notices.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice(s), this list of conditions and the following disclaimer in
15 *    the documentation and/or other materials provided with the
16 *    distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 ******************************************************************************/
31
32#ifndef QL_H_
33#define	QL_H_
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD$");
37
38/*
39 * List definitions.
40 */
41#define ql_head(a_type)							\
42struct {								\
43	a_type *qlh_first;						\
44}
45
46#define ql_head_initializer(a_head) {NULL}
47
48#define ql_elm(a_type)	qr(a_type)
49
50/* List functions. */
51#define ql_new(a_head) do {						\
52	(a_head)->qlh_first = NULL;					\
53} while (0)
54
55#define ql_elm_new(a_elm, a_field) qr_new((a_elm), a_field)
56
57#define ql_first(a_head) ((a_head)->qlh_first)
58
59#define ql_last(a_head, a_field)					\
60	((ql_first(a_head) != NULL)					\
61	    ? qr_prev(ql_first(a_head), a_field) : NULL)
62
63#define ql_next(a_head, a_elm, a_field)					\
64	((ql_last(a_head, a_field) != (a_elm))				\
65	    ? qr_next((a_elm), a_field)	: NULL)
66
67#define ql_prev(a_head, a_elm, a_field)					\
68	((ql_first(a_head) != (a_elm)) ? qr_prev((a_elm), a_field)	\
69				       : NULL)
70
71#define ql_before_insert(a_head, a_qlelm, a_elm, a_field) do {		\
72	qr_before_insert((a_qlelm), (a_elm), a_field);			\
73	if (ql_first(a_head) == (a_qlelm)) {				\
74		ql_first(a_head) = (a_elm);				\
75	}								\
76} while (0)
77
78#define ql_after_insert(a_qlelm, a_elm, a_field)			\
79	qr_after_insert((a_qlelm), (a_elm), a_field)
80
81#define ql_head_insert(a_head, a_elm, a_field) do {			\
82	if (ql_first(a_head) != NULL) {					\
83		qr_before_insert(ql_first(a_head), (a_elm), a_field);	\
84	}								\
85	ql_first(a_head) = (a_elm);					\
86} while (0)
87
88#define ql_tail_insert(a_head, a_elm, a_field) do {			\
89	if (ql_first(a_head) != NULL) {					\
90		qr_before_insert(ql_first(a_head), (a_elm), a_field);	\
91	}								\
92	ql_first(a_head) = qr_next((a_elm), a_field);			\
93} while (0)
94
95#define ql_remove(a_head, a_elm, a_field) do {				\
96	if (ql_first(a_head) == (a_elm)) {				\
97		ql_first(a_head) = qr_next(ql_first(a_head), a_field);	\
98	}								\
99	if (ql_first(a_head) != (a_elm)) {				\
100		qr_remove((a_elm), a_field);				\
101	} else {							\
102		ql_first(a_head) = NULL;				\
103	}								\
104} while (0)
105
106#define ql_head_remove(a_head, a_type, a_field) do {			\
107	a_type *t = ql_first(a_head);					\
108	ql_remove((a_head), t, a_field);				\
109} while (0)
110
111#define ql_tail_remove(a_head, a_type, a_field) do {			\
112	a_type *t = ql_last(a_head, a_field);				\
113	ql_remove((a_head), t, a_field);				\
114} while (0)
115
116#define ql_foreach(a_var, a_head, a_field)				\
117	qr_foreach((a_var), ql_first(a_head), a_field)
118
119#define ql_reverse_foreach(a_var, a_head, a_field)			\
120	qr_reverse_foreach((a_var), ql_first(a_head), a_field)
121
122#endif /* QL_H_ */
123