#ifdef out unused function to fix warning for non rtc and non bitmap targets
[kugel-rb.git] / firmware / export / linkedlist.h
blobbea41eeca5facee33c3cc9339b7ee2cd6c09d0bc
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) by Linux Kernel Developers
12 * Original source can be found in linux kernel: <kernel>/include/list.h
14 * All files in this archive are subject to the GNU General Public License.
15 * See the file COPYING in the source tree root for full license agreement.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #ifndef _LINKED_LIST_H_
23 #define _LINKED_LIST_H_
25 #include <stddef.h> /* used for offsetof */
27 static inline void prefetch(const void *x) { (void)x; }
30 * Simple doubly linked list implementation.
32 * Some of the internal functions ("__xxx") are useful when
33 * manipulating whole lists rather than single entries, as
34 * sometimes we already know the next/prev entries and we can
35 * generate better code by using them directly rather than
36 * using the generic single-entry routines.
39 /* TODO move this macro? */
40 /* more about this macro: http://www.kroah.com/log/linux/container_of.html */
41 #define container_of(ptr, type, member) ({ \
42 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
43 (type *)( (char *)__mptr - offsetof(type,member) );})
46 * These are non-NULL pointers that will result in page faults
47 * under normal circumstances, used to verify that nobody uses
48 * non-initialized list entries.
50 #define LIST_POISON1 ((void *) 0x00100100)
51 #define LIST_POISON2 ((void *) 0x00200200)
53 struct list_head {
54 struct list_head *next, *prev;
57 #define LIST_HEAD_INIT(name) { &(name), &(name) }
59 #define LIST_HEAD(name) \
60 struct list_head name = LIST_HEAD_INIT(name)
62 static inline void INIT_LIST_HEAD(struct list_head *list)
64 list->next = list;
65 list->prev = list;
69 * Insert a new entry between two known consecutive entries.
71 * This is only for internal list manipulation where we know
72 * the prev/next entries already!
74 static inline void __list_add(struct list_head *new,
75 struct list_head *prev,
76 struct list_head *next)
78 next->prev = new;
79 new->next = next;
80 new->prev = prev;
81 prev->next = new;
85 /**
86 * list_add - add a new entry
87 * @new: new entry to be added
88 * @head: list head to add it after
90 * Insert a new entry after the specified head.
91 * This is good for implementing stacks.
93 static inline void list_add(struct list_head *new, struct list_head *head)
95 __list_add(new, head, head->next);
99 /**
100 * list_add_tail - add a new entry
101 * @new: new entry to be added
102 * @head: list head to add it before
104 * Insert a new entry before the specified head.
105 * This is useful for implementing queues.
107 static inline void list_add_tail(struct list_head *new, struct list_head *head)
109 __list_add(new, head->prev, head);
113 * Delete a list entry by making the prev/next entries
114 * point to each other.
116 * This is only for internal list manipulation where we know
117 * the prev/next entries already!
119 static inline void __list_del(struct list_head * prev, struct list_head * next)
121 next->prev = prev;
122 prev->next = next;
126 * list_del - deletes entry from list.
127 * @entry: the element to delete from the list.
128 * Note: list_empty() on entry does not return true after this, the entry is
129 * in an undefined state.
131 static inline void list_del(struct list_head *entry)
133 __list_del(entry->prev, entry->next);
134 entry->next = LIST_POISON1;
135 entry->prev = LIST_POISON2;
139 * list_replace - replace old entry by new one
140 * @old : the element to be replaced
141 * @new : the new element to insert
143 * If @old was empty, it will be overwritten.
145 static inline void list_replace(struct list_head *old,
146 struct list_head *new)
148 new->next = old->next;
149 new->next->prev = new;
150 new->prev = old->prev;
151 new->prev->next = new;
154 static inline void list_replace_init(struct list_head *old,
155 struct list_head *new)
157 list_replace(old, new);
158 INIT_LIST_HEAD(old);
162 * list_del_init - deletes entry from list and reinitialize it.
163 * @entry: the element to delete from the list.
165 static inline void list_del_init(struct list_head *entry)
167 __list_del(entry->prev, entry->next);
168 INIT_LIST_HEAD(entry);
172 * list_move - delete from one list and add as another's head
173 * @list: the entry to move
174 * @head: the head that will precede our entry
176 static inline void list_move(struct list_head *list, struct list_head *head)
178 __list_del(list->prev, list->next);
179 list_add(list, head);
183 * list_move_tail - delete from one list and add as another's tail
184 * @list: the entry to move
185 * @head: the head that will follow our entry
187 static inline void list_move_tail(struct list_head *list,
188 struct list_head *head)
190 __list_del(list->prev, list->next);
191 list_add_tail(list, head);
195 * list_is_last - tests whether @list is the last entry in list @head
196 * @list: the entry to test
197 * @head: the head of the list
199 static inline int list_is_last(const struct list_head *list,
200 const struct list_head *head)
202 return list->next == head;
206 * list_empty - tests whether a list is empty
207 * @head: the list to test.
209 static inline int list_empty(const struct list_head *head)
211 return head->next == head;
214 static inline void __list_splice(struct list_head *list,
215 struct list_head *head)
217 struct list_head *first = list->next;
218 struct list_head *last = list->prev;
219 struct list_head *at = head->next;
221 first->prev = head;
222 head->next = first;
224 last->next = at;
225 at->prev = last;
229 * list_splice - join two lists
230 * @list: the new list to add.
231 * @head: the place to add it in the first list.
233 static inline void list_splice(struct list_head *list, struct list_head *head)
235 if (!list_empty(list)) {
236 __list_splice(list, head);
241 * list_splice_init - join two lists and reinitialise the emptied list.
242 * @list: the new list to add.
243 * @head: the place to add it in the first list.
245 * The list at @list is reinitialised
247 static inline void list_splice_init(struct list_head *list,
248 struct list_head *head)
250 if (!list_empty(list)) {
251 __list_splice(list, head);
252 INIT_LIST_HEAD(list);
257 * list_entry - get the struct for this entry
258 * @ptr: the &struct list_head pointer.
259 * @type: the type of the struct this is embedded in.
260 * @member: the name of the list_struct within the struct.
262 #define list_entry(ptr, type, member) \
263 container_of(ptr, type, member)
266 * list_for_each - iterate over a list
267 * @pos: the &struct list_head to use as a loop cursor.
268 * @head: the head for your list.
270 #define list_for_each(pos, head) \
271 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
272 pos = pos->next)
275 * __list_for_each - iterate over a list
276 * @pos: the &struct list_head to use as a loop cursor.
277 * @head: the head for your list.
279 * This variant differs from list_for_each() in that it's the
280 * simplest possible list iteration code, no prefetching is done.
281 * Use this for code that knows the list to be very short (empty
282 * or 1 entry) most of the time.
284 #define __list_for_each(pos, head) \
285 for (pos = (head)->next; pos != (head); pos = pos->next)
288 * list_for_each_prev - iterate over a list backwards
289 * @pos: the &struct list_head to use as a loop cursor.
290 * @head: the head for your list.
292 #define list_for_each_prev(pos, head) \
293 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
294 pos = pos->prev)
297 * list_for_each_entry - iterate over list of given type
298 * @pos: the type * to use as a loop cursor.
299 * @head: the head for your list.
300 * @member: the name of the list_struct within the struct.
302 #define list_for_each_entry(pos, head, member) \
303 for (pos = list_entry((head)->next, typeof(*pos), member); \
304 prefetch(pos->member.next), &pos->member != (head); \
305 pos = list_entry(pos->member.next, typeof(*pos), member))
308 * list_for_each_entry_reverse - iterate backwards over list of given type.
309 * @pos: the type * to use as a loop cursor.
310 * @head: the head for your list.
311 * @member: the name of the list_struct within the struct.
313 #define list_for_each_entry_reverse(pos, head, member) \
314 for (pos = list_entry((head)->prev, typeof(*pos), member); \
315 prefetch(pos->member.prev), &pos->member != (head); \
316 pos = list_entry(pos->member.prev, typeof(*pos), member))
319 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
320 * @pos: the type * to use as a start point
321 * @head: the head of the list
322 * @member: the name of the list_struct within the struct.
324 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
326 #define list_prepare_entry(pos, head, member) \
327 ((pos) ? : list_entry(head, typeof(*pos), member))
330 * list_for_each_entry_continue - continue iteration over list of given type
331 * @pos: the type * to use as a loop cursor.
332 * @head: the head for your list.
333 * @member: the name of the list_struct within the struct.
335 * Continue to iterate over list of given type, continuing after
336 * the current position.
338 #define list_for_each_entry_continue(pos, head, member) \
339 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
340 prefetch(pos->member.next), &pos->member != (head); \
341 pos = list_entry(pos->member.next, typeof(*pos), member))
344 * list_for_each_entry_from - iterate over list of given type from the current point
345 * @pos: the type * to use as a loop cursor.
346 * @head: the head for your list.
347 * @member: the name of the list_struct within the struct.
349 * Iterate over list of given type, continuing from current position.
351 #define list_for_each_entry_from(pos, head, member) \
352 for (; prefetch(pos->member.next), &pos->member != (head); \
353 pos = list_entry(pos->member.next, typeof(*pos), member))
355 #endif /*_LINKED_LIST_H_*/