mfd: add platform_data to mfd_cell
[deliverable/linux.git] / include / linux / list.h
CommitLineData
1da177e4
LT
1#ifndef _LINUX_LIST_H
2#define _LINUX_LIST_H
3
1da177e4 4#include <linux/stddef.h>
c9cf5528 5#include <linux/poison.h>
1da177e4
LT
6#include <linux/prefetch.h>
7#include <asm/system.h>
8
1da177e4
LT
9/*
10 * Simple doubly linked list implementation.
11 *
12 * Some of the internal functions ("__xxx") are useful when
13 * manipulating whole lists rather than single entries, as
14 * sometimes we already know the next/prev entries and we can
15 * generate better code by using them directly rather than
16 * using the generic single-entry routines.
17 */
18
19struct list_head {
20 struct list_head *next, *prev;
21};
22
23#define LIST_HEAD_INIT(name) { &(name), &(name) }
24
25#define LIST_HEAD(name) \
26 struct list_head name = LIST_HEAD_INIT(name)
27
490d6ab1
ZB
28static inline void INIT_LIST_HEAD(struct list_head *list)
29{
30 list->next = list;
31 list->prev = list;
32}
1da177e4
LT
33
34/*
35 * Insert a new entry between two known consecutive entries.
36 *
37 * This is only for internal list manipulation where we know
38 * the prev/next entries already!
39 */
199a9afc 40#ifndef CONFIG_DEBUG_LIST
1da177e4
LT
41static inline void __list_add(struct list_head *new,
42 struct list_head *prev,
43 struct list_head *next)
44{
45 next->prev = new;
46 new->next = next;
47 new->prev = prev;
48 prev->next = new;
49}
199a9afc
DJ
50#else
51extern void __list_add(struct list_head *new,
52 struct list_head *prev,
53 struct list_head *next);
54#endif
1da177e4
LT
55
56/**
57 * list_add - add a new entry
58 * @new: new entry to be added
59 * @head: list head to add it after
60 *
61 * Insert a new entry after the specified head.
62 * This is good for implementing stacks.
63 */
64static inline void list_add(struct list_head *new, struct list_head *head)
65{
66 __list_add(new, head, head->next);
67}
199a9afc 68
1da177e4
LT
69
70/**
71 * list_add_tail - add a new entry
72 * @new: new entry to be added
73 * @head: list head to add it before
74 *
75 * Insert a new entry before the specified head.
76 * This is useful for implementing queues.
77 */
78static inline void list_add_tail(struct list_head *new, struct list_head *head)
79{
80 __list_add(new, head->prev, head);
81}
82
1da177e4
LT
83/*
84 * Delete a list entry by making the prev/next entries
85 * point to each other.
86 *
87 * This is only for internal list manipulation where we know
88 * the prev/next entries already!
89 */
90static inline void __list_del(struct list_head * prev, struct list_head * next)
91{
92 next->prev = prev;
93 prev->next = next;
94}
95
96/**
97 * list_del - deletes entry from list.
98 * @entry: the element to delete from the list.
72fd4a35 99 * Note: list_empty() on entry does not return true after this, the entry is
1da177e4
LT
100 * in an undefined state.
101 */
199a9afc 102#ifndef CONFIG_DEBUG_LIST
1da177e4
LT
103static 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}
199a9afc
DJ
109#else
110extern void list_del(struct list_head *entry);
111#endif
1da177e4 112
54e73770
ON
113/**
114 * list_replace - replace old entry by new one
115 * @old : the element to be replaced
116 * @new : the new element to insert
72fd4a35
RD
117 *
118 * If @old was empty, it will be overwritten.
54e73770
ON
119 */
120static inline void list_replace(struct list_head *old,
121 struct list_head *new)
122{
123 new->next = old->next;
124 new->next->prev = new;
125 new->prev = old->prev;
126 new->prev->next = new;
127}
128
129static inline void list_replace_init(struct list_head *old,
130 struct list_head *new)
131{
132 list_replace(old, new);
133 INIT_LIST_HEAD(old);
134}
135
1da177e4
LT
136/**
137 * list_del_init - deletes entry from list and reinitialize it.
138 * @entry: the element to delete from the list.
139 */
140static inline void list_del_init(struct list_head *entry)
141{
142 __list_del(entry->prev, entry->next);
143 INIT_LIST_HEAD(entry);
144}
145
146/**
147 * list_move - delete from one list and add as another's head
148 * @list: the entry to move
149 * @head: the head that will precede our entry
150 */
151static inline void list_move(struct list_head *list, struct list_head *head)
152{
78db2ad6
DW
153 __list_del(list->prev, list->next);
154 list_add(list, head);
1da177e4
LT
155}
156
157/**
158 * list_move_tail - delete from one list and add as another's tail
159 * @list: the entry to move
160 * @head: the head that will follow our entry
161 */
162static inline void list_move_tail(struct list_head *list,
163 struct list_head *head)
164{
78db2ad6
DW
165 __list_del(list->prev, list->next);
166 list_add_tail(list, head);
1da177e4
LT
167}
168
e8f4d97e
SN
169/**
170 * list_is_last - tests whether @list is the last entry in list @head
171 * @list: the entry to test
172 * @head: the head of the list
173 */
174static inline int list_is_last(const struct list_head *list,
175 const struct list_head *head)
176{
177 return list->next == head;
178}
179
1da177e4
LT
180/**
181 * list_empty - tests whether a list is empty
182 * @head: the list to test.
183 */
184static inline int list_empty(const struct list_head *head)
185{
186 return head->next == head;
187}
188
189/**
fe96e57d
RD
190 * list_empty_careful - tests whether a list is empty and not being modified
191 * @head: the list to test
192 *
193 * Description:
194 * tests whether a list is empty _and_ checks that no other CPU might be
195 * in the process of modifying either member (next or prev)
1da177e4
LT
196 *
197 * NOTE: using list_empty_careful() without synchronization
198 * can only be safe if the only activity that can happen
199 * to the list entry is list_del_init(). Eg. it cannot be used
200 * if another CPU could re-list_add() it.
1da177e4
LT
201 */
202static inline int list_empty_careful(const struct list_head *head)
203{
204 struct list_head *next = head->next;
205 return (next == head) && (next == head->prev);
99602572
MH
206}
207
208/**
209 * list_is_singular - tests whether a list has just one entry.
210 * @head: the list to test.
211 */
212static inline int list_is_singular(const struct list_head *head)
213{
214 return !list_empty(head) && (head->next == head->prev);
1da177e4
LT
215}
216
95d8c365 217static inline void __list_splice(const struct list_head *list,
1da177e4
LT
218 struct list_head *head)
219{
220 struct list_head *first = list->next;
221 struct list_head *last = list->prev;
222 struct list_head *at = head->next;
223
224 first->prev = head;
225 head->next = first;
226
227 last->next = at;
228 at->prev = last;
229}
230
231/**
232 * list_splice - join two lists
233 * @list: the new list to add.
234 * @head: the place to add it in the first list.
235 */
95d8c365
RD
236static inline void list_splice(const struct list_head *list,
237 struct list_head *head)
1da177e4
LT
238{
239 if (!list_empty(list))
240 __list_splice(list, head);
241}
242
243/**
244 * list_splice_init - join two lists and reinitialise the emptied list.
245 * @list: the new list to add.
246 * @head: the place to add it in the first list.
247 *
248 * The list at @list is reinitialised
249 */
250static inline void list_splice_init(struct list_head *list,
251 struct list_head *head)
252{
253 if (!list_empty(list)) {
254 __list_splice(list, head);
255 INIT_LIST_HEAD(list);
256 }
257}
258
259/**
260 * list_entry - get the struct for this entry
261 * @ptr: the &struct list_head pointer.
262 * @type: the type of the struct this is embedded in.
263 * @member: the name of the list_struct within the struct.
264 */
265#define list_entry(ptr, type, member) \
266 container_of(ptr, type, member)
267
b5e61818
PE
268/**
269 * list_first_entry - get the first element from a list
270 * @ptr: the list head to take the element from.
271 * @type: the type of the struct this is embedded in.
272 * @member: the name of the list_struct within the struct.
273 *
274 * Note, that list is expected to be not empty.
275 */
276#define list_first_entry(ptr, type, member) \
277 list_entry((ptr)->next, type, member)
278
1da177e4
LT
279/**
280 * list_for_each - iterate over a list
8e3a67a9 281 * @pos: the &struct list_head to use as a loop cursor.
1da177e4
LT
282 * @head: the head for your list.
283 */
284#define list_for_each(pos, head) \
285 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
286 pos = pos->next)
287
288/**
289 * __list_for_each - iterate over a list
8e3a67a9 290 * @pos: the &struct list_head to use as a loop cursor.
1da177e4
LT
291 * @head: the head for your list.
292 *
293 * This variant differs from list_for_each() in that it's the
294 * simplest possible list iteration code, no prefetching is done.
295 * Use this for code that knows the list to be very short (empty
296 * or 1 entry) most of the time.
297 */
298#define __list_for_each(pos, head) \
299 for (pos = (head)->next; pos != (head); pos = pos->next)
300
301/**
302 * list_for_each_prev - iterate over a list backwards
8e3a67a9 303 * @pos: the &struct list_head to use as a loop cursor.
1da177e4
LT
304 * @head: the head for your list.
305 */
306#define list_for_each_prev(pos, head) \
307 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
308 pos = pos->prev)
309
310/**
fe96e57d 311 * list_for_each_safe - iterate over a list safe against removal of list entry
8e3a67a9 312 * @pos: the &struct list_head to use as a loop cursor.
1da177e4
LT
313 * @n: another &struct list_head to use as temporary storage
314 * @head: the head for your list.
315 */
316#define list_for_each_safe(pos, n, head) \
317 for (pos = (head)->next, n = pos->next; pos != (head); \
318 pos = n, n = pos->next)
319
37c42524 320/**
8f731f7d 321 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
37c42524
DL
322 * @pos: the &struct list_head to use as a loop cursor.
323 * @n: another &struct list_head to use as temporary storage
324 * @head: the head for your list.
325 */
326#define list_for_each_prev_safe(pos, n, head) \
327 for (pos = (head)->prev, n = pos->prev; \
328 prefetch(pos->prev), pos != (head); \
329 pos = n, n = pos->prev)
330
1da177e4
LT
331/**
332 * list_for_each_entry - iterate over list of given type
8e3a67a9 333 * @pos: the type * to use as a loop cursor.
1da177e4
LT
334 * @head: the head for your list.
335 * @member: the name of the list_struct within the struct.
336 */
337#define list_for_each_entry(pos, head, member) \
338 for (pos = list_entry((head)->next, typeof(*pos), member); \
339 prefetch(pos->member.next), &pos->member != (head); \
340 pos = list_entry(pos->member.next, typeof(*pos), member))
341
342/**
343 * list_for_each_entry_reverse - iterate backwards over list of given type.
8e3a67a9 344 * @pos: the type * to use as a loop cursor.
1da177e4
LT
345 * @head: the head for your list.
346 * @member: the name of the list_struct within the struct.
347 */
348#define list_for_each_entry_reverse(pos, head, member) \
349 for (pos = list_entry((head)->prev, typeof(*pos), member); \
350 prefetch(pos->member.prev), &pos->member != (head); \
351 pos = list_entry(pos->member.prev, typeof(*pos), member))
352
353/**
72fd4a35 354 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1da177e4
LT
355 * @pos: the type * to use as a start point
356 * @head: the head of the list
357 * @member: the name of the list_struct within the struct.
fe96e57d 358 *
72fd4a35 359 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1da177e4
LT
360 */
361#define list_prepare_entry(pos, head, member) \
362 ((pos) ? : list_entry(head, typeof(*pos), member))
363
364/**
fe96e57d 365 * list_for_each_entry_continue - continue iteration over list of given type
8e3a67a9 366 * @pos: the type * to use as a loop cursor.
1da177e4
LT
367 * @head: the head for your list.
368 * @member: the name of the list_struct within the struct.
fe96e57d
RD
369 *
370 * Continue to iterate over list of given type, continuing after
371 * the current position.
1da177e4
LT
372 */
373#define list_for_each_entry_continue(pos, head, member) \
374 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
375 prefetch(pos->member.next), &pos->member != (head); \
376 pos = list_entry(pos->member.next, typeof(*pos), member))
377
768f3591
PE
378/**
379 * list_for_each_entry_continue_reverse - iterate backwards from the given point
380 * @pos: the type * to use as a loop cursor.
381 * @head: the head for your list.
382 * @member: the name of the list_struct within the struct.
383 *
384 * Start to iterate over list of given type backwards, continuing after
385 * the current position.
386 */
387#define list_for_each_entry_continue_reverse(pos, head, member) \
388 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
389 prefetch(pos->member.prev), &pos->member != (head); \
390 pos = list_entry(pos->member.prev, typeof(*pos), member))
391
e229c2fb 392/**
fe96e57d 393 * list_for_each_entry_from - iterate over list of given type from the current point
8e3a67a9 394 * @pos: the type * to use as a loop cursor.
e229c2fb
ACM
395 * @head: the head for your list.
396 * @member: the name of the list_struct within the struct.
fe96e57d
RD
397 *
398 * Iterate over list of given type, continuing from current position.
e229c2fb
ACM
399 */
400#define list_for_each_entry_from(pos, head, member) \
401 for (; prefetch(pos->member.next), &pos->member != (head); \
402 pos = list_entry(pos->member.next, typeof(*pos), member))
403
1da177e4
LT
404/**
405 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
8e3a67a9 406 * @pos: the type * to use as a loop cursor.
1da177e4
LT
407 * @n: another type * to use as temporary storage
408 * @head: the head for your list.
409 * @member: the name of the list_struct within the struct.
410 */
411#define list_for_each_entry_safe(pos, n, head, member) \
412 for (pos = list_entry((head)->next, typeof(*pos), member), \
413 n = list_entry(pos->member.next, typeof(*pos), member); \
414 &pos->member != (head); \
415 pos = n, n = list_entry(n->member.next, typeof(*n), member))
416
74459dc7 417/**
fe96e57d 418 * list_for_each_entry_safe_continue
8e3a67a9 419 * @pos: the type * to use as a loop cursor.
74459dc7
ACM
420 * @n: another type * to use as temporary storage
421 * @head: the head for your list.
422 * @member: the name of the list_struct within the struct.
fe96e57d
RD
423 *
424 * Iterate over list of given type, continuing after current point,
425 * safe against removal of list entry.
74459dc7
ACM
426 */
427#define list_for_each_entry_safe_continue(pos, n, head, member) \
8c60f3fa
ACM
428 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
429 n = list_entry(pos->member.next, typeof(*pos), member); \
d8dcffee
ACM
430 &pos->member != (head); \
431 pos = n, n = list_entry(n->member.next, typeof(*n), member))
432
433/**
fe96e57d 434 * list_for_each_entry_safe_from
8e3a67a9 435 * @pos: the type * to use as a loop cursor.
d8dcffee
ACM
436 * @n: another type * to use as temporary storage
437 * @head: the head for your list.
438 * @member: the name of the list_struct within the struct.
fe96e57d
RD
439 *
440 * Iterate over list of given type from current point, safe against
441 * removal of list entry.
d8dcffee
ACM
442 */
443#define list_for_each_entry_safe_from(pos, n, head, member) \
444 for (n = list_entry(pos->member.next, typeof(*pos), member); \
74459dc7
ACM
445 &pos->member != (head); \
446 pos = n, n = list_entry(n->member.next, typeof(*n), member))
447
0ad42352 448/**
fe96e57d 449 * list_for_each_entry_safe_reverse
8e3a67a9 450 * @pos: the type * to use as a loop cursor.
0ad42352
DH
451 * @n: another type * to use as temporary storage
452 * @head: the head for your list.
453 * @member: the name of the list_struct within the struct.
fe96e57d
RD
454 *
455 * Iterate backwards over list of given type, safe against removal
456 * of list entry.
0ad42352
DH
457 */
458#define list_for_each_entry_safe_reverse(pos, n, head, member) \
459 for (pos = list_entry((head)->prev, typeof(*pos), member), \
460 n = list_entry(pos->member.prev, typeof(*pos), member); \
461 &pos->member != (head); \
462 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
463
1da177e4
LT
464/*
465 * Double linked lists with a single pointer list head.
466 * Mostly useful for hash tables where the two pointer list head is
467 * too wasteful.
468 * You lose the ability to access the tail in O(1).
469 */
470
471struct hlist_head {
472 struct hlist_node *first;
473};
474
475struct hlist_node {
476 struct hlist_node *next, **pprev;
477};
478
479#define HLIST_HEAD_INIT { .first = NULL }
480#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
481#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
490d6ab1
ZB
482static inline void INIT_HLIST_NODE(struct hlist_node *h)
483{
484 h->next = NULL;
485 h->pprev = NULL;
486}
1da177e4
LT
487
488static inline int hlist_unhashed(const struct hlist_node *h)
489{
490 return !h->pprev;
491}
492
493static inline int hlist_empty(const struct hlist_head *h)
494{
495 return !h->first;
496}
497
498static inline void __hlist_del(struct hlist_node *n)
499{
500 struct hlist_node *next = n->next;
501 struct hlist_node **pprev = n->pprev;
502 *pprev = next;
503 if (next)
504 next->pprev = pprev;
505}
506
507static inline void hlist_del(struct hlist_node *n)
508{
509 __hlist_del(n);
510 n->next = LIST_POISON1;
511 n->pprev = LIST_POISON2;
512}
513
1da177e4
LT
514static inline void hlist_del_init(struct hlist_node *n)
515{
da753bea 516 if (!hlist_unhashed(n)) {
1da177e4
LT
517 __hlist_del(n);
518 INIT_HLIST_NODE(n);
519 }
520}
521
522static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
523{
524 struct hlist_node *first = h->first;
525 n->next = first;
526 if (first)
527 first->pprev = &n->next;
528 h->first = n;
529 n->pprev = &h->first;
530}
531
1da177e4
LT
532/* next must be != NULL */
533static inline void hlist_add_before(struct hlist_node *n,
534 struct hlist_node *next)
535{
536 n->pprev = next->pprev;
537 n->next = next;
538 next->pprev = &n->next;
539 *(n->pprev) = n;
540}
541
542static inline void hlist_add_after(struct hlist_node *n,
543 struct hlist_node *next)
544{
545 next->next = n->next;
546 n->next = next;
547 next->pprev = &n->next;
548
549 if(next->next)
550 next->next->pprev = &next->next;
551}
552
553#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
554
555#define hlist_for_each(pos, head) \
556 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
557 pos = pos->next)
558
559#define hlist_for_each_safe(pos, n, head) \
560 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
561 pos = n)
562
1da177e4
LT
563/**
564 * hlist_for_each_entry - iterate over list of given type
8e3a67a9
RD
565 * @tpos: the type * to use as a loop cursor.
566 * @pos: the &struct hlist_node to use as a loop cursor.
1da177e4
LT
567 * @head: the head for your list.
568 * @member: the name of the hlist_node within the struct.
569 */
570#define hlist_for_each_entry(tpos, pos, head, member) \
571 for (pos = (head)->first; \
572 pos && ({ prefetch(pos->next); 1;}) && \
573 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
574 pos = pos->next)
575
576/**
fe96e57d 577 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
8e3a67a9
RD
578 * @tpos: the type * to use as a loop cursor.
579 * @pos: the &struct hlist_node to use as a loop cursor.
1da177e4
LT
580 * @member: the name of the hlist_node within the struct.
581 */
582#define hlist_for_each_entry_continue(tpos, pos, member) \
583 for (pos = (pos)->next; \
584 pos && ({ prefetch(pos->next); 1;}) && \
585 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
586 pos = pos->next)
587
588/**
fe96e57d 589 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
8e3a67a9
RD
590 * @tpos: the type * to use as a loop cursor.
591 * @pos: the &struct hlist_node to use as a loop cursor.
1da177e4
LT
592 * @member: the name of the hlist_node within the struct.
593 */
594#define hlist_for_each_entry_from(tpos, pos, member) \
595 for (; pos && ({ prefetch(pos->next); 1;}) && \
596 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
597 pos = pos->next)
598
599/**
600 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
8e3a67a9
RD
601 * @tpos: the type * to use as a loop cursor.
602 * @pos: the &struct hlist_node to use as a loop cursor.
1da177e4
LT
603 * @n: another &struct hlist_node to use as temporary storage
604 * @head: the head for your list.
605 * @member: the name of the hlist_node within the struct.
606 */
607#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
608 for (pos = (head)->first; \
609 pos && ({ n = pos->next; 1; }) && \
610 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
611 pos = n)
612
1da177e4 613#endif
This page took 0.747047 seconds and 5 git commands to generate.