http://prdownloads.sourceforge.net/lufs/lufs-0.9.6.tar.gz?download
[lufs.git] / lufsd / list.h
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
3
4 /*
5  * Simple doubly linked list implementation.
6  *
7  * Some of the internal functions ("__xxx") are useful when
8  * manipulating whole lists rather than single entries, as
9  * sometimes we already know the next/prev entries and we can
10  * generate better code by using them directly rather than
11  * using the generic single-entry routines.
12  */
13
14 struct list_head {
15         struct list_head *next, *prev;
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 #define INIT_LIST_HEAD(ptr) do { \
24         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
25 } while (0)
26
27 /*
28  * Insert a new entry between two known consecutive entries. 
29  *
30  * This is only for internal list manipulation where we know
31  * the prev/next entries already!
32  */
33 static __inline__ void __list_add(struct list_head * nnew,
34         struct list_head * prev,
35         struct list_head * next)
36 {
37         next->prev = nnew;
38         nnew->next = next;
39         nnew->prev = prev;
40         prev->next = nnew;
41 }
42
43 /**
44  * list_add - add a new entry
45  * @new: new entry to be added
46  * @head: list head to add it after
47  *
48  * Insert a new entry after the specified head.
49  * This is good for implementing stacks.
50  */
51 static __inline__ void list_add(struct list_head *nnew, struct list_head *head)
52 {
53         __list_add(nnew, head, head->next);
54 }
55
56 /**
57  * list_add_tail - add a new entry
58  * @nnew: new entry to be added
59  * @head: list head to add it before
60  *
61  * Insert a new entry before the specified head.
62  * This is useful for implementing queues.
63  */
64 static __inline__ void list_add_tail(struct list_head *nnew, struct list_head *head)
65 {
66         __list_add(nnew, head->prev, head);
67 }
68
69 /*
70  * Delete a list entry by making the prev/next entries
71  * point to each other.
72  *
73  * This is only for internal list manipulation where we know
74  * the prev/next entries already!
75  */
76 static __inline__ void __list_del(struct list_head * prev,
77                                   struct list_head * next)
78 {
79         next->prev = prev;
80         prev->next = next;
81 }
82
83 /**
84  * list_del - deletes entry from list.
85  * @entry: the element to delete from the list.
86  * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
87  */
88 static __inline__ void list_del(struct list_head *entry)
89 {
90         __list_del(entry->prev, entry->next);
91 }
92
93 /**
94  * list_del_init - deletes entry from list and reinitialize it.
95  * @entry: the element to delete from the list.
96  */
97 static __inline__ void list_del_init(struct list_head *entry)
98 {
99         __list_del(entry->prev, entry->next);
100         INIT_LIST_HEAD(entry); 
101 }
102
103 /**
104  * list_empty - tests whether a list is empty
105  * @head: the list to test.
106  */
107 static __inline__ int list_empty(struct list_head *head)
108 {
109         return head->next == head;
110 }
111
112 /**
113  * list_splice - join two lists
114  * @list: the new list to add.
115  * @head: the place to add it in the first list.
116  */
117 static __inline__ void list_splice(struct list_head *list, struct list_head *head)
118 {
119         struct list_head *first = list->next;
120
121         if (first != list) {
122                 struct list_head *last = list->prev;
123                 struct list_head *at = head->next;
124
125                 first->prev = head;
126                 head->next = first;
127
128                 last->next = at;
129                 at->prev = last;
130         }
131 }
132
133 /**
134  * list_entry - get the struct for this entry
135  * @ptr:        the &struct list_head pointer.
136  * @type:       the type of the struct this is embedded in.
137  * @member:     the name of the list_struct within the struct.
138  */
139 #define list_entry(ptr, type, member) \
140         ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
141
142 /**
143  * list_for_each        -       iterate over a list
144  * @pos:        the &struct list_head to use as a loop counter.
145  * @head:       the head for your list.
146  */
147 #define list_for_each(pos, head) \
148         for (pos = (head)->next; pos != (head); \
149                 pos = pos->next)
150                 
151 /**
152  * list_for_each_safe   -       iterate over a list safe against removal of list entry
153  * @pos:        the &struct list_head to use as a loop counter.
154  * @n:          another &struct list_head to use as temporary storage
155  * @head:       the head for your list.
156  */
157 #define list_for_each_safe(pos, n, head) \
158         for (pos = (head)->next, n = pos->next; pos != (head); \
159                 pos = n, n = pos->next)
160
161 /**
162  * list_for_each_prev   -       iterate over a list in reverse order
163  * @pos:        the &struct list_head to use as a loop counter.
164  * @head:       the head for your list.
165  */
166 #define list_for_each_prev(pos, head) \
167         for (pos = (head)->prev; pos != (head); \
168                 pos = pos->prev)
169                 
170
171 #endif