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

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

linked list improvements
port uspace linked list improvements back to kernel

  • Property mode set to 100644
File size: 6.9 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup genericadt
30 * @{
31 */
32/** @file
33 */
34
35#ifndef KERN_LIST_H_
36#define KERN_LIST_H_
37
38#include <typedefs.h>
39#include <trace.h>
40
41/** Doubly linked list head and link type. */
42typedef struct link {
43 struct link *prev; /**< Pointer to the previous item in the list. */
44 struct link *next; /**< Pointer to the next item in the list. */
45} link_t;
46
47/** Declare and initialize statically allocated list.
48 *
49 * @param name Name of the new statically allocated list.
50 *
51 */
52#define LIST_INITIALIZE(name) \
53 link_t name = { \
54 .prev = &name, \
55 .next = &name \
56 }
57
58#define list_get_instance(link, type, member) \
59 ((type *) (((void *)(link)) - ((void *) &(((type *) NULL)->member))))
60
61#define list_foreach(list, iterator) \
62 for (link_t *iterator = (list).next; \
63 iterator != &(list); iterator = iterator->next)
64
65/** Initialize doubly-linked circular list link
66 *
67 * Initialize doubly-linked list link.
68 *
69 * @param link Pointer to link_t structure to be initialized.
70 *
71 */
72NO_TRACE static inline void link_initialize(link_t *link)
73{
74 link->prev = NULL;
75 link->next = NULL;
76}
77
78/** Initialize doubly-linked circular list
79 *
80 * Initialize doubly-linked circular list.
81 *
82 * @param list Pointer to link_t structure representing the list.
83 *
84 */
85NO_TRACE static inline void list_initialize(link_t *list)
86{
87 list->prev = list;
88 list->next = list;
89}
90
91/** Add item to the beginning of doubly-linked circular list
92 *
93 * Add item to the beginning of doubly-linked circular list.
94 *
95 * @param link Pointer to link_t structure to be added.
96 * @param list Pointer to link_t structure representing the list.
97 *
98 */
99NO_TRACE static inline void list_prepend(link_t *link, link_t *list)
100{
101 link->next = list->next;
102 link->prev = list;
103 list->next->prev = link;
104 list->next = link;
105}
106
107/** Add item to the end of doubly-linked circular list
108 *
109 * Add item to the end of doubly-linked circular list.
110 *
111 * @param link Pointer to link_t structure to be added.
112 * @param list Pointer to link_t structure representing the list.
113 *
114 */
115NO_TRACE static inline void list_append(link_t *link, link_t *list)
116{
117 link->prev = list->prev;
118 link->next = list;
119 list->prev->next = link;
120 list->prev = link;
121}
122
123/** Insert item before another item in doubly-linked circular list.
124 *
125 */
126static inline void list_insert_before(link_t *link, link_t *list)
127{
128 list_append(link, list);
129}
130
131/** Insert item after another item in doubly-linked circular list.
132 *
133 */
134static inline void list_insert_after(link_t *link, link_t *list)
135{
136 list_prepend(list, link);
137}
138
139/** Remove item from doubly-linked circular list
140 *
141 * Remove item from doubly-linked circular list.
142 *
143 * @param link Pointer to link_t structure to be removed from the list
144 * it is contained in.
145 *
146 */
147NO_TRACE static inline void list_remove(link_t *link)
148{
149 link->next->prev = link->prev;
150 link->prev->next = link->next;
151 link_initialize(link);
152}
153
154/** Query emptiness of doubly-linked circular list
155 *
156 * Query emptiness of doubly-linked circular list.
157 *
158 * @param list Pointer to link_t structure representing the list.
159 *
160 */
161NO_TRACE static inline int list_empty(link_t *list)
162{
163 return (list->next == list);
164}
165
166/** Get head item of a list.
167 *
168 * @param list Pointer to link_t structure representing the list.
169 *
170 * @return Head item of the list.
171 * @return NULL if the list is empty.
172 *
173 */
174static inline link_t *list_head(link_t *list)
175{
176 return ((list->next == list) ? NULL : list->next);
177}
178
179/** Split or concatenate headless doubly-linked circular list
180 *
181 * Split or concatenate headless doubly-linked circular list.
182 *
183 * Note that the algorithm works both directions:
184 * concatenates splitted lists and splits concatenated lists.
185 *
186 * @param part1 Pointer to link_t structure leading the first
187 * (half of the headless) list.
188 * @param part2 Pointer to link_t structure leading the second
189 * (half of the headless) list.
190 *
191 */
192NO_TRACE static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
193{
194 part1->prev->next = part2;
195 part2->prev->next = part1;
196
197 link_t *hlp = part1->prev;
198
199 part1->prev = part2->prev;
200 part2->prev = hlp;
201}
202
203/** Split headless doubly-linked circular list
204 *
205 * Split headless doubly-linked circular list.
206 *
207 * @param part1 Pointer to link_t structure leading
208 * the first half of the headless list.
209 * @param part2 Pointer to link_t structure leading
210 * the second half of the headless list.
211 *
212 */
213NO_TRACE static inline void headless_list_split(link_t *part1, link_t *part2)
214{
215 headless_list_split_or_concat(part1, part2);
216}
217
218/** Concatenate two headless doubly-linked circular lists
219 *
220 * Concatenate two headless doubly-linked circular lists.
221 *
222 * @param part1 Pointer to link_t structure leading
223 * the first headless list.
224 * @param part2 Pointer to link_t structure leading
225 * the second headless list.
226 *
227 */
228NO_TRACE static inline void headless_list_concat(link_t *part1, link_t *part2)
229{
230 headless_list_split_or_concat(part1, part2);
231}
232
233/** Get n-th item of a list.
234 *
235 * @param list Pointer to link_t structure representing the list.
236 * @param n Item number (indexed from zero).
237 *
238 * @return n-th item of the list.
239 * @return NULL if no n-th item found.
240 *
241 */
242static inline link_t *list_nth(link_t *list, unsigned int n)
243{
244 unsigned int cnt = 0;
245
246 list_foreach(*list, link) {
247 if (cnt == n)
248 return link;
249
250 cnt++;
251 }
252
253 return NULL;
254}
255
256extern int list_member(const link_t *, const link_t *);
257extern void list_concat(link_t *, link_t *);
258extern unsigned int list_count(const link_t *);
259
260#endif
261
262/** @}
263 */
Note: See TracBrowser for help on using the repository browser.