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
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
list_add(struct list_head * item,struct list_head * list)59 static inline void list_add(struct list_head *item, struct list_head *list)
60 {
61 item->prev = list;
62 item->next = list->next;
63 list->next->prev = item;
64 list->next = item;
65 }
66
list_addtail(struct list_head * item,struct list_head * list)67 static inline void list_addtail(struct list_head *item, struct list_head *list)
68 {
69 item->next = list;
70 item->prev = list->prev;
71 list->prev->next = item;
72 list->prev = item;
73 }
74
75 static inline bool list_empty(struct list_head *list);
76
list_replace(struct list_head * from,struct list_head * to)77 static inline void list_replace(struct list_head *from, struct list_head *to)
78 {
79 if (list_empty(from)) {
80 list_inithead(to);
81 } else {
82 to->prev = from->prev;
83 to->next = from->next;
84 from->next->prev = to;
85 from->prev->next = to;
86 }
87 }
88
list_del(struct list_head * item)89 static inline void list_del(struct list_head *item)
90 {
91 item->prev->next = item->next;
92 item->next->prev = item->prev;
93 item->prev = item->next = NULL;
94 }
95
list_delinit(struct list_head * item)96 static inline void list_delinit(struct list_head *item)
97 {
98 item->prev->next = item->next;
99 item->next->prev = item->prev;
100 item->next = item;
101 item->prev = item;
102 }
103
list_empty(struct list_head * list)104 static inline bool list_empty(struct list_head *list)
105 {
106 return list->next == list;
107 }
108
109 /**
110 * Returns whether the list has exactly one element.
111 */
list_is_singular(const struct list_head * list)112 static inline bool list_is_singular(const struct list_head *list)
113 {
114 return list->next != NULL && list->next != list && list->next->next == list;
115 }
116
list_length(struct list_head * list)117 static inline unsigned list_length(struct list_head *list)
118 {
119 struct list_head *node;
120 unsigned length = 0;
121 for (node = list->next; node != list; node = node->next)
122 length++;
123 return length;
124 }
125
list_splice(struct list_head * src,struct list_head * dst)126 static inline void list_splice(struct list_head *src, struct list_head *dst)
127 {
128 if (list_empty(src))
129 return;
130
131 src->next->prev = dst;
132 src->prev->next = dst->next;
133 dst->next->prev = src->prev;
134 dst->next = src->next;
135 }
136
list_splicetail(struct list_head * src,struct list_head * dst)137 static inline void list_splicetail(struct list_head *src, struct list_head *dst)
138 {
139 if (list_empty(src))
140 return;
141
142 src->prev->next = dst;
143 src->next->prev = dst->prev;
144 dst->prev->next = src->next;
145 dst->prev = src->prev;
146 }
147
list_validate(struct list_head * list)148 static inline void list_validate(struct list_head *list)
149 {
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);
154 }
155
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)
162
163 #define LIST_ENTRY(__type, __item, __field) \
164 ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
165
166 #define LIST_IS_EMPTY(__list) \
167 ((__list)->next == (__list))
168
169 /**
170 * Cast from a pointer to a member of a struct back to the containing struct.
171 *
172 * 'sample' MUST be initialized, or else the result is undefined!
173 */
174 #ifndef container_of
175 #define container_of(ptr, sample, member) \
176 (void *)((char *)(ptr) \
177 - ((char *)&(sample)->member - (char *)(sample)))
178 #endif
179
180 #define list_first_entry(ptr, type, member) \
181 LIST_ENTRY(type, (ptr)->next, member)
182
183 #define list_last_entry(ptr, type, member) \
184 LIST_ENTRY(type, (ptr)->prev, member)
185
186
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))
191
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))
197
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))
203
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))
208
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))
213
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))
218
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); \
223 pos = __next, \
224 __next = LIST_ENTRY(type, __next->member.next, member))
225
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))
230
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); \
235 pos = __prev, \
236 __prev = LIST_ENTRY(type, __prev->member.prev, member))
237
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))
242
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))
247
248 #endif /*_UTIL_LIST_H_*/
249