util/list: Add a list pair iterator
[mesa.git] / src / util / list.h
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
58 static inline void list_inithead(struct list_head *item)
59 {
60 item->prev = item;
61 item->next = item;
62 }
63
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
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
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
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
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
109 static inline bool list_is_empty(const struct list_head *list)
110 {
111 return list->next == list;
112 }
113
114 /**
115 * Returns whether the list has exactly one element.
116 */
117 static inline bool list_is_singular(const struct list_head *list)
118 {
119 return list->next != NULL && list->next != list && list->next->next == list;
120 }
121
122 static inline unsigned list_length(const struct list_head *list)
123 {
124 struct list_head *node;
125 unsigned length = 0;
126 for (node = list->next; node != list; node = node->next)
127 length++;
128 return length;
129 }
130
131 static inline void list_splice(struct list_head *src, struct list_head *dst)
132 {
133 if (list_is_empty(src))
134 return;
135
136 src->next->prev = dst;
137 src->prev->next = dst->next;
138 dst->next->prev = src->prev;
139 dst->next = src->next;
140 }
141
142 static inline void list_splicetail(struct list_head *src, struct list_head *dst)
143 {
144 if (list_is_empty(src))
145 return;
146
147 src->prev->next = dst;
148 src->next->prev = dst->prev;
149 dst->prev->next = src->next;
150 dst->prev = src->prev;
151 }
152
153 static inline void list_validate(const struct list_head *list)
154 {
155 struct list_head *node;
156 assert(list->next->prev == list && list->prev->next == list);
157 for (node = list->next; node != list; node = node->next)
158 assert(node->next->prev == node && node->prev->next == node);
159 }
160
161 #define LIST_ENTRY(__type, __item, __field) \
162 ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
163
164 /**
165 * Cast from a pointer to a member of a struct back to the containing struct.
166 *
167 * 'sample' MUST be initialized, or else the result is undefined!
168 */
169 #ifndef container_of
170 #define container_of(ptr, sample, member) \
171 (void *)((char *)(ptr) \
172 - ((char *)&(sample)->member - (char *)(sample)))
173 #endif
174
175 #define list_first_entry(ptr, type, member) \
176 LIST_ENTRY(type, (ptr)->next, member)
177
178 #define list_last_entry(ptr, type, member) \
179 LIST_ENTRY(type, (ptr)->prev, member)
180
181
182 #define LIST_FOR_EACH_ENTRY(pos, head, member) \
183 for (pos = NULL, pos = container_of((head)->next, pos, member); \
184 &pos->member != (head); \
185 pos = container_of(pos->member.next, pos, member))
186
187 #define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member) \
188 for (pos = NULL, pos = container_of((head)->next, pos, member), \
189 storage = container_of(pos->member.next, pos, member); \
190 &pos->member != (head); \
191 pos = storage, storage = container_of(storage->member.next, storage, member))
192
193 #define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member) \
194 for (pos = NULL, pos = container_of((head)->prev, pos, member), \
195 storage = container_of(pos->member.prev, pos, member); \
196 &pos->member != (head); \
197 pos = storage, storage = container_of(storage->member.prev, storage, member))
198
199 #define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member) \
200 for (pos = NULL, pos = container_of((start), pos, member); \
201 &pos->member != (head); \
202 pos = container_of(pos->member.next, pos, member))
203
204 #define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member) \
205 for (pos = NULL, pos = container_of((start), pos, member); \
206 &pos->member != (head); \
207 pos = container_of(pos->member.prev, pos, member))
208
209 #define list_for_each_entry(type, pos, head, member) \
210 for (type *pos = LIST_ENTRY(type, (head)->next, member), \
211 *__next = LIST_ENTRY(type, pos->member.next, member); \
212 &pos->member != (head); \
213 pos = LIST_ENTRY(type, pos->member.next, member), \
214 list_assert(pos == __next, "use _safe iterator"), \
215 __next = LIST_ENTRY(type, __next->member.next, member))
216
217 #define list_for_each_entry_safe(type, pos, head, member) \
218 for (type *pos = LIST_ENTRY(type, (head)->next, member), \
219 *__next = LIST_ENTRY(type, pos->member.next, member); \
220 &pos->member != (head); \
221 pos = __next, \
222 __next = LIST_ENTRY(type, __next->member.next, member))
223
224 #define list_for_each_entry_rev(type, pos, head, member) \
225 for (type *pos = LIST_ENTRY(type, (head)->prev, member), \
226 *__prev = LIST_ENTRY(type, pos->member.prev, member); \
227 &pos->member != (head); \
228 pos = LIST_ENTRY(type, pos->member.prev, member), \
229 list_assert(pos == __prev, "use _safe iterator"), \
230 __prev = LIST_ENTRY(type, __prev->member.prev, member))
231
232 #define list_for_each_entry_safe_rev(type, pos, head, member) \
233 for (type *pos = LIST_ENTRY(type, (head)->prev, member), \
234 *__prev = LIST_ENTRY(type, pos->member.prev, member); \
235 &pos->member != (head); \
236 pos = __prev, \
237 __prev = LIST_ENTRY(type, __prev->member.prev, member))
238
239 #define list_for_each_entry_from(type, pos, start, head, member) \
240 for (type *pos = LIST_ENTRY(type, (start), member); \
241 &pos->member != (head); \
242 pos = LIST_ENTRY(type, pos->member.next, member))
243
244 #define list_for_each_entry_from_rev(type, pos, start, head, member) \
245 for (type *pos = LIST_ENTRY(type, (start), member); \
246 &pos->member != (head); \
247 pos = LIST_ENTRY(type, pos->member.prev, member))
248
249 #define list_pair_for_each_entry(type, pos1, pos2, head1, head2, member) \
250 for (type *pos1 = LIST_ENTRY(type, (head1)->next, member), \
251 *pos2 = LIST_ENTRY(type, (head2)->next, member); \
252 &pos1->member != (head1) && &pos2->member != (head2); \
253 pos1 = LIST_ENTRY(type, pos1->member.next, member), \
254 pos2 = LIST_ENTRY(type, pos2->member.next, member))
255
256 #endif /*_UTIL_LIST_H_*/