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 libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #ifndef LIBC_LIST_H_
|
---|
36 | #define LIBC_LIST_H_
|
---|
37 |
|
---|
38 | #include <unistd.h>
|
---|
39 |
|
---|
40 | /** Doubly linked list head and link type. */
|
---|
41 | typedef struct link {
|
---|
42 | struct link *prev; /**< Pointer to the previous item in the list. */
|
---|
43 | struct link *next; /**< Pointer to the next item in the list. */
|
---|
44 | } link_t;
|
---|
45 |
|
---|
46 | /** Declare and initialize statically allocated list.
|
---|
47 | *
|
---|
48 | * @param name Name of the new statically allocated list.
|
---|
49 | *
|
---|
50 | */
|
---|
51 | #define LIST_INITIALIZE(name) \
|
---|
52 | link_t name = { \
|
---|
53 | .prev = &name, \
|
---|
54 | .next = &name \
|
---|
55 | }
|
---|
56 |
|
---|
57 | #define list_get_instance(link, type, member) \
|
---|
58 | ((type *) (((void *)(link)) - ((void *) &(((type *) NULL)->member))))
|
---|
59 |
|
---|
60 | #define list_foreach(list, iterator) \
|
---|
61 | for (link_t *iterator = (list).next; \
|
---|
62 | iterator != &(list); iterator = iterator->next)
|
---|
63 |
|
---|
64 | /** Initialize doubly-linked circular list link
|
---|
65 | *
|
---|
66 | * Initialize doubly-linked list link.
|
---|
67 | *
|
---|
68 | * @param link Pointer to link_t structure to be initialized.
|
---|
69 | *
|
---|
70 | */
|
---|
71 | static inline void link_initialize(link_t *link)
|
---|
72 | {
|
---|
73 | link->prev = NULL;
|
---|
74 | link->next = NULL;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /** Initialize doubly-linked circular list
|
---|
78 | *
|
---|
79 | * Initialize doubly-linked circular list.
|
---|
80 | *
|
---|
81 | * @param list Pointer to link_t structure representing the list.
|
---|
82 | *
|
---|
83 | */
|
---|
84 | static inline void list_initialize(link_t *list)
|
---|
85 | {
|
---|
86 | list->prev = list;
|
---|
87 | list->next = list;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /** Add item to the beginning of doubly-linked circular list
|
---|
91 | *
|
---|
92 | * Add item to the beginning of doubly-linked circular list.
|
---|
93 | *
|
---|
94 | * @param link Pointer to link_t structure to be added.
|
---|
95 | * @param list Pointer to link_t structure representing the list.
|
---|
96 | *
|
---|
97 | */
|
---|
98 | static inline void list_prepend(link_t *link, link_t *list)
|
---|
99 | {
|
---|
100 | link->next = list->next;
|
---|
101 | link->prev = list;
|
---|
102 | list->next->prev = link;
|
---|
103 | list->next = link;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /** Add item to the end of doubly-linked circular list
|
---|
107 | *
|
---|
108 | * Add item to the end of doubly-linked circular list.
|
---|
109 | *
|
---|
110 | * @param link Pointer to link_t structure to be added.
|
---|
111 | * @param list Pointer to link_t structure representing the list.
|
---|
112 | *
|
---|
113 | */
|
---|
114 | static inline void list_append(link_t *link, link_t *list)
|
---|
115 | {
|
---|
116 | link->prev = list->prev;
|
---|
117 | link->next = list;
|
---|
118 | list->prev->next = link;
|
---|
119 | list->prev = link;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /** Insert item before another item in doubly-linked circular list.
|
---|
123 | *
|
---|
124 | */
|
---|
125 | static inline void list_insert_before(link_t *link, link_t *list)
|
---|
126 | {
|
---|
127 | list_append(link, list);
|
---|
128 | }
|
---|
129 |
|
---|
130 | /** Insert item after another item in doubly-linked circular list.
|
---|
131 | *
|
---|
132 | */
|
---|
133 | static inline void list_insert_after(link_t *link, link_t *list)
|
---|
134 | {
|
---|
135 | list_prepend(list, link);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** Remove item from doubly-linked circular list
|
---|
139 | *
|
---|
140 | * Remove item from doubly-linked circular list.
|
---|
141 | *
|
---|
142 | * @param link Pointer to link_t structure to be removed from the list
|
---|
143 | * it is contained in.
|
---|
144 | *
|
---|
145 | */
|
---|
146 | static inline void list_remove(link_t *link)
|
---|
147 | {
|
---|
148 | link->next->prev = link->prev;
|
---|
149 | link->prev->next = link->next;
|
---|
150 | link_initialize(link);
|
---|
151 | }
|
---|
152 |
|
---|
153 | /** Query emptiness of doubly-linked circular list
|
---|
154 | *
|
---|
155 | * Query emptiness of doubly-linked circular list.
|
---|
156 | *
|
---|
157 | * @param list Pointer to link_t structure representing the list.
|
---|
158 | *
|
---|
159 | */
|
---|
160 | static inline int list_empty(link_t *list)
|
---|
161 | {
|
---|
162 | return (list->next == list);
|
---|
163 | }
|
---|
164 |
|
---|
165 | /** Get head item of a list.
|
---|
166 | *
|
---|
167 | * @param list Pointer to link_t structure representing the list.
|
---|
168 | *
|
---|
169 | * @return Head item of the list.
|
---|
170 | * @return NULL if the list is empty.
|
---|
171 | *
|
---|
172 | */
|
---|
173 | static inline link_t *list_head(link_t *list)
|
---|
174 | {
|
---|
175 | return ((list->next == list) ? NULL : list->next);
|
---|
176 | }
|
---|
177 |
|
---|
178 | /** Split or concatenate headless doubly-linked circular list
|
---|
179 | *
|
---|
180 | * Split or concatenate headless doubly-linked circular list.
|
---|
181 | *
|
---|
182 | * Note that the algorithm works both directions:
|
---|
183 | * concatenates splitted lists and splits concatenated lists.
|
---|
184 | *
|
---|
185 | * @param part1 Pointer to link_t structure leading the first
|
---|
186 | * (half of the headless) list.
|
---|
187 | * @param part2 Pointer to link_t structure leading the second
|
---|
188 | * (half of the headless) list.
|
---|
189 | *
|
---|
190 | */
|
---|
191 | static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
|
---|
192 | {
|
---|
193 | part1->prev->next = part2;
|
---|
194 | part2->prev->next = part1;
|
---|
195 |
|
---|
196 | link_t *hlp = part1->prev;
|
---|
197 |
|
---|
198 | part1->prev = part2->prev;
|
---|
199 | part2->prev = hlp;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /** Split headless doubly-linked circular list
|
---|
203 | *
|
---|
204 | * Split headless doubly-linked circular list.
|
---|
205 | *
|
---|
206 | * @param part1 Pointer to link_t structure leading
|
---|
207 | * the first half of the headless list.
|
---|
208 | * @param part2 Pointer to link_t structure leading
|
---|
209 | * the second half of the headless list.
|
---|
210 | *
|
---|
211 | */
|
---|
212 | static inline void headless_list_split(link_t *part1, link_t *part2)
|
---|
213 | {
|
---|
214 | headless_list_split_or_concat(part1, part2);
|
---|
215 | }
|
---|
216 |
|
---|
217 | /** Concatenate two headless doubly-linked circular lists
|
---|
218 | *
|
---|
219 | * Concatenate two headless doubly-linked circular lists.
|
---|
220 | *
|
---|
221 | * @param part1 Pointer to link_t structure leading
|
---|
222 | * the first headless list.
|
---|
223 | * @param part2 Pointer to link_t structure leading
|
---|
224 | * the second headless list.
|
---|
225 | *
|
---|
226 | */
|
---|
227 | static inline void headless_list_concat(link_t *part1, link_t *part2)
|
---|
228 | {
|
---|
229 | headless_list_split_or_concat(part1, part2);
|
---|
230 | }
|
---|
231 |
|
---|
232 | /** Get n-th item of a list.
|
---|
233 | *
|
---|
234 | * @param list Pointer to link_t structure representing the list.
|
---|
235 | * @param n Item number (indexed from zero).
|
---|
236 | *
|
---|
237 | * @return n-th item of the list.
|
---|
238 | * @return NULL if no n-th item found.
|
---|
239 | *
|
---|
240 | */
|
---|
241 | static inline link_t *list_nth(link_t *list, unsigned int n)
|
---|
242 | {
|
---|
243 | unsigned int cnt = 0;
|
---|
244 |
|
---|
245 | list_foreach(*list, link) {
|
---|
246 | if (cnt == n)
|
---|
247 | return link;
|
---|
248 |
|
---|
249 | cnt++;
|
---|
250 | }
|
---|
251 |
|
---|
252 | return NULL;
|
---|
253 | }
|
---|
254 |
|
---|
255 | extern int list_member(const link_t *, const link_t *);
|
---|
256 | extern void list_concat(link_t *, link_t *);
|
---|
257 | extern unsigned int list_count(const link_t *);
|
---|
258 |
|
---|
259 | #endif
|
---|
260 |
|
---|
261 | /** @}
|
---|
262 | */
|
---|