• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
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 unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifndef _LINUXKPI_LINUX_LIST_H_
30 #define _LINUXKPI_LINUX_LIST_H_
31 
32 #include "linux/compiler.h"
33 #include "los_list.h"
34 
35 #ifdef __cplusplus
36 #if __cplusplus
37 extern "C" {
38 #endif /* __cplusplus */
39 #endif /* __cplusplus */
40 
41 #ifndef prefetch
42 #define	prefetch(x)
43 #endif
44 
45 #define LINUX_LIST_HEAD_INIT(name) { &(name), &(name) }
46 
47 #define LINUX_LIST_HEAD(name) \
48 	struct list_head name = LINUX_LIST_HEAD_INIT(name)
49 
50 #ifndef LIST_HEAD_DEF
51 #define	LIST_HEAD_DEF
52 	struct list_head {
53 		struct list_head *next;
54 		struct list_head *prev;
55 	};
56 #endif
57 
58 #define container_of LOS_DL_LIST_ENTRY
59 
60 static inline void
INIT_LIST_HEAD(struct list_head * list)61 INIT_LIST_HEAD(struct list_head *list)
62 {
63 
64 	list->next = list->prev = list;
65 }
66 
67 static inline int
list_empty(const struct list_head * head)68 list_empty(const struct list_head *head)
69 {
70 
71 	return (head->next == head);
72 }
73 
74 static inline int
list_empty_careful(const struct list_head * head)75 list_empty_careful(const struct list_head *head)
76 {
77 	struct list_head *next = head->next;
78 
79 	return ((next == head) && (next == head->prev));
80 }
81 
82 static inline void
__list_del(struct list_head * prev,struct list_head * next)83 __list_del(struct list_head *prev, struct list_head *next)
84 {
85 	next->prev = prev;
86 	WRITE_ONCE(prev->next, next);
87 }
88 
89 static inline void
__list_del_entry(struct list_head * entry)90 __list_del_entry(struct list_head *entry)
91 {
92 
93 	__list_del(entry->prev, entry->next);
94 }
95 
96 static inline void
list_del(struct list_head * entry)97 list_del(struct list_head *entry)
98 {
99 
100 	__list_del(entry->prev, entry->next);
101 }
102 
103 static inline void
list_replace(struct list_head * old,struct list_head * new)104 list_replace(struct list_head *old, struct list_head *new)
105 {
106 	new->next = old->next;
107 	new->next->prev = new;
108 	new->prev = old->prev;
109 	new->prev->next = new;
110 }
111 
112 static inline void
list_replace_init(struct list_head * old,struct list_head * new)113 list_replace_init(struct list_head *old, struct list_head *new)
114 {
115 	list_replace(old, new);
116 	INIT_LIST_HEAD(old);
117 }
118 
119 static inline void
linux_list_add(struct list_head * new,struct list_head * prev,struct list_head * next)120 linux_list_add(struct list_head *new, struct list_head *prev,
121     struct list_head *next)
122 {
123 
124 	next->prev = new;
125 	new->next = next;
126 	new->prev = prev;
127 	prev->next = new;
128 }
129 
130 static inline void
list_del_init(struct list_head * entry)131 list_del_init(struct list_head *entry)
132 {
133 
134 	list_del(entry);
135 	INIT_LIST_HEAD(entry);
136 }
137 
138 #define	list_entry(ptr, type, field)	container_of(ptr, type, field)
139 
140 #define	list_first_entry(ptr, type, member) \
141 	list_entry((ptr)->next, type, member)
142 
143 #define	list_last_entry(ptr, type, member)	\
144 	list_entry((ptr)->prev, type, member)
145 
146 #define	list_first_entry_or_null(ptr, type, member) \
147 	(!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
148 
149 #define	list_next_entry(ptr, member)					\
150 	list_entry(((ptr)->member.next), typeof(*(ptr)), member)
151 
152 #define	list_safe_reset_next(ptr, n, member) \
153 	(n) = list_next_entry(ptr, member)
154 
155 #define	list_prev_entry(ptr, member)					\
156 	list_entry(((ptr)->member.prev), typeof(*(ptr)), member)
157 
158 #define	list_for_each(p, head)						\
159 	for (p = (head)->next; p != (head); p = (p)->next)
160 
161 #define	list_for_each_safe(p, n, head)					\
162 	for (p = (head)->next, n = (p)->next; p != (head); p = n, n = (p)->next)
163 
164 #define list_for_each_entry(p, h, field)				\
165 	for (p = list_entry((h)->next, typeof(*p), field); &(p)->field != (h); \
166 	    p = list_entry((p)->field.next, typeof(*p), field))
167 
168 #define list_for_each_entry_safe(p, n, h, field)			\
169 	for (p = list_entry((h)->next, typeof(*p), field),		\
170 	    n = list_entry((p)->field.next, typeof(*p), field); &(p)->field != (h);\
171 	    p = n, n = list_entry(n->field.next, typeof(*n), field))
172 
173 #define	list_for_each_entry_from(p, h, field) \
174 	for ( ; &(p)->field != (h); \
175 	    p = list_entry((p)->field.next, typeof(*p), field))
176 
177 #define	list_for_each_entry_continue(p, h, field)			\
178 	for (p = list_next_entry((p), field); &(p)->field != (h);	\
179 	    p = list_next_entry((p), field))
180 
181 #define	list_for_each_entry_safe_from(pos, n, head, member)			\
182 	for (n = list_entry((pos)->member.next, typeof(*pos), member);		\
183 	     &(pos)->member != (head);						\
184 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
185 
186 #define	list_for_each_entry_reverse(p, h, field)			\
187 	for (p = list_entry((h)->prev, typeof(*p), field); &(p)->field != (h); \
188 	    p = list_entry((p)->field.prev, typeof(*p), field))
189 
190 #define	list_for_each_entry_safe_reverse(p, n, h, field)		\
191 	for (p = list_entry((h)->prev, typeof(*p), field),		\
192 	    n = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
193 	    p = n, n = list_entry(n->field.prev, typeof(*n), field))
194 
195 #define	list_for_each_entry_continue_reverse(p, h, field) \
196 	for (p = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
197 	    p = list_entry((p)->field.prev, typeof(*p), field))
198 
199 #define	list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = (p)->prev)
200 
201 #define	list_for_each_entry_from_reverse(p, h, field)	\
202 	for (; &p->field != (h);			\
203 	     p = list_prev_entry(p, field))
204 
205 static inline void
list_add(struct list_head * new,struct list_head * head)206 list_add(struct list_head *new, struct list_head *head)
207 {
208 
209 	linux_list_add(new, head, head->next);
210 }
211 
212 static inline void
list_add_tail(struct list_head * new,struct list_head * head)213 list_add_tail(struct list_head *new, struct list_head *head)
214 {
215 
216 	linux_list_add(new, head->prev, head);
217 }
218 
219 static inline void
list_move(struct list_head * list,struct list_head * head)220 list_move(struct list_head *list, struct list_head *head)
221 {
222 
223 	list_del(list);
224 	list_add(list, head);
225 }
226 
227 static inline void
list_move_tail(struct list_head * entry,struct list_head * head)228 list_move_tail(struct list_head *entry, struct list_head *head)
229 {
230 
231 	list_del(entry);
232 	list_add_tail(entry, head);
233 }
234 
235 static inline void
list_rotate_to_front(struct list_head * entry,struct list_head * head)236 list_rotate_to_front(struct list_head *entry, struct list_head *head)
237 {
238 
239 	list_move_tail(entry, head);
240 }
241 
242 static inline void
list_bulk_move_tail(struct list_head * head,struct list_head * first,struct list_head * last)243 list_bulk_move_tail(struct list_head *head, struct list_head *first,
244     struct list_head *last)
245 {
246 	first->prev->next = last->next;
247 	last->next->prev = first->prev;
248 	head->prev->next = first;
249 	first->prev = head->prev;
250 	last->next = head;
251 	head->prev = last;
252 }
253 
254 static inline void
linux_list_splice(const struct list_head * list,struct list_head * prev,struct list_head * next)255 linux_list_splice(const struct list_head *list, struct list_head *prev,
256     struct list_head *next)
257 {
258 	struct list_head *first = NULL;
259 	struct list_head *last = NULL;
260 
261 	if (list_empty(list))
262 		return;
263 	first = list->next;
264 	last = list->prev;
265 	first->prev = prev;
266 	prev->next = first;
267 	last->next = next;
268 	next->prev = last;
269 }
270 
271 static inline void
list_splice(const struct list_head * list,struct list_head * head)272 list_splice(const struct list_head *list, struct list_head *head)
273 {
274 
275 	linux_list_splice(list, head, head->next);
276 }
277 
278 static inline void
list_splice_tail(struct list_head * list,struct list_head * head)279 list_splice_tail(struct list_head *list, struct list_head *head)
280 {
281 
282 	linux_list_splice(list, head->prev, head);
283 }
284 
285 static inline void
list_splice_init(struct list_head * list,struct list_head * head)286 list_splice_init(struct list_head *list, struct list_head *head)
287 {
288 
289 	linux_list_splice(list, head, head->next);
290 	INIT_LIST_HEAD(list);
291 }
292 
293 static inline void
list_splice_tail_init(struct list_head * list,struct list_head * head)294 list_splice_tail_init(struct list_head *list, struct list_head *head)
295 {
296 
297 	linux_list_splice(list, head->prev, head);
298 	INIT_LIST_HEAD(list);
299 }
300 
301 struct hlist_head {
302 	struct hlist_node *first;
303 };
304 
305 struct hlist_node {
306 	struct hlist_node *next, **pprev;
307 };
308 #define	HLIST_HEAD_INIT { }
309 #define	HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
310 #define	INIT_HLIST_HEAD(head) (head)->first = NULL
311 #define	INIT_HLIST_NODE(node)						\
312 do {									\
313 	(node)->next = NULL;						\
314 	(node)->pprev = NULL;						\
315 } while (0)
316 
317 static inline int
hlist_unhashed(const struct hlist_node * h)318 hlist_unhashed(const struct hlist_node *h)
319 {
320 
321 	return !h->pprev;
322 }
323 
324 static inline int
hlist_empty(const struct hlist_head * h)325 hlist_empty(const struct hlist_head *h)
326 {
327 
328 	return !READ_ONCE(h->first);
329 }
330 
331 static inline void
hlist_del(struct hlist_node * n)332 hlist_del(struct hlist_node *n)
333 {
334 
335 	WRITE_ONCE(*(n->pprev), n->next);
336 	if (n->next != NULL)
337 		n->next->pprev = n->pprev;
338 }
339 
340 static inline void
hlist_del_init(struct hlist_node * n)341 hlist_del_init(struct hlist_node *n)
342 {
343 
344 	if (hlist_unhashed(n))
345 		return;
346 	hlist_del(n);
347 	INIT_HLIST_NODE(n);
348 }
349 
350 static inline void
hlist_add_head(struct hlist_node * n,struct hlist_head * h)351 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
352 {
353 
354 	n->next = h->first;
355 	if (h->first != NULL)
356 		h->first->pprev = &n->next;
357 	WRITE_ONCE(h->first, n);
358 	n->pprev = &h->first;
359 }
360 
361 static inline void
hlist_add_before(struct hlist_node * n,struct hlist_node * next)362 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
363 {
364 
365 	n->pprev = next->pprev;
366 	n->next = next;
367 	next->pprev = &n->next;
368 	WRITE_ONCE(*(n->pprev), n);
369 }
370 
371 static inline void
hlist_add_behind(struct hlist_node * n,struct hlist_node * prev)372 hlist_add_behind(struct hlist_node *n, struct hlist_node *prev)
373 {
374 
375 	n->next = prev->next;
376 	WRITE_ONCE(prev->next, n);
377 	n->pprev = &prev->next;
378 
379 	if (n->next != NULL)
380 		n->next->pprev = &n->next;
381 }
382 
383 static inline void
hlist_move_list(struct hlist_head * old,struct hlist_head * new)384 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
385 {
386 
387 	new->first = old->first;
388 	if (new->first)
389 		new->first->pprev = &new->first;
390 	old->first = NULL;
391 }
392 
list_is_singular(const struct list_head * head)393 static inline int list_is_singular(const struct list_head *head)
394 {
395 	return !list_empty(head) && (head->next == head->prev);
396 }
397 
__list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)398 static inline void __list_cut_position(struct list_head *list,
399 		struct list_head *head, struct list_head *entry)
400 {
401 	struct list_head *new_first = entry->next;
402 	list->next = head->next;
403 	list->next->prev = list;
404 	list->prev = entry;
405 	entry->next = list;
406 	head->next = new_first;
407 	new_first->prev = head;
408 }
409 
list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)410 static inline void list_cut_position(struct list_head *list,
411 		struct list_head *head, struct list_head *entry)
412 {
413 	if (list_empty(head))
414 		return;
415 	if (list_is_singular(head) &&
416 		(head->next != entry && head != entry))
417 		return;
418 	if (entry == head)
419 		INIT_LIST_HEAD(list);
420 	else
421 		__list_cut_position(list, head, entry);
422 }
423 
list_is_first(const struct list_head * list,const struct list_head * head)424 static inline int list_is_first(const struct list_head *list,
425 				const struct list_head *head)
426 {
427 
428 	return (list->prev == head);
429 }
430 
list_is_last(const struct list_head * list,const struct list_head * head)431 static inline int list_is_last(const struct list_head *list,
432 				const struct list_head *head)
433 {
434 	return list->next == head;
435 }
436 
437 static inline size_t
list_count_nodes(const struct list_head * list)438 list_count_nodes(const struct list_head *list)
439 {
440 	const struct list_head *lh;
441 	size_t count;
442 
443 	count = 0;
444 	list_for_each(lh, list) {
445 		count++;
446 	}
447 
448 	return (count);
449 }
450 
451 #define	hlist_entry(ptr, type, field)	container_of(ptr, type, field)
452 
453 #define	hlist_for_each(p, head)						\
454 	for (p = (head)->first; p; p = (p)->next)
455 
456 #define	hlist_for_each_safe(p, n, head)					\
457 	for (p = (head)->first; p && ({ n = (p)->next; 1; }); p = n)
458 
459 #define	hlist_entry_safe(ptr, type, member) \
460 	((ptr) ? hlist_entry(ptr, type, member) : NULL)
461 
462 #define	hlist_for_each_entry(pos, head, member)				\
463 	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
464 	     pos;							\
465 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
466 
467 #define	hlist_for_each_entry_continue(pos, member)			\
468 	for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \
469 	     (pos);							\
470 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
471 
472 #define	hlist_for_each_entry_from(pos, member)				\
473 	for (; (pos);								\
474 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
475 
476 #define	hlist_for_each_entry_safe(pos, n, head, member)			\
477 	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \
478 	     (pos) && ({ n = (pos)->member.next; 1; });			\
479 	     pos = hlist_entry_safe(n, typeof(*(pos)), member))
480 
481 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 51300
482 extern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
483     const struct list_head *a, const struct list_head *b));
484 #else
485 extern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
486     struct list_head *a, struct list_head *b));
487 #endif
488 
489 #ifdef __cplusplus
490 #if __cplusplus
491 }
492 #endif /* __cplusplus */
493 #endif /* __cplusplus */
494 
495 #endif /* _LINUXKPI_LINUX_LIST_H_ */
496