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

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

Reverse list iteration - list_foreach_rev() and link_used().

  • Property mode set to 100644
File size: 9.1 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[a74d0ad]3 * Copyright (c) 2013 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
[7856d09]39#include <debug.h>
[7ac426e]40#include <typedefs.h>
[7a0359b]41#include <trace.h>
[f761f1eb]42
[55b77d9]43/** Doubly linked list link. */
[b3f8fb7]44typedef struct link {
[0a02653]45 struct link *prev; /**< Pointer to the previous item in the list. */
46 struct link *next; /**< Pointer to the next item in the list. */
[b3f8fb7]47} link_t;
[f761f1eb]48
[55b77d9]49/** Doubly linked list. */
50typedef struct list {
51 link_t head; /**< List head. Does not have any data. */
52} list_t;
53
[7dd2561]54/** Declare and initialize statically allocated list.
55 *
56 * @param name Name of the new statically allocated list.
[0a02653]57 *
[7dd2561]58 */
[80bcaed]59#define LIST_INITIALIZE(name) \
[55b77d9]60 list_t name = { \
61 .head = { \
62 .prev = &(name).head, \
63 .next = &(name).head \
64 } \
[0a02653]65 }
66
67#define list_get_instance(link, type, member) \
[a74d0ad]68 ((type *) (((void *)(link)) - list_link_to_void(&(((type *) NULL)->member))))
[0a02653]69
[feeac0d]70#define list_foreach(list, member, itype, iterator) \
[07525cd]71 for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \
[feeac0d]72 for (link_t *_link = (list).head.next; \
73 iterator = list_get_instance(_link, itype, member), \
74 _link != &(list).head; _link = _link->next)
[7dd2561]75
[7856d09]76#define list_foreach_rev(list, member, itype, iterator) \
77 for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \
78 for (link_t *_link = (list).head.prev; \
79 iterator = list_get_instance(_link, itype, member), \
80 _link != &(list).head; _link = _link->prev)
81
[b72efe8]82#define assert_link_not_used(link) \
[7856d09]83 ASSERT(!link_used(link))
[b72efe8]84
[40a468a]85/** Initialize doubly-linked circular list link
86 *
87 * Initialize doubly-linked list link.
88 *
89 * @param link Pointer to link_t structure to be initialized.
[0a02653]90 *
[40a468a]91 */
[7a0359b]92NO_TRACE static inline void link_initialize(link_t *link)
[c0a91d1]93{
94 link->prev = NULL;
95 link->next = NULL;
[f761f1eb]96}
97
[40a468a]98/** Initialize doubly-linked circular list
99 *
100 * Initialize doubly-linked circular list.
101 *
[55b77d9]102 * @param list Pointer to list_t structure.
[0a02653]103 *
[40a468a]104 */
[55b77d9]105NO_TRACE static inline void list_initialize(list_t *list)
[c0a91d1]106{
[55b77d9]107 list->head.prev = &list->head;
108 list->head.next = &list->head;
[f761f1eb]109}
110
[55b77d9]111/** Insert item before another item in doubly-linked circular list.
[0a02653]112 *
[40a468a]113 */
[55b77d9]114static inline void list_insert_before(link_t *lnew, link_t *lold)
[c0a91d1]115{
[55b77d9]116 lnew->next = lold;
117 lnew->prev = lold->prev;
118 lold->prev->next = lnew;
119 lold->prev = lnew;
[f761f1eb]120}
121
[55b77d9]122/** Insert item after another item in doubly-linked circular list.
[0a02653]123 *
[40a468a]124 */
[55b77d9]125static inline void list_insert_after(link_t *lnew, link_t *lold)
[c0a91d1]126{
[55b77d9]127 lnew->prev = lold;
128 lnew->next = lold->next;
129 lold->next->prev = lnew;
130 lold->next = lnew;
[0a02653]131}
132
[55b77d9]133/** Add item to the beginning of doubly-linked circular list
134 *
135 * Add item to the beginning of doubly-linked circular list.
136 *
137 * @param link Pointer to link_t structure to be added.
138 * @param list Pointer to list_t structure.
[0a02653]139 *
140 */
[55b77d9]141NO_TRACE static inline void list_prepend(link_t *link, list_t *list)
[0a02653]142{
[55b77d9]143 list_insert_after(link, &list->head);
[0a02653]144}
145
[55b77d9]146/** Add item to the end of doubly-linked circular list
147 *
148 * Add item to the end of doubly-linked circular list.
149 *
150 * @param link Pointer to link_t structure to be added.
151 * @param list Pointer to list_t structure.
[0a02653]152 *
153 */
[55b77d9]154NO_TRACE static inline void list_append(link_t *link, list_t *list)
[0a02653]155{
[55b77d9]156 list_insert_before(link, &list->head);
[f761f1eb]157}
158
[40a468a]159/** Remove item from doubly-linked circular list
160 *
161 * Remove item from doubly-linked circular list.
162 *
[0a02653]163 * @param link Pointer to link_t structure to be removed from the list
164 * it is contained in.
165 *
[40a468a]166 */
[7a0359b]167NO_TRACE static inline void list_remove(link_t *link)
[c0a91d1]168{
[14a60e3]169 if ((link->prev != NULL) && (link->next != NULL)) {
170 link->next->prev = link->prev;
171 link->prev->next = link->next;
172 }
173
[c0a91d1]174 link_initialize(link);
[f761f1eb]175}
176
[40a468a]177/** Query emptiness of doubly-linked circular list
178 *
179 * Query emptiness of doubly-linked circular list.
180 *
[55b77d9]181 * @param list Pointer to lins_t structure.
[0a02653]182 *
[40a468a]183 */
[4748038]184NO_TRACE static inline int list_empty(const list_t *list)
[40a468a]185{
[55b77d9]186 return (list->head.next == &list->head);
[40a468a]187}
188
[55b77d9]189/** Get first item in list.
[0a02653]190 *
[55b77d9]191 * @param list Pointer to list_t structure.
192 *
193 * @return Head item of the list.
194 * @return NULL if the list is empty.
[85147f3]195 *
[55b77d9]196 */
[4748038]197static inline link_t *list_first(const list_t *list)
[55b77d9]198{
199 return ((list->head.next == &list->head) ? NULL : list->head.next);
200}
201
202/** Get last item in list.
203 *
204 * @param list Pointer to list_t structure.
[0a02653]205 *
206 * @return Head item of the list.
207 * @return NULL if the list is empty.
[85147f3]208 *
[0a02653]209 */
[55b77d9]210static inline link_t *list_last(list_t *list)
[0a02653]211{
[55b77d9]212 return ((list->head.prev == &list->head) ? NULL : list->head.prev);
[0a02653]213}
[40a468a]214
[feeac0d]215/** Get next item in list.
216 *
217 * @param link Current item link
218 * @param list List containing @a link
219 *
220 * @return Next item or NULL if @a link is the last item.
221 */
222static inline link_t *list_next(link_t *link, const list_t *list)
223{
224 return (link->next == &list->head) ? NULL : link->next;
225}
226
227/** Get previous item in list.
228 *
229 * @param link Current item link
230 * @param list List containing @a link
231 *
232 * @return Previous item or NULL if @a link is the first item.
233 */
234static inline link_t *list_prev(link_t *link, const list_t *list)
235{
236 return (link->prev == &list->head) ? NULL : link->prev;
237}
238
[40a468a]239/** Split or concatenate headless doubly-linked circular list
240 *
241 * Split or concatenate headless doubly-linked circular list.
242 *
243 * Note that the algorithm works both directions:
244 * concatenates splitted lists and splits concatenated lists.
245 *
[0a02653]246 * @param part1 Pointer to link_t structure leading the first
247 * (half of the headless) list.
248 * @param part2 Pointer to link_t structure leading the second
249 * (half of the headless) list.
250 *
[40a468a]251 */
[7a0359b]252NO_TRACE static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
[40a468a]253{
254 part1->prev->next = part2;
[0a02653]255 part2->prev->next = part1;
256
257 link_t *hlp = part1->prev;
258
[40a468a]259 part1->prev = part2->prev;
260 part2->prev = hlp;
261}
262
263/** Split headless doubly-linked circular list
264 *
265 * Split headless doubly-linked circular list.
266 *
[0a02653]267 * @param part1 Pointer to link_t structure leading
268 * the first half of the headless list.
269 * @param part2 Pointer to link_t structure leading
270 * the second half of the headless list.
271 *
[40a468a]272 */
[7a0359b]273NO_TRACE static inline void headless_list_split(link_t *part1, link_t *part2)
[40a468a]274{
275 headless_list_split_or_concat(part1, part2);
276}
277
278/** Concatenate two headless doubly-linked circular lists
279 *
280 * Concatenate two headless doubly-linked circular lists.
281 *
[0a02653]282 * @param part1 Pointer to link_t structure leading
283 * the first headless list.
284 * @param part2 Pointer to link_t structure leading
285 * the second headless list.
286 *
[40a468a]287 */
[7a0359b]288NO_TRACE static inline void headless_list_concat(link_t *part1, link_t *part2)
[40a468a]289{
290 headless_list_split_or_concat(part1, part2);
291}
[f761f1eb]292
[55b77d9]293/** Get n-th item in a list.
[0a02653]294 *
295 * @param list Pointer to link_t structure representing the list.
296 * @param n Item number (indexed from zero).
297 *
298 * @return n-th item of the list.
299 * @return NULL if no n-th item found.
300 *
301 */
[55b77d9]302static inline link_t *list_nth(list_t *list, unsigned int n)
[0a02653]303{
304 unsigned int cnt = 0;
[feeac0d]305 link_t *link;
[0a02653]306
[feeac0d]307 link = list_first(list);
308 while (link != NULL) {
[0a02653]309 if (cnt == n)
310 return link;
311
312 cnt++;
[feeac0d]313 link = list_next(link, list);
[0a02653]314 }
315
316 return NULL;
317}
[f761f1eb]318
[a74d0ad]319/** Verify that argument type is a pointer to link_t (at compile time).
320 *
321 * This can be used to check argument type in a macro.
322 */
323static inline const void *list_link_to_void(const link_t *link)
324{
325 return link;
326}
327
[7856d09]328/** Determine if link is used.
329 *
330 * @param link Link
331 * @return @c true if link is used, @c false if not.
332 */
333static inline bool link_used(link_t *link)
334{
335 if (link->prev == NULL && link->next == NULL)
336 return false;
337
338 ASSERT(link->prev != NULL && link->next != NULL);
339 return true;
340}
341
[55b77d9]342extern int list_member(const link_t *, const list_t *);
343extern void list_concat(list_t *, list_t *);
344extern unsigned int list_count(const list_t *);
[f761f1eb]345
346#endif
[b45c443]347
[06e1e95]348/** @}
[b45c443]349 */
Note: See TracBrowser for help on using the repository browser.