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