take two
[profile/ivi/xorg-x11-drv-intel.git] / src / intel_list.h
1 /*
2  * Copyright © 2010-2012 Intel Corporation
3  * Copyright © 2010 Francisco Jerez <currojerez@riseup.net>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  */
25
26 #ifndef _INTEL_LIST_H_
27 #define _INTEL_LIST_H_
28
29 #include <xorgVersion.h>
30
31 #if XORG_VERSION_CURRENT < XORG_VERSION_NUMERIC(1,9,0,0,0) || XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1,11,99,903,0)
32
33 #include <stdbool.h>
34
35 /**
36  * @file Classic doubly-link circular list implementation.
37  * For real usage examples of the linked list, see the file test/list.c
38  *
39  * Example:
40  * We need to keep a list of struct foo in the parent struct bar, i.e. what
41  * we want is something like this.
42  *
43  *     struct bar {
44  *          ...
45  *          struct foo *list_of_foos; -----> struct foo {}, struct foo {}, struct foo{}
46  *          ...
47  *     }
48  *
49  * We need one list head in bar and a list element in all list_of_foos (both are of
50  * data type 'struct list').
51  *
52  *     struct bar {
53  *          ...
54  *          struct list list_of_foos;
55  *          ...
56  *     }
57  *
58  *     struct foo {
59  *          ...
60  *          struct list entry;
61  *          ...
62  *     }
63  *
64  * Now we initialize the list head:
65  *
66  *     struct bar bar;
67  *     ...
68  *     list_init(&bar.list_of_foos);
69  *
70  * Then we create the first element and add it to this list:
71  *
72  *     struct foo *foo = malloc(...);
73  *     ....
74  *     list_add(&foo->entry, &bar.list_of_foos);
75  *
76  * Repeat the above for each element you want to add to the list. Deleting
77  * works with the element itself.
78  *      list_del(&foo->entry);
79  *      free(foo);
80  *
81  * Note: calling list_del(&bar.list_of_foos) will set bar.list_of_foos to an empty
82  * list again.
83  *
84  * Looping through the list requires a 'struct foo' as iterator and the
85  * name of the field the subnodes use.
86  *
87  * struct foo *iterator;
88  * list_for_each_entry(iterator, &bar.list_of_foos, entry) {
89  *      if (iterator->something == ...)
90  *             ...
91  * }
92  *
93  * Note: You must not call list_del() on the iterator if you continue the
94  * loop. You need to run the safe for-each loop instead:
95  *
96  * struct foo *iterator, *next;
97  * list_for_each_entry_safe(iterator, next, &bar.list_of_foos, entry) {
98  *      if (...)
99  *              list_del(&iterator->entry);
100  * }
101  *
102  */
103
104 /**
105  * The linkage struct for list nodes. This struct must be part of your
106  * to-be-linked struct. struct list is required for both the head of the
107  * list and for each list node.
108  *
109  * Position and name of the struct list field is irrelevant.
110  * There are no requirements that elements of a list are of the same type.
111  * There are no requirements for a list head, any struct list can be a list
112  * head.
113  */
114 struct list {
115     struct list *next, *prev;
116 };
117
118 /**
119  * Initialize the list as an empty list.
120  *
121  * Example:
122  * list_init(&bar->list_of_foos);
123  *
124  * @param The list to initialized.
125  */
126 static void
127 list_init(struct list *list)
128 {
129     list->next = list->prev = list;
130 }
131
132 static inline void
133 __list_add(struct list *entry,
134             struct list *prev,
135             struct list *next)
136 {
137     next->prev = entry;
138     entry->next = next;
139     entry->prev = prev;
140     prev->next = entry;
141 }
142
143 /**
144  * Insert a new element after the given list head. The new element does not
145  * need to be initialised as empty list.
146  * The list changes from:
147  *      head → some element → ...
148  * to
149  *      head → new element → older element → ...
150  *
151  * Example:
152  * struct foo *newfoo = malloc(...);
153  * list_add(&newfoo->entry, &bar->list_of_foos);
154  *
155  * @param entry The new element to prepend to the list.
156  * @param head The existing list.
157  */
158 static inline void
159 list_add(struct list *entry, struct list *head)
160 {
161     __list_add(entry, head, head->next);
162 }
163
164 static inline void
165 list_add_tail(struct list *entry, struct list *head)
166 {
167     __list_add(entry, head->prev, head);
168 }
169
170 static inline void list_replace(struct list *old,
171                                 struct list *new)
172 {
173         new->next = old->next;
174         new->next->prev = new;
175         new->prev = old->prev;
176         new->prev->next = new;
177 }
178
179 #define list_last_entry(ptr, type, member) \
180     list_entry((ptr)->prev, type, member)
181
182 #define list_for_each(pos, head)                                \
183     for (pos = (head)->next; pos != (head); pos = pos->next)
184
185 /**
186  * Append a new element to the end of the list given with this list head.
187  *
188  * The list changes from:
189  *      head → some element → ... → lastelement
190  * to
191  *      head → some element → ... → lastelement → new element
192  *
193  * Example:
194  * struct foo *newfoo = malloc(...);
195  * list_append(&newfoo->entry, &bar->list_of_foos);
196  *
197  * @param entry The new element to prepend to the list.
198  * @param head The existing list.
199  */
200 static inline void
201 list_append(struct list *entry, struct list *head)
202 {
203     __list_add(entry, head->prev, head);
204 }
205
206
207 static inline void
208 __list_del(struct list *prev, struct list *next)
209 {
210         assert(next->prev == prev->next);
211         next->prev = prev;
212         prev->next = next;
213 }
214
215 static inline void
216 _list_del(struct list *entry)
217 {
218     assert(entry->prev->next == entry);
219     assert(entry->next->prev == entry);
220     __list_del(entry->prev, entry->next);
221 }
222
223 /**
224  * Remove the element from the list it is in. Using this function will reset
225  * the pointers to/from this element so it is removed from the list. It does
226  * NOT free the element itself or manipulate it otherwise.
227  *
228  * Using list_del on a pure list head (like in the example at the top of
229  * this file) will NOT remove the first element from
230  * the list but rather reset the list as empty list.
231  *
232  * Example:
233  * list_del(&foo->entry);
234  *
235  * @param entry The element to remove.
236  */
237 static inline void
238 list_del(struct list *entry)
239 {
240     _list_del(entry);
241     list_init(entry);
242 }
243
244 static inline void list_move(struct list *list, struct list *head)
245 {
246         if (list->prev != head) {
247                 _list_del(list);
248                 list_add(list, head);
249         }
250 }
251
252 static inline void list_move_tail(struct list *list, struct list *head)
253 {
254         _list_del(list);
255         list_add_tail(list, head);
256 }
257
258 /**
259  * Check if the list is empty.
260  *
261  * Example:
262  * list_is_empty(&bar->list_of_foos);
263  *
264  * @return True if the list contains one or more elements or False otherwise.
265  */
266 static inline bool
267 list_is_empty(struct list *head)
268 {
269     return head->next == head;
270 }
271
272 /**
273  * Alias of container_of
274  */
275 #define list_entry(ptr, type, member) \
276     container_of(ptr, type, member)
277
278 /**
279  * Retrieve the first list entry for the given list pointer.
280  *
281  * Example:
282  * struct foo *first;
283  * first = list_first_entry(&bar->list_of_foos, struct foo, list_of_foos);
284  *
285  * @param ptr The list head
286  * @param type Data type of the list element to retrieve
287  * @param member Member name of the struct list field in the list element.
288  * @return A pointer to the first list element.
289  */
290 #define list_first_entry(ptr, type, member) \
291     list_entry((ptr)->next, type, member)
292
293 /**
294  * Retrieve the last list entry for the given listpointer.
295  *
296  * Example:
297  * struct foo *first;
298  * first = list_last_entry(&bar->list_of_foos, struct foo, list_of_foos);
299  *
300  * @param ptr The list head
301  * @param type Data type of the list element to retrieve
302  * @param member Member name of the struct list field in the list element.
303  * @return A pointer to the last list element.
304  */
305 #define list_last_entry(ptr, type, member) \
306     list_entry((ptr)->prev, type, member)
307
308 #define __container_of(ptr, sample, member)                             \
309     (void *)((char *)(ptr)                                              \
310              - ((char *)&(sample)->member - (char *)(sample)))
311 /**
312  * Loop through the list given by head and set pos to struct in the list.
313  *
314  * Example:
315  * struct foo *iterator;
316  * list_for_each_entry(iterator, &bar->list_of_foos, entry) {
317  *      [modify iterator]
318  * }
319  *
320  * This macro is not safe for node deletion. Use list_for_each_entry_safe
321  * instead.
322  *
323  * @param pos Iterator variable of the type of the list elements.
324  * @param head List head
325  * @param member Member name of the struct list in the list elements.
326  *
327  */
328 #define list_for_each_entry(pos, head, member)                          \
329     for (pos = __container_of((head)->next, pos, member);               \
330          &pos->member != (head);                                        \
331          pos = __container_of(pos->member.next, pos, member))
332
333 #define list_for_each_entry_reverse(pos, head, member)                          \
334     for (pos = __container_of((head)->prev, pos, member);               \
335          &pos->member != (head);                                        \
336          pos = __container_of(pos->member.prev, pos, member))
337
338 /**
339  * Loop through the list, keeping a backup pointer to the element. This
340  * macro allows for the deletion of a list element while looping through the
341  * list.
342  *
343  * See list_for_each_entry for more details.
344  */
345 #define list_for_each_entry_safe(pos, tmp, head, member)                \
346     for (pos = __container_of((head)->next, pos, member),               \
347          tmp = __container_of(pos->member.next, pos, member);           \
348          &pos->member != (head);                                        \
349          pos = tmp, tmp = __container_of(pos->member.next, tmp, member))
350
351 #else
352
353 #include <list.h>
354
355 static inline void
356 list_add_tail(struct list *entry, struct list *head)
357 {
358     __list_add(entry, head->prev, head);
359 }
360
361 static inline void
362 _list_del(struct list *entry)
363 {
364     assert(entry->prev->next == entry);
365     assert(entry->next->prev == entry);
366     __list_del(entry->prev, entry->next);
367 }
368
369 static inline void list_replace(struct list *old,
370                                 struct list *new)
371 {
372         new->next = old->next;
373         new->next->prev = new;
374         new->prev = old->prev;
375         new->prev->next = new;
376 }
377
378 static inline void list_move(struct list *list, struct list *head)
379 {
380         if (list->prev != head) {
381                 _list_del(list);
382                 list_add(list, head);
383         }
384 }
385
386 static inline void list_move_tail(struct list *list, struct list *head)
387 {
388         _list_del(list);
389         list_add_tail(list, head);
390 }
391
392 #define list_last_entry(ptr, type, member) \
393     list_entry((ptr)->prev, type, member)
394
395 #define list_for_each_entry_reverse(pos, head, member)                          \
396     for (pos = __container_of((head)->prev, pos, member);               \
397          &pos->member != (head);                                        \
398          pos = __container_of(pos->member.prev, pos, member))
399
400 #endif
401
402 #undef container_of
403 #define container_of(ptr, type, member) \
404         ((type *)((char *)(ptr) - (char *) &((type *)0)->member))
405
406 #endif /* _INTEL_LIST_H_ */
407