xref: /freebsd/sys/dev/hptnr/list.h (revision b111430e54537a66080df115c3a2950a2922da5a)
1 /* $Id: list.h,v 1.6 2006/10/31 06:25:28 gmm Exp $ */
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4  *
5  * Copyright (C) 2005-2011 HighPoint Technologies, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #include <dev/hptnr/hptnr_config.h>
32 #ifndef _HPT_LIST_H_
33 #define _HPT_LIST_H_
34 
35 #ifndef _LINUX_LIST_H
36 
37 #ifndef HPT_INLINE
38 #define HPT_INLINE __inline
39 #endif
40 
41 struct list_head {
42 	struct list_head *next, *prev;
43 };
44 
45 #define INIT_LIST_HEAD(ptr) do { (ptr)->next = (ptr); (ptr)->prev = (ptr); } while (0)
46 
47 static HPT_INLINE void __list_add(struct list_head * _new, struct list_head * prev, struct list_head * next)
48 {
49 	next->prev = _new;
50 	_new->next = next;
51 	_new->prev = prev;
52 	prev->next = _new;
53 }
54 
55 static HPT_INLINE void list_add(struct list_head *_new, struct list_head *head)
56 {
57 	__list_add(_new, head, head->next);
58 }
59 
60 static HPT_INLINE void list_add_tail(struct list_head *_new, struct list_head *head)
61 {
62 	__list_add(_new, head->prev, head);
63 }
64 
65 static HPT_INLINE void __list_del(struct list_head * prev, struct list_head * next)
66 {
67 	next->prev = prev;
68 	prev->next = next;
69 }
70 
71 static HPT_INLINE void list_del(struct list_head *entry)
72 {
73 	__list_del(entry->prev, entry->next);
74 }
75 
76 static HPT_INLINE void list_del_init(struct list_head *entry)
77 {
78 	__list_del(entry->prev, entry->next);
79 	INIT_LIST_HEAD(entry);
80 }
81 
82 static HPT_INLINE int list_empty(struct list_head *head)
83 {
84 	HPT_ASSERT(!(head->next==head && head->prev!=head));
85 	return head->next == head;
86 }
87 
88 static HPT_INLINE void __list_splice(struct list_head *list,
89 				 struct list_head *head)
90 {
91 	struct list_head *first = list->next;
92 	struct list_head *last = list->prev;
93 	struct list_head *at = head->next;
94 
95 	first->prev = head;
96 	head->next = first;
97 
98 	last->next = at;
99 	at->prev = last;
100 }
101 
102 static HPT_INLINE void list_splice(struct list_head *list, struct list_head *head)
103 {
104 	if (!list_empty(list))
105 		__list_splice(list, head);
106 }
107 
108 static HPT_INLINE void list_splice_init(struct list_head *list, struct list_head *head)
109 {
110 	if (!list_empty(list)) {
111 		__list_splice(list, head);
112 		INIT_LIST_HEAD(list);
113 	}
114 }
115 
116 #define list_entry(ptr, type, member) \
117 	((type *)((char *)(ptr)-(HPT_UPTR)(&((type *)0)->member)))
118 
119 #define list_for_each(pos, head) \
120 	for (pos = (head)->next; pos != (head); pos = pos->next)
121 
122 #define list_for_each_safe(pos, n, head) \
123 	for (pos = (head)->next, n = pos->next; pos != (head); \
124 		pos = n, n = pos->next)
125 
126 #define get_first_item(attached, type, member) \
127 	((type *)((char *)((attached)->next)-(HPT_UPTR)(&((type *)0)->member)))
128 
129 #endif
130 
131 #endif
132