Blame lib/list_head.h

Packit c22fc9
/*
Packit c22fc9
 * Soft:        Keepalived is a failover program for the LVS project
Packit c22fc9
 *              <www.linuxvirtualserver.org>. It monitor & manipulate
Packit c22fc9
 *              a loadbalanced server pool using multi-layer checks.
Packit c22fc9
 *		In addition it provides a VRRPv2 & VRRPv3 stack.
Packit c22fc9
 *
Packit c22fc9
 * Author:      Alexandre Cassen, <acassen@corp.free.fr>
Packit c22fc9
 *
Packit c22fc9
 *              This program is distributed in the hope that it will be useful,
Packit c22fc9
 *              but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit c22fc9
 *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Packit c22fc9
 *              See the GNU General Public License for more details.
Packit c22fc9
 *
Packit c22fc9
 *              This program is free software; you can redistribute it and/or
Packit c22fc9
 *              modify it under the terms of the GNU General Public License
Packit c22fc9
 *              as published by the Free Software Foundation; either version
Packit c22fc9
 *              2 of the License, or (at your option) any later version.
Packit c22fc9
 *
Packit c22fc9
 * Copyright (C) 2018 Alexandre Cassen, <acassen@corp.free.fr>
Packit c22fc9
 */
Packit c22fc9
Packit c22fc9
#ifndef _LIST_HEAD_H
Packit c22fc9
#define _LIST_HEAD_H
Packit c22fc9
Packit c22fc9
#include <stdlib.h>
Packit c22fc9
#include <stddef.h>
Packit c22fc9
Packit Service dfccb1
#include "container.h"
Packit c22fc9
Packit c22fc9
/*
Packit c22fc9
 * These are non-NULL pointers that will result in page faults
Packit c22fc9
 * under normal circumstances, used to verify that nobody uses
Packit c22fc9
 * non-initialized list entries.
Packit c22fc9
 */
Packit c22fc9
#define LIST_POISON1  ((void *) 0x00100100)
Packit c22fc9
#define LIST_POISON2  ((void *) 0x00200200)
Packit c22fc9
Packit c22fc9
/*
Packit c22fc9
 * Simple doubly linked list implementation.
Packit c22fc9
 *
Packit c22fc9
 * Some of the internal functions ("__xxx") are useful when
Packit c22fc9
 * manipulating whole lists rather than single entries, as
Packit c22fc9
 * sometimes we already know the next/prev entries and we can
Packit c22fc9
 * generate better code by using them directly rather than
Packit c22fc9
 * using the generic single-entry routines.
Packit c22fc9
 */
Packit c22fc9
Packit c22fc9
typedef struct list_head {
Packit c22fc9
	struct list_head *next, *prev;
Packit c22fc9
} list_head_t;
Packit c22fc9
Packit Service dfccb1
/* Simple initializer */
Packit Service dfccb1
#define LIST_HEAD_INITIALIZER(name) { &(name), &(name) }
Packit Service dfccb1
#define LIST_HEAD_INITIALIZE(name) \
Packit Service dfccb1
	list_head_t name = LIST_HEAD_INITIALIZER(name)
Packit c22fc9
Packit c22fc9
#define INIT_LIST_HEAD(ptr) do { \
Packit c22fc9
	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
Packit c22fc9
} while (0)
Packit c22fc9
Packit c22fc9
/*
Packit c22fc9
 * Insert a new entry between two known consecutive entries.
Packit c22fc9
 *
Packit c22fc9
 * This is only for internal list manipulation where we know
Packit c22fc9
 * the prev/next entries already!
Packit c22fc9
 */
Packit c22fc9
static inline void __list_add(struct list_head *new,
Packit c22fc9
			      struct list_head *prev,
Packit c22fc9
			      struct list_head *next)
