]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/omapdrmtest.git/blob - util/list.h
util/viddec3test: Update use of deprecated APIs and enums
[glsdk/omapdrmtest.git] / util / list.h
1 /*
2  * Copyright © 2010 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  */
26 #ifndef _LIST_H_
27 #define _LIST_H_
29 /**
30  * @file Classic doubly-link circular list implementation.
31  * For real usage examples of the linked list, see the file test/list.c
32  *
33  * Example:
34  * We need to keep a list of struct foo in the parent struct bar, i.e. what
35  * we want is something like this.
36  *
37  *     struct bar {
38  *          ...
39  *          struct foo *list_of_foos; -----> struct foo {}, struct foo {}, struct foo{}
40  *          ...
41  *     }
42  *
43  * We need one list head in bar and a list element in all list_of_foos (both are of
44  * data type 'struct list').
45  *
46  *     struct bar {
47  *          ...
48  *          struct list list_of_foos;
49  *          ...
50  *     }
51  *
52  *     struct foo {
53  *          ...
54  *          struct list entry;
55  *          ...
56  *     }
57  *
58  * Now we initialize the list head:
59  *
60  *     struct bar bar;
61  *     ...
62  *     list_init(&bar.list_of_foos);
63  *
64  * Then we create the first element and add it to this list:
65  *
66  *     struct foo *foo = malloc(...);
67  *     ....
68  *     list_add(&foo->entry, &bar.list_of_foos);
69  *
70  * Repeat the above for each element you want to add to the list. Deleting
71  * works with the element itself.
72  *      list_del(&foo->entry);
73  *      free(foo);
74  *
75  * Note: calling list_del(&bar.list_of_foos) will set bar.list_of_foos to an empty
76  * list again.
77  *
78  * Looping through the list requires a 'struct foo' as iterator and the
79  * name of the field the subnodes use.
80  *
81  * struct foo *iterator;
82  * list_for_each_entry(iterator, &bar.list_of_foos, entry) {
83  *      if (iterator->something == ...)
84  *             ...
85  * }
86  *
87  * Note: You must not call list_del() on the iterator if you continue the
88  * loop. You need to run the safe for-each loop instead:
89  *
90  * struct foo *iterator, *next;
91  * list_for_each_entry_safe(iterator, next, &bar.list_of_foos, entry) {
92  *      if (...)
93  *              list_del(&iterator->entry);
94  * }
95  *
96  */
98 /**
99  * The linkage struct for list nodes. This struct must be part of your
100  * to-be-linked struct. struct list is required for both the head of the
101  * list and for each list node.
102  *
103  * Position and name of the struct list field is irrelevant.
104  * There are no requirements that elements of a list are of the same type.
105  * There are no requirements for a list head, any struct list can be a list
106  * head.
107  */
108 struct list {
109     struct list *next, *prev;
110 };
112 /**
113  * Initialize the list as an empty list.
114  *
115  * Example:
116  * list_init(&bar->list_of_foos);
117  *
118  * @param The list to initialized.
119  */
120 static void
121 list_init(struct list *list)
123     list->next = list->prev = list;
126 static inline void
127 __list_add(struct list *entry,
128             struct list *prev,
129             struct list *next)
131     next->prev = entry;
132     entry->next = next;
133     entry->prev = prev;
134     prev->next = entry;
137 /**
138  * Insert a new element after the given list head. The new element does not
139  * need to be initialised as empty list.
140  * The list changes from:
141  *      head → some element → ...
142  * to
143  *      head → new element → older element → ...
144  *
145  * Example:
146  * struct foo *newfoo = malloc(...);
147  * list_add(&newfoo->entry, &bar->list_of_foos);
148  *
149  * @param entry The new element to prepend to the list.
150  * @param head The existing list.
151  */
152 static inline void
153 list_add(struct list *entry, struct list *head)
155     __list_add(entry, head, head->next);
158 /**
159  * Append a new element to the end of the list given with this list head.
160  *
161  * The list changes from:
162  *      head → some element → ... → lastelement
163  * to
164  *      head → some element → ... → lastelement → new element
165  *
166  * Example:
167  * struct foo *newfoo = malloc(...);
168  * list_append(&newfoo->entry, &bar->list_of_foos);
169  *
170  * @param entry The new element to prepend to the list.
171  * @param head The existing list.
172  */
173 static inline void
174 list_append(struct list *entry, struct list *head)
176     __list_add(entry, head->prev, head);
180 static inline void
181 __list_del(struct list *prev, struct list *next)
183     next->prev = prev;
184     prev->next = next;
187 /**
188  * Remove the element from the list it is in. Using this function will reset
189  * the pointers to/from this element so it is removed from the list. It does
190  * NOT free the element itself or manipulate it otherwise.
191  *
192  * Using list_del on a pure list head (like in the example at the top of
193  * this file) will NOT remove the first element from
194  * the list but rather reset the list as empty list.
195  *
196  * Example:
197  * list_del(&foo->entry);
198  *
199  * @param entry The element to remove.
200  */
201 static inline void
202 list_del(struct list *entry)
204     __list_del(entry->prev, entry->next);
205     list_init(entry);
208 /**
209  * Check if the list is empty.
210  *
211  * Example:
212  * list_is_empty(&bar->list_of_foos);
213  *
214  * @return True if the list contains one or more elements or False otherwise.
215  */
216 static inline bool
217 list_is_empty(struct list *head)
219     return head->next == head;
222 /**
223  * Returns a pointer to the container of this list element.
224  *
225  * Example:
226  * struct foo* f;
227  * f = container_of(&foo->entry, struct foo, entry);
228  * assert(f == foo);
229  *
230  * @param ptr Pointer to the struct list.
231  * @param type Data type of the list element.
232  * @param member Member name of the struct list field in the list element.
233  * @return A pointer to the data struct containing the list head.
234  */
235 #ifndef container_of
236 #define container_of(ptr, type, member) \
237     (type *)((char *)(ptr) - (char *) &((type *)0)->member)
238 #endif
240 /**
241  * Alias of container_of
242  */
243 #define list_entry(ptr, type, member) \
244     container_of(ptr, type, member)
246 /**
247  * Retrieve the first list entry for the given list pointer.
248  *
249  * Example:
250  * struct foo *first;
251  * first = list_first_entry(&bar->list_of_foos, struct foo, list_of_foos);
252  *
253  * @param ptr The list head
254  * @param type Data type of the list element to retrieve
255  * @param member Member name of the struct list field in the list element.
256  * @return A pointer to the first list element.
257  */
258 #define list_first_entry(ptr, type, member) \
259     list_entry((ptr)->next, type, member)
261 /**
262  * Retrieve the last list entry for the given listpointer.
263  *
264  * Example:
265  * struct foo *first;
266  * first = list_last_entry(&bar->list_of_foos, struct foo, list_of_foos);
267  *
268  * @param ptr The list head
269  * @param type Data type of the list element to retrieve
270  * @param member Member name of the struct list field in the list element.
271  * @return A pointer to the last list element.
272  */
273 #define list_last_entry(ptr, type, member) \
274     list_entry((ptr)->prev, type, member)
276 #define __container_of(ptr, sample, member)                             \
277     (void *)((char *)(ptr)                                              \
278              - ((char *)&(sample)->member - (char *)(sample)))
279 /**
280  * Loop through the list given by head and set pos to struct in the list.
281  *
282  * Example:
283  * struct foo *iterator;
284  * list_for_each_entry(iterator, &bar->list_of_foos, entry) {
285  *      [modify iterator]
286  * }
287  *
288  * This macro is not safe for node deletion. Use list_for_each_entry_safe
289  * instead.
290  *
291  * @param pos Iterator variable of the type of the list elements.
292  * @param head List head
293  * @param member Member name of the struct list in the list elements.
294  *
295  */
296 #define list_for_each_entry(pos, head, member)                          \
297     for (pos = __container_of((head)->next, pos, member);               \
298          &pos->member != (head);                                        \
299          pos = __container_of(pos->member.next, pos, member))
301 /**
302  * Loop through the list, keeping a backup pointer to the element. This
303  * macro allows for the deletion of a list element while looping through the
304  * list.
305  *
306  * See list_for_each_entry for more details.
307  */
308 #define list_for_each_entry_safe(pos, tmp, head, member)                \
309     for (pos = __container_of((head)->next, pos, member),               \
310          tmp = __container_of(pos->member.next, pos, member);           \
311          &pos->member != (head);                                        \
312          pos = tmp, tmp = __container_of(pos->member.next, tmp, member))
314 #endif