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 <debug.h>
|
---|
40 | #include <typedefs.h>
|
---|
41 | #include <trace.h>
|
---|
42 |
|
---|
43 | /** Doubly linked list link. */
|
---|
44 | typedef struct link {
|
---|
45 | struct link *prev; /**< Pointer to the previous item in the list. */
|
---|
46 | struct link *next; /**< Pointer to the next item in the list. */
|
---|
47 | } link_t;
|
---|
48 |
|
---|
49 | /** Doubly linked list. */
|
---|
50 | typedef struct list {
|
---|
51 | link_t head; /**< List head. Does not have any data. */
|
---|
52 | } list_t;
|
---|
53 |
|
---|
54 | /** Declare and initialize statically allocated list.
|
---|
55 | *
|
---|
56 | * @param name Name of the new statically allocated list.
|
---|
57 | *
|
---|
58 | */
|
---|
59 | #define LIST_INITIALIZE(name) \
|
---|
60 | list_t name = { \
|
---|
61 | .head = { \
|
---|
62 | .prev = &(name).head, \
|
---|
63 | .next = &(name).head \
|
---|
64 | } \
|
---|
65 | }
|
---|
66 |
|
---|
67 | #define list_get_instance(link, type, member) \
|
---|
68 | ((type *) (((void *)(link)) - list_link_to_void(&(((type *) NULL)->member))))
|
---|
69 |
|
---|
70 | #define list_foreach(list, member, itype, iterator) \
|
---|
71 | for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \
|
---|
72 | for (link_t *_link = (list).head.next; \
|
---|
73 | iterator = list_get_instance(_link, itype, member), \
|
---|
74 | _link != &(list).head; _link = _link->next)
|
---|
75 |
|
---|
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 |
|
---|
82 | #define assert_link_not_used(link) \
|
---|
83 | ASSERT(!link_used(link))
|
---|
84 |
|
---|
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.
|
---|
90 | *
|
---|
91 | */
|
---|
92 | NO_TRACE static inline void link_initialize(link_t *link)
|
---|
93 | {
|
---|
94 | link->prev = NULL;
|
---|
95 | link->next = NULL;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** Initialize doubly-linked circular list
|
---|
99 | *
|
---|
100 | * Initialize doubly-linked circular list.
|
---|
101 | *
|
---|
102 | * @param list Pointer to list_t structure.
|
---|
103 | *
|
---|
104 | */
|
---|
105 | NO_TRACE static inline void list_initialize(list_t *list)
|
---|
106 | {
|
---|
107 | list->head.prev = &list->head;
|
---|
108 | list->head.next = &list->head;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /** Insert item before another item in doubly-linked circular list.
|
---|
112 | *
|
---|
113 | */
|
---|
114 | static inline void list_insert_before(link_t *lnew, link_t *lold)
|
---|
115 | {
|
---|
116 | lnew->next = lold;
|
---|
117 | lnew->prev = lold->prev;
|
---|
118 | lold->prev->next = lnew;
|
---|
119 | lold->prev = lnew;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /** Insert item after another item in doubly-linked circular list.
|
---|
123 | *
|
---|
124 | */
|
---|
125 | static inline void list_insert_after(link_t *lnew, link_t *lold)
|
---|
126 | {
|
---|
127 | lnew->prev = lold;
|
---|
128 | lnew->next = lold->next;
|
---|
129 | lold->next->prev = lnew;
|
---|
130 | lold->next = lnew;
|
---|
131 | }
|
---|
132 |
|
---|
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.
|
---|
139 | *
|
---|
140 | */
|
---|
141 | NO_TRACE static inline void list_prepend(link_t *link, list_t *list)
|
---|
142 | {
|
---|
143 | list_insert_after(link, &list->head);
|
---|
144 | }
|
---|
145 |
|
---|
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.
|
---|
152 | *
|
---|
153 | */
|
---|
154 | NO_TRACE static inline void list_append(link_t *link, list_t *list)
|
---|
155 | {
|
---|
156 | list_insert_before(link, &list->head);
|
---|
157 | }
|
---|
158 |
|
---|
159 | /** Remove item from doubly-linked circular list
|
---|
160 | *
|
---|
161 | * Remove item from doubly-linked circular list.
|
---|
162 | *
|
---|
163 | * @param link Pointer to link_t structure to be removed from the list
|
---|
164 | * it is contained in.
|
---|
165 | *
|
---|
166 | */
|
---|
167 | NO_TRACE static inline void list_remove(link_t *link)
|
---|
168 | {
|
---|
169 | if ((link->prev != NULL) && (link->next != NULL)) {
|
---|
170 | link->next->prev = link->prev;
|
---|
171 | link->prev->next = link->next;
|
---|
172 | }
|
---|
173 |
|
---|
174 | link_initialize(link);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /** Query emptiness of doubly-linked circular list
|
---|
178 | *
|
---|
179 | * Query emptiness of doubly-linked circular list.
|
---|
180 | *
|
---|
181 | * @param list Pointer to lins_t structure.
|
---|
182 | *
|
---|
183 | */
|
---|
184 | NO_TRACE static inline int list_empty(const list_t *list)
|
---|
185 | {
|
---|
186 | return (list->head.next == &list->head);
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** Get first item in list.
|
---|
190 | *
|
---|
191 | * @param list Pointer to list_t structure.
|
---|
192 | *
|
---|
193 | * @return Head item of the list.
|
---|
194 | * @return NULL if the list is empty.
|
---|
195 | *
|
---|
196 | */
|
---|
197 | static inline link_t *list_first(const list_t *list)
|
---|
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.
|
---|
205 | *
|
---|
206 | * @return Head item of the list.
|
---|
207 | * @return NULL if the list is empty.
|
---|
208 | *
|
---|
209 | */
|
---|
210 | static inline link_t *list_last(list_t *list)
|
---|
211 | {
|
---|
212 | return ((list->head.prev == &list->head) ? NULL : list->head.prev);
|
---|
213 | }
|
---|
214 |
|
---|
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 | */
|
---|
222 | static 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 | */
|
---|
234 | static inline link_t *list_prev(link_t *link, const list_t *list)
|
---|
235 | {
|
---|
236 | return (link->prev == &list->head) ? NULL : link->prev;
|
---|
237 | }
|
---|
238 |
|
---|
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 | *
|
---|
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 | *
|
---|
251 | */
|
---|
252 | NO_TRACE static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
|
---|
253 | {
|
---|
254 | part1->prev->next = part2;
|
---|
255 | part2->prev->next = part1;
|
---|
256 |
|
---|
257 | link_t *hlp = part1->prev;
|
---|
258 |
|
---|
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 | *
|
---|
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 | *
|
---|
272 | */
|
---|
273 | NO_TRACE static inline void headless_list_split(link_t *part1, link_t *part2)
|
---|
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 | *
|
---|
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 | *
|
---|
287 | */
|
---|
288 | NO_TRACE static inline void headless_list_concat(link_t *part1, link_t *part2)
|
---|
289 | {
|
---|
290 | headless_list_split_or_concat(part1, part2);
|
---|
291 | }
|
---|
292 |
|
---|
293 | /** Get n-th item in a list.
|
---|
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 | */
|
---|
302 | static inline link_t *list_nth(list_t *list, unsigned int n)
|
---|
303 | {
|
---|
304 | unsigned int cnt = 0;
|
---|
305 | link_t *link;
|
---|
306 |
|
---|
307 | link = list_first(list);
|
---|
308 | while (link != NULL) {
|
---|
309 | if (cnt == n)
|
---|
310 | return link;
|
---|
311 |
|
---|
312 | cnt++;
|
---|
313 | link = list_next(link, list);
|
---|
314 | }
|
---|
315 |
|
---|
316 | return NULL;
|
---|
317 | }
|
---|
318 |
|
---|
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 | */
|
---|
323 | static inline const void *list_link_to_void(const link_t *link)
|
---|
324 | {
|
---|
325 | return link;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /** Determine if link is used.
|
---|
329 | *
|
---|
330 | * @param link Link
|
---|
331 | * @return @c true if link is used, @c false if not.
|
---|
332 | */
|
---|
333 | static 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 |
|
---|
342 | extern int list_member(const link_t *, const list_t *);
|
---|
343 | extern void list_concat(list_t *, list_t *);
|
---|
344 | extern unsigned int list_count(const list_t *);
|
---|
345 |
|
---|
346 | #endif
|
---|
347 |
|
---|
348 | /** @}
|
---|
349 | */
|
---|