Blame maemo/list.h

Packit Service cd2a00
#ifndef __LIST_H
Packit Service cd2a00
#define __LIST_H
Packit Service cd2a00
Packit Service cd2a00
/* This file is from Linux Kernel (include/linux/list.h) 
Packit Service cd2a00
 * and modified by simply removing hardware prefetching of list items. 
Packit Service cd2a00
 * Here by copyright, credits attributed to wherever they belong.
Packit Service cd2a00
 * Kulesh Shanmugasundaram (kulesh [squiggly] isis.poly.edu)
Packit Service cd2a00
 */
Packit Service cd2a00
Packit Service cd2a00
/*
Packit Service cd2a00
 * Simple doubly linked list implementation.
Packit Service cd2a00
 *
Packit Service cd2a00
 * Some of the internal functions ("__xxx") are useful when
Packit Service cd2a00
 * manipulating whole lists rather than single entries, as
Packit Service cd2a00
 * sometimes we already know the next/prev entries and we can
Packit Service cd2a00
 * generate better code by using them directly rather than
Packit Service cd2a00
 * using the generic single-entry routines.
Packit Service cd2a00
 */
Packit Service cd2a00
Packit Service cd2a00
struct list_head {
Packit Service cd2a00
	struct list_head *next, *prev;
Packit Service cd2a00
};
Packit Service cd2a00
Packit Service cd2a00
#define LIST_HEAD_INIT(name) { &(name), &(name) }
Packit Service cd2a00
Packit Service cd2a00
#define LIST_HEAD(name) \
Packit Service cd2a00
	struct list_head name = LIST_HEAD_INIT(name)
Packit Service cd2a00
Packit Service cd2a00
#define INIT_LIST_HEAD(ptr) do { \
Packit Service cd2a00
	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
Packit Service cd2a00
} while (0)
Packit Service cd2a00
Packit Service cd2a00
/*
Packit Service cd2a00
 * Insert a new entry between two known consecutive entries. 
Packit Service cd2a00
 *
Packit Service cd2a00
 * This is only for internal list manipulation where we know
Packit Service cd2a00
 * the prev/next entries already!
Packit Service cd2a00
 */
Packit Service cd2a00
static inline void __list_add(struct list_head *new,
Packit Service cd2a00
			      struct list_head *prev,
Packit Service cd2a00
			      struct list_head *next)
Packit Service cd2a00
{
Packit Service cd2a00
	next->prev = new;
Packit Service cd2a00
	new->next = next;
Packit Service cd2a00
	new->prev = prev;
Packit Service cd2a00
	prev->next = new;
Packit Service cd2a00
}
Packit Service cd2a00
Packit Service cd2a00
/**
Packit Service cd2a00
 * list_add - add a new entry
Packit Service cd2a00
 * @new: new entry to be added
Packit Service cd2a00
 * @head: list head to add it after
Packit Service cd2a00
 *
Packit Service cd2a00
 * Insert a new entry after the specified head.
Packit Service cd2a00
 * This is good for implementing stacks.
Packit Service cd2a00
 */
Packit Service cd2a00
static inline void list_add(struct list_head *new, struct list_head *head)
Packit Service cd2a00
{
Packit Service cd2a00
	__list_add(new, head, head->next);
Packit Service cd2a00
}
Packit Service cd2a00
Packit Service cd2a00
/**
Packit Service cd2a00
 * list_add_tail - add a new entry
Packit Service cd2a00
 * @new: new entry to be added
Packit Service cd2a00
 * @head: list head to add it before
Packit Service cd2a00
 *
Packit Service cd2a00
 * Insert a new entry before the specified head.
Packit Service cd2a00
 * This is useful for implementing queues.
Packit Service cd2a00
 */
Packit Service cd2a00
static inline void list_add_tail(struct list_head *new, struct list_head *head)
Packit Service cd2a00
{
Packit Service cd2a00
	__list_add(new, head->prev, head);
Packit Service cd2a00
}
Packit Service cd2a00
Packit Service cd2a00
/*
Packit Service cd2a00
 * Delete a list entry by making the prev/next entries
Packit Service cd2a00
 * point to each other.
Packit Service cd2a00
 *
Packit Service cd2a00
 * This is only for internal list manipulation where we know
Packit Service cd2a00
 * the prev/next entries already!
Packit Service cd2a00
 */
