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"
49 struct list_head
*prev
;
50 struct list_head
*next
;
53 static inline void list_inithead(struct list_head
*item
)
59 static inline void list_add(struct list_head
*item
, struct list_head
*list
)
62 item
->next
= list
->next
;
63 list
->next
->prev
= item
;
67 static inline void list_addtail(struct list_head
*item
, struct list_head
*list
)
70 item
->prev
= list
->prev
;
71 list
->prev
->next
= item
;
75 static inline bool list_empty(struct list_head
*list
);
77 static inline void list_replace(struct list_head
*from
, struct list_head
*to
)
79 if (list_empty(from
)) {
82 to
->prev
= from
->prev
;
83 to
->next
= from
->next
;
84 from
->next
->prev
= to
;
85 from
->prev
->next
= to
;
89 static inline void list_del(struct list_head
*item
)
91 item
->prev
->next
= item
->next
;
92 item
->next
->prev
= item
->prev
;
93 item
->prev
= item
->next
= NULL
;
96 static inline void list_delinit(struct list_head
*item
)
98 item
->prev
->next
= item
->next
;
99 item
->next
->prev
= item
->prev
;
104 static inline bool list_empty(struct list_head
*list
)
106 return list
->next
== list
;
110 * Returns whether the list has exactly one element.
112 static inline bool list_is_singular(const struct list_head
*list
)
114 return list
->next
!= NULL
&& list
->next
!= list
&& list
->next
->next
== list
;
117 static inline unsigned list_length(struct list_head
*list
)
119 struct list_head
*node
;
121 for (node
= list
->next
; node
!= list
; node
= node
->next
)
126 static inline void list_splice(struct list_head
*src
, struct list_head
*dst
)
131 src
->next
->prev
= dst
;
132 src
->prev
->next
= dst
->next
;
133 dst
->next
->prev
= src
->prev
;
134 dst
->next
= src
->next
;
137 static inline void list_splicetail(struct list_head
*src
, struct list_head
*dst
)
142 src
->prev
->next
= dst
;
143 src
->next
->prev
= dst
->prev
;
144 dst
->prev
->next
= src
->next
;
145 dst
->prev
= src
->prev
;
148 static inline void list_validate(struct list_head
*list
)
150 struct list_head
*node
;
151 assert(list
->next
->prev
== list
&& list
->prev
->next
== list
);
152 for (node
= list
->next
; node
!= list
; node
= node
->next
)
153 assert(node
->next
->prev
== node
&& node
->prev
->next
== node
);
156 #define LIST_INITHEAD(__item) list_inithead(__item)
157 #define LIST_ADD(__item, __list) list_add(__item, __list)
158 #define LIST_ADDTAIL(__item, __list) list_addtail(__item, __list)
159 #define LIST_REPLACE(__from, __to) list_replace(__from, __to)
160 #define LIST_DEL(__item) list_del(__item)
161 #define LIST_DELINIT(__item) list_delinit(__item)
163 #define LIST_ENTRY(__type, __item, __field) \
164 ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
166 #define LIST_IS_EMPTY(__list) \
167 ((__list)->next == (__list))
170 * Cast from a pointer to a member of a struct back to the containing struct.
172 * 'sample' MUST be initialized, or else the result is undefined!
175 #define container_of(ptr, sample, member) \
176 (void *)((char *)(ptr) \
177 - ((char *)&(sample)->member - (char *)(sample)))
180 #define list_first_entry(ptr, type, member) \
181 LIST_ENTRY(type, (ptr)->next, member)
183 #define list_last_entry(ptr, type, member) \
184 LIST_ENTRY(type, (ptr)->prev, member)
187 #define LIST_FOR_EACH_ENTRY(pos, head, member) \
188 for (pos = NULL, pos = container_of((head)->next, pos, member); \
189 &pos->member != (head); \
190 pos = container_of(pos->member.next, pos, member))
192 #define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member) \
193 for (pos = NULL, pos = container_of((head)->next, pos, member), \
194 storage = container_of(pos->member.next, pos, member); \
195 &pos->member != (head); \
196 pos = storage, storage = container_of(storage->member.next, storage, member))
198 #define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member) \
199 for (pos = NULL, pos = container_of((head)->prev, pos, member), \
200 storage = container_of(pos->member.prev, pos, member); \
201 &pos->member != (head); \
202 pos = storage, storage = container_of(storage->member.prev, storage, member))
204 #define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member) \
205 for (pos = NULL, pos = container_of((start), pos, member); \
206 &pos->member != (head); \
207 pos = container_of(pos->member.next, pos, member))
209 #define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member) \
210 for (pos = NULL, pos = container_of((start), pos, member); \
211 &pos->member != (head); \
212 pos = container_of(pos->member.prev, pos, member))
214 #define list_for_each_entry(type, pos, head, member) \
215 for (type *pos = LIST_ENTRY(type, (head)->next, member); \
216 &pos->member != (head); \
217 pos = LIST_ENTRY(type, pos->member.next, member))
219 #define list_for_each_entry_safe(type, pos, head, member) \
220 for (type *pos = LIST_ENTRY(type, (head)->next, member), \
221 *__next = LIST_ENTRY(type, pos->member.next, member); \
222 &pos->member != (head); \
224 __next = LIST_ENTRY(type, __next->member.next, member))
226 #define list_for_each_entry_rev(type, pos, head, member) \
227 for (type *pos = LIST_ENTRY(type, (head)->prev, member); \
228 &pos->member != (head); \
229 pos = LIST_ENTRY(type, pos->member.prev, member))
231 #define list_for_each_entry_safe_rev(type, pos, head, member) \
232 for (type *pos = LIST_ENTRY(type, (head)->prev, member), \
233 *__prev = LIST_ENTRY(type, pos->member.prev, member); \
234 &pos->member != (head); \
236 __prev = LIST_ENTRY(type, __prev->member.prev, member))
238 #define list_for_each_entry_from(type, pos, start, head, member) \
239 for (type *pos = LIST_ENTRY(type, (start), member); \
240 &pos->member != (head); \
241 pos = LIST_ENTRY(type, pos->member.next, member))
243 #define list_for_each_entry_from_rev(type, pos, start, head, member) \
244 for (type *pos = LIST_ENTRY(type, (start), member); \
245 &pos->member != (head); \
246 pos = LIST_ENTRY(type, pos->member.prev, member))
248 #endif /*_UTIL_LIST_H_*/