Blame kpatch-build/list.h

Packit c71e3f
/*
Packit c71e3f
 * list.h
Packit c71e3f
 *
Packit c71e3f
 * Adapted from http://www.mcs.anl.gov/~kazutomo/list/list.h which is a
Packit c71e3f
 * userspace port of the Linux kernel implementation in include/linux/list.h
Packit c71e3f
 *
Packit c71e3f
 * Thus licensed as GPLv2.
Packit c71e3f
 *
Packit c71e3f
 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
Packit c71e3f
 *
Packit c71e3f
 * This program is free software; you can redistribute it and/or
Packit c71e3f
 * modify it under the terms of the GNU General Public License
Packit c71e3f
 * as published by the Free Software Foundation; either version 2
Packit c71e3f
 * of the License, or (at your option) any later version.
Packit c71e3f
 *
Packit c71e3f
 * This program is distributed in the hope that it will be useful,
Packit c71e3f
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit c71e3f
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit c71e3f
 * GNU General Public License for more details.
Packit c71e3f
 *
Packit c71e3f
 * You should have received a copy of the GNU General Public License
Packit c71e3f
 * along with this program; if not, write to the Free Software
Packit c71e3f
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA,
Packit c71e3f
 * 02110-1301, USA.
Packit c71e3f
 */
Packit c71e3f
Packit c71e3f
#ifndef _LIST_H
Packit c71e3f
#define _LIST_H
Packit c71e3f
Packit c71e3f
/**
Packit c71e3f
 * Get offset of a member
Packit c71e3f
 */
Packit c71e3f
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
Packit c71e3f
Packit c71e3f
/**
Packit c71e3f
 * Casts a member of a structure out to the containing structure
Packit c71e3f
 * @param ptr        the pointer to the member.
Packit c71e3f
 * @param type       the type of the container struct this is embedded in.
Packit c71e3f
 * @param member     the name of the member within the struct.
Packit c71e3f
 *
Packit c71e3f
 */
Packit c71e3f
#define container_of(ptr, type, member) ({                      \
Packit c71e3f
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
Packit c71e3f
        (type *)( (char *)__mptr - offsetof(type,member) );})
Packit c71e3f
Packit c71e3f
/*
Packit c71e3f
 * These are non-NULL pointers that will result in page faults
Packit c71e3f
 * under normal circumstances, used to verify that nobody uses
Packit c71e3f
 * non-initialized list entries.
Packit c71e3f
 */
Packit c71e3f
#define LIST_POISON1  ((void *) 0x00100100)
Packit c71e3f
#define LIST_POISON2  ((void *) 0x00200200)
Packit c71e3f
Packit c71e3f
/**
Packit c71e3f
 * Simple doubly linked list implementation.
Packit c71e3f
 *
Packit c71e3f
 * Some of the internal functions ("__xxx") are useful when
Packit c71e3f
 * manipulating whole lists rather than single entries, as
Packit c71e3f
 * sometimes we already know the next/prev entries and we can
Packit c71e3f
 * generate better code by using them directly rather than
Packit c71e3f
 * using the generic single-entry routines.
Packit c71e3f
 */
Packit c71e3f
struct list_head {
Packit c71e3f
	struct list_head *next, *prev;
Packit c71e3f
};
Packit c71e3f
Packit c71e3f
#define LIST_HEAD_INIT(name) { &(name), &(name) }
Packit c71e3f
Packit c71e3f
#define LIST_HEAD(name) \
Packit c71e3f
	struct list_head name = LIST_HEAD_INIT(name)
Packit c71e3f
Packit c71e3f
#define INIT_LIST_HEAD(ptr) do { \
Packit c71e3f
	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
Packit c71e3f
} while (0)
Packit c71e3f
Packit c71e3f
/*
Packit c71e3f
 * Insert a new entry between two known consecutive entries.
Packit c71e3f
 *
Packit c71e3f
 * This is only for internal list manipulation where we know
Packit c71e3f
 * the prev/next entries already!
Packit c71e3f
 */
Packit c71e3f
static inline void __list_add(struct list_head *new,
Packit c71e3f
			      struct list_head *prev,
Packit c71e3f
			      struct list_head *next)
Packit c71e3f
{
Packit c71e3f
	next->prev = new;
Packit c71e3f
	new->next = next;
Packit c71e3f
	new->prev = prev;
Packit c71e3f
	prev->next = new;
Packit c71e3f
}
Packit c71e3f
Packit c71e3f
/**
Packit c71e3f
 * list_add - add a new entry
Packit c71e3f
 * @new: new entry to be added
Packit c71e3f
 * @head: list head to add it after
Packit c71e3f
 *
Packit c71e3f
 * Insert a new entry after the specified head.
Packit c71e3f
 * This is good for implementing stacks.
Packit c71e3f
 */
Packit c71e3f
static inline void list_add(struct list_head *new, struct list_head *head)
Packit c71e3f
{
Packit c71e3f
	__list_add(new, head, head->next);
Packit c71e3f
}
Packit c71e3f
Packit c71e3f
/**
Packit c71e3f
 * list_add_tail - add a new entry
Packit c71e3f
 * @new: new entry to be added
Packit c71e3f
 * @head: list head to add it before
Packit c71e3f
 *
Packit c71e3f
 * Insert a new entry before the specified head.
Packit c71e3f
 * This is useful for implementing queues.
Packit c71e3f
 */
