xref: /netbsd-src/sys/external/bsd/drm/dist/bsd-core/drm_linux_list.h (revision 4add633a5d2fec50da1d714e01bc407420d679e9)
1 /* drm_linux_list.h -- linux list functions for the BSDs.
2  * Created: Mon Apr 7 14:30:16 1999 by anholt@FreeBSD.org
3  */
4 /*-
5  * Copyright 2003 Eric Anholt
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the next
16  * paragraph) shall be included in all copies or substantial portions of the
17  * Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Authors:
28  *    Eric Anholt <anholt@FreeBSD.org>
29  *
30  */
31 
32 #ifndef _DRM_LINUX_LIST_H_
33 #define _DRM_LINUX_LIST_H_
34 
35 struct list_head {
36 	struct list_head *next, *prev;
37 };
38 
39 #define list_entry(ptr, type, member) container_of(ptr,type,member)
40 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
41 
42 static __inline__ void
INIT_LIST_HEAD(struct list_head * head)43 INIT_LIST_HEAD(struct list_head *head) {
44 	(head)->next = head;
45 	(head)->prev = head;
46 }
47 
48 static __inline__ int
list_empty(struct list_head * head)49 list_empty(struct list_head *head) {
50 	return (head)->next == head;
51 }
52 
53 static __inline__ void
list_add(struct list_head * new,struct list_head * head)54 list_add(struct list_head *new, struct list_head *head) {
55         (head)->next->prev = new;
56         (new)->next = (head)->next;
57         (new)->prev = head;
58         (head)->next = new;
59 }
60 
61 static __inline__ void
list_add_tail(struct list_head * entry,struct list_head * head)62 list_add_tail(struct list_head *entry, struct list_head *head) {
63 	(entry)->prev = (head)->prev;
64 	(entry)->next = head;
65 	(head)->prev->next = entry;
66 	(head)->prev = entry;
67 }
68 
69 static __inline__ void
list_del(struct list_head * entry)70 list_del(struct list_head *entry) {
71 	(entry)->next->prev = (entry)->prev;
72 	(entry)->prev->next = (entry)->next;
73 }
74 
75 static __inline__ void
list_del_init(struct list_head * entry)76 list_del_init(struct list_head *entry) {
77 	(entry)->next->prev = (entry)->prev;
78 	(entry)->prev->next = (entry)->next;
79 	INIT_LIST_HEAD(entry);
80 }
81 
82 #define list_for_each(entry, head)				\
83     for (entry = (head)->next; entry != head; entry = (entry)->next)
84 
85 #define list_for_each_prev(entry, head) \
86         for (entry = (head)->prev; entry != (head); \
87                 entry = entry->prev)
88 
89 #define list_for_each_safe(entry, temp, head)			\
90     for (entry = (head)->next, temp = (entry)->next;		\
91 	entry != head; 						\
92 	entry = temp, temp = entry->next)
93 
94 /**
95  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
96  * @pos:        the type * to use as a loop cursor.
97  * @n:          another type * to use as temporary storage
98  * @head:       the head for your list.
99  * @member:     the name of the list_struct within the struct.
100  */
101 #define list_for_each_entry_safe(pos, n, head, member)			\
102 	for (pos = list_entry((head)->next, __typeof(*pos), member),	\
103 	    n = list_entry(pos->member.next, __typeof(*pos), member);	\
104 	    &pos->member != (head);					\
105 	    pos = n, n = list_entry(n->member.next, __typeof(*n), member))
106 
107 #endif /* _DRM_LINUX_LIST_H_ */
108