]>
git.proxmox.com Git - mirror_frr.git/blob - nhrpd/list.h
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) \
30 const typeof(((type *)0)->member) *__mptr = (ptr); \
31 (type *)((char *)__mptr - offsetof(type, member)); \
36 struct hlist_node
*first
;
40 struct hlist_node
*next
;
41 struct hlist_node
**pprev
;
44 static inline int hlist_empty(const struct hlist_head
*h
)
49 static inline int hlist_hashed(const struct hlist_node
*n
)
51 return n
->pprev
!= NULL
;
54 static inline void hlist_del(struct hlist_node
*n
)
56 struct hlist_node
*next
= n
->next
;
57 struct hlist_node
**pprev
= n
->pprev
;
67 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
69 struct hlist_node
*first
= h
->first
;
73 first
->pprev
= &n
->next
;
78 static inline void hlist_add_after(struct hlist_node
*n
,
79 struct hlist_node
*prev
)
82 n
->pprev
= &prev
->next
;
86 static inline struct hlist_node
**hlist_tail_ptr(struct hlist_head
*h
)
88 struct hlist_node
*n
= h
->first
;
91 while (n
->next
!= NULL
)
96 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
98 #define hlist_for_each(pos, head) \
99 for (pos = (head)->first; pos; pos = pos->next)
101 #define hlist_for_each_safe(pos, n, head) \
102 for (pos = (head)->first; pos && ({ \
108 #define hlist_for_each_entry(tpos, pos, head, member) \
109 for (pos = (head)->first; \
111 tpos = hlist_entry(pos, typeof(*tpos), member); \
116 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
117 for (pos = (head)->first; \
123 tpos = hlist_entry(pos, typeof(*tpos), member); \
130 struct list_head
*next
, *prev
;
133 #define LIST_INITIALIZER(l) { .next = &l, .prev = &l }
135 static inline void list_init(struct list_head
*list
)
141 static inline void __list_add(struct list_head
*new, struct list_head
*prev
,
142 struct list_head
*next
)
150 static inline void list_add(struct list_head
*new, struct list_head
*head
)
152 __list_add(new, head
, head
->next
);
155 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
157 __list_add(new, head
->prev
, head
);
160 static inline void __list_del(struct list_head
*prev
, struct list_head
*next
)
166 static inline void list_del(struct list_head
*entry
)
168 __list_del(entry
->prev
, entry
->next
);
173 static inline int list_hashed(const struct list_head
*n
)
175 return n
->next
!= n
&& n
->next
!= NULL
;
178 static inline int list_empty(const struct list_head
*n
)
180 return !list_hashed(n
);
183 #define list_next(ptr, type, member) \
184 (list_hashed(ptr) ? container_of((ptr)->next, type, member) : NULL)
186 #define list_entry(ptr, type, member) container_of(ptr,type,member)
188 #define list_for_each(pos, head) \
189 for (pos = (head)->next; pos != (head); pos = pos->next)
191 #define list_for_each_safe(pos, n, head) \
192 for (pos = (head)->next, n = pos->next; pos != (head); \
193 pos = n, n = pos->next)
195 #define list_for_each_entry(pos, head, member) \
196 for (pos = list_entry((head)->next, typeof(*pos), member); \
197 &pos->member != (head); \
198 pos = list_entry(pos->member.next, typeof(*pos), member))
200 #define list_for_each_entry_safe(pos, n, head, member) \
201 for (pos = ((head)->next != head ? \
202 list_entry((head)->next, typeof(*pos), member) : \
205 list_entry(pos->member.next, typeof(*pos), member) : NULL); \
206 pos && (&pos->member != (head)); \
207 pos = n, n = list_entry(n->member.next, typeof(*n), member))