Rev 4065 | Rev 5270 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4065 | Rev 5056 | ||
---|---|---|---|
1 | #ifndef _LINUX_LIST_H |
1 | #ifndef _LINUX_LIST_H |
2 | #define _LINUX_LIST_H |
2 | #define _LINUX_LIST_H |
3 | 3 | ||
4 | #include |
4 | #include |
5 | #include |
5 | #include |
6 | #include |
6 | #include |
7 | 7 | ||
8 | /* |
8 | /* |
9 | * Simple doubly linked list implementation. |
9 | * Simple doubly linked list implementation. |
10 | * |
10 | * |
11 | * Some of the internal functions ("__xxx") are useful when |
11 | * Some of the internal functions ("__xxx") are useful when |
12 | * manipulating whole lists rather than single entries, as |
12 | * manipulating whole lists rather than single entries, as |
13 | * sometimes we already know the next/prev entries and we can |
13 | * sometimes we already know the next/prev entries and we can |
14 | * generate better code by using them directly rather than |
14 | * generate better code by using them directly rather than |
15 | * using the generic single-entry routines. |
15 | * using the generic single-entry routines. |
16 | */ |
16 | */ |
17 | 17 | ||
18 | #define LIST_HEAD_INIT(name) { &(name), &(name) } |
18 | #define LIST_HEAD_INIT(name) { &(name), &(name) } |
19 | 19 | ||
20 | #define LIST_HEAD(name) \ |
20 | #define LIST_HEAD(name) \ |
21 | struct list_head name = LIST_HEAD_INIT(name) |
21 | struct list_head name = LIST_HEAD_INIT(name) |
22 | 22 | ||
23 | static inline void INIT_LIST_HEAD(struct list_head *list) |
23 | static inline void INIT_LIST_HEAD(struct list_head *list) |
24 | { |
24 | { |
25 | list->next = list; |
25 | list->next = list; |
26 | list->prev = list; |
26 | list->prev = list; |
27 | } |
27 | } |
28 | 28 | ||
29 | /* |
29 | /* |
30 | * Insert a new entry between two known consecutive entries. |
30 | * Insert a new entry between two known consecutive entries. |
31 | * |
31 | * |
32 | * This is only for internal list manipulation where we know |
32 | * This is only for internal list manipulation where we know |
33 | * the prev/next entries already! |
33 | * the prev/next entries already! |
34 | */ |
34 | */ |
35 | #ifndef CONFIG_DEBUG_LIST |
35 | #ifndef CONFIG_DEBUG_LIST |
36 | static inline void __list_add(struct list_head *new, |
36 | static inline void __list_add(struct list_head *new, |
37 | struct list_head *prev, |
37 | struct list_head *prev, |
38 | struct list_head *next) |
38 | struct list_head *next) |
39 | { |
39 | { |
40 | next->prev = new; |
40 | next->prev = new; |
41 | new->next = next; |
41 | new->next = next; |
42 | new->prev = prev; |
42 | new->prev = prev; |
43 | prev->next = new; |
43 | prev->next = new; |
44 | } |
44 | } |
45 | #else |
45 | #else |
46 | extern void __list_add(struct list_head *new, |
46 | extern void __list_add(struct list_head *new, |
47 | struct list_head *prev, |
47 | struct list_head *prev, |
48 | struct list_head *next); |
48 | struct list_head *next); |
49 | #endif |
49 | #endif |
50 | 50 | ||
51 | /** |
51 | /** |
52 | * list_add - add a new entry |
52 | * list_add - add a new entry |
53 | * @new: new entry to be added |
53 | * @new: new entry to be added |
54 | * @head: list head to add it after |
54 | * @head: list head to add it after |
55 | * |
55 | * |
56 | * Insert a new entry after the specified head. |
56 | * Insert a new entry after the specified head. |
57 | * This is good for implementing stacks. |
57 | * This is good for implementing stacks. |
58 | */ |
58 | */ |
59 | static inline void list_add(struct list_head *new, struct list_head *head) |
59 | static inline void list_add(struct list_head *new, struct list_head *head) |
60 | { |
60 | { |
61 | __list_add(new, head, head->next); |
61 | __list_add(new, head, head->next); |
62 | } |
62 | } |
63 | 63 | ||
64 | 64 | ||
65 | /** |
65 | /** |
66 | * list_add_tail - add a new entry |
66 | * list_add_tail - add a new entry |
67 | * @new: new entry to be added |
67 | * @new: new entry to be added |
68 | * @head: list head to add it before |
68 | * @head: list head to add it before |
69 | * |
69 | * |
70 | * Insert a new entry before the specified head. |
70 | * Insert a new entry before the specified head. |
71 | * This is useful for implementing queues. |
71 | * This is useful for implementing queues. |
72 | */ |
72 | */ |
73 | static inline void list_add_tail(struct list_head *new, struct list_head *head) |
73 | static inline void list_add_tail(struct list_head *new, struct list_head *head) |
74 | { |
74 | { |
75 | __list_add(new, head->prev, head); |
75 | __list_add(new, head->prev, head); |
76 | } |
76 | } |
77 | 77 | ||
78 | /* |
78 | /* |
79 | * Delete a list entry by making the prev/next entries |
79 | * Delete a list entry by making the prev/next entries |
80 | * point to each other. |
80 | * point to each other. |
81 | * |
81 | * |
82 | * This is only for internal list manipulation where we know |
82 | * This is only for internal list manipulation where we know |
83 | * the prev/next entries already! |
83 | * the prev/next entries already! |
84 | */ |
84 | */ |
85 | static inline void __list_del(struct list_head * prev, struct list_head * next) |
85 | static inline void __list_del(struct list_head * prev, struct list_head * next) |
86 | { |
86 | { |
87 | next->prev = prev; |
87 | next->prev = prev; |
88 | prev->next = next; |
88 | prev->next = next; |
89 | } |
89 | } |
90 | 90 | ||
91 | /** |
91 | /** |
92 | * list_del - deletes entry from list. |
92 | * list_del - deletes entry from list. |
93 | * @entry: the element to delete from the list. |
93 | * @entry: the element to delete from the list. |
94 | * Note: list_empty() on entry does not return true after this, the entry is |
94 | * Note: list_empty() on entry does not return true after this, the entry is |
95 | * in an undefined state. |
95 | * in an undefined state. |
96 | */ |
96 | */ |
97 | #ifndef CONFIG_DEBUG_LIST |
97 | #ifndef CONFIG_DEBUG_LIST |
98 | static inline void __list_del_entry(struct list_head *entry) |
98 | static inline void __list_del_entry(struct list_head *entry) |
99 | { |
99 | { |
100 | __list_del(entry->prev, entry->next); |
100 | __list_del(entry->prev, entry->next); |
101 | } |
101 | } |
102 | 102 | ||
103 | static inline void list_del(struct list_head *entry) |
103 | static inline void list_del(struct list_head *entry) |
104 | { |
104 | { |
105 | __list_del(entry->prev, entry->next); |
105 | __list_del(entry->prev, entry->next); |
106 | entry->next = LIST_POISON1; |
106 | entry->next = LIST_POISON1; |
107 | entry->prev = LIST_POISON2; |
107 | entry->prev = LIST_POISON2; |
108 | } |
108 | } |
109 | #else |
109 | #else |
110 | extern void __list_del_entry(struct list_head *entry); |
110 | extern void __list_del_entry(struct list_head *entry); |
111 | extern void list_del(struct list_head *entry); |
111 | extern void list_del(struct list_head *entry); |
112 | #endif |
112 | #endif |
113 | 113 | ||
114 | /** |
114 | /** |
115 | * list_replace - replace old entry by new one |
115 | * list_replace - replace old entry by new one |
116 | * @old : the element to be replaced |
116 | * @old : the element to be replaced |
117 | * @new : the new element to insert |
117 | * @new : the new element to insert |
118 | * |
118 | * |
119 | * If @old was empty, it will be overwritten. |
119 | * If @old was empty, it will be overwritten. |
120 | */ |
120 | */ |
121 | static inline void list_replace(struct list_head *old, |
121 | static inline void list_replace(struct list_head *old, |
122 | struct list_head *new) |
122 | struct list_head *new) |
123 | { |
123 | { |
124 | new->next = old->next; |
124 | new->next = old->next; |
125 | new->next->prev = new; |
125 | new->next->prev = new; |
126 | new->prev = old->prev; |
126 | new->prev = old->prev; |
127 | new->prev->next = new; |
127 | new->prev->next = new; |
128 | } |
128 | } |
129 | 129 | ||
130 | static inline void list_replace_init(struct list_head *old, |
130 | static inline void list_replace_init(struct list_head *old, |
131 | struct list_head *new) |
131 | struct list_head *new) |
132 | { |
132 | { |
133 | list_replace(old, new); |
133 | list_replace(old, new); |
134 | INIT_LIST_HEAD(old); |
134 | INIT_LIST_HEAD(old); |
135 | } |
135 | } |
136 | 136 | ||
137 | /** |
137 | /** |
138 | * list_del_init - deletes entry from list and reinitialize it. |
138 | * list_del_init - deletes entry from list and reinitialize it. |
139 | * @entry: the element to delete from the list. |
139 | * @entry: the element to delete from the list. |
140 | */ |
140 | */ |
141 | static inline void list_del_init(struct list_head *entry) |
141 | static inline void list_del_init(struct list_head *entry) |
142 | { |
142 | { |
143 | __list_del_entry(entry); |
143 | __list_del_entry(entry); |
144 | INIT_LIST_HEAD(entry); |
144 | INIT_LIST_HEAD(entry); |
145 | } |
145 | } |
146 | 146 | ||
147 | /** |
147 | /** |
148 | * list_move - delete from one list and add as another's head |
148 | * list_move - delete from one list and add as another's head |
149 | * @list: the entry to move |
149 | * @list: the entry to move |
150 | * @head: the head that will precede our entry |
150 | * @head: the head that will precede our entry |
151 | */ |
151 | */ |
152 | static inline void list_move(struct list_head *list, struct list_head *head) |
152 | static inline void list_move(struct list_head *list, struct list_head *head) |
153 | { |
153 | { |
154 | __list_del_entry(list); |
154 | __list_del_entry(list); |
155 | list_add(list, head); |
155 | list_add(list, head); |
156 | } |
156 | } |
157 | 157 | ||
158 | /** |
158 | /** |
159 | * list_move_tail - delete from one list and add as another's tail |
159 | * list_move_tail - delete from one list and add as another's tail |
160 | * @list: the entry to move |
160 | * @list: the entry to move |
161 | * @head: the head that will follow our entry |
161 | * @head: the head that will follow our entry |
162 | */ |
162 | */ |
163 | static inline void list_move_tail(struct list_head *list, |
163 | static inline void list_move_tail(struct list_head *list, |
164 | struct list_head *head) |
164 | struct list_head *head) |
165 | { |
165 | { |
166 | __list_del_entry(list); |
166 | __list_del_entry(list); |
167 | list_add_tail(list, head); |
167 | list_add_tail(list, head); |
168 | } |
168 | } |
169 | 169 | ||
170 | /** |
170 | /** |
171 | * list_is_last - tests whether @list is the last entry in list @head |
171 | * list_is_last - tests whether @list is the last entry in list @head |
172 | * @list: the entry to test |
172 | * @list: the entry to test |
173 | * @head: the head of the list |
173 | * @head: the head of the list |
174 | */ |
174 | */ |
175 | static inline int list_is_last(const struct list_head *list, |
175 | static inline int list_is_last(const struct list_head *list, |
176 | const struct list_head *head) |
176 | const struct list_head *head) |
177 | { |
177 | { |
178 | return list->next == head; |
178 | return list->next == head; |
179 | } |
179 | } |
180 | 180 | ||
181 | /** |
181 | /** |
182 | * list_empty - tests whether a list is empty |
182 | * list_empty - tests whether a list is empty |
183 | * @head: the list to test. |
183 | * @head: the list to test. |
184 | */ |
184 | */ |
185 | static inline int list_empty(const struct list_head *head) |
185 | static inline int list_empty(const struct list_head *head) |
186 | { |
186 | { |
187 | return head->next == head; |
187 | return head->next == head; |
188 | } |
188 | } |
189 | 189 | ||
190 | /** |
190 | /** |
191 | * list_empty_careful - tests whether a list is empty and not being modified |
191 | * list_empty_careful - tests whether a list is empty and not being modified |
192 | * @head: the list to test |
192 | * @head: the list to test |
193 | * |
193 | * |
194 | * Description: |
194 | * Description: |
195 | * tests whether a list is empty _and_ checks that no other CPU might be |
195 | * tests whether a list is empty _and_ checks that no other CPU might be |
196 | * in the process of modifying either member (next or prev) |
196 | * in the process of modifying either member (next or prev) |
197 | * |
197 | * |
198 | * NOTE: using list_empty_careful() without synchronization |
198 | * NOTE: using list_empty_careful() without synchronization |
199 | * can only be safe if the only activity that can happen |
199 | * can only be safe if the only activity that can happen |
200 | * to the list entry is list_del_init(). Eg. it cannot be used |
200 | * to the list entry is list_del_init(). Eg. it cannot be used |
201 | * if another CPU could re-list_add() it. |
201 | * if another CPU could re-list_add() it. |
202 | */ |
202 | */ |
203 | static inline int list_empty_careful(const struct list_head *head) |
203 | static inline int list_empty_careful(const struct list_head *head) |
204 | { |
204 | { |
205 | struct list_head *next = head->next; |
205 | struct list_head *next = head->next; |
206 | return (next == head) && (next == head->prev); |
206 | return (next == head) && (next == head->prev); |
207 | } |
207 | } |
208 | 208 | ||
209 | /** |
209 | /** |
210 | * list_rotate_left - rotate the list to the left |
210 | * list_rotate_left - rotate the list to the left |
211 | * @head: the head of the list |
211 | * @head: the head of the list |
212 | */ |
212 | */ |
213 | static inline void list_rotate_left(struct list_head *head) |
213 | static inline void list_rotate_left(struct list_head *head) |
214 | { |
214 | { |
215 | struct list_head *first; |
215 | struct list_head *first; |
216 | 216 | ||
217 | if (!list_empty(head)) { |
217 | if (!list_empty(head)) { |
218 | first = head->next; |
218 | first = head->next; |
219 | list_move_tail(first, head); |
219 | list_move_tail(first, head); |
220 | } |
220 | } |
221 | } |
221 | } |
222 | 222 | ||
223 | /** |
223 | /** |
224 | * list_is_singular - tests whether a list has just one entry. |
224 | * list_is_singular - tests whether a list has just one entry. |
225 | * @head: the list to test. |
225 | * @head: the list to test. |
226 | */ |
226 | */ |
227 | static inline int list_is_singular(const struct list_head *head) |
227 | static inline int list_is_singular(const struct list_head *head) |
228 | { |
228 | { |
229 | return !list_empty(head) && (head->next == head->prev); |
229 | return !list_empty(head) && (head->next == head->prev); |
230 | } |
230 | } |
231 | 231 | ||
232 | static inline void __list_cut_position(struct list_head *list, |
232 | static inline void __list_cut_position(struct list_head *list, |
233 | struct list_head *head, struct list_head *entry) |
233 | struct list_head *head, struct list_head *entry) |
234 | { |
234 | { |
235 | struct list_head *new_first = entry->next; |
235 | struct list_head *new_first = entry->next; |
236 | list->next = head->next; |
236 | list->next = head->next; |
237 | list->next->prev = list; |
237 | list->next->prev = list; |
238 | list->prev = entry; |
238 | list->prev = entry; |
239 | entry->next = list; |
239 | entry->next = list; |
240 | head->next = new_first; |
240 | head->next = new_first; |
241 | new_first->prev = head; |
241 | new_first->prev = head; |
242 | } |
242 | } |
243 | 243 | ||
244 | /** |
244 | /** |
245 | * list_cut_position - cut a list into two |
245 | * list_cut_position - cut a list into two |
246 | * @list: a new list to add all removed entries |
246 | * @list: a new list to add all removed entries |
247 | * @head: a list with entries |
247 | * @head: a list with entries |
248 | * @entry: an entry within head, could be the head itself |
248 | * @entry: an entry within head, could be the head itself |
249 | * and if so we won't cut the list |
249 | * and if so we won't cut the list |
250 | * |
250 | * |
251 | * This helper moves the initial part of @head, up to and |
251 | * This helper moves the initial part of @head, up to and |
252 | * including @entry, from @head to @list. You should |
252 | * including @entry, from @head to @list. You should |
253 | * pass on @entry an element you know is on @head. @list |
253 | * pass on @entry an element you know is on @head. @list |
254 | * should be an empty list or a list you do not care about |
254 | * should be an empty list or a list you do not care about |
255 | * losing its data. |
255 | * losing its data. |
256 | * |
256 | * |
257 | */ |
257 | */ |
258 | static inline void list_cut_position(struct list_head *list, |
258 | static inline void list_cut_position(struct list_head *list, |
259 | struct list_head *head, struct list_head *entry) |
259 | struct list_head *head, struct list_head *entry) |
260 | { |
260 | { |
261 | if (list_empty(head)) |
261 | if (list_empty(head)) |
262 | return; |
262 | return; |
263 | if (list_is_singular(head) && |
263 | if (list_is_singular(head) && |
264 | (head->next != entry && head != entry)) |
264 | (head->next != entry && head != entry)) |
265 | return; |
265 | return; |
266 | if (entry == head) |
266 | if (entry == head) |
267 | INIT_LIST_HEAD(list); |
267 | INIT_LIST_HEAD(list); |
268 | else |
268 | else |
269 | __list_cut_position(list, head, entry); |
269 | __list_cut_position(list, head, entry); |
270 | } |
270 | } |
271 | 271 | ||
272 | static inline void __list_splice(const struct list_head *list, |
272 | static inline void __list_splice(const struct list_head *list, |
273 | struct list_head *prev, |
273 | struct list_head *prev, |
274 | struct list_head *next) |
274 | struct list_head *next) |
275 | { |
275 | { |
276 | struct list_head *first = list->next; |
276 | struct list_head *first = list->next; |
277 | struct list_head *last = list->prev; |
277 | struct list_head *last = list->prev; |
278 | 278 | ||
279 | first->prev = prev; |
279 | first->prev = prev; |
280 | prev->next = first; |
280 | prev->next = first; |
281 | 281 | ||
282 | last->next = next; |
282 | last->next = next; |
283 | next->prev = last; |
283 | next->prev = last; |
284 | } |
284 | } |
285 | 285 | ||
286 | /** |
286 | /** |
287 | * list_splice - join two lists, this is designed for stacks |
287 | * list_splice - join two lists, this is designed for stacks |
288 | * @list: the new list to add. |
288 | * @list: the new list to add. |
289 | * @head: the place to add it in the first list. |
289 | * @head: the place to add it in the first list. |
290 | */ |
290 | */ |
291 | static inline void list_splice(const struct list_head *list, |
291 | static inline void list_splice(const struct list_head *list, |
292 | struct list_head *head) |
292 | struct list_head *head) |
293 | { |
293 | { |
294 | if (!list_empty(list)) |
294 | if (!list_empty(list)) |
295 | __list_splice(list, head, head->next); |
295 | __list_splice(list, head, head->next); |
296 | } |
296 | } |
297 | 297 | ||
298 | /** |
298 | /** |
299 | * list_splice_tail - join two lists, each list being a queue |
299 | * list_splice_tail - join two lists, each list being a queue |
300 | * @list: the new list to add. |
300 | * @list: the new list to add. |
301 | * @head: the place to add it in the first list. |
301 | * @head: the place to add it in the first list. |
302 | */ |
302 | */ |
303 | static inline void list_splice_tail(struct list_head *list, |
303 | static inline void list_splice_tail(struct list_head *list, |
304 | struct list_head *head) |
304 | struct list_head *head) |
305 | { |
305 | { |
306 | if (!list_empty(list)) |
306 | if (!list_empty(list)) |
307 | __list_splice(list, head->prev, head); |
307 | __list_splice(list, head->prev, head); |
308 | } |
308 | } |
309 | 309 | ||
310 | /** |
310 | /** |
311 | * list_splice_init - join two lists and reinitialise the emptied list. |
311 | * list_splice_init - join two lists and reinitialise the emptied list. |
312 | * @list: the new list to add. |
312 | * @list: the new list to add. |
313 | * @head: the place to add it in the first list. |
313 | * @head: the place to add it in the first list. |
314 | * |
314 | * |
315 | * The list at @list is reinitialised |
315 | * The list at @list is reinitialised |
316 | */ |
316 | */ |
317 | static inline void list_splice_init(struct list_head *list, |
317 | static inline void list_splice_init(struct list_head *list, |
318 | struct list_head *head) |
318 | struct list_head *head) |
319 | { |
319 | { |
320 | if (!list_empty(list)) { |
320 | if (!list_empty(list)) { |
321 | __list_splice(list, head, head->next); |
321 | __list_splice(list, head, head->next); |
322 | INIT_LIST_HEAD(list); |
322 | INIT_LIST_HEAD(list); |
323 | } |
323 | } |
324 | } |
324 | } |
325 | 325 | ||
326 | /** |
326 | /** |
327 | * list_splice_tail_init - join two lists and reinitialise the emptied list |
327 | * list_splice_tail_init - join two lists and reinitialise the emptied list |
328 | * @list: the new list to add. |
328 | * @list: the new list to add. |
329 | * @head: the place to add it in the first list. |
329 | * @head: the place to add it in the first list. |
330 | * |
330 | * |
331 | * Each of the lists is a queue. |
331 | * Each of the lists is a queue. |
332 | * The list at @list is reinitialised |
332 | * The list at @list is reinitialised |
333 | */ |
333 | */ |
334 | static inline void list_splice_tail_init(struct list_head *list, |
334 | static inline void list_splice_tail_init(struct list_head *list, |
335 | struct list_head *head) |
335 | struct list_head *head) |
336 | { |
336 | { |
337 | if (!list_empty(list)) { |
337 | if (!list_empty(list)) { |
338 | __list_splice(list, head->prev, head); |
338 | __list_splice(list, head->prev, head); |
339 | INIT_LIST_HEAD(list); |
339 | INIT_LIST_HEAD(list); |
340 | } |
340 | } |
341 | } |
341 | } |
342 | 342 | ||
343 | /** |
343 | /** |
344 | * list_entry - get the struct for this entry |
344 | * list_entry - get the struct for this entry |
345 | * @ptr: the &struct list_head pointer. |
345 | * @ptr: the &struct list_head pointer. |
346 | * @type: the type of the struct this is embedded in. |
346 | * @type: the type of the struct this is embedded in. |
347 | * @member: the name of the list_struct within the struct. |
347 | * @member: the name of the list_struct within the struct. |
348 | */ |
348 | */ |
349 | #define list_entry(ptr, type, member) \ |
349 | #define list_entry(ptr, type, member) \ |
350 | container_of(ptr, type, member) |
350 | container_of(ptr, type, member) |
351 | 351 | ||
352 | /** |
352 | /** |
353 | * list_first_entry - get the first element from a list |
353 | * list_first_entry - get the first element from a list |
354 | * @ptr: the list head to take the element from. |
354 | * @ptr: the list head to take the element from. |
355 | * @type: the type of the struct this is embedded in. |
355 | * @type: the type of the struct this is embedded in. |
356 | * @member: the name of the list_struct within the struct. |
356 | * @member: the name of the list_struct within the struct. |
357 | * |
357 | * |
358 | * Note, that list is expected to be not empty. |
358 | * Note, that list is expected to be not empty. |
359 | */ |
359 | */ |
360 | #define list_first_entry(ptr, type, member) \ |
360 | #define list_first_entry(ptr, type, member) \ |
361 | list_entry((ptr)->next, type, member) |
361 | list_entry((ptr)->next, type, member) |
362 | 362 | ||
363 | /** |
363 | /** |
- | 364 | * list_last_entry - get the last element from a list |
|
- | 365 | * @ptr: the list head to take the element from. |
|
- | 366 | * @type: the type of the struct this is embedded in. |
|
- | 367 | * @member: the name of the list_struct within the struct. |
|
- | 368 | * |
|
- | 369 | * Note, that list is expected to be not empty. |
|
- | 370 | */ |
|
- | 371 | #define list_last_entry(ptr, type, member) \ |
|
- | 372 | list_entry((ptr)->prev, type, member) |
|
- | 373 | ||
- | 374 | /** |
|
364 | * list_first_entry_or_null - get the first element from a list |
375 | * list_first_entry_or_null - get the first element from a list |
365 | * @ptr: the list head to take the element from. |
376 | * @ptr: the list head to take the element from. |
366 | * @type: the type of the struct this is embedded in. |
377 | * @type: the type of the struct this is embedded in. |
367 | * @member: the name of the list_struct within the struct. |
378 | * @member: the name of the list_struct within the struct. |
368 | * |
379 | * |
369 | * Note that if the list is empty, it returns NULL. |
380 | * Note that if the list is empty, it returns NULL. |
370 | */ |
381 | */ |
371 | #define list_first_entry_or_null(ptr, type, member) \ |
382 | #define list_first_entry_or_null(ptr, type, member) \ |
372 | (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL) |
383 | (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL) |
373 | 384 | ||
374 | /** |
385 | /** |
- | 386 | * list_next_entry - get the next element in list |
|
- | 387 | * @pos: the type * to cursor |
|
- | 388 | * @member: the name of the list_struct within the struct. |
|
- | 389 | */ |
|
- | 390 | #define list_next_entry(pos, member) \ |
|
- | 391 | list_entry((pos)->member.next, typeof(*(pos)), member) |
|
- | 392 | ||
- | 393 | /** |
|
- | 394 | * list_prev_entry - get the prev element in list |
|
- | 395 | * @pos: the type * to cursor |
|
- | 396 | * @member: the name of the list_struct within the struct. |
|
- | 397 | */ |
|
- | 398 | #define list_prev_entry(pos, member) \ |
|
- | 399 | list_entry((pos)->member.prev, typeof(*(pos)), member) |
|
- | 400 | ||
- | 401 | /** |
|
375 | * list_for_each - iterate over a list |
402 | * list_for_each - iterate over a list |
376 | * @pos: the &struct list_head to use as a loop cursor. |
403 | * @pos: the &struct list_head to use as a loop cursor. |
377 | * @head: the head for your list. |
404 | * @head: the head for your list. |
378 | */ |
405 | */ |
379 | #define list_for_each(pos, head) \ |
406 | #define list_for_each(pos, head) \ |
380 | for (pos = (head)->next; pos != (head); pos = pos->next) |
407 | for (pos = (head)->next; pos != (head); pos = pos->next) |
381 | 408 | ||
382 | /** |
409 | /** |
383 | * list_for_each_prev - iterate over a list backwards |
410 | * list_for_each_prev - iterate over a list backwards |
384 | * @pos: the &struct list_head to use as a loop cursor. |
411 | * @pos: the &struct list_head to use as a loop cursor. |
385 | * @head: the head for your list. |
412 | * @head: the head for your list. |
386 | */ |
413 | */ |
387 | #define list_for_each_prev(pos, head) \ |
414 | #define list_for_each_prev(pos, head) \ |
388 | for (pos = (head)->prev; pos != (head); pos = pos->prev) |
415 | for (pos = (head)->prev; pos != (head); pos = pos->prev) |
389 | 416 | ||
390 | /** |
417 | /** |
391 | * list_for_each_safe - iterate over a list safe against removal of list entry |
418 | * list_for_each_safe - iterate over a list safe against removal of list entry |
392 | * @pos: the &struct list_head to use as a loop cursor. |
419 | * @pos: the &struct list_head to use as a loop cursor. |
393 | * @n: another &struct list_head to use as temporary storage |
420 | * @n: another &struct list_head to use as temporary storage |
394 | * @head: the head for your list. |
421 | * @head: the head for your list. |
395 | */ |
422 | */ |
396 | #define list_for_each_safe(pos, n, head) \ |
423 | #define list_for_each_safe(pos, n, head) \ |
397 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
424 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
398 | pos = n, n = pos->next) |
425 | pos = n, n = pos->next) |
399 | 426 | ||
400 | /** |
427 | /** |
401 | * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry |
428 | * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry |
402 | * @pos: the &struct list_head to use as a loop cursor. |
429 | * @pos: the &struct list_head to use as a loop cursor. |
403 | * @n: another &struct list_head to use as temporary storage |
430 | * @n: another &struct list_head to use as temporary storage |
404 | * @head: the head for your list. |
431 | * @head: the head for your list. |
405 | */ |
432 | */ |
406 | #define list_for_each_prev_safe(pos, n, head) \ |
433 | #define list_for_each_prev_safe(pos, n, head) \ |
407 | for (pos = (head)->prev, n = pos->prev; \ |
434 | for (pos = (head)->prev, n = pos->prev; \ |
408 | pos != (head); \ |
435 | pos != (head); \ |
409 | pos = n, n = pos->prev) |
436 | pos = n, n = pos->prev) |
410 | 437 | ||
411 | /** |
438 | /** |
412 | * list_for_each_entry - iterate over list of given type |
439 | * list_for_each_entry - iterate over list of given type |
413 | * @pos: the type * to use as a loop cursor. |
440 | * @pos: the type * to use as a loop cursor. |
414 | * @head: the head for your list. |
441 | * @head: the head for your list. |
415 | * @member: the name of the list_struct within the struct. |
442 | * @member: the name of the list_struct within the struct. |
416 | */ |
443 | */ |
417 | #define list_for_each_entry(pos, head, member) \ |
444 | #define list_for_each_entry(pos, head, member) \ |
418 | for (pos = list_entry((head)->next, typeof(*pos), member); \ |
445 | for (pos = list_first_entry(head, typeof(*pos), member); \ |
419 | &pos->member != (head); \ |
446 | &pos->member != (head); \ |
420 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
447 | pos = list_next_entry(pos, member)) |
421 | 448 | ||
422 | /** |
449 | /** |
423 | * list_for_each_entry_reverse - iterate backwards over list of given type. |
450 | * list_for_each_entry_reverse - iterate backwards over list of given type. |
424 | * @pos: the type * to use as a loop cursor. |
451 | * @pos: the type * to use as a loop cursor. |
425 | * @head: the head for your list. |
452 | * @head: the head for your list. |
426 | * @member: the name of the list_struct within the struct. |
453 | * @member: the name of the list_struct within the struct. |
427 | */ |
454 | */ |
428 | #define list_for_each_entry_reverse(pos, head, member) \ |
455 | #define list_for_each_entry_reverse(pos, head, member) \ |
429 | for (pos = list_entry((head)->prev, typeof(*pos), member); \ |
456 | for (pos = list_last_entry(head, typeof(*pos), member); \ |
430 | &pos->member != (head); \ |
457 | &pos->member != (head); \ |
431 | pos = list_entry(pos->member.prev, typeof(*pos), member)) |
458 | pos = list_prev_entry(pos, member)) |
432 | 459 | ||
433 | /** |
460 | /** |
434 | * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() |
461 | * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() |
435 | * @pos: the type * to use as a start point |
462 | * @pos: the type * to use as a start point |
436 | * @head: the head of the list |
463 | * @head: the head of the list |
437 | * @member: the name of the list_struct within the struct. |
464 | * @member: the name of the list_struct within the struct. |
438 | * |
465 | * |
439 | * Prepares a pos entry for use as a start point in list_for_each_entry_continue(). |
466 | * Prepares a pos entry for use as a start point in list_for_each_entry_continue(). |
440 | */ |
467 | */ |
441 | #define list_prepare_entry(pos, head, member) \ |
468 | #define list_prepare_entry(pos, head, member) \ |
442 | ((pos) ? : list_entry(head, typeof(*pos), member)) |
469 | ((pos) ? : list_entry(head, typeof(*pos), member)) |
443 | 470 | ||
444 | /** |
471 | /** |
445 | * list_for_each_entry_continue - continue iteration over list of given type |
472 | * list_for_each_entry_continue - continue iteration over list of given type |
446 | * @pos: the type * to use as a loop cursor. |
473 | * @pos: the type * to use as a loop cursor. |
447 | * @head: the head for your list. |
474 | * @head: the head for your list. |
448 | * @member: the name of the list_struct within the struct. |
475 | * @member: the name of the list_struct within the struct. |
449 | * |
476 | * |
450 | * Continue to iterate over list of given type, continuing after |
477 | * Continue to iterate over list of given type, continuing after |
451 | * the current position. |
478 | * the current position. |
452 | */ |
479 | */ |
453 | #define list_for_each_entry_continue(pos, head, member) \ |
480 | #define list_for_each_entry_continue(pos, head, member) \ |
454 | for (pos = list_entry(pos->member.next, typeof(*pos), member); \ |
481 | for (pos = list_next_entry(pos, member); \ |
455 | &pos->member != (head); \ |
482 | &pos->member != (head); \ |
456 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
483 | pos = list_next_entry(pos, member)) |
457 | 484 | ||
458 | /** |
485 | /** |
459 | * list_for_each_entry_continue_reverse - iterate backwards from the given point |
486 | * list_for_each_entry_continue_reverse - iterate backwards from the given point |
460 | * @pos: the type * to use as a loop cursor. |
487 | * @pos: the type * to use as a loop cursor. |
461 | * @head: the head for your list. |
488 | * @head: the head for your list. |
462 | * @member: the name of the list_struct within the struct. |
489 | * @member: the name of the list_struct within the struct. |
463 | * |
490 | * |
464 | * Start to iterate over list of given type backwards, continuing after |
491 | * Start to iterate over list of given type backwards, continuing after |
465 | * the current position. |
492 | * the current position. |
466 | */ |
493 | */ |
467 | #define list_for_each_entry_continue_reverse(pos, head, member) \ |
494 | #define list_for_each_entry_continue_reverse(pos, head, member) \ |
468 | for (pos = list_entry(pos->member.prev, typeof(*pos), member); \ |
495 | for (pos = list_prev_entry(pos, member); \ |
469 | &pos->member != (head); \ |
496 | &pos->member != (head); \ |
470 | pos = list_entry(pos->member.prev, typeof(*pos), member)) |
497 | pos = list_prev_entry(pos, member)) |
471 | 498 | ||
472 | /** |
499 | /** |
473 | * list_for_each_entry_from - iterate over list of given type from the current point |
500 | * list_for_each_entry_from - iterate over list of given type from the current point |
474 | * @pos: the type * to use as a loop cursor. |
501 | * @pos: the type * to use as a loop cursor. |
475 | * @head: the head for your list. |
502 | * @head: the head for your list. |
476 | * @member: the name of the list_struct within the struct. |
503 | * @member: the name of the list_struct within the struct. |
477 | * |
504 | * |
478 | * Iterate over list of given type, continuing from current position. |
505 | * Iterate over list of given type, continuing from current position. |
479 | */ |
506 | */ |
480 | #define list_for_each_entry_from(pos, head, member) \ |
507 | #define list_for_each_entry_from(pos, head, member) \ |
481 | for (; &pos->member != (head); \ |
508 | for (; &pos->member != (head); \ |
482 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
509 | pos = list_next_entry(pos, member)) |
483 | 510 | ||
484 | /** |
511 | /** |
485 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
512 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
486 | * @pos: the type * to use as a loop cursor. |
513 | * @pos: the type * to use as a loop cursor. |
487 | * @n: another type * to use as temporary storage |
514 | * @n: another type * to use as temporary storage |
488 | * @head: the head for your list. |
515 | * @head: the head for your list. |
489 | * @member: the name of the list_struct within the struct. |
516 | * @member: the name of the list_struct within the struct. |
490 | */ |
517 | */ |
491 | #define list_for_each_entry_safe(pos, n, head, member) \ |
518 | #define list_for_each_entry_safe(pos, n, head, member) \ |
492 | for (pos = list_entry((head)->next, typeof(*pos), member), \ |
519 | for (pos = list_first_entry(head, typeof(*pos), member), \ |
493 | n = list_entry(pos->member.next, typeof(*pos), member); \ |
520 | n = list_next_entry(pos, member); \ |
494 | &pos->member != (head); \ |
521 | &pos->member != (head); \ |
495 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
522 | pos = n, n = list_next_entry(n, member)) |
496 | 523 | ||
497 | /** |
524 | /** |
498 | * list_for_each_entry_safe_continue - continue list iteration safe against removal |
525 | * list_for_each_entry_safe_continue - continue list iteration safe against removal |
499 | * @pos: the type * to use as a loop cursor. |
526 | * @pos: the type * to use as a loop cursor. |
500 | * @n: another type * to use as temporary storage |
527 | * @n: another type * to use as temporary storage |
501 | * @head: the head for your list. |
528 | * @head: the head for your list. |
502 | * @member: the name of the list_struct within the struct. |
529 | * @member: the name of the list_struct within the struct. |
503 | * |
530 | * |
504 | * Iterate over list of given type, continuing after current point, |
531 | * Iterate over list of given type, continuing after current point, |
505 | * safe against removal of list entry. |
532 | * safe against removal of list entry. |
506 | */ |
533 | */ |
507 | #define list_for_each_entry_safe_continue(pos, n, head, member) \ |
534 | #define list_for_each_entry_safe_continue(pos, n, head, member) \ |
508 | for (pos = list_entry(pos->member.next, typeof(*pos), member), \ |
535 | for (pos = list_next_entry(pos, member), \ |
509 | n = list_entry(pos->member.next, typeof(*pos), member); \ |
536 | n = list_next_entry(pos, member); \ |
510 | &pos->member != (head); \ |
537 | &pos->member != (head); \ |
511 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
538 | pos = n, n = list_next_entry(n, member)) |
512 | 539 | ||
513 | /** |
540 | /** |
514 | * list_for_each_entry_safe_from - iterate over list from current point safe against removal |
541 | * list_for_each_entry_safe_from - iterate over list from current point safe against removal |
515 | * @pos: the type * to use as a loop cursor. |
542 | * @pos: the type * to use as a loop cursor. |
516 | * @n: another type * to use as temporary storage |
543 | * @n: another type * to use as temporary storage |
517 | * @head: the head for your list. |
544 | * @head: the head for your list. |
518 | * @member: the name of the list_struct within the struct. |
545 | * @member: the name of the list_struct within the struct. |
519 | * |
546 | * |
520 | * Iterate over list of given type from current point, safe against |
547 | * Iterate over list of given type from current point, safe against |
521 | * removal of list entry. |
548 | * removal of list entry. |
522 | */ |
549 | */ |
523 | #define list_for_each_entry_safe_from(pos, n, head, member) \ |
550 | #define list_for_each_entry_safe_from(pos, n, head, member) \ |
524 | for (n = list_entry(pos->member.next, typeof(*pos), member); \ |
551 | for (n = list_next_entry(pos, member); \ |
525 | &pos->member != (head); \ |
552 | &pos->member != (head); \ |
526 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
553 | pos = n, n = list_next_entry(n, member)) |
527 | 554 | ||
528 | /** |
555 | /** |
529 | * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal |
556 | * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal |
530 | * @pos: the type * to use as a loop cursor. |
557 | * @pos: the type * to use as a loop cursor. |
531 | * @n: another type * to use as temporary storage |
558 | * @n: another type * to use as temporary storage |
532 | * @head: the head for your list. |
559 | * @head: the head for your list. |
533 | * @member: the name of the list_struct within the struct. |
560 | * @member: the name of the list_struct within the struct. |
534 | * |
561 | * |
535 | * Iterate backwards over list of given type, safe against removal |
562 | * Iterate backwards over list of given type, safe against removal |
536 | * of list entry. |
563 | * of list entry. |
537 | */ |
564 | */ |
538 | #define list_for_each_entry_safe_reverse(pos, n, head, member) \ |
565 | #define list_for_each_entry_safe_reverse(pos, n, head, member) \ |
539 | for (pos = list_entry((head)->prev, typeof(*pos), member), \ |
566 | for (pos = list_last_entry(head, typeof(*pos), member), \ |
540 | n = list_entry(pos->member.prev, typeof(*pos), member); \ |
567 | n = list_prev_entry(pos, member); \ |
541 | &pos->member != (head); \ |
568 | &pos->member != (head); \ |
542 | pos = n, n = list_entry(n->member.prev, typeof(*n), member)) |
569 | pos = n, n = list_prev_entry(n, member)) |
543 | 570 | ||
544 | /** |
571 | /** |
545 | * list_safe_reset_next - reset a stale list_for_each_entry_safe loop |
572 | * list_safe_reset_next - reset a stale list_for_each_entry_safe loop |
546 | * @pos: the loop cursor used in the list_for_each_entry_safe loop |
573 | * @pos: the loop cursor used in the list_for_each_entry_safe loop |
547 | * @n: temporary storage used in list_for_each_entry_safe |
574 | * @n: temporary storage used in list_for_each_entry_safe |
548 | * @member: the name of the list_struct within the struct. |
575 | * @member: the name of the list_struct within the struct. |
549 | * |
576 | * |
550 | * list_safe_reset_next is not safe to use in general if the list may be |
577 | * list_safe_reset_next is not safe to use in general if the list may be |
551 | * modified concurrently (eg. the lock is dropped in the loop body). An |
578 | * modified concurrently (eg. the lock is dropped in the loop body). An |
552 | * exception to this is if the cursor element (pos) is pinned in the list, |
579 | * exception to this is if the cursor element (pos) is pinned in the list, |
553 | * and list_safe_reset_next is called after re-taking the lock and before |
580 | * and list_safe_reset_next is called after re-taking the lock and before |
554 | * completing the current iteration of the loop body. |
581 | * completing the current iteration of the loop body. |
555 | */ |
582 | */ |
556 | #define list_safe_reset_next(pos, n, member) \ |
583 | #define list_safe_reset_next(pos, n, member) \ |
557 | n = list_entry(pos->member.next, typeof(*pos), member) |
584 | n = list_next_entry(pos, member) |
558 | 585 | ||
559 | /* |
586 | /* |
560 | * Double linked lists with a single pointer list head. |
587 | * Double linked lists with a single pointer list head. |
561 | * Mostly useful for hash tables where the two pointer list head is |
588 | * Mostly useful for hash tables where the two pointer list head is |
562 | * too wasteful. |
589 | * too wasteful. |
563 | * You lose the ability to access the tail in O(1). |
590 | * You lose the ability to access the tail in O(1). |
564 | */ |
591 | */ |
565 | 592 | ||
566 | #define HLIST_HEAD_INIT { .first = NULL } |
593 | #define HLIST_HEAD_INIT { .first = NULL } |
567 | #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } |
594 | #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } |
568 | #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) |
595 | #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) |
569 | static inline void INIT_HLIST_NODE(struct hlist_node *h) |
596 | static inline void INIT_HLIST_NODE(struct hlist_node *h) |
570 | { |
597 | { |
571 | h->next = NULL; |
598 | h->next = NULL; |
572 | h->pprev = NULL; |
599 | h->pprev = NULL; |
573 | } |
600 | } |
574 | 601 | ||
575 | static inline int hlist_unhashed(const struct hlist_node *h) |
602 | static inline int hlist_unhashed(const struct hlist_node *h) |
576 | { |
603 | { |
577 | return !h->pprev; |
604 | return !h->pprev; |
578 | } |
605 | } |
579 | 606 | ||
580 | static inline int hlist_empty(const struct hlist_head *h) |
607 | static inline int hlist_empty(const struct hlist_head *h) |
581 | { |
608 | { |
582 | return !h->first; |
609 | return !h->first; |
583 | } |
610 | } |
584 | 611 | ||
585 | static inline void __hlist_del(struct hlist_node *n) |
612 | static inline void __hlist_del(struct hlist_node *n) |
586 | { |
613 | { |
587 | struct hlist_node *next = n->next; |
614 | struct hlist_node *next = n->next; |
588 | struct hlist_node **pprev = n->pprev; |
615 | struct hlist_node **pprev = n->pprev; |
589 | *pprev = next; |
616 | *pprev = next; |
590 | if (next) |
617 | if (next) |
591 | next->pprev = pprev; |
618 | next->pprev = pprev; |
592 | } |
619 | } |
593 | 620 | ||
594 | static inline void hlist_del(struct hlist_node *n) |
621 | static inline void hlist_del(struct hlist_node *n) |
595 | { |
622 | { |
596 | __hlist_del(n); |
623 | __hlist_del(n); |
597 | n->next = LIST_POISON1; |
624 | n->next = LIST_POISON1; |
598 | n->pprev = LIST_POISON2; |
625 | n->pprev = LIST_POISON2; |
599 | } |
626 | } |
600 | 627 | ||
601 | static inline void hlist_del_init(struct hlist_node *n) |
628 | static inline void hlist_del_init(struct hlist_node *n) |
602 | { |
629 | { |
603 | if (!hlist_unhashed(n)) { |
630 | if (!hlist_unhashed(n)) { |
604 | __hlist_del(n); |
631 | __hlist_del(n); |
605 | INIT_HLIST_NODE(n); |
632 | INIT_HLIST_NODE(n); |
606 | } |
633 | } |
607 | } |
634 | } |
608 | 635 | ||
609 | static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) |
636 | static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) |
610 | { |
637 | { |
611 | struct hlist_node *first = h->first; |
638 | struct hlist_node *first = h->first; |
612 | n->next = first; |
639 | n->next = first; |
613 | if (first) |
640 | if (first) |
614 | first->pprev = &n->next; |
641 | first->pprev = &n->next; |
615 | h->first = n; |
642 | h->first = n; |
616 | n->pprev = &h->first; |
643 | n->pprev = &h->first; |
617 | } |
644 | } |
618 | 645 | ||
619 | /* next must be != NULL */ |
646 | /* next must be != NULL */ |
620 | static inline void hlist_add_before(struct hlist_node *n, |
647 | static inline void hlist_add_before(struct hlist_node *n, |
621 | struct hlist_node *next) |
648 | struct hlist_node *next) |
622 | { |
649 | { |
623 | n->pprev = next->pprev; |
650 | n->pprev = next->pprev; |
624 | n->next = next; |
651 | n->next = next; |
625 | next->pprev = &n->next; |
652 | next->pprev = &n->next; |
626 | *(n->pprev) = n; |
653 | *(n->pprev) = n; |
627 | } |
654 | } |
628 | 655 | ||
629 | static inline void hlist_add_after(struct hlist_node *n, |
656 | static inline void hlist_add_behind(struct hlist_node *n, |
630 | struct hlist_node *next) |
657 | struct hlist_node *prev) |
631 | { |
658 | { |
632 | next->next = n->next; |
659 | n->next = prev->next; |
633 | n->next = next; |
660 | prev->next = n; |
634 | next->pprev = &n->next; |
661 | n->pprev = &prev->next; |
635 | 662 | ||
636 | if(next->next) |
663 | if (n->next) |
637 | next->next->pprev = &next->next; |
664 | n->next->pprev = &n->next; |
638 | } |
665 | } |
639 | 666 | ||
640 | /* after that we'll appear to be on some hlist and hlist_del will work */ |
667 | /* after that we'll appear to be on some hlist and hlist_del will work */ |
641 | static inline void hlist_add_fake(struct hlist_node *n) |
668 | static inline void hlist_add_fake(struct hlist_node *n) |
642 | { |
669 | { |
643 | n->pprev = &n->next; |
670 | n->pprev = &n->next; |
644 | } |
671 | } |
645 | 672 | ||
646 | /* |
673 | /* |
647 | * Move a list from one list head to another. Fixup the pprev |
674 | * Move a list from one list head to another. Fixup the pprev |
648 | * reference of the first entry if it exists. |
675 | * reference of the first entry if it exists. |
649 | */ |
676 | */ |
650 | static inline void hlist_move_list(struct hlist_head *old, |
677 | static inline void hlist_move_list(struct hlist_head *old, |
651 | struct hlist_head *new) |
678 | struct hlist_head *new) |
652 | { |
679 | { |
653 | new->first = old->first; |
680 | new->first = old->first; |
654 | if (new->first) |
681 | if (new->first) |
655 | new->first->pprev = &new->first; |
682 | new->first->pprev = &new->first; |
656 | old->first = NULL; |
683 | old->first = NULL; |
657 | } |
684 | } |
658 | 685 | ||
659 | #define hlist_entry(ptr, type, member) container_of(ptr,type,member) |
686 | #define hlist_entry(ptr, type, member) container_of(ptr,type,member) |
660 | 687 | ||
661 | #define hlist_for_each(pos, head) \ |
688 | #define hlist_for_each(pos, head) \ |
662 | for (pos = (head)->first; pos ; pos = pos->next) |
689 | for (pos = (head)->first; pos ; pos = pos->next) |
663 | 690 | ||
664 | #define hlist_for_each_safe(pos, n, head) \ |
691 | #define hlist_for_each_safe(pos, n, head) \ |
665 | for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ |
692 | for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ |
666 | pos = n) |
693 | pos = n) |
667 | 694 | ||
668 | #define hlist_entry_safe(ptr, type, member) \ |
695 | #define hlist_entry_safe(ptr, type, member) \ |
669 | ({ typeof(ptr) ____ptr = (ptr); \ |
696 | ({ typeof(ptr) ____ptr = (ptr); \ |
670 | ____ptr ? hlist_entry(____ptr, type, member) : NULL; \ |
697 | ____ptr ? hlist_entry(____ptr, type, member) : NULL; \ |
671 | }) |
698 | }) |
672 | 699 | ||
673 | /** |
700 | /** |
674 | * hlist_for_each_entry - iterate over list of given type |
701 | * hlist_for_each_entry - iterate over list of given type |
675 | * @pos: the type * to use as a loop cursor. |
702 | * @pos: the type * to use as a loop cursor. |
676 | * @head: the head for your list. |
703 | * @head: the head for your list. |
677 | * @member: the name of the hlist_node within the struct. |
704 | * @member: the name of the hlist_node within the struct. |
678 | */ |
705 | */ |
679 | #define hlist_for_each_entry(pos, head, member) \ |
706 | #define hlist_for_each_entry(pos, head, member) \ |
680 | for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ |
707 | for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ |
681 | pos; \ |
708 | pos; \ |
682 | pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) |
709 | pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) |
683 | 710 | ||
684 | /** |
711 | /** |
685 | * hlist_for_each_entry_continue - iterate over a hlist continuing after current point |
712 | * hlist_for_each_entry_continue - iterate over a hlist continuing after current point |
686 | * @pos: the type * to use as a loop cursor. |
713 | * @pos: the type * to use as a loop cursor. |
687 | * @member: the name of the hlist_node within the struct. |
714 | * @member: the name of the hlist_node within the struct. |
688 | */ |
715 | */ |
689 | #define hlist_for_each_entry_continue(pos, member) \ |
716 | #define hlist_for_each_entry_continue(pos, member) \ |
690 | for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\ |
717 | for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\ |
691 | pos; \ |
718 | pos; \ |
692 | pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) |
719 | pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) |
693 | 720 | ||
694 | /** |
721 | /** |
695 | * hlist_for_each_entry_from - iterate over a hlist continuing from current point |
722 | * hlist_for_each_entry_from - iterate over a hlist continuing from current point |
696 | * @pos: the type * to use as a loop cursor. |
723 | * @pos: the type * to use as a loop cursor. |
697 | * @member: the name of the hlist_node within the struct. |
724 | * @member: the name of the hlist_node within the struct. |
698 | */ |
725 | */ |
699 | #define hlist_for_each_entry_from(pos, member) \ |
726 | #define hlist_for_each_entry_from(pos, member) \ |
700 | for (; pos; \ |
727 | for (; pos; \ |
701 | pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) |
728 | pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) |
702 | 729 | ||
703 | /** |
730 | /** |
704 | * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
731 | * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
705 | * @pos: the type * to use as a loop cursor. |
732 | * @pos: the type * to use as a loop cursor. |
706 | * @n: another &struct hlist_node to use as temporary storage |
733 | * @n: another &struct hlist_node to use as temporary storage |
707 | * @head: the head for your list. |
734 | * @head: the head for your list. |
708 | * @member: the name of the hlist_node within the struct. |
735 | * @member: the name of the hlist_node within the struct. |
709 | */ |
736 | */ |
710 | #define hlist_for_each_entry_safe(pos, n, head, member) \ |
737 | #define hlist_for_each_entry_safe(pos, n, head, member) \ |
711 | for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\ |
738 | for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\ |
712 | pos && ({ n = pos->member.next; 1; }); \ |
739 | pos && ({ n = pos->member.next; 1; }); \ |
713 | pos = hlist_entry_safe(n, typeof(*pos), member)) |
740 | pos = hlist_entry_safe(n, typeof(*pos), member)) |
714 | 741 | ||
715 | #endif |
742 | #endif |