Packit Service cd2a00
static inline void __list_del(struct list_head *prev, struct list_head *next)
Packit Service cd2a00
{
Packit Service cd2a00
	next->prev = prev;
Packit Service cd2a00
	prev->next = next;
Packit Service cd2a00
}
Packit Service cd2a00
Packit Service cd2a00
/**
Packit Service cd2a00
 * list_del - deletes entry from list.
Packit Service cd2a00
 * @entry: the element to delete from the list.
Packit Service cd2a00
 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
Packit Service cd2a00
 */
Packit Service cd2a00
static inline void list_del(struct list_head *entry)
Packit Service cd2a00
{
Packit Service cd2a00
	__list_del(entry->prev, entry->next);
Packit Service cd2a00
	entry->next = (void *) 0;
Packit Service cd2a00
	entry->prev = (void *) 0;
Packit Service cd2a00
}
Packit Service cd2a00
Packit Service cd2a00
/**
Packit Service cd2a00
 * list_del_init - deletes entry from list and reinitialize it.
Packit Service cd2a00
 * @entry: the element to delete from the list.
Packit Service cd2a00
 */
Packit Service cd2a00
static inline void list_del_init(struct list_head *entry)
Packit Service cd2a00
{
Packit Service cd2a00
	__list_del(entry->prev, entry->next);
Packit Service cd2a00
	INIT_LIST_HEAD(entry); 
Packit Service cd2a00
}
Packit Service cd2a00
Packit Service cd2a00
/**
Packit Service cd2a00
 * list_move - delete from one list and add as another's head
Packit Service cd2a00
 * @list: the entry to move
Packit Service cd2a00
 * @head: the head that will precede our entry
Packit Service cd2a00
 */
Packit Service cd2a00
static inline void list_move(struct list_head *list, struct list_head *head)
Packit Service cd2a00
{
Packit Service cd2a00
        __list_del(list->prev, list->next);
Packit Service cd2a00
        list_add(list, head);
Packit Service cd2a00
}
Packit Service cd2a00
Packit Service cd2a00
/**
Packit Service cd2a00
 * list_move_tail - delete from one list and add as another's tail
Packit Service cd2a00
 * @list: the entry to move
Packit Service cd2a00
 * @head: the head that will follow our entry
Packit Service cd2a00
 */
Packit Service cd2a00
static inline void list_move_tail(struct list_head *list,
Packit Service cd2a00
				  struct list_head *head)
Packit Service cd2a00
{
Packit Service cd2a00
        __list_del(list->prev, list->next);
Packit Service cd2a00
        list_add_tail(list, head);
Packit Service cd2a00
}
Packit Service cd2a00
Packit Service cd2a00
/**
Packit Service cd2a00
 * list_empty - tests whether a list is empty
Packit Service cd2a00
 * @head: the list to test.
Packit Service cd2a00
 */
Packit Service cd2a00
static inline int list_empty(struct list_head *head)
Packit Service cd2a00
{
Packit Service cd2a00
	return head->next == head;
Packit Service cd2a00
}
Packit Service cd2a00
Packit Service cd2a00
static inline void __list_splice(struct list_head *list,
Packit Service cd2a00
				 struct list_head *head)
Packit Service cd2a00
{
Packit Service cd2a00
	struct list_head *first = list->next;
Packit Service cd2a00
	struct list_head *last = list->prev;
Packit Service cd2a00
	struct list_head *at = head->next;
Packit Service cd2a00
Packit Service cd2a00
	first->prev = head;
Packit Service cd2a00
	head->next = first;
Packit Service cd2a00
Packit Service cd2a00
	last->next = at;
Packit Service cd2a00
	at->prev = last;
Packit Service cd2a00
}
Packit Service cd2a00
Packit Service cd2a00
/**
Packit Service cd2a00
 * list_splice - join two lists
Packit Service cd2a00
 * @list: the new list to add.
Packit Service cd2a00
 * @head: the place to add it in the first list.
Packit Service cd2a00
 */
Packit Service cd2a00
static inline void list_splice(struct list_head *list, struct list_head *head)
Packit Service cd2a00
{
Packit Service cd2a00
	if (!list_empty(list))
Packit Service cd2a00
		__list_splice(list, head);
Packit Service cd2a00
}
Packit Service cd2a00
Packit Service cd2a00
/**
Packit Service cd2a00
 * list_splice_init - join two lists and reinitialise the emptied list.
Packit Service cd2a00
 * @list: the new list to add.
Packit Service cd2a00
 * @head: the place to add it in the first list.
Packit Service cd2a00
 *
Packit Service cd2a00
 * The list at @list is reinitialised
Packit Service cd2a00
 */
