]>
git.zerfleddert.de Git - amt/blob - list.h
4 * Simple doubly linked list implementation.
5 * -- shameless stolen from the linux kernel sources
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.
15 struct list_head
*next
, *prev
;
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); \
28 * Insert a new entry between two known consecutive entries.
30 * This is only for internal list manipulation where we know
31 * the prev/next entries already!
33 static __inline__
void __list_add(struct list_head
* new,
34 struct list_head
* prev
,
35 struct list_head
* next
)
44 * list_add - add a new entry
45 * @new: new entry to be added
46 * @head: list head to add it after
48 * Insert a new entry after the specified head.
49 * This is good for implementing stacks.
51 static __inline__
void list_add(struct list_head
*new, struct list_head
*head
)
53 __list_add(new, head
, head
->next
);
57 * list_add_tail - add a new entry
58 * @new: new entry to be added
59 * @head: list head to add it before
61 * Insert a new entry before the specified head.
62 * This is useful for implementing queues.
64 static __inline__
void list_add_tail(struct list_head
*new, struct list_head
*head
)
66 __list_add(new, head
->prev
, head
);
70 * Delete a list entry by making the prev/next entries
71 * point to each other.
73 * This is only for internal list manipulation where we know
74 * the prev/next entries already!
76 static __inline__
void __list_del(struct list_head
* prev
,
77 struct list_head
* next
)
84 * list_del - deletes entry from list.
85 * @entry: the element to delete from the list.
86 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
88 static __inline__
void list_del(struct list_head
*entry
)
90 __list_del(entry
->prev
, entry
->next
);
94 * list_del_init - deletes entry from list and reinitialize it.
95 * @entry: the element to delete from the list.
97 static __inline__
void list_del_init(struct list_head
*entry
)
99 __list_del(entry
->prev
, entry
->next
);
100 INIT_LIST_HEAD(entry
);
104 * list_empty - tests whether a list is empty
105 * @head: the list to test.
107 static __inline__
int list_empty(struct list_head
*head
)
109 return head
->next
== head
;
113 * list_splice - join two lists
114 * @list: the new list to add.
115 * @head: the place to add it in the first list.
117 static __inline__
void list_splice(struct list_head
*list
, struct list_head
*head
)
119 struct list_head
*first
= list
->next
;
122 struct list_head
*last
= list
->prev
;
123 struct list_head
*at
= head
->next
;
134 * list_entry - get the struct for this entry
135 * @ptr: the &struct list_head pointer.
136 * @type: the type of the struct this is embedded in.
137 * @member: the name of the list_struct within the struct.
139 #define list_entry(ptr, type, member) \
140 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
143 * list_for_each - iterate over a list
144 * @pos: the &struct list_head to use as a loop counter.
145 * @head: the head for your list.
147 #define list_for_each(pos, head) \
148 for (pos = (head)->next; pos != (head); pos = pos->next)
151 * list_for_each_safe - iterate over a list safe against removal of list entry
152 * @pos: the &struct list_head to use as a loop counter.
153 * @n: another &struct list_head to use as temporary storage
154 * @head: the head for your list.
156 #define list_for_each_safe(pos, n, head) \
157 for (pos = (head)->next, n = pos->next; pos != (head); \
158 pos = n, n = pos->next)
161 * list_for_each_prev - iterate over a list in reverse order
162 * @pos: the &struct list_head to use as a loop counter.
163 * @head: the head for your list.
165 #define list_for_each_prev(pos, head) \
166 for (pos = (head)->prev; pos != (head); pos = pos->prev)
168 #endif /* _LIST_H_ */