1228940Sdelphij/*-
2284730Sdelphij * Copyright (c) 2005-2011 HighPoint Technologies, Inc.
3228940Sdelphij * All rights reserved.
4228940Sdelphij *
5228940Sdelphij * Redistribution and use in source and binary forms, with or without
6228940Sdelphij * modification, are permitted provided that the following conditions
7228940Sdelphij * are met:
8228940Sdelphij * 1. Redistributions of source code must retain the above copyright
9228940Sdelphij *    notice, this list of conditions and the following disclaimer.
10228940Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
11228940Sdelphij *    notice, this list of conditions and the following disclaimer in the
12228940Sdelphij *    documentation and/or other materials provided with the distribution.
13228940Sdelphij *
14228940Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15228940Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16228940Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17228940Sdelphij * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18228940Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19228940Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20228940Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21228940Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22228940Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23228940Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24228940Sdelphij * SUCH DAMAGE.
25228940Sdelphij *
26228940Sdelphij * $FreeBSD$
27228940Sdelphij */
28228940Sdelphij
29228940Sdelphij#include <dev/hpt27xx/hpt27xx_config.h>
30228940Sdelphij
31228940Sdelphij#ifndef _HPT_LIST_H_
32228940Sdelphij#define _HPT_LIST_H_
33228940Sdelphij
34228940Sdelphij#ifndef _LINUX_LIST_H
35228940Sdelphij
36228940Sdelphij#ifndef HPT_INLINE
37228940Sdelphij#define HPT_INLINE __inline
38228940Sdelphij#endif
39228940Sdelphij
40228940Sdelphijstruct list_head {
41228940Sdelphij	struct list_head *next, *prev;
42228940Sdelphij};
43228940Sdelphij
44228940Sdelphij#define INIT_LIST_HEAD(ptr) do { (ptr)->next = (ptr); (ptr)->prev = (ptr); } while (0)
45228940Sdelphij
46228940Sdelphijstatic HPT_INLINE void __list_add(struct list_head * _new, struct list_head * prev, struct list_head * next)
47228940Sdelphij{
48228940Sdelphij	next->prev = _new;
49228940Sdelphij	_new->next = next;
50228940Sdelphij	_new->prev = prev;
51228940Sdelphij	prev->next = _new;
52228940Sdelphij}
53228940Sdelphij
54228940Sdelphijstatic HPT_INLINE void list_add(struct list_head *_new, struct list_head *head)
55228940Sdelphij{
56228940Sdelphij	__list_add(_new, head, head->next);
57228940Sdelphij}
58228940Sdelphij
59228940Sdelphijstatic HPT_INLINE void list_add_tail(struct list_head *_new, struct list_head *head)
60228940Sdelphij{
61228940Sdelphij	__list_add(_new, head->prev, head);
62228940Sdelphij}
63228940Sdelphij
64228940Sdelphijstatic HPT_INLINE void __list_del(struct list_head * prev, struct list_head * next)
65228940Sdelphij{
66228940Sdelphij	next->prev = prev;
67228940Sdelphij	prev->next = next;
68228940Sdelphij}
69228940Sdelphij
70228940Sdelphijstatic HPT_INLINE void list_del(struct list_head *entry)
71228940Sdelphij{
72228940Sdelphij	__list_del(entry->prev, entry->next);
73228940Sdelphij}
74228940Sdelphij
75228940Sdelphijstatic HPT_INLINE void list_del_init(struct list_head *entry)
76228940Sdelphij{
77228940Sdelphij	__list_del(entry->prev, entry->next);
78228940Sdelphij	INIT_LIST_HEAD(entry);
79228940Sdelphij}
80228940Sdelphij
81228940Sdelphijstatic HPT_INLINE int list_empty(struct list_head *head)
82228940Sdelphij{
83228940Sdelphij	HPT_ASSERT(!(head->next==head && head->prev!=head));
84228940Sdelphij	return head->next == head;
85228940Sdelphij}
86228940Sdelphij
87228940Sdelphijstatic HPT_INLINE void __list_splice(struct list_head *list,
88228940Sdelphij				 struct list_head *head)
89228940Sdelphij{
90228940Sdelphij	struct list_head *first = list->next;
91228940Sdelphij	struct list_head *last = list->prev;
92228940Sdelphij	struct list_head *at = head->next;
93228940Sdelphij
94228940Sdelphij	first->prev = head;
95228940Sdelphij	head->next = first;
96228940Sdelphij
97228940Sdelphij	last->next = at;
98228940Sdelphij	at->prev = last;
99228940Sdelphij}
100228940Sdelphij
101228940Sdelphijstatic HPT_INLINE void list_splice(struct list_head *list, struct list_head *head)
102228940Sdelphij{
103228940Sdelphij	if (!list_empty(list))
104228940Sdelphij		__list_splice(list, head);
105228940Sdelphij}
106228940Sdelphij
107228940Sdelphijstatic HPT_INLINE void list_splice_init(struct list_head *list, struct list_head *head)
108228940Sdelphij{
109228940Sdelphij	if (!list_empty(list)) {
110228940Sdelphij		__list_splice(list, head);
111228940Sdelphij		INIT_LIST_HEAD(list);
112228940Sdelphij	}
113228940Sdelphij}
114228940Sdelphij
115228940Sdelphij#define list_entry(ptr, type, member) \
116228940Sdelphij	((type *)((char *)(ptr)-(HPT_UPTR)(&((type *)0)->member)))
117228940Sdelphij
118228940Sdelphij#define list_for_each(pos, head) \
119228940Sdelphij	for (pos = (head)->next; pos != (head); pos = pos->next)
120228940Sdelphij
121228940Sdelphij#define list_for_each_safe(pos, n, head) \
122228940Sdelphij	for (pos = (head)->next, n = pos->next; pos != (head); \
123228940Sdelphij		pos = n, n = pos->next)
124228940Sdelphij
125228940Sdelphij#define get_first_item(attached, type, member) \
126228940Sdelphij	((type *)((char *)((attached)->next)-(HPT_UPTR)(&((type *)0)->member)))
127228940Sdelphij
128228940Sdelphij#endif
129228940Sdelphij
130228940Sdelphij#endif
131