ia64/xen-unstable

view extras/mini-os/include/list.h @ 18110:60828077e175

mini-os is missing list_top in its list.h

Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jul 18 15:44:39 2008 +0100 (2008-07-18)
parents 06d84bf87159
children 3ab0e76fb8e2
line source
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
4 /*
5 * Simple doubly linked list implementation.
6 *
7 * Some of the internal functions ("__xxx") are useful when
8 * manipulating whole lists rather than single entries, as
9 * sometimes we already know the next/prev entries and we can
10 * generate better code by using them directly rather than
11 * using the generic single-entry routines.
12 */
14 struct list_head {
15 struct list_head *next, *prev;
16 };
18 #define LIST_HEAD_INIT(name) { &(name), &(name) }
20 #define LIST_HEAD(name) \
21 struct list_head name = LIST_HEAD_INIT(name)
23 #define INIT_LIST_HEAD(ptr) do { \
24 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
25 } while (0)
27 #define list_top(head, type, member) \
28 ({ \
29 struct list_head *_head = (head); \
30 list_empty(_head) ? NULL : list_entry(_head->next, type, member); \
31 })
33 /*
34 * Insert a new entry between two known consecutive entries.
35 *
36 * This is only for internal list manipulation where we know
37 * the prev/next entries already!
38 */
39 static __inline__ void __list_add(struct list_head * new,
40 struct list_head * prev,
41 struct list_head * next)
42 {
43 next->prev = new;
44 new->next = next;
45 new->prev = prev;
46 prev->next = new;
47 }
49 /**
50 * list_add - add a new entry
51 * @new: new entry to be added
52 * @head: list head to add it after
53 *
54 * Insert a new entry after the specified head.
55 * This is good for implementing stacks.
56 */
57 static __inline__ void list_add(struct list_head *new, struct list_head *head)
58 {
59 __list_add(new, head, head->next);
60 }
62 /**
63 * list_add_tail - add a new entry
64 * @new: new entry to be added
65 * @head: list head to add it before
66 *
67 * Insert a new entry before the specified head.
68 * This is useful for implementing queues.
69 */
70 static __inline__ void list_add_tail(struct list_head *new, struct list_head *head)
71 {
72 __list_add(new, head->prev, head);
73 }
75 /*
76 * Delete a list entry by making the prev/next entries
77 * point to each other.
78 *
79 * This is only for internal list manipulation where we know
80 * the prev/next entries already!
81 */
82 static __inline__ void __list_del(struct list_head * prev,
83 struct list_head * next)
84 {
85 next->prev = prev;
86 prev->next = next;
87 }
89 /**
90 * list_del - deletes entry from list.
91 * @entry: the element to delete from the list.
92 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
93 */
94 static __inline__ void list_del(struct list_head *entry)
95 {
96 __list_del(entry->prev, entry->next);
97 }
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 }
109 /**
110 * list_empty - tests whether a list is empty
111 * @head: the list to test.
112 */
113 static __inline__ int list_empty(struct list_head *head)
114 {
115 return head->next == head;
116 }
118 /**
119 * list_splice - join two lists
120 * @list: the new list to add.
121 * @head: the place to add it in the first list.
122 */
123 static __inline__ void list_splice(struct list_head *list, struct list_head *head)
124 {
125 struct list_head *first = list->next;
127 if (first != list) {
128 struct list_head *last = list->prev;
129 struct list_head *at = head->next;
131 first->prev = head;
132 head->next = first;
134 last->next = at;
135 at->prev = last;
136 }
137 }
139 /**
140 * list_entry - get the struct for this entry
141 * @ptr: the &struct list_head pointer.
142 * @type: the type of the struct this is embedded in.
143 * @member: the name of the list_struct within the struct.
144 */
145 #define list_entry(ptr, type, member) \
146 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
148 /**
149 * list_for_each - iterate over a list
150 * @pos: the &struct list_head to use as a loop counter.
151 * @head: the head for your list.
152 */
153 #define list_for_each(pos, head) \
154 for (pos = (head)->next; pos != (head); pos = pos->next)
156 /**
157 * list_for_each_safe - iterate over a list safe against removal of list entry
158 * @pos: the &struct list_head to use as a loop counter.
159 * @n: another &struct list_head to use as temporary storage
160 * @head: the head for your list.
161 */
162 #define list_for_each_safe(pos, n, head) \
163 for (pos = (head)->next, n = pos->next; pos != (head); \
164 pos = n, n = pos->next)
166 /**
167 * list_for_each_entry - iterate over list of given type
168 * @pos: the type * to use as a loop counter.
169 * @head: the head for your list.
170 * @member: the name of the list_struct within the struct.
171 */
172 #define list_for_each_entry(pos, head, member) \
173 for (pos = list_entry((head)->next, typeof(*pos), member); \
174 &pos->member != (head); \
175 pos = list_entry(pos->member.next, typeof(*pos), member))
177 /**
178 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
179 * @pos: the type * to use as a loop counter.
180 * @n: another type * to use as temporary storage
181 * @head: the head for your list.
182 * @member: the name of the list_struct within the struct.
183 */
184 #define list_for_each_entry_safe(pos, n, head, member) \
185 for (pos = list_entry((head)->next, typeof(*pos), member), \
186 n = list_entry(pos->member.next, typeof(*pos), member); \
187 &pos->member != (head); \
188 pos = n, n = list_entry(n->member.next, typeof(*n), member))
189 #endif /* _LINUX_LIST_H */