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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 207e8880 was b94bc6c, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

Added LIST_INITIALIZER (to uspace) which initializes statically allocated list_t variables but does not declare them.

  • Property mode set to 100644
File size: 8.9 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * Copyright (c) 2011 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 <unistd.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 = LIST_INITIALIZER(name)
60
61/** Initializer for statically allocated list.
62 *
63 * @code
64 * struct named_list {
65 * const char *name;
66 * list_t list;
67 * } var = {
68 * .name = "default name",
69 * .list = LIST_INITIALIZER(name_list.list)
70 * };
71 * @endcode
72 *
73 * @param name Name of the new statically allocated list.
74 *
75 */
76#define LIST_INITIALIZER(name) \
77 { \
78 .head = { \
79 .prev = &(name).head, \
80 .next = &(name).head \
81 } \
82 }
83
84#define list_get_instance(link, type, member) \
85 ((type *) (((void *)(link)) - ((void *) &(((type *) NULL)->member))))
86
87#define list_foreach(list, iterator) \
88 for (link_t *iterator = (list).head.next; \
89 iterator != &(list).head; iterator = iterator->next)
90
91/** Unlike list_foreach(), allows removing items while traversing a list.
92 *
93 * @code
94 * list_t mylist;
95 * typedef struct item {
96 * int value;
97 * link_t item_link;
98 * } item_t;
99 *
100 * //..
101 *
102 * // Print each list element's value and remove the element from the list.
103 * list_foreach_safe(mylist, cur_link, next_link) {
104 * item_t *cur_item = list_get_instance(cur_link, item_t, item_link);
105 * printf("%d\n", cur_item->value);
106 * list_remove(cur_link);
107 * }
108 * @endcode
109 *
110 * @param list List to traverse.
111 * @param iterator Iterator to the current element of the list.
112 * The item this iterator points may be safely removed
113 * from the list.
114 * @param next_iter Iterator to the next element of the list.
115 */
116#define list_foreach_safe(list, iterator, next_iter) \
117 for (link_t *iterator = (list).head.next, \
118 *next_iter = iterator->next; \
119 iterator != &(list).head; \
120 iterator = next_iter, next_iter = iterator->next)
121
122#define assert_link_not_used(link) \
123 assert(((link)->prev == NULL) && ((link)->next == NULL))
124
125/** Returns true if the link is definitely part of a list. False if not sure. */
126static inline int link_in_use(link_t *link)
127{
128 return link->prev != NULL && link->next != NULL;
129}
130
131/** Initialize doubly-linked circular list link
132 *
133 * Initialize doubly-linked list link.
134 *
135 * @param link Pointer to link_t structure to be initialized.
136 *
137 */
138static inline void link_initialize(link_t *link)
139{
140 link->prev = NULL;
141 link->next = NULL;
142}
143
144/** Initialize doubly-linked circular list
145 *
146 * Initialize doubly-linked circular list.
147 *
148 * @param list Pointer to list_t structure.
149 *
150 */
151static inline void list_initialize(list_t *list)
152{
153 list->head.prev = &list->head;
154 list->head.next = &list->head;
155}
156
157/** Insert item before another item in doubly-linked circular list.
158 *
159 */
160static inline void list_insert_before(link_t *lnew, link_t *lold)
161{
162 lnew->next = lold;
163 lnew->prev = lold->prev;
164 lold->prev->next = lnew;
165 lold->prev = lnew;
166}
167
168/** Insert item after another item in doubly-linked circular list.
169 *
170 */
171static inline void list_insert_after(link_t *lnew, link_t *lold)
172{
173 lnew->prev = lold;
174 lnew->next = lold->next;
175 lold->next->prev = lnew;
176 lold->next = lnew;
177}
178
179/** Add item to the beginning of doubly-linked circular list
180 *
181 * Add item to the beginning of doubly-linked circular list.
182 *
183 * @param link Pointer to link_t structure to be added.
184 * @param list Pointer to list_t structure.
185 *
186 */
187static inline void list_prepend(link_t *link, list_t *list)
188{
189 list_insert_after(link, &list->head);
190}
191
192/** Add item to the end of doubly-linked circular list
193 *
194 * Add item to the end of doubly-linked circular list.
195 *
196 * @param link Pointer to link_t structure to be added.
197 * @param list Pointer to list_t structure.
198 *
199 */
200static inline void list_append(link_t *link, list_t *list)
201{
202 list_insert_before(link, &list->head);
203}
204
205/** Remove item from doubly-linked circular list
206 *
207 * Remove item from doubly-linked circular list.
208 *
209 * @param link Pointer to link_t structure to be removed from the list
210 * it is contained in.
211 *
212 */
213static inline void list_remove(link_t *link)
214{
215 if ((link->prev != NULL) && (link->next != NULL)) {
216 link->next->prev = link->prev;
217 link->prev->next = link->next;
218 }
219
220 link_initialize(link);
221}
222
223/** Query emptiness of doubly-linked circular list
224 *
225 * Query emptiness of doubly-linked circular list.
226 *
227 * @param list Pointer to lins_t structure.
228 *
229 */
230static inline int list_empty(const list_t *list)
231{
232 return (list->head.next == &list->head);
233}
234
235/** Get first item in list.
236 *
237 * @param list Pointer to list_t structure.
238 *
239 * @return Head item of the list.
240 * @return NULL if the list is empty.
241 *
242 */
243static inline link_t *list_first(const list_t *list)
244{
245 return ((list->head.next == &list->head) ? NULL : list->head.next);
246}
247
248/** Get last item in list.
249 *
250 * @param list Pointer to list_t structure.
251 *
252 * @return Head item of the list.
253 * @return NULL if the list is empty.
254 *
255 */
256static inline link_t *list_last(list_t *list)
257{
258 return ((list->head.prev == &list->head) ? NULL : list->head.prev);
259}
260
261/** Split or concatenate headless doubly-linked circular list
262 *
263 * Split or concatenate headless doubly-linked circular list.
264 *
265 * Note that the algorithm works both directions:
266 * concatenates splitted lists and splits concatenated lists.
267 *
268 * @param part1 Pointer to link_t structure leading the first
269 * (half of the headless) list.
270 * @param part2 Pointer to link_t structure leading the second
271 * (half of the headless) list.
272 *
273 */
274static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
275{
276 part1->prev->next = part2;
277 part2->prev->next = part1;
278
279 link_t *hlp = part1->prev;
280
281 part1->prev = part2->prev;
282 part2->prev = hlp;
283}
284
285/** Split headless doubly-linked circular list
286 *
287 * Split headless doubly-linked circular list.
288 *
289 * @param part1 Pointer to link_t structure leading
290 * the first half of the headless list.
291 * @param part2 Pointer to link_t structure leading
292 * the second half of the headless list.
293 *
294 */
295static inline void headless_list_split(link_t *part1, link_t *part2)
296{
297 headless_list_split_or_concat(part1, part2);
298}
299
300/** Concatenate two headless doubly-linked circular lists
301 *
302 * Concatenate two headless doubly-linked circular lists.
303 *
304 * @param part1 Pointer to link_t structure leading
305 * the first headless list.
306 * @param part2 Pointer to link_t structure leading
307 * the second headless list.
308 *
309 */
310static inline void headless_list_concat(link_t *part1, link_t *part2)
311{
312 headless_list_split_or_concat(part1, part2);
313}
314
315/** Get n-th item in a list.
316 *
317 * @param list Pointer to link_t structure representing the list.
318 * @param n Item number (indexed from zero).
319 *
320 * @return n-th item of the list.
321 * @return NULL if no n-th item found.
322 *
323 */
324static inline link_t *list_nth(list_t *list, unsigned int n)
325{
326 unsigned int cnt = 0;
327
328 list_foreach(*list, link) {
329 if (cnt == n)
330 return link;
331
332 cnt++;
333 }
334
335 return NULL;
336}
337
338extern int list_member(const link_t *, const list_t *);
339extern void list_concat(list_t *, list_t *);
340extern unsigned int list_count(const list_t *);
341
342#endif
343
344/** @}
345 */
Note: See TracBrowser for help on using the repository browser.