Blame libiptc/linux_list.h

Packit 7b22a4
#ifndef _LINUX_LIST_H
Packit 7b22a4
#define _LINUX_LIST_H
Packit 7b22a4
Packit 7b22a4
#undef offsetof
Packit 7b22a4
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * container_of - cast a member of a structure out to the containing structure
Packit 7b22a4
 *
Packit 7b22a4
 * @ptr:	the pointer to the member.
Packit 7b22a4
 * @type:	the type of the container struct this is embedded in.
Packit 7b22a4
 * @member:	the name of the member within the struct.
Packit 7b22a4
 *
Packit 7b22a4
 */
Packit 7b22a4
#define container_of(ptr, type, member) ({			\
Packit 7b22a4
        const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
Packit 7b22a4
        (type *)( (char *)__mptr - offsetof(type,member) );})
Packit 7b22a4
Packit 7b22a4
/*
Packit 7b22a4
 * Check at compile time that something is of a particular type.
Packit 7b22a4
 * Always evaluates to 1 so you may use it easily in comparisons.
Packit 7b22a4
 */
Packit 7b22a4
#define typecheck(type,x) \
Packit 7b22a4
({	type __dummy; \
Packit 7b22a4
	typeof(x) __dummy2; \
Packit 7b22a4
	(void)(&__dummy == &__dummy2); \
Packit 7b22a4
	1; \
Packit 7b22a4
})
Packit 7b22a4
Packit 7b22a4
#define prefetch(x)		((void)0)
Packit 7b22a4
Packit 7b22a4
/* empty define to make this work in userspace -HW */
Packit 7b22a4
#define smp_wmb()
Packit 7b22a4
Packit 7b22a4
/*
Packit 7b22a4
 * These are non-NULL pointers that will result in page faults
Packit 7b22a4
 * under normal circumstances, used to verify that nobody uses
Packit 7b22a4
 * non-initialized list entries.
Packit 7b22a4
 */
Packit 7b22a4
#define LIST_POISON1  ((void *) 0x00100100)
Packit 7b22a4
#define LIST_POISON2  ((void *) 0x00200200)
Packit 7b22a4
Packit 7b22a4
/*
Packit 7b22a4
 * Simple doubly linked list implementation.
Packit 7b22a4
 *
Packit 7b22a4
 * Some of the internal functions ("__xxx") are useful when
Packit 7b22a4
 * manipulating whole lists rather than single entries, as
Packit 7b22a4
 * sometimes we already know the next/prev entries and we can
Packit 7b22a4
 * generate better code by using them directly rather than
Packit 7b22a4
 * using the generic single-entry routines.
Packit 7b22a4
 */
Packit 7b22a4
Packit 7b22a4
struct list_head {
Packit 7b22a4
	struct list_head *next, *prev;
Packit 7b22a4
};
Packit 7b22a4
Packit 7b22a4
#define LIST_HEAD_INIT(name) { &(name), &(name) }
Packit 7b22a4
Packit 7b22a4
#define LIST_HEAD(name) \
Packit 7b22a4
	struct list_head name = LIST_HEAD_INIT(name)
Packit 7b22a4
Packit 7b22a4
#define INIT_LIST_HEAD(ptr) do { \
Packit 7b22a4
	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
Packit 7b22a4
} while (0)
Packit 7b22a4
Packit 7b22a4
/*
Packit 7b22a4
 * Insert a new entry between two known consecutive entries.
Packit 7b22a4
 *
Packit 7b22a4
 * This is only for internal list manipulation where we know
Packit 7b22a4
 * the prev/next entries already!
Packit 7b22a4
 */
Packit 7b22a4
static inline void __list_add(struct list_head *new,
Packit 7b22a4
			      struct list_head *prev,
Packit 7b22a4
			      struct list_head *next)
