1 /* drm_linux_list.h -- linux list functions for the BSDs.
2 * Created: Mon Apr 7 14:30:16 1999 by anholt@FreeBSD.org
5 * Copyright 2003 Eric Anholt
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:
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
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.
28 * Eric Anholt <anholt@FreeBSD.org>
30 * $FreeBSD: src/sys/dev/drm2/drm_linux_list.h,v 1.1 2012/05/22 11:07:44 kib Exp $
33 #ifndef _DRM_LINUX_LIST_H_
34 #define _DRM_LINUX_LIST_H_
37 struct list_head *next, *prev;
40 #define list_entry(ptr, type, member) container_of(ptr,type,member)
41 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
43 static __inline__ void
44 INIT_LIST_HEAD(struct list_head *head) {
49 #define LIST_HEAD_INIT(name) { &(name), &(name) }
51 #define DRM_LIST_HEAD(name) \
52 struct list_head name = LIST_HEAD_INIT(name)
55 list_empty(const struct list_head *head) {
56 return (head)->next == head;
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;
67 static __inline__ void
68 list_add_tail(struct list_head *entry, struct list_head *head) {
69 (entry)->prev = (head)->prev;
71 (head)->prev->next = entry;
75 static __inline__ void
76 list_del(struct list_head *entry) {
77 (entry)->next->prev = (entry)->prev;
78 (entry)->prev->next = (entry)->next;
81 static inline void list_replace(struct list_head *old,
82 struct list_head *new)
84 new->next = old->next;
85 new->next->prev = new;
86 new->prev = old->prev;
87 new->prev->next = new;
90 static inline void list_move(struct list_head *list, struct list_head *head)
96 static inline void list_move_tail(struct list_head *list,
97 struct list_head *head)
100 list_add_tail(list, head);
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);
110 #define list_for_each(entry, head) \
111 for (entry = (head)->next; entry != head; entry = (entry)->next)
113 #define list_for_each_prev(entry, head) \
114 for (entry = (head)->prev; entry != (head); \
117 #define list_for_each_safe(entry, temp, head) \
118 for (entry = (head)->next, temp = (entry)->next; \
120 entry = temp, temp = entry->next)
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))
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))
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.
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))
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))
150 #define list_first_entry(ptr, type, member) \
151 list_entry((ptr)->next, type, member)
155 __list_splice(const struct list_head *list, struct list_head *prev,
156 struct list_head *next)
158 struct list_head *first = list->next;
159 struct list_head *last = list->prev;
169 list_splice(const struct list_head *list, struct list_head *head)
171 if (list_empty(list))
174 __list_splice(list, head, head->next);
177 void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
178 struct list_head *a, struct list_head *b));
180 #endif /* _DRM_LINUX_LIST_H_ */