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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9724d7f was 55b77d9, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Separate list_t typedef from link_t (kernel part).

  • list_t represents lists
  • Use list_first(), list_last(), list_empty() where appropriate
  • Use list_foreach() where possible
  • Replace improper uses of list_prepend() with list_insert_after()
  • Replace improper uses of list_append() with list_insert_before()
  • Property mode set to 100644
File size: 7.3 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 genericadt
31 * @{
32 */
33/** @file
34 */
35
36#ifndef KERN_LIST_H_
37#define KERN_LIST_H_
38
39#include <typedefs.h>
40#include <trace.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 = { \
60 .head = { \
61 .prev = &(name).head, \
62 .next = &(name).head \
63 } \
64 }
65
66#define list_get_instance(link, type, member) \
67 ((type *) (((void *)(link)) - ((void *) &(((type *) NULL)->member))))
68
69#define list_foreach(list, iterator) \
70 for (link_t *iterator = (list).head.next; \
71 iterator != &(list).head; iterator = iterator->next)
72
73/** Initialize doubly-linked circular list link
74 *
75 * Initialize doubly-linked list link.
76 *
77 * @param link Pointer to link_t structure to be initialized.
78 *
79 */
80NO_TRACE static inline void link_initialize(link_t *link)
81{
82 link->prev = NULL;
83 link->next = NULL;
84}
85
86/** Initialize doubly-linked circular list
87 *
88 * Initialize doubly-linked circular list.
89 *
90 * @param list Pointer to list_t structure.
91 *
92 */
93NO_TRACE static inline void list_initialize(list_t *list)
94{
95 list->head.prev = &list->head;
96 list->head.next = &list->head;
97}
98
99/** Insert item before another item in doubly-linked circular list.
100 *
101 */
102static inline void list_insert_before(link_t *lnew, link_t *lold)
103{
104 lnew->next = lold;
105 lnew->prev = lold->prev;
106 lold->prev->next = lnew;
107 lold->prev = lnew;
108}
109
110/** Insert item after another item in doubly-linked circular list.
111 *
112 */
113static inline void list_insert_after(link_t *lnew, link_t *lold)
114{
115 lnew->prev = lold;
116 lnew->next = lold->next;
117 lold->next->prev = lnew;
118 lold->next = lnew;
119}
120
121/** Add item to the beginning of doubly-linked circular list
122 *
123 * Add item to the beginning of doubly-linked circular list.
124 *
125 * @param link Pointer to link_t structure to be added.
126 * @param list Pointer to list_t structure.
127 *
128 */
129NO_TRACE static inline void list_prepend(link_t *link, list_t *list)
130{
131 list_insert_after(link, &list->head);
132}
133
134/** Add item to the end of doubly-linked circular list
135 *
136 * Add item to the end of doubly-linked circular list.
137 *
138 * @param link Pointer to link_t structure to be added.
139 * @param list Pointer to list_t structure.
140 *
141 */
142NO_TRACE static inline void list_append(link_t *link, list_t *list)
143{
144 list_insert_before(link, &list->head);
145}
146
147/** Remove item from doubly-linked circular list
148 *
149 * Remove item from doubly-linked circular list.
150 *
151 * @param link Pointer to link_t structure to be removed from the list
152 * it is contained in.
153 *
154 */
155NO_TRACE static inline void list_remove(link_t *link)
156{
157 link->next->prev = link->prev;
158 link->prev->next = link->next;
159 link_initialize(link);
160}
161
162/** Query emptiness of doubly-linked circular list
163 *
164 * Query emptiness of doubly-linked circular list.
165 *
166 * @param list Pointer to lins_t structure.
167 *
168 */
169NO_TRACE static inline int list_empty(list_t *list)
170{
171 return (list->head.next == &list->head);
172}
173
174/** Get first item in list.
175 *
176 * @param list Pointer to list_t structure.
177 *
178 * @return Head item of the list.
179 * @return NULL if the list is empty.
180 *
181 */
182static inline link_t *list_first(list_t *list)
183{
184 return ((list->head.next == &list->head) ? NULL : list->head.next);
185}
186
187/** Get last item in list.
188 *
189 * @param list Pointer to list_t structure.
190 *
191 * @return Head item of the list.
192 * @return NULL if the list is empty.
193 *
194 */
195static inline link_t *list_last(list_t *list)
196{
197 return ((list->head.prev == &list->head) ? NULL : list->head.prev);
198}
199
200/** Split or concatenate headless doubly-linked circular list
201 *
202 * Split or concatenate headless doubly-linked circular list.
203 *
204 * Note that the algorithm works both directions:
205 * concatenates splitted lists and splits concatenated lists.
206 *
207 * @param part1 Pointer to link_t structure leading the first
208 * (half of the headless) list.
209 * @param part2 Pointer to link_t structure leading the second
210 * (half of the headless) list.
211 *
212 */
213NO_TRACE static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
214{
215 part1->prev->next = part2;
216 part2->prev->next = part1;
217
218 link_t *hlp = part1->prev;
219
220 part1->prev = part2->prev;
221 part2->prev = hlp;
222}
223
224/** Split headless doubly-linked circular list
225 *
226 * Split headless doubly-linked circular list.
227 *
228 * @param part1 Pointer to link_t structure leading
229 * the first half of the headless list.
230 * @param part2 Pointer to link_t structure leading
231 * the second half of the headless list.
232 *
233 */
234NO_TRACE static inline void headless_list_split(link_t *part1, link_t *part2)
235{
236 headless_list_split_or_concat(part1, part2);
237}
238
239/** Concatenate two headless doubly-linked circular lists
240 *
241 * Concatenate two headless doubly-linked circular lists.
242 *
243 * @param part1 Pointer to link_t structure leading
244 * the first headless list.
245 * @param part2 Pointer to link_t structure leading
246 * the second headless list.
247 *
248 */
249NO_TRACE static inline void headless_list_concat(link_t *part1, link_t *part2)
250{
251 headless_list_split_or_concat(part1, part2);
252}
253
254/** Get n-th item in a list.
255 *
256 * @param list Pointer to link_t structure representing the list.
257 * @param n Item number (indexed from zero).
258 *
259 * @return n-th item of the list.
260 * @return NULL if no n-th item found.
261 *
262 */
263static inline link_t *list_nth(list_t *list, unsigned int n)
264{
265 unsigned int cnt = 0;
266
267 list_foreach(*list, link) {
268 if (cnt == n)
269 return link;
270
271 cnt++;
272 }
273
274 return NULL;
275}
276
277extern int list_member(const link_t *, const list_t *);
278extern void list_concat(list_t *, list_t *);
279extern unsigned int list_count(const list_t *);
280
281#endif
282
283/** @}
284 */
Note: See TracBrowser for help on using the repository browser.