1
/*
2
 * Stolen from Linux 2.6.7
3
 *
4
 * This program is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU General Public License as
6
 * published by the Free Software Foundation; version 2 of the
7
 * License.
8
 *
9
 * This program is distributed in the hope that it will be useful, but
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
#ifndef _LINUX_LIST_H
19
#define _LINUX_LIST_H
20
21
#include "compiler.h" /* container_of */
22
23
static inline void prefetch(const void *x)
24
{
25
}
26
27
/*
28
 * These are non-NULL pointers that will result in page faults
29
 * under normal circumstances, used to verify that nobody uses
30
 * non-initialized list entries.
31
 */
32
#define LIST_POISON1  ((void *) 0x00100100)
33
#define LIST_POISON2  ((void *) 0x00200200)
34
35
/*
36
 * Simple doubly linked list implementation.
37
 *
38
 * Some of the internal functions ("__xxx") are useful when
39
 * manipulating whole lists rather than single entries, as
40
 * sometimes we already know the next/prev entries and we can
41
 * generate better code by using them directly rather than
42
 * using the generic single-entry routines.
43
 */
44
45
struct list_head {
46
	struct list_head *next, *prev;
47
};
48
49
#define LIST_HEAD_INIT(name) { &(name), &(name) }
50
51
#define LIST_HEAD(name) \
52
	struct list_head name = LIST_HEAD_INIT(name)
53
54
static inline void list_init(struct list_head *head)
55
{
56
	head->next = head;
57
	head->prev = head;
58
}
59
60
/*
61
 * Insert a new entry between two known consecutive entries.
62
 *
63
 * This is only for internal list manipulation where we know
64
 * the prev/next entries already!
65
 */
66
static inline void __list_add(struct list_head *new,
67
			      struct list_head *prev,
68
			      struct list_head *next)
69
{
70
	next->prev = new;
71
	new->next = next;
72
	new->prev = prev;
73
	prev->next = new;
74
}
75
76
/**
77
 * list_add - add a new entry
78
 * @new: new entry to be added
79
 * @head: list head to add it after
80
 *
81
 * Insert a new entry after the specified head.
82
 * This is good for implementing stacks.
83
 */
84
static inline void list_add(struct list_head *new, struct list_head *head)
85
{
86
	__list_add(new, head, head->next);
87
}
88
89
/**
90
 * list_add_tail - add a new entry
91
 * @new: new entry to be added
92
 * @head: list head to add it before
93
 *
94
 * Insert a new entry before the specified head.
95
 * This is useful for implementing queues.
96
 */
97
static inline void list_add_tail(struct list_head *new, struct list_head *head)
98
{
99
	__list_add(new, head->prev, head);
100
}
101
102
/*
103
 * Delete a list entry by making the prev/next entries
104
 * point to each other.
105
 *
106
 * This is only for internal list manipulation where we know
107
 * the prev/next entries already!
108
 */
109
static inline void __list_del(struct list_head *prev, struct list_head *next)
110
{
111
	next->prev = prev;
112
	prev->next = next;
113
}
114
115
/**
116
 * list_del - deletes entry from list.
117
 * @entry: the element to delete from the list.
118
 * Note: list_empty on entry does not return true after this, the entry is
119
 * in an undefined state.
120
 */
121
static inline void list_del(struct list_head *entry)
122
{
123
	__list_del(entry->prev, entry->next);
124
	entry->next = LIST_POISON1;
125
	entry->prev = LIST_POISON2;
126
}
127
128
/**
129
 * list_del_init - deletes entry from list and reinitialize it.
130
 * @entry: the element to delete from the list.
131
 */
132
static inline void list_del_init(struct list_head *entry)
133
{
134
	__list_del(entry->prev, entry->next);
135
	list_init(entry);
136
}
137
138
/**
139
 * list_move - delete from one list and add as another's head
140
 * @list: the entry to move
141
 * @head: the head that will precede our entry
142
 */
143
static inline void list_move(struct list_head *list, struct list_head *head)
144
{
145
	__list_del(list->prev, list->next);
146
	list_add(list, head);
147
}
148
149
/**
150
 * list_move_tail - delete from one list and add as another's tail
151
 * @list: the entry to move
152
 * @head: the head that will follow our entry
153
 */
154
static inline void list_move_tail(struct list_head *list,
155
				  struct list_head *head)
156
{
157
	__list_del(list->prev, list->next);
158
	list_add_tail(list, head);
159
}
160
161
/**
162
 * list_empty - tests whether a list is empty
163
 * @head: the list to test.
164
 */
165
static inline int list_empty(const struct list_head *head)
166
{
167
	return head->next == head;
168
}
169
170
static inline void __list_splice(struct list_head *list,
171
				 struct list_head *head)
172
{
173
	struct list_head *first = list->next;
174
	struct list_head *last = list->prev;
175
	struct list_head *at = head->next;
176
177
	first->prev = head;
178
	head->next = first;
179
180
	last->next = at;
181
	at->prev = last;
182
}
183
184
/**
185
 * list_splice - join two lists
186
 * @list: the new list to add.
187
 * @head: the place to add it in the first list.
188
 */
