Blame alsactl/list.h

Packit Service a9274b
/*
Packit Service a9274b
 * Copied from the Linux kernel source tree, version 2.6.0-test1.
Packit Service a9274b
 *
Packit Service a9274b
 * Licensed under the GPL v2 as per the whole kernel source tree.
Packit Service a9274b
 *
Packit Service a9274b
 */
Packit Service a9274b
Packit Service a9274b
#ifndef _LIST_H
Packit Service a9274b
#define _LIST_H
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * container_of - cast a member of a structure out to the containing structure
Packit Service a9274b
 *
Packit Service a9274b
 * @ptr:	the pointer to the member.
Packit Service a9274b
 * @type:	the type of the container struct this is embedded in.
Packit Service a9274b
 * @member:	the name of the member within the struct.
Packit Service a9274b
 *
Packit Service a9274b
 */
Packit Service a9274b
#define container_of(ptr, type, member) ({			\
Packit Service a9274b
	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
Packit Service a9274b
	(type *)( (char *)__mptr - offsetof(type,member) );})
Packit Service a9274b
Packit Service a9274b
/*
Packit Service a9274b
 * These are non-NULL pointers that will result in page faults
Packit Service a9274b
 * under normal circumstances, used to verify that nobody uses
Packit Service a9274b
 * non-initialized list entries.
Packit Service a9274b
 */
Packit Service a9274b
#define LIST_POISON1  ((void *) 0x00100100)
Packit Service a9274b
#define LIST_POISON2  ((void *) 0x00200200)
Packit Service a9274b
Packit Service a9274b
/*
Packit Service a9274b
 * Simple doubly linked list implementation.
Packit Service a9274b
 *
Packit Service a9274b
 * Some of the internal functions ("__xxx") are useful when
Packit Service a9274b
 * manipulating whole lists rather than single entries, as
Packit Service a9274b
 * sometimes we already know the next/prev entries and we can
Packit Service a9274b
 * generate better code by using them directly rather than
Packit Service a9274b
 * using the generic single-entry routines.
Packit Service a9274b
 */
Packit Service a9274b
Packit Service a9274b
struct list_head {
Packit Service a9274b
	struct list_head *next, *prev;
Packit Service a9274b
};
Packit Service a9274b
Packit Service a9274b
#define LIST_HEAD_INIT(name) { &(name), &(name) }
Packit Service a9274b
Packit Service a9274b
#define LIST_HEAD(name) \
Packit Service a9274b
	struct list_head name = LIST_HEAD_INIT(name)
Packit Service a9274b
Packit Service a9274b
#define INIT_LIST_HEAD(ptr) do { \
Packit Service a9274b
	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
Packit Service a9274b
} while (0)
Packit Service a9274b
Packit Service a9274b
/*
Packit Service a9274b
 * Insert a new entry between two known consecutive entries. 
Packit Service a9274b
 *
Packit Service a9274b
 * This is only for internal list manipulation where we know
Packit Service a9274b
 * the prev/next entries already!
Packit Service a9274b
 */
Packit Service a9274b
static inline void __list_add(struct list_head *new,
Packit Service a9274b
			      struct list_head *prev,
Packit Service a9274b
			      struct list_head *next)
