source: mainline/uspace/lib/c/include/adt/list.h@ 00b7fc8

Last change on this file since 00b7fc8 was 00b7fc8, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

wip

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