Packit 7b22a4
{
Packit 7b22a4
	next->prev = new;
Packit 7b22a4
	new->next = next;
Packit 7b22a4
	new->prev = prev;
Packit 7b22a4
	prev->next = new;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_add - add a new entry
Packit 7b22a4
 * @new: new entry to be added
Packit 7b22a4
 * @head: list head to add it after
Packit 7b22a4
 *
Packit 7b22a4
 * Insert a new entry after the specified head.
Packit 7b22a4
 * This is good for implementing stacks.
Packit 7b22a4
 */
Packit 7b22a4
static inline void list_add(struct list_head *new, struct list_head *head)
Packit 7b22a4
{
Packit 7b22a4
	__list_add(new, head, head->next);
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_add_tail - add a new entry
Packit 7b22a4
 * @new: new entry to be added
Packit 7b22a4
 * @head: list head to add it before
Packit 7b22a4
 *
Packit 7b22a4
 * Insert a new entry before the specified head.
Packit 7b22a4
 * This is useful for implementing queues.
Packit 7b22a4
 */
Packit 7b22a4
static inline void list_add_tail(struct list_head *new, struct list_head *head)
Packit 7b22a4
{
Packit 7b22a4
	__list_add(new, head->prev, head);
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/*
Packit 7b22a4
 * Insert a new entry between two known consecutive entries.
Packit 7b22a4
 *
Packit 7b22a4
 * This is only for internal list manipulation where we know
Packit 7b22a4
 * the prev/next entries already!
Packit 7b22a4
 */
Packit 7b22a4
static inline void __list_add_rcu(struct list_head * new,
Packit 7b22a4
		struct list_head * prev, struct list_head * next)
Packit 7b22a4
{
Packit 7b22a4
	new->next = next;
Packit 7b22a4
	new->prev = prev;
Packit 7b22a4
	smp_wmb();
Packit 7b22a4
	next->prev = new;
Packit 7b22a4
	prev->next = new;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_add_rcu - add a new entry to rcu-protected list
Packit 7b22a4
 * @new: new entry to be added
Packit 7b22a4
 * @head: list head to add it after
Packit 7b22a4
 *
Packit 7b22a4
 * Insert a new entry after the specified head.
Packit 7b22a4
 * This is good for implementing stacks.
Packit 7b22a4
 *
Packit 7b22a4
 * The caller must take whatever precautions are necessary
Packit 7b22a4
 * (such as holding appropriate locks) to avoid racing
Packit 7b22a4
 * with another list-mutation primitive, such as list_add_rcu()
Packit 7b22a4
 * or list_del_rcu(), running on this same list.
Packit 7b22a4
 * However, it is perfectly legal to run concurrently with
Packit 7b22a4
 * the _rcu list-traversal primitives, such as
Packit 7b22a4
 * list_for_each_entry_rcu().
Packit 7b22a4
 */
Packit 7b22a4
static inline void list_add_rcu(struct list_head *new, struct list_head *head)
Packit 7b22a4
{
Packit 7b22a4
	__list_add_rcu(new, head, head->next);
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_add_tail_rcu - add a new entry to rcu-protected list
Packit 7b22a4
 * @new: new entry to be added
Packit 7b22a4
 * @head: list head to add it before
Packit 7b22a4
 *
Packit 7b22a4
 * Insert a new entry before the specified head.
Packit 7b22a4
 * This is useful for implementing queues.
Packit 7b22a4
 *
Packit 7b22a4
 * The caller must take whatever precautions are necessary
Packit 7b22a4
 * (such as holding appropriate locks) to avoid racing
Packit 7b22a4
 * with another list-mutation primitive, such as list_add_tail_rcu()
Packit 7b22a4
 * or list_del_rcu(), running on this same list.
Packit 7b22a4
 * However, it is perfectly legal to run concurrently with
Packit 7b22a4
 * the _rcu list-traversal primitives, such as
Packit 7b22a4
 * list_for_each_entry_rcu().
Packit 7b22a4
 */
Packit 7b22a4
static inline void list_add_tail_rcu(struct list_head *new,
Packit 7b22a4
					struct list_head *head)
Packit 7b22a4
{
Packit 7b22a4
	__list_add_rcu(new, head->prev, head);
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/*
Packit 7b22a4
 * Delete a list entry by making the prev/next entries
Packit 7b22a4
 * point to each other.
Packit 7b22a4
 *
Packit 7b22a4
 * This is only for internal list manipulation where we know
Packit 7b22a4
 * the prev/next entries already!
Packit 7b22a4
 */
Packit 7b22a4
static inline void __list_del(struct list_head * prev, struct list_head * next)
Packit 7b22a4
{
Packit 7b22a4
	next->prev = prev;
Packit 7b22a4
	prev->next = next;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_del - deletes entry from list.
Packit 7b22a4
 * @entry: the element to delete from the list.
Packit 7b22a4
 * Note: list_empty on entry does not return true after this, the entry is
Packit 7b22a4
 * in an undefined state.
Packit 7b22a4
 */
Packit 7b22a4
static inline void list_del(struct list_head *entry)
Packit 7b22a4
{
Packit 7b22a4
	__list_del(entry->prev, entry->next);
Packit 7b22a4
	entry->next = LIST_POISON1;
Packit 7b22a4
	entry->prev = LIST_POISON2;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_del_rcu - deletes entry from list without re-initialization
Packit 7b22a4
 * @entry: the element to delete from the list.
Packit 7b22a4
 *
Packit 7b22a4
 * Note: list_empty on entry does not return true after this,
Packit 7b22a4
 * the entry is in an undefined state. It is useful for RCU based
Packit 7b22a4
 * lockfree traversal.
Packit 7b22a4
 *
Packit 7b22a4
 * In particular, it means that we can not poison the forward
Packit 7b22a4
 * pointers that may still be used for walking the list.
Packit 7b22a4
 *
Packit 7b22a4
 * The caller must take whatever precautions are necessary
Packit 7b22a4
 * (such as holding appropriate locks) to avoid racing
Packit 7b22a4
 * with another list-mutation primitive, such as list_del_rcu()
Packit 7b22a4
 * or list_add_rcu(), running on this same list.
Packit 7b22a4
 * However, it is perfectly legal to run concurrently with
Packit 7b22a4
 * the _rcu list-traversal primitives, such as
Packit 7b22a4
 * list_for_each_entry_rcu().
Packit 7b22a4
 *
Packit 7b22a4
 * Note that the caller is not permitted to immediately free
Packit 7b22a4
 * the newly deleted entry.  Instead, either synchronize_kernel()
Packit 7b22a4
 * or call_rcu() must be used to defer freeing until an RCU
Packit 7b22a4
 * grace period has elapsed.
Packit 7b22a4
 */
Packit 7b22a4
static inline void list_del_rcu(struct list_head *entry)
Packit 7b22a4
{
Packit 7b22a4
	__list_del(entry->prev, entry->next);
Packit 7b22a4
	entry->prev = LIST_POISON2;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_del_init - deletes entry from list and reinitialize it.
Packit 7b22a4
 * @entry: the element to delete from the list.
Packit 7b22a4
 */
Packit 7b22a4
static inline void list_del_init(struct list_head *entry)
Packit 7b22a4
{
Packit 7b22a4
	__list_del(entry->prev, entry->next);
Packit 7b22a4
	INIT_LIST_HEAD(entry);
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_move - delete from one list and add as another's head
Packit 7b22a4
 * @list: the entry to move
Packit 7b22a4
 * @head: the head that will precede our entry
Packit 7b22a4
 */
Packit 7b22a4
static inline void list_move(struct list_head *list, struct list_head *head)
Packit 7b22a4
{
Packit 7b22a4
        __list_del(list->prev, list->next);
Packit 7b22a4
        list_add(list, head);
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_move_tail - delete from one list and add as another's tail
Packit 7b22a4
 * @list: the entry to move
Packit 7b22a4
 * @head: the head that will follow our entry
Packit 7b22a4
 */
Packit 7b22a4
static inline void list_move_tail(struct list_head *list,
Packit 7b22a4
				  struct list_head *head)
Packit 7b22a4
{
Packit 7b22a4
        __list_del(list->prev, list->next);
Packit 7b22a4
        list_add_tail(list, head);
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_empty - tests whether a list is empty
Packit 7b22a4
 * @head: the list to test.
Packit 7b22a4
 */
Packit 7b22a4
static inline int list_empty(const struct list_head *head)
Packit 7b22a4
{
Packit 7b22a4
	return head->next == head;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_empty_careful - tests whether a list is
Packit 7b22a4
 * empty _and_ checks that no other CPU might be
Packit 7b22a4
 * in the process of still modifying either member
Packit 7b22a4
 *
Packit 7b22a4
 * NOTE: using list_empty_careful() without synchronization
Packit 7b22a4
 * can only be safe if the only activity that can happen
Packit 7b22a4
 * to the list entry is list_del_init(). Eg. it cannot be used
Packit 7b22a4
 * if another CPU could re-list_add() it.
Packit 7b22a4
 *
Packit 7b22a4
 * @head: the list to test.
Packit 7b22a4
 */
Packit 7b22a4
static inline int list_empty_careful(const struct list_head *head)
Packit 7b22a4
{
Packit 7b22a4
	struct list_head *next = head->next;
Packit 7b22a4
	return (next == head) && (next == head->prev);
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
static inline void __list_splice(struct list_head *list,
Packit 7b22a4
				 struct list_head *head)
Packit 7b22a4
{
Packit 7b22a4
	struct list_head *first = list->next;
Packit 7b22a4
	struct list_head *last = list->prev;
Packit 7b22a4
	struct list_head *at = head->next;
Packit 7b22a4
Packit 7b22a4
	first->prev = head;
Packit 7b22a4
	head->next = first;
Packit 7b22a4
Packit 7b22a4
	last->next = at;
Packit 7b22a4
	at->prev = last;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_splice - join two lists
Packit 7b22a4
 * @list: the new list to add.
Packit 7b22a4
 * @head: the place to add it in the first list.
Packit 7b22a4
 */
Packit 7b22a4
static inline void list_splice(struct list_head *list, struct list_head *head)
Packit 7b22a4
{
Packit 7b22a4
	if (!list_empty(list))
Packit 7b22a4
		__list_splice(list, head);
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_splice_init - join two lists and reinitialise the emptied list.
Packit 7b22a4
 * @list: the new list to add.
Packit 7b22a4
 * @head: the place to add it in the first list.
Packit 7b22a4
 *
Packit 7b22a4
 * The list at @list is reinitialised
Packit 7b22a4
 */
Packit 7b22a4
static inline void list_splice_init(struct list_head *list,
Packit 7b22a4
				    struct list_head *head)
Packit 7b22a4
{
Packit 7b22a4
	if (!list_empty(list)) {
Packit 7b22a4
		__list_splice(list, head);
Packit 7b22a4
		INIT_LIST_HEAD(list);
Packit 7b22a4
	}
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_entry - get the struct for this entry
Packit 7b22a4
 * @ptr:	the &struct list_head pointer.
Packit 7b22a4
 * @type:	the type of the struct this is embedded in.
Packit 7b22a4
 * @member:	the name of the list_struct within the struct.
Packit 7b22a4
 */
Packit 7b22a4
#define list_entry(ptr, type, member) \
Packit 7b22a4
	container_of(ptr, type, member)
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_for_each	-	iterate over a list
Packit 7b22a4
 * @pos:	the &struct list_head to use as a loop counter.
Packit 7b22a4
 * @head:	the head for your list.
Packit 7b22a4
 */
Packit 7b22a4
#define list_for_each(pos, head) \
Packit 7b22a4
	for (pos = (head)->next, prefetch(pos->next); pos != (head); \
Packit 7b22a4
        	pos = pos->next, prefetch(pos->next))
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * __list_for_each	-	iterate over a list
Packit 7b22a4
 * @pos:	the &struct list_head to use as a loop counter.
Packit 7b22a4
 * @head:	the head for your list.
Packit 7b22a4
 *
Packit 7b22a4
 * This variant differs from list_for_each() in that it's the
Packit 7b22a4
 * simplest possible list iteration code, no prefetching is done.
Packit 7b22a4
 * Use this for code that knows the list to be very short (empty
Packit 7b22a4
 * or 1 entry) most of the time.
Packit 7b22a4
 */
Packit 7b22a4
#define __list_for_each(pos, head) \
Packit 7b22a4
	for (pos = (head)->next; pos != (head); pos = pos->next)
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_for_each_prev	-	iterate over a list backwards
Packit 7b22a4
 * @pos:	the &struct list_head to use as a loop counter.
Packit 7b22a4
 * @head:	the head for your list.
Packit 7b22a4
 */
Packit 7b22a4
#define list_for_each_prev(pos, head) \
Packit 7b22a4
	for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
Packit 7b22a4
        	pos = pos->prev, prefetch(pos->prev))
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_for_each_safe	-	iterate over a list safe against removal of list entry
Packit 7b22a4
 * @pos:	the &struct list_head to use as a loop counter.
Packit 7b22a4
 * @n:		another &struct list_head to use as temporary storage
Packit 7b22a4
 * @head:	the head for your list.
Packit 7b22a4
 */
Packit 7b22a4
#define list_for_each_safe(pos, n, head) \
Packit 7b22a4
	for (pos = (head)->next, n = pos->next; pos != (head); \
Packit 7b22a4
		pos = n, n = pos->next)
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_for_each_entry	-	iterate over list of given type
Packit 7b22a4
 * @pos:	the type * to use as a loop counter.
Packit 7b22a4
 * @head:	the head for your list.
Packit 7b22a4
 * @member:	the name of the list_struct within the struct.
Packit 7b22a4
 */
Packit 7b22a4
#define list_for_each_entry(pos, head, member)				\
Packit 7b22a4
	for (pos = list_entry((head)->next, typeof(*pos), member),	\
Packit 7b22a4
		     prefetch(pos->member.next);			\
Packit 7b22a4
	     &pos->member != (head); 					\
Packit 7b22a4
	     pos = list_entry(pos->member.next, typeof(*pos), member),	\
Packit 7b22a4
		     prefetch(pos->member.next))
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_for_each_entry_reverse - iterate backwards over list of given type.
Packit 7b22a4
 * @pos:	the type * to use as a loop counter.
Packit 7b22a4
 * @head:	the head for your list.
Packit 7b22a4
 * @member:	the name of the list_struct within the struct.
Packit 7b22a4
 */
Packit 7b22a4
#define list_for_each_entry_reverse(pos, head, member)			\
Packit 7b22a4
	for (pos = list_entry((head)->prev, typeof(*pos), member),	\
Packit 7b22a4
		     prefetch(pos->member.prev);			\
Packit 7b22a4
	     &pos->member != (head); 					\
Packit 7b22a4
	     pos = list_entry(pos->member.prev, typeof(*pos), member),	\
Packit 7b22a4
		     prefetch(pos->member.prev))
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_prepare_entry - prepare a pos entry for use as a start point in
Packit 7b22a4
 *			list_for_each_entry_continue
Packit 7b22a4
 * @pos:	the type * to use as a start point
Packit 7b22a4
 * @head:	the head of the list
Packit 7b22a4
 * @member:	the name of the list_struct within the struct.
Packit 7b22a4
 */
Packit 7b22a4
#define list_prepare_entry(pos, head, member) \
Packit 7b22a4
	((pos) ? : list_entry(head, typeof(*pos), member))
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_for_each_entry_continue -	iterate over list of given type
Packit 7b22a4
 *			continuing after existing point
Packit 7b22a4
 * @pos:	the type * to use as a loop counter.
Packit 7b22a4
 * @head:	the head for your list.
Packit 7b22a4
 * @member:	the name of the list_struct within the struct.
Packit 7b22a4
 */
Packit 7b22a4
#define list_for_each_entry_continue(pos, head, member) 		\
Packit 7b22a4
	for (pos = list_entry(pos->member.next, typeof(*pos), member),	\
Packit 7b22a4
		     prefetch(pos->member.next);			\
Packit 7b22a4
	     &pos->member != (head);					\
Packit 7b22a4
	     pos = list_entry(pos->member.next, typeof(*pos), member),	\
Packit 7b22a4
		     prefetch(pos->member.next))
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Packit 7b22a4
 * @pos:	the type * to use as a loop counter.
Packit 7b22a4
 * @n:		another type * to use as temporary storage
Packit 7b22a4
 * @head:	the head for your list.
Packit 7b22a4
 * @member:	the name of the list_struct within the struct.
Packit 7b22a4
 */
Packit 7b22a4
#define list_for_each_entry_safe(pos, n, head, member)			\
Packit 7b22a4
	for (pos = list_entry((head)->next, typeof(*pos), member),	\
Packit 7b22a4
		n = list_entry(pos->member.next, typeof(*pos), member);	\
Packit 7b22a4
	     &pos->member != (head); 					\
Packit 7b22a4
	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_for_each_rcu	-	iterate over an rcu-protected list
Packit 7b22a4
 * @pos:	the &struct list_head to use as a loop counter.
Packit 7b22a4
 * @head:	the head for your list.
Packit 7b22a4
 *
Packit 7b22a4
 * This list-traversal primitive may safely run concurrently with
Packit 7b22a4
 * the _rcu list-mutation primitives such as list_add_rcu()
Packit 7b22a4
 * as long as the traversal is guarded by rcu_read_lock().
Packit 7b22a4
 */
Packit 7b22a4
#define list_for_each_rcu(pos, head) \
Packit 7b22a4
	for (pos = (head)->next, prefetch(pos->next); pos != (head); \
Packit 7b22a4
        	pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
Packit 7b22a4
Packit 7b22a4
#define __list_for_each_rcu(pos, head) \
Packit 7b22a4
	for (pos = (head)->next; pos != (head); \
Packit 7b22a4
        	pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_for_each_safe_rcu	-	iterate over an rcu-protected list safe
Packit 7b22a4
 *					against removal of list entry
Packit 7b22a4
 * @pos:	the &struct list_head to use as a loop counter.
Packit 7b22a4
 * @n:		another &struct list_head to use as temporary storage
Packit 7b22a4
 * @head:	the head for your list.
Packit 7b22a4
 *
Packit 7b22a4
 * This list-traversal primitive may safely run concurrently with
Packit 7b22a4
 * the _rcu list-mutation primitives such as list_add_rcu()
Packit 7b22a4
 * as long as the traversal is guarded by rcu_read_lock().
Packit 7b22a4
 */
Packit 7b22a4
#define list_for_each_safe_rcu(pos, n, head) \
Packit 7b22a4
	for (pos = (head)->next, n = pos->next; pos != (head); \
Packit 7b22a4
		pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_for_each_entry_rcu	-	iterate over rcu list of given type
Packit 7b22a4
 * @pos:	the type * to use as a loop counter.
Packit 7b22a4
 * @head:	the head for your list.
Packit 7b22a4
 * @member:	the name of the list_struct within the struct.
Packit 7b22a4
 *
Packit 7b22a4
 * This list-traversal primitive may safely run concurrently with
Packit 7b22a4
 * the _rcu list-mutation primitives such as list_add_rcu()
Packit 7b22a4
 * as long as the traversal is guarded by rcu_read_lock().
Packit 7b22a4
 */
Packit 7b22a4
#define list_for_each_entry_rcu(pos, head, member)			\
Packit 7b22a4
	for (pos = list_entry((head)->next, typeof(*pos), member),	\
Packit 7b22a4
		     prefetch(pos->member.next);			\
Packit 7b22a4
	     &pos->member != (head); 					\
Packit 7b22a4
	     pos = list_entry(pos->member.next, typeof(*pos), member),	\
Packit 7b22a4
		     ({ smp_read_barrier_depends(); 0;}),		\
Packit 7b22a4
		     prefetch(pos->member.next))
Packit 7b22a4
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * list_for_each_continue_rcu	-	iterate over an rcu-protected list
Packit 7b22a4
 *			continuing after existing point.
Packit 7b22a4
 * @pos:	the &struct list_head to use as a loop counter.
Packit 7b22a4
 * @head:	the head for your list.
Packit 7b22a4
 *
Packit 7b22a4
 * This list-traversal primitive may safely run concurrently with
Packit 7b22a4
 * the _rcu list-mutation primitives such as list_add_rcu()
Packit 7b22a4
 * as long as the traversal is guarded by rcu_read_lock().
Packit 7b22a4
 */
Packit 7b22a4
#define list_for_each_continue_rcu(pos, head) \
Packit 7b22a4
	for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
Packit 7b22a4
        	(pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
Packit 7b22a4
Packit 7b22a4
/*
Packit 7b22a4
 * Double linked lists with a single pointer list head.
Packit 7b22a4
 * Mostly useful for hash tables where the two pointer list head is
Packit 7b22a4
 * too wasteful.
Packit 7b22a4
 * You lose the ability to access the tail in O(1).
Packit 7b22a4
 */
Packit 7b22a4
Packit 7b22a4
struct hlist_head {
Packit 7b22a4
	struct hlist_node *first;
Packit 7b22a4
};
Packit 7b22a4
Packit 7b22a4
struct hlist_node {
Packit 7b22a4
	struct hlist_node *next, **pprev;
Packit 7b22a4
};
Packit 7b22a4
Packit 7b22a4
#define HLIST_HEAD_INIT { .first = NULL }
Packit 7b22a4
#define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
Packit 7b22a4
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
Packit 7b22a4
#define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
Packit 7b22a4
Packit 7b22a4
static inline int hlist_unhashed(const struct hlist_node *h)
Packit 7b22a4
{
Packit 7b22a4
	return !h->pprev;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
static inline int hlist_empty(const struct hlist_head *h)
Packit 7b22a4
{
Packit 7b22a4
	return !h->first;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
static inline void __hlist_del(struct hlist_node *n)
Packit 7b22a4
{
Packit 7b22a4
	struct hlist_node *next = n->next;
Packit 7b22a4
	struct hlist_node **pprev = n->pprev;
Packit 7b22a4
	*pprev = next;
Packit 7b22a4
	if (next)
Packit 7b22a4
		next->pprev = pprev;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
static inline void hlist_del(struct hlist_node *n)
Packit 7b22a4
{
Packit 7b22a4
	__hlist_del(n);
Packit 7b22a4
	n->next = LIST_POISON1;
Packit 7b22a4
	n->pprev = LIST_POISON2;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * hlist_del_rcu - deletes entry from hash list without re-initialization
Packit 7b22a4
 * @n: the element to delete from the hash list.
Packit 7b22a4
 *
Packit 7b22a4
 * Note: list_unhashed() on entry does not return true after this,
Packit 7b22a4
 * the entry is in an undefined state. It is useful for RCU based
Packit 7b22a4
 * lockfree traversal.
Packit 7b22a4
 *
Packit 7b22a4
 * In particular, it means that we can not poison the forward
Packit 7b22a4
 * pointers that may still be used for walking the hash list.
Packit 7b22a4
 *
Packit 7b22a4
 * The caller must take whatever precautions are necessary
Packit 7b22a4
 * (such as holding appropriate locks) to avoid racing
Packit 7b22a4
 * with another list-mutation primitive, such as hlist_add_head_rcu()
Packit 7b22a4
 * or hlist_del_rcu(), running on this same list.
Packit 7b22a4
 * However, it is perfectly legal to run concurrently with
Packit 7b22a4
 * the _rcu list-traversal primitives, such as
Packit 7b22a4
 * hlist_for_each_entry().
Packit 7b22a4
 */
Packit 7b22a4
static inline void hlist_del_rcu(struct hlist_node *n)
Packit 7b22a4
{
Packit 7b22a4
	__hlist_del(n);
Packit 7b22a4
	n->pprev = LIST_POISON2;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
static inline void hlist_del_init(struct hlist_node *n)
Packit 7b22a4
{
Packit 7b22a4
	if (n->pprev)  {
Packit 7b22a4
		__hlist_del(n);
Packit 7b22a4
		INIT_HLIST_NODE(n);
Packit 7b22a4
	}
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
#define hlist_del_rcu_init hlist_del_init
Packit 7b22a4
Packit 7b22a4
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
Packit 7b22a4
{
Packit 7b22a4
	struct hlist_node *first = h->first;
Packit 7b22a4
	n->next = first;
Packit 7b22a4
	if (first)
Packit 7b22a4
		first->pprev = &n->next;
Packit 7b22a4
	h->first = n;
Packit 7b22a4
	n->pprev = &h->first;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * hlist_add_head_rcu - adds the specified element to the specified hlist,
Packit 7b22a4
 * while permitting racing traversals.
Packit 7b22a4
 * @n: the element to add to the hash list.
Packit 7b22a4
 * @h: the list to add to.
Packit 7b22a4
 *
Packit 7b22a4
 * The caller must take whatever precautions are necessary
Packit 7b22a4
 * (such as holding appropriate locks) to avoid racing
Packit 7b22a4
 * with another list-mutation primitive, such as hlist_add_head_rcu()
Packit 7b22a4
 * or hlist_del_rcu(), running on this same list.
Packit 7b22a4
 * However, it is perfectly legal to run concurrently with
Packit 7b22a4
 * the _rcu list-traversal primitives, such as
Packit 7b22a4
 * hlist_for_each_entry(), but only if smp_read_barrier_depends()
Packit 7b22a4
 * is used to prevent memory-consistency problems on Alpha CPUs.
Packit 7b22a4
 * Regardless of the type of CPU, the list-traversal primitive
Packit 7b22a4
 * must be guarded by rcu_read_lock().
Packit 7b22a4
 *
Packit 7b22a4
 * OK, so why don't we have an hlist_for_each_entry_rcu()???
Packit 7b22a4
 */
Packit 7b22a4
static inline void hlist_add_head_rcu(struct hlist_node *n,
Packit 7b22a4
					struct hlist_head *h)
Packit 7b22a4
{
Packit 7b22a4
	struct hlist_node *first = h->first;
Packit 7b22a4
	n->next = first;
Packit 7b22a4
	n->pprev = &h->first;
Packit 7b22a4
	smp_wmb();
Packit 7b22a4
	if (first)
Packit 7b22a4
		first->pprev = &n->next;
Packit 7b22a4
	h->first = n;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
/* next must be != NULL */
Packit 7b22a4
static inline void hlist_add_before(struct hlist_node *n,
Packit 7b22a4
					struct hlist_node *next)
Packit 7b22a4
{
Packit 7b22a4
	n->pprev = next->pprev;
Packit 7b22a4
	n->next = next;
Packit 7b22a4
	next->pprev = &n->next;
Packit 7b22a4
	*(n->pprev) = n;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
static inline void hlist_add_after(struct hlist_node *n,
Packit 7b22a4
					struct hlist_node *next)
Packit 7b22a4
{
Packit 7b22a4
	next->next = n->next;
Packit 7b22a4
	n->next = next;
Packit 7b22a4
	next->pprev = &n->next;
Packit 7b22a4
Packit 7b22a4
	if(next->next)
Packit 7b22a4
		next->next->pprev  = &next->next;
Packit 7b22a4
}
Packit 7b22a4
Packit 7b22a4
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
Packit 7b22a4
Packit 7b22a4
#define hlist_for_each(pos, head) \
Packit 7b22a4
	for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
Packit 7b22a4
	     pos = pos->next)
Packit 7b22a4
Packit 7b22a4
#define hlist_for_each_safe(pos, n, head) \
Packit 7b22a4
	for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
Packit 7b22a4
	     pos = n)
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * hlist_for_each_entry	- iterate over list of given type
Packit 7b22a4
 * @tpos:	the type * to use as a loop counter.
Packit 7b22a4
 * @pos:	the &struct hlist_node to use as a loop counter.
Packit 7b22a4
 * @head:	the head for your list.
Packit 7b22a4
 * @member:	the name of the hlist_node within the struct.
Packit 7b22a4
 */
Packit 7b22a4
#define hlist_for_each_entry(tpos, pos, head, member)			 \
Packit 7b22a4
	for (pos = (head)->first;					 \
Packit 7b22a4
	     pos && ({ prefetch(pos->next); 1;}) &&			 \
Packit 7b22a4
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
Packit 7b22a4
	     pos = pos->next)
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
Packit 7b22a4
 * @tpos:	the type * to use as a loop counter.
Packit 7b22a4
 * @pos:	the &struct hlist_node to use as a loop counter.
Packit 7b22a4
 * @member:	the name of the hlist_node within the struct.
Packit 7b22a4
 */
Packit 7b22a4
#define hlist_for_each_entry_continue(tpos, pos, member)		 \
Packit 7b22a4
	for (pos = (pos)->next;						 \
Packit 7b22a4
	     pos && ({ prefetch(pos->next); 1;}) &&			 \
Packit 7b22a4
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
Packit 7b22a4
	     pos = pos->next)
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
Packit 7b22a4
 * @tpos:	the type * to use as a loop counter.
Packit 7b22a4
 * @pos:	the &struct hlist_node to use as a loop counter.
Packit 7b22a4
 * @member:	the name of the hlist_node within the struct.
Packit 7b22a4
 */
Packit 7b22a4
#define hlist_for_each_entry_from(tpos, pos, member)			 \
Packit 7b22a4
	for (; pos && ({ prefetch(pos->next); 1;}) &&			 \
Packit 7b22a4
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
Packit 7b22a4
	     pos = pos->next)
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Packit 7b22a4
 * @tpos:	the type * to use as a loop counter.
Packit 7b22a4
 * @pos:	the &struct hlist_node to use as a loop counter.
Packit 7b22a4
 * @n:		another &struct hlist_node to use as temporary storage
Packit 7b22a4
 * @head:	the head for your list.
Packit 7b22a4
 * @member:	the name of the hlist_node within the struct.
Packit 7b22a4
 */
Packit 7b22a4
#define hlist_for_each_entry_safe(tpos, pos, n, head, member) 		 \
Packit 7b22a4
	for (pos = (head)->first;					 \
Packit 7b22a4
	     pos && ({ n = pos->next; 1; }) && 				 \
Packit 7b22a4
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
Packit 7b22a4
	     pos = n)
Packit 7b22a4
Packit 7b22a4
/**
Packit 7b22a4
 * hlist_for_each_entry_rcu - iterate over rcu list of given type
Packit 7b22a4
 * @pos:	the type * to use as a loop counter.
Packit 7b22a4
 * @pos:	the &struct hlist_node to use as a loop counter.
Packit 7b22a4
 * @head:	the head for your list.
Packit 7b22a4
 * @member:	the name of the hlist_node within the struct.
Packit 7b22a4
 *
Packit 7b22a4
 * This list-traversal primitive may safely run concurrently with
Packit 7b22a4
 * the _rcu list-mutation primitives such as hlist_add_rcu()
Packit 7b22a4
 * as long as the traversal is guarded by rcu_read_lock().
Packit 7b22a4
 */
Packit 7b22a4
#define hlist_for_each_entry_rcu(tpos, pos, head, member)		 \
Packit 7b22a4
	for (pos = (head)->first;					 \
Packit 7b22a4
	     pos && ({ prefetch(pos->next); 1;}) &&			 \
Packit 7b22a4
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
Packit 7b22a4
	     pos = pos->next, ({ smp_read_barrier_depends(); 0; }) )
Packit 7b22a4
Packit 7b22a4
#endif