Blame btt/list.h

Packit c4abd9
#ifndef _LINUX_LIST_H
Packit c4abd9
#define _LINUX_LIST_H
Packit c4abd9
Packit c4abd9
#include <stdio.h>
Packit c4abd9
Packit c4abd9
#ifndef offsetof
Packit c4abd9
/**
Packit c4abd9
 * Get offset of a member
Packit c4abd9
 */
Packit c4abd9
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
Packit c4abd9
#endif
Packit c4abd9
Packit c4abd9
#ifndef container_of
Packit c4abd9
/**
Packit c4abd9
 * Casts a member of a structure out to the containing structure
Packit c4abd9
 * @param ptr        the pointer to the member.
Packit c4abd9
 * @param type       the type of the container struct this is embedded in.
Packit c4abd9
 * @param member     the name of the member within the struct.
Packit c4abd9
 *
Packit c4abd9
 */
Packit c4abd9
#define container_of(ptr, type, member) ({                      \
Packit c4abd9
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
Packit c4abd9
	        (type *)( (char *)__mptr - offsetof(type,member) );})
Packit c4abd9
#endif
Packit c4abd9
Packit c4abd9
/*
Packit c4abd9
 * These are non-NULL pointers that will result in page faults
Packit c4abd9
 * under normal circumstances, used to verify that nobody uses
Packit c4abd9
 * non-initialized list entries.
Packit c4abd9
 */
Packit c4abd9
#define LIST_POISON1  ((void *) 0x00100100)
Packit c4abd9
#define LIST_POISON2  ((void *) 0x00200200)
Packit c4abd9
Packit c4abd9
struct list_head {
Packit c4abd9
	struct list_head *next, *prev;
Packit c4abd9
};
Packit c4abd9
Packit c4abd9
#define LIST_HEAD_INIT(name) { &(name), &(name) }
Packit c4abd9
Packit c4abd9
#define LIST_HEAD(name) \
Packit c4abd9
	struct list_head name = LIST_HEAD_INIT(name)
Packit c4abd9
Packit c4abd9
static inline void INIT_LIST_HEAD(struct list_head *list)
Packit c4abd9
{
Packit c4abd9
	list->next = list;
Packit c4abd9
	list->prev = list;
Packit c4abd9
}
Packit c4abd9
Packit c4abd9
/*
Packit c4abd9
 * Insert a new entry between two known consecutive entries.
Packit c4abd9
 *
Packit c4abd9
 * This is only for internal list manipulation where we know
Packit c4abd9
 * the prev/next entries already!
Packit c4abd9
 */
Packit c4abd9
static inline void __list_add(struct list_head *new,
Packit c4abd9
			      struct list_head *prev,
Packit c4abd9
			      struct list_head *next)
Packit c4abd9
{
Packit c4abd9
	next->prev = new;
Packit c4abd9
	new->next = next;
Packit c4abd9
	new->prev = prev;
Packit c4abd9
	prev->next = new;
Packit c4abd9
}
Packit c4abd9
Packit c4abd9
/**
Packit c4abd9
 * list_add - add a new entry
Packit c4abd9
 * @new: new entry to be added
Packit c4abd9
 * @head: list head to add it after
Packit c4abd9
 *
Packit c4abd9
 * Insert a new entry after the specified head.
Packit c4abd9
 * This is good for implementing stacks.
Packit c4abd9
 */
Packit c4abd9
static inline void list_add(struct list_head *new, struct list_head *head)
Packit c4abd9
{
Packit c4abd9
	__list_add(new, head, head->next);
Packit c4abd9
}
Packit c4abd9
Packit c4abd9
/**
Packit c4abd9
 * list_add_tail - add a new entry
Packit c4abd9
 * @new: new entry to be added
Packit c4abd9
 * @head: list head to add it before
Packit c4abd9
 *
Packit c4abd9
 * Insert a new entry before the specified head.
Packit c4abd9
 * This is useful for implementing queues.
Packit c4abd9
 */
Packit c4abd9
static inline void list_add_tail(struct list_head *new, struct list_head *head)
Packit c4abd9
{
Packit c4abd9
	__list_add(new, head->prev, head);
Packit c4abd9
}
Packit c4abd9
Packit c4abd9
/*
Packit c4abd9
 * Delete a list entry by making the prev/next entries
Packit c4abd9
 * point to each other.
Packit c4abd9
 *
Packit c4abd9
 * This is only for internal list manipulation where we know
Packit c4abd9
 * the prev/next entries already!
Packit c4abd9
 */
Packit c4abd9
static inline void __list_del(struct list_head * prev, struct list_head * next)
Packit c4abd9
{
Packit c4abd9
	next->prev = prev;
Packit c4abd9
	prev->next = next;
Packit c4abd9
}
Packit c4abd9
Packit c4abd9
/**
Packit c4abd9
 * list_del - deletes entry from list.
Packit c4abd9
 * @entry: the element to delete from the list.
Packit c4abd9
 * Note: list_empty on entry does not return true after this, the entry is
Packit c4abd9
 * in an undefined state.
Packit c4abd9
 */
Packit c4abd9
static inline void list_del(struct list_head *entry)
Packit c4abd9
{
Packit c4abd9
	__list_del(entry->prev, entry->next);
Packit c4abd9
	entry->next = LIST_POISON1;
Packit c4abd9
	entry->prev = LIST_POISON2;
Packit c4abd9
}
Packit c4abd9
Packit c4abd9
/**
Packit c4abd9
 * __list_for_each	-	iterate over a list
Packit c4abd9
 * @pos:	the &struct list_head to use as a loop counter.
Packit c4abd9
 * @head:	the head for your list.
Packit c4abd9
 *
Packit c4abd9
 * This variant differs from list_for_each() in that it's the
Packit c4abd9
 * simplest possible list iteration code, no prefetching is done.
Packit c4abd9
 * Use this for code that knows the list to be very short (empty
Packit c4abd9
 * or 1 entry) most of the time.
Packit c4abd9
 */
