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