1 /**************************************************************************
3 * Copyright 2006 VMware, Inc., Bismarck, ND. USA.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 **************************************************************************/
30 * List macros heavily inspired by the Linux kernel
31 * list handling. No list looping yet.
33 * Is not threadsafe, so common operations need to
34 * be protected using an external mutex.
44 #include "c99_compat.h"
47 # define list_assert(cond, msg) assert(cond && msg)
49 # define list_assert(cond, msg) (void)(0 && (cond))
54 struct list_head
*prev
;
55 struct list_head
*next
;
58 static inline void list_inithead(struct list_head
*item
)
64 static inline void list_add(struct list_head
*item
, struct list_head
*list
)
67 item
->next
= list
->next
;
68 list
->next
->prev
= item
;
72 static inline void list_addtail(struct list_head
*item
, struct list_head
*list
)
75 item
->prev
= list
->prev
;
76 list
->prev
->next
= item
;
80 static inline bool list_is_empty(const struct list_head
*list
);
82 static inline void list_replace(struct list_head
*from
, struct list_head
*to
)
84 if (list_is_empty(from
)) {
87 to
->prev
= from
->prev
;
88 to
->next
= from
->next
;
89 from
->next
->prev
= to
;
90 from
->prev
->next
= to
;
94 static inline void list_del(struct list_head
*item
)
96 item
->prev
->next
= item
->next
;
97 item
->next
->prev
= item
->prev
;
98 item
->prev
= item
->next
= NULL
;
101 static inline void list_delinit(struct list_head
*item
)
103 item
->prev
->next
= item
->next
;
104 item
->next
->prev
= item
->prev
;
109 static inline bool list_is_empty(const struct list_head
*list
)
111 return list
->next
== list
;
115 * Returns whether the list has exactly one element.
117 static inline bool list_is_singular(const struct list_head
*list
)
119 return list
->next
!= NULL
&& list
->next
!= list
&& list
->next
->next
== list
;
122 static inline unsigned list_length(const struct list_head
*list
)
124 struct list_head
*node
;
126 for (node
= list
->next
; node
!= list
; node
= node
->next
)
131 static inline void list_splice(struct list_head
*src
, struct list_head
*dst
)
133 if (list_is_empty(src
))
136 src
->next
->prev
= dst
;
137 src
->prev
->next
= dst
->next
;
138 dst
->next
->prev
= src
->prev
;
139 dst
->next
= src
->next
;
142 static inline void list_splicetail(struct list_head
*src
, struct list_head
*dst
)
144 if (list_is_empty(src
))
147 src
->prev
->next
= dst
;
148 src
->next
->prev
= dst
->prev
;
149 dst
->prev
->next
= src
->next
;
150 dst
->prev
= src
->prev
;
153 static inline void list_validate(const struct list_head
*list
)
155 struct list_head
*node
;
156 assert(list
->next
->prev
== list
&& list
->prev
->next
== list
);
157 for (node
= list
->next
; node
!= list
; node
= node
->next
)
158 assert(node
->next
->prev
== node
&& node
->prev
->next
== node
);
161 #define LIST_ENTRY(__type, __item, __field) \
162 ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
165 * Cast from a pointer to a member of a struct back to the containing struct.
167 * 'sample' MUST be initialized, or else the result is undefined!
170 #define container_of(ptr, sample, member) \
171 (void *)((char *)(ptr) \
172 - ((char *)&(sample)->member - (char *)(sample)))
175 #define list_first_entry(ptr, type, member) \
176 LIST_ENTRY(type, (ptr)->next, member)
178 #define list_last_entry(ptr, type, member) \
179 LIST_ENTRY(type, (ptr)->prev, member)
182 #define LIST_FOR_EACH_ENTRY(pos, head, member) \
183 for (pos = NULL, pos = container_of((head)->next, pos, member); \
184 &pos->member != (head); \
185 pos = container_of(pos->member.next, pos, member))
187 #define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member) \
188 for (pos = NULL, pos = container_of((head)->next, pos, member), \
189 storage = container_of(pos->member.next, pos, member); \
190 &pos->member != (head); \
191 pos = storage, storage = container_of(storage->member.next, storage, member))
193 #define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member) \
194 for (pos = NULL, pos = container_of((head)->prev, pos, member), \
195 storage = container_of(pos->member.prev, pos, member); \
196 &pos->member != (head); \
197 pos = storage, storage = container_of(storage->member.prev, storage, member))
199 #define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member) \
200 for (pos = NULL, pos = container_of((start), pos, member); \
201 &pos->member != (head); \
202 pos = container_of(pos->member.next, pos, member))
204 #define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member) \
205 for (pos = NULL, pos = container_of((start), pos, member); \
206 &pos->member != (head); \
207 pos = container_of(pos->member.prev, pos, member))
209 #define list_for_each_entry(type, pos, head, member) \
210 for (type *pos = LIST_ENTRY(type, (head)->next, member), \
211 *__next = LIST_ENTRY(type, pos->member.next, member); \
212 &pos->member != (head); \
213 pos = LIST_ENTRY(type, pos->member.next, member), \
214 list_assert(pos == __next, "use _safe iterator"), \
215 __next = LIST_ENTRY(type, __next->member.next, member))
217 #define list_for_each_entry_safe(type, pos, head, member) \
218 for (type *pos = LIST_ENTRY(type, (head)->next, member), \
219 *__next = LIST_ENTRY(type, pos->member.next, member); \
220 &pos->member != (head); \
222 __next = LIST_ENTRY(type, __next->member.next, member))
224 #define list_for_each_entry_rev(type, pos, head, member) \
225 for (type *pos = LIST_ENTRY(type, (head)->prev, member), \
226 *__prev = LIST_ENTRY(type, pos->member.prev, member); \
227 &pos->member != (head); \
228 pos = LIST_ENTRY(type, pos->member.prev, member), \
229 list_assert(pos == __prev, "use _safe iterator"), \
230 __prev = LIST_ENTRY(type, __prev->member.prev, member))
232 #define list_for_each_entry_safe_rev(type, pos, head, member) \
233 for (type *pos = LIST_ENTRY(type, (head)->prev, member), \
234 *__prev = LIST_ENTRY(type, pos->member.prev, member); \
235 &pos->member != (head); \
237 __prev = LIST_ENTRY(type, __prev->member.prev, member))
239 #define list_for_each_entry_from(type, pos, start, head, member) \
240 for (type *pos = LIST_ENTRY(type, (start), member); \
241 &pos->member != (head); \
242 pos = LIST_ENTRY(type, pos->member.next, member))
244 #define list_for_each_entry_from_rev(type, pos, start, head, member) \
245 for (type *pos = LIST_ENTRY(type, (start), member); \
246 &pos->member != (head); \
247 pos = LIST_ENTRY(type, pos->member.prev, member))
249 #endif /*_UTIL_LIST_H_*/