1// This list structure implementation is adapted from the list implementation
2// on the Linux kernel.
3
4// Original source:
5// http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.25.y.git;a=blob_plain;f=include/linux/list.h;hb=HEAD
6
7#ifndef _LINUX_LIST_H
8#define _LINUX_LIST_H
9
10/*
11 * Simple doubly linked list implementation.
12 *
13 * Some of the internal functions ("__xxx") are useful when
14 * manipulating whole lists rather than single entries, as
15 * sometimes we already know the next/prev entries and we can
16 * generate better code by using them directly rather than
17 * using the generic single-entry routines.
18 */
19
20#include <stdlib.h>
21#include <stddef.h>
22
23struct list_head {
24	struct list_head *next, *prev;
25};
26
27#define LIST_HEAD_INIT(name) { &(name), &(name) }
28
29#define LIST_HEAD(name) \
30	struct list_head name = LIST_HEAD_INIT(name)
31
32static inline void INIT_LIST_HEAD(struct list_head *list)
33{
34	list->next = list;
35	list->prev = list;
36}
37
38/*
39 * Insert a new entry between two known consecutive entries.
40 *
41 * This is only for internal list manipulation where we know
42 * the prev/next entries already!
43 */
44static inline void __list_add(struct list_head *new,
45			      struct list_head *prev,
46			      struct list_head *next)
47{
48	next->prev = new;
49	new->next = next;
50	new->prev = prev;
51	prev->next = new;
52}
53
54/**
55 * list_add - add a new entry
56 * @new: new entry to be added
57 * @head: list head to add it after
58 *
59 * Insert a new entry after the specified head.
60 * This is good for implementing stacks.
61 */
62static inline void list_add(struct list_head *new, struct list_head *head)
63{
64	__list_add(new, head, head->next);
65}
66
67
68
69/**
70 * list_add_tail - add a new entry
71 * @new: new entry to be added
72 * @head: list head to add it before
73 *
74 * Insert a new entry before the specified head.
75 * This is useful for implementing queues.
76 */
77static inline void list_add_tail(struct list_head *new, struct list_head *head)
78{
79	__list_add(new, head->prev, head);
80}
81
82
83/*
84 * Delete a list entry by making the prev/next entries
85 * point to each other.
86 *
87 * This is only for internal list manipulation where we know
88 * the prev/next entries already!
89 */
90static inline void __list_del(struct list_head * prev, struct list_head * next)
91{
92	next->prev = prev;
93	prev->next = next;
94}
95
96/**
97 * list_del - deletes entry from list.
98 * @entry: the element to delete from the list.
99 * Note: list_empty() on entry does not return true after this, the entry is
100 * in an undefined state.
101 */
102static inline void list_del(struct list_head *entry)
103{
104	__list_del(entry->prev, entry->next);
105	entry->next = NULL;
106	entry->prev = NULL;
107}
108
109/**
110 * list_replace - replace old entry by new one
111 * @old : the element to be replaced
112 * @new : the new element to insert
113 *
114 * If @old was empty, it will be overwritten.
115 */
116static inline void list_replace(struct list_head *old,
117				struct list_head *new)
118{
119	new->next = old->next;
120	new->next->prev = new;
121	new->prev = old->prev;
122	new->prev->next = new;
123}
124
125static inline void list_replace_init(struct list_head *old,
126					struct list_head *new)
127{
128	list_replace(old, new);
129	INIT_LIST_HEAD(old);
130}
131
132/**
133 * list_del_init - deletes entry from list and reinitialize it.
134 * @entry: the element to delete from the list.
135 */
136static inline void list_del_init(struct list_head *entry)
137{
138	__list_del(entry->prev, entry->next);
139	INIT_LIST_HEAD(entry);
140}
141
142/**
143 * list_move - delete from one list and add as another's head
144 * @list: the entry to move
145 * @head: the head that will precede our entry
146 */
147static inline void list_move(struct list_head *list, struct list_head *head)
148{
149	__list_del(list->prev, list->next);
150	list_add(list, head);
151}
152
153/**
154 * list_move_tail - delete from one list and add as another's tail
155 * @list: the entry to move
156 * @head: the head that will follow our entry
157 */
158static inline void list_move_tail(struct list_head *list,
159				  struct list_head *head)
160{
161	__list_del(list->prev, list->next);
162	list_add_tail(list, head);
163}
164
165/**
166 * list_is_last - tests whether @list is the last entry in list @head
167 * @list: the entry to test
168 * @head: the head of the list
169 */
170static inline int list_is_last(const struct list_head *list,
171				const struct list_head *head)
172{
173	return list->next == head;
174}
175
176/**
177 * list_empty - tests whether a list is empty
178 * @head: the list to test.
179 */
180static inline int list_empty(const struct list_head *head)
181{
182	return head->next == head;
183}
184
185/**
186 * list_empty_careful - tests whether a list is empty and not being modified
187 * @head: the list to test
188 *
189 * Description:
190 * tests whether a list is empty _and_ checks that no other CPU might be
191 * in the process of modifying either member (next or prev)
192 *
193 * NOTE: using list_empty_careful() without synchronization
194 * can only be safe if the only activity that can happen
195 * to the list entry is list_del_init(). Eg. it cannot be used
196 * if another CPU could re-list_add() it.
197 */
198static inline int list_empty_careful(const struct list_head *head)
199{
200	struct list_head *next = head->next;
201	return (next == head) && (next == head->prev);
202}
203
204static inline void __list_splice(struct list_head *list,
205				 struct list_head *head)
206{
207	struct list_head *first = list->next;
208	struct list_head *last = list->prev;
209	struct list_head *at = head->next;
210
211	first->prev = head;
212	head->next = first;
213
214	last->next = at;
215	at->prev = last;
216}
217
218/**
219 * list_splice - join two lists
220 * @list: the new list to add.
221 * @head: the place to add it in the first list.
222 */
223static inline void list_splice(struct list_head *list, struct list_head *head)
224{
225	if (!list_empty(list))
226		__list_splice(list, head);
227}
228
229/**
230 * list_splice_init - join two lists and reinitialise the emptied list.
231 * @list: the new list to add.
232 * @head: the place to add it in the first list.
233 *
234 * The list at @list is reinitialised
235 */
236static inline void list_splice_init(struct list_head *list,
237				    struct list_head *head)
238{
239	if (!list_empty(list)) {
240		__list_splice(list, head);
241		INIT_LIST_HEAD(list);
242	}
243}
244
245/**
246 * list_entry - get the struct for this entry
247 * @ptr:	the &struct list_head pointer.
248 * @type:	the type of the struct this is embedded in.
249 * @member:	the name of the list_struct within the struct.
250 */
251#define list_entry(ptr, type, member) \
252	container_of(ptr, type, member)
253
254/**
255 * list_first_entry - get the first element from a list
256 * @ptr:	the list head to take the element from.
257 * @type:	the type of the struct this is embedded in.
258 * @member:	the name of the list_struct within the struct.
259 *
260 * Note, that list is expected to be not empty.
261 */
262#define list_first_entry(ptr, type, member) \
263	list_entry((ptr)->next, type, member)
264
265/**
266 * list_for_each	-	iterate over a list
267 * @pos:	the &struct list_head to use as a loop cursor.
268 * @head:	the head for your list.
269 */
270#define list_for_each(pos, head) \
271	for (pos = (head)->next; pos != (head); \
272        	pos = pos->next)
273
274/**
275 * __list_for_each	-	iterate over a list
276 * @pos:	the &struct list_head to use as a loop cursor.
277 * @head:	the head for your list.
278 *
279 * This variant differs from list_for_each() in that it's the
280 * simplest possible list iteration code, no prefetching is done.
281 * Use this for code that knows the list to be very short (empty
282 * or 1 entry) most of the time.
283 */
284#define __list_for_each(pos, head) \
285	for (pos = (head)->next; pos != (head); pos = pos->next)
286
287/**
288 * list_for_each_prev	-	iterate over a list backwards
289 * @pos:	the &struct list_head to use as a loop cursor.
290 * @head:	the head for your list.
291 */
292#define list_for_each_prev(pos, head) \
293	for (pos = (head)->prev; pos != (head); \
294        	pos = pos->prev)
295
296/**
297 * list_for_each_safe - iterate over a list safe against removal of list entry
298 * @pos:	the &struct list_head to use as a loop cursor.
299 * @n:		another &struct list_head to use as temporary storage
300 * @head:	the head for your list.
301 */
302#define list_for_each_safe(pos, n, head) \
303	for (pos = (head)->next, n = pos->next; pos != (head); \
304		pos = n, n = pos->next)
305
306/**
307 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
308 * @pos:	the &struct list_head to use as a loop cursor.
309 * @n:		another &struct list_head to use as temporary storage
310 * @head:	the head for your list.
311 */
312#define list_for_each_prev_safe(pos, n, head) \
313	for (pos = (head)->prev, n = pos->prev; \
314	     pos != (head); \
315	     pos = n, n = pos->prev)
316
317/**
318 * list_for_each_entry	-	iterate over list of given type
319 * @pos:	the type * to use as a loop cursor.
320 * @head:	the head for your list.
321 * @member:	the name of the list_struct within the struct.
322 */
323#define list_for_each_entry(pos, head, member)				\
324	for (pos = list_entry((head)->next, typeof(*pos), member);	\
325	     &pos->member != (head); 	\
326	     pos = list_entry(pos->member.next, typeof(*pos), member))
327
328/**
329 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
330* @pos:        the type * to use as a loop cursor.
331* @n:          another type * to use as temporary storage
332* @head:       the head for your list.
333* @member:     the name of the list_struct within the struct.
334*/
335#define list_for_each_entry_safe(pos, n, head, member)                  \
336	for (pos = list_entry((head)->next, typeof(*pos), member),      \
337		n = list_entry(pos->member.next, typeof(*pos), member); \
338             &pos->member != (head);					\
339             pos = n, n = list_entry(n->member.next, typeof(*n), member))
340
341/**
342 * list_for_each_entry_reverse - iterate backwards over list of given type.
343 * @pos:	the type * to use as a loop cursor.
344 * @head:	the head for your list.
345 * @member:	the name of the list_struct within the struct.
346 */
347#define list_for_each_entry_reverse(pos, head, member)			\
348	for (pos = list_entry((head)->prev, typeof(*pos), member);	\
349	     &pos->member != (head); 	\
350	     pos = list_entry(pos->member.prev, typeof(*pos), member))
351
352/**
353 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
354 * @pos:	the type * to use as a start point
355 * @head:	the head of the list
356 * @member:	the name of the list_struct within the struct.
357 *
358 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
359 */
360#define list_prepare_entry(pos, head, member) \
361	((pos) ? : list_entry(head, typeof(*pos), member))
362
363/**
364 * list_for_each_entry_continue - continue iteration over list of given type
365 * @pos:	the type * to use as a loop cursor.
366 * @head:	the head for your list.
367 * @member:	the name of the list_struct within the struct.
368 *
369 * Continue to iterate over list of given type, continuing after
370 * the current position.
371 */
372#define list_for_each_entry_continue(pos, head, member) 		\
373	for (pos = list_entry(pos->member.next, typeof(*pos), member);	\
374	     &pos->member != (head);	\
375	     pos = list_entry(pos->member.next, typeof(*pos), member))
376
377/**
378 * list_for_each_entry_continue_reverse - iterate backwards from the given point
379 * @pos:	the type * to use as a loop cursor.
380 * @head:	the head for your list.
381 * @member:	the name of the list_struct within the struct.
382 *
383 * Start to iterate over list of given type backwards, continuing after
384 * the current position.
385 */
386#define list_for_each_entry_continue_reverse(pos, head, member)		\
387	for (pos = list_entry(pos->member.prev, typeof(*pos), member);	\
388	     &pos->member != (head);	\
389	     pos = list_entry(pos->member.prev, typeof(*pos), member))
390
391/**
392 * list_for_each_entry_from - iterate over list of given type from the current point
393 * @pos:	the type * to use as a loop cursor.
394 * @head:	the head for your list.
395 * @member:	the name of the list_struct within the struct.
396 *
397 * Iterate over list of given type, continuing from current position.
398 */
399#define list_for_each_entry_from(pos, head, member) 			\
400	for (; &pos->member != (head);	\
401	     pos = list_entry(pos->member.next, typeof(*pos), member))
402
403/**
404 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
405 * @pos:	the type * to use as a loop cursor.
406 * @n:		another type * to use as temporary storage
407 * @head:	the head for your list.
408 * @member:	the name of the list_struct within the struct.
409 */
410#define list_for_each_entry_safe(pos, n, head, member)			\
411	for (pos = list_entry((head)->next, typeof(*pos), member),	\
412		n = list_entry(pos->member.next, typeof(*pos), member);	\
413	     &pos->member != (head); 					\
414	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
415
416/**
417 * list_for_each_entry_safe_continue
418 * @pos:	the type * to use as a loop cursor.
419 * @n:		another type * to use as temporary storage
420 * @head:	the head for your list.
421 * @member:	the name of the list_struct within the struct.
422 *
423 * Iterate over list of given type, continuing after current point,
424 * safe against removal of list entry.
425 */
426#define list_for_each_entry_safe_continue(pos, n, head, member) 		\
427	for (pos = list_entry(pos->member.next, typeof(*pos), member), 		\
428		n = list_entry(pos->member.next, typeof(*pos), member);		\
429	     &pos->member != (head);						\
430	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
431
432/**
433 * list_for_each_entry_safe_from
434 * @pos:	the type * to use as a loop cursor.
435 * @n:		another type * to use as temporary storage
436 * @head:	the head for your list.
437 * @member:	the name of the list_struct within the struct.
438 *
439 * Iterate over list of given type from current point, safe against
440 * removal of list entry.
441 */
442#define list_for_each_entry_safe_from(pos, n, head, member) 			\
443	for (n = list_entry(pos->member.next, typeof(*pos), member);		\
444	     &pos->member != (head);						\
445	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
446
447/**
448 * list_for_each_entry_safe_reverse
449 * @pos:	the type * to use as a loop cursor.
450 * @n:		another type * to use as temporary storage
451 * @head:	the head for your list.
452 * @member:	the name of the list_struct within the struct.
453 *
454 * Iterate backwards over list of given type, safe against removal
455 * of list entry.
456 */
457#define list_for_each_entry_safe_reverse(pos, n, head, member)		\
458	for (pos = list_entry((head)->prev, typeof(*pos), member),	\
459		n = list_entry(pos->member.prev, typeof(*pos), member);	\
460	     &pos->member != (head); 					\
461	     pos = n, n = list_entry(n->member.prev, typeof(*n), member))
462
463
464#endif
465