1191762Simp/*
2191762Simp * @TAG(OTHER_GPL)
3191762Simp */
4191762Simp
5191762Simp#pragma once
6191762Simp
7191762Simp#include <stddef.h>
8191762Simp
9191762Simp#define LIST_POISON1  ((void *) 0x0)
10191762Simp#define LIST_POISON2  ((void *) 0x0)
11191762Simp
12191762Simp#ifndef ARCH_HAS_PREFETCH
13191762Simp#define ARCH_HAS_PREFETCH
14191762Simpstatic inline void prefetch(const void *x) {;}
15191762Simp#endif
16191762Simp
17191762Simp/*
18191762Simp * Simple doubly linked list implementation.
19191762Simp *
20191762Simp * Some of the internal functions ("__xxx") are useful when
21191762Simp * manipulating whole lists rather than single entries, as
22191762Simp * sometimes we already know the next/prev entries and we can
23191762Simp * generate better code by using them directly rather than
24191762Simp * using the generic single-entry routines.
25191762Simp */
26191762Simp
27191762Simpstruct list_head {
28191762Simp	struct list_head *next, *prev;
29191762Simp};
30191762Simp
31191762Simpstatic inline void INIT_LIST_HEAD(struct list_head *list)
32191762Simp{
33191762Simp	list->next = list;
34191762Simp	list->prev = list;
35191762Simp}
36191762Simp
37191762Simp/*
38191762Simp * Insert a new entry between two known consecutive entries.
39191762Simp *
40191762Simp * This is only for internal list manipulation where we know
41191762Simp * the prev/next entries already!
42235338Sadrian */
43191762Simpstatic inline void __list_add(struct list_head *new,
44191762Simp			      struct list_head *prev,
45191762Simp			      struct list_head *next)
46191762Simp{
47191762Simp	next->prev = new;
48191762Simp	new->next = next;
49191762Simp	new->prev = prev;
50191762Simp	prev->next = new;
51191762Simp}
52191762Simp
53191762Simp/**
54191762Simp * list_add - add a new entry
55191762Simp * @new: new entry to be added
56191762Simp * @head: list head to add it after
57191762Simp *
58191762Simp * Insert a new entry after the specified head.
59191762Simp * This is good for implementing stacks.
60191762Simp */
61191762Simpstatic inline void list_add(struct list_head *new, struct list_head *head)
62191762Simp{
63191762Simp	__list_add(new, head, head->next);
64191762Simp}
65191762Simp
66191762Simp/**
67191762Simp * list_add_tail - add a new entry
68191762Simp * @new: new entry to be added
69206358Srpaulo * @head: list head to add it before
70191762Simp *
71191762Simp * Insert a new entry before the specified head.
72191762Simp * This is useful for implementing queues.
73191762Simp */
74191762Simpstatic inline void list_add_tail(struct list_head *new, struct list_head *head)
75191762Simp{
76191762Simp	__list_add(new, head->prev, head);
77191762Simp}
78191762Simp
79191762Simp/*
80191762Simp * Delete a list entry by making the prev/next entries
81191762Simp * point to each other.
82191762Simp *
83191762Simp * This is only for internal list manipulation where we know
84191762Simp * the prev/next entries already!
85191762Simp */
86191762Simpstatic inline void __list_del(struct list_head *prev, struct list_head *next)
87191762Simp{
88191762Simp	next->prev = prev;
89191762Simp	prev->next = next;
90191762Simp}
91191762Simp
92191762Simp/**
93191762Simp * list_del - deletes entry from list.
94191762Simp * @entry: the element to delete from the list.
95191762Simp * Note: list_empty() on entry does not return true after this, the entry is
96191762Simp * in an undefined state.
97191762Simp */
98191762Simpstatic inline void list_del(struct list_head *entry)
99191762Simp{
100228621Sbschmidt	__list_del(entry->prev, entry->next);
101228621Sbschmidt	entry->next = LIST_POISON1;
102228621Sbschmidt	entry->prev = LIST_POISON2;
103191762Simp}
104191762Simp
105191762Simp/**
106191762Simp * list_replace - replace old entry by new one
107191762Simp * @old : the element to be replaced
108191762Simp * @new : the new element to insert
109191762Simp *
110199197Sjhb * If @old was empty, it will be overwritten.
111191762Simp */
112191762Simpstatic inline void list_replace(struct list_head *old,
113191762Simp				struct list_head *new)
114191762Simp{
115191762Simp	new->next = old->next;
116191762Simp	new->next->prev = new;
117191762Simp	new->prev = old->prev;
118191762Simp	new->prev->next = new;
119191762Simp}
120191762Simp
121191762Simpstatic inline void list_replace_init(struct list_head *old,
122228621Sbschmidt					struct list_head *new)
123192468Ssam{
124191762Simp	list_replace(old, new);
125191762Simp	INIT_LIST_HEAD(old);
126191762Simp}
127191762Simp
128191762Simp/**
129191762Simp * list_del_init - deletes entry from list and reinitialize it.
130191762Simp * @entry: the element to delete from the list.
131191762Simp */
132191762Simpstatic inline void list_del_init(struct list_head *entry)
133191762Simp{
134191762Simp	__list_del(entry->prev, entry->next);
135191762Simp	INIT_LIST_HEAD(entry);
136191762Simp}
137191762Simp
138191762Simp/**
139191762Simp * list_move - delete from one list and add as another's head
140191762Simp * @list: the entry to move
141191762Simp * @head: the head that will precede our entry
142191762Simp */
143191762Simpstatic inline void list_move(struct list_head *list, struct list_head *head)
144191762Simp{
145191762Simp	__list_del(list->prev, list->next);
146191762Simp	list_add(list, head);
147191762Simp}
148191762Simp
149191762Simp/**
150191762Simp * list_move_tail - delete from one list and add as another's tail
151191762Simp * @list: the entry to move
152191762Simp * @head: the head that will follow our entry
153191762Simp */
154191762Simpstatic inline void list_move_tail(struct list_head *list,
155191762Simp				  struct list_head *head)
156191762Simp{
157191762Simp	__list_del(list->prev, list->next);
158191762Simp	list_add_tail(list, head);
159191762Simp}
160191762Simp
161191762Simp/**
162191762Simp * list_is_last - tests whether @list is the last entry in list @head
163191762Simp * @list: the entry to test
164191762Simp * @head: the head of the list
165191762Simp */
166191762Simpstatic inline int list_is_last(const struct list_head *list,
167191762Simp				const struct list_head *head)
168191762Simp{
169191762Simp	return list->next == head;
170191762Simp}
171191762Simp
172191762Simp/**
173191762Simp * list_empty - tests whether a list is empty
174191762Simp * @head: the list to test.
175191762Simp */
176191762Simpstatic inline int list_empty(const struct list_head *head)
177191762Simp{
178191762Simp	return head->next == head;
179191762Simp}
180191762Simp
181191762Simp/**
182191762Simp * list_empty_careful - tests whether a list is empty and not being modified
183191762Simp * @head: the list to test
184191762Simp *
185191762Simp * Description:
186191762Simp * tests whether a list is empty _and_ checks that no other CPU might be
187191762Simp * in the process of modifying either member (next or prev)
188191762Simp *
189191762Simp * NOTE: using list_empty_careful() without synchronization
190191762Simp * can only be safe if the only activity that can happen
191191762Simp * to the list entry is list_del_init(). Eg. it cannot be used
192191762Simp * if another CPU could re-list_add() it.
193191762Simp */
194191762Simpstatic inline int list_empty_careful(const struct list_head *head)
195191762Simp{
196191762Simp	struct list_head *next = head->next;
197191762Simp	return (next == head) && (next == head->prev);
198191762Simp}
199191762Simp
200191762Simp/**
201191762Simp * list_is_singular - tests whether a list has just one entry.
202191762Simp * @head: the list to test.
203191762Simp */
204191762Simpstatic inline int list_is_singular(const struct list_head *head)
205191762Simp{
206191762Simp	return !list_empty(head) && (head->next == head->prev);
207191762Simp}
208191762Simp
209191762Simpstatic inline void __list_cut_position(struct list_head *list,
210191762Simp		struct list_head *head, struct list_head *entry)
211191762Simp{
212191762Simp	struct list_head *new_first = entry->next;
213191762Simp	list->next = head->next;
214191762Simp	list->next->prev = list;
215191762Simp	list->prev = entry;
216191762Simp	entry->next = list;
217191762Simp	head->next = new_first;
218191762Simp	new_first->prev = head;
219191762Simp}
220191762Simp
221191762Simp/**
222191762Simp * list_cut_position - cut a list into two
223191762Simp * @list: a new list to add all removed entries
224226181Sadrian * @head: a list with entries
225191762Simp * @entry: an entry within head, could be the head itself
226191762Simp *	and if so we won't cut the list
227191762Simp *
228191762Simp * This helper moves the initial part of @head, up to and
229191762Simp * including @entry, from @head to @list. You should
230191762Simp * pass on @entry an element you know is on @head. @list
231191762Simp * should be an empty list or a list you do not care about
232191762Simp * losing its data.
233191762Simp *
234191762Simp */
235191762Simpstatic inline void list_cut_position(struct list_head *list,
236191762Simp		struct list_head *head, struct list_head *entry)
237191762Simp{
238191762Simp	if (list_empty(head))
239191762Simp		return;
240191762Simp	if (list_is_singular(head) &&
241191762Simp		(head->next != entry && head != entry))
242191762Simp		return;
243191762Simp	if (entry == head)
244191762Simp		INIT_LIST_HEAD(list);
245191762Simp	else
246191762Simp		__list_cut_position(list, head, entry);
247191762Simp}
248191762Simp
249191762Simpstatic inline void __list_splice(const struct list_head *list,
250191762Simp				 struct list_head *prev,
251191762Simp				 struct list_head *next)
252191762Simp{
253191762Simp	struct list_head *first = list->next;
254191762Simp	struct list_head *last = list->prev;
255191762Simp
256191762Simp	first->prev = prev;
257191762Simp	prev->next = first;
258191762Simp
259191762Simp	last->next = next;
260191762Simp	next->prev = last;
261191762Simp}
262191762Simp
263191762Simp/**
264191762Simp * list_splice - join two lists, this is designed for stacks
265191762Simp * @list: the new list to add.
266191762Simp * @head: the place to add it in the first list.
267191762Simp */
268191762Simpstatic inline void list_splice(const struct list_head *list,
269191762Simp				struct list_head *head)
270191762Simp{
271191762Simp	if (!list_empty(list))
272191762Simp		__list_splice(list, head, head->next);
273191762Simp}
274191762Simp
275191762Simp/**
276191762Simp * list_splice_tail - join two lists, each list being a queue
277191762Simp * @list: the new list to add.
278191762Simp * @head: the place to add it in the first list.
279191762Simp */
280191762Simpstatic inline void list_splice_tail(struct list_head *list,
281191762Simp				struct list_head *head)
282191762Simp{
283191762Simp	if (!list_empty(list))
284191762Simp		__list_splice(list, head->prev, head);
285191762Simp}
286191762Simp
287191762Simp/**
288191762Simp * list_splice_init - join two lists and reinitialise the emptied list.
289191762Simp * @list: the new list to add.
290191762Simp * @head: the place to add it in the first list.
291191762Simp *
292191762Simp * The list at @list is reinitialised
293191762Simp */
294191762Simpstatic inline void list_splice_init(struct list_head *list,
295191762Simp				    struct list_head *head)
296191762Simp{
297191762Simp	if (!list_empty(list)) {
298191762Simp		__list_splice(list, head, head->next);
299191762Simp		INIT_LIST_HEAD(list);
300191762Simp	}
301191762Simp}
302191762Simp
303191762Simp/**
304191762Simp * list_splice_tail_init - join two lists and reinitialise the emptied list
305191762Simp * @list: the new list to add.
306191762Simp * @head: the place to add it in the first list.
307191762Simp *
308191762Simp * Each of the lists is a queue.
309191762Simp * The list at @list is reinitialised
310191762Simp */
311191762Simpstatic inline void list_splice_tail_init(struct list_head *list,
312191762Simp					 struct list_head *head)
313191762Simp{
314191762Simp	if (!list_empty(list)) {
315191762Simp		__list_splice(list, head->prev, head);
316191762Simp		INIT_LIST_HEAD(list);
317191762Simp	}
318191762Simp}
319191762Simp
320191762Simp/**
321191762Simp * list_entry - get the struct for this entry
322191762Simp * @ptr:	the &struct list_head pointer.
323191762Simp * @type:	the type of the struct this is embedded in.
324191762Simp * @member:	the name of the list_struct within the struct.
325191762Simp */
326191762Simp#define list_entry(ptr, type, member) \
327191762Simp	container_of(ptr, type, member)
328191762Simp
329191762Simp/**
330191762Simp * list_first_entry - get the first element from a list
331191762Simp * @ptr:	the list head to take the element from.
332191762Simp * @type:	the type of the struct this is embedded in.
333191762Simp * @member:	the name of the list_struct within the struct.
334191762Simp *
335191762Simp * Note, that list is expected to be not empty.
336191762Simp */
337191762Simp#define list_first_entry(ptr, type, member) \
338191762Simp	list_entry((ptr)->next, type, member)
339191762Simp
340191762Simp/**
341191762Simp * list_for_each	-	iterate over a list
342191762Simp * @pos:	the &struct list_head to use as a loop cursor.
343191762Simp * @head:	the head for your list.
344191762Simp */
345191762Simp#define list_for_each(pos, head) \
346191762Simp	for (pos = (head)->next; prefetch(pos->next), pos != (head); \
347191762Simp		pos = pos->next)
348191762Simp
349191762Simp/**
350191762Simp * __list_for_each	-	iterate over a list
351191762Simp * @pos:	the &struct list_head to use as a loop cursor.
352191762Simp * @head:	the head for your list.
353191762Simp *
354191762Simp * This variant differs from list_for_each() in that it's the
355191762Simp * simplest possible list iteration code, no prefetching is done.
356191762Simp * Use this for code that knows the list to be very short (empty
357191762Simp * or 1 entry) most of the time.
358191762Simp */
359191762Simp#define __list_for_each(pos, head) \
360191762Simp	for (pos = (head)->next; pos != (head); pos = pos->next)
361191762Simp
362191762Simp/**
363191762Simp * list_for_each_prev	-	iterate over a list backwards
364191762Simp * @pos:	the &struct list_head to use as a loop cursor.
365191762Simp * @head:	the head for your list.
366191762Simp */
367191762Simp#define list_for_each_prev(pos, head) \
368191762Simp	for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
369191762Simp		pos = pos->prev)
370191762Simp
371191762Simp/**
372191762Simp * list_for_each_safe - iterate over a list safe against removal of list entry
373191762Simp * @pos:	the &struct list_head to use as a loop cursor.
374191762Simp * @n:		another &struct list_head to use as temporary storage
375191762Simp * @head:	the head for your list.
376191762Simp */
377191762Simp#define list_for_each_safe(pos, n, head) \
378191762Simp	for (pos = (head)->next, n = pos->next; pos != (head); \
379191762Simp		pos = n, n = pos->next)
380191762Simp
381191762Simp/**
382191762Simp * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
383191762Simp * @pos:	the &struct list_head to use as a loop cursor.
384191762Simp * @n:		another &struct list_head to use as temporary storage
385191762Simp * @head:	the head for your list.
386191762Simp */
387191762Simp#define list_for_each_prev_safe(pos, n, head) \
388191762Simp	for (pos = (head)->prev, n = pos->prev; \
389191762Simp	     prefetch(pos->prev), pos != (head); \
390191762Simp	     pos = n, n = pos->prev)
391191762Simp
392191762Simp/**
393191762Simp * list_for_each_entry	-	iterate over list of given type
394191762Simp * @pos:	the type * to use as a loop cursor.
395191762Simp * @head:	the head for your list.
396191762Simp * @member:	the name of the list_struct within the struct.
397191762Simp */
398191762Simp#define list_for_each_entry(pos, head, member)				\
399191762Simp	for (pos = list_entry((head)->next, typeof(*pos), member);	\
400191762Simp	     prefetch(pos->member.next), &pos->member != (head);	\
401191762Simp	     pos = list_entry(pos->member.next, typeof(*pos), member))
402191762Simp
403191762Simp/**
404191762Simp * list_for_each_entry_reverse - iterate backwards over list of given type.
405191762Simp * @pos:	the type * to use as a loop cursor.
406191762Simp * @head:	the head for your list.
407191762Simp * @member:	the name of the list_struct within the struct.
408191762Simp */
409191762Simp#define list_for_each_entry_reverse(pos, head, member)			\
410191762Simp	for (pos = list_entry((head)->prev, typeof(*pos), member);	\
411191762Simp	     prefetch(pos->member.prev), &pos->member != (head);	\
412191762Simp	     pos = list_entry(pos->member.prev, typeof(*pos), member))
413191762Simp
414191762Simp/**
415191762Simp * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
416191762Simp * @pos:	the type * to use as a start point
417191762Simp * @head:	the head of the list
418191762Simp * @member:	the name of the list_struct within the struct.
419191762Simp *
420191762Simp * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
421191762Simp */
422191762Simp#define list_prepare_entry(pos, head, member) \
423191762Simp	((pos) ? : list_entry(head, typeof(*pos), member))
424191762Simp
425191762Simp/**
426191762Simp * list_for_each_entry_continue - continue iteration over list of given type
427191762Simp * @pos:	the type * to use as a loop cursor.
428191762Simp * @head:	the head for your list.
429191762Simp * @member:	the name of the list_struct within the struct.
430191762Simp *
431191762Simp * Continue to iterate over list of given type, continuing after
432191762Simp * the current position.
433191762Simp */
434191762Simp#define list_for_each_entry_continue(pos, head, member) 		\
435191762Simp	for (pos = list_entry(pos->member.next, typeof(*pos), member);	\
436191762Simp	     prefetch(pos->member.next), &pos->member != (head);	\
437191762Simp	     pos = list_entry(pos->member.next, typeof(*pos), member))
438191762Simp
439191762Simp/**
440191762Simp * list_for_each_entry_continue_reverse - iterate backwards from the given point
441191762Simp * @pos:	the type * to use as a loop cursor.
442191762Simp * @head:	the head for your list.
443191762Simp * @member:	the name of the list_struct within the struct.
444191762Simp *
445191762Simp * Start to iterate over list of given type backwards, continuing after
446191762Simp * the current position.
447191762Simp */
448191762Simp#define list_for_each_entry_continue_reverse(pos, head, member)		\
449191762Simp	for (pos = list_entry(pos->member.prev, typeof(*pos), member);	\
450191762Simp	     prefetch(pos->member.prev), &pos->member != (head);	\
451191762Simp	     pos = list_entry(pos->member.prev, typeof(*pos), member))
452191762Simp
453191762Simp/**
454191762Simp * list_for_each_entry_from - iterate over list of given type from the current point
455191762Simp * @pos:	the type * to use as a loop cursor.
456191762Simp * @head:	the head for your list.
457191762Simp * @member:	the name of the list_struct within the struct.
458191762Simp *
459191762Simp * Iterate over list of given type, continuing from current position.
460191762Simp */
461191762Simp#define list_for_each_entry_from(pos, head, member)			\
462191762Simp	for (; prefetch(pos->member.next), &pos->member != (head);	\
463191762Simp	     pos = list_entry(pos->member.next, typeof(*pos), member))
464207554Ssobomax
465207554Ssobomax/**
466191762Simp * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
467199197Sjhb * @pos:	the type * to use as a loop cursor.
468191762Simp * @n:		another type * to use as temporary storage
469191762Simp * @head:	the head for your list.
470191762Simp * @member:	the name of the list_struct within the struct.
471191762Simp */
472191762Simp#define list_for_each_entry_safe(pos, n, head, member)			\
473191762Simp	for (pos = list_entry((head)->next, typeof(*pos), member),	\
474191762Simp		n = list_entry(pos->member.next, typeof(*pos), member);	\
475191762Simp	     &pos->member != (head);					\
476191762Simp	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
477191762Simp
478191762Simp/**
479191762Simp * list_for_each_entry_safe_continue
480191762Simp * @pos:	the type * to use as a loop cursor.
481191762Simp * @n:		another type * to use as temporary storage
482191762Simp * @head:	the head for your list.
483191762Simp * @member:	the name of the list_struct within the struct.
484191762Simp *
485191762Simp * Iterate over list of given type, continuing after current point,
486191762Simp * safe against removal of list entry.
487191762Simp */
488191762Simp#define list_for_each_entry_safe_continue(pos, n, head, member) 		\
489191762Simp	for (pos = list_entry(pos->member.next, typeof(*pos), member),		\
490191762Simp		n = list_entry(pos->member.next, typeof(*pos), member);		\
491191762Simp	     &pos->member != (head);						\
492191762Simp	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
493191762Simp
494191762Simp/**
495191762Simp * list_for_each_entry_safe_from
496191762Simp * @pos:	the type * to use as a loop cursor.
497191762Simp * @n:		another type * to use as temporary storage
498191762Simp * @head:	the head for your list.
499191762Simp * @member:	the name of the list_struct within the struct.
500191762Simp *
501191762Simp * Iterate over list of given type from current point, safe against
502191762Simp * removal of list entry.
503191762Simp */
504191762Simp#define list_for_each_entry_safe_from(pos, n, head, member)			\
505191762Simp	for (n = list_entry(pos->member.next, typeof(*pos), member);		\
506191762Simp	     &pos->member != (head);						\
507191762Simp	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
508191762Simp
509191762Simp/**
510191762Simp * list_for_each_entry_safe_reverse
511191762Simp * @pos:	the type * to use as a loop cursor.
512191762Simp * @n:		another type * to use as temporary storage
513191762Simp * @head:	the head for your list.
514214894Sbschmidt * @member:	the name of the list_struct within the struct.
515191762Simp *
516191762Simp * Iterate backwards over list of given type, safe against removal
517191762Simp * of list entry.
518191762Simp */
519191762Simp#define list_for_each_entry_safe_reverse(pos, n, head, member)		\
520191762Simp	for (pos = list_entry((head)->prev, typeof(*pos), member),	\
521191762Simp		n = list_entry(pos->member.prev, typeof(*pos), member);	\
522191762Simp	     &pos->member != (head);					\
523191762Simp	     pos = n, n = list_entry(n->member.prev, typeof(*n), member))
524191762Simp
525191762Simp/*
526191762Simp * Double linked lists with a single pointer list head.
527191762Simp * Mostly useful for hash tables where the two pointer list head is
528191762Simp * too wasteful.
529191762Simp * You lose the ability to access the tail in O(1).
530191762Simp */
531192468Ssam
532192468Ssamstruct hlist_head {
533192468Ssam	struct hlist_node *first;
534192468Ssam};
535192468Ssam
536191762Simpstruct hlist_node {
537191762Simp	struct hlist_node *next, **pprev;
538191762Simp};
539191762Simp
540217323Smdf#define HLIST_HEAD_INIT { .first = NULL }
541191762Simp#define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
542191762Simp#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
543191762Simpstatic inline void INIT_HLIST_NODE(struct hlist_node *h)
544217323Smdf{
545191762Simp	h->next = NULL;
546191762Simp	h->pprev = NULL;
547191762Simp}
548191762Simp
549191762Simpstatic inline int hlist_unhashed(const struct hlist_node *h)
550191762Simp{
551191762Simp	return !h->pprev;
552191762Simp}
553191762Simp
554191762Simpstatic inline int hlist_empty(const struct hlist_head *h)
555191762Simp{
556191762Simp	return !h->first;
557191762Simp}
558191762Simp
559191762Simpstatic inline void __hlist_del(struct hlist_node *n)
560191762Simp{
561191762Simp	struct hlist_node *next = n->next;
562191762Simp	struct hlist_node **pprev = n->pprev;
563191762Simp	*pprev = next;
564191762Simp	if (next)
565191762Simp		next->pprev = pprev;
566191762Simp}
567191762Simp
568191762Simpstatic inline void hlist_del(struct hlist_node *n)
569191762Simp{
570191762Simp	__hlist_del(n);
571191762Simp	n->next = LIST_POISON1;
572191762Simp	n->pprev = LIST_POISON2;
573191762Simp}
574191762Simp
575191762Simpstatic inline void hlist_del_init(struct hlist_node *n)
576191762Simp{
577191762Simp	if (!hlist_unhashed(n)) {
578193237Simp		__hlist_del(n);
579191762Simp		INIT_HLIST_NODE(n);
580199197Sjhb	}
581191762Simp}
582191762Simp
583191762Simpstatic inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
584191762Simp{
585191762Simp	struct hlist_node *first = h->first;
586191762Simp	n->next = first;
587191762Simp	if (first)
588191762Simp		first->pprev = &n->next;
589191762Simp	h->first = n;
590191762Simp	n->pprev = &h->first;
591191762Simp}
592191762Simp
593191762Simp/* next must be != NULL */
594191762Simpstatic inline void hlist_add_before(struct hlist_node *n,
595228621Sbschmidt					struct hlist_node *next)
596228621Sbschmidt{
597228621Sbschmidt	n->pprev = next->pprev;
598228621Sbschmidt	n->next = next;
599191762Simp	next->pprev = &n->next;
600191762Simp	*(n->pprev) = n;
601191762Simp}
602191762Simp
603191762Simpstatic inline void hlist_add_after(struct hlist_node *n,
604191762Simp					struct hlist_node *next)
605191762Simp{
606191762Simp	next->next = n->next;
607191762Simp	n->next = next;
608191762Simp	next->pprev = &n->next;
609191762Simp
610191762Simp	if(next->next)
611191762Simp		next->next->pprev  = &next->next;
612191762Simp}
613191762Simp
614191762Simp#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
615191762Simp
616191762Simp#define hlist_for_each(pos, head) \
617191762Simp	for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
618191762Simp	     pos = pos->next)
619191762Simp
620206358Srpaulo#define hlist_for_each_safe(pos, n, head) \
621191762Simp	for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
622191762Simp	     pos = n)
623191762Simp
624191762Simp/**
625191762Simp * hlist_for_each_entry	- iterate over list of given type
626191762Simp * @tpos:	the type * to use as a loop cursor.
627191762Simp * @pos:	the &struct hlist_node to use as a loop cursor.
628191762Simp * @head:	the head for your list.
629191762Simp * @member:	the name of the hlist_node within the struct.
630191762Simp */
631191762Simp#define hlist_for_each_entry(tpos, pos, head, member)			 \
632191762Simp	for (pos = (head)->first;					 \
633206358Srpaulo	     pos && ({ prefetch(pos->next); 1;}) &&			 \
634191762Simp		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
635191762Simp	     pos = pos->next)
636191762Simp
637191762Simp/**
638191762Simp * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
639191762Simp * @tpos:	the type * to use as a loop cursor.
640191762Simp * @pos:	the &struct hlist_node to use as a loop cursor.
641191762Simp * @member:	the name of the hlist_node within the struct.
642191762Simp */
643191762Simp#define hlist_for_each_entry_continue(tpos, pos, member)		 \
644191762Simp	for (pos = (pos)->next;						 \
645191762Simp	     pos && ({ prefetch(pos->next); 1;}) &&			 \
646191762Simp		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
647191762Simp	     pos = pos->next)
648191762Simp
649191762Simp/**
650191762Simp * hlist_for_each_entry_from - iterate over a hlist continuing from current point
651191762Simp * @tpos:	the type * to use as a loop cursor.
652191762Simp * @pos:	the &struct hlist_node to use as a loop cursor.
653191762Simp * @member:	the name of the hlist_node within the struct.
654191762Simp */
655191762Simp#define hlist_for_each_entry_from(tpos, pos, member)			 \
656191762Simp	for (; pos && ({ prefetch(pos->next); 1;}) &&			 \
657191762Simp		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
658191762Simp	     pos = pos->next)
659191762Simp
660191762Simp/**
661191762Simp * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
662191762Simp * @tpos:	the type * to use as a loop cursor.
663191762Simp * @pos:	the &struct hlist_node to use as a loop cursor.
664191762Simp * @n:		another &struct hlist_node to use as temporary storage
665191762Simp * @head:	the head for your list.
666191762Simp * @member:	the name of the hlist_node within the struct.
667191762Simp */
668191762Simp#define hlist_for_each_entry_safe(tpos, pos, n, head, member)		 \
669191762Simp	for (pos = (head)->first;					 \
670191762Simp	     pos && ({ n = pos->next; 1; }) &&				 \
671191762Simp		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
672191762Simp	     pos = n)
673191762Simp
674191762Simp