source: mainline/uspace/lib/c/include/adt/list.h@ 85147f3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 85147f3 was feeac0d, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Simplify use of list_foreach.

  • Property mode set to 100644
File size: 9.5 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * Copyright (c) 2013 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 libc
31 * @{
32 */
33/** @file
34 */
35
36#ifndef LIBC_LIST_H_
37#define LIBC_LIST_H_
38
39#include <assert.h>
40#include <unistd.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)) - list_link_to_void(&(((type *) NULL)->member))))
68
69#define list_foreach(list, member, itype, iterator) \
70 for (itype *iterator = NULL; iterator == NULL; iterator =(itype *)1) \
71 for (link_t *_link = (list).head.next; \
72 iterator = list_get_instance(_link, itype, member), \
73 _link != &(list).head; _link = _link->next)
74
75/** Unlike list_foreach(), allows removing items while traversing a list.
76 *
77 * @code
78 * list_t mylist;
79 * typedef struct item {
80 * int value;
81 * link_t item_link;
82 * } item_t;
83 *
84 * //..
85 *
86 * // Print each list element's value and remove the element from the list.
87 * list_foreach_safe(mylist, cur_link, next_link) {
88 * item_t *cur_item = list_get_instance(cur_link, item_t, item_link);
89 * printf("%d\n", cur_item->value);
90 * list_remove(cur_link);
91 * }
92 * @endcode
93 *
94 * @param list List to traverse.
95 * @param iterator Iterator to the current element of the list.
96 * The item this iterator points may be safely removed
97 * from the list.
98 * @param next_iter Iterator to the next element of the list.
99 */
100#define list_foreach_safe(list, iterator, next_iter) \
101 for (link_t *iterator = (list).head.next, \
102 *next_iter = iterator->next; \
103 iterator != &(list).head; \
104 iterator = next_iter, next_iter = iterator->next)
105
106#define assert_link_not_used(link) \
107 assert(((link)->prev == NULL) && ((link)->next == NULL))
108
109/** Returns true if the link is definitely part of a list. False if not sure. */
110static inline int link_in_use(link_t *link)
111{
112 return link->prev != NULL && link->next != NULL;
113}
114
115/** Initialize doubly-linked circular list link
116 *
117 * Initialize doubly-linked list link.
118 *
119 * @param link Pointer to link_t structure to be initialized.
120 *
121 */
122static inline void link_initialize(link_t *link)
123{
124 link->prev = NULL;
125 link->next = NULL;
126}
127
128/** Initialize doubly-linked circular list
129 *
130 * Initialize doubly-linked circular list.
131 *
132 * @param list Pointer to list_t structure.
133 *
134 */
135static inline void list_initialize(list_t *list)
136{
137 list->head.prev = &list->head;
138 list->head.next = &list->head;
139}
140
141/** Insert item before another item in doubly-linked circular list.
142 *
143 */
144static inline void list_insert_before(link_t *lnew, link_t *lold)
145{
146 lnew->next = lold;
147 lnew->prev = lold->prev;
148 lold->prev->next = lnew;
149 lold->prev = lnew;
150}
151
152/** Insert item after another item in doubly-linked circular list.
153 *
154 */
155static inline void list_insert_after(link_t *lnew, link_t *lold)
156{
157 lnew->prev = lold;
158 lnew->next = lold->next;
159 lold->next->prev = lnew;
160 lold->next = lnew;
161}
162
163/** Add item to the beginning of doubly-linked circular list
164 *
165 * Add item to the beginning of doubly-linked circular list.
166 *
167 * @param link Pointer to link_t structure to be added.
168 * @param list Pointer to list_t structure.
169 *
170 */
171static inline void list_prepend(link_t *link, list_t *list)
172{
173 list_insert_after(link, &list->head);
174}
175
176/** Add item to the end of doubly-linked circular list
177 *
178 * Add item to the end of doubly-linked circular list.
179 *
180 * @param link Pointer to link_t structure to be added.
181 * @param list Pointer to list_t structure.
182 *
183 */
184static inline void list_append(link_t *link, list_t *list)
185{
186 list_insert_before(link, &list->head);
187}
188
189/** Remove item from doubly-linked circular list
190 *
191 * Remove item from doubly-linked circular list.
192 *
193 * @param link Pointer to link_t structure to be removed from the list
194 * it is contained in.
195 *
196 */
197static inline void list_remove(link_t *link)
198{
199 if ((link->prev != NULL) && (link->next != NULL)) {
200 link->next->prev = link->prev;
201 link->prev->next = link->next;
202 }
203
204 link_initialize(link);
205}
206
207/** Query emptiness of doubly-linked circular list
208 *
209 * Query emptiness of doubly-linked circular list.
210 *
211 * @param list Pointer to lins_t structure.
212 *
213 */
214static inline int list_empty(const list_t *list)
215{
216 return (list->head.next == &list->head);
217}
218
219/** Get first item in list.
220 *
221 * @param list Pointer to list_t structure.
222 *
223 * @return Head item of the list.
224 * @return NULL if the list is empty.
225 *
226 */
227static inline link_t *list_first(const list_t *list)
228{
229 return ((list->head.next == &list->head) ? NULL : list->head.next);
230}
231
232/** Get last item in list.
233 *
234 * @param list Pointer to list_t structure.
235 *
236 * @return Head item of the list.
237 * @return NULL if the list is empty.
238 *
239 */
240static inline link_t *list_last(list_t *list)
241{
242 return (list->head.prev == &list->head) ? NULL : list->head.prev;
243}
244
245/** Get next item in list.
246 *
247 * @param link Current item link
248 * @param list List containing @a link
249 *
250 * @return Next item or NULL if @a link is the last item.
251 */
252static inline link_t *list_next(link_t *link, const list_t *list)
253{
254 return (link->next == &list->head) ? NULL : link->next;
255}
256
257/** Get previous item in list.
258 *
259 * @param link Current item link
260 * @param list List containing @a link
261 *
262 * @return Previous item or NULL if @a link is the first item.
263 */
264static inline link_t *list_prev(link_t *link, const list_t *list)
265{
266 return (link->prev == &list->head) ? NULL : link->prev;
267}
268
269/** Split or concatenate headless doubly-linked circular list
270 *
271 * Split or concatenate headless doubly-linked circular list.
272 *
273 * Note that the algorithm works both directions:
274 * concatenates splitted lists and splits concatenated lists.
275 *
276 * @param part1 Pointer to link_t structure leading the first
277 * (half of the headless) list.
278 * @param part2 Pointer to link_t structure leading the second
279 * (half of the headless) list.
280 *
281 */
282static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
283{
284 part1->prev->next = part2;
285 part2->prev->next = part1;
286
287 link_t *hlp = part1->prev;
288
289 part1->prev = part2->prev;
290 part2->prev = hlp;
291}
292
293/** Split headless doubly-linked circular list
294 *
295 * Split headless doubly-linked circular list.
296 *
297 * @param part1 Pointer to link_t structure leading
298 * the first half of the headless list.
299 * @param part2 Pointer to link_t structure leading
300 * the second half of the headless list.
301 *
302 */
303static inline void headless_list_split(link_t *part1, link_t *part2)
304{
305 headless_list_split_or_concat(part1, part2);
306}
307
308/** Concatenate two headless doubly-linked circular lists
309 *
310 * Concatenate two headless doubly-linked circular lists.
311 *
312 * @param part1 Pointer to link_t structure leading
313 * the first headless list.
314 * @param part2 Pointer to link_t structure leading
315 * the second headless list.
316 *
317 */
318static inline void headless_list_concat(link_t *part1, link_t *part2)
319{
320 headless_list_split_or_concat(part1, part2);
321}
322
323/** Get n-th item in a list.
324 *
325 * @param list Pointer to link_t structure representing the list.
326 * @param n Item number (indexed from zero).
327 *
328 * @return n-th item of the list.
329 * @return NULL if no n-th item found.
330 *
331 */
332static inline link_t *list_nth(list_t *list, unsigned int n)
333{
334 unsigned int cnt = 0;
335
336 link_t *link = list_first(list);
337 while (link != NULL) {
338 if (cnt == n)
339 return link;
340
341 cnt++;
342 link = list_next(link, list);
343 }
344
345 return NULL;
346}
347
348/** Verify that argument type is a pointer to link_t (at compile time).
349 *
350 * This can be used to check argument type in a macro.
351 */
352static inline const void *list_link_to_void(const link_t *link)
353{
354 return link;
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.