1 #ifndef __LIST_H__
2 #define __LIST_H__ 1
3 /* List and hash list stuff from kernel */
4
5 #include <stddef.h>
6
7 #define container_of(ptr, type, member) ({ \
8 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
9 (type *)( (char *)__mptr - offsetof(type,member) );})
10
11 struct list_head {
12 struct list_head *next, *prev;
13 };
14
INIT_LIST_HEAD(struct list_head * list)15 static inline void INIT_LIST_HEAD(struct list_head *list)
16 {
17 list->next = list;
18 list->prev = list;
19 }
20
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)21 static inline void __list_add(struct list_head *new,
22 struct list_head *prev,
23 struct list_head *next)
24 {
25 next->prev = new;
26 new->next = next;
27 new->prev = prev;
28 prev->next = new;
29 }
30
list_add(struct list_head * new,struct list_head * head)31 static inline void list_add(struct list_head *new, struct list_head *head)
32 {
33 __list_add(new, head, head->next);
34 }
35
list_add_tail(struct list_head * new,struct list_head * head)36 static inline void list_add_tail(struct list_head *new, struct list_head *head)
37 {
38 __list_add(new, head->prev, head);
39 }
40
__list_del(struct list_head * prev,struct list_head * next)41 static inline void __list_del(struct list_head *prev, struct list_head *next)
42 {
43 next->prev = prev;
44 prev->next = next;
45 }
46
list_del(struct list_head * entry)47 static inline void list_del(struct list_head *entry)
48 {
49 __list_del(entry->prev, entry->next);
50 }
51
52 #define list_entry(ptr, type, member) \
53 container_of(ptr, type, member)
54
55 #define list_first_entry(ptr, type, member) \
56 list_entry((ptr)->next, type, member)
57
58 #define list_last_entry(ptr, type, member) \
59 list_entry((ptr)->prev, type, member)
60
61 #define list_next_entry(pos, member) \
62 list_entry((pos)->member.next, typeof(*(pos)), member)
63
64 #define list_prev_entry(pos, member) \
65 list_entry((pos)->member.prev, typeof(*(pos)), member)
66
67 #define list_for_each_entry(pos, head, member) \
68 for (pos = list_first_entry(head, typeof(*pos), member); \
69 &pos->member != (head); \
70 pos = list_next_entry(pos, member))
71
72 #define list_for_each_entry_safe(pos, n, head, member) \
73 for (pos = list_first_entry(head, typeof(*pos), member), \
74 n = list_next_entry(pos, member); \
75 &pos->member != (head); \
76 pos = n, n = list_next_entry(n, member))
77
78 #define list_for_each_entry_reverse(pos, head, member) \
79 for (pos = list_last_entry(head, typeof(*pos), member); \
80 &pos->member != (head); \
81 pos = list_prev_entry(pos, member))
82
83 struct hlist_head {
84 struct hlist_node *first;
85 };
86
87 struct hlist_node {
88 struct hlist_node *next, **pprev;
89 };
90
hlist_del(struct hlist_node * n)91 static inline void hlist_del(struct hlist_node *n)
92 {
93 struct hlist_node *next = n->next;
94 struct hlist_node **pprev = n->pprev;
95 *pprev = next;
96 if (next)
97 next->pprev = pprev;
98 }
99
hlist_add_head(struct hlist_node * n,struct hlist_head * h)100 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
101 {
102 struct hlist_node *first = h->first;
103 n->next = first;
104 if (first)
105 first->pprev = &n->next;
106 h->first = n;
107 n->pprev = &h->first;
108 }
109
110 #define hlist_for_each(pos, head) \
111 for (pos = (head)->first; pos ; pos = pos->next)
112
113
114 #define hlist_for_each_safe(pos, n, head) \
115 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
116 pos = n)
117
118 #define hlist_entry_safe(ptr, type, member) \
119 ({ typeof(ptr) ____ptr = (ptr); \
120 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
121 })
122
123 #define hlist_for_each_entry(pos, head, member) \
124 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
125 pos; \
126 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
127
128 #endif /* __LIST_H__ */
129