-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.h
271 lines (211 loc) · 7.31 KB
/
list.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H
#include <stdio.h>
#include <stdlib.h>
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t)(&((TYPE *)0)->MEMBER))
#endif
#ifndef container_of
#define container_of(ptr, type, member) ({ \
const typeof(*ptr) *__ptr = (ptr); \
(type *) ((char*)__ptr - offsetof(type, member));})
#endif
#define LIST_POISON1 ((void *) 0x00100100)
#define LIST_POISON2 ((void *) 0x00200200)
static inline void prefetch(const void *x) {;}
static inline void prefetchw(const void *x) {;}
struct list_head {
struct list_head *prev, *next;
};
#define LIST_HEAD_INIT(name) {&(name), &(name)}
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name);
#define INIT_LIST_HEAD(ptr) do {\
(ptr)->prev = (ptr); (ptr)->next = (ptr); \
} while (0)
static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
{
new->prev = prev;
prev->next = new;
next->prev = new;
new->next = next;
}
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
static inline void __list_del(struct list_head *prev, struct list_head *next)
{
prev->next = next;
next->prev = prev;
}
static inline void list_del(struct list_head *pos)
{
__list_del(pos->prev, pos->next);
}
static inline void list_del_init(struct list_head *pos)
{
__list_del(pos->prev, pos->next);
pos->prev = LIST_POISON1;
pos->next = LIST_POISON2;
}
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
static inline void list_move_tail(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
static inline int list_empty(struct list_head *head)
{
return head->next == head;
}
static inline int list_empty_careful(struct list_head *head)
{
return (head->next == head) && (head->next == head->prev);
}
static inline void __list_splice(struct list_head *list, struct list_head *head)
{
head->next->prev = list->prev;
list->prev->next = head->next;
head->next = list->next;
list->next->prev = head;
}
static inline void list_splice(struct list_head *list, struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head);
}
static inline void list_splice_init(struct list_head *list, struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head);
INIT_LIST_HEAD(list);
}
}
#define list_entry(ptr, type, member) container_of(ptr, type, member)
#define __list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
#define list_for_each(pos, head) \
for (pos = (head)->next; prefetch(pos->next); pos != (head); pos = pos->next)
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)
#define list_for_each_safe(pos, temp, head) \
for (pos = (head)->next, temp = pos->next; pos != (head); pos = temp, temp = pos->next)
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member);\
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
#define list_for_each_entry_index(pos, index, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), index = 0;\
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member), index++)
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_entry((head)->prev, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.prev, typeof(*pos), member))
#define list_prepare_entry(pos, head, member) \
((pos) ? : list_entry(head, typeof(*pos), member))
#define list_for_each_entry_continue(pos, head, member) \
for (pos = list_entry((pos)->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
#define list_for_each_entry_safe(pos, temp, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), temp = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = temp, temp = list_entry(temp->member.next, typeof(*temp), member))
#endif
struct hlist_node {
struct hlist_node *next, **pprev;
};
struct hlist_head {
struct hlist_node *first;
};
#define HLIST_HEAD_INIT {.first = NULL}
#define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
#define INIT_HLIST_NODE(node) ((node)->next = NULL, (node)->pprev = NULL)
static inline int hlist_unhashed(const struct hlist_node *h)
{
return !h->pprev;
}
static inline int hlist_empty(const struct hlist_head *head)
{
return !head->first;
}
/* hlist must not be empty */
static inline void __hlist_del(struct hlist_node *node)
{
*node->pprev = node->next;
if (node->next)
node->next->pprev = node->pprev;
}
static inline void hlist_del(struct hlist_node *node)
{
__hlist_del(node);
node->next = LIST_POISON1;
node->pprev = LIST_POISON2;
}
static inline void hlist_del_init(struct hlist_node *node)
{
__hlist_del(node);
INIT_HLIST_NODE(node);
}
/* head must not be null */
static inline void hlist_add_head(struct hlist_node *node, struct hlist_head *head)
{
struct hlist_node *first = head->first;
head->first = node;
node->pprev = &head->first;
node->next = first;
if (first)
first->pprev = &node->next;
}
static inline void hlist_add_before(struct hlist_node *node, struct hlist_node *next)
{
struct hlist_node **pprev = next->pprev;
*pprev = node;
node->pprev = pprev;
node->next = next;
next->pprev = &node->next;
}
static inline void hlist_add_after(struct hlist_node *node, struct hlist_node *next)
{
struct hlist_node *after = next->next;
next->next = node;
node->pprev = &next->next;
node->next = after;
if (after)
after->pprev = &node->next;
}
#define hlist_entry(ptr, type, member) container_of(ptr, type, member)
#define hlist_for_each(pos, head) \
for (pos = (head)->first; \
pos && ({prefetch(pos->next); 1;}); \
pos = pos->next)
#define hlist_for_each_safe(pos, n, head) \
for (pos = (head)->first; \
pos && ({n = pos->next; 1;}); \
pos = n)
#define hlist_for_each_entry(tpos, pos, head, member) \
for (pos = (head)->first; \
pos && ({prefetch(pos->next; 1;)}) && ({tops = hlist_entry(pos, typeof(*tpos), member)}; 1;); \
pos = pos->next)
#define hlist_for_each_entry_continue(tpos, pos, member) \
for (pos = (pos)->next; \
pos && ({tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = pos->next)
#define hlist_for_each_entry_from(tpos, pos, member) \
for (; pos && ({tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = pos->next)
#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
for (pos = (head)->first; \
pos && ({n = pos->next; 1;}) && ({tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = n)