drm: Import drm2+i915 work from FreeBSD
[dragonfly.git] / sys / dev / drm2 / drm_linux_list.h
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 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: src/sys/dev/drm2/drm_linux_list.h,v 1.1 2012/05/22 11:07:44 kib Exp $");
34
35 #ifndef _DRM_LINUX_LIST_H_
36 #define _DRM_LINUX_LIST_H_
37
38 struct list_head {
39         struct list_head *next, *prev;
40 };
41
42 #define list_entry(ptr, type, member) container_of(ptr,type,member)
43 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
44
45 static __inline__ void
46 INIT_LIST_HEAD(struct list_head *head) {
47         (head)->next = head;
48         (head)->prev = head;
49 }
50
51 #define LIST_HEAD_INIT(name) { &(name), &(name) }
52
53 #define DRM_LIST_HEAD(name) \
54         struct list_head name = LIST_HEAD_INIT(name)
55
56 static __inline__ int
57 list_empty(const struct list_head *head) {
58         return (head)->next == head;
59 }
60
61 static __inline__ void
62 list_add(struct list_head *new, struct list_head *head) {
63         (head)->next->prev = new;
64         (new)->next = (head)->next;
65         (new)->prev = head;
66         (head)->next = new;
67 }
68
69 static __inline__ void
70 list_add_tail(struct list_head *entry, struct list_head *head) {
71         (entry)->prev = (head)->prev;
72         (entry)->next = head;
73         (head)->prev->next = entry;
74         (head)->prev = entry;
75 }
76
77 static __inline__ void
78 list_del(struct list_head *entry) {
79         (entry)->next->prev = (entry)->prev;
80         (entry)->prev->next = (entry)->next;
81 }
82
83 static inline void list_replace(struct list_head *old,
84                                 struct list_head *new)
85 {
86         new->next = old->next;
87         new->next->prev = new;
88         new->prev = old->prev;
89         new->prev->next = new;
90 }
91
92 static inline void list_move(struct list_head *list, struct list_head *head)
93 {
94         list_del(list);
95         list_add(list, head);
96 }
97
98 static inline void list_move_tail(struct list_head *list,
99     struct list_head *head)
100 {
101         list_del(list);
102         list_add_tail(list, head);
103 }
104
105 static __inline__ void
106 list_del_init(struct list_head *entry) {
107         (entry)->next->prev = (entry)->prev;
108         (entry)->prev->next = (entry)->next;
109         INIT_LIST_HEAD(entry);
110 }
111
112 #define list_for_each(entry, head)                              \
113     for (entry = (head)->next; entry != head; entry = (entry)->next)
114
115 #define list_for_each_prev(entry, head) \
116         for (entry = (head)->prev; entry != (head); \
117                 entry = entry->prev)
118
119 #define list_for_each_safe(entry, temp, head)                   \
120     for (entry = (head)->next, temp = (entry)->next;            \
121         entry != head;                                          \
122         entry = temp, temp = entry->next)
123
124 #define list_for_each_entry(pos, head, member)                          \
125     for (pos = list_entry((head)->next, __typeof(*pos), member);        \
126         &pos->member != (head);                                         \
127         pos = list_entry(pos->member.next, __typeof(*pos), member))
128
129 #define list_for_each_entry_continue_reverse(pos, head, member)         \
130         for (pos = list_entry(pos->member.prev, __typeof(*pos), member);  \
131              &pos->member != (head);                                    \
132              pos = list_entry(pos->member.prev, __typeof(*pos), member))
133
134 /**
135  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
136  * @pos:        the type * to use as a loop cursor.
137  * @n:          another type * to use as temporary storage
138  * @head:       the head for your list.
139  * @member:     the name of the list_struct within the struct.
140  */
141 #define list_for_each_entry_safe(pos, n, head, member)                  \
142         for (pos = list_entry((head)->next, __typeof(*pos), member),    \
143             n = list_entry(pos->member.next, __typeof(*pos), member);   \
144             &pos->member != (head);                                     \
145             pos = n, n = list_entry(n->member.next, __typeof(*n), member))
146
147 #define list_first_entry(ptr, type, member) \
148         list_entry((ptr)->next, type, member)
149
150
151 static inline void
152 __list_splice(const struct list_head *list, struct list_head *prev,
153     struct list_head *next)
154 {
155         struct list_head *first = list->next;
156         struct list_head *last = list->prev;
157
158         first->prev = prev;
159         prev->next = first;
160
161         last->next = next;
162         next->prev = last;
163 }
164
165 static inline void
166 list_splice(const struct list_head *list, struct list_head *head)
167 {
168         if (list_empty(list))
169                 return;
170
171         __list_splice(list, head, head->next);
172 }
173
174 void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
175     struct list_head *a, struct list_head *b));
176
177 #endif /* _DRM_LINUX_LIST_H_ */