1 /* This code has been stolen from Linux kernel :) */
2 /* distributed under GNU GPL version 2. */
13 * Simple doubly linked list implementation.
15 * Some of the internal functions ("__xxx") are useful when
16 * manipulating whole lists rather than single entries, as
17 * sometimes we already know the next/prev entries and we can
18 * generate better code by using them directly rather than
19 * using the generic single-entry routines.
23 struct list_head *next, *prev;
26 #define ATALK_LIST_HEAD_INIT(name) { &(name), &(name) }
28 #define ATALK_LIST_HEAD(name) \
29 struct list_head name = ATALK_LIST_HEAD_INIT(name)
31 #define ATALK_INIT_LIST_HEAD(ptr) do { \
32 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
37 * Insert a new entry between two known consecutive entries.
39 * This is only for internal list manipulation where we know
40 * the prev/next entries already!
42 static __inline__ void __list_add(struct list_head * new,
43 struct list_head * prev,
44 struct list_head * next)
53 * list_add - add a new entry
54 * @new: new entry to be added
55 * @head: list head to add it after
57 * Insert a new entry after the specified head.
58 * This is good for implementing stacks.
60 static __inline__ void list_add(struct list_head *new, struct list_head *head)
62 __list_add(new, head, head->next);
66 * list_add_tail - add a new entry
67 * @new: new entry to be added
68 * @head: list head to add it before
70 * Insert a new entry before the specified head.
71 * This is useful for implementing queues.
73 static __inline__ void list_add_tail(struct list_head *new, struct list_head *head)
75 __list_add(new, head->prev, head);
79 * Delete a list entry by making the prev/next entries
80 * point to each other.
82 * This is only for internal list manipulation where we know
83 * the prev/next entries already!
85 static __inline__ void __list_del(struct list_head * prev,
86 struct list_head * next)
93 * list_del - deletes entry from list.
94 * @entry: the element to delete from the list.
95 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
97 static __inline__ void list_del(struct list_head *entry)
99 __list_del(entry->prev, entry->next);
103 * list_del_init - deletes entry from list and reinitialize it.
104 * @entry: the element to delete from the list.
106 static __inline__ void list_del_init(struct list_head *entry)
108 __list_del(entry->prev, entry->next);
109 ATALK_INIT_LIST_HEAD(entry);
113 * list_empty - tests whether a list is empty
114 * @head: the list to test.
116 static __inline__ int list_empty(struct list_head *head)
118 return head->next == head;
122 * list_splice - join two lists
123 * @list: the new list to add.
124 * @head: the place to add it in the first list.
126 static __inline__ void list_splice(struct list_head *list, struct list_head *head)
128 struct list_head *first = list->next;
131 struct list_head *last = list->prev;
132 struct list_head *at = head->next;
144 * list_entry - get the struct for this entry
145 * @ptr: the &struct list_head pointer.
146 * @type: the type of the struct this is embedded in.
147 * @member: the name of the list_struct within the struct.
149 #define list_entry(ptr, type, member) \
150 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
153 * list_for_each - iterate over a list
154 * @pos: the &struct list_head to use as a loop counter.
155 * @n: another &struct list_head to use as temporary storage
156 * @head: the head for your list.
158 #define list_for_each(pos, head) \
159 for (pos = (head)->next; pos != (head); \
163 * list_for_each_prev - iterate over a list in reverse order
164 * @pos: the &struct list_head to use as a loop counter.
165 * @head: the head for your list.
167 #define list_for_each_prev(pos, head) \
168 for (pos = (head)->prev; pos != (head); \