Merge with Linux 2.4.0-test6-pre9.
[linux-2.6/linux-mips.git] / include / linux / list.h
blobed38faa2159e0b267ab9418da5ac9a965e4404e7
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
4 #ifdef __KERNEL__
6 /*
7 * Simple doubly linked list implementation.
9 * Some of the internal functions ("__xxx") are useful when
10 * manipulating whole lists rather than single entries, as
11 * sometimes we already know the next/prev entries and we can
12 * generate better code by using them directly rather than
13 * using the generic single-entry routines.
16 struct list_head {
17 struct list_head *next, *prev;
20 #define LIST_HEAD_INIT(name) { &(name), &(name) }
22 #define LIST_HEAD(name) \
23 struct list_head name = LIST_HEAD_INIT(name)
25 #define INIT_LIST_HEAD(ptr) do { \
26 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
27 } while (0)
30 * Insert a new entry between two known consecutive entries.
32 * This is only for internal list manipulation where we know
33 * the prev/next entries already!
35 static __inline__ void __list_add(struct list_head * new,
36 struct list_head * prev,
37 struct list_head * next)
39 next->prev = new;
40 new->next = next;
41 new->prev = prev;
42 prev->next = new;
45 /**
46 * list_add - add a new entry
47 * @new: new entry to be added
48 * @head: list head to add it after
50 * Insert a new entry after the specified head.
51 * This is good for implementing stacks.
53 static __inline__ void list_add(struct list_head *new, struct list_head *head)
55 __list_add(new, head, head->next);
58 /**
59 * list_add_tail - add a new entry
60 * @new: new entry to be added
61 * @head: list head to add it before
63 * Insert a new entry before the specified head.
64 * This is useful for implementing queues.
66 static __inline__ void list_add_tail(struct list_head *new, struct list_head *head)
68 __list_add(new, head->prev, head);
72 * Delete a list entry by making the prev/next entries
73 * point to each other.
75 * This is only for internal list manipulation where we know
76 * the prev/next entries already!
78 static __inline__ void __list_del(struct list_head * prev,
79 struct list_head * next)
81 next->prev = prev;
82 prev->next = next;
85 /**
86 * list_del - deletes entry from list.
87 * @entry: the element to delete from the list.
88 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
90 static __inline__ void list_del(struct list_head *entry)
92 __list_del(entry->prev, entry->next);
95 /**
96 * list_del_init - deletes entry from list and reinitialize it.
97 * @entry: the element to delete from the list.
99 static __inline__ void list_del_init(struct list_head *entry)
101 __list_del(entry->prev, entry->next);
102 INIT_LIST_HEAD(entry);
106 * list_empty - tests whether a list is empty
107 * @head: the list to test.
109 static __inline__ int list_empty(struct list_head *head)
111 return head->next == head;
115 * list_splice - join two lists
116 * @list: the new list to add.
117 * @head: the place to add it in the first list.
119 static __inline__ void list_splice(struct list_head *list, struct list_head *head)
121 struct list_head *first = list->next;
123 if (first != list) {
124 struct list_head *last = list->prev;
125 struct list_head *at = head->next;
127 first->prev = head;
128 head->next = first;
130 last->next = at;
131 at->prev = last;
136 * list_entry - get the struct for this entry
137 * @ptr: the &struct list_head pointer.
138 * @type: the type of the struct this is embedded in.
139 * @member: the name of the list_struct within the struct.
141 #define list_entry(ptr, type, member) \
142 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
145 * list_for_each - iterate over a list
146 * @pos: the &struct list_head to use as a loop counter.
147 * @head: the head for your list.
149 #define list_for_each(pos, head) \
150 for (pos = (head)->next; pos != (head); pos = pos->next)
152 #endif /* __KERNEL__ */
154 #endif