1 /**************************************************************************
2 *
3 * Copyright 2006 VMware, Inc., Bismarck, ND. USA.
4 * All Rights Reserved.
5 *
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:
13 *
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.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28 /**
29 * \file
30 * List macros heavily inspired by the Linux kernel
31 * list handling. No list looping yet.
32 *
33 * Is not threadsafe, so common operations need to
34 * be protected using an external mutex.
35 */
36
37 #ifndef _UTIL_LIST_H_
38 #define _UTIL_LIST_H_
39
40
41 #include <stdbool.h>
42 #include <stddef.h>
43 #include <assert.h>
44
45 #define list_assert(cond, msg) assert(cond && msg)
46
47 struct list_head
48 {
49 struct list_head *prev;
50 struct list_head *next;
51 };
52
list_inithead(struct list_head * item)53 static inline void list_inithead(struct list_head *item)
54 {
55 item->prev = item;
56 item->next = item;
57 }
58
59 /**
60 * Prepend an item to a list
61 *
62 * @param item The element to add to the list
63 * @param list The list to prepend to
64 */
list_add(struct list_head * item,struct list_head * list)65 static inline void list_add(struct list_head *item, struct list_head *list)
66 {
67 item->prev = list;
68 item->next = list->next;
69 list->next->prev = item;
70 list->next = item;
71 }
72
73 /**
74 * Append an item to a list
75 *
76 * @param item The element to add to the list
77 * @param list The list to append to
78 */
list_addtail(struct list_head * item,struct list_head * list)79 static inline void list_addtail(struct list_head *item, struct list_head *list)
80 {
81 item->next = list;
82 item->prev = list->prev;
83 list->prev->next = item;
84 list->prev = item;
85 }
86
87 static inline bool list_is_empty(const struct list_head *list);
88
list_replace(struct list_head * from,struct list_head * to)89 static inline void list_replace(struct list_head *from, struct list_head *to)
90 {
91 if (list_is_empty(from)) {
92 list_inithead(to);
93 } else {
94 to->prev = from->prev;
95 to->next = from->next;
96 from->next->prev = to;
97 from->prev->next = to;
98 }
99 }
100
list_del(struct list_head * item)101 static inline void list_del(struct list_head *item)
102 {
103 item->prev->next = item->next;
104 item->next->prev = item->prev;
105 item->prev = item->next = NULL;
106 }
107
list_delinit(struct list_head * item)108 static inline void list_delinit(struct list_head *item)
109 {
110 item->prev->next = item->next;
111 item->next->prev = item->prev;
112 item->next = item;
113 item->prev = item;
114 }
115
list_is_empty(const struct list_head * list)116 static inline bool list_is_empty(const struct list_head *list)
117 {
118 return list->next == list;
119 }
120
list_is_linked(const struct list_head * list)121 static inline bool list_is_linked(const struct list_head *list)
122 {
123 /* both must be NULL or both must be not NULL */
124 assert((list->prev != NULL) == (list->next != NULL));
125
126 return list->next != NULL;
127 }
128
129 /**
130 * Returns whether the list has exactly one element.
131 */
list_is_singular(const struct list_head * list)132 static inline bool list_is_singular(const struct list_head *list)
133 {
134 return list_is_linked(list) && !list_is_empty(list) && list->next->next == list;
135 }
136
list_length(const struct list_head * list)137 static inline unsigned list_length(const struct list_head *list)
138 {
139 struct list_head *node;
140 unsigned length = 0;
141 for (node = list->next; node != list; node = node->next)
142 length++;
143 return length;
144 }
145
list_splice(struct list_head * src,struct list_head * dst)146 static inline void list_splice(struct list_head *src, struct list_head *dst)
147 {
148 if (list_is_empty(src))
149 return;
150
151 src->next->prev = dst;
152 src->prev->next = dst->next;
153 dst->next->prev = src->prev;
154 dst->next = src->next;
155 }
156
list_splicetail(struct list_head * src,struct list_head * dst)157 static inline void list_splicetail(struct list_head *src, struct list_head *dst)
158 {
159 if (list_is_empty(src))
160 return;
161
162 src->prev->next = dst;
163 src->next->prev = dst->prev;
164 dst->prev->next = src->next;
165 dst->prev = src->prev;
166 }
167
list_validate(const struct list_head * list)168 static inline void list_validate(const struct list_head *list)
169 {
170 struct list_head *node;
171 assert(list_is_linked(list));
172 assert(list->next->prev == list && list->prev->next == list);
173 for (node = list->next; node != list; node = node->next)
174 assert(node->next->prev == node && node->prev->next == node);
175 }
176
177 /**
178 * Move an item from one place in a list to another
179 *
180 * The item can be in this list, or in another.
181 *
182 * @param item The item to move
183 * @param loc The element to put the item in front of
184 */
list_move_to(struct list_head * item,struct list_head * loc)185 static inline void list_move_to(struct list_head *item, struct list_head *loc) {
186 list_del(item);
187 list_add(item, loc);
188 }
189
190 #define list_entry(__item, __type, __field) \
191 ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
192
193 /**
194 * Cast from a pointer to a member of a struct back to the containing struct.
195 *
196 * 'sample' MUST be initialized, or else the result is undefined!
197 */
198 #define list_container_of(ptr, sample, member) \
199 (void *)((char *)(ptr) \
200 - ((char *)&(sample)->member - (char *)(sample)))
201
202 #define list_first_entry(ptr, type, member) \
203 list_entry((ptr)->next, type, member)
204
205 #define list_last_entry(ptr, type, member) \
206 list_entry((ptr)->prev, type, member)
207
208
209 #define LIST_FOR_EACH_ENTRY(pos, head, member) \
210 for (pos = NULL, pos = list_container_of((head)->next, pos, member); \
211 &pos->member != (head); \
212 pos = list_container_of(pos->member.next, pos, member))
213
214 #define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member) \
215 for (pos = NULL, pos = list_container_of((head)->next, pos, member), \
216 storage = list_container_of(pos->member.next, pos, member); \
217 &pos->member != (head); \
218 pos = storage, storage = list_container_of(storage->member.next, storage, member))
219
220 #define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member) \
221 for (pos = NULL, pos = list_container_of((head)->prev, pos, member), \
222 storage = list_container_of(pos->member.prev, pos, member); \
223 &pos->member != (head); \
224 pos = storage, storage = list_container_of(storage->member.prev, storage, member))
225
226 #define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member) \
227 for (pos = NULL, pos = list_container_of((start), pos, member); \
228 &pos->member != (head); \
229 pos = list_container_of(pos->member.next, pos, member))
230
231 #define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member) \
232 for (pos = NULL, pos = list_container_of((start), pos, member); \
233 &pos->member != (head); \
234 pos = list_container_of(pos->member.prev, pos, member))
235
236 #define list_for_each_entry(type, pos, head, member) \
237 for (type *pos = list_entry((head)->next, type, member), \
238 *__next = list_entry(pos->member.next, type, member); \
239 &pos->member != (head); \
240 pos = list_entry(pos->member.next, type, member), \
241 list_assert(pos == __next, "use _safe iterator"), \
242 __next = list_entry(__next->member.next, type, member))
243
244 #define list_for_each_entry_safe(type, pos, head, member) \
245 for (type *pos = list_entry((head)->next, type, member), \
246 *__next = list_entry(pos->member.next, type, member); \
247 &pos->member != (head); \
248 pos = __next, \
249 __next = list_entry(__next->member.next, type, member))
250
251 #define list_for_each_entry_rev(type, pos, head, member) \
252 for (type *pos = list_entry((head)->prev, type, member), \
253 *__prev = list_entry(pos->member.prev, type, member); \
254 &pos->member != (head); \
255 pos = list_entry(pos->member.prev, type, member), \
256 list_assert(pos == __prev, "use _safe iterator"), \
257 __prev = list_entry(__prev->member.prev, type, member))
258
259 #define list_for_each_entry_safe_rev(type, pos, head, member) \
260 for (type *pos = list_entry((head)->prev, type, member), \
261 *__prev = list_entry(pos->member.prev, type, member); \
262 &pos->member != (head); \
263 pos = __prev, \
264 __prev = list_entry(__prev->member.prev, type, member))
265
266 #define list_for_each_entry_from(type, pos, start, head, member) \
267 for (type *pos = list_entry((start), type, member); \
268 &pos->member != (head); \
269 pos = list_entry(pos->member.next, type, member))
270
271 #define list_for_each_entry_from_safe(type, pos, start, head, member) \
272 for (type *pos = list_entry((start), type, member), \
273 *__next = list_entry(pos->member.next, type, member); \
274 &pos->member != (head); \
275 pos = __next, \
276 __next = list_entry(__next->member.next, type, member))
277
278 #define list_for_each_entry_from_rev(type, pos, start, head, member) \
279 for (type *pos = list_entry((start), type, member); \
280 &pos->member != (head); \
281 pos = list_entry(pos->member.prev, type, member))
282
283 #define list_pair_for_each_entry(type, pos1, pos2, head1, head2, member) \
284 for (type *pos1 = list_entry((head1)->next, type, member), \
285 *pos2 = list_entry((head2)->next, type, member); \
286 &pos1->member != (head1) && &pos2->member != (head2); \
287 pos1 = list_entry(pos1->member.next, type, member), \
288 pos2 = list_entry(pos2->member.next, type, member))
289
290 #endif /*_UTIL_LIST_H_*/
291