1331722Seadler/*
2174604Sscottl * Copyright (c) HighPoint Technologies, Inc.
3174604Sscottl * All rights reserved.
4174604Sscottl *
5174604Sscottl * Redistribution and use in source and binary forms, with or without
6174604Sscottl * modification, are permitted provided that the following conditions
7174604Sscottl * are met:
8174604Sscottl * 1. Redistributions of source code must retain the above copyright
9174604Sscottl *    notice, this list of conditions and the following disclaimer.
10174604Sscottl * 2. Redistributions in binary form must reproduce the above copyright
11174604Sscottl *    notice, this list of conditions and the following disclaimer in the
12174604Sscottl *    documentation and/or other materials provided with the distribution.
13174604Sscottl *
14174604Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15174604Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16174604Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17174604Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18174604Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19174604Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20174604Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21174604Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22174604Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23174604Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24174604Sscottl * SUCH DAMAGE.
25174604Sscottl *
26174604Sscottl * $FreeBSD$
27174604Sscottl */
28174604Sscottl#include <dev/hptrr/hptrr_config.h>
29174604Sscottl/*
30174604Sscottl * $Id: list.h,v 1.6 2006/10/31 06:25:28 gmm Exp $
31174604Sscottl * Copyright (C) 2004-2005 HighPoint Technologies, Inc. All rights reserved.
32174604Sscottl */
33174604Sscottl#ifndef _HPT_LIST_H_
34174604Sscottl#define _HPT_LIST_H_
35174604Sscottl
36174604Sscottl#ifndef _LINUX_LIST_H
37174604Sscottl
38174604Sscottl#ifndef HPT_INLINE
39174604Sscottl#define HPT_INLINE __inline
40174604Sscottl#endif
41174604Sscottl
42174604Sscottlstruct list_head {
43174604Sscottl	struct list_head *next, *prev;
44174604Sscottl};
45174604Sscottl
46174604Sscottl#define INIT_LIST_HEAD(ptr) do { (ptr)->next = (ptr); (ptr)->prev = (ptr); } while (0)
47174604Sscottl
48174604Sscottlstatic HPT_INLINE void __list_add(struct list_head * _new, struct list_head * prev, struct list_head * next)
49174604Sscottl{
50174604Sscottl	next->prev = _new;
51174604Sscottl	_new->next = next;
52174604Sscottl	_new->prev = prev;
53174604Sscottl	prev->next = _new;
54174604Sscottl}
55174604Sscottl
56174604Sscottlstatic HPT_INLINE void list_add(struct list_head *_new, struct list_head *head)
57174604Sscottl{
58174604Sscottl	__list_add(_new, head, head->next);
59174604Sscottl}
60174604Sscottl
61174604Sscottlstatic HPT_INLINE void list_add_tail(struct list_head *_new, struct list_head *head)
62174604Sscottl{
63174604Sscottl	__list_add(_new, head->prev, head);
64174604Sscottl}
65174604Sscottl
66174604Sscottlstatic HPT_INLINE void __list_del(struct list_head * prev, struct list_head * next)
67174604Sscottl{
68174604Sscottl	next->prev = prev;
69174604Sscottl	prev->next = next;
70174604Sscottl}
71174604Sscottl
72174604Sscottlstatic HPT_INLINE void list_del(struct list_head *entry)
73174604Sscottl{
74174604Sscottl	__list_del(entry->prev, entry->next);
75174604Sscottl}
76174604Sscottl
77174604Sscottlstatic HPT_INLINE void list_del_init(struct list_head *entry)
78174604Sscottl{
79174604Sscottl	__list_del(entry->prev, entry->next);
80174604Sscottl	INIT_LIST_HEAD(entry);
81174604Sscottl}
82174604Sscottl
83174604Sscottlstatic HPT_INLINE int list_empty(struct list_head *head)
84174604Sscottl{
85174604Sscottl	HPT_ASSERT(!(head->next==head && head->prev!=head));
86174604Sscottl	return head->next == head;
87174604Sscottl}
88174604Sscottl
89174604Sscottlstatic HPT_INLINE void __list_splice(struct list_head *list,
90174604Sscottl				 struct list_head *head)
91174604Sscottl{
92174604Sscottl	struct list_head *first = list->next;
93174604Sscottl	struct list_head *last = list->prev;
94174604Sscottl	struct list_head *at = head->next;
95174604Sscottl
96174604Sscottl	first->prev = head;
97174604Sscottl	head->next = first;
98174604Sscottl
99174604Sscottl	last->next = at;
100174604Sscottl	at->prev = last;
101174604Sscottl}
102174604Sscottl
103174604Sscottlstatic HPT_INLINE void list_splice(struct list_head *list, struct list_head *head)
104174604Sscottl{
105174604Sscottl	if (!list_empty(list))
106174604Sscottl		__list_splice(list, head);
107174604Sscottl}
108174604Sscottl
109174604Sscottlstatic HPT_INLINE void list_splice_init(struct list_head *list, struct list_head *head)
110174604Sscottl{
111174604Sscottl	if (!list_empty(list)) {
112174604Sscottl		__list_splice(list, head);
113174604Sscottl		INIT_LIST_HEAD(list);
114174604Sscottl	}
115174604Sscottl}
116174604Sscottl
117174604Sscottl#define list_entry(ptr, type, member) \
118174604Sscottl	((type *)((char *)(ptr)-(HPT_UPTR)(&((type *)0)->member)))
119174604Sscottl
120174604Sscottl#define list_for_each(pos, head) \
121174604Sscottl	for (pos = (head)->next; pos != (head); pos = pos->next)
122174604Sscottl
123174604Sscottl#define list_for_each_safe(pos, n, head) \
124174604Sscottl	for (pos = (head)->next, n = pos->next; pos != (head); \
125174604Sscottl		pos = n, n = pos->next)
126174604Sscottl
127174604Sscottl#define get_first_item(attached, type, member) \
128174604Sscottl	((type *)((char *)((attached)->next)-(HPT_UPTR)(&((type *)0)->member)))
129174604Sscottl
130174604Sscottl#endif
131174604Sscottl
132174604Sscottl#endif
133