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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 313775b 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
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[f761f1eb]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
[06e1e95]29/** @addtogroup genericadt
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[06e1e95]35#ifndef KERN_LIST_H_
36#define KERN_LIST_H_
[f761f1eb]37
[7ac426e]38#include <typedefs.h>
[7a0359b]39#include <trace.h>
[f761f1eb]40
[7dd2561]41/** Doubly linked list head and link type. */
[b3f8fb7]42typedef struct link {
[0a02653]43 struct link *prev; /**< Pointer to the previous item in the list. */
44 struct link *next; /**< Pointer to the next item in the list. */
[b3f8fb7]45} link_t;
[f761f1eb]46
[7dd2561]47/** Declare and initialize statically allocated list.
48 *
49 * @param name Name of the new statically allocated list.
[0a02653]50 *
[7dd2561]51 */
[80bcaed]52#define LIST_INITIALIZE(name) \
[0a02653]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)
[7dd2561]64
[40a468a]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.
[0a02653]70 *
[40a468a]71 */
[7a0359b]72NO_TRACE static inline void link_initialize(link_t *link)
[c0a91d1]73{
74 link->prev = NULL;
75 link->next = NULL;
[f761f1eb]76}
77
[40a468a]78/** Initialize doubly-linked circular list
79 *
80 * Initialize doubly-linked circular list.
81 *
[0a02653]82 * @param list Pointer to link_t structure representing the list.
83 *
[40a468a]84 */
[0a02653]85NO_TRACE static inline void list_initialize(link_t *list)
[c0a91d1]86{
[0a02653]87 list->prev = list;
88 list->next = list;
[f761f1eb]89}
90
[40a468a]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.
[0a02653]96 * @param list Pointer to link_t structure representing the list.
97 *
[40a468a]98 */
[0a02653]99NO_TRACE static inline void list_prepend(link_t *link, link_t *list)
[c0a91d1]100{
[0a02653]101 link->next = list->next;
102 link->prev = list;
103 list->next->prev = link;
104 list->next = link;
[f761f1eb]105}
106
[40a468a]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.
[0a02653]112 * @param list Pointer to link_t structure representing the list.
113 *
[40a468a]114 */
[0a02653]115NO_TRACE static inline void list_append(link_t *link, link_t *list)
[c0a91d1]116{
[0a02653]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);
[f761f1eb]137}
138
[40a468a]139/** Remove item from doubly-linked circular list
140 *
141 * Remove item from doubly-linked circular list.
142 *
[0a02653]143 * @param link Pointer to link_t structure to be removed from the list
144 * it is contained in.
145 *
[40a468a]146 */
[7a0359b]147NO_TRACE static inline void list_remove(link_t *link)
[c0a91d1]148{
149 link->next->prev = link->prev;
150 link->prev->next = link->next;
151 link_initialize(link);
[f761f1eb]152}
153
[40a468a]154/** Query emptiness of doubly-linked circular list
155 *
156 * Query emptiness of doubly-linked circular list.
157 *
[0a02653]158 * @param list Pointer to link_t structure representing the list.
159 *
[40a468a]160 */
[0a02653]161NO_TRACE static inline int list_empty(link_t *list)
[40a468a]162{
[0a02653]163 return (list->next == list);
[40a468a]164}
165
[0a02653]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}
[40a468a]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 *
[0a02653]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 *
[40a468a]191 */
[7a0359b]192NO_TRACE static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
[40a468a]193{
194 part1->prev->next = part2;
[0a02653]195 part2->prev->next = part1;
196
197 link_t *hlp = part1->prev;
198
[40a468a]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 *
[0a02653]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 *
[40a468a]212 */
[7a0359b]213NO_TRACE static inline void headless_list_split(link_t *part1, link_t *part2)
[40a468a]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 *
[0a02653]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 *
[40a468a]227 */
[7a0359b]228NO_TRACE static inline void headless_list_concat(link_t *part1, link_t *part2)
[40a468a]229{
230 headless_list_split_or_concat(part1, part2);
231}
[f761f1eb]232
[0a02653]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}
[f761f1eb]255
[0a02653]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 *);
[f761f1eb]259
260#endif
[b45c443]261
[06e1e95]262/** @}
[b45c443]263 */
Note: See TracBrowser for help on using the repository browser.