1
2#define POISON_POINTER_DELTA 0
3#define LIST_POISON1  ((void *) (0x00100100 + POISON_POINTER_DELTA))
4#define LIST_POISON2  ((void *) (0x00200200 + POISON_POINTER_DELTA))
5
6#if !defined(offsetof)
7#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
8#endif
9#define container_of(ptr, type, member) ({                      \
10		const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
11		(type *)( (char *)__mptr - offsetof(type,member) );})
12
13struct list_head {
14	struct list_head *next, *prev;
15};
16
17#define LIST_HEAD_INIT(name) { &(name), &(name) }
18
19#define LIST_HEAD(name) \
20	struct list_head name = LIST_HEAD_INIT(name)
21
22static inline void INIT_LIST_HEAD(struct list_head *list)
23{
24	list->next = list;
25	list->prev = list;
26}
27
28static inline void __list_add(struct list_head *new,
29		struct list_head *prev,
30		struct list_head *next)
31{
32	next->prev = new;
33	new->next = next;
34	new->prev = prev;
35	prev->next = new;
36}
37
38static inline void list_add(struct list_head *new, struct list_head *head)
39{
40	__list_add(new, head, head->next);
41}
42
43static inline void list_add_tail(struct list_head *new, struct list_head *head)
44{
45	__list_add(new, head->prev, head);
46}
47
48static inline void __list_del(struct list_head * prev, struct list_head * next)
49{
50	next->prev = prev;
51	prev->next = next;
52}
53
54static inline void __list_del_entry(struct list_head *entry)
55{
56	__list_del(entry->prev, entry->next);
57}
58
59static inline void list_del(struct list_head *entry)
60{
61	__list_del(entry->prev, entry->next);
62	entry->next = LIST_POISON1;
63	entry->prev = LIST_POISON2;
64}
65
66static inline int list_empty(const struct list_head *head)
67{
68	return head->next == head;
69}
70
71#define list_entry(ptr, type, member) \
72	container_of(ptr, type, member)
73
74#define list_for_each(pos, head) \
75	for (pos = (head)->next; pos != (head); pos = pos->next)
76
77#define list_for_each_safe(pos, n, head) \
78	for (pos = (head)->next, n = pos->next; pos != (head); \
79			pos = n, n = pos->next)
80#define list_for_each_entry(pos, head, member)                          \
81	for (pos = list_entry((head)->next, typeof(*pos), member);      \
82			&pos->member != (head);    \
83			pos = list_entry(pos->member.next, typeof(*pos), member))
84#define list_for_each_entry_safe(pos, n, head, member)                  \
85	for (pos = list_entry((head)->next, typeof(*pos), member),      \
86			n = list_entry(pos->member.next, typeof(*pos), member); \
87			&pos->member != (head);                                    \
88			pos = n, n = list_entry(n->member.next, typeof(*n), member))
89