• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #ifndef _LINUX_LIST_H
4 #define _LINUX_LIST_H
5 
6 #ifndef offsetof
7 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
8 #endif
9 
10 #define container_of(ptr, type, member) ({			\
11 	const typeof(((type *)0)->member) * __mptr = (ptr);	\
12 	(type *)((char *)__mptr - offsetof(type, member));	\
13 	})
14 
15 /*
16  * These are non-NULL pointers that will result in page faults
17  * under normal circumstances, used to verify that nobody uses
18  * non-initialized list entries.
19  */
20 #define LIST_POISON1  ((void *) 0x00100100)
21 #define LIST_POISON2  ((void *) 0x00200200)
22 
23 /**
24  * Simple doubly linked list implementation.
25  *
26  * Some of the internal functions ("__xxx") are useful when
27  * manipulating whole lists rather than single entries, as
28  * sometimes we already know the next/prev entries and we can
29  * generate better code by using them directly rather than
30  * using the generic single-entry routines.
31  */
32 struct list_head {
33 	struct list_head *next, *prev;
34 };
35 
36 #define LIST_HEAD_INIT(name) { &(name), &(name) }
37 
38 #define LIST_HEAD(name) \
39 	struct list_head name = LIST_HEAD_INIT(name)
40 
41 #define INIT_LIST_HEAD(ptr) do { \
42 	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
43 } while (0)
44 
45 /*
46  * Insert a new entry between two known consecutive entries.
47  *
48  * This is only for internal list manipulation where we know
49  * the prev/next entries already!
50  */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)51 static inline void __list_add(struct list_head *new,
52 			      struct list_head *prev,
53 			      struct list_head *next)
54 {
55 	next->prev = new;
56 	new->next = next;
57 	new->prev = prev;
58 	prev->next = new;
59 }
60 
61 /**
62  * list_add - add a new entry
63  * @new: new entry to be added
64  * @head: list head to add it after
65  *
66  * Insert a new entry after the specified head.
67  * This is good for implementing stacks.
68  */
list_add(struct list_head * new,struct list_head * head)69 static inline void list_add(struct list_head *new, struct list_head *head)
70 {
71 	__list_add(new, head, head->next);
72 }
73 
74 /**
75  * list_add_tail - add a new entry
76  * @new: new entry to be added
77  * @head: list head to add it before
78  *
79  * Insert a new entry before the specified head.
80  * This is useful for implementing queues.
81  */
list_add_tail(struct list_head * new,struct list_head * head)82 static inline void list_add_tail(struct list_head *new, struct list_head *head)
83 {
84 	__list_add(new, head->prev, head);
85 }
86 
87 
88 /*
89  * Delete a list entry by making the prev/next entries
90  * point to each other.
91  *
92  * This is only for internal list manipulation where we know
93  * the prev/next entries already!
94  */
__list_del(struct list_head * prev,struct list_head * next)95 static inline void __list_del(struct list_head *prev, struct list_head *next)
96 {
97 	next->prev = prev;
98 	prev->next = next;
99 }
100 
101 /**
102  * list_del - deletes entry from list.
103  * @entry: the element to delete from the list.
104  * Note: list_empty on entry does not return true after this, the entry is
105  * in an undefined state.
106  */
list_del(struct list_head * entry)107 static inline void list_del(struct list_head *entry)
108 {
109 	__list_del(entry->prev, entry->next);
110 	entry->next = LIST_POISON1;
111 	entry->prev = LIST_POISON2;
112 }
113 
114 
115 
116 /**
117  * list_del_init - deletes entry from list and reinitialize it.
118  * @entry: the element to delete from the list.
119  */
list_del_init(struct list_head * entry)120 static inline void list_del_init(struct list_head *entry)
121 {
122 	__list_del(entry->prev, entry->next);
123 	INIT_LIST_HEAD(entry);
124 }
125 
126 /**
127  * list_move - delete from one list and add as another's head
128  * @list: the entry to move
129  * @head: the head that will precede our entry
130  */
list_move(struct list_head * list,struct list_head * head)131 static inline void list_move(struct list_head *list, struct list_head *head)
132 {
133 	__list_del(list->prev, list->next);
134 	list_add(list, head);
135 }
136 
137 /**
138  * list_move_tail - delete from one list and add as another's tail
139  * @list: the entry to move
140  * @head: the head that will follow our entry
141  */
list_move_tail(struct list_head * list,struct list_head * head)142 static inline void list_move_tail(struct list_head *list,
143 				  struct list_head *head)
144 {
145 	__list_del(list->prev, list->next);
146 	list_add_tail(list, head);
147 }
148 
149 /**
150  * list_empty - tests whether a list is empty
151  * @head: the list to test.
152  */
list_empty(const struct list_head * head)153 static inline int list_empty(const struct list_head *head)
154 {
155 	return head->next == head;
156 }
157 
__list_splice(struct list_head * list,struct list_head * head)158 static inline void __list_splice(struct list_head *list,
159 				 struct list_head *head)
160 {
161 	struct list_head *first = list->next;
162 	struct list_head *last = list->prev;
163 	struct list_head *at = head->next;
164 
165 	first->prev = head;
166 	head->next = first;
167 
168 	last->next = at;
169 	at->prev = last;
170 }
171 
172 /**
173  * list_splice - join two lists
174  * @list: the new list to add.
175  * @head: the place to add it in the first list.
176  */
list_splice(struct list_head * list,struct list_head * head)177 static inline void list_splice(struct list_head *list, struct list_head *head)
178 {
179 	if (!list_empty(list))
180 		__list_splice(list, head);
181 }
182 
183 /**
184  * list_splice_init - join two lists and reinitialise the emptied list.
185  * @list: the new list to add.
186  * @head: the place to add it in the first list.
187  *
188  * The list at @list is reinitialised
189  */
list_splice_init(struct list_head * list,struct list_head * head)190 static inline void list_splice_init(struct list_head *list,
191 				    struct list_head *head)
192 {
193 	if (!list_empty(list)) {
194 		__list_splice(list, head);
195 		INIT_LIST_HEAD(list);
196 	}
197 }
198 
199 /**
200  * list_entry - get the struct for this entry
201  * @ptr:	the &struct list_head pointer.
202  * @type:	the type of the struct this is embedded in.
203  * @member:	the name of the list_struct within the struct.
204  */
205 #define list_entry(ptr, type, member) \
206 	container_of(ptr, type, member)
207 
208 /**
209  * list_for_each - iterate over a list
210  * @pos:	the &struct list_head to use as a loop counter.
211  * @head:	the head for your list.
212  */
213 
214 #define list_for_each(pos, head) \
215 	for (pos = (head)->next; pos != (head);	\
216 		pos = pos->next)
217 
218 /**
219  * __list_for_each - iterate over a list
220  * @pos:	the &struct list_head to use as a loop counter.
221  * @head:	the head for your list.
222  *
223  * This variant differs from list_for_each() in that it's the
224  * simplest possible list iteration code, no prefetching is done.
225  * Use this for code that knows the list to be very short (empty
226  * or 1 entry) most of the time.
227  */
228 #define __list_for_each(pos, head) \
229 	for (pos = (head)->next; pos != (head); pos = pos->next)
230 
231 /**
232  * list_for_each_prev - iterate over a list backwards
233  * @pos:	the &struct list_head to use as a loop counter.
234  * @head:	the head for your list.
235  */
236 #define list_for_each_prev(pos, head) \
237 	for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
238 		pos = pos->prev)
239 
240 /**
241  * list_for_each_safe - iterate over a list safe against removal of list entry
242  * @pos:	the &struct list_head to use as a loop counter.
243  * @n:		another &struct list_head to use as temporary storage
244  * @head:	the head for your list.
245  */
246 #define list_for_each_safe(pos, n, head) \
247 	for (pos = (head)->next, n = pos->next; pos != (head); \
248 		pos = n, n = pos->next)
249 
250 /**
251  * list_for_each_entry - iterate over list of given type
252  * @pos:	the type * to use as a loop counter.
253  * @head:	the head for your list.
254  * @member:	the name of the list_struct within the struct.
255  */
256 #define list_for_each_entry(pos, head, member)				\
257 	for (pos = list_entry((head)->next, typeof(*pos), member);	\
258 	     &pos->member != (head);					\
259 	     pos = list_entry(pos->member.next, typeof(*pos), member))
260 
261 /**
262  * list_for_each_entry_reverse - iterate backwards over list of given type.
263  * @pos:	the type * to use as a loop counter.
264  * @head:	the head for your list.
265  * @member:	the name of the list_struct within the struct.
266  */
267 #define list_for_each_entry_reverse(pos, head, member)			\
268 	for (pos = list_entry((head)->prev, typeof(*pos), member);	\
269 	     &pos->member != (head);	\
270 	     pos = list_entry(pos->member.prev, typeof(*pos), member))
271 
272 /**
273  * list_prepare_entry - prepare a pos entry for use as a start point in
274  * list_for_each_entry_continue
275  * @pos:	the type * to use as a start point
276  * @head:	the head of the list
277  * @member:	the name of the list_struct within the struct.
278  */
279 #define list_prepare_entry(pos, head, member) \
280 	((pos) ? : list_entry(head, typeof(*pos), member))
281 
282 /**
283  * list_for_each_entry_continue - iterate over list of given type
284  * continuing after existing point
285  * @pos:	the type * to use as a loop counter.
286  * @head:	the head for your list.
287  * @member:	the name of the list_struct within the struct.
288  */
289 #define list_for_each_entry_continue(pos, head, member)			\
290 	for (pos = list_entry(pos->member.next, typeof(*pos), member);	\
291 	     &pos->member != (head);	\
292 	     pos = list_entry(pos->member.next, typeof(*pos), member))
293 
294 /**
295  * list_for_each_entry_safe - iterate over list of given type safe against
296  * removal of list entry
297  * @pos:	the type * to use as a loop counter.
298  * @n:		another type * to use as temporary storage
299  * @head:	the head for your list.
300  * @member:	the name of the list_struct within the struct.
301  */
302 #define list_for_each_entry_safe(pos, n, head, member)			\
303 	for (pos = list_entry((head)->next, typeof(*pos), member),	\
304 		n = list_entry(pos->member.next, typeof(*pos), member);	\
305 	     &pos->member != (head);					\
306 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
307 
308 /**
309  * list_for_each_entry_safe_continue - iterate over list of given type
310  * continuing after existing point safe against removal of list entry
311  * @pos:	the type * to use as a loop counter.
312  * @n:		another type * to use as temporary storage
313  * @head:	the head for your list.
314  * @member:	the name of the list_struct within the struct.
315  */
316 #define list_for_each_entry_safe_continue(pos, n, head, member)		\
317 	for (pos = list_entry(pos->member.next, typeof(*pos), member),	\
318 		n = list_entry(pos->member.next, typeof(*pos), member);	\
319 	     &pos->member != (head);					\
320 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
321 
322 /**
323  * list_for_each_entry_safe_reverse - iterate backwards over list of given
324  * type safe against removal of list entry
325  * @pos:	the type * to use as a loop counter.
326  * @n:		another type * to use as temporary storage
327  * @head:	the head for your list.
328  * @member:	the name of the list_struct within the struct.
329  */
330 #define list_for_each_entry_safe_reverse(pos, n, head, member)		\
331 	for (pos = list_entry((head)->prev, typeof(*pos), member),	\
332 		n = list_entry(pos->member.prev, typeof(*pos), member);	\
333 	     &pos->member != (head);					\
334 	     pos = n, n = list_entry(n->member.prev, typeof(*n), member))
335 
336 #endif
337