xref: /aosp_15_r20/external/ltp/testcases/realtime/include/list.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /******************************************************************************
2  *
3  *   Copyright © International Business Machines  Corp., 2008
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * NAME
20  *       list.h
21  *
22  * DESCRIPTION
23  *      This code was kindly borrowed from linux/include/linux/list.h
24  *      Some of the unneeded functions were removed for brevity sake.
25  *      In addition, the container functions were added so we could
26  *      access the container of the list from the list_head struct.
27  *
28  * USAGE:
29  *      To be included in test cases
30  *
31  * AUTHOR
32  *        Darren Hart <[email protected]>
33  *
34  * HISTORY
35  *      2006-Oct-17: Initial version by Darren Hart
36  *
37  *****************************************************************************/
38 
39 #ifndef _LINUX_LIST_H
40 #define _LINUX_LIST_H
41 
42 /*
43  * These are non-NULL pointers that will result in page faults
44  * under normal circumstances, used to verify that nobody uses
45  * non-initialized list entries.
46  */
47 #define LIST_POISON1  ((void *) 0x00100100)
48 #define LIST_POISON2  ((void *) 0x00200200)
49 
50 /*
51  * Simple doubly linked list implementation.
52  *
53  * Some of the internal functions ("__xxx") are useful when
54  * manipulating whole lists rather than single entries, as
55  * sometimes we already know the next/prev entries and we can
56  * generate better code by using them directly rather than
57  * using the generic single-entry routines.
58  */
59 
60 struct list_head {
61 	struct list_head *next, *prev;
62 };
63 
64 #define LIST_HEAD_INIT(name) { &(name), &(name) }
65 
66 #define LIST_HEAD(name) \
67 	struct list_head name = LIST_HEAD_INIT(name)
68 
INIT_LIST_HEAD(struct list_head * list)69 static inline void INIT_LIST_HEAD(struct list_head *list)
70 {
71 	list->next = list;
72 	list->prev = list;
73 }
74 
75 /*
76  * Insert a new entry between two known consecutive entries.
77  *
78  * This is only for internal list manipulation where we know
79  * the prev/next entries already!
80  */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)81 static inline void __list_add(struct list_head *new,
82 			      struct list_head *prev,
83 			      struct list_head *next)
84 {
85 	next->prev = new;
86 	new->next = next;
87 	new->prev = prev;
88 	prev->next = new;
89 }
90 
91 /**
92  * list_add - add a new entry
93  * @new: new entry to be added
94  * @head: list head to add it after
95  *
96  * Insert a new entry after the specified head.
97  * This is good for implementing stacks.
98  */
list_add(struct list_head * new,struct list_head * head)99 static inline void list_add(struct list_head *new, struct list_head *head)
100 {
101 	__list_add(new, head, head->next);
102 }
103 
104 /**
105  * list_add_tail - add a new entry
106  * @new: new entry to be added
107  * @head: list head to add it before
108  *
109  * Insert a new entry before the specified head.
110  * This is useful for implementing queues.
111  */
list_add_tail(struct list_head * new,struct list_head * head)112 static inline void list_add_tail(struct list_head *new, struct list_head *head)
113 {
114 	__list_add(new, head->prev, head);
115 }
116 
117 /*
118  * Delete a list entry by making the prev/next entries
119  * point to each other.
120  *
121  * This is only for internal list manipulation where we know
122  * the prev/next entries already!
123  */
__list_del(struct list_head * prev,struct list_head * next)124 static inline void __list_del(struct list_head * prev, struct list_head * next)
125 {
126 	next->prev = prev;
127 	prev->next = next;
128 }
129 
130 /**
131  * list_del - deletes entry from list.
132  * @entry: the element to delete from the list.
133  * Note: list_empty on entry does not return true after this, the entry is
134  * in an undefined state.
135  */
list_del(struct list_head * entry)136 static inline void list_del(struct list_head *entry)
137 {
138 	__list_del(entry->prev, entry->next);
139 	entry->next = LIST_POISON1;
140 	entry->prev = LIST_POISON2;
141 }
142 
143 /**
144  * list_del_init - deletes entry from list and reinitialize it.
145  * @entry: the element to delete from the list.
146  */
list_del_init(struct list_head * entry)147 static inline void list_del_init(struct list_head *entry)
148 {
149 	__list_del(entry->prev, entry->next);
150 	INIT_LIST_HEAD(entry);
151 }
152 
153 /**
154  * list_move - delete from one list and add as another's head
155  * @list: the entry to move
156  * @head: the head that will precede our entry
157  */
list_move(struct list_head * list,struct list_head * head)158 static inline void list_move(struct list_head *list, struct list_head *head)
159 {
160         __list_del(list->prev, list->next);
161         list_add(list, head);
162 }
163 
164 /**
165  * list_move_tail - delete from one list and add as another's tail
166  * @list: the entry to move
167  * @head: the head that will follow our entry
168  */
list_move_tail(struct list_head * list,struct list_head * head)169 static inline void list_move_tail(struct list_head *list,
170 				  struct list_head *head)
171 {
172         __list_del(list->prev, list->next);
173         list_add_tail(list, head);
174 }
175 
176 /**
177  * list_empty - tests whether a list is empty
178  * @head: the list to test.
179  */
list_empty(const struct list_head * head)180 static inline int list_empty(const struct list_head *head)
181 {
182 	return head->next == head;
183 }
184 
185 /**
186  * list_empty_careful - tests whether a list is
187  * empty _and_ checks that no other CPU might be
188  * in the process of still modifying either member
189  *
190  * NOTE: using list_empty_careful() without synchronization
191  * can only be safe if the only activity that can happen
192  * to the list entry is list_del_init(). Eg. it cannot be used
193  * if another CPU could re-list_add() it.
194  *
195  * @head: the list to test.
196  */
list_empty_careful(const struct list_head * head)197 static inline int list_empty_careful(const struct list_head *head)
198 {
199 	struct list_head *next = head->next;
200 	return (next == head) && (next == head->prev);
201 }
202 
__list_splice(struct list_head * list,struct list_head * head)203 static inline void __list_splice(struct list_head *list,
204 				 struct list_head *head)
205 {
206 	struct list_head *first = list->next;
207 	struct list_head *last = list->prev;
208 	struct list_head *at = head->next;
209 
210 	first->prev = head;
211 	head->next = first;
212 
213 	last->next = at;
214 	at->prev = last;
215 }
216 
217 /**
218  * list_splice - join two lists
219  * @list: the new list to add.
220  * @head: the place to add it in the first list.
221  */
list_splice(struct list_head * list,struct list_head * head)222 static inline void list_splice(struct list_head *list, struct list_head *head)
223 {
224 	if (!list_empty(list))
225 		__list_splice(list, head);
226 }
227 
228 /**
229  * list_splice_init - join two lists and reinitialise the emptied list.
230  * @list: the new list to add.
231  * @head: the place to add it in the first list.
232  *
233  * The list at @list is reinitialised
234  */
list_splice_init(struct list_head * list,struct list_head * head)235 static inline void list_splice_init(struct list_head *list,
236 				    struct list_head *head)
237 {
238 	if (!list_empty(list)) {
239 		__list_splice(list, head);
240 		INIT_LIST_HEAD(list);
241 	}
242 }
243 
244 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
245 /**
246  * container_of - cast a member of a structure out to the containing structure
247  * @ptr:	the pointer to the member.
248  * @type:	the type of the container struct this is embedded in.
249  * @member:	the name of the member within the struct.
250  *
251  */
252 #define container_of(ptr, type, member) ({			\
253 		const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
254 		(type *)( (char *)__mptr - offsetof(type,member) );})
255 
256 
257 /**
258  * list_entry - get the struct for this entry
259  * @ptr:	the &struct list_head pointer.
260  * @type:	the type of the struct this is embedded in.
261  * @member:	the name of the list_struct within the struct.
262  */
263 #define list_entry(ptr, type, member) \
264 	container_of(ptr, type, member)
265 
266 /**
267  * list_for_each	-	iterate over a list
268  * @pos:	the &struct list_head to use as a loop counter.
269  * @head:	the head for your list.
270  */
271 #define list_for_each(pos, head) \
272 	for (pos = (head)->next; pos != (head); \
273         	pos = pos->next)
274 
275 /**
276  * list_for_each_prev	-	iterate over a list backwards
277  * @pos:	the &struct list_head to use as a loop counter.
278  * @head:	the head for your list.
279  */
280 #define list_for_each_prev(pos, head) \
281 	for (pos = (head)->prev; pos != (head); \
282         	pos = pos->prev)
283 
284 /**
285  * list_for_each_safe	-	iterate over a list safe against removal of list entry
286  * @pos:	the &struct list_head to use as a loop counter.
287  * @n:		another &struct list_head to use as temporary storage
288  * @head:	the head for your list.
289  */
290 #define list_for_each_safe(pos, n, head) \
291 	for (pos = (head)->next, n = pos->next; pos != (head); \
292 		pos = n, n = pos->next)
293 
294 /**
295  * list_for_each_entry	-	iterate over list of given type
296  * @pos:	the type * to use as a loop counter.
297  * @head:	the head for your list.
298  * @member:	the name of the list_struct within the struct.
299  */
300 #define list_for_each_entry(pos, head, member)				\
301 	for (pos = list_entry((head)->next, typeof(*pos), member);	\
302 	     &pos->member != (head); 	\
303 	     pos = list_entry(pos->member.next, typeof(*pos), member))
304 
305 /**
306  * list_for_each_entry_reverse - iterate backwards over list of given type.
307  * @pos:	the type * to use as a loop counter.
308  * @head:	the head for your list.
309  * @member:	the name of the list_struct within the struct.
310  */
311 #define list_for_each_entry_reverse(pos, head, member)			\
312 	for (pos = list_entry((head)->prev, typeof(*pos), member);	\
313 	     &pos->member != (head); 	\
314 	     pos = list_entry(pos->member.prev, typeof(*pos), member))
315 
316 /**
317  * list_prepare_entry - prepare a pos entry for use as a start point in
318  *			list_for_each_entry_continue
319  * @pos:	the type * to use as a start point
320  * @head:	the head of the list
321  * @member:	the name of the list_struct within the struct.
322  */
323 #define list_prepare_entry(pos, head, member) \
324 	((pos) ? : list_entry(head, typeof(*pos), member))
325 
326 /**
327  * list_for_each_entry_continue -	iterate over list of given type
328  *			continuing after existing point
329  * @pos:	the type * to use as a loop counter.
330  * @head:	the head for your list.
331  * @member:	the name of the list_struct within the struct.
332  */
333 #define list_for_each_entry_continue(pos, head, member) 		\
334 	for (pos = list_entry(pos->member.next, typeof(*pos), member);	\
335 	     &pos->member != (head);	\
336 	     pos = list_entry(pos->member.next, typeof(*pos), member))
337 
338 /**
339  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
340  * @pos:	the type * to use as a loop counter.
341  * @n:		another type * to use as temporary storage
342  * @head:	the head for your list.
343  * @member:	the name of the list_struct within the struct.
344  */
345 #define list_for_each_entry_safe(pos, n, head, member)			\
346 	for (pos = list_entry((head)->next, typeof(*pos), member),	\
347 		n = list_entry(pos->member.next, typeof(*pos), member);	\
348 	     &pos->member != (head); 					\
349 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
350 
351 /**
352  * list_for_each_entry_safe_continue -	iterate over list of given type
353  *			continuing after existing point safe against removal of list entry
354  * @pos:	the type * to use as a loop counter.
355  * @n:		another type * to use as temporary storage
356  * @head:	the head for your list.
357  * @member:	the name of the list_struct within the struct.
358  */
359 #define list_for_each_entry_safe_continue(pos, n, head, member) 		\
360 	for (pos = list_entry(pos->member.next, typeof(*pos), member), 		\
361 		n = list_entry(pos->member.next, typeof(*pos), member);		\
362 	     &pos->member != (head);						\
363 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
364 
365 /**
366  * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
367  *				      removal of list entry
368  * @pos:	the type * to use as a loop counter.
369  * @n:		another type * to use as temporary storage
370  * @head:	the head for your list.
371  * @member:	the name of the list_struct within the struct.
372  */
373 #define list_for_each_entry_safe_reverse(pos, n, head, member)		\
374 	for (pos = list_entry((head)->prev, typeof(*pos), member),	\
375 		n = list_entry(pos->member.prev, typeof(*pos), member);	\
376 	     &pos->member != (head); 					\
377 	     pos = n, n = list_entry(n->member.prev, typeof(*n), member))
378 
379 #endif
380