Packit Service a9274b
{
Packit Service a9274b
	next->prev = new;
Packit Service a9274b
	new->next = next;
Packit Service a9274b
	new->prev = prev;
Packit Service a9274b
	prev->next = new;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_add - add a new entry
Packit Service a9274b
 * @new: new entry to be added
Packit Service a9274b
 * @head: list head to add it after
Packit Service a9274b
 *
Packit Service a9274b
 * Insert a new entry after the specified head.
Packit Service a9274b
 * This is good for implementing stacks.
Packit Service a9274b
 */
Packit Service a9274b
static inline void list_add(struct list_head *new, struct list_head *head)
Packit Service a9274b
{
Packit Service a9274b
	__list_add(new, head, head->next);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_add_tail - add a new entry
Packit Service a9274b
 * @new: new entry to be added
Packit Service a9274b
 * @head: list head to add it before
Packit Service a9274b
 *
Packit Service a9274b
 * Insert a new entry before the specified head.
Packit Service a9274b
 * This is useful for implementing queues.
Packit Service a9274b
 */
Packit Service a9274b
static inline void list_add_tail(struct list_head *new, struct list_head *head)
Packit Service a9274b
{
Packit Service a9274b
	__list_add(new, head->prev, head);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/*
Packit Service a9274b
 * Delete a list entry by making the prev/next entries
Packit Service a9274b
 * point to each other.
Packit Service a9274b
 *
Packit Service a9274b
 * This is only for internal list manipulation where we know
Packit Service a9274b
 * the prev/next entries already!
Packit Service a9274b
 */
Packit Service a9274b
static inline void __list_del(struct list_head * prev, struct list_head * next)
Packit Service a9274b
{
Packit Service a9274b
	next->prev = prev;
Packit Service a9274b
	prev->next = next;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_del - deletes entry from list.
Packit Service a9274b
 * @entry: the element to delete from the list.
Packit Service a9274b
 * Note: list_empty on entry does not return true after this, the entry is
Packit Service a9274b
 * in an undefined state.
Packit Service a9274b
 */
Packit Service a9274b
static inline void list_del(struct list_head *entry)
Packit Service a9274b
{
Packit Service a9274b
	__list_del(entry->prev, entry->next);
Packit Service a9274b
	entry->next = LIST_POISON1;
Packit Service a9274b
	entry->prev = LIST_POISON2;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_del_init - deletes entry from list and reinitialize it.
Packit Service a9274b
 * @entry: the element to delete from the list.
Packit Service a9274b
 */
Packit Service a9274b
static inline void list_del_init(struct list_head *entry)
Packit Service a9274b
{
Packit Service a9274b
	__list_del(entry->prev, entry->next);
Packit Service a9274b
	INIT_LIST_HEAD(entry); 
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_move - delete from one list and add as another's head
Packit Service a9274b
 * @list: the entry to move
Packit Service a9274b
 * @head: the head that will precede our entry
Packit Service a9274b
 */
Packit Service a9274b
static inline void list_move(struct list_head *list, struct list_head *head)
Packit Service a9274b
{
Packit Service a9274b
	__list_del(list->prev, list->next);
Packit Service a9274b
	list_add(list, head);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_move_tail - delete from one list and add as another's tail
Packit Service a9274b
 * @list: the entry to move
Packit Service a9274b
 * @head: the head that will follow our entry
Packit Service a9274b
 */
Packit Service a9274b
static inline void list_move_tail(struct list_head *list,
Packit Service a9274b
				  struct list_head *head)
Packit Service a9274b
{
Packit Service a9274b
	__list_del(list->prev, list->next);
Packit Service a9274b
	list_add_tail(list, head);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_empty - tests whether a list is empty
Packit Service a9274b
 * @head: the list to test.
Packit Service a9274b
 */
Packit Service a9274b
static inline int list_empty(struct list_head *head)
Packit Service a9274b
{
Packit Service a9274b
	return head->next == head;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
static inline void __list_splice(struct list_head *list,
Packit Service a9274b
				 struct list_head *head)
Packit Service a9274b
{
Packit Service a9274b
	struct list_head *first = list->next;
Packit Service a9274b
	struct list_head *last = list->prev;
Packit Service a9274b
	struct list_head *at = head->next;
Packit Service a9274b
Packit Service a9274b
	first->prev = head;
Packit Service a9274b
	head->next = first;
Packit Service a9274b
Packit Service a9274b
	last->next = at;
Packit Service a9274b
	at->prev = last;
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_splice - join two lists
Packit Service a9274b
 * @list: the new list to add.
Packit Service a9274b
 * @head: the place to add it in the first list.
Packit Service a9274b
 */
Packit Service a9274b
static inline void list_splice(struct list_head *list, struct list_head *head)
Packit Service a9274b
{
Packit Service a9274b
	if (!list_empty(list))
Packit Service a9274b
		__list_splice(list, head);
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_splice_init - join two lists and reinitialise the emptied list.
Packit Service a9274b
 * @list: the new list to add.
Packit Service a9274b
 * @head: the place to add it in the first list.
Packit Service a9274b
 *
Packit Service a9274b
 * The list at @list is reinitialised
Packit Service a9274b
 */
Packit Service a9274b
static inline void list_splice_init(struct list_head *list,
Packit Service a9274b
				    struct list_head *head)
Packit Service a9274b
{
Packit Service a9274b
	if (!list_empty(list)) {
Packit Service a9274b
		__list_splice(list, head);
Packit Service a9274b
		INIT_LIST_HEAD(list);
Packit Service a9274b
	}
Packit Service a9274b
}
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_entry - get the struct for this entry
Packit Service a9274b
 * @ptr:	the &struct list_head pointer.
Packit Service a9274b
 * @type:	the type of the struct this is embedded in.
Packit Service a9274b
 * @member:	the name of the list_struct within the struct.
Packit Service a9274b
 */
Packit Service a9274b
#define list_entry(ptr, type, member) \
Packit Service a9274b
	container_of(ptr, type, member)
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_for_each	-	iterate over a list
Packit Service a9274b
 * @pos:	the &struct list_head to use as a loop counter.
Packit Service a9274b
 * @head:	the head for your list.
Packit Service a9274b
 */
Packit Service a9274b
#define list_for_each(pos, head) \
Packit Service a9274b
	for (pos = (head)->next; pos != (head); \
Packit Service a9274b
		pos = pos->next)
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * __list_for_each	-	iterate over a list
Packit Service a9274b
 * @pos:	the &struct list_head to use as a loop counter.
Packit Service a9274b
 * @head:	the head for your list.
Packit Service a9274b
 *
Packit Service a9274b
 * This variant differs from list_for_each() in that it's the
Packit Service a9274b
 * simplest possible list iteration code.
Packit Service a9274b
 * Use this for code that knows the list to be very short (empty
Packit Service a9274b
 * or 1 entry) most of the time.
Packit Service a9274b
 */
Packit Service a9274b
#define __list_for_each(pos, head) \
Packit Service a9274b
	for (pos = (head)->next; pos != (head); pos = pos->next)
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_for_each_prev	-	iterate over a list backwards
Packit Service a9274b
 * @pos:	the &struct list_head to use as a loop counter.
Packit Service a9274b
 * @head:	the head for your list.
Packit Service a9274b
 */
Packit Service a9274b
#define list_for_each_prev(pos, head) \
Packit Service a9274b
	for (pos = (head)->prev; pos != (head); pos = pos->prev)
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_for_each_safe	-	iterate over a list safe against removal of list entry
Packit Service a9274b
 * @pos:	the &struct list_head to use as a loop counter.
Packit Service a9274b
 * @n:		another &struct list_head to use as temporary storage
Packit Service a9274b
 * @head:	the head for your list.
Packit Service a9274b
 */
Packit Service a9274b
#define list_for_each_safe(pos, n, head) \
Packit Service a9274b
	for (pos = (head)->next, n = pos->next; pos != (head); \
Packit Service a9274b
		pos = n, n = pos->next)
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_for_each_entry	-	iterate over list of given type
Packit Service a9274b
 * @pos:	the type * to use as a loop counter.
Packit Service a9274b
 * @head:	the head for your list.
Packit Service a9274b
 * @member:	the name of the list_struct within the struct.
Packit Service a9274b
 */
Packit Service a9274b
#define list_for_each_entry(pos, head, member)				\
Packit Service a9274b
	for (pos = list_entry((head)->next, typeof(*pos), member);	\
Packit Service a9274b
	     &pos->member != (head); 					\
Packit Service a9274b
	     pos = list_entry(pos->member.next, typeof(*pos), member))
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_for_each_entry_reverse - iterate backwards over list of given type.
Packit Service a9274b
 * @pos:	the type * to use as a loop counter.
Packit Service a9274b
 * @head:	the head for your list.
Packit Service a9274b
 * @member:	the name of the list_struct within the struct.
Packit Service a9274b
 */
Packit Service a9274b
#define list_for_each_entry_reverse(pos, head, member)			\
Packit Service a9274b
	for (pos = list_entry((head)->prev, typeof(*pos), member);	\
Packit Service a9274b
	     &pos->member != (head); 					\
Packit Service a9274b
	     pos = list_entry(pos->member.prev, typeof(*pos), member))
Packit Service a9274b
Packit Service a9274b
/**
Packit Service a9274b
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Packit Service a9274b
 * @pos:	the type * to use as a loop counter.
Packit Service a9274b
 * @n:		another type * to use as temporary storage
Packit Service a9274b
 * @head:	the head for your list.
Packit Service a9274b
 * @member:	the name of the list_struct within the struct.
Packit Service a9274b
 */
Packit Service a9274b
#define list_for_each_entry_safe(pos, n, head, member)			\
Packit Service a9274b
	for (pos = list_entry((head)->next, typeof(*pos), member),	\
Packit Service a9274b
		n = list_entry(pos->member.next, typeof(*pos), member);	\
Packit Service a9274b
	     &pos->member != (head); 					\
Packit Service a9274b
	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
Packit Service a9274b
Packit Service a9274b
#endif /* _LIST_H */