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