2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 struct ao_list *next, *prev;
28 ao_list_init(struct ao_list *list)
30 list->next = list->prev = list;
34 __ao_list_add(struct ao_list *list, struct ao_list *prev, struct ao_list *next)
43 * Insert a new element after the given list head. The new element does not
44 * need to be initialised as empty list.
45 * The list changes from:
46 * head → some element → ...
48 * head → new element → older element → ...
51 * struct foo *newfoo = malloc(...);
52 * ao_list_add(&newfoo->entry, &bar->list_of_foos);
54 * @param entry The new element to prepend to the list.
55 * @param head The existing list.
58 ao_list_insert(struct ao_list *entry, struct ao_list *head)
60 __ao_list_add(entry, head, head->next);
64 * Append a new element to the end of the list given with this list head.
66 * The list changes from:
67 * head → some element → ... → lastelement
69 * head → some element → ... → lastelement → new element
72 * struct foo *newfoo = malloc(...);
73 * ao_list_append(&newfoo->entry, &bar->list_of_foos);
75 * @param entry The new element to prepend to the list.
76 * @param head The existing list.
79 ao_list_append(struct ao_list *entry, struct ao_list *head)
81 __ao_list_add(entry, head->prev, head);
85 __ao_list_del(struct ao_list *prev, struct ao_list *next)
92 * Remove the element from the list it is in. Using this function will reset
93 * the pointers to/from this element so it is removed from the list. It does
94 * NOT free the element itself or manipulate it otherwise.
96 * Using ao_list_del on a pure list head (like in the example at the top of
97 * this file) will NOT remove the first element from
98 * the list but rather reset the list as empty list.
101 * ao_list_del(&foo->entry);
103 * @param entry The element to remove.
106 ao_list_del(struct ao_list *entry)
108 __ao_list_del(entry->prev, entry->next);
113 * Check if the list is empty.
116 * ao_list_is_empty(&bar->list_of_foos);
118 * @return True if the list contains one or more elements or False otherwise.
120 static inline uint8_t
121 ao_list_is_empty(struct ao_list *head)
123 return head->next == head;
127 * Returns a pointer to the container of this list element.
131 * f = container_of(&foo->entry, struct foo, entry);
134 * @param ptr Pointer to the struct ao_list.
135 * @param type Data type of the list element.
136 * @param member Member name of the struct ao_list field in the list element.
137 * @return A pointer to the data struct containing the list head.
139 #define ao_container_of(ptr, type, member) \
140 ((type *)((char *)(ptr) - offsetof(type, member)))
143 * Alias of ao_container_of
145 #define ao_list_entry(ptr, type, member) \
146 ao_container_of(ptr, type, member)
149 * Retrieve the first list entry for the given list pointer.
153 * first = ao_list_first_entry(&bar->list_of_foos, struct foo, list_of_foos);
155 * @param ptr The list head
156 * @param type Data type of the list element to retrieve
157 * @param member Member name of the struct ao_list field in the list element.
158 * @return A pointer to the first list element.
160 #define ao_list_first_entry(ptr, type, member) \
161 ao_list_entry((ptr)->next, type, member)
164 * Retrieve the last list entry for the given listpointer.
168 * first = ao_list_last_entry(&bar->list_of_foos, struct foo, list_of_foos);
170 * @param ptr The list head
171 * @param type Data type of the list element to retrieve
172 * @param member Member name of the struct ao_list field in the list element.
173 * @return A pointer to the last list element.
175 #define ao_list_last_entry(ptr, type, member) \
176 ao_list_entry((ptr)->prev, type, member)
179 * Loop through the list given by head and set pos to struct in the list.
182 * struct foo *iterator;
183 * ao_list_for_each_entry(iterator, &bar->list_of_foos, entry) {
187 * This macro is not safe for node deletion. Use ao_list_for_each_entry_safe
190 * @param pos Iterator variable of the type of the list elements.
191 * @param head List head
192 * @param member Member name of the struct ao_list in the list elements.
195 #define ao_list_for_each_entry(pos, head, type, member) \
196 for (pos = ao_container_of((head)->next, type, member); \
197 &pos->member != (head); \
198 pos = ao_container_of(pos->member.next, type, member))
201 * Loop through the list, keeping a backup pointer to the element. This
202 * macro allows for the deletion of a list element while looping through the
205 * See ao_list_for_each_entry for more details.
207 #define ao_list_for_each_entry_safe(pos, tmp, head, type, member) \
208 for ((pos = ao_container_of((head)->next, type, member)), \
209 (tmp = ao_container_of(pos->member.next, type, member)); \
210 &pos->member != (head); \
211 (pos = tmp), (tmp = ao_container_of(pos->member.next, type, member)))
213 #endif /* _AO_LIST_H_ */