• 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  * Delete a list entry by making the prev/next entries
89  * point to each other.
90  *
91  * This is only for internal list manipulation where we know
92  * the prev/next entries already!
93  */
__list_del(struct list_head * prev,struct list_head * next)94 static inline void __list_del(struct list_head *prev, struct list_head *next)
95 {
96 	next->prev = prev;
97 	prev->next = next;
98 }
99 
100 /**
101  * list_del - deletes entry from list.
102  * @entry: the element to delete from the list.
103  * Note: list_empty on entry does not return true after this, the entry is
104  * in an undefined state.
105  */
list_del(struct list_head * entry)106 static inline void list_del(struct list_head *entry)
107 {
108 	__list_del(entry->prev, entry->next);
109 	entry->next = LIST_POISON1;
110 	entry->prev = LIST_POISON2;
111 }
112 
113 /**
114  * list_del_init - deletes entry from list and reinitialize it.
115  * @entry: the element to delete from the list.
116  */
list_del_init(struct list_head * entry)117 static inline void list_del_init(struct list_head *entry)
118 {
119 	__list_del(entry->prev, entry->next);
120 	INIT_LIST_HEAD(entry);
121 }
122 
123 /**
124  * list_move - delete from one list and add as another's head
125  * @list: the entry to move
126  * @head: the head that will precede our entry
127  */
list_move(struct list_head * list,struct list_head * head)128 static inline void list_move(struct list_head *list, struct list_head *head)
129 {
130 	__list_del(list->prev, list->next);
131 	list_add(list, head);
132 }
133 
134 /**
135  * list_move_tail - delete from one list and add as another's tail
136  * @list: the entry to move
137  * @head: the head that will follow our entry
138  */
list_move_tail(struct list_head * list,struct list_head * head)139 static inline void list_move_tail(struct list_head *list,
140 				  struct list_head *head)
141 {
142 	__list_del(list->prev, list->next);
143 	list_add_tail(list, head);
144 }
145 
146 /**
147  * list_empty - tests whether a list is empty
148  * @head: the list to test.
149  */
list_empty(const struct list_head * head)150 static inline int list_empty(const struct list_head *head)
151 {
152 	return head->next == head;
153 }
154 
__list_splice(struct list_head * list,struct list_head * head)155 static inline void __list_splice(struct list_head *list,
156 				 struct list_head *head)
157 {
158 	struct list_head *first = list->next;
159 	struct list_head *last = list->prev;
160 	struct list_head *at = head->next;
161 
162 	first->prev = head;
163 	head->next = first;
164 
165 	last->next = at;
166 	at->prev = last;
167 }
168 
169 /**
170  * list_splice - join two lists
171  * @list: the new list to add.
172  * @head: the place to add it in the first list.
173  */
list_splice(struct list_head * list,struct list_head * head)174 static inline void list_splice(struct list_head *list, struct list_head *head)
175 {
176 	if (!list_empty(list))
177 		__list_splice(list, head);
178 }
179 
180 /**
181  * list_splice_init - join two lists and reinitialise the emptied list.
182  * @list: the new list to add.
183  * @head: the place to add it in the first list.
184  *
185  * The list at @list is reinitialised
186  */
list_splice_init(struct list_head * list,struct list_head * head)187 static inline void list_splice_init(struct list_head *list,
188 				    struct list_head *head)
189 {
190 	if (!list_empty(list)) {
191 		__list_splice(list, head);
192 		INIT_LIST_HEAD(list);
193 	}
194 }
195 
196 /**
197  * list_entry - get the struct for this entry
198  * @ptr:	the &struct list_head pointer.
199  * @type:	the type of the struct this is embedded in.
200  * @member:	the name of the list_struct within the struct.
201  */
202 #define list_entry(ptr, type, member) \
203 	container_of(ptr, type, member)
204 
205 /**
206  * list_for_each - iterate over a list
207  * @pos:	the &struct list_head to use as a loop counter.
208  * @head:	the head for your list.
209  */
210 
211 #define list_for_each(pos, head) \
212 	for (pos = (head)->next; pos != (head);	\
213 		pos = pos->next)
214 
215 /**
216  * __list_for_each - iterate over a list
217  * @pos:	the &struct list_head to use as a loop counter.
218  * @head:	the head for your list.
219  *
220  * This variant differs from list_for_each() in that it's the
221  * simplest possible list iteration code, no prefetching is done.
222  * Use this for code that knows the list to be very short (empty
223  * or 1 entry) most of the time.
224  */
225 #define __list_for_each(pos, head) \
226 	for (pos = (head)->next; pos != (head); pos = pos->next)
227 
228 /**
229  * list_for_each_prev - iterate over a list backwards
230  * @pos:	the &struct list_head to use as a loop counter.
231  * @head:	the head for your list.
232  */
233 #define list_for_each_prev(pos, head) \
234 	for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
235 		pos = pos->prev)
236 
237 /**
238  * list_for_each_safe - iterate over a list safe against removal of list entry
239  * @pos:	the &struct list_head to use as a loop counter.
240  * @n:		another &struct list_head to use as temporary storage
241  * @head:	the head for your list.
242  */
243 #define list_for_each_safe(pos, n, head) \
244 	for (pos = (head)->next, n = pos->next; pos != (head); \
245 		pos = n, n = pos->next)
246 
247 /**
248  * list_for_each_entry - iterate over list of given type
249  * @pos:	the type * to use as a loop counter.
250  * @head:	the head for your list.
251  * @member:	the name of the list_struct within the struct.
252  */
253 #define list_for_each_entry(pos, head, member)				\
254 	for (pos = list_entry((head)->next, typeof(*pos), member);	\
255 	     &pos->member != (head);					\
256 	     pos = list_entry(pos->member.next, typeof(*pos), member))
257 
258 /**
259  * list_for_each_entry_reverse - iterate backwards over list of given type.
260  * @pos:	the type * to use as a loop counter.
261  * @head:	the head for your list.
262  * @member:	the name of the list_struct within the struct.
263  */
264 #define list_for_each_entry_reverse(pos, head, member)			\
265 	for (pos = list_entry((head)->prev, typeof(*pos), member);	\
266 	     &pos->member != (head);	\
267 	     pos = list_entry(pos->member.prev, typeof(*pos), member))
268 
269 /**
270  * list_prepare_entry - prepare a pos entry for use as a start point in
271  * list_for_each_entry_continue
272  * @pos:	the type * to use as a start point
273  * @head:	the head of the list
274  * @member:	the name of the list_struct within the struct.
275  */
276 #define list_prepare_entry(pos, head, member) \
277 	((pos) ? : list_entry(head, typeof(*pos), member))
278 
279 /**
280  * list_for_each_entry_continue - iterate over list of given type
281  * continuing after existing point
282  * @pos:	the type * to use as a loop counter.
283  * @head:	the head for your list.
284  * @member:	the name of the list_struct within the struct.
285  */
286 #define list_for_each_entry_continue(pos, head, member)			\
287 	for (pos = list_entry(pos->member.next, typeof(*pos), member);	\
288 	     &pos->member != (head);	\
289 	     pos = list_entry(pos->member.next, typeof(*pos), member))
290 
291 /**
292  * list_for_each_entry_safe - iterate over list of given type safe against
293  * removal of list entry
294  * @pos:	the type * to use as a loop counter.
295  * @n:		another type * to use as temporary storage
296  * @head:	the head for your list.
297  * @member:	the name of the list_struct within the struct.
298  */
299 #define list_for_each_entry_safe(pos, n, head, member)			\
300 	for (pos = list_entry((head)->next, typeof(*pos), member),	\
301 		n = list_entry(pos->member.next, typeof(*pos), member);	\
302 	     &pos->member != (head);					\
303 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
304 
305 /**
306  * list_for_each_entry_safe_continue - iterate over list of given type
307  * continuing after existing point safe against removal of list entry
308  * @pos:	the type * to use as a loop counter.
309  * @n:		another type * to use as temporary storage
310  * @head:	the head for your list.
311  * @member:	the name of the list_struct within the struct.
312  */
313 #define list_for_each_entry_safe_continue(pos, n, head, member)		\
314 	for (pos = list_entry(pos->member.next, typeof(*pos), member),	\
315 		n = list_entry(pos->member.next, typeof(*pos), member);	\
316 	     &pos->member != (head);					\
317 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
318 
319 /**
320  * list_for_each_entry_safe_reverse - iterate backwards over list of given
321  * type safe against removal of list entry
322  * @pos:	the type * to use as a loop counter.
323  * @n:		another type * to use as temporary storage
324  * @head:	the head for your list.
325  * @member:	the name of the list_struct within the struct.
326  */
327 #define list_for_each_entry_safe_reverse(pos, n, head, member)		\
328 	for (pos = list_entry((head)->prev, typeof(*pos), member),	\
329 		n = list_entry(pos->member.prev, typeof(*pos), member);	\
330 	     &pos->member != (head);					\
331 	     pos = n, n = list_entry(n->member.prev, typeof(*n), member))
332 
333 #endif
334