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