Fix the creation of the dumpdir directory in stress_floppy Makefile
[ltp-debian.git] / testcases / realtime / include / list.h
blob4e1552318f7e76e5237ec49d533ab184c70e62cf
1 /******************************************************************************
3 * Copyright © International Business Machines Corp., 2008
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * NAME
20 * list.h
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.
28 * USAGE:
29 * To be included in test cases
31 * AUTHOR
32 * Darren Hart <dvhltc@us.ibm.com>
34 * HISTORY
35 * 2006-Oct-17: Initial version by Darren Hart
37 *****************************************************************************/
39 #ifndef _LINUX_LIST_H
40 #define _LINUX_LIST_H
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.
47 #define LIST_POISON1 ((void *) 0x00100100)
48 #define LIST_POISON2 ((void *) 0x00200200)
51 * Simple doubly linked list implementation.
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.
60 struct list_head {
61 struct list_head *next, *prev;
64 #define LIST_HEAD_INIT(name) { &(name), &(name) }
66 #define LIST_HEAD(name) \
67 struct list_head name = LIST_HEAD_INIT(name)
69 static inline void INIT_LIST_HEAD(struct list_head *list)
71 list->next = list;
72 list->prev = list;
76 * Insert a new entry between two known consecutive entries.
78 * This is only for internal list manipulation where we know
79 * the prev/next entries already!
81 static inline void __list_add(struct list_head *new,
82 struct list_head *prev,
83 struct list_head *next)
85 next->prev = new;
86 new->next = next;
87 new->prev = prev;
88 prev->next = new;
91 /**
92 * list_add - add a new entry
93 * @new: new entry to be added
94 * @head: list head to add it after
96 * Insert a new entry after the specified head.
97 * This is good for implementing stacks.
99 static inline void list_add(struct list_head *new, struct list_head *head)
101 __list_add(new, head, head->next);
105 * list_add_tail - add a new entry
106 * @new: new entry to be added
107 * @head: list head to add it before
109 * Insert a new entry before the specified head.
110 * This is useful for implementing queues.
112 static inline void list_add_tail(struct list_head *new, struct list_head *head)
114 __list_add(new, head->prev, head);
118 * Delete a list entry by making the prev/next entries
119 * point to each other.
121 * This is only for internal list manipulation where we know
122 * the prev/next entries already!
124 static inline void __list_del(struct list_head * prev, struct list_head * next)
126 next->prev = prev;
127 prev->next = next;
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.
136 static inline void list_del(struct list_head *entry)
138 __list_del(entry->prev, entry->next);
139 entry->next = LIST_POISON1;
140 entry->prev = LIST_POISON2;
144 * list_del_init - deletes entry from list and reinitialize it.
145 * @entry: the element to delete from the list.
147 static inline void list_del_init(struct list_head *entry)
149 __list_del(entry->prev, entry->next);
150 INIT_LIST_HEAD(entry);
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
158 static inline void list_move(struct list_head *list, struct list_head *head)
160 __list_del(list->prev, list->next);
161 list_add(list, head);
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
169 static inline void list_move_tail(struct list_head *list,
170 struct list_head *head)
172 __list_del(list->prev, list->next);
173 list_add_tail(list, head);
177 * list_empty - tests whether a list is empty
178 * @head: the list to test.
180 static inline int list_empty(const struct list_head *head)
182 return head->next == head;
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
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.
195 * @head: the list to test.
197 static inline int list_empty_careful(const struct list_head *head)
199 struct list_head *next = head->next;
200 return (next == head) && (next == head->prev);
203 static inline void __list_splice(struct list_head *list,
204 struct list_head *head)
206 struct list_head *first = list->next;
207 struct list_head *last = list->prev;
208 struct list_head *at = head->next;
210 first->prev = head;
211 head->next = first;
213 last->next = at;
214 at->prev = last;
218 * list_splice - join two lists
219 * @list: the new list to add.
220 * @head: the place to add it in the first list.
222 static inline void list_splice(struct list_head *list, struct list_head *head)
224 if (!list_empty(list))
225 __list_splice(list, head);
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.
233 * The list at @list is reinitialised
235 static inline void list_splice_init(struct list_head *list,
236 struct list_head *head)
238 if (!list_empty(list)) {
239 __list_splice(list, head);
240 INIT_LIST_HEAD(list);
244 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
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.
252 #define container_of(ptr, type, member) ({ \
253 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
254 (type *)( (char *)__mptr - offsetof(type,member) );})
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.
263 #define list_entry(ptr, type, member) \
264 container_of(ptr, type, member)
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.
271 #define list_for_each(pos, head) \
272 for (pos = (head)->next; pos != (head); \
273 pos = pos->next)
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.
280 #define list_for_each_prev(pos, head) \
281 for (pos = (head)->prev; pos != (head); \
282 pos = pos->prev)
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.
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)
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.
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))
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.
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))
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.
323 #define list_prepare_entry(pos, head, member) \
324 ((pos) ? : list_entry(head, typeof(*pos), member))
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.
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))
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.
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))
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.
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))
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.
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))
379 #endif