• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "c99_compat.h"
45 
46 #ifdef DEBUG
47 #  define list_assert(cond, msg)  assert(cond && msg)
48 #else
49 #  define list_assert(cond, msg)  (void)(0 && (cond))
50 #endif
51 
52 struct list_head
53 {
54     struct list_head *prev;
55     struct list_head *next;
56 };
57 
list_inithead(struct list_head * item)58 static inline void list_inithead(struct list_head *item)
59 {
60     item->prev = item;
61     item->next = item;
62 }
63 
list_add(struct list_head * item,struct list_head * list)64 static inline void list_add(struct list_head *item, struct list_head *list)
65 {
66     item->prev = list;
67     item->next = list->next;
68     list->next->prev = item;
69     list->next = item;
70 }
71 
list_addtail(struct list_head * item,struct list_head * list)72 static inline void list_addtail(struct list_head *item, struct list_head *list)
73 {
74     item->next = list;
75     item->prev = list->prev;
76     list->prev->next = item;
77     list->prev = item;
78 }
79 
80 static inline bool list_is_empty(const struct list_head *list);
81 
list_replace(struct list_head * from,struct list_head * to)82 static inline void list_replace(struct list_head *from, struct list_head *to)
83 {
84     if (list_is_empty(from)) {
85         list_inithead(to);
86     } else {
87         to->prev = from->prev;
88         to->next = from->next;
89         from->next->prev = to;
90         from->prev->next = to;
91     }
92 }
93 
list_del(struct list_head * item)94 static inline void list_del(struct list_head *item)
95 {
96     item->prev->next = item->next;
97     item->next->prev = item->prev;
98     item->prev = item->next = NULL;
99 }
100 
list_delinit(struct list_head * item)101 static inline void list_delinit(struct list_head *item)
102 {
103     item->prev->next = item->next;
104     item->next->prev = item->prev;
105     item->next = item;
106     item->prev = item;
107 }
108 
list_is_empty(const struct list_head * list)109 static inline bool list_is_empty(const struct list_head *list)
110 {
111    return list->next == list;
112 }
113 
list_is_linked(const struct list_head * list)114 static inline bool list_is_linked(const struct list_head *list)
115 {
116    /* both must be NULL or both must be not NULL */
117    assert((list->prev != NULL) == (list->next != NULL));
118 
119    return list->next != NULL;
120 }
121 
122 /**
123  * Returns whether the list has exactly one element.
124  */
list_is_singular(const struct list_head * list)125 static inline bool list_is_singular(const struct list_head *list)
126 {
127    return list_is_linked(list) && !list_is_empty(list) && list->next->next == list;
128 }
129 
list_length(const struct list_head * list)130 static inline unsigned list_length(const struct list_head *list)
131 {
132    struct list_head *node;
133    unsigned length = 0;
134    for (node = list->next; node != list; node = node->next)
135       length++;
136    return length;
137 }
138 
list_splice(struct list_head * src,struct list_head * dst)139 static inline void list_splice(struct list_head *src, struct list_head *dst)
140 {
141    if (list_is_empty(src))
142       return;
143 
144    src->next->prev = dst;
145    src->prev->next = dst->next;
146    dst->next->prev = src->prev;
147    dst->next = src->next;
148 }
149 
list_splicetail(struct list_head * src,struct list_head * dst)150 static inline void list_splicetail(struct list_head *src, struct list_head *dst)
151 {
152    if (list_is_empty(src))
153       return;
154 
155    src->prev->next = dst;
156    src->next->prev = dst->prev;
157    dst->prev->next = src->next;
158    dst->prev = src->prev;
159 }
160 
list_validate(const struct list_head * list)161 static inline void list_validate(const struct list_head *list)
162 {
163    struct list_head *node;
164    assert(list_is_linked(list));
165    assert(list->next->prev == list && list->prev->next == list);
166    for (node = list->next; node != list; node = node->next)
167       assert(node->next->prev == node && node->prev->next == node);
168 }
169 
170 #define LIST_ENTRY(__type, __item, __field)   \
171     ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
172 
173 /**
174  * Cast from a pointer to a member of a struct back to the containing struct.
175  *
176  * 'sample' MUST be initialized, or else the result is undefined!
177  */
178 #define list_container_of(ptr, sample, member)				\
179     (void *)((char *)(ptr)						\
180 	     - ((char *)&(sample)->member - (char *)(sample)))
181 
182 #define list_first_entry(ptr, type, member) \
183         LIST_ENTRY(type, (ptr)->next, member)
184 
185 #define list_last_entry(ptr, type, member) \
186         LIST_ENTRY(type, (ptr)->prev, member)
187 
188 
189 #define LIST_FOR_EACH_ENTRY(pos, head, member)				\
190    for (pos = NULL, pos = list_container_of((head)->next, pos, member);	\
191 	&pos->member != (head);						\
192 	pos = list_container_of(pos->member.next, pos, member))
193 
194 #define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member)	\
195    for (pos = NULL, pos = list_container_of((head)->next, pos, member),	\
196 	storage = list_container_of(pos->member.next, pos, member);	\
197 	&pos->member != (head);						\
198 	pos = storage, storage = list_container_of(storage->member.next, storage, member))
199 
200 #define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member)	\
201    for (pos = NULL, pos = list_container_of((head)->prev, pos, member),	\
202 	storage = list_container_of(pos->member.prev, pos, member);		\
203 	&pos->member != (head);						\
204 	pos = storage, storage = list_container_of(storage->member.prev, storage, member))
205 
206 #define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member)		\
207    for (pos = NULL, pos = list_container_of((start), pos, member);		\
208 	&pos->member != (head);						\
209 	pos = list_container_of(pos->member.next, pos, member))
210 
211 #define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member)		\
212    for (pos = NULL, pos = list_container_of((start), pos, member);		\
213 	&pos->member != (head);						\
214 	pos = list_container_of(pos->member.prev, pos, member))
215 
216 #define list_for_each_entry(type, pos, head, member)                    \
217    for (type *pos = LIST_ENTRY(type, (head)->next, member),             \
218 	     *__next = LIST_ENTRY(type, pos->member.next, member);      \
219 	&pos->member != (head);                                         \
220 	pos = LIST_ENTRY(type, pos->member.next, member),               \
221 	list_assert(pos == __next, "use _safe iterator"),               \
222 	__next = LIST_ENTRY(type, __next->member.next, member))
223 
224 #define list_for_each_entry_safe(type, pos, head, member)               \
225    for (type *pos = LIST_ENTRY(type, (head)->next, member),             \
226 	     *__next = LIST_ENTRY(type, pos->member.next, member);      \
227 	&pos->member != (head);                                         \
228 	pos = __next,                                                   \
229 	__next = LIST_ENTRY(type, __next->member.next, member))
230 
231 #define list_for_each_entry_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);                                         \
235 	pos = LIST_ENTRY(type, pos->member.prev, member),               \
236 	list_assert(pos == __prev, "use _safe iterator"),               \
237 	__prev = LIST_ENTRY(type, __prev->member.prev, member))
238 
239 #define list_for_each_entry_safe_rev(type, pos, head, member)           \
240    for (type *pos = LIST_ENTRY(type, (head)->prev, member),             \
241 	     *__prev = LIST_ENTRY(type, pos->member.prev, member);      \
242 	&pos->member != (head);                                         \
243 	pos = __prev,                                                   \
244         __prev = LIST_ENTRY(type, __prev->member.prev, member))
245 
246 #define list_for_each_entry_from(type, pos, start, head, member)        \
247    for (type *pos = LIST_ENTRY(type, (start), member);                  \
248 	&pos->member != (head);                                         \
249 	pos = LIST_ENTRY(type, pos->member.next, member))
250 
251 #define list_for_each_entry_from_safe(type, pos, start, head, member)   \
252    for (type *pos = LIST_ENTRY(type, (start), member),                  \
253 	     *__next = LIST_ENTRY(type, pos->member.next, member);      \
254 	&pos->member != (head);                                         \
255 	pos = __next,                                                   \
256 	__next = LIST_ENTRY(type, __next->member.next, member))
257 
258 #define list_for_each_entry_from_rev(type, pos, start, head, member)    \
259    for (type *pos = LIST_ENTRY(type, (start), member);                  \
260 	&pos->member != (head);                                         \
261 	pos = LIST_ENTRY(type, pos->member.prev, member))
262 
263 #define list_pair_for_each_entry(type, pos1, pos2, head1, head2, member) \
264    for (type *pos1 = LIST_ENTRY(type, (head1)->next, member),           \
265              *pos2 = LIST_ENTRY(type, (head2)->next, member);           \
266         &pos1->member != (head1) && &pos2->member != (head2);           \
267 	pos1 = LIST_ENTRY(type, pos1->member.next, member),               \
268 	pos2 = LIST_ENTRY(type, pos2->member.next, member))
269 
270 #endif /*_UTIL_LIST_H_*/
271