Add elf_core.c into the EXTRA_SOURCES for the static library
[mit.git] / list.h
blob95ca43c47e079435a2944b54b4287469b9628620
1 /* Stolen from Linux Kernel Source's list.h -- GPL. */
2 #ifndef _MODINITTOOLS_LIST_H
3 #define _MODINITTOOLS_LIST_H
5 #undef offsetof
6 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
8 /**
9 * container_of - cast a member of a structure out to the containing structure
11 * @ptr: the pointer to the member.
12 * @type: the type of the container struct this is embedded in.
13 * @member: the name of the member within the struct.
16 #define container_of(ptr, type, member) ({ \
17 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
18 (type *)( (char *)__mptr - offsetof(type,member) );})
21 * Simple doubly linked list implementation.
23 * Some of the internal functions ("__xxx") are useful when
24 * manipulating whole lists rather than single entries, as
25 * sometimes we already know the next/prev entries and we can
26 * generate better code by using them directly rather than
27 * using the generic single-entry routines.
30 struct list_head {
31 struct list_head *next, *prev;
34 #define LIST_HEAD_INIT(name) { &(name), &(name) }
36 #define LIST_HEAD(name) \
37 struct list_head name = LIST_HEAD_INIT(name)
39 #define INIT_LIST_HEAD(ptr) do { \
40 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
41 } while (0)
44 * Insert a new entry between two known consecutive entries.
46 * This is only for internal list manipulation where we know
47 * the prev/next entries already!
49 static inline void __list_add(struct list_head *new,
50 struct list_head *prev,
51 struct list_head *next)
53 next->prev = new;
54 new->next = next;
55 new->prev = prev;
56 prev->next = new;
59 /**
60 * list_add - add a new entry
61 * @new: new entry to be added
62 * @head: list head to add it after
64 * Insert a new entry after the specified head.
65 * This is good for implementing stacks.
67 static inline void list_add(struct list_head *new, struct list_head *head)
69 __list_add(new, head, head->next);
72 /**
73 * list_add_tail - add a new entry
74 * @new: new entry to be added
75 * @head: list head to add it before
77 * Insert a new entry before the specified head.
78 * This is useful for implementing queues.
80 static inline void list_add_tail(struct list_head *new, struct list_head *head)
82 __list_add(new, head->prev, head);
86 * Delete a list entry by making the prev/next entries
87 * point to each other.
89 * This is only for internal list manipulation where we know
90 * the prev/next entries already!
92 static inline void __list_del(struct list_head * prev, struct list_head * next)
94 next->prev = prev;
95 prev->next = next;
98 /**
99 * list_del - deletes entry from list.
100 * @entry: the element to delete from the list.
101 * Note: list_empty on entry does not return true after this, the entry is
102 * in an undefined state.
104 static inline void list_del(struct list_head *entry)
106 __list_del(entry->prev, entry->next);
110 * list_del_init - deletes entry from list and reinitialize it.
111 * @entry: the element to delete from the list.
113 static inline void list_del_init(struct list_head *entry)
115 __list_del(entry->prev, entry->next);
116 INIT_LIST_HEAD(entry);
120 * list_move - delete from one list and add as another's head
121 * @list: the entry to move
122 * @head: the head that will precede our entry
124 static inline void list_move(struct list_head *list, struct list_head *head)
126 __list_del(list->prev, list->next);
127 list_add(list, head);
131 * list_move_tail - delete from one list and add as another's tail
132 * @list: the entry to move
133 * @head: the head that will follow our entry
135 static inline void list_move_tail(struct list_head *list,
136 struct list_head *head)
138 __list_del(list->prev, list->next);
139 list_add_tail(list, head);
143 * list_empty - tests whether a list is empty
144 * @head: the list to test.
146 static inline int list_empty(struct list_head *head)
148 return head->next == head;
151 static inline void __list_splice(struct list_head *list,
152 struct list_head *head)
154 struct list_head *first = list->next;
155 struct list_head *last = list->prev;
156 struct list_head *at = head->next;
158 first->prev = head;
159 head->next = first;
161 last->next = at;
162 at->prev = last;
166 * list_splice - join two lists
167 * @list: the new list to add.
168 * @head: the place to add it in the first list.
170 static inline void list_splice(struct list_head *list, struct list_head *head)
172 if (!list_empty(list))
173 __list_splice(list, head);
177 * list_splice_init - join two lists and reinitialise the emptied list.
178 * @list: the new list to add.
179 * @head: the place to add it in the first list.
181 * The list at @list is reinitialised
183 static inline void list_splice_init(struct list_head *list,
184 struct list_head *head)
186 if (!list_empty(list)) {
187 __list_splice(list, head);
188 INIT_LIST_HEAD(list);
193 * list_entry - get the struct for this entry
194 * @ptr: the &struct list_head pointer.
195 * @type: the type of the struct this is embedded in.
196 * @member: the name of the list_struct within the struct.
198 #define list_entry(ptr, type, member) \
199 container_of(ptr, type, member)
202 * list_for_each - iterate over a list
203 * @pos: the &struct list_head to use as a loop counter.
204 * @head: the head for your list.
206 #define list_for_each(pos, head) \
207 for (pos = (head)->next; pos != (head); pos = pos->next)
210 * list_for_each_prev - iterate over a list backwards
211 * @pos: the &struct list_head to use as a loop counter.
212 * @head: the head for your list.
214 #define list_for_each_prev(pos, head) \
215 for (pos = (head)->prev; pos != (head); pos = pos->prev)
218 * list_for_each_safe - iterate over a list safe against removal of list entry
219 * @pos: the &struct list_head to use as a loop counter.
220 * @n: another &struct list_head to use as temporary storage
221 * @head: the head for your list.
223 #define list_for_each_safe(pos, n, head) \
224 for (pos = (head)->next, n = pos->next; pos != (head); \
225 pos = n, n = pos->next)
228 * list_for_each_entry - iterate over list of given type
229 * @pos: the type * to use as a loop counter.
230 * @head: the head for your list.
231 * @member: the name of the list_struct within the struct.
233 #define list_for_each_entry(pos, head, member) \
234 for (pos = list_entry((head)->next, typeof(*pos), member); \
235 &pos->member != (head); \
236 pos = list_entry(pos->member.next, typeof(*pos), member))
239 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
240 * @pos: the type * to use as a loop cursor.
241 * @n: another type * to use as temporary storage
242 * @head: the head for your list.
243 * @member: the name of the list_struct within the struct.
245 #define list_for_each_entry_safe(pos, n, head, member) \
246 for (pos = list_entry((head)->next, typeof(*pos), member), \
247 n = list_entry(pos->member.next, typeof(*pos), member); \
248 &pos->member != (head); \
249 pos = n, n = list_entry(n->member.next, typeof(*n), member))
251 #endif