ef8fdd29f55d58127bccbe955c76954e9ae0d25b
[reactos.git] / reactos / drivers / usb / cromwell / linux / list.h
1 #ifndef _BOOT_LIST_H
2 #define _BOOT_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 *new,
34 struct list_head *prev,
35 struct list_head *next)
36 {
37 next->prev = new;
38 new->next = next;
39 new->prev = prev;
40 prev->next = new;
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 *new, struct list_head *head)
52 {
53 __list_add(new, head, head->next);
54 }
55
56 /**
57 * list_add_tail - add a new entry
58 * @new: 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 *new, struct list_head *head)
65 {
66 __list_add(new, 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, struct list_head *next)
77 {
78 next->prev = prev;
79 prev->next = next;
80 }
81
82 /**
83 * list_del - deletes entry from list.
84 * @entry: the element to delete from the list.
85 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
86 */
87 static inline void list_del(struct list_head *entry)
88 {
89 __list_del(entry->prev, entry->next);
90 entry->next = (void *) 0;
91 entry->prev = (void *) 0;
92 }
93
94 /**
95 * list_del_init - deletes entry from list and reinitialize it.
96 * @entry: the element to delete from the list.
97 */
98 static inline void list_del_init(struct list_head *entry)
99 {
100 __list_del(entry->prev, entry->next);
101 INIT_LIST_HEAD(entry);
102 }
103
104 /**
105 * list_move - delete from one list and add as another's head
106 * @list: the entry to move
107 * @head: the head that will precede our entry
108 */
109 static inline void list_move(struct list_head *list, struct list_head *head)
110 {
111 __list_del(list->prev, list->next);
112 list_add(list, head);
113 }
114
115 /**
116 * list_move_tail - delete from one list and add as another's tail
117 * @list: the entry to move
118 * @head: the head that will follow our entry
119 */
120 static inline void list_move_tail(struct list_head *list,
121 struct list_head *head)
122 {
123 __list_del(list->prev, list->next);
124 list_add_tail(list, head);
125 }
126
127 /**
128 * list_empty - tests whether a list is empty
129 * @head: the list to test.
130 */
131 static inline int list_empty(struct list_head *head)
132 {
133 return head->next == head;
134 }
135
136 static inline void __list_splice(struct list_head *list,
137 struct list_head *head)
138 {
139 struct list_head *first = list->next;
140 struct list_head *last = list->prev;
141 struct list_head *at = head->next;
142
143 first->prev = head;
144 head->next = first;
145
146 last->next = at;
147 at->prev = last;
148 }
149
150 /**
151 * list_splice - join two lists
152 * @list: the new list to add.
153 * @head: the place to add it in the first list.
154 */
155 static inline void list_splice(struct list_head *list, struct list_head *head)
156 {
157 if (!list_empty(list))
158 __list_splice(list, head);
159 }
160
161 /**
162 * list_splice_init - join two lists and reinitialise the emptied list.
163 * @list: the new list to add.
164 * @head: the place to add it in the first list.
165 *
166 * The list at @list is reinitialised
167 */
168 static inline void list_splice_init(struct list_head *list,
169 struct list_head *head)
170 {
171 if (!list_empty(list)) {
172 __list_splice(list, head);
173 INIT_LIST_HEAD(list);
174 }
175 }
176
177 /**
178 * list_entry - get the struct for this entry
179 * @ptr: the &struct list_head pointer.
180 * @type: the type of the struct this is embedded in.
181 * @member: the name of the list_struct within the struct.
182 */
183 #define list_entry(ptr, type, member) \
184 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
185
186 /**
187 * list_for_each - iterate over a list
188 * @pos: the &struct list_head to use as a loop counter.
189 * @head: the head for your list.
190 */
191 #define list_for_each(pos, head) \
192 for (pos = (head)->next; pos != (head); \
193 pos = pos->next)
194 /**
195 * list_for_each_prev - iterate over a list backwards
196 * @pos: the &struct list_head to use as a loop counter.
197 * @head: the head for your list.
198 */
199 #define list_for_each_prev(pos, head) \
200 for (pos = (head)->prev; pos != (head); \
201 pos = pos->prev)
202
203 /**
204 * list_for_each_safe - iterate over a list safe against removal of list entry
205 * @pos: the &struct list_head to use as a loop counter.
206 * @n: another &struct list_head to use as temporary storage
207 * @head: the head for your list.
208 */
209 #define list_for_each_safe(pos, n, head) \
210 for (pos = (head)->next, n = pos->next; pos != (head); \
211 pos = n, n = pos->next)
212
213 /**
214 * list_for_each_entry - iterate over list of given type
215 * @pos: the type * to use as a loop counter.
216 * @head: the head for your list.
217 * @member: the name of the list_struct within the struct.
218 */
219 #define list_for_each_entry(pos, head, member) \
220 for (pos = list_entry((head)->next, typeof(*pos), member) \
221 &pos->member != (head); \
222 pos = list_entry(pos->member.next, typeof(*pos), member))
223
224 #endif