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.
48 struct list_head
*prev
;
49 struct list_head
*next
;
52 static inline void list_inithead(struct list_head
*item
)
58 static inline void list_add(struct list_head
*item
, struct list_head
*list
)
61 item
->next
= list
->next
;
62 list
->next
->prev
= item
;
66 static inline void list_addtail(struct list_head
*item
, struct list_head
*list
)
69 item
->prev
= list
->prev
;
70 list
->prev
->next
= item
;
74 static inline bool list_empty(struct list_head
*list
);
76 static inline void list_replace(struct list_head
*from
, struct list_head
*to
)
78 if (list_empty(from
)) {
81 to
->prev
= from
->prev
;
82 to
->next
= from
->next
;
83 from
->next
->prev
= to
;
84 from
->prev
->next
= to
;
88 static inline void list_del(struct list_head
*item
)
90 item
->prev
->next
= item
->next
;
91 item
->next
->prev
= item
->prev
;
92 item
->prev
= item
->next
= NULL
;
95 static inline void list_delinit(struct list_head
*item
)
97 item
->prev
->next
= item
->next
;
98 item
->next
->prev
= item
->prev
;
103 static inline bool list_empty(struct list_head
*list
)
105 return list
->next
== list
;
109 * Returns whether the list has exactly one element.
111 static inline bool list_is_singular(const struct list_head
*list
)
113 return list
->next
!= NULL
&& list
->next
!= list
&& list
->next
->next
== list
;
116 static inline unsigned list_length(struct list_head
*list
)
118 struct list_head
*node
;
120 for (node
= list
->next
; node
!= list
; node
= node
->next
)
125 static inline void list_splice(struct list_head
*src
, struct list_head
*dst
)
130 src
->next
->prev
= dst
;
131 src
->prev
->next
= dst
->next
;
132 dst
->next
->prev
= src
->prev
;
133 dst
->next
= src
->next
;
136 static inline void list_splicetail(struct list_head
*src
, struct list_head
*dst
)
141 src
->prev
->next
= dst
;
142 src
->next
->prev
= dst
->prev
;
143 dst
->prev
->next
= src
->next
;
144 dst
->prev
= src
->prev
;
147 static inline void list_validate(struct list_head
*list
)
149 struct list_head
*node
;
150 assert(list
->next
->prev
== list
&& list
->prev
->next
== list
);
151 for (node
= list
->next
; node
!= list
; node
= node
->next
)
152 assert(node
->next
->prev
== node
&& node
->prev
->next
== node
);
155 #define LIST_INITHEAD(__item) list_inithead(__item)
156 #define LIST_ADD(__item, __list) list_add(__item, __list)
157 #define LIST_ADDTAIL(__item, __list) list_addtail(__item, __list)
158 #define LIST_REPLACE(__from, __to) list_replace(__from, __to)
159 #define LIST_DEL(__item) list_del(__item)
160 #define LIST_DELINIT(__item) list_delinit(__item)
162 #define LIST_ENTRY(__type, __item, __field) \
163 ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
165 #define LIST_IS_EMPTY(__list) \
166 ((__list)->next == (__list))
169 * Cast from a pointer to a member of a struct back to the containing struct.
171 * 'sample' MUST be initialized, or else the result is undefined!
174 #define container_of(ptr, sample, member) \
175 (void *)((char *)(ptr) \
176 - ((char *)&(sample)->member - (char *)(sample)))
179 #define list_first_entry(ptr, type, member) \
180 LIST_ENTRY(type, (ptr)->next, member)
182 #define list_last_entry(ptr, type, member) \
183 LIST_ENTRY(type, (ptr)->prev, member)
186 #define LIST_FOR_EACH_ENTRY(pos, head, member) \
187 for (pos = NULL, pos = container_of((head)->next, pos, member); \
188 &pos->member != (head); \
189 pos = container_of(pos->member.next, pos, member))
191 #define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member) \
192 for (pos = NULL, pos = container_of((head)->next, pos, member), \
193 storage = container_of(pos->member.next, pos, member); \
194 &pos->member != (head); \
195 pos = storage, storage = container_of(storage->member.next, storage, member))
197 #define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member) \
198 for (pos = NULL, pos = container_of((head)->prev, pos, member), \
199 storage = container_of(pos->member.prev, pos, member); \
200 &pos->member != (head); \
201 pos = storage, storage = container_of(storage->member.prev, storage, member))
203 #define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member) \
204 for (pos = NULL, pos = container_of((start), pos, member); \
205 &pos->member != (head); \
206 pos = container_of(pos->member.next, pos, member))
208 #define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member) \
209 for (pos = NULL, pos = container_of((start), pos, member); \
210 &pos->member != (head); \
211 pos = container_of(pos->member.prev, pos, member))
213 #define list_for_each_entry(type, pos, head, member) \
214 for (type *pos = LIST_ENTRY(type, (head)->next, member); \
215 &pos->member != (head); \
216 pos = LIST_ENTRY(type, pos->member.next, member))
218 #define list_for_each_entry_safe(type, pos, head, member) \
219 for (type *pos = LIST_ENTRY(type, (head)->next, member), \
220 *__next = LIST_ENTRY(type, pos->member.next, member); \
221 &pos->member != (head); \
223 __next = LIST_ENTRY(type, __next->member.next, member))
225 #define list_for_each_entry_rev(type, pos, head, member) \
226 for (type *pos = LIST_ENTRY(type, (head)->prev, member); \
227 &pos->member != (head); \
228 pos = LIST_ENTRY(type, pos->member.prev, member))
230 #define list_for_each_entry_safe_rev(type, pos, head, member) \
231 for (type *pos = LIST_ENTRY(type, (head)->prev, member), \
232 *__prev = LIST_ENTRY(type, pos->member.prev, member); \
233 &pos->member != (head); \
235 __prev = LIST_ENTRY(type, __prev->member.prev, member))
237 #define list_for_each_entry_from(type, pos, start, head, member) \
238 for (type *pos = LIST_ENTRY(type, (start), member); \
239 &pos->member != (head); \
240 pos = LIST_ENTRY(type, pos->member.next, member))
242 #define list_for_each_entry_from_rev(type, pos, start, head, member) \
243 for (type *pos = LIST_ENTRY(type, (start), member); \
244 &pos->member != (head); \
245 pos = LIST_ENTRY(type, pos->member.prev, member))
247 #endif /*_UTIL_LIST_H_*/