Packit c71e3f
static inline void list_add_tail(struct list_head *new, struct list_head *head)
Packit c71e3f
{
Packit c71e3f
	__list_add(new, head->prev, head);
Packit c71e3f
}
Packit c71e3f
Packit c71e3f
Packit c71e3f
/*
Packit c71e3f
 * Delete a list entry by making the prev/next entries
Packit c71e3f
 * point to each other.
Packit c71e3f
 *
Packit c71e3f
 * This is only for internal list manipulation where we know
Packit c71e3f
 * the prev/next entries already!
Packit c71e3f
 */
Packit c71e3f
static inline void __list_del(struct list_head * prev, struct list_head * next)
Packit c71e3f
{
Packit c71e3f
	next->prev = prev;
Packit c71e3f
	prev->next = next;
Packit c71e3f
}
Packit c71e3f
Packit c71e3f
/**
Packit c71e3f
 * list_del - deletes entry from list.
Packit c71e3f
 * @entry: the element to delete from the list.
Packit c71e3f
 * Note: list_empty on entry does not return true after this, the entry is
Packit c71e3f
 * in an undefined state.
Packit c71e3f
 */
Packit c71e3f
static inline void list_del(struct list_head *entry)
Packit c71e3f
{
Packit c71e3f
	__list_del(entry->prev, entry->next);
Packit c71e3f
	entry->next = LIST_POISON1;
Packit c71e3f
	entry->prev = LIST_POISON2;
Packit c71e3f
}
Packit c71e3f
Packit c71e3f
/**
Packit c71e3f
 * list_replace - replace old entry by new one
Packit c71e3f
 * @old : the element to be replaced
Packit c71e3f
 * @new : the new element to insert
Packit c71e3f
 *
Packit c71e3f
 * If @old was empty, it will be overwritten.
Packit c71e3f
 */
Packit c71e3f
static inline void list_replace(struct list_head *old,
Packit c71e3f
				struct list_head *new)
Packit c71e3f
{
Packit c71e3f
	new->next = old->next;
Packit c71e3f
	new->next->prev = new;
Packit c71e3f
	new->prev = old->prev;
Packit c71e3f
	new->prev->next = new;
Packit c71e3f
}
Packit c71e3f
Packit c71e3f
#define list_entry(ptr, type, member) \
Packit c71e3f
	container_of(ptr, type, member)
Packit c71e3f
Packit c71e3f
/**
Packit c71e3f
 * list_first_entry - get the first element from a list
Packit c71e3f
 * @ptr:	the list head to take the element from.
Packit c71e3f
 * @type:	the type of the struct this is embedded in.
Packit c71e3f
 * @member:	the name of the list_struct within the struct.
Packit c71e3f
 *
Packit c71e3f
 * Note, that list is expected to be not empty.
Packit c71e3f
 */
Packit c71e3f
#define list_first_entry(ptr, type, member) \
Packit c71e3f
	list_entry((ptr)->next, type, member)
Packit c71e3f
Packit c71e3f
/**
Packit c71e3f
 * list_next_entry - get the next element in list
Packit c71e3f
 * @pos:	the type * to cursor
Packit c71e3f
 * @member:	the name of the list_struct within the struct.
Packit c71e3f
 */
Packit c71e3f
#define list_next_entry(pos, member) \
Packit c71e3f
	list_entry((pos)->member.next, typeof(*(pos)), member)
Packit c71e3f
Packit c71e3f
/**
Packit c71e3f
 * list_for_each_entry	-	iterate over list of given type
Packit c71e3f
 * @pos:	the type * to use as a loop counter.
Packit c71e3f
 * @head:	the head for your list.
Packit c71e3f
 * @member:	the name of the list_struct within the struct.
Packit c71e3f
 */
Packit c71e3f
#define list_for_each_entry(pos, head, member)				\
Packit c71e3f
	for (pos = list_entry((head)->next, typeof(*pos), member);	\
Packit c71e3f
	     &pos->member != (head);					\
Packit c71e3f
	     pos = list_entry(pos->member.next, typeof(*pos), member))
Packit c71e3f
Packit c71e3f
/**
Packit c71e3f
 * list_for_each_entry_continue - continue iteration over list of given type
Packit c71e3f
 * @pos:	the type * to use as a loop cursor.
Packit c71e3f
 * @head:	the head for your list.
Packit c71e3f
 * @member:	the name of the list_struct within the struct.
Packit c71e3f
 *
Packit c71e3f
 * Continue to iterate over list of given type, continuing after
Packit c71e3f
 * the current position.
Packit c71e3f
 */
Packit c71e3f
#define list_for_each_entry_continue(pos, head, member) 		\
Packit c71e3f
	for (pos = list_next_entry(pos, member);			\
Packit c71e3f
	     &pos->member != (head);					\
Packit c71e3f
	     pos = list_next_entry(pos, member))
Packit c71e3f
Packit c71e3f
/**
Packit c71e3f
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Packit c71e3f
 * @pos:	the type * to use as a loop counter.
Packit c71e3f
 * @n:		another type * to use as temporary storage
Packit c71e3f
 * @head:	the head for your list.
Packit c71e3f
 * @member:	the name of the list_struct within the struct.
Packit c71e3f
 */
Packit c71e3f
#define list_for_each_entry_safe(pos, n, head, member)			\
Packit c71e3f
	for (pos = list_entry((head)->next, typeof(*pos), member),	\
Packit c71e3f
		n = list_entry(pos->member.next, typeof(*pos), member);	\
Packit c71e3f
	     &pos->member != (head); 					\
Packit c71e3f
	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
Packit c71e3f
Packit c71e3f
#endif /* _LIST_H_ */