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

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

Type check the member argument of list_get_instance macro.

  • Property mode set to 100644
File size: 7.7 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 <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)) - list_link_to_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#define assert_link_not_used(link) \
74 ASSERT(((link)->prev == NULL) && ((link)->next == NULL))
75
76/** Initialize doubly-linked circular list link
77 *
78 * Initialize doubly-linked list link.
79 *
80 * @param link Pointer to link_t structure to be initialized.
81 *
82 */
83NO_TRACE static inline void link_initialize(link_t *link)
84{
85 link->prev = NULL;
86 link->next = NULL;
87}
88
89/** Initialize doubly-linked circular list
90 *
91 * Initialize doubly-linked circular list.
92 *
93 * @param list Pointer to list_t structure.
94 *
95 */
96NO_TRACE static inline void list_initialize(list_t *list)
97{
98 list->head.prev = &list->head;
99 list->head.next = &list->head;
100}
101
102/** Insert item before another item in doubly-linked circular list.
103 *
104 */
105static inline void list_insert_before(link_t *lnew, link_t *lold)
106{
107 lnew->next = lold;
108 lnew->prev = lold->prev;
109 lold->prev->next = lnew;
110 lold->prev = lnew;
111}
112
113/** Insert item after another item in doubly-linked circular list.
114 *
115 */
116static inline void list_insert_after(link_t *lnew, link_t *lold)
117{
118 lnew->prev = lold;
119 lnew->next = lold->next;
120 lold->next->prev = lnew;
121 lold->next = lnew;
122}
123
124/** Add item to the beginning of doubly-linked circular list
125 *
126 * Add item to the beginning of doubly-linked circular list.
127 *
128 * @param link Pointer to link_t structure to be added.
129 * @param list Pointer to list_t structure.
130 *
131 */
132NO_TRACE static inline void list_prepend(link_t *link, list_t *list)
133{
134 list_insert_after(link, &list->head);
135}
136
137/** Add item to the end of doubly-linked circular list
138 *
139 * Add item to the end of doubly-linked circular list.
140 *
141 * @param link Pointer to link_t structure to be added.
142 * @param list Pointer to list_t structure.
143 *
144 */
145NO_TRACE static inline void list_append(link_t *link, list_t *list)
146{
147 list_insert_before(link, &list->head);
148}
149
150/** Remove item from doubly-linked circular list
151 *
152 * Remove item from doubly-linked circular list.
153 *
154 * @param link Pointer to link_t structure to be removed from the list
155 * it is contained in.
156 *
157 */
158NO_TRACE static inline void list_remove(link_t *link)
159{
160 if ((link->prev != NULL) && (link->next != NULL)) {
161 link->next->prev = link->prev;
162 link->prev->next = link->next;
163 }
164
165 link_initialize(link);
166}
167
168/** Query emptiness of doubly-linked circular list
169 *
170 * Query emptiness of doubly-linked circular list.
171 *
172 * @param list Pointer to lins_t structure.
173 *
174 */
175NO_TRACE static inline int list_empty(const list_t *list)
176{
177 return (list->head.next == &list->head);
178}
179
180/** Get first item in list.
181 *
182 * @param list Pointer to list_t structure.
183 *
184 * @return Head item of the list.
185 * @return NULL if the list is empty.
186 *
187 */
188static inline link_t *list_first(const list_t *list)
189{
190 return ((list->head.next == &list->head) ? NULL : list->head.next);
191}
192
193/** Get last item in list.
194 *
195 * @param list Pointer to list_t structure.
196 *
197 * @return Head item of the list.
198 * @return NULL if the list is empty.
199 *
200 */
201static inline link_t *list_last(list_t *list)
202{
203 return ((list->head.prev == &list->head) ? NULL : list->head.prev);
204}
205
206/** Split or concatenate headless doubly-linked circular list
207 *
208 * Split or concatenate headless doubly-linked circular list.
209 *
210 * Note that the algorithm works both directions:
211 * concatenates splitted lists and splits concatenated lists.
212 *
213 * @param part1 Pointer to link_t structure leading the first
214 * (half of the headless) list.
215 * @param part2 Pointer to link_t structure leading the second
216 * (half of the headless) list.
217 *
218 */
219NO_TRACE static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
220{
221 part1->prev->next = part2;
222 part2->prev->next = part1;
223
224 link_t *hlp = part1->prev;
225
226 part1->prev = part2->prev;
227 part2->prev = hlp;
228}
229
230/** Split headless doubly-linked circular list
231 *
232 * Split headless doubly-linked circular list.
233 *
234 * @param part1 Pointer to link_t structure leading
235 * the first half of the headless list.
236 * @param part2 Pointer to link_t structure leading
237 * the second half of the headless list.
238 *
239 */
240NO_TRACE static inline void headless_list_split(link_t *part1, link_t *part2)
241{
242 headless_list_split_or_concat(part1, part2);
243}
244
245/** Concatenate two headless doubly-linked circular lists
246 *
247 * Concatenate two headless doubly-linked circular lists.
248 *
249 * @param part1 Pointer to link_t structure leading
250 * the first headless list.
251 * @param part2 Pointer to link_t structure leading
252 * the second headless list.
253 *
254 */
255NO_TRACE static inline void headless_list_concat(link_t *part1, link_t *part2)
256{
257 headless_list_split_or_concat(part1, part2);
258}
259
260/** Get n-th item in a list.
261 *
262 * @param list Pointer to link_t structure representing the list.
263 * @param n Item number (indexed from zero).
264 *
265 * @return n-th item of the list.
266 * @return NULL if no n-th item found.
267 *
268 */
269static inline link_t *list_nth(list_t *list, unsigned int n)
270{
271 unsigned int cnt = 0;
272
273 list_foreach(*list, link) {
274 if (cnt == n)
275 return link;
276
277 cnt++;
278 }
279
280 return NULL;
281}
282
283/** Verify that argument type is a pointer to link_t (at compile time).
284 *
285 * This can be used to check argument type in a macro.
286 */
287static inline const void *list_link_to_void(const link_t *link)
288{
289 return link;
290}
291
292extern int list_member(const link_t *, const list_t *);
293extern void list_concat(list_t *, list_t *);
294extern unsigned int list_count(const list_t *);
295
296#endif
297
298/** @}
299 */
Note: See TracBrowser for help on using the repository browser.