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