xref: /freebsd/sys/dev/hptrr/list.h (revision 718cf2ccb9956613756ab15d7a0e28f2c8e91cab)
1*718cf2ccSPedro F. Giffuni /*-
2*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*718cf2ccSPedro 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  * $FreeBSD$
29b063a422SScott Long  */
30b063a422SScott Long #include <dev/hptrr/hptrr_config.h>
31b063a422SScott Long /*
32b063a422SScott Long  * $Id: list.h,v 1.6 2006/10/31 06:25:28 gmm Exp $
33b063a422SScott Long  * Copyright (C) 2004-2005 HighPoint Technologies, Inc. All rights reserved.
34b063a422SScott Long  */
35b063a422SScott Long #ifndef _HPT_LIST_H_
36b063a422SScott Long #define _HPT_LIST_H_
37b063a422SScott Long 
38b063a422SScott Long #ifndef _LINUX_LIST_H
39b063a422SScott Long 
40b063a422SScott Long #ifndef HPT_INLINE
41b063a422SScott Long #define HPT_INLINE __inline
42b063a422SScott Long #endif
43b063a422SScott Long 
44b063a422SScott Long struct list_head {
45b063a422SScott Long 	struct list_head *next, *prev;
46b063a422SScott Long };
47b063a422SScott Long 
48b063a422SScott Long #define INIT_LIST_HEAD(ptr) do { (ptr)->next = (ptr); (ptr)->prev = (ptr); } while (0)
49b063a422SScott Long 
50b063a422SScott Long static HPT_INLINE void __list_add(struct list_head * _new, struct list_head * prev, struct list_head * next)
51b063a422SScott Long {
52b063a422SScott Long 	next->prev = _new;
53b063a422SScott Long 	_new->next = next;
54b063a422SScott Long 	_new->prev = prev;
55b063a422SScott Long 	prev->next = _new;
56b063a422SScott Long }
57b063a422SScott Long 
58b063a422SScott Long static HPT_INLINE void list_add(struct list_head *_new, struct list_head *head)
59b063a422SScott Long {
60b063a422SScott Long 	__list_add(_new, head, head->next);
61b063a422SScott Long }
62b063a422SScott Long 
63b063a422SScott Long static HPT_INLINE void list_add_tail(struct list_head *_new, struct list_head *head)
64b063a422SScott Long {
65b063a422SScott Long 	__list_add(_new, head->prev, head);
66b063a422SScott Long }
67b063a422SScott Long 
68b063a422SScott Long static HPT_INLINE void __list_del(struct list_head * prev, struct list_head * next)
69b063a422SScott Long {
70b063a422SScott Long 	next->prev = prev;
71b063a422SScott Long 	prev->next = next;
72b063a422SScott Long }
73b063a422SScott Long 
74b063a422SScott Long static HPT_INLINE void list_del(struct list_head *entry)
75b063a422SScott Long {
76b063a422SScott Long 	__list_del(entry->prev, entry->next);
77b063a422SScott Long }
78b063a422SScott Long 
79b063a422SScott Long static HPT_INLINE void list_del_init(struct list_head *entry)
80b063a422SScott Long {
81b063a422SScott Long 	__list_del(entry->prev, entry->next);
82b063a422SScott Long 	INIT_LIST_HEAD(entry);
83b063a422SScott Long }
84b063a422SScott Long 
85b063a422SScott Long static HPT_INLINE int list_empty(struct list_head *head)
86b063a422SScott Long {
87b063a422SScott Long 	HPT_ASSERT(!(head->next==head && head->prev!=head));
88b063a422SScott Long 	return head->next == head;
89b063a422SScott Long }
90b063a422SScott Long 
91b063a422SScott Long static HPT_INLINE void __list_splice(struct list_head *list,
92b063a422SScott Long 				 struct list_head *head)
93b063a422SScott Long {
94b063a422SScott Long 	struct list_head *first = list->next;
95b063a422SScott Long 	struct list_head *last = list->prev;
96b063a422SScott Long 	struct list_head *at = head->next;
97b063a422SScott Long 
98b063a422SScott Long 	first->prev = head;
99b063a422SScott Long 	head->next = first;
100b063a422SScott Long 
101b063a422SScott Long 	last->next = at;
102b063a422SScott Long 	at->prev = last;
103b063a422SScott Long }
104b063a422SScott Long 
105b063a422SScott Long static HPT_INLINE void list_splice(struct list_head *list, struct list_head *head)
106b063a422SScott Long {
107b063a422SScott Long 	if (!list_empty(list))
108b063a422SScott Long 		__list_splice(list, head);
109b063a422SScott Long }
110b063a422SScott Long 
111b063a422SScott Long static HPT_INLINE void list_splice_init(struct list_head *list, struct list_head *head)
112b063a422SScott Long {
113b063a422SScott Long 	if (!list_empty(list)) {
114b063a422SScott Long 		__list_splice(list, head);
115b063a422SScott Long 		INIT_LIST_HEAD(list);
116b063a422SScott Long 	}
117b063a422SScott Long }
118b063a422SScott Long 
119b063a422SScott Long #define list_entry(ptr, type, member) \
120b063a422SScott Long 	((type *)((char *)(ptr)-(HPT_UPTR)(&((type *)0)->member)))
121b063a422SScott Long 
122b063a422SScott Long #define list_for_each(pos, head) \
123b063a422SScott Long 	for (pos = (head)->next; pos != (head); pos = pos->next)
124b063a422SScott Long 
125b063a422SScott Long #define list_for_each_safe(pos, n, head) \
126b063a422SScott Long 	for (pos = (head)->next, n = pos->next; pos != (head); \
127b063a422SScott Long 		pos = n, n = pos->next)
128b063a422SScott Long 
129b063a422SScott Long #define get_first_item(attached, type, member) \
130b063a422SScott Long 	((type *)((char *)((attached)->next)-(HPT_UPTR)(&((type *)0)->member)))
131b063a422SScott Long 
132b063a422SScott Long #endif
133b063a422SScott Long 
134b063a422SScott Long #endif
135