1
2#ifndef LIST_H
3#define LIST_H
4
5
6
7
8
9#undef offsetof
10#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
11
12
13
14
15
16
17
18
19#define container_of(ptr, type, member) ({ \
20 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
21 (type *)( (char *)__mptr - offsetof(type,member) );})
22
23
24struct list_head {
25 struct list_head *next, *prev;
26};
27
28
29#define LIST_HEAD_INIT(name) { &(name), &(name) }
30
31#define LIST_HEAD(name) \
32 struct list_head name = LIST_HEAD_INIT(name)
33
34
35
36
37
38
39
40#define list_entry(ptr, type, member) \
41 container_of(ptr, type, member)
42
43
44
45
46
47
48
49#define list_for_each_entry(pos, head, member) \
50 for (pos = list_entry((head)->next, typeof(*pos), member); \
51 &pos->member != (head); \
52 pos = list_entry(pos->member.next, typeof(*pos), member))
53
54
55
56
57
58
59
60
61#define list_for_each_entry_safe(pos, n, head, member) \
62 for (pos = list_entry((head)->next, typeof(*pos), member), \
63 n = list_entry(pos->member.next, typeof(*pos), member); \
64 &pos->member != (head); \
65 pos = n, n = list_entry(n->member.next, typeof(*n), member))
66
67
68
69
70
71static inline int list_empty(const struct list_head *head)
72{
73 return head->next == head;
74}
75
76
77
78
79
80
81
82static inline void __list_add(struct list_head *_new,
83 struct list_head *prev,
84 struct list_head *next)
85{
86 next->prev = _new;
87 _new->next = next;
88 _new->prev = prev;
89 prev->next = _new;
90}
91
92
93
94
95
96
97
98
99
100static inline void list_add_tail(struct list_head *_new, struct list_head *head)
101{
102 __list_add(_new, head->prev, head);
103}
104
105
106
107
108
109
110
111
112static inline void __list_del(struct list_head *prev, struct list_head *next)
113{
114 next->prev = prev;
115 prev->next = next;
116}
117
118#define LIST_POISON1 ((void *) 0x00100100)
119#define LIST_POISON2 ((void *) 0x00200200)
120
121
122
123
124
125
126static inline void list_del(struct list_head *entry)
127{
128 __list_del(entry->prev, entry->next);
129 entry->next = (struct list_head*)LIST_POISON1;
130 entry->prev = (struct list_head*)LIST_POISON2;
131}
132#endif
133