1 /*-
2  * Copyright (C) 2005-2011 HighPoint Technologies, Inc.
3  * Copyright (c) 2017-2022 Intel Corporation.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 #ifndef LIST_H_
29 #define LIST_H_
30 
31 #include <types.h>
32 
33 struct list_head {
34 	struct list_head *next, *prev;
35 };
36 
37 struct hlist_head {
38 	struct hlist_node *first;
39 };
40 
41 struct hlist_node {
42 	struct hlist_node *next, **pprev;
43 };
44 
45 #define INIT_LIST_HEAD(ptr) do { (ptr)->next = (ptr); (ptr)->prev = (ptr); } \
46 	while (0)
47 
list_add_node(struct list_head * new_node,struct list_head * prev,struct list_head * next)48 static inline void list_add_node(struct list_head *new_node, struct list_head *prev,
49 	struct list_head *next)
50 {
51 	next->prev = new_node;
52 	new_node->next = next;
53 	new_node->prev = prev;
54 	prev->next = new_node;
55 }
56 
list_add(struct list_head * new_node,struct list_head * head)57 static inline void list_add(struct list_head *new_node, struct list_head *head)
58 {
59 	list_add_node(new_node, head, head->next);
60 }
61 
list_add_tail(struct list_head * new_node,struct list_head * head)62 static inline void list_add_tail(struct list_head *new_node,
63 	struct list_head *head)
64 {
65 	list_add_node(new_node, head->prev, head);
66 }
67 
list_del_node(struct list_head * prev,struct list_head * next)68 static inline void list_del_node(struct list_head *prev, struct list_head *next)
69 {
70 	next->prev = prev;
71 	prev->next = next;
72 }
73 
list_del(const struct list_head * entry)74 static inline void list_del(const struct list_head *entry)
75 {
76 	list_del_node(entry->prev, entry->next);
77 }
78 
list_del_init(struct list_head * entry)79 static inline void list_del_init(struct list_head *entry)
80 {
81 	list_del_node(entry->prev, entry->next);
82 	INIT_LIST_HEAD(entry);
83 }
84 
list_empty(const struct list_head * head)85 static inline _Bool list_empty(const struct list_head *head)
86 {
87 	return head->next == head;
88 }
89 
list_splice_node(const struct list_head * list,struct list_head * head)90 static inline void list_splice_node(const struct list_head *list,
91 				 struct list_head *head)
92 {
93 	struct list_head *first = list->next;
94 	struct list_head *last = list->prev;
95 	struct list_head *at = head->next;
96 
97 	first->prev = head;
98 	head->next = first;
99 
100 	last->next = at;
101 	at->prev = last;
102 }
103 
list_splice(const struct list_head * list,struct list_head * head)104 static inline void list_splice(const struct list_head *list, struct list_head *head)
105 {
106 	if (!list_empty(list)) {
107 		list_splice_node(list, head);
108 	}
109 }
110 
list_splice_init(struct list_head * list,struct list_head * head)111 static inline void list_splice_init(struct list_head *list,
112 	struct list_head *head)
113 {
114 	if (!list_empty(list)) {
115 		list_splice_node(list, head);
116 		INIT_LIST_HEAD(list);
117 	}
118 }
119 
120 #define container_of(ptr, type, member) \
121 	((type *)(((char *)(ptr)) - offsetof(type, member)))
122 
123 #define list_for_each(pos, head) \
124 	for ((pos) = (head)->next; (pos) != (head); (pos) = (pos)->next)
125 
126 #define list_for_each_safe(pos, n, head) \
127 	for ((pos) = (head)->next, (n) = (pos)->next; (pos) != (head); \
128 		(pos) = (n), (n) = (pos)->next)
129 
130 #define get_first_item(attached, type, member) \
131 	((type *)((char *)((attached)->next)-(uint64_t)(&((type *)0)->member)))
132 
133 static inline void
hlist_del(struct hlist_node * n)134 hlist_del(struct hlist_node *n)
135 {
136 
137         if (n->next != NULL) {
138                 n->next->pprev = n->pprev;
139 	}
140         *n->pprev = n->next;
141 }
142 
143 static inline void
hlist_add_head(struct hlist_node * n,struct hlist_head * h)144 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
145 {
146 
147 	n->next = h->first;
148 	if (h->first != NULL) {
149 		h->first->pprev = &n->next;
150 	}
151 	h->first = n;
152 	n->pprev = &h->first;
153 }
154 
155 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
156 
157 #define hlist_for_each(pos, head) \
158 	for ((pos) = (head)->first; (pos) != NULL; (pos) = (pos)->next)
159 
160 #endif /* LIST_H_ */
161