1 /* This code has been stolen from Linux kernel :) */
2 /* distributed under GNU GPL version 2. */
8 * Simple doubly linked list implementation.
10 * Some of the internal functions ("__xxx") are useful when
11 * manipulating whole lists rather than single entries, as
12 * sometimes we already know the next/prev entries and we can
13 * generate better code by using them directly rather than
14 * using the generic single-entry routines.
18 struct list_head *next, *prev;
21 #define ATALK_LIST_HEAD_INIT(name) { &(name), &(name) }
23 #define ATALK_LIST_HEAD(name) \
24 struct list_head name = ATALK_LIST_HEAD_INIT(name)
26 #define ATALK_INIT_LIST_HEAD(ptr) do { \
27 (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 * new,
38 struct list_head * prev,
39 struct list_head * next)
48 * list_add - add a new entry
49 * @new: 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 *new, struct list_head *head)
57 __list_add(new, head, head->next);
61 * list_add_tail - add a new entry
62 * @new: 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 *new, struct list_head *head)
70 __list_add(new, 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.
90 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
92 static inline void list_del(struct list_head *entry)
94 __list_del(entry->prev, entry->next);
98 * list_del_init - deletes entry from list and reinitialize it.
99 * @entry: the element to delete from the list.
101 static inline void list_del_init(struct list_head *entry)
103 __list_del(entry->prev, entry->next);
104 ATALK_INIT_LIST_HEAD(entry);
108 * list_empty - tests whether a list is empty
109 * @head: the list to test.
111 static inline int list_empty(struct list_head *head)
113 return head->next == head;
117 * list_splice - join two lists
118 * @list: the new list to add.
119 * @head: the place to add it in the first list.
121 static inline void list_splice(struct list_head *list, struct list_head *head)
123 struct list_head *first = list->next;
126 struct list_head *last = list->prev;
127 struct list_head *at = head->next;
139 * list_entry - get the struct for this entry
140 * @ptr: the &struct list_head pointer.
141 * @type: the type of the struct this is embedded in.
142 * @member: the name of the list_struct within the struct.
144 #define list_entry(ptr, type, member) \
145 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
148 * list_for_each - iterate over a list
149 * @pos: the &struct list_head to use as a loop counter.
150 * @n: another &struct list_head to use as temporary storage
151 * @head: the head for your list.
153 #define list_for_each(pos, head) \
154 for (pos = (head)->next; pos != (head); \
158 * list_for_each_prev - iterate over a list in reverse order
159 * @pos: the &struct list_head to use as a loop counter.
160 * @head: the head for your list.
162 #define list_for_each_prev(pos, head) \
163 for (pos = (head)->prev; pos != (head); \