Blame kpatch-build/list.h

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