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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d4475a44 was d4475a44, checked in by Martin Decky <martin@…>, 7 years ago

use uint8_t * instead of char *

Technically, there should be no difference on all current platforms. But
the intention is more clear and it is failproof.

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