1 /* Linux kernel style list handling function
3 * Written from scratch by Timo Teräs <timo.teras@iki.fi>, but modeled
4 * after the linux kernel code.
6 * This file is free software: you may copy, redistribute and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
20 #ifdef __compiler_offsetof
21 #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
23 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
28 #define container_of(ptr, type, member) ({ \
29 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
30 (type *)( (char *)__mptr - offsetof(type,member) );})
34 struct hlist_node *first;
38 struct hlist_node *next;
39 struct hlist_node **pprev;
42 static inline int hlist_empty(const struct hlist_head *h)
47 static inline int hlist_hashed(const struct hlist_node *n)
49 return n->pprev != NULL;
52 static inline void hlist_del(struct hlist_node *n)
54 struct hlist_node *next = n->next;
55 struct hlist_node **pprev = n->pprev;
65 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
67 struct hlist_node *first = h->first;
71 first->pprev = &n->next;
76 static inline void hlist_add_after(struct hlist_node *n, struct hlist_node *prev)
79 n->pprev = &prev->next;
83 static inline struct hlist_node **hlist_tail_ptr(struct hlist_head *h)
85 struct hlist_node *n = h->first;
88 while (n->next != NULL)
93 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
95 #define hlist_for_each(pos, head) \
96 for (pos = (head)->first; pos; pos = pos->next)
98 #define hlist_for_each_safe(pos, n, head) \
99 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); pos = n)
101 #define hlist_for_each_entry(tpos, pos, head, member) \
102 for (pos = (head)->first; pos && \
103 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
106 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
107 for (pos = (head)->first; \
108 pos && ({ n = pos->next; 1; }) && \
109 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
114 struct list_head *next, *prev;
117 #define LIST_INITIALIZER(l) { .next = &l, .prev = &l }
119 static inline void list_init(struct list_head *list)
125 static inline void __list_add(struct list_head *new,
126 struct list_head *prev,
127 struct list_head *next)
135 static inline void list_add(struct list_head *new, struct list_head *head)
137 __list_add(new, head, head->next);
140 static inline void list_add_tail(struct list_head *new, struct list_head *head)
142 __list_add(new, head->prev, head);
145 static inline void __list_del(struct list_head * prev, struct list_head * next)
151 static inline void list_del(struct list_head *entry)
153 __list_del(entry->prev, entry->next);
158 static inline int list_hashed(const struct list_head *n)
160 return n->next != n && n->next != NULL;
163 static inline int list_empty(const struct list_head *n)
165 return !list_hashed(n);
168 #define list_next(ptr, type, member) \
169 (list_hashed(ptr) ? container_of((ptr)->next,type,member) : NULL)
171 #define list_entry(ptr, type, member) container_of(ptr,type,member)
173 #define list_for_each(pos, head) \
174 for (pos = (head)->next; pos != (head); pos = pos->next)
176 #define list_for_each_safe(pos, n, head) \
177 for (pos = (head)->next, n = pos->next; pos != (head); \
178 pos = n, n = pos->next)
180 #define list_for_each_entry(pos, head, member) \
181 for (pos = list_entry((head)->next, typeof(*pos), member); \
182 &pos->member != (head); \
183 pos = list_entry(pos->member.next, typeof(*pos), member))
185 #define list_for_each_entry_safe(pos, n, head, member) \
186 for (pos = list_entry((head)->next, typeof(*pos), member), \
187 n = list_entry(pos->member.next, typeof(*pos), member); \
188 &pos->member != (head); \
189 pos = n, n = list_entry(n->member.next, typeof(*n), member))