Packit Service cd2a00
static inline void list_splice_init(struct list_head *list,
Packit Service cd2a00
				    struct list_head *head)
Packit Service cd2a00
{
Packit Service cd2a00
	if (!list_empty(list)) {
Packit Service cd2a00
		__list_splice(list, head);
Packit Service cd2a00
		INIT_LIST_HEAD(list);
Packit Service cd2a00
	}
Packit Service cd2a00
}
Packit Service cd2a00
Packit Service cd2a00
/**
Packit Service cd2a00
 * list_entry - get the struct for this entry
Packit Service cd2a00
 * @ptr:	the &struct list_head pointer.
Packit Service cd2a00
 * @type:	the type of the struct this is embedded in.
Packit Service cd2a00
 * @member:	the name of the list_struct within the struct.
Packit Service cd2a00
 */
Packit Service cd2a00
#define list_entry(ptr, type, member) \
Packit Service cd2a00
	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
Packit Service cd2a00
Packit Service cd2a00
/**
Packit Service cd2a00
 * list_for_each	-	iterate over a list
Packit Service cd2a00
 * @pos:	the &struct list_head to use as a loop counter.
Packit Service cd2a00
 * @head:	the head for your list.
Packit Service cd2a00
 */
Packit Service cd2a00
#define list_for_each(pos, head) \
Packit Service cd2a00
	for (pos = (head)->next; pos != (head); \
Packit Service cd2a00
        	pos = pos->next)
Packit Service cd2a00
/**
Packit Service cd2a00
 * list_for_each_prev	-	iterate over a list backwards
Packit Service cd2a00
 * @pos:	the &struct list_head to use as a loop counter.
Packit Service cd2a00
 * @head:	the head for your list.
Packit Service cd2a00
 */
Packit Service cd2a00
#define list_for_each_prev(pos, head) \
Packit Service cd2a00
	for (pos = (head)->prev; pos != (head); \
Packit Service cd2a00
        	pos = pos->prev)
Packit Service cd2a00
        	
Packit Service cd2a00
/**
Packit Service cd2a00
 * list_for_each_safe	-	iterate over a list safe against removal of list entry
Packit Service cd2a00
 * @pos:	the &struct list_head to use as a loop counter.
Packit Service cd2a00
 * @n:		another &struct list_head to use as temporary storage
Packit Service cd2a00
 * @head:	the head for your list.
Packit Service cd2a00
 */
Packit Service cd2a00
#define list_for_each_safe(pos, n, head) \
Packit Service cd2a00
	for (pos = (head)->next, n = pos->next; pos != (head); \
Packit Service cd2a00
		pos = n, n = pos->next)
Packit Service cd2a00
Packit Service cd2a00
/**
Packit Service cd2a00
 * list_for_each_entry	-	iterate over list of given type
Packit Service cd2a00
 * @pos:	the type * to use as a loop counter.
Packit Service cd2a00
 * @head:	the head for your list.
Packit Service cd2a00
 * @member:	the name of the list_struct within the struct.
Packit Service cd2a00
 */
Packit Service cd2a00
#define list_for_each_entry(pos, head, member)				\
Packit Service cd2a00
	for (pos = list_entry((head)->next, typeof(*pos), member);	\
Packit Service cd2a00
	     &pos->member != (head); 					\
Packit Service cd2a00
	     pos = list_entry(pos->member.next, typeof(*pos), member))
Packit Service cd2a00
Packit Service cd2a00
/**
Packit Service cd2a00
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Packit Service cd2a00
 * @pos:	the type * to use as a loop counter.
Packit Service cd2a00
 * @n:		another type * to use as temporary storage
Packit Service cd2a00
 * @head:	the head for your list.
Packit Service cd2a00
 * @member:	the name of the list_struct within the struct.
Packit Service cd2a00
 */
Packit Service cd2a00
#define list_for_each_entry_safe(pos, n, head, member)			\
Packit Service cd2a00
	for (pos = list_entry((head)->next, typeof(*pos), member),	\
Packit Service cd2a00
		n = list_entry(pos->member.next, typeof(*pos), member);	\
Packit Service cd2a00
	     &pos->member != (head); 					\
Packit Service cd2a00
	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
Packit Service cd2a00
Packit Service cd2a00
Packit Service cd2a00
#endif