xref: /openbmc/u-boot/include/linux/list.h (revision 69df3c4d)
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
3 
4 #ifndef ARCH_HAS_PREFETCH
5 #define ARCH_HAS_PREFETCH
6 static inline void prefetch(const void *x) {;}
7 #endif
8 
9 /*
10  * Simple doubly linked list implementation.
11  *
12  * Some of the internal functions ("__xxx") are useful when
13  * manipulating whole lists rather than single entries, as
14  * sometimes we already know the next/prev entries and we can
15  * generate better code by using them directly rather than
16  * using the generic single-entry routines.
17  */
18 
19 struct list_head {
20 	struct list_head *next, *prev;
21 };
22 
23 #define LIST_HEAD_INIT(name) { &(name), &(name) }
24 
25 #define LIST_HEAD(name) \
26 	struct list_head name = LIST_HEAD_INIT(name)
27 
28 #define INIT_LIST_HEAD(ptr) do { \
29 	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
30 } while (0)
31 
32 /*
33  * Insert a new entry between two known consecutive entries.
34  *
35  * This is only for internal list manipulation where we know
36  * the prev/next entries already!
37  */
38 static inline void __list_add(struct list_head *new,
39 			      struct list_head *prev,
40 			      struct list_head *next)
41 {
42 	next->prev = new;
43 	new->next = next;
44 	new->prev = prev;
45 	prev->next = new;
46 }
47 
48 /**
49  * list_add - add a new entry
50  * @new: new entry to be added
51  * @head: list head to add it after
52  *
53  * Insert a new entry after the specified head.
54  * This is good for implementing stacks.
55  */
56 static inline void list_add(struct list_head *new, struct list_head *head)
57 {
58 	__list_add(new, head, head->next);
59 }
60 
61 /**
62  * list_add_tail - add a new entry
63  * @new: new entry to be added
64  * @head: list head to add it before
65  *
66  * Insert a new entry before the specified head.
67  * This is useful for implementing queues.
68  */
69 static inline void list_add_tail(struct list_head *new, struct list_head *head)
70 {
71 	__list_add(new, head->prev, head);
72 }
73 
74 /*
75  * Delete a list entry by making the prev/next entries
76  * point to each other.
77  *
78  * This is only for internal list manipulation where we know
79  * the prev/next entries already!
80  */
81 static inline void __list_del(struct list_head *prev, struct list_head *next)
82 {
83 	next->prev = prev;
84 	prev->next = next;
85 }
86 
87 /**
88  * list_del - deletes entry from list.
89  * @entry: the element to delete from the list.
90  * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
91  */
92 static inline void list_del(struct list_head *entry)
93 {
94 	__list_del(entry->prev, entry->next);
95 	entry->next = (void *) 0;
96 	entry->prev = (void *) 0;
97 }
98 
99 /**
100  * list_del_init - deletes entry from list and reinitialize it.
101  * @entry: the element to delete from the list.
102  */
103 static inline void list_del_init(struct list_head *entry)
104 {
105 	__list_del(entry->prev, entry->next);
106 	INIT_LIST_HEAD(entry);
107 }
108 
109 /**
110  * list_move - delete from one list and add as another's head
111  * @list: the entry to move
112  * @head: the head that will precede our entry
113  */
114 static inline void list_move(struct list_head *list, struct list_head *head)
115 {
116 	__list_del(list->prev, list->next);
117 	list_add(list, head);
118 }
119 
120 /**
121  * list_move_tail - delete from one list and add as another's tail
122  * @list: the entry to move
123  * @head: the head that will follow our entry
124  */
125 static inline void list_move_tail(struct list_head *list,
126 				  struct list_head *head)
127 {
128 	__list_del(list->prev, list->next);
129 	list_add_tail(list, head);
130 }
131 
132 /**
133  * list_empty - tests whether a list is empty
134  * @head: the list to test.
135  */
136 static inline int list_empty(struct list_head *head)
137 {
138 	return head->next == head;
139 }
140 
141 static inline void __list_splice(struct list_head *list,
142 				 struct list_head *head)
143 {
144 	struct list_head *first = list->next;
145 	struct list_head *last = list->prev;
146 	struct list_head *at = head->next;
147 
148 	first->prev = head;
149 	head->next = first;
150 
151 	last->next = at;
152 	at->prev = last;
153 }
154 
155 /**
156  * list_splice - join two lists
157  * @list: the new list to add.
158  * @head: the place to add it in the first list.
159  */
160 static inline void list_splice(struct list_head *list, struct list_head *head)
161 {
162 	if (!list_empty(list))
163 		__list_splice(list, head);
164 }
165 
166 /**
167  * list_splice_init - join two lists and reinitialise the emptied list.
168  * @list: the new list to add.
169  * @head: the place to add it in the first list.
170  *
171  * The list at @list is reinitialised
172  */
173 static inline void list_splice_init(struct list_head *list,
174 				    struct list_head *head)
175 {
176 	if (!list_empty(list)) {
177 		__list_splice(list, head);
178 		INIT_LIST_HEAD(list);
179 	}
180 }
181 
182 /**
183  * list_entry - get the struct for this entry
184  * @ptr:	the &struct list_head pointer.
185  * @type:	the type of the struct this is embedded in.
186  * @member:	the name of the list_struct within the struct.
187  */
188 #define list_entry(ptr, type, member) \
189 	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
190 
191 /**
192  * list_for_each	-	iterate over a list
193  * @pos:	the &struct list_head to use as a loop counter.
194  * @head:	the head for your list.
195  */
196 #define list_for_each(pos, head) \
197 	for (pos = (head)->next, prefetch(pos->next); pos != (head); \
198 		pos = pos->next, prefetch(pos->next))
199 /**
200  * list_for_each_prev	-	iterate over a list backwards
201  * @pos:	the &struct list_head to use as a loop counter.
202  * @head:	the head for your list.
203  */
204 #define list_for_each_prev(pos, head) \
205 	for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
206 		pos = pos->prev, prefetch(pos->prev))
207 
208 /**
209  * list_for_each_safe	-	iterate over a list safe against removal of list entry
210  * @pos:	the &struct list_head to use as a loop counter.
211  * @n:		another &struct list_head to use as temporary storage
212  * @head:	the head for your list.
213  */
214 #define list_for_each_safe(pos, n, head) \
215 	for (pos = (head)->next, n = pos->next; pos != (head); \
216 		pos = n, n = pos->next)
217 
218 /**
219  * list_for_each_entry	-	iterate over list of given type
220  * @pos:	the type * to use as a loop counter.
221  * @head:	the head for your list.
222  * @member:	the name of the list_struct within the struct.
223  */
224 #define list_for_each_entry(pos, head, member)				\
225 	for (pos = list_entry((head)->next, typeof(*pos), member),	\
226 		     prefetch(pos->member.next);			\
227 	     &pos->member != (head);					\
228 	     pos = list_entry(pos->member.next, typeof(*pos), member),	\
229 		     prefetch(pos->member.next))
230 
231 /**
232  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
233  * @pos:	the type * to use as a loop counter.
234  * @n:		another type * to use as temporary storage
235  * @head:	the head for your list.
236  * @member:	the name of the list_struct within the struct.
237  */
238 #define list_for_each_entry_safe(pos, n, head, member)			\
239 	for (pos = list_entry((head)->next, typeof(*pos), member),	\
240 		n = list_entry(pos->member.next, typeof(*pos), member); \
241 	     &pos->member != (head);					\
242 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
243 
244 /**
245  * list_for_each_entry_continue -	iterate over list of given type
246  *			continuing after existing point
247  * @pos:	the type * to use as a loop counter.
248  * @head:	the head for your list.
249  * @member:	the name of the list_struct within the struct.
250  */
251 #define list_for_each_entry_continue(pos, head, member)			\
252 	for (pos = list_entry(pos->member.next, typeof(*pos), member),	\
253 		     prefetch(pos->member.next);			\
254 	     &pos->member != (head);					\
255 	     pos = list_entry(pos->member.next, typeof(*pos), member),	\
256 		     prefetch(pos->member.next))
257 
258 #endif
259