Packit c22fc9
{
Packit c22fc9
	next->prev = new;
Packit c22fc9
	new->next = next;
Packit c22fc9
	new->prev = prev;
Packit c22fc9
	prev->next = new;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_head_add - add a new entry
Packit c22fc9
 * @new: new entry to be added
Packit c22fc9
 * @head: list head to add it after
Packit c22fc9
 *
Packit c22fc9
 * Insert a new entry after the specified head.
Packit c22fc9
 * This is good for implementing stacks.
Packit c22fc9
 */
Packit c22fc9
static inline void list_head_add(struct list_head *new, struct list_head *head)
Packit c22fc9
{
Packit c22fc9
	__list_add(new, head, head->next);
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
// list_head_add_tail
Packit c22fc9
/**
Packit c22fc9
 * list_add_tail - add a new entry
Packit c22fc9
 * @new: new entry to be added
Packit c22fc9
 * @head: list head to add it before
Packit c22fc9
 *
Packit c22fc9
 * Insert a new entry before the specified head.
Packit c22fc9
 * This is useful for implementing queues.
Packit c22fc9
 */
Packit c22fc9
static inline void list_add_tail(struct list_head *new, struct list_head *head)
Packit c22fc9
{
Packit c22fc9
	__list_add(new, head->prev, head);
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/*
Packit c22fc9
 * Delete a list entry by making the prev/next entries
Packit c22fc9
 * point to each other.
Packit c22fc9
 *
Packit c22fc9
 * This is only for internal list manipulation where we know
Packit c22fc9
 * the prev/next entries already!
Packit c22fc9
 */
Packit c22fc9
static inline void __list_del(struct list_head * prev, struct list_head * next)
Packit c22fc9
{
Packit c22fc9
	next->prev = prev;
Packit c22fc9
	prev->next = next;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_del - deletes entry from list.
Packit c22fc9
 * @entry: the element to delete from the list.
Packit c22fc9
 * Note: list_empty on entry does not return true after this, the entry is
Packit c22fc9
 * in an undefined state.
Packit c22fc9
 */
Packit c22fc9
static inline void list_head_del(struct list_head *entry)
Packit c22fc9
{
Packit c22fc9
	__list_del(entry->prev, entry->next);
Packit c22fc9
	entry->next = LIST_POISON1;
Packit c22fc9
	entry->prev = LIST_POISON2;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_del_init - deletes entry from list and reinitialize it.
Packit c22fc9
 * @entry: the element to delete from the list.
Packit c22fc9
 */
Packit c22fc9
static inline void list_del_init(struct list_head *entry)
Packit c22fc9
{
Packit c22fc9
	__list_del(entry->prev, entry->next);
Packit c22fc9
	INIT_LIST_HEAD(entry);
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_move - delete from one list and add as another's head
Packit Service dfccb1
 * @lst: the entry to move
Packit c22fc9
 * @head: the head that will precede our entry
Packit c22fc9
 */
Packit Service dfccb1
static inline void list_move(struct list_head *lst, struct list_head *head)
Packit c22fc9
{
Packit Service dfccb1
	__list_del(lst->prev, lst->next);
Packit Service dfccb1
	list_head_add(lst, head);
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_move_tail - delete from one list and add as another's tail
Packit Service dfccb1
 * @lst: the entry to move
Packit c22fc9
 * @head: the head that will follow our entry
Packit c22fc9
 */
Packit Service dfccb1
static inline void list_move_tail(struct list_head *lst,
Packit c22fc9
				  struct list_head *head)
Packit c22fc9
{
Packit Service dfccb1
	__list_del(lst->prev, lst->next);
Packit Service dfccb1
	list_add_tail(lst, head);
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_is_first - tests whether @list is the first entry in list @head
Packit Service dfccb1
 * @lst: the entry to test
Packit c22fc9
 * @head: the head of the list
Packit c22fc9
 */
Packit Service dfccb1
static inline int list_is_first(const struct list_head *lst,
Packit c22fc9
				const struct list_head *head)
Packit c22fc9
{
Packit Service dfccb1
	return lst->prev == head;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_is_last - tests whether @list is the last entry in list @head
Packit Service dfccb1
 * @lst: the entry to test
Packit c22fc9
 * @head: the head of the list
Packit c22fc9
 */
Packit Service dfccb1
static inline int list_is_last(const struct list_head *lst,
Packit c22fc9
			       const struct list_head *head)
Packit c22fc9
{
Packit Service dfccb1
	return lst->next == head;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_empty - tests whether a list is empty
Packit c22fc9
 * @head: the list to test.
Packit c22fc9
 */
Packit c22fc9
static inline int list_empty(const struct list_head *head)
Packit c22fc9
{
Packit c22fc9
	return head->next == head;
Packit c22fc9
}
Packit c22fc9
Packit Service dfccb1
/**
Packit Service dfccb1
 * list_copy - copy one list to another
Packit Service dfccb1
 * @dst: the destination list
Packit Service dfccb1
 * @src: the source list
Packit Service dfccb1
 */
Packit Service dfccb1
static inline void list_copy(struct list_head *dst, struct list_head *src)
Packit Service dfccb1
{
Packit Service dfccb1
	if (list_empty(src))
Packit Service dfccb1
		INIT_LIST_HEAD(dst);
Packit Service dfccb1
	else {
Packit Service dfccb1
		*dst = *src;
Packit Service dfccb1
		dst->next->prev = dst;
Packit Service dfccb1
		dst->prev->next = dst;
Packit Service dfccb1
	}
Packit Service dfccb1
}
Packit Service dfccb1
Packit Service dfccb1
static inline void __list_splice(struct list_head *lst,
Packit c22fc9
				 struct list_head *head)
Packit c22fc9
{
Packit Service dfccb1
	struct list_head *first = lst->next;
Packit Service dfccb1
	struct list_head *last = lst->prev;
Packit c22fc9
	struct list_head *at = head->next;
Packit c22fc9
Packit c22fc9
	first->prev = head;
Packit c22fc9
	head->next = first;
Packit c22fc9
Packit c22fc9
	last->next = at;
Packit c22fc9
	at->prev = last;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_splice - join two lists
Packit Service dfccb1
 * @lst: the new list to add.
Packit c22fc9
 * @head: the place to add it in the first list.
Packit c22fc9
 */
Packit Service dfccb1
static inline void list_splice(struct list_head *lst, struct list_head *head)
Packit c22fc9
{
Packit Service dfccb1
	if (!list_empty(lst))
Packit Service dfccb1
		__list_splice(lst, head);
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_splice_init - join two lists and reinitialise the emptied list.
Packit c22fc9
 * @list: the new list to add.
Packit c22fc9
 * @head: the place to add it in the first list.
Packit c22fc9
 *
Packit c22fc9
 * The list at @list is reinitialised
Packit c22fc9
 */
Packit Service dfccb1
static inline void list_splice_init(struct list_head *lst,
Packit c22fc9
				    struct list_head *head)
Packit c22fc9
{
Packit Service dfccb1
	if (!list_empty(lst)) {
Packit Service dfccb1
		__list_splice(lst, head);
Packit Service dfccb1
		INIT_LIST_HEAD(lst);
Packit c22fc9
	}
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_sort - merge sort elements of a list.
Packit c22fc9
 * @head: the head of the list.
Packit c22fc9
 * @cmp: the function used to compare to list nodes.
Packit c22fc9
 */
Packit c22fc9
void list_sort(struct list_head *head,
Packit c22fc9
	       int (*cmp)(struct list_head *a, struct list_head *b));
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_entry - get the struct for this entry
Packit c22fc9
 * @ptr:	the &struct list_head pointer.
Packit c22fc9
 * @type:	the type of the struct this is embedded in.
Packit c22fc9
 * @member:	the name of the list_struct within the struct.
Packit c22fc9
 */
Packit c22fc9
#define list_entry(ptr, type, member) \
Packit c22fc9
	container_of(ptr, type, member)
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_first_entry - get the first element from a list
Packit c22fc9
 * @ptr:	the list head to take the element from.
Packit c22fc9
 * @type:	the type of the struct this is embedded in.
Packit c22fc9
 * @member:	the name of the list_struct within the struct.
Packit c22fc9
 *
Packit c22fc9
 * Note, that list is expected to be not empty.
Packit c22fc9
 */
Packit c22fc9
#define list_first_entry(ptr, type, member) \
Packit c22fc9
	list_entry((ptr)->next, type, member)
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_last_entry - get the last element from a list
Packit c22fc9
 * @ptr:	the list head to take the element from.
Packit c22fc9
 * @type:	the type of the struct this is embedded in.
Packit c22fc9
 * @member:	the name of the list_struct within the struct.
Packit c22fc9
 *
Packit c22fc9
 * Note, that list is expected to be not empty.
Packit c22fc9
 */
Packit c22fc9
#define list_last_entry(ptr, type, member) \
Packit c22fc9
	list_entry((ptr)->prev, type, member)
Packit c22fc9
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_for_each	-	iterate over a list
Packit c22fc9
 * @pos:	the &struct list_head to use as a loop counter.
Packit c22fc9
 * @head:	the head for your list.
Packit c22fc9
 */
Packit c22fc9
#define list_for_each(pos, head) \
Packit c22fc9
	for (pos = (head)->next; pos != (head); \
Packit c22fc9
		pos = pos->next)
Packit c22fc9
Packit c22fc9
// __list_for_each isn't different
Packit c22fc9
/**
Packit c22fc9
 * __list_for_each	-	iterate over a list
Packit c22fc9
 * @pos:	the &struct list_head to use as a loop counter.
Packit c22fc9
 * @head:	the head for your list.
Packit c22fc9
 *
Packit c22fc9
 * This variant differs from list_for_each() in that it's the
Packit c22fc9
 * simplest possible list iteration code, no prefetching is done.
Packit c22fc9
 * Use this for code that knows the list to be very short (empty
Packit c22fc9
 * or 1 entry) most of the time.
Packit c22fc9
 */
Packit c22fc9
#define __list_for_each(pos, head) \
Packit c22fc9
	for (pos = (head)->next; pos != (head); pos = pos->next)
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_for_each_prev	-	iterate over a list backwards
Packit c22fc9
 * @pos:	the &struct list_head to use as a loop counter.
Packit c22fc9
 * @head:	the head for your list.
Packit c22fc9
 */
Packit c22fc9
#define list_for_each_prev(pos, head) \
Packit c22fc9
	for (pos = (head)->prev; pos != (head); \
Packit c22fc9
		pos = pos->prev)
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_for_each_safe	-	iterate over a list safe against removal of list entry
Packit c22fc9
 * @pos:	the &struct list_head to use as a loop counter.
Packit c22fc9
 * @n:		another &struct list_head to use as temporary storage
Packit c22fc9
 * @head:	the head for your list.
Packit c22fc9
 */
Packit c22fc9
#define list_for_each_safe(pos, n, head) \
Packit c22fc9
	for (pos = (head)->next, n = pos->next; pos != (head); \
Packit c22fc9
		pos = n, n = pos->next)
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_for_each_entry	-	iterate over list of given type
Packit c22fc9
 * @pos:	the type * to use as a loop counter.
Packit c22fc9
 * @head:	the head for your list.
Packit c22fc9
 * @member:	the name of the list_struct within the struct.
Packit c22fc9
 */
Packit c22fc9
#define list_for_each_entry(pos, head, member)				\
Packit c22fc9
	for (pos = list_entry((head)->next, typeof(*pos), member);	\
Packit Service dfccb1
	     &pos->member != (head);					\
Packit c22fc9
	     pos = list_entry(pos->member.next, typeof(*pos), member))
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_for_each_entry_reverse - iterate backwards over list of given type.
Packit c22fc9
 * @pos:	the type * to use as a loop counter.
Packit c22fc9
 * @head:	the head for your list.
Packit c22fc9
 * @member:	the name of the list_struct within the struct.
Packit c22fc9
 */
Packit c22fc9
#define list_for_each_entry_reverse(pos, head, member)			\
Packit c22fc9
	for (pos = list_entry((head)->prev, typeof(*pos), member);	\
Packit Service dfccb1
	     &pos->member != (head);					\
Packit c22fc9
	     pos = list_entry(pos->member.prev, typeof(*pos), member))
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_for_each_entry_continue -	iterate over list of given type
Packit c22fc9
 *			continuing after existing point
Packit c22fc9
 * @pos:	the type * to use as a loop counter.
Packit c22fc9
 * @head:	the head for your list.
Packit c22fc9
 * @member:	the name of the list_struct within the struct.
Packit c22fc9
 */
Packit Service dfccb1
#define list_for_each_entry_continue(pos, head, member)			\
Packit c22fc9
	for (pos = list_entry(pos->member.next, typeof(*pos), member);	\
Packit c22fc9
	     &pos->member != (head);					\
Packit c22fc9
	     pos = list_entry(pos->member.next, typeof(*pos), member))
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_for_each_entry_continue_reverse - iterate backwards from the given point
Packit c22fc9
 * @pos:	the type * to use as a loop cursor.
Packit c22fc9
 * @head:	the head for your list.
Packit c22fc9
 * @member:	the name of the list_struct within the struct.
Packit c22fc9
 *
Packit c22fc9
 * Start to iterate over list of given type backwards, continuing after
Packit c22fc9
 * the current position.
Packit c22fc9
 */
Packit c22fc9
#define list_for_each_entry_continue_reverse(pos, head, member)		\
Packit c22fc9
	for (pos = list_entry(pos->member.prev, typeof(*pos), member);	\
Packit c22fc9
	     &pos->member != (head);					\
Packit c22fc9
	     pos = list_entry(pos->member.prev, typeof(*pos), member))
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_for_each_entry_from - iterate over list of given type from the current point
Packit c22fc9
 * @pos:	the type * to use as a loop cursor.
Packit c22fc9
 * @head:	the head for your list.
Packit c22fc9
 * @member:	the name of the list_struct within the struct.
Packit c22fc9
 *
Packit c22fc9
 * Iterate over list of given type, continuing from current position.
Packit c22fc9
 */
Packit Service dfccb1
#define list_for_each_entry_from(pos, head, member)			\
Packit c22fc9
	for (; &pos->member != (head);					\
Packit c22fc9
	     pos = list_entry(pos->member.next, typeof(*pos), member))
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_for_each_entry_from - iterate over list of given type from the current point
Packit c22fc9
 * @pos:	the type * to use as a loop cursor.
Packit c22fc9
 * @head:	the head for your list.
Packit c22fc9
 * @member:	the name of the list_struct within the struct.
Packit c22fc9
 *
Packit c22fc9
 * Iterate over list of given type, continuing from current position.
Packit c22fc9
 */
Packit c22fc9
#define list_for_each_entry_from_reverse(pos, head, member)		\
Packit c22fc9
	for (; &pos->member != (head);					\
Packit c22fc9
	     pos = list_entry(pos->member.prev, typeof(*pos), member))
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Packit c22fc9
 * @pos:	the type * to use as a loop counter.
Packit c22fc9
 * @n:		another type * to use as temporary storage
Packit c22fc9
 * @head:	the head for your list.
Packit c22fc9
 * @member:	the name of the list_struct within the struct.
Packit c22fc9
 */
Packit c22fc9
// Try the alternative
Packit c22fc9
#define list_for_each_entry_safe(pos, n, head, member)			\
Packit c22fc9
	for (pos = list_entry((head)->next, typeof(*pos), member),	\
Packit c22fc9
		n = list_entry(pos->member.next, typeof(*pos), member);	\
Packit Service dfccb1
	     &pos->member != (head);					\
Packit c22fc9
	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
Packit c22fc9
Packit c22fc9
Packit c22fc9
/*
Packit c22fc9
 * Double linked lists with a single pointer list head.
Packit c22fc9
 * Mostly useful for hash tables where the two pointer list head is
Packit c22fc9
 * too wasteful.
Packit c22fc9
 * You lose the ability to access the tail in O(1).
Packit c22fc9
 */
Packit c22fc9
Packit c22fc9
typedef struct hlist_head {
Packit c22fc9
	struct hlist_node *first;
Packit c22fc9
} hlist_head_t;
Packit c22fc9
Packit c22fc9
typedef struct hlist_node {
Packit c22fc9
	struct hlist_node *next, **pprev;
Packit c22fc9
} hlist_node_t;
Packit c22fc9
Packit Service dfccb1
#define HLIST_HEAD_INITIALIZER { .first = NULL }
Packit Service dfccb1
#define HLIST_HEAD_INITIALIZE(name) \
Packit Service dfccb1
	hlist_head_t name = { .first = NULL }
Packit c22fc9
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
Packit c22fc9
#define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
Packit c22fc9
Packit c22fc9
static __inline__ int hlist_unhashed(const struct hlist_node *h)
Packit c22fc9
{
Packit c22fc9
	return !h->pprev;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
static __inline__ int hlist_empty(const struct hlist_head *h)
Packit c22fc9
{
Packit c22fc9
	return !h->first;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
static __inline__ void __hlist_del(struct hlist_node *n)
Packit c22fc9
{
Packit c22fc9
	struct hlist_node *next = n->next;
Packit c22fc9
	struct hlist_node **pprev = n->pprev;
Packit c22fc9
	*pprev = next;
Packit c22fc9
	if (next)
Packit c22fc9
		next->pprev = pprev;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
static __inline__ void hlist_del(struct hlist_node *n)
Packit c22fc9
{
Packit c22fc9
	__hlist_del(n);
Packit c22fc9
	n->next = LIST_POISON1;
Packit c22fc9
	n->pprev = LIST_POISON2;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
static __inline__ void hlist_del_init(struct hlist_node *n)
Packit c22fc9
{
Packit c22fc9
	if (n->pprev)  {
Packit c22fc9
		__hlist_del(n);
Packit c22fc9
		INIT_HLIST_NODE(n);
Packit c22fc9
	}
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
static __inline__ void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
Packit c22fc9
{
Packit c22fc9
	struct hlist_node *first = h->first;
Packit c22fc9
	n->next = first;
Packit c22fc9
	if (first)
Packit c22fc9
		first->pprev = &n->next;
Packit c22fc9
	h->first = n;
Packit c22fc9
	n->pprev = &h->first;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
static __inline__ void hlist_add_before(struct hlist_node *n, struct hlist_node *next)
Packit c22fc9
{
Packit c22fc9
	n->pprev = next->pprev;
Packit c22fc9
	n->next = next;
Packit c22fc9
	next->pprev = &n->next;
Packit c22fc9
	*(n->pprev) = n;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
static __inline__ void hlist_add_after(struct hlist_node *n,
Packit c22fc9
				       struct hlist_node *next)
Packit c22fc9
{
Packit c22fc9
	next->next = n->next;
Packit c22fc9
	n->next = next;
Packit c22fc9
	next->pprev = &n->next;
Packit c22fc9
	if (next->next)
Packit c22fc9
		next->next->pprev = &next->next;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
Packit c22fc9
Packit c22fc9
#define hlist_for_each(pos, head) \
Packit c22fc9
	for (pos = (head)->first; pos; pos = pos->next)
Packit c22fc9
Packit c22fc9
#define hlist_for_each_safe(pos, n, head) \
Packit c22fc9
	for (pos = (head)->first; n = pos ? pos->next : 0, pos; \
Packit c22fc9
	     pos = n)
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * hlist_for_each_entry	- iterate over list of given type
Packit c22fc9
 * @tpos:	the type * to use as a loop counter.
Packit c22fc9
 * @pos:	the &struct hlist_node to use as a loop counter.
Packit c22fc9
 * @head:	the head for your list.
Packit c22fc9
 * @member:	the name of the hlist_node within the struct.
Packit c22fc9
 */
Packit c22fc9
#define hlist_for_each_entry(tpos, pos, head, member)			 \
Packit c22fc9
	for (pos = (head)->first;					 \
Packit c22fc9
	     pos &&							 \
Packit c22fc9
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
Packit c22fc9
	     pos = pos->next)
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
Packit c22fc9
 * @tpos:	the type * to use as a loop counter.
Packit c22fc9
 * @pos:	the &struct hlist_node to use as a loop counter.
Packit c22fc9
 * @member:	the name of the hlist_node within the struct.
Packit c22fc9
 */
Packit c22fc9
#define hlist_for_each_entry_continue(tpos, pos, member)		 \
Packit c22fc9
	for (pos = (pos)->next;						 \
Packit c22fc9
	     pos &&							 \
Packit c22fc9
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
Packit c22fc9
	     pos = pos->next)
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
Packit c22fc9
 * @tpos:	the type * to use as a loop counter.
Packit c22fc9
 * @pos:	the &struct hlist_node to use as a loop counter.
Packit c22fc9
 * @member:	the name of the hlist_node within the struct.
Packit c22fc9
 */
Packit c22fc9
#define hlist_for_each_entry_from(tpos, pos, member)			 \
Packit c22fc9
	for (; pos &&							 \
Packit c22fc9
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
Packit c22fc9
	     pos = pos->next)
Packit c22fc9
Packit c22fc9
/**
Packit c22fc9
 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
Packit c22fc9
 * @tpos:	the type * to use as a loop counter.
Packit c22fc9
 * @pos:	the &struct hlist_node to use as a loop counter.
Packit c22fc9
 * @n:		another &struct hlist_node to use as temporary storage
Packit c22fc9
 * @head:	the head for your list.
Packit c22fc9
 * @member:	the name of the hlist_node within the struct.
Packit c22fc9
 */
Packit Service dfccb1
#define hlist_for_each_entry_safe(tpos, pos, n, head, member)		 \
Packit c22fc9
	for (pos = (head)->first;					 \
Packit Service dfccb1
	     pos && ({ n = pos->next; 1; }) &&				 \
Packit c22fc9
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
Packit c22fc9
	     pos = n)
Packit c22fc9
Packit c22fc9
#define hlist_insert_sorted(tpos, ntpos, pos, head, smember, hmember, res)	\
Packit c22fc9
	res = 0;								\
Packit c22fc9
	tpos = NULL;								\
Packit c22fc9
	hlist_for_each_entry(tpos, pos, (head), hmember)			\
Packit c22fc9
		if (ntpos->smember <= tpos->smember)				\
Packit c22fc9
			break;							\
Packit c22fc9
	if (tpos) {								\
Packit c22fc9
		if (ntpos->smember < tpos->smember)				\
Packit c22fc9
			hlist_add_before(&ntpos->hmember, &tpos->hmember);	\
Packit c22fc9
		else if (ntpos->smember > tpos->smember)			\
Packit c22fc9
			hlist_add_after(&tpos->hmember, &ntpos->hmember);	\
Packit c22fc9
		else								\
Packit c22fc9
			res = 1;						\
Packit c22fc9
	} else									\
Packit c22fc9
		hlist_add_head(&ntpos->hmember, (head));
Packit c22fc9
Packit c22fc9
#endif /* ! _LIST_HEAD_H */