Rev 2446 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2446 | Rev 3068 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genericadt |
29 | /** @addtogroup genericadt |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
34 | 34 | ||
35 | #ifndef KERN_LIST_H_ |
35 | #ifndef KERN_LIST_H_ |
36 | #define KERN_LIST_H_ |
36 | #define KERN_LIST_H_ |
37 | 37 | ||
38 | #include <arch/types.h> |
38 | #include <arch/types.h> |
- | 39 | #include <typedefs.h> |
|
39 | 40 | ||
40 | /** Doubly linked list head and link type. */ |
41 | /** Doubly linked list head and link type. */ |
41 | typedef struct link { |
42 | typedef struct link { |
42 | struct link *prev; /**< Pointer to the previous item in the list. */ |
43 | struct link *prev; /**< Pointer to the previous item in the list. */ |
43 | struct link *next; /**< Pointer to the next item in the list. */ |
44 | struct link *next; /**< Pointer to the next item in the list. */ |
44 | } link_t; |
45 | } link_t; |
45 | 46 | ||
46 | /** Declare and initialize statically allocated list. |
47 | /** Declare and initialize statically allocated list. |
47 | * |
48 | * |
48 | * @param name Name of the new statically allocated list. |
49 | * @param name Name of the new statically allocated list. |
49 | */ |
50 | */ |
50 | #define LIST_INITIALIZE(name) \ |
51 | #define LIST_INITIALIZE(name) \ |
51 | link_t name = { .prev = &name, .next = &name } |
52 | link_t name = { .prev = &name, .next = &name } |
52 | 53 | ||
53 | /** Initialize doubly-linked circular list link |
54 | /** Initialize doubly-linked circular list link |
54 | * |
55 | * |
55 | * Initialize doubly-linked list link. |
56 | * Initialize doubly-linked list link. |
56 | * |
57 | * |
57 | * @param link Pointer to link_t structure to be initialized. |
58 | * @param link Pointer to link_t structure to be initialized. |
58 | */ |
59 | */ |
59 | static inline void link_initialize(link_t *link) |
60 | static inline void link_initialize(link_t *link) |
60 | { |
61 | { |
61 | link->prev = NULL; |
62 | link->prev = NULL; |
62 | link->next = NULL; |
63 | link->next = NULL; |
63 | } |
64 | } |
64 | 65 | ||
65 | /** Initialize doubly-linked circular list |
66 | /** Initialize doubly-linked circular list |
66 | * |
67 | * |
67 | * Initialize doubly-linked circular list. |
68 | * Initialize doubly-linked circular list. |
68 | * |
69 | * |
69 | * @param head Pointer to link_t structure representing head of the list. |
70 | * @param head Pointer to link_t structure representing head of the list. |
70 | */ |
71 | */ |
71 | static inline void list_initialize(link_t *head) |
72 | static inline void list_initialize(link_t *head) |
72 | { |
73 | { |
73 | head->prev = head; |
74 | head->prev = head; |
74 | head->next = head; |
75 | head->next = head; |
75 | } |
76 | } |
76 | 77 | ||
77 | /** Add item to the beginning of doubly-linked circular list |
78 | /** Add item to the beginning of doubly-linked circular list |
78 | * |
79 | * |
79 | * Add item to the beginning of doubly-linked circular list. |
80 | * Add item to the beginning of doubly-linked circular list. |
80 | * |
81 | * |
81 | * @param link Pointer to link_t structure to be added. |
82 | * @param link Pointer to link_t structure to be added. |
82 | * @param head Pointer to link_t structure representing head of the list. |
83 | * @param head Pointer to link_t structure representing head of the list. |
83 | */ |
84 | */ |
84 | static inline void list_prepend(link_t *link, link_t *head) |
85 | static inline void list_prepend(link_t *link, link_t *head) |
85 | { |
86 | { |
86 | link->next = head->next; |
87 | link->next = head->next; |
87 | link->prev = head; |
88 | link->prev = head; |
88 | head->next->prev = link; |
89 | head->next->prev = link; |
89 | head->next = link; |
90 | head->next = link; |
90 | } |
91 | } |
91 | 92 | ||
92 | /** Add item to the end of doubly-linked circular list |
93 | /** Add item to the end of doubly-linked circular list |
93 | * |
94 | * |
94 | * Add item to the end of doubly-linked circular list. |
95 | * Add item to the end of doubly-linked circular list. |
95 | * |
96 | * |
96 | * @param link Pointer to link_t structure to be added. |
97 | * @param link Pointer to link_t structure to be added. |
97 | * @param head Pointer to link_t structure representing head of the list. |
98 | * @param head Pointer to link_t structure representing head of the list. |
98 | */ |
99 | */ |
99 | static inline void list_append(link_t *link, link_t *head) |
100 | static inline void list_append(link_t *link, link_t *head) |
100 | { |
101 | { |
101 | link->prev = head->prev; |
102 | link->prev = head->prev; |
102 | link->next = head; |
103 | link->next = head; |
103 | head->prev->next = link; |
104 | head->prev->next = link; |
104 | head->prev = link; |
105 | head->prev = link; |
105 | } |
106 | } |
106 | 107 | ||
107 | /** Remove item from doubly-linked circular list |
108 | /** Remove item from doubly-linked circular list |
108 | * |
109 | * |
109 | * Remove item from doubly-linked circular list. |
110 | * Remove item from doubly-linked circular list. |
110 | * |
111 | * |
111 | * @param link Pointer to link_t structure to be removed from the list it is |
112 | * @param link Pointer to link_t structure to be removed from the list it is |
112 | * contained in. |
113 | * contained in. |
113 | */ |
114 | */ |
114 | static inline void list_remove(link_t *link) |
115 | static inline void list_remove(link_t *link) |
115 | { |
116 | { |
116 | link->next->prev = link->prev; |
117 | link->next->prev = link->prev; |
117 | link->prev->next = link->next; |
118 | link->prev->next = link->next; |
118 | link_initialize(link); |
119 | link_initialize(link); |
119 | } |
120 | } |
120 | 121 | ||
121 | /** Query emptiness of doubly-linked circular list |
122 | /** Query emptiness of doubly-linked circular list |
122 | * |
123 | * |
123 | * Query emptiness of doubly-linked circular list. |
124 | * Query emptiness of doubly-linked circular list. |
124 | * |
125 | * |
125 | * @param head Pointer to link_t structure representing head of the list. |
126 | * @param head Pointer to link_t structure representing head of the list. |
126 | */ |
127 | */ |
127 | static inline bool list_empty(link_t *head) |
128 | static inline bool list_empty(link_t *head) |
128 | { |
129 | { |
129 | return head->next == head ? true : false; |
130 | return head->next == head ? true : false; |
130 | } |
131 | } |
131 | 132 | ||
132 | 133 | ||
133 | /** Split or concatenate headless doubly-linked circular list |
134 | /** Split or concatenate headless doubly-linked circular list |
134 | * |
135 | * |
135 | * Split or concatenate headless doubly-linked circular list. |
136 | * Split or concatenate headless doubly-linked circular list. |
136 | * |
137 | * |
137 | * Note that the algorithm works both directions: |
138 | * Note that the algorithm works both directions: |
138 | * concatenates splitted lists and splits concatenated lists. |
139 | * concatenates splitted lists and splits concatenated lists. |
139 | * |
140 | * |
140 | * @param part1 Pointer to link_t structure leading the first (half of the |
141 | * @param part1 Pointer to link_t structure leading the first (half of the |
141 | * headless) list. |
142 | * headless) list. |
142 | * @param part2 Pointer to link_t structure leading the second (half of the |
143 | * @param part2 Pointer to link_t structure leading the second (half of the |
143 | * headless) list. |
144 | * headless) list. |
144 | */ |
145 | */ |
145 | static inline void headless_list_split_or_concat(link_t *part1, link_t *part2) |
146 | static inline void headless_list_split_or_concat(link_t *part1, link_t *part2) |
146 | { |
147 | { |
147 | link_t *hlp; |
148 | link_t *hlp; |
148 | 149 | ||
149 | part1->prev->next = part2; |
150 | part1->prev->next = part2; |
150 | part2->prev->next = part1; |
151 | part2->prev->next = part1; |
151 | hlp = part1->prev; |
152 | hlp = part1->prev; |
152 | part1->prev = part2->prev; |
153 | part1->prev = part2->prev; |
153 | part2->prev = hlp; |
154 | part2->prev = hlp; |
154 | } |
155 | } |
155 | 156 | ||
156 | 157 | ||
157 | /** Split headless doubly-linked circular list |
158 | /** Split headless doubly-linked circular list |
158 | * |
159 | * |
159 | * Split headless doubly-linked circular list. |
160 | * Split headless doubly-linked circular list. |
160 | * |
161 | * |
161 | * @param part1 Pointer to link_t structure leading the first half of the |
162 | * @param part1 Pointer to link_t structure leading the first half of the |
162 | * headless list. |
163 | * headless list. |
163 | * @param part2 Pointer to link_t structure leading the second half of the |
164 | * @param part2 Pointer to link_t structure leading the second half of the |
164 | * headless list. |
165 | * headless list. |
165 | */ |
166 | */ |
166 | static inline void headless_list_split(link_t *part1, link_t *part2) |
167 | static inline void headless_list_split(link_t *part1, link_t *part2) |
167 | { |
168 | { |
168 | headless_list_split_or_concat(part1, part2); |
169 | headless_list_split_or_concat(part1, part2); |
169 | } |
170 | } |
170 | 171 | ||
171 | /** Concatenate two headless doubly-linked circular lists |
172 | /** Concatenate two headless doubly-linked circular lists |
172 | * |
173 | * |
173 | * Concatenate two headless doubly-linked circular lists. |
174 | * Concatenate two headless doubly-linked circular lists. |
174 | * |
175 | * |
175 | * @param part1 Pointer to link_t structure leading the first headless list. |
176 | * @param part1 Pointer to link_t structure leading the first headless list. |
176 | * @param part2 Pointer to link_t structure leading the second headless list. |
177 | * @param part2 Pointer to link_t structure leading the second headless list. |
177 | */ |
178 | */ |
178 | static inline void headless_list_concat(link_t *part1, link_t *part2) |
179 | static inline void headless_list_concat(link_t *part1, link_t *part2) |
179 | { |
180 | { |
180 | headless_list_split_or_concat(part1, part2); |
181 | headless_list_split_or_concat(part1, part2); |
181 | } |
182 | } |
182 | 183 | ||
183 | #define list_get_instance(link, type, member) \ |
184 | #define list_get_instance(link, type, member) \ |
184 | ((type *)(((uint8_t *)(link)) - ((uint8_t *)&(((type *)NULL)->member)))) |
185 | ((type *)(((uint8_t *)(link)) - ((uint8_t *)&(((type *)NULL)->member)))) |
185 | 186 | ||
186 | extern bool list_member(const link_t *link, const link_t *head); |
187 | extern bool list_member(const link_t *link, const link_t *head); |
187 | extern void list_concat(link_t *head1, link_t *head2); |
188 | extern void list_concat(link_t *head1, link_t *head2); |
188 | 189 | ||
189 | #endif |
190 | #endif |
190 | 191 | ||
191 | /** @} |
192 | /** @} |
192 | */ |
193 | */ |
193 | 194 |