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
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[55b77d9]3 * Copyright (c) 2011 Jiri Svoboda
[f761f1eb]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
[06e1e95]30/** @addtogroup genericadt
[b45c443]31 * @{
32 */
33/** @file
34 */
35
[06e1e95]36#ifndef KERN_LIST_H_
37#define KERN_LIST_H_
[f761f1eb]38
[7ac426e]39#include <typedefs.h>
[7a0359b]40#include <trace.h>
[f761f1eb]41
[55b77d9]42/** Doubly linked list link. */
[b3f8fb7]43typedef struct link {
[0a02653]44 struct link *prev; /**< Pointer to the previous item in the list. */
45 struct link *next; /**< Pointer to the next item in the list. */
[b3f8fb7]46} link_t;
[f761f1eb]47
[55b77d9]48/** Doubly linked list. */
49typedef struct list {
50 link_t head; /**< List head. Does not have any data. */
51} list_t;
52
[7dd2561]53/** Declare and initialize statically allocated list.
54 *
55 * @param name Name of the new statically allocated list.
[0a02653]56 *
[7dd2561]57 */
[80bcaed]58#define LIST_INITIALIZE(name) \
[55b77d9]59 list_t name = { \
60 .head = { \
61 .prev = &(name).head, \
62 .next = &(name).head \
63 } \
[0a02653]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) \
[55b77d9]70 for (link_t *iterator = (list).head.next; \
71 iterator != &(list).head; iterator = iterator->next)
[7dd2561]72
[ef1603b]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
[b72efe8]105#define assert_link_not_used(link) \
[14a60e3]106 ASSERT(((link)->prev == NULL) && ((link)->next == NULL))
[b72efe8]107
[40a468a]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.
[0a02653]113 *
[40a468a]114 */
[7a0359b]115NO_TRACE static inline void link_initialize(link_t *link)
[c0a91d1]116{
117 link->prev = NULL;
118 link->next = NULL;
[f761f1eb]119}
120
[ef1603b]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
[40a468a]132/** Initialize doubly-linked circular list
133 *
134 * Initialize doubly-linked circular list.
135 *
[55b77d9]136 * @param list Pointer to list_t structure.
[0a02653]137 *
[40a468a]138 */
[55b77d9]139NO_TRACE static inline void list_initialize(list_t *list)
[c0a91d1]140{
[55b77d9]141 list->head.prev = &list->head;
142 list->head.next = &list->head;
[f761f1eb]143}
144
[55b77d9]145/** Insert item before another item in doubly-linked circular list.
[0a02653]146 *
[40a468a]147 */
[55b77d9]148static inline void list_insert_before(link_t *lnew, link_t *lold)
[c0a91d1]149{
[55b77d9]150 lnew->next = lold;
151 lnew->prev = lold->prev;
152 lold->prev->next = lnew;
153 lold->prev = lnew;
[f761f1eb]154}
155
[55b77d9]156/** Insert item after another item in doubly-linked circular list.
[0a02653]157 *
[40a468a]158 */
[55b77d9]159static inline void list_insert_after(link_t *lnew, link_t *lold)
[c0a91d1]160{
[55b77d9]161 lnew->prev = lold;
162 lnew->next = lold->next;
163 lold->next->prev = lnew;
164 lold->next = lnew;
[0a02653]165}
166
[55b77d9]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.
[0a02653]173 *
174 */
[55b77d9]175NO_TRACE static inline void list_prepend(link_t *link, list_t *list)
[0a02653]176{
[55b77d9]177 list_insert_after(link, &list->head);
[0a02653]178}
179
[55b77d9]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.
[0a02653]186 *
187 */
[55b77d9]188NO_TRACE static inline void list_append(link_t *link, list_t *list)
[0a02653]189{
[55b77d9]190 list_insert_before(link, &list->head);
[f761f1eb]191}
192
[40a468a]193/** Remove item from doubly-linked circular list
194 *
195 * Remove item from doubly-linked circular list.
196 *
[0a02653]197 * @param link Pointer to link_t structure to be removed from the list
198 * it is contained in.
199 *
[40a468a]200 */
[7a0359b]201NO_TRACE static inline void list_remove(link_t *link)
[c0a91d1]202{
[14a60e3]203 if ((link->prev != NULL) && (link->next != NULL)) {
204 link->next->prev = link->prev;
205 link->prev->next = link->next;
206 }
207
[c0a91d1]208 link_initialize(link);
[f761f1eb]209}
210
[40a468a]211/** Query emptiness of doubly-linked circular list
212 *
213 * Query emptiness of doubly-linked circular list.
214 *
[55b77d9]215 * @param list Pointer to lins_t structure.
[0a02653]216 *
[40a468a]217 */
[4748038]218NO_TRACE static inline int list_empty(const list_t *list)
[40a468a]219{
[55b77d9]220 return (list->head.next == &list->head);
[40a468a]221}
222
[55b77d9]223/** Get first item in list.
[0a02653]224 *
[55b77d9]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 */
[4748038]231static inline link_t *list_first(const list_t *list)
[55b77d9]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.
[0a02653]239 *
240 * @return Head item of the list.
241 * @return NULL if the list is empty.
242 *
243 */
[55b77d9]244static inline link_t *list_last(list_t *list)
[0a02653]245{
[55b77d9]246 return ((list->head.prev == &list->head) ? NULL : list->head.prev);
[0a02653]247}
[40a468a]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 *
[0a02653]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 *
[40a468a]261 */
[7a0359b]262NO_TRACE static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
[40a468a]263{
264 part1->prev->next = part2;
[0a02653]265 part2->prev->next = part1;
266
267 link_t *hlp = part1->prev;
268
[40a468a]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 *
[0a02653]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 *
[40a468a]282 */
[7a0359b]283NO_TRACE static inline void headless_list_split(link_t *part1, link_t *part2)
[40a468a]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 *
[0a02653]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 *
[40a468a]297 */
[7a0359b]298NO_TRACE static inline void headless_list_concat(link_t *part1, link_t *part2)
[40a468a]299{
300 headless_list_split_or_concat(part1, part2);
301}
[f761f1eb]302
[2ee1ccc]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
[55b77d9]334/** Get n-th item in a list.
[0a02653]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 */
[55b77d9]343static inline link_t *list_nth(list_t *list, unsigned int n)
[0a02653]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}
[f761f1eb]356
[55b77d9]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 *);
[f761f1eb]360
361#endif
[b45c443]362
[06e1e95]363/** @}
[b45c443]364 */
Note: See TracBrowser for help on using the repository browser.