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