2 * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
3 * Copyright (C) 1999-2008 by Theodore Ts'o
5 * (based on list.h from e2fsprogs)
8 #ifndef UTIL_LINUX_LIST_H
9 #define UTIL_LINUX_LIST_H
11 /* TODO: use AC_C_INLINE */
13 #define _INLINE_ static __inline__
14 #else /* For Watcom C */
15 #define _INLINE_ static inline
19 * Simple doubly linked list implementation.
21 * Some of the internal functions ("__xxx") are useful when
22 * manipulating whole lists rather than single entries, as
23 * sometimes we already know the next/prev entries and we can
24 * generate better code by using them directly rather than
25 * using the generic single-entry routines.
29 struct list_head *next, *prev;
32 #define LIST_HEAD_INIT(name) { &(name), &(name) }
34 #define LIST_HEAD(name) \
35 struct list_head name = LIST_HEAD_INIT(name)
37 #define INIT_LIST_HEAD(ptr) do { \
38 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
42 * Insert a new entry between two known consecutive entries.
44 * This is only for internal list manipulation where we know
45 * the prev/next entries already!
47 _INLINE_ void __list_add(struct list_head * add,
48 struct list_head * prev,
49 struct list_head * next)
58 * list_add - add a new entry
59 * @add: new entry to be added
60 * @head: list head to add it after
62 * Insert a new entry after the specified head.
63 * This is good for implementing stacks.
65 _INLINE_ void list_add(struct list_head *add, struct list_head *head)
67 __list_add(add, head, head->next);
71 * list_add_tail - add a new entry
72 * @add: new entry to be added
73 * @head: list head to add it before
75 * Insert a new entry before the specified head.
76 * This is useful for implementing queues.
78 _INLINE_ void list_add_tail(struct list_head *add, struct list_head *head)
80 __list_add(add, head->prev, head);
84 * Delete a list entry by making the prev/next entries
85 * point to each other.
87 * This is only for internal list manipulation where we know
88 * the prev/next entries already!
90 _INLINE_ void __list_del(struct list_head * prev,
91 struct list_head * next)
98 * list_del - deletes entry from list.
99 * @entry: the element to delete from the list.
101 * list_empty() on @entry does not return true after this, @entry is
102 * in an undefined state.
104 _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 _INLINE_ void list_del_init(struct list_head *entry)
115 __list_del(entry->prev, entry->next);
116 INIT_LIST_HEAD(entry);
120 * list_empty - tests whether a list is empty
121 * @head: the list to test.
123 _INLINE_ int list_empty(struct list_head *head)
125 return head->next == head;
129 * list_last_entry - tests whether is entry last in the list
130 * @entry: the entry to test.
131 * @head: the list to test.
133 _INLINE_ int list_last_entry(struct list_head *entry, struct list_head *head)
135 return head->prev == entry;
139 * list_splice - join two lists
140 * @list: the new list to add.
141 * @head: the place to add it in the first list.
143 _INLINE_ void list_splice(struct list_head *list, struct list_head *head)
145 struct list_head *first = list->next;
148 struct list_head *last = list->prev;
149 struct list_head *at = head->next;
160 * list_entry - get the struct for this entry
161 * @ptr: the &struct list_head pointer.
162 * @type: the type of the struct this is embedded in.
163 * @member: the name of the list_struct within the struct.
165 #define list_entry(ptr, type, member) \
166 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
169 * list_for_each - iterate over elements in a list
170 * @pos: the &struct list_head to use as a loop counter.
171 * @head: the head for your list.
173 #define list_for_each(pos, head) \
174 for (pos = (head)->next; pos != (head); pos = pos->next)
177 * list_for_each_backwardly - iterate over elements in a list in reverse
178 * @pos: the &struct list_head to use as a loop counter.
179 * @head: the head for your list.
181 #define list_for_each_backwardly(pos, head) \
182 for (pos = (head)->prev; pos != (head); pos = pos->prev)
185 * list_for_each_safe - iterate over elements in a list, but don't dereference
186 * pos after the body is done (in case it is freed)
187 * @pos: the &struct list_head to use as a loop counter.
188 * @pnext: the &struct list_head to use as a pointer to the next item.
189 * @head: the head for your list (not included in iteration).
191 #define list_for_each_safe(pos, pnext, head) \
192 for (pos = (head)->next, pnext = pos->next; pos != (head); \
193 pos = pnext, pnext = pos->next)
197 #endif /* UTIL_LINUX_LIST_H */