5
* Simple doubly linked list implementation.
6
* -- shameless stolen from the linux kernel sources
8
* Some of the internal functions ("__xxx") are useful when
9
* manipulating whole lists rather than single entries, as
10
* sometimes we already know the next/prev entries and we can
11
* generate better code by using them directly rather than
12
* using the generic single-entry routines.
16
struct list_head *next, *prev;
19
#define LIST_HEAD_INIT(name) { &(name), &(name) }
21
#define LIST_HEAD(name) \
22
struct list_head name = LIST_HEAD_INIT(name)
24
#define INIT_LIST_HEAD(ptr) do { \
25
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
29
* Insert a item entry between two known consecutive entries.
31
* This is only for internal list manipulation where we know
32
* the prev/next entries already!
34
static __inline__ void __list_add(struct list_head * item,
35
struct list_head * prev,
36
struct list_head * next)
45
* list_add - add a item entry
46
* @item: item entry to be added
47
* @head: list head to add it after
49
* Insert a item entry after the specified head.
50
* This is good for implementing stacks.
52
static __inline__ void list_add(struct list_head *item, struct list_head *head)
54
__list_add(item, head, head->next);
58
* list_add_tail - add a item entry
59
* @item: item entry to be added
60
* @head: list head to add it before
62
* Insert a item entry before the specified head.
63
* This is useful for implementing queues.
65
static __inline__ void list_add_tail(struct list_head *item, struct list_head *head)
67
__list_add(item, head->prev, head);
71
* Delete a list entry by making the prev/next entries
72
* point to each other.
74
* This is only for internal list manipulation where we know
75
* the prev/next entries already!
77
static __inline__ void __list_del(struct list_head * prev,
78
struct list_head * next)
85
* list_del - deletes entry from list.
86
* @entry: the element to delete from the list.
87
* Note: list_empty on entry does not return true after this, the entry is in an undefined state.
89
static __inline__ void list_del(struct list_head *entry)
91
__list_del(entry->prev, entry->next);
95
* list_del_init - deletes entry from list and reinitialize it.
96
* @entry: the element to delete from the list.
98
static __inline__ void list_del_init(struct list_head *entry)
100
__list_del(entry->prev, entry->next);
101
INIT_LIST_HEAD(entry);
105
* list_empty - tests whether a list is empty
106
* @head: the list to test.
108
static __inline__ int list_empty(struct list_head *head)
110
return head->next == head;
114
* list_splice - join two lists
115
* @list: the item list to add.
116
* @head: the place to add it in the first list.
118
static __inline__ void list_splice(struct list_head *list, struct list_head *head)
120
struct list_head *first = list->next;
123
struct list_head *last = list->prev;
124
struct list_head *at = head->next;
135
* list_entry - get the struct for this entry
136
* @ptr: the &struct list_head pointer.
137
* @type: the type of the struct this is embedded in.
138
* @member: the name of the list_struct within the struct.
140
#define list_entry(ptr, type, member) \
141
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
144
* list_for_each - iterate over a list
145
* @pos: the &struct list_head to use as a loop counter.
146
* @head: the head for your list.
148
#define list_for_each(pos, head) \
149
for (pos = (head)->next; pos != (head); pos = pos->next)
152
* list_for_each_safe - iterate over a list safe against removal of list entry
153
* @pos: the &struct list_head to use as a loop counter.
154
* @n: another &struct list_head to use as temporary storage
155
* @head: the head for your list.
157
#define list_for_each_safe(pos, n, head) \
158
for (pos = (head)->next, n = pos->next; pos != (head); \
159
pos = n, n = pos->next)
162
* list_for_each_prev - iterate over a list in reverse order
163
* @pos: the &struct list_head to use as a loop counter.
164
* @head: the head for your list.
166
#define list_for_each_prev(pos, head) \
167
for (pos = (head)->prev; pos != (head); pos = pos->prev)