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

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

ASSERT → assert

  • Property mode set to 100644
File size: 10.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 genericadt
31 * @{
32 */
33/** @file
34 */
35
36#ifndef KERN_LIST_H_
37#define KERN_LIST_H_
38
39#include <assert.h>
40#include <stdbool.h>
41#include <stddef.h>
42#include <trace.h>
43
44/** Doubly linked list link. */
45typedef struct link {
46 struct link *prev; /**< Pointer to the previous item in the list. */
47 struct link *next; /**< Pointer to the next item in the list. */
48} link_t;
49
50/** Doubly linked list. */
51typedef struct list {
52 link_t head; /**< List head. Does not have any data. */
53} list_t;
54
55
56extern bool list_member(const link_t *, const list_t *);
57extern void list_splice(list_t *, link_t *);
58extern unsigned long list_count(const list_t *);
59
60
61/** Declare and initialize statically allocated list.
62 *
63 * @param name Name of the new statically allocated list.
64 *
65 */
66#define LIST_INITIALIZE(name) \
67 list_t name = { \
68 .head = { \
69 .prev = &(name).head, \
70 .next = &(name).head \
71 } \
72 }
73
74#define list_get_instance(link, type, member) \
75 ((type *) (((void *)(link)) - list_link_to_void(&(((type *) NULL)->member))))
76
77#define list_foreach(list, member, itype, iterator) \
78 for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \
79 for (link_t *_link = (list).head.next; \
80 iterator = list_get_instance(_link, itype, member), \
81 _link != &(list).head; _link = _link->next)
82
83#define list_foreach_rev(list, member, itype, iterator) \
84 for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \
85 for (link_t *_link = (list).head.prev; \
86 iterator = list_get_instance(_link, itype, member), \
87 _link != &(list).head; _link = _link->prev)
88
89/** Unlike list_foreach(), allows removing items while traversing a list.
90 *
91 * @code
92 * list_t mylist;
93 * typedef struct item {
94 * int value;
95 * link_t item_link;
96 * } item_t;
97 *
98 * //..
99 *
100 * // Print each list element's value and remove the element from the list.
101 * list_foreach_safe(mylist, cur_link, next_link) {
102 * item_t *cur_item = list_get_instance(cur_link, item_t, item_link);
103 * printf("%d\n", cur_item->value);
104 * list_remove(cur_link);
105 * }
106 * @endcode
107 *
108 * @param list List to traverse.
109 * @param iterator Iterator to the current element of the list.
110 * The item this iterator points may be safely removed
111 * from the list.
112 * @param next_iter Iterator to the next element of the list.
113 */
114#define list_foreach_safe(list, iterator, next_iter) \
115 for (link_t *iterator = (list).head.next, \
116 *next_iter = iterator->next; \
117 iterator != &(list).head; \
118 iterator = next_iter, next_iter = iterator->next)
119
120
121#define assert_link_not_used(link) \
122 assert(!link_used(link))
123
124/** Initialize doubly-linked circular list link
125 *
126 * Initialize doubly-linked list link.
127 *
128 * @param link Pointer to link_t structure to be initialized.
129 *
130 */
131NO_TRACE static inline void link_initialize(link_t *link)
132{
133 link->prev = NULL;
134 link->next = NULL;
135}
136
137/** Initialize doubly-linked circular list
138 *
139 * Initialize doubly-linked circular list.
140 *
141 * @param list Pointer to list_t structure.
142 *
143 */
144NO_TRACE static inline void list_initialize(list_t *list)
145{
146 list->head.prev = &list->head;
147 list->head.next = &list->head;
148}
149
150/** Insert item before another item in doubly-linked circular list.
151 *
152 */
153static inline void list_insert_before(link_t *lnew, link_t *lold)
154{
155 lnew->next = lold;
156 lnew->prev = lold->prev;
157 lold->prev->next = lnew;
158 lold->prev = lnew;
159}
160
161/** Insert item after another item in doubly-linked circular list.
162 *
163 */
164static inline void list_insert_after(link_t *lnew, link_t *lold)
165{
166 lnew->prev = lold;
167 lnew->next = lold->next;
168 lold->next->prev = lnew;
169 lold->next = lnew;
170}
171
172/** Add item to the beginning of doubly-linked circular list
173 *
174 * Add item to the beginning of doubly-linked circular list.
175 *
176 * @param link Pointer to link_t structure to be added.
177 * @param list Pointer to list_t structure.
178 *
179 */
180NO_TRACE static inline void list_prepend(link_t *link, list_t *list)
181{
182 list_insert_after(link, &list->head);
183}
184
185/** Add item to the end of doubly-linked circular list
186 *
187 * Add item to the end of doubly-linked circular list.
188 *
189 * @param link Pointer to link_t structure to be added.
190 * @param list Pointer to list_t structure.
191 *
192 */
193NO_TRACE static inline void list_append(link_t *link, list_t *list)
194{
195 list_insert_before(link, &list->head);
196}
197
198/** Remove item from doubly-linked circular list
199 *
200 * Remove item from doubly-linked circular list.
201 *
202 * @param link Pointer to link_t structure to be removed from the list
203 * it is contained in.
204 *
205 */
206NO_TRACE static inline void list_remove(link_t *link)
207{
208 if ((link->prev != NULL) && (link->next != NULL)) {
209 link->next->prev = link->prev;
210 link->prev->next = link->next;
211 }
212
213 link_initialize(link);
214}
215
216/** Query emptiness of doubly-linked circular list
217 *
218 * Query emptiness of doubly-linked circular list.
219 *
220 * @param list Pointer to lins_t structure.
221 *
222 */
223NO_TRACE static inline bool list_empty(const list_t *list)
224{
225 return (list->head.next == &list->head);
226}
227
228/** Get first item in list.
229 *
230 * @param list Pointer to list_t structure.
231 *
232 * @return Head item of the list.
233 * @return NULL if the list is empty.
234 *
235 */
236static inline link_t *list_first(const list_t *list)
237{
238 return ((list->head.next == &list->head) ? NULL : list->head.next);
239}
240
241/** Get last item in list.
242 *
243 * @param list Pointer to list_t structure.
244 *
245 * @return Head item of the list.
246 * @return NULL if the list is empty.
247 *
248 */
249static inline link_t *list_last(list_t *list)
250{
251 return ((list->head.prev == &list->head) ? NULL : list->head.prev);
252}
253
254/** Get next item in list.
255 *
256 * @param link Current item link
257 * @param list List containing @a link
258 *
259 * @return Next item or NULL if @a link is the last item.
260 */
261static inline link_t *list_next(link_t *link, const list_t *list)
262{
263 return (link->next == &list->head) ? NULL : link->next;
264}
265
266/** Get previous item in list.
267 *
268 * @param link Current item link
269 * @param list List containing @a link
270 *
271 * @return Previous item or NULL if @a link is the first item.
272 */
273static inline link_t *list_prev(link_t *link, const list_t *list)
274{
275 return (link->prev == &list->head) ? NULL : link->prev;
276}
277
278/** Split or concatenate headless doubly-linked circular list
279 *
280 * Split or concatenate headless doubly-linked circular list.
281 *
282 * Note that the algorithm works both directions:
283 * concatenates splitted lists and splits concatenated lists.
284 *
285 * @param part1 Pointer to link_t structure leading the first
286 * (half of the headless) list.
287 * @param part2 Pointer to link_t structure leading the second
288 * (half of the headless) list.
289 *
290 */
291NO_TRACE static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
292{
293 part1->prev->next = part2;
294 part2->prev->next = part1;
295
296 link_t *hlp = part1->prev;
297
298 part1->prev = part2->prev;
299 part2->prev = hlp;
300}
301
302/** Split headless doubly-linked circular list
303 *
304 * Split headless doubly-linked circular list.
305 *
306 * @param part1 Pointer to link_t structure leading
307 * the first half of the headless list.
308 * @param part2 Pointer to link_t structure leading
309 * the second half of the headless list.
310 *
311 */
312NO_TRACE static inline void headless_list_split(link_t *part1, link_t *part2)
313{
314 headless_list_split_or_concat(part1, part2);
315}
316
317/** Concatenate two headless doubly-linked circular lists
318 *
319 * Concatenate two headless doubly-linked circular lists.
320 *
321 * @param part1 Pointer to link_t structure leading
322 * the first headless list.
323 * @param part2 Pointer to link_t structure leading
324 * the second headless list.
325 *
326 */
327NO_TRACE static inline void headless_list_concat(link_t *part1, link_t *part2)
328{
329 headless_list_split_or_concat(part1, part2);
330}
331
332/** Concatenate two lists
333 *
334 * Concatenate lists @a list1 and @a list2, producing a single
335 * list @a list1 containing items from both (in @a list1, @a list2
336 * order) and empty list @a list2.
337 *
338 * @param list1 First list and concatenated output
339 * @param list2 Second list and empty output.
340 *
341 */
342NO_TRACE static inline void list_concat(list_t *list1, list_t *list2)
343{
344 list_splice(list2, list1->head.prev);
345}
346
347/** Get n-th item in a list.
348 *
349 * @param list Pointer to link_t structure representing the list.
350 * @param n Item number (indexed from zero).
351 *
352 * @return n-th item of the list.
353 * @return NULL if no n-th item found.
354 *
355 */
356static inline link_t *list_nth(list_t *list, unsigned long n)
357{
358 unsigned long cnt = 0;
359 link_t *link;
360
361 link = list_first(list);
362 while (link != NULL) {
363 if (cnt == n)
364 return link;
365
366 cnt++;
367 link = list_next(link, list);
368 }
369
370 return NULL;
371}
372
373/** Verify that argument type is a pointer to link_t (at compile time).
374 *
375 * This can be used to check argument type in a macro.
376 */
377static inline const void *list_link_to_void(const link_t *link)
378{
379 return link;
380}
381
382/** Determine if link is used.
383 *
384 * @param link Link
385 * @return @c true if link is used, @c false if not.
386 */
387static inline bool link_used(link_t *link)
388{
389 if (link->prev == NULL && link->next == NULL)
390 return false;
391
392 assert(link->prev != NULL && link->next != NULL);
393 return true;
394}
395
396#endif
397
398/** @}
399 */
Note: See TracBrowser for help on using the repository browser.