189
static inline void list_splice(struct list_head *list, struct list_head *head)
190
{
191
	if (!list_empty(list))
192
		__list_splice(list, head);
193
}
194
195
/**
196
 * list_splice_init - join two lists and reinitialise the emptied list.
197
 * @list: the new list to add.
198
 * @head: the place to add it in the first list.
199
 *
200
 * The list at @list is reinitialised
201
 */
202
static inline void list_splice_init(struct list_head *list,
203
				    struct list_head *head)
204
{
205
	if (!list_empty(list)) {
206
		__list_splice(list, head);
207
		list_init(list);
208
	}
209
}
210
211
/**
212
 * list_entry - get the struct for this entry
213
 * @ptr:	the &struct list_head pointer.
214
 * @type:	the type of the struct this is embedded in.
215
 * @member:	the name of the list_struct within the struct.
216
 */
217
#define list_entry(ptr, type, member) \
218
	container_of(ptr, type, member)
219
220
/**
221
 * list_for_each	-	iterate over a list
222
 * @pos:	the &struct list_head to use as a loop counter.
223
 * @head:	the head for your list.
224
 */
225
#define list_for_each(pos, head) \
226
	for (pos = (head)->next, prefetch(pos->next); pos != (head); \
227
		pos = pos->next, prefetch(pos->next))
228
229
/**
230
 * __list_for_each	-	iterate over a list
231
 * @pos:	the &struct list_head to use as a loop counter.
232
 * @head:	the head for your list.
233
 *
234
 * This variant differs from list_for_each() in that it's the
235
 * simplest possible list iteration code, no prefetching is done.
236
 * Use this for code that knows the list to be very short (empty
237
 * or 1 entry) most of the time.
238
 */
239
#define __list_for_each(pos, head) \
240
	for (pos = (head)->next; pos != (head); pos = pos->next)
241
242
/**
243
 * list_for_each_prev	-	iterate over a list backwards
244
 * @pos:	the &struct list_head to use as a loop counter.
245
 * @head:	the head for your list.
246
 */
247
#define list_for_each_prev(pos, head) \
248
	for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
249
		pos = pos->prev, prefetch(pos->prev))
250
251
/**
252
 * list_for_each_safe	-	iterate over a list safe against removal of list entry
253
 * @pos:	the &struct list_head to use as a loop counter.
254
 * @n:		another &struct list_head to use as temporary storage
255
 * @head:	the head for your list.
256
 */
257
#define list_for_each_safe(pos, n, head) \
258
	for (pos = (head)->next, n = pos->next; pos != (head); \
259
		pos = n, n = pos->next)
260
261
/**
262
 * list_for_each_entry	-	iterate over list of given type
263
 * @pos:	the type * to use as a loop counter.
264
 * @head:	the head for your list.
265
 * @member:	the name of the list_struct within the struct.
266
 */
267
#define list_for_each_entry(pos, head, member)				\
268
	for (pos = list_entry((head)->next, __typeof__(*pos), member),	\
269
		     prefetch(pos->member.next);			\
270
	     &pos->member != (head); 					\
271
	     pos = list_entry(pos->member.next, __typeof__(*pos), member),	\
272
		     prefetch(pos->member.next))
273
274
/**
275
 * list_for_each_entry_reverse - iterate backwards over list of given type.
276
 * @pos:	the type * to use as a loop counter.
277
 * @head:	the head for your list.
278
 * @member:	the name of the list_struct within the struct.
279
 */
280
#define list_for_each_entry_reverse(pos, head, member)			\
281
	for (pos = list_entry((head)->prev, __typeof__(*pos), member),	\
282
		     prefetch(pos->member.prev);			\
283
	     &pos->member != (head); 					\
284
	     pos = list_entry(pos->member.prev, __typeof__(*pos), member),	\
285
		     prefetch(pos->member.prev))
286
287
/**
288
 * list_prepare_entry - prepare a pos entry for use as a start point in
289
 *			list_for_each_entry_continue
290
 * @pos:	the type * to use as a start point
291
 * @head:	the head of the list
292
 * @member:	the name of the list_struct within the struct.
293
 */
294
#define list_prepare_entry(pos, head, member) \
295
	((pos) ? : list_entry(head, __typeof__(*pos), member))
296
297
/**
298
 * list_for_each_entry_continue -	iterate over list of given type
299
 *			continuing after existing point
300
 * @pos:	the type * to use as a loop counter.
301
 * @head:	the head for your list.
302
 * @member:	the name of the list_struct within the struct.
303
 */
304
#define list_for_each_entry_continue(pos, head, member) 		\
305
	for (pos = list_entry(pos->member.next, __typeof__(*pos), member),	\
306
		     prefetch(pos->member.next);			\
307
	     &pos->member != (head);					\
308
	     pos = list_entry(pos->member.next, __typeof__(*pos), member),	\
309
		     prefetch(pos->member.next))
310
311
/**
312
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
313
 * @pos:	the type * to use as a loop counter.
314
 * @n:		another type * to use as temporary storage
315
 * @head:	the head for your list.
316
 * @member:	the name of the list_struct within the struct.
317
 */
318
#define list_for_each_entry_safe(pos, n, head, member)			\
319
	for (pos = list_entry((head)->next, __typeof__(*pos), member),	\
320
		n = list_entry(pos->member.next, __typeof__(*pos), member);	\
321
	     &pos->member != (head); 					\
322
	     pos = n, n = list_entry(n->member.next, __typeof__(*n), member))
323
324
#endif