source: mainline/kernel/generic/include/adt/list.h@ ef1603b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ef1603b was ef1603b, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

adt: list_foreach_safe() enables item removal while traversing a list.

  • Property mode set to 100644
File size: 9.5 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * Copyright (c) 2011 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup genericadt
31 * @{
32 */
33/** @file
34 */
35
36#ifndef KERN_LIST_H_
37#define KERN_LIST_H_
38
39#include <typedefs.h>
40#include <trace.h>
41
42/** Doubly linked list link. */
43typedef struct link {
44 struct link *prev; /**< Pointer to the previous item in the list. */
45 struct link *next; /**< Pointer to the next item in the list. */
46} link_t;
47
48/** Doubly linked list. */
49typedef struct list {
50 link_t head; /**< List head. Does not have any data. */
51} list_t;
52
53/** Declare and initialize statically allocated list.
54 *
55 * @param name Name of the new statically allocated list.
56 *
57 */
58#define LIST_INITIALIZE(name) \
59 list_t name = { \
60 .head = { \
61 .prev = &(name).head, \
62 .next = &(name).head \
63 } \
64 }
65
66#define list_get_instance(link, type, member) \
67 ((type *) (((void *)(link)) - ((void *) &(((type *) NULL)->member))))
68
69#define list_foreach(list, iterator) \
70 for (link_t *iterator = (list).head.next; \
71 iterator != &(list).head; iterator = iterator->next)
72
73/** Unlike list_foreach(), allows removing items while traversing a list.
74 *
75 * @code
76 * list_t mylist;
77 * typedef struct item {
78 * int value;
79 * link_t item_link;
80 * } item_t;
81 *
82 * //..
83 *
84 * // Print each list element's value and remove the element from the list.
85 * list_foreach_safe(mylist, cur_link, next_link) {
86 * item_t *cur_item = list_get_instance(cur_link, item_t, item_link);
87 * printf("%d\n", cur_item->value);
88 * list_remove(cur_link);
89 * }
90 * @endcode
91 *
92 * @param list List to traverse.
93 * @param iterator Iterator to the current element of the list.
94 * The item this iterator points may be safely removed
95 * from the list.
96 * @param next_iter Iterator to the next element of the list.
97 */
98#define list_foreach_safe(list, iterator, next_iter) \
99 for (link_t *iterator = (list).head.next, \
100 *next_iter = iterator->next; \
101 iterator != &(list).head; \
102 iterator = next_iter, next_iter = iterator->next)
103
104
105#define assert_link_not_used(link) \
106 ASSERT(((link)->prev == NULL) && ((link)->next == NULL))
107
108/** Initialize doubly-linked circular list link
109 *
110 * Initialize doubly-linked list link.
111 *
112 * @param link Pointer to link_t structure to be initialized.
113 *
114 */
115NO_TRACE static inline void link_initialize(link_t *link)
116{
117 link->prev = NULL;
118 link->next = NULL;
119}
120
121/** Returns true if the initialized link is already in use by any list.
122 *
123 * @param link Link to examine whether if belongs to a list or not.
124 * @return 1 if the link is part of a list.
125 * @return 0 otherwise.
126 */
127NO_TRACE static inline int link_used(const link_t *link)
128{
129 return link->prev != NULL || link->next != NULL;
130}
131
132/** Initialize doubly-linked circular list
133 *
134 * Initialize doubly-linked circular list.
135 *
136 * @param list Pointer to list_t structure.
137 *
138 */
139NO_TRACE static inline void list_initialize(list_t *list)
140{
141 list->head.prev = &list->head;
142 list->head.next = &list->head;
143}
144
145/** Insert item before another item in doubly-linked circular list.
146 *
147 */
148static inline void list_insert_before(link_t *lnew, link_t *lold)
149{
150 lnew->next = lold;
151 lnew->prev = lold->prev;
152 lold->prev->next = lnew;
153 lold->prev = lnew;
154}
155
156/** Insert item after another item in doubly-linked circular list.
157 *
158 */
159static inline void list_insert_after(link_t *lnew, link_t *lold)
160{
161 lnew->prev = lold;
162 lnew->next = lold->next;
163 lold->next->prev = lnew;
164 lold->next = lnew;
165}
166
167/** Add item to the beginning of doubly-linked circular list
168 *
169 * Add item to the beginning of doubly-linked circular list.
170 *
171 * @param link Pointer to link_t structure to be added.
172 * @param list Pointer to list_t structure.
173 *
174 */
175NO_TRACE static inline void list_prepend(link_t *link, list_t *list)
176{
177 list_insert_after(link, &list->head);
178}
179
180/** Add item to the end of doubly-linked circular list
181 *
182 * Add item to the end of doubly-linked circular list.
183 *
184 * @param link Pointer to link_t structure to be added.
185 * @param list Pointer to list_t structure.
186 *
187 */
188NO_TRACE static inline void list_append(link_t *link, list_t *list)
189{
190 list_insert_before(link, &list->head);
191}
192
193/** Remove item from doubly-linked circular list
194 *
195 * Remove item from doubly-linked circular list.
196 *
197 * @param link Pointer to link_t structure to be removed from the list
198 * it is contained in.
199 *
200 */
201NO_TRACE static inline void list_remove(link_t *link)
202{
203 if ((link->prev != NULL) && (link->next != NULL)) {
204 link->next->prev = link->prev;
205 link->prev->next = link->next;
206 }
207
208 link_initialize(link);
209}
210
211/** Query emptiness of doubly-linked circular list
212 *
213 * Query emptiness of doubly-linked circular list.
214 *
215 * @param list Pointer to lins_t structure.
216 *
217 */
218NO_TRACE static inline int list_empty(const list_t *list)
219{
220 return (list->head.next == &list->head);
221}
222
223/** Get first item in list.
224 *
225 * @param list Pointer to list_t structure.
226 *
227 * @return Head item of the list.
228 * @return NULL if the list is empty.
229 *
230 */
231static inline link_t *list_first(const list_t *list)
232{
233 return ((list->head.next == &list->head) ? NULL : list->head.next);
234}
235
236/** Get last item in list.
237 *
238 * @param list Pointer to list_t structure.
239 *
240 * @return Head item of the list.
241 * @return NULL if the list is empty.
242 *
243 */
244static inline link_t *list_last(list_t *list)
245{
246 return ((list->head.prev == &list->head) ? NULL : list->head.prev);
247}
248
249/** Split or concatenate headless doubly-linked circular list
250 *
251 * Split or concatenate headless doubly-linked circular list.
252 *
253 * Note that the algorithm works both directions:
254 * concatenates splitted lists and splits concatenated lists.
255 *
256 * @param part1 Pointer to link_t structure leading the first
257 * (half of the headless) list.
258 * @param part2 Pointer to link_t structure leading the second
259 * (half of the headless) list.
260 *
261 */
262NO_TRACE static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
263{
264 part1->prev->next = part2;
265 part2->prev->next = part1;
266
267 link_t *hlp = part1->prev;
268
269 part1->prev = part2->prev;
270 part2->prev = hlp;
271}
272
273/** Split headless doubly-linked circular list
274 *
275 * Split headless doubly-linked circular list.
276 *
277 * @param part1 Pointer to link_t structure leading
278 * the first half of the headless list.
279 * @param part2 Pointer to link_t structure leading
280 * the second half of the headless list.
281 *
282 */
283NO_TRACE static inline void headless_list_split(link_t *part1, link_t *part2)
284{
285 headless_list_split_or_concat(part1, part2);
286}
287
288/** Concatenate two headless doubly-linked circular lists
289 *
290 * Concatenate two headless doubly-linked circular lists.
291 *
292 * @param part1 Pointer to link_t structure leading
293 * the first headless list.
294 * @param part2 Pointer to link_t structure leading
295 * the second headless list.
296 *
297 */
298NO_TRACE static inline void headless_list_concat(link_t *part1, link_t *part2)
299{
300 headless_list_split_or_concat(part1, part2);
301}
302
303/** Moves items of one list into another after the specified item.
304 *
305 * Inserts all items of @a list after item at @a pos in another list.
306 * Both lists may be empty.
307 *
308 * In order to insert the list at the beginning of another list, use:
309 * @code
310 * list_splice(&list_dest.head, &list_src);
311 * @endcode
312 *
313 * @param list Source list to move after pos.
314 * @param pos Source items will be placed after this item.
315 */
316NO_TRACE static inline void list_splice(list_t *list, link_t *pos)
317{
318 link_t *pos_next = pos->next;
319
320 if (!list_empty(list)) {
321 link_t *first = list->head.next;
322 link_t *last = list->head.prev;
323
324 pos->next = first;
325 first->prev = pos;
326
327 last->next = pos_next;
328 pos_next->prev = last;
329
330 list_initialize(list);
331 }
332}
333
334/** Get n-th item in a list.
335 *
336 * @param list Pointer to link_t structure representing the list.
337 * @param n Item number (indexed from zero).
338 *
339 * @return n-th item of the list.
340 * @return NULL if no n-th item found.
341 *
342 */
343static inline link_t *list_nth(list_t *list, unsigned int n)
344{
345 unsigned int cnt = 0;
346
347 list_foreach(*list, link) {
348 if (cnt == n)
349 return link;
350
351 cnt++;
352 }
353
354 return NULL;
355}
356
357extern int list_member(const link_t *, const list_t *);
358extern void list_concat(list_t *, list_t *);
359extern unsigned int list_count(const list_t *);
360
361#endif
362
363/** @}
364 */
Note: See TracBrowser for help on using the repository browser.