-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.h
169 lines (138 loc) · 3.38 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
#ifndef _LIST_H
#define _LIST_H
#ifndef _MSC_VER
#include <stddef.h>
#endif
#ifndef offsetof
#define offsetof(type, member) ((size_t) &((type *)0)->member)
#endif
#ifndef container_of
#define container_of(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))
#endif
struct list {
struct list *next, *prev;
};
#define LIST_INIT(name) { &(name), &(name) }
#define LIST(name) \
struct list name = LIST_INIT(name)
#define list_entry(ptr, type, member) \
((type *)( (char *)(ptr) - offsetof(type, member)))
static inline void init_list(struct list *list)
{
list->next = list;
list->prev = list;
}
static inline void __list_add(struct list *list,
struct list *prev,
struct list *next)
{
next->prev = list;
list->next = next;
list->prev = prev;
prev->next = list;
}
static inline void list_add(struct list *list,
struct list *head)
{
__list_add(list, head, head->next);
}
static inline void list_add_tail(struct list *list,
struct list *head)
{
__list_add(list, head->prev, head);
}
static inline void __list_del(struct list *prev,
struct list *next)
{
next->prev = prev;
prev->next = next;
}
static inline void list_del(struct list *list)
{
__list_del(list->prev, list->next);
list->prev = NULL;
list->next = NULL;
}
static inline void list_del_init(struct list *entry)
{
__list_del(entry->prev, entry->next);
init_list(entry);
}
static inline void list_move(struct list *list, struct list *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
static inline void list_move_tail(struct list *list, struct list *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
static inline int list_empty(struct list *head)
{
return head->next == head;
}
#define list_for_each(ptr, head) \
for((ptr) = (head)->next; (ptr) != (head); (ptr) = (ptr)->next)
#define list_for_each_safe(pos, n, head) \
for(pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* hlist from kernel
*/
struct hlist_node {
struct hlist_node *next, **pprev;
};
struct hlist {
struct hlist_node *first;
};
#define HLIST_INIT { .first = NULL }
#define HLIST(name) struct hlist name = HLIST_INIT
static inline void init_hlist(struct hlist *h)
{
h->first = NULL;
}
static inline void init_hlist_node(struct hlist_node *n)
{
n->next = NULL;
n->pprev = NULL;
}
static inline int hlist_unhashed(const struct hlist_node *n)
{
return !n->pprev;
}
static inline int hlist_empty(const struct hlist *h)
{
return !h->first;
}
static inline void hlist_del(struct hlist_node *n)
{
struct hlist_node *next = n->next;
struct hlist_node **pprev = n->pprev;
*pprev = next;
if (next)
next->pprev = pprev;
}
static inline void hlist_del_init(struct hlist_node *n)
{
if (!hlist_unhashed(n)) {
hlist_del(n);
init_hlist_node(n);
}
}
static inline void hlist_add_head(struct hlist_node *n, struct hlist *h)
{
struct hlist_node *first = h->first;
n->next = first;
if (first)
first->pprev = &n->next;
h->first = n;
n->pprev = &h->first;
}
#define hlist_entry(ptr, type, member) container_of(ptr, type, member)
#define hlist_for_each(pos, head) \
for (pos = (head)->first; pos; pos = pos->next)
#define hlist_for_each_safe(pos, n, head) \
for (pos = (head)->first; pos && (n = pos->next, 1); pos = n)
#endif