-
Notifications
You must be signed in to change notification settings - Fork 19
/
queue.h
196 lines (157 loc) · 6.88 KB
/
queue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) 2015 Nuxi, https://nuxi.nl/
//
// SPDX-License-Identifier: BSD-2-Clause
#ifndef COMMON_QUEUE_H
#define COMMON_QUEUE_H
#include <stddef.h>
// SLIST: Single-linked list.
#define SLIST_HEAD(name, type) \
struct name { \
struct type *sl_first; \
}
#define SLIST_HEAD_INITIALIZER(head) \
{ NULL }
#define SLIST_ENTRY(type) \
struct { \
struct type *sl_next; \
}
#define SLIST_EMPTY(head) ((head)->sl_first == NULL)
#define SLIST_FIRST(head) ((head)->sl_first)
#define SLIST_FOREACH(var, head, field) \
for ((var) = (head)->sl_first; (var) != NULL; (var) = (var)->field.sl_next)
#define SLIST_FOREACH_PREVPTR(var, varprev, head, field) \
for ((var) = (head)->sl_first, (varprev) = &(head)->sl_first; (var) != NULL; \
(varprev) = *(varprev) == (var) ? &(var)->field.sl_next : (varprev), \
(var) = *(varprev))
#define SLIST_FOREACH_SAFE(var, head, field, tmpvar) \
for ((var) = (head)->sl_first, \
(tmpvar) = (var) != NULL ? (var)->field.sl_next : NULL; \
(var) != NULL; (var) = (tmpvar), \
(tmpvar) = (var) != NULL ? (var)->field.sl_next : NULL)
#define SLIST_INIT(head) \
do { \
(head)->sl_first = NULL; \
} while (0)
#define SLIST_INSERT_AFTER(fieldelm, elm, field) \
do { \
(elm)->field.sl_next = (fieldelm)->field.sl_next; \
(fieldelm)->field.sl_next = (elm); \
} while (0)
#define SLIST_INSERT_HEAD(head, elm, field) \
do { \
(elm)->field.sl_next = (head)->sl_first; \
(head)->sl_first = (elm); \
} while (0)
#define SLIST_REMOVE_PREVPTR(varprev, field) \
do { \
*(varprev) = (*(varprev))->field.sl_next; \
} while (0)
#define SLIST_REMOVE_HEAD(head, field) \
do { \
(head)->sl_first = (head)->sl_first->field.sl_next; \
} while (0)
#define SLIST_SWAP(head1, head2, type) \
do { \
struct type *tmp = (head1)->sl_first; \
(head1)->sl_first = (head2)->sl_first; \
(head2)->sl_first = tmp; \
} while (0)
// STAILQ: Single-linked list with tail pointer.
#define STAILQ_HEAD(name, type) \
struct name { \
struct type *st_first; \
struct type **st_last; \
}
#define STAILQ_ENTRY(type) \
struct { \
struct type *st_next; \
}
#define STAILQ_EMPTY(head) ((head)->st_first == NULL)
#define STAILQ_FIRST(head) ((head)->st_first)
#define STAILQ_INSERT_HEAD(head, elm, field) \
do { \
if ((head)->st_first == NULL) \
(head)->st_last = &(elm)->field.st_next; \
(elm)->field.st_next = (head)->st_first; \
(head)->st_first = (elm); \
} while (0)
#define STAILQ_INSERT_TAIL(head, elm, field) \
do { \
(elm)->field.st_next = NULL; \
*(head)->st_last = (elm); \
(head)->st_last = &(elm)->field.st_next; \
} while (0)
#define STAILQ_FOREACH_SAFE(var, head, field, tmpvar) \
for ((var) = (head)->st_first, \
(tmpvar) = (var) != NULL ? (var)->field.st_next : NULL; \
(var) != NULL; (var) = (tmpvar), \
(tmpvar) = (var) != NULL ? (var)->field.st_next : NULL)
#define STAILQ_INIT(head) \
do { \
(head)->st_first = NULL; \
(head)->st_last = &(head)->st_first; \
} while (0)
#define STAILQ_REMOVE_HEAD(head, field) \
do { \
(head)->st_first = (head)->st_first->field.st_next; \
if ((head)->st_first == NULL) \
(head)->st_last = &(head)->st_first; \
} while (0)
#define STAILQ_SWAP(head1, head2, type) \
do { \
struct type *tmp_first = (head1)->st_first; \
(head1)->st_first = (head2)->st_first; \
(head2)->st_first = tmp_first; \
struct type **tmp_last = (head1)->st_last; \
(head1)->st_last = (head2)->st_last; \
(head2)->st_last = tmp_last; \
if ((head1)->st_first == NULL) \
(head1)->st_last = &(head1)->st_first; \
if ((head2)->st_first == NULL) \
(head2)->st_last = &(head2)->st_first; \
} while (0)
// LIST: Double-linked list.
#define LIST_HEAD(name, type) \
struct name { \
struct type *l_first; \
}
#define LIST_HEAD_INITIALIZER(head) \
{ NULL }
#define LIST_ENTRY(type) \
struct { \
struct type *__l_next; \
struct type **__l_prev; \
}
#define LIST_EMPTY(head) ((head)->l_first == NULL)
#define LIST_INSERT_HEAD(head, element, field) \
do { \
(element)->field.__l_next = (head)->l_first; \
if ((head)->l_first != NULL) \
(head)->l_first->field.__l_prev = &(element)->field.__l_next; \
(head)->l_first = (element); \
(element)->field.__l_prev = &(head)->l_first; \
} while (0)
#define LIST_FOREACH(var, head, field) \
for ((var) = (head)->l_first; (var) != NULL; (var) = (var)->field.__l_next)
#define LIST_FOREACH_SAFE(var, head, field, tmpvar) \
for ((var) = (head)->l_first, \
(tmpvar) = (var) != NULL ? (var)->field.__l_next : NULL; \
(var) != NULL; (var) = (tmpvar), \
(tmpvar) = (var) != NULL ? (var)->field.__l_next : NULL)
#define LIST_REMOVE(element, field) \
do { \
if ((element)->field.__l_next != NULL) \
(element)->field.__l_next->field.__l_prev = (element)->field.__l_prev; \
*(element)->field.__l_prev = (element)->field.__l_next; \
} while (0)
#define LIST_SWAP(head1, head2, type, field) \
do { \
struct type *tmp = (head1)->l_first; \
(head1)->l_first = (head2)->l_first; \
if ((head1)->l_first != NULL) \
(head1)->l_first->field.__l_prev = &(head1)->l_first; \
(head2)->l_first = tmp; \
if ((head2)->l_first != NULL) \
(head2)->l_first->field.__l_prev = &(head2)->l_first; \
} while (0)
#endif