Packit c4abd9
#define __list_for_each(pos, head) \
Packit c4abd9
	for (pos = (head)->next; pos != (head); pos = pos->next)
Packit c4abd9
Packit c4abd9
/**
Packit c4abd9
 * list_for_each_safe	-	iterate over a list safe against removal of list entry
Packit c4abd9
 * @pos:	the &struct list_head to use as a loop counter.
Packit c4abd9
 * @n:		another &struct list_head to use as temporary storage
Packit c4abd9
 * @head:	the head for your list.
Packit c4abd9
 */
Packit c4abd9
#define list_for_each_safe(pos, n, head) \
Packit c4abd9
	for (pos = (head)->next, n = pos->next; pos != (head); \
Packit c4abd9
		pos = n, n = pos->next)
Packit c4abd9
Packit c4abd9
/**
Packit c4abd9
 * list_entry - get the struct for this entry
Packit c4abd9
 * @ptr:        the &struct list_head pointer.
Packit c4abd9
 * @type:       the type of the struct this is embedded in.
Packit c4abd9
 * @member:     the name of the list_struct within the struct.
Packit c4abd9
 */
Packit c4abd9
#define list_entry(ptr, type, member) \
Packit c4abd9
        container_of(ptr, type, member)
Packit c4abd9
Packit c4abd9
static inline int list_len(struct list_head *head_p)
Packit c4abd9
{
Packit c4abd9
	struct list_head *p;
Packit c4abd9
	int n = 0;
Packit c4abd9
Packit c4abd9
	__list_for_each(p, head_p) {
Packit c4abd9
		n++;
Packit c4abd9
	}
Packit c4abd9
Packit c4abd9
	return n;
Packit c4abd9
}
Packit c4abd9
Packit c4abd9
/**
Packit c4abd9
 * list_empty - tests whether a list is empty
Packit c4abd9
 * @head: the list to test.
Packit c4abd9
 */
Packit c4abd9
static inline int list_empty(const struct list_head *head)
Packit c4abd9
{
Packit c4abd9
	return head->next == head;
Packit c4abd9
}
Packit c4abd9
Packit c4abd9
/**
Packit c4abd9
 * list_first - Returns first entry on list, or NULL if empty
Packit c4abd9
 * @head: the list
Packit c4abd9
 */
Packit c4abd9
static inline struct list_head *list_first(const struct list_head *head)
Packit c4abd9
{
Packit c4abd9
	return list_empty(head) ? NULL : head->next;
Packit c4abd9
}
Packit c4abd9
Packit c4abd9
/**
Packit c4abd9
 * list_move_tail - delete from one list and add as another's tail
Packit c4abd9
 * @list: the entry to move
Packit c4abd9
 * @head: the head that will follow our entry
Packit c4abd9
 */
Packit c4abd9
static inline void list_move_tail(struct list_head *list,
Packit c4abd9
				  struct list_head *head)
Packit c4abd9
{
Packit c4abd9
        __list_del(list->prev, list->next);
Packit c4abd9
        list_add_tail(list, head);
Packit c4abd9
}
Packit c4abd9
Packit c4abd9
static inline void __list_splice(struct list_head *list,
Packit c4abd9
                                 struct list_head *head)
Packit c4abd9
{
Packit c4abd9
        struct list_head *first = list->next;
Packit c4abd9
        struct list_head *last = list->prev;
Packit c4abd9
        struct list_head *at = head->next;
Packit c4abd9
Packit c4abd9
        first->prev = head;
Packit c4abd9
        head->next = first;
Packit c4abd9
Packit c4abd9
        last->next = at;
Packit c4abd9
        at->prev = last;
Packit c4abd9
}
Packit c4abd9
Packit c4abd9
/**
Packit c4abd9
 *  * list_splice - join two lists
Packit c4abd9
 *   * @list: the new list to add.
Packit c4abd9
 *    * @head: the place to add it in the first list.
Packit c4abd9
 *     */
Packit c4abd9
static inline void list_splice(struct list_head *list, struct list_head *head)
Packit c4abd9
{
Packit c4abd9
        if (!list_empty(list))
Packit c4abd9
                __list_splice(list, head);
Packit c4abd9
}
Packit c4abd9
Packit c4abd9
/**
Packit c4abd9
 * list_replace - replace old entry by new one
Packit c4abd9
 * @old : the element to be replaced
Packit c4abd9
 * @new : the new element to insert
Packit c4abd9
 *
Packit c4abd9
 * If @old was empty, it will be overwritten.
Packit c4abd9
 */
Packit c4abd9
static inline void list_replace(struct list_head *old,
Packit c4abd9
				struct list_head *new)
Packit c4abd9
{
Packit c4abd9
	new->next = old->next;
Packit c4abd9
	new->next->prev = new;
Packit c4abd9
	new->prev = old->prev;
Packit c4abd9
	new->prev->next = new;
Packit c4abd9
}
Packit c4abd9
Packit c4abd9
static inline void list_replace_init(struct list_head *old,
Packit c4abd9
					struct list_head *new)
Packit c4abd9
{
Packit c4abd9
	list_replace(old, new);
Packit c4abd9
	INIT_LIST_HEAD(old);
Packit c4abd9
}
Packit c4abd9
Packit c4abd9
#endif