list: fix list_replace() for empty lists
[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
45
46 struct list_head
47 {
48 struct list_head *prev;
49 struct list_head *next;
50 };
51
52 static inline void list_inithead(struct list_head *item)
53 {
54 item->prev = item;
55 item->next = item;
56 }
57
58 static inline void list_add(struct list_head *item, struct list_head *list)
59 {
60 item->prev = list;
61 item->next = list->next;
62 list->next->prev = item;
63 list->next = item;
64 }
65
66 static inline void list_addtail(struct list_head *item, struct list_head *list)
67 {
68 item->next = list;
69 item->prev = list->prev;
70 list->prev->next = item;
71 list->prev = item;
72 }
73
74 static inline bool list_empty(struct list_head *list);
75
76 static inline void list_replace(struct list_head *from, struct list_head *to)
77 {
78 if (list_empty(from)) {
79 list_inithead(to);
80 } else {
81 to->prev = from->prev;
82 to->next = from->next;
83 from->next->prev = to;
84 from->prev->next = to;
85 }
86 }
87
88 static inline void list_del(struct list_head *item)
89 {
90 item->prev->next = item->next;
91 item->next->prev = item->prev;
92 item->prev = item->next = NULL;
93 }
94
95 static inline void list_delinit(struct list_head *item)
96 {
97 item->prev->next = item->next;
98 item->next->prev = item->prev;
99 item->next = item;
100 item->prev = item;
101 }
102
103 static inline bool list_empty(struct list_head *list)
104 {
105 return list->next == list;
106 }
107
108 /**
109 * Returns whether the list has exactly one element.
110 */
111 static inline bool list_is_singular(const struct list_head *list)
112 {
113 return list->next != NULL && list->next->next == list;
114 }
115
116 static inline unsigned list_length(struct list_head *list)
117 {
118 struct list_head *node;
119 unsigned length = 0;
120 for (node = list->next; node != list; node = node->next)
121 length++;
122 return length;
123 }
124
125 static inline void list_splice(struct list_head *src, struct list_head *dst)
126 {
127 if (list_empty(src))
128 return;
129
130 src->next->prev = dst;
131 src->prev->next = dst->next;
132 dst->next->prev = src->prev;
133 dst->next = src->next;
134 }
135
136 static inline void list_splicetail(struct list_head *src, struct list_head *dst)
137 {
138 if (list_empty(src))
139 return;
140
141 src->prev->next = dst;
142 src->next->prev = dst->prev;
143 dst->prev->next = src->next;
144 dst->prev = src->prev;
145 }
146
147 static inline void list_validate(struct list_head *list)
148 {
149 struct list_head *node;
150 assert(list->next->prev == list && list->prev->next == list);
151 for (node = list->next; node != list; node = node->next)
152 assert(node->next->prev == node && node->prev->next == node);
153 }
154
155 #define LIST_INITHEAD(__item) list_inithead(__item)
156 #define LIST_ADD(__item, __list) list_add(__item, __list)
157 #define LIST_ADDTAIL(__item, __list) list_addtail(__item, __list)
158 #define LIST_REPLACE(__from, __to) list_replace(__from, __to)
159 #define LIST_DEL(__item) list_del(__item)
160 #define LIST_DELINIT(__item) list_delinit(__item)
161
162 #define LIST_ENTRY(__type, __item, __field) \
163 ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
164
165 #define LIST_IS_EMPTY(__list) \
166 ((__list)->next == (__list))
167
168 /**
169 * Cast from a pointer to a member of a struct back to the containing struct.
170 *
171 * 'sample' MUST be initialized, or else the result is undefined!
172 */
173 #ifndef container_of
174 #define container_of(ptr, sample, member) \
175 (void *)((char *)(ptr) \
176 - ((char *)&(sample)->member - (char *)(sample)))
177 #endif
178
179 #define list_first_entry(ptr, type, member) \
180 LIST_ENTRY(type, (ptr)->next, member)
181
182 #define list_last_entry(ptr, type, member) \
183 LIST_ENTRY(type, (ptr)->prev, member)
184
185
186 #define LIST_FOR_EACH_ENTRY(pos, head, member) \
187 for (pos = NULL, pos = container_of((head)->next, pos, member); \
188 &pos->member != (head); \
189 pos = container_of(pos->member.next, pos, member))
190
191 #define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member) \
192 for (pos = NULL, pos = container_of((head)->next, pos, member), \
193 storage = container_of(pos->member.next, pos, member); \
194 &pos->member != (head); \
195 pos = storage, storage = container_of(storage->member.next, storage, member))
196
197 #define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member) \
198 for (pos = NULL, pos = container_of((head)->prev, pos, member), \
199 storage = container_of(pos->member.prev, pos, member); \
200 &pos->member != (head); \
201 pos = storage, storage = container_of(storage->member.prev, storage, member))
202
203 #define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member) \
204 for (pos = NULL, pos = container_of((start), pos, member); \
205 &pos->member != (head); \
206 pos = container_of(pos->member.next, pos, member))
207
208 #define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member) \
209 for (pos = NULL, pos = container_of((start), pos, member); \
210 &pos->member != (head); \
211 pos = container_of(pos->member.prev, pos, member))
212
213 #define list_for_each_entry(type, pos, head, member) \
214 for (type *pos = LIST_ENTRY(type, (head)->next, member); \
215 &pos->member != (head); \
216 pos = LIST_ENTRY(type, pos->member.next, member))
217
218 #define list_for_each_entry_safe(type, pos, head, member) \
219 for (type *pos = LIST_ENTRY(type, (head)->next, member), \
220 *__next = LIST_ENTRY(type, pos->member.next, member); \
221 &pos->member != (head); \
222 pos = __next, \
223 __next = LIST_ENTRY(type, __next->member.next, member))
224
225 #define list_for_each_entry_rev(type, pos, head, member) \
226 for (type *pos = LIST_ENTRY(type, (head)->prev, member); \
227 &pos->member != (head); \
228 pos = LIST_ENTRY(type, pos->member.prev, member))
229
230 #define list_for_each_entry_safe_rev(type, pos, head, member) \
231 for (type *pos = LIST_ENTRY(type, (head)->prev, member), \
232 *__prev = LIST_ENTRY(type, pos->member.prev, member); \
233 &pos->member != (head); \
234 pos = __prev, \
235 __prev = LIST_ENTRY(type, __prev->member.prev, member))
236
237 #define list_for_each_entry_from(type, pos, start, head, member) \
238 for (type *pos = LIST_ENTRY(type, (start), member); \
239 &pos->member != (head); \
240 pos = LIST_ENTRY(type, pos->member.next, member))
241
242 #define list_for_each_entry_from_rev(type, pos, start, head, member) \
243 for (type *pos = LIST_ENTRY(type, (start), member); \
244 &pos->member != (head); \
245 pos = LIST_ENTRY(type, pos->member.prev, member))
246
247 #endif /*_UTIL_LIST_H_*/