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 */
114struct 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 */
126static void
127list_init(struct list *list)
128{
129    list->next = list->prev = list;
130}
131
132static 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 */
158static inline void
159list_add(struct list *entry, struct list *head)
160{
161    __list_add(entry, head, head->next);
162}
163
164static inline void
165list_add_tail(struct list *entry, struct list *head)
166{
167    __list_add(entry, head->prev, head);
168}
169
170static 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 */
200static inline void
201list_append(struct list *entry, struct list *head)
202{
203    __list_add(entry, head->prev, head);
204}
205
206
207static 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
215static 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 */
237static inline void
238list_del(struct list *entry)
239{
240    _list_del(entry);
241    list_init(entry);
242}
243
244static 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
252static 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 */
266static inline bool
267list_is_empty(const 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#ifdef HAVE_TYPEOF
309#define __container_of(ptr, sample, member)			\
310    container_of(ptr, typeof(*sample), member)
311#else
312/* This implementation of __container_of has undefined behavior according
313 * to the C standard, but it works in many cases.  If your compiler doesn't
314 * support typeof() and fails with this implementation, please try a newer
315 * compiler.
316 */
317#define __container_of(ptr, sample, member)                            \
318    (void *)((char *)(ptr)                                             \
319            - ((char *)&(sample)->member - (char *)(sample)))
320#endif
321
322/**
323 * Loop through the list given by head and set pos to struct in the list.
324 *
325 * Example:
326 * struct foo *iterator;
327 * list_for_each_entry(iterator, &bar->list_of_foos, entry) {
328 *      [modify iterator]
329 * }
330 *
331 * This macro is not safe for node deletion. Use list_for_each_entry_safe
332 * instead.
333 *
334 * @param pos Iterator variable of the type of the list elements.
335 * @param head List head
336 * @param member Member name of the struct list in the list elements.
337 *
338 */
339#define list_for_each_entry(pos, head, member)				\
340    for (pos = NULL,                                                    \
341         pos = __container_of((head)->next, pos, member);		\
342	 &pos->member != (head);					\
343	 pos = __container_of(pos->member.next, pos, member))
344
345#define list_for_each_entry_reverse(pos, head, member)				\
346    for (pos = NULL,                                                    \
347         pos = __container_of((head)->prev, pos, member);		\
348	 &pos->member != (head);					\
349	 pos = __container_of(pos->member.prev, pos, member))
350
351/**
352 * Loop through the list, keeping a backup pointer to the element. This
353 * macro allows for the deletion of a list element while looping through the
354 * list.
355 *
356 * See list_for_each_entry for more details.
357 */
358#define list_for_each_entry_safe(pos, tmp, head, member)		\
359    for (pos = NULL,                                                    \
360         pos = __container_of((head)->next, pos, member),		\
361	 tmp = __container_of(pos->member.next, pos, member);		\
362	 &pos->member != (head);					\
363	 pos = tmp, tmp = __container_of(pos->member.next, tmp, member))
364
365#else
366
367#include <list.h>
368
369static inline void
370list_add_tail(struct list *entry, struct list *head)
371{
372    __list_add(entry, head->prev, head);
373}
374
375static inline void
376_list_del(struct list *entry)
377{
378    assert(entry->prev->next == entry);
379    assert(entry->next->prev == entry);
380    __list_del(entry->prev, entry->next);
381}
382
383static inline void list_replace(struct list *old,
384				struct list *new)
385{
386	new->next = old->next;
387	new->next->prev = new;
388	new->prev = old->prev;
389	new->prev->next = new;
390}
391
392static inline void list_move(struct list *list, struct list *head)
393{
394	if (list->prev != head) {
395		_list_del(list);
396		list_add(list, head);
397	}
398}
399
400static inline void list_move_tail(struct list *list, struct list *head)
401{
402	_list_del(list);
403	list_add_tail(list, head);
404}
405
406#define list_last_entry(ptr, type, member) \
407    list_entry((ptr)->prev, type, member)
408
409#define list_for_each_entry_reverse(pos, head, member)				\
410    for (pos = __container_of((head)->prev, pos, member);		\
411	 &pos->member != (head);					\
412	 pos = __container_of(pos->member.prev, pos, member))
413
414#endif
415
416#undef container_of
417#define container_of(ptr, type, member) \
418	((type *)((char *)(ptr) - (char *) &((type *)0)->member))
419
420static inline int list_is_singular(const struct list *list)
421{
422	return list->next == list->prev;
423}
424
425#endif /* _INTEL_LIST_H_ */
426
427