2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2010 Francisco Jerez <currojerez@riseup.net>
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:
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
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
29 #include <stddef.h> /* offsetof() */
32 * @file Classic doubly-link circular list implementation.
33 * For real usage examples of the linked list, see the file test/list.c
36 * We need to keep a list of struct foo in the parent struct bar, i.e. what
37 * we want is something like this.
41 * struct foo *list_of_foos; -----> struct foo {}, struct foo {}, struct foo{}
45 * We need one list head in bar and a list element in all list_of_foos (both are of
46 * data type 'struct xorg_list').
50 * struct xorg_list list_of_foos;
56 * struct xorg_list entry;
60 * Now we initialize the list head:
64 * xorg_list_init(&bar.list_of_foos);
66 * Then we create the first element and add it to this list:
68 * struct foo *foo = malloc(...);
70 * xorg_list_add(&foo->entry, &bar.list_of_foos);
72 * Repeat the above for each element you want to add to the list. Deleting
73 * works with the element itself.
74 * xorg_list_del(&foo->entry);
77 * Note: calling xorg_list_del(&bar.list_of_foos) will set bar.list_of_foos to an empty
80 * Looping through the list requires a 'struct foo' as iterator and the
81 * name of the field the subnodes use.
83 * struct foo *iterator;
84 * xorg_list_for_each_entry(iterator, &bar.list_of_foos, entry) {
85 * if (iterator->something == ...)
89 * Note: You must not call xorg_list_del() on the iterator if you continue the
90 * loop. You need to run the safe for-each loop instead:
92 * struct foo *iterator, *next;
93 * xorg_list_for_each_entry_safe(iterator, next, &bar.list_of_foos, entry) {
95 * xorg_list_del(&iterator->entry);
101 * The linkage struct for list nodes. This struct must be part of your
102 * to-be-linked struct. struct xorg_list is required for both the head of the
103 * list and for each list node.
105 * Position and name of the struct xorg_list field is irrelevant.
106 * There are no requirements that elements of a list are of the same type.
107 * There are no requirements for a list head, any struct xorg_list can be a list
111 struct xorg_list
*next
, *prev
;
115 * Initialize the list as an empty list.
118 * xorg_list_init(&bar->list_of_foos);
120 * @param The list to initialized.
123 xorg_list_init(struct xorg_list
*list
)
125 list
->next
= list
->prev
= list
;
129 __xorg_list_add(struct xorg_list
*entry
,
130 struct xorg_list
*prev
, struct xorg_list
*next
)
139 * Insert a new element after the given list head. The new element does not
140 * need to be initialised as empty list.
141 * The list changes from:
142 * head → some element → ...
144 * head → new element → older element → ...
147 * struct foo *newfoo = malloc(...);
148 * xorg_list_add(&newfoo->entry, &bar->list_of_foos);
150 * @param entry The new element to prepend to the list.
151 * @param head The existing list.
154 xorg_list_add(struct xorg_list
*entry
, struct xorg_list
*head
)
156 __xorg_list_add(entry
, head
, head
->next
);
160 * Append a new element to the end of the list given with this list head.
162 * The list changes from:
163 * head → some element → ... → lastelement
165 * head → some element → ... → lastelement → new element
168 * struct foo *newfoo = malloc(...);
169 * xorg_list_append(&newfoo->entry, &bar->list_of_foos);
171 * @param entry The new element to prepend to the list.
172 * @param head The existing list.
175 xorg_list_append(struct xorg_list
*entry
, struct xorg_list
*head
)
177 __xorg_list_add(entry
, head
->prev
, head
);
181 __xorg_list_del(struct xorg_list
*prev
, struct xorg_list
*next
)
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.
192 * Using xorg_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.
197 * xorg_list_del(&foo->entry);
199 * @param entry The element to remove.
202 xorg_list_del(struct xorg_list
*entry
)
204 __xorg_list_del(entry
->prev
, entry
->next
);
205 xorg_list_init(entry
);
209 * Check if the list is empty.
212 * xorg_list_is_empty(&bar->list_of_foos);
214 * @return True if the list contains one or more elements or False otherwise.
217 xorg_list_is_empty(struct xorg_list
*head
)
219 return head
->next
== head
;
223 * Returns a pointer to the container of this list element.
227 * f = container_of(&foo->entry, struct foo, entry);
230 * @param ptr Pointer to the struct xorg_list.
231 * @param type Data type of the list element.
232 * @param member Member name of the struct xorg_list field in the list element.
233 * @return A pointer to the data struct containing the list head.
236 #define container_of(ptr, type, member) \
237 (type *)((char *)(ptr) - offsetof(type, member))
241 * Alias of container_of
243 #define xorg_list_entry(ptr, type, member) \
244 container_of(ptr, type, member)
247 * Retrieve the first list entry for the given list pointer.
251 * first = xorg_list_first_entry(&bar->list_of_foos, struct foo, list_of_foos);
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 xorg_list field in the list element.
256 * @return A pointer to the first list element.
258 #define xorg_list_first_entry(ptr, type, member) \
259 xorg_list_entry((ptr)->next, type, member)
262 * Retrieve the last list entry for the given listpointer.
266 * first = xorg_list_last_entry(&bar->list_of_foos, struct foo, list_of_foos);
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 xorg_list field in the list element.
271 * @return A pointer to the last list element.
273 #define xorg_list_last_entry(ptr, type, member) \
274 xorg_list_entry((ptr)->prev, type, member)
277 #define __container_of(ptr, sample, member) \
278 container_of(ptr, typeof(*sample), member)
280 /* This implementation of __container_of has undefined behavior according
281 * to the C standard, but it works in many cases. If your compiler doesn't
282 * support typeof() and fails with this implementation, please try a newer
285 #define __container_of(ptr, sample, member) \
286 (void *)((char *)(ptr) \
287 - ((char *)&(sample)->member - (char *)(sample)))
291 * Loop through the list given by head and set pos to struct in the list.
294 * struct foo *iterator;
295 * xorg_list_for_each_entry(iterator, &bar->list_of_foos, entry) {
299 * This macro is not safe for node deletion. Use xorg_list_for_each_entry_safe
302 * @param pos Iterator variable of the type of the list elements.
303 * @param head List head
304 * @param member Member name of the struct xorg_list in the list elements.
307 #define xorg_list_for_each_entry(pos, head, member) \
308 for (pos = __container_of((head)->next, pos, member); \
309 &pos->member != (head); \
310 pos = __container_of(pos->member.next, pos, member))
313 * Loop through the list, keeping a backup pointer to the element. This
314 * macro allows for the deletion of a list element while looping through the
317 * See xorg_list_for_each_entry for more details.
319 #define xorg_list_for_each_entry_safe(pos, tmp, head, member) \
320 for (pos = __container_of((head)->next, pos, member), \
321 tmp = __container_of(pos->member.next, pos, member); \
322 &pos->member != (head); \
323 pos = tmp, tmp = __container_of(pos->member.next, tmp, member))
325 /* NULL-Terminated List Interface
327 * The interface below does _not_ use the struct xorg_list as described above.
328 * It is mainly for legacy structures that cannot easily be switched to
331 * This interface is for structs like
338 * The position and field name of "next" are arbitrary.
342 * Init the element as null-terminated list.
345 * struct foo *list = malloc();
346 * nt_list_init(list, next);
348 * @param list The list element that will be the start of the list
349 * @param member Member name of the field pointing to next struct
351 #define nt_list_init(_list, _member) \
352 (_list)->_member = NULL
355 * Returns the next element in the list or NULL on termination.
358 * struct foo *element = list;
359 * while ((element = nt_list_next(element, next)) { }
361 * This macro is not safe for node deletion. Use nt_list_for_each_entry_safe
364 * @param list The list or current element.
365 * @param member Member name of the field pointing to next struct.
367 #define nt_list_next(_list, _member) \
371 * Iterate through each element in the list.
374 * struct foo *iterator;
375 * nt_list_for_each_entry(iterator, list, next) {
379 * @param entry Assigned to the current list element
380 * @param list The list to iterate through.
381 * @param member Member name of the field pointing to next struct.
383 #define nt_list_for_each_entry(_entry, _list, _member) \
384 for (_entry = _list; _entry; _entry = (_entry)->_member)
387 * Iterate through each element in the list, keeping a backup pointer to the
388 * element. This macro allows for the deletion of a list element while
389 * looping through the list.
391 * See nt_list_for_each_entry for more details.
393 * @param entry Assigned to the current list element
394 * @param tmp The pointer to the next element
395 * @param list The list to iterate through.
396 * @param member Member name of the field pointing to next struct.
398 #define nt_list_for_each_entry_safe(_entry, _tmp, _list, _member) \
399 for (_entry = _list, _tmp = (_entry) ? (_entry)->_member : NULL;\
401 _entry = _tmp, _tmp = (_tmp) ? (_tmp)->_member: NULL)
404 * Append the element to the end of the list. This macro may be used to
408 * struct foo *elem = malloc(...);
409 * nt_list_init(elem, next)
410 * nt_list_append(elem, list, struct foo, next);
412 * Resulting list order:
413 * list_item_0 -> list_item_1 -> ... -> elem_item_0 -> elem_item_1 ...
415 * @param entry An entry (or list) to append to the list
416 * @param list The list to append to. This list must be a valid list, not
418 * @param type The list type
419 * @param member Member name of the field pointing to next struct
421 #define nt_list_append(_entry, _list, _type, _member) \
423 _type *__iterator = _list; \
424 while (__iterator->_member) { __iterator = __iterator->_member;}\
425 __iterator->_member = _entry; \
429 * Insert the element at the next position in the list. This macro may be
430 * used to insert a list into a list.
432 * struct foo *elem = malloc(...);
433 * nt_list_init(elem, next)
434 * nt_list_insert(elem, list, struct foo, next);
436 * Resulting list order:
437 * list_item_0 -> elem_item_0 -> elem_item_1 ... -> list_item_1 -> ...
439 * @param entry An entry (or list) to append to the list
440 * @param list The list to insert to. This list must be a valid list, not
442 * @param type The list type
443 * @param member Member name of the field pointing to next struct
445 #define nt_list_insert(_entry, _list, _type, _member) \
447 nt_list_append((_list)->_member, _entry, _type, _member); \
448 (_list)->_member = _entry; \
452 * Delete the entry from the list by iterating through the list and
453 * removing any reference from the list to the entry.
456 * struct foo *elem = <assign to right element>
457 * nt_list_del(elem, list, struct foo, next);
459 * @param entry The entry to delete from the list. entry is always
460 * re-initialized as a null-terminated list.
461 * @param list The list containing the entry, set to the new list without
463 * @param type The list type
464 * @param member Member name of the field pointing to the next entry
466 #define nt_list_del(_entry, _list, _type, _member) \
468 _type *__e = _entry; \
469 if (__e == NULL || _list == NULL) break; \
470 if ((_list) == __e) { \
471 _list = __e->_member; \
473 _type *__prev = _list; \
474 while (__prev->_member && __prev->_member != __e) \
475 __prev = nt_list_next(__prev, _member); \
476 if (__prev->_member) \
477 __prev->_member = __e->_member; \
479 nt_list_init(__e, _member); \
484 * This is a remainder of the xfree86 DDX attempt of having a set of generic
485 * list functions. Unfortunately, the xf86OptionRec uses it and we can't
486 * easily get rid of it. Do not use for new code.
488 typedef struct generic_list_rec
{
490 } GenericListRec
, *GenericListPtr
, *glp
;