Blame include/list.h

Packit 8480eb
#ifndef _LINUX_LIST_H
Packit 8480eb
#define _LINUX_LIST_H
Packit 8480eb
Packit 8480eb
/*
Packit 8480eb
 * Simple doubly linked list implementation.
Packit 8480eb
 *
Packit 8480eb
 * Some of the internal functions ("__xxx") are useful when
Packit 8480eb
 * manipulating whole lists rather than single entries, as
Packit 8480eb
 * sometimes we already know the next/prev entries and we can
Packit 8480eb
 * generate better code by using them directly rather than
Packit 8480eb
 * using the generic single-entry routines.
Packit 8480eb
 */
Packit 8480eb
Packit 8480eb
struct list_head {
Packit 8480eb
	struct list_head *next, *prev;
Packit 8480eb
};
Packit 8480eb
Packit 8480eb
#define LIST_HEAD_INIT(name) { &(name), &(name) }
Packit 8480eb
Packit 8480eb
#define LIST_HEAD(name) \
Packit 8480eb
	struct list_head name = LIST_HEAD_INIT(name)
Packit 8480eb
Packit 8480eb
#define INIT_LIST_HEAD(ptr) do { \
Packit 8480eb
	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
Packit 8480eb
} while (0)
Packit 8480eb
Packit 8480eb
/*
Packit 8480eb
 * Insert a new entry between two known consecutive entries. 
Packit 8480eb
 *
Packit 8480eb
 * This is only for internal list manipulation where we know
Packit 8480eb
 * the prev/next entries already!
Packit 8480eb
 */
Packit 8480eb
static __inline__ void __list_add(struct list_head * new,
Packit 8480eb
	struct list_head * prev,
Packit 8480eb
	struct list_head * next)
Packit 8480eb
{
Packit 8480eb
	next->prev = new;
Packit 8480eb
	new->next = next;
Packit 8480eb
	new->prev = prev;
Packit 8480eb
	prev->next = new;
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
/**
Packit 8480eb
 * list_add - add a new entry
Packit 8480eb
 * @new: new entry to be added
Packit 8480eb
 * @head: list head to add it after
Packit 8480eb
 *
Packit 8480eb
 * Insert a new entry after the specified head.
Packit 8480eb
 * This is good for implementing stacks.
Packit 8480eb
 */
Packit 8480eb
static __inline__ void list_add(struct list_head *new, struct list_head *head)
Packit 8480eb
{
Packit 8480eb
	__list_add(new, head, head->next);
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
/**
Packit 8480eb
 * list_add_tail - add a new entry
Packit 8480eb
 * @new: new entry to be added
Packit 8480eb
 * @head: list head to add it before
Packit 8480eb
 *
Packit 8480eb
 * Insert a new entry before the specified head.
Packit 8480eb
 * This is useful for implementing queues.
Packit 8480eb
 */
Packit 8480eb
static __inline__ void list_add_tail(struct list_head *new, struct list_head *head)
Packit 8480eb
{
Packit 8480eb
	__list_add(new, head->prev, head);
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
/*
Packit 8480eb
 * Delete a list entry by making the prev/next entries
Packit 8480eb
 * point to each other.
Packit 8480eb
 *
Packit 8480eb
 * This is only for internal list manipulation where we know
Packit 8480eb
 * the prev/next entries already!
Packit 8480eb
 */
Packit 8480eb
static __inline__ void __list_del(struct list_head * prev,
Packit 8480eb
				  struct list_head * next)
Packit 8480eb
{
Packit 8480eb
	next->prev = prev;
Packit 8480eb
	prev->next = next;
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
/**
Packit 8480eb
 * list_del - deletes entry from list.
Packit 8480eb
 * @entry: the element to delete from the list.
Packit 8480eb
 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
Packit 8480eb
 */
Packit 8480eb
static __inline__ void list_del(struct list_head *entry)
Packit 8480eb
{
Packit 8480eb
	__list_del(entry->prev, entry->next);
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
/**
Packit 8480eb
 * list_del_init - deletes entry from list and reinitialize it.
Packit 8480eb
 * @entry: the element to delete from the list.
Packit 8480eb
 */
Packit 8480eb
static __inline__ void list_del_init(struct list_head *entry)
Packit 8480eb
{
Packit 8480eb
	__list_del(entry->prev, entry->next);
Packit 8480eb
	INIT_LIST_HEAD(entry); 
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
/**
Packit 8480eb
 * list_empty - tests whether a list is empty
Packit 8480eb
 * @head: the list to test.
Packit 8480eb
 */
Packit 8480eb
static __inline__ int list_empty(struct list_head *head)
Packit 8480eb
{
Packit 8480eb
	return head->next == head;
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
/**
Packit 8480eb
 * list_splice - join two lists
Packit 8480eb
 * @list: the new list to add.
Packit 8480eb
 * @head: the place to add it in the first list.
Packit 8480eb
 */
Packit 8480eb
static __inline__ void list_splice(struct list_head *list, struct list_head *head)
Packit 8480eb
{
Packit 8480eb
	struct list_head *first = list->next;
Packit 8480eb
Packit 8480eb
	if (first != list) {
Packit 8480eb
		struct list_head *last = list->prev;
Packit 8480eb
		struct list_head *at = head->next;
Packit 8480eb
Packit 8480eb
		first->prev = head;
Packit 8480eb
		head->next = first;
Packit 8480eb
Packit 8480eb
		last->next = at;
Packit 8480eb
		at->prev = last;
Packit 8480eb
	}
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
/**
Packit 8480eb
 * list_entry - get the struct for this entry
Packit 8480eb
 * @ptr:	the &struct list_head pointer.
Packit 8480eb
 * @type:	the type of the struct this is embedded in.
Packit 8480eb
 * @member:	the name of the list_struct within the struct.
Packit 8480eb
 */
Packit 8480eb
#define list_entry(ptr, type, member) \
Packit 8480eb
	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
Packit 8480eb
Packit 8480eb
/**
Packit 8480eb
 * list_for_each	-	iterate over a list
Packit 8480eb
 * @pos:	the &struct list_head to use as a loop counter.
Packit 8480eb
 * @head:	the head for your list.
Packit 8480eb
 */
Packit 8480eb
#define list_for_each(pos, head) \
Packit 8480eb
	for (pos = (head)->next; pos != (head); pos = pos->next)
Packit 8480eb
Packit 8480eb
/**
Packit 8480eb
 * list_for_each_prev	-	iterate over a list in reverse
Packit 8480eb
 * @pos:	the &struct list_head to use as a loop counter.
Packit 8480eb
 * @head:	the head for your list.
Packit 8480eb
 */
Packit 8480eb
#define list_for_each_prev(pos, head) \
Packit 8480eb
	for (pos = (head)->prev; pos != (head); pos = pos->prev)
Packit 8480eb
Packit 8480eb
#endif