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