Move all block size reductions to shrink_block()
[hed.git] / util / lists.h
blob1aa84c9865d9a197288a9e314c97b6358a957987
1 /* $Id: lists.h,v 1.41 2004/07/14 00:24:24 jonas Exp $ */
3 #ifndef EL__UTIL_LISTS_H
4 #define EL__UTIL_LISTS_H
6 /*
7 * 'Hoo now!' replied Treebeard. 'Hoo! Now that would be telling! Not so hasty.
8 * And *I* am doing the asking. You are in *my* country. What are *you*,
9 * I wonder? I cannot place you. You do not seem to come in the old lists that
10 * I learned when I was young. But that was a long, long time ago, and they may
11 * have made new lists. Let me see! Let me see! How did it go?
14 #include <stddef.h>
16 #include <libhed/types.h>
18 /* Fix namespace clash with system headers (like FreeBSD's, all hail BSD!). */
19 #undef LIST_HEAD
20 #undef list_head
21 #define list_head hed_list_head
24 * Simple doubly linked list implementation.
26 * Some of the internal functions ("__xxx") are useful when
27 * manipulating whole lists rather than single entries, as
28 * sometimes we already know the next/prev entries and we can
29 * generate better code by using them directly rather than
30 * using the generic single-entry routines.
33 #define LIST_HEAD_INIT(name) { &(name), &(name) }
35 #define LIST_HEAD(name) \
36 struct list_head name = LIST_HEAD_INIT(name)
38 static inline void INIT_LIST_HEAD(struct list_head *list)
40 list->next = list;
41 list->prev = list;
45 * Insert a new entry between two known consecutive entries.
47 * This is only for internal list manipulation where we know
48 * the prev/next entries already!
50 static inline void __list_add(struct list_head *new,
51 struct list_head *prev,
52 struct list_head *next)
54 next->prev = new;
55 new->next = next;
56 new->prev = prev;
57 prev->next = new;
60 /**
61 * list_add - add a new entry
62 * @new: new entry to be added
63 * @head: list head to add it after
65 * Insert a new entry after the specified head.
66 * This is good for implementing stacks.
68 static inline void list_add(struct list_head *new, struct list_head *head)
70 __list_add(new, head, head->next);
73 /**
74 * list_add_tail - add a new entry
75 * @new: new entry to be added
76 * @head: list head to add it before
78 * Insert a new entry before the specified head.
79 * This is useful for implementing queues.
81 static inline void list_add_tail(struct list_head *new, struct list_head *head)
83 __list_add(new, head->prev, head);
88 * Delete a list entry by making the prev/next entries
89 * point to each other.
91 * This is only for internal list manipulation where we know
92 * the prev/next entries already!
94 static inline void __list_del(struct list_head * prev, struct list_head * next)
96 next->prev = prev;
97 prev->next = next;
101 * list_del - deletes entry from list.
102 * @entry: the element to delete from the list.
103 * Note: list_empty on entry does not return true after this, the entry is
104 * in an undefined state.
106 static inline void list_del(struct list_head *entry)
108 __list_del(entry->prev, entry->next);
109 #ifndef NDEBUG
110 entry->next = entry->prev = NULL;
111 #endif
115 * list_del_init - deletes entry from list and reinitialize it.
116 * @entry: the element to delete from the list.
118 static inline void list_del_init(struct list_head *entry)
120 __list_del(entry->prev, entry->next);
121 INIT_LIST_HEAD(entry);
125 * list_move - delete from one list and add as another's head
126 * @list: the entry to move
127 * @head: the head that will precede our entry
129 static inline void list_move(struct list_head *list, struct list_head *head)
131 __list_del(list->prev, list->next);
132 list_add(list, head);
136 * list_move_tail - delete from one list and add as another's tail
137 * @list: the entry to move
138 * @head: the head that will follow our entry
140 static inline void list_move_tail(struct list_head *list,
141 struct list_head *head)
143 __list_del(list->prev, list->next);
144 list_add_tail(list, head);
148 * list_is_last - tests whether @list is the last entry in list @head
149 * @list: the entry to test
150 * @head: the head of the list
152 static inline int list_is_last(const struct list_head *list,
153 const struct list_head *head)
155 return list->next == head;
159 * list_empty - tests whether a list is empty
160 * @head: the list to test.
162 static inline int list_empty(const struct list_head *head)
164 return head->next == head;
167 static inline void __list_splice(const struct list_head *list,
168 struct list_head *head)
170 struct list_head *first = list->next;
171 struct list_head *last = list->prev;
172 struct list_head *at = head->next;
174 first->prev = head;
175 head->next = first;
177 last->next = at;
178 at->prev = last;
182 * list_splice - join two lists
183 * @list: the new list to add.
184 * @head: the place to add it in the first list.
186 static inline void list_splice(const struct list_head *list,
187 struct list_head *head)
189 if (!list_empty(list))
190 __list_splice(list, head);
194 * list_entry - get the struct for this entry
195 * @ptr: the &struct list_head pointer.
196 * @type: the type of the struct this is embedded in.
197 * @member: the name of the struct list_head within the struct.
199 #define list_entry(ptr, type, member) ({ \
200 const struct list_head *__mptr = (ptr); \
201 (type *)( (char *)__mptr - offsetof(type,member) );})
204 * list_for_each - iterate over a list
205 * @pos: the &struct list_head to use as a loop cursor.
206 * @head: the head for your list.
208 #define list_for_each(pos, head) \
209 for (pos = (head)->next; pos != (head); pos = pos->next)
212 * list_for_each_prev - iterate over a list backwards
213 * @pos: the &struct list_head to use as a loop cursor.
214 * @head: the head for your list.
216 #define list_for_each_prev(pos, head) \
217 for (pos = (head)->prev; pos != (head); pos = pos->prev)
220 * list_for_each_safe - iterate over a list safe against removal of list entry
221 * @pos: the &struct list_head to use as a loop cursor.
222 * @n: another &struct list_head to use as temporary storage
223 * @head: the head for your list.
225 #define list_for_each_safe(pos, n, head) \
226 for (pos = (head)->next, n = pos->next; pos != (head); \
227 pos = n, n = pos->next)
230 * list_for_each_entry - iterate over list of given type
231 * @pos: the type * to use as a loop cursor.
232 * @head: the head for your list.
233 * @member: the name of the list_struct within the struct.
235 #define list_for_each_entry(pos, head, member) \
236 for (pos = list_entry((head)->next, typeof(*pos), member); \
237 &pos->member != (head); \
238 pos = list_entry(pos->member.next, typeof(*pos), member))
241 * list_for_each_entry_reverse - iterate backwards over list of given type.
242 * @pos: the type * to use as a loop cursor.
243 * @head: the head for your list.
244 * @member: the name of the list_struct within the struct.
246 #define list_for_each_entry_reverse(pos, head, member) \
247 for (pos = list_entry((head)->prev, typeof(*pos), member); \
248 &pos->member != (head); \
249 pos = list_entry(pos->member.prev, typeof(*pos), member))
252 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
253 * @pos: the type * to use as a loop cursor.
254 * @n: another type * to use as temporary storage
255 * @head: the head for your list.
256 * @member: the name of the list_struct within the struct.
258 #define list_for_each_entry_safe(pos, n, head, member) \
259 for (pos = list_entry((head)->next, typeof(*pos), member), \
260 n = list_entry(pos->member.next, typeof(*pos), member); \
261 &pos->member != (head); \
262 pos = n, n = list_entry(n->member.next, typeof(*n), member))
265 * list_for_each_entry_safe_reverse
266 * @pos: the type * to use as a loop cursor.
267 * @n: another type * to use as temporary storage
268 * @head: the head for your list.
269 * @member: the name of the list_struct within the struct.
271 * Iterate backwards over list of given type, safe against removal
272 * of list entry.
274 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
275 for (pos = list_entry((head)->prev, typeof(*pos), member), \
276 n = list_entry(pos->member.prev, typeof(*pos), member); \
277 &pos->member != (head); \
278 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
281 #endif /* EL__UTIL_LISTS_H */