1 #if !defined(_BLKID_LIST_H) && !defined(LIST_HEAD)
9 * Simple doubly linked list implementation.
11 * Some of the internal functions ("__xxx") are useful when
12 * manipulating whole lists rather than single entries, as
13 * sometimes we already know the next/prev entries and we can
14 * generate better code by using them directly rather than
15 * using the generic single-entry routines.
19 struct list_head *next, *prev;
22 #define LIST_HEAD_INIT(name) { &(name), &(name) }
24 #define LIST_HEAD(name) \
25 struct list_head name = LIST_HEAD_INIT(name)
27 #define INIT_LIST_HEAD(ptr) do { \
28 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
32 * Insert a new entry between two known consecutive entries.
34 * This is only for internal list manipulation where we know
35 * the prev/next entries already!
37 static __inline__ void __list_add(struct list_head * add,
38 struct list_head * prev,
39 struct list_head * next)
48 * list_add - add a new entry
49 * @add: new entry to be added
50 * @head: list head to add it after
52 * Insert a new entry after the specified head.
53 * This is good for implementing stacks.
55 static __inline__ void list_add(struct list_head *add, struct list_head *head)
57 __list_add(add, head, head->next);
61 * list_add_tail - add a new entry
62 * @add: new entry to be added
63 * @head: list head to add it before
65 * Insert a new entry before the specified head.
66 * This is useful for implementing queues.
68 static __inline__ void list_add_tail(struct list_head *add, struct list_head *head)
70 __list_add(add, head->prev, head);
74 * Delete a list entry by making the prev/next entries
75 * point to each other.
77 * This is only for internal list manipulation where we know
78 * the prev/next entries already!
80 static __inline__ void __list_del(struct list_head * prev,
81 struct list_head * next)
88 * list_del - deletes entry from list.
89 * @entry: the element to delete from the list.
91 * list_empty() on @entry does not return true after this, @entry is
92 * in an undefined state.
94 static __inline__ void list_del(struct list_head *entry)
96 __list_del(entry->prev, entry->next);
100 * list_del_init - deletes entry from list and reinitialize it.
101 * @entry: the element to delete from the list.
103 static __inline__ void list_del_init(struct list_head *entry)
105 __list_del(entry->prev, entry->next);
106 INIT_LIST_HEAD(entry);
110 * list_empty - tests whether a list is empty
111 * @head: the list to test.
113 static __inline__ int list_empty(struct list_head *head)
115 return head->next == head;
119 * list_splice - join two lists
120 * @list: the new list to add.
121 * @head: the place to add it in the first list.
123 static __inline__ void list_splice(struct list_head *list, struct list_head *head)
125 struct list_head *first = list->next;
128 struct list_head *last = list->prev;
129 struct list_head *at = head->next;
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.
145 #define list_entry(ptr, type, member) \
146 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
149 * list_for_each - iterate over elements in a list
150 * @pos: the &struct list_head to use as a loop counter.
151 * @head: the head for your list.
153 #define list_for_each(pos, head) \
154 for (pos = (head)->next; pos != (head); pos = pos->next)
157 * list_for_each_safe - iterate over elements in a list, but don't dereference
158 * pos after the body is done (in case it is freed)
159 * @pos: the &struct list_head to use as a loop counter.
160 * @pnext: the &struct list_head to use as a pointer to the next item.
161 * @head: the head for your list (not included in iteration).
163 #define list_for_each_safe(pos, pnext, head) \
164 for (pos = (head)->next, pnext = pos->next; pos != (head); \
165 pos = pnext, pnext = pos->next)
171 #endif /* _BLKID_LIST_H */