8 * This linked list handling code is based on the Linux kernel's
12 FILE_LICENCE ( GPL2_ONLY
);
18 * Simple doubly linked list implementation.
20 * Some of the internal functions ("__xxx") are useful when
21 * manipulating whole lists rather than single entries, as
22 * sometimes we already know the next/prev entries and we can
23 * generate better code by using them directly rather than
24 * using the generic single-entry routines.
28 struct list_head
*next
;
29 struct list_head
*prev
;
32 #define LIST_HEAD_INIT( name ) { &(name), &(name) }
34 #define LIST_HEAD( name ) \
35 struct list_head name = LIST_HEAD_INIT ( name )
37 #define INIT_LIST_HEAD( ptr ) do { \
38 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
42 * Insert a new entry between two known consecutive entries.
44 * This is only for internal list manipulation where we know
45 * the prev/next entries already!
47 static inline void __list_add ( struct list_head
*new,
48 struct list_head
*prev
,
49 struct list_head
*next
) {
57 * Add a new entry to the head of a list
59 * @v new New entry to be added
60 * @v head List head to add it after
62 * Insert a new entry after the specified head. This is good for
63 * implementing stacks.
65 static inline void list_add ( struct list_head
*new, struct list_head
*head
) {
66 __list_add ( new, head
, head
->next
);
68 #define list_add( new, head ) do { \
69 assert ( (head)->next->prev == (head) ); \
70 assert ( (head)->prev->next == (head) ); \
71 list_add ( (new), (head) ); \
75 * Add a new entry to the tail of a list
77 * @v new New entry to be added
78 * @v head List head to add it before
80 * Insert a new entry before the specified head. This is useful for
81 * implementing queues.
83 static inline void list_add_tail ( struct list_head
*new,
84 struct list_head
*head
) {
85 __list_add ( new, head
->prev
, head
);
87 #define list_add_tail( new, head ) do { \
88 assert ( (head)->next->prev == (head) ); \
89 assert ( (head)->prev->next == (head) ); \
90 list_add_tail ( (new), (head) ); \
94 * Delete a list entry by making the prev/next entries
95 * point to each other.
97 * This is only for internal list manipulation where we know
98 * the prev/next entries already!
100 static inline void __list_del ( struct list_head
* prev
,
101 struct list_head
* next
) {
107 * Delete an entry from a list
109 * @v entry Element to delete from the list
111 * Note that list_empty() on entry does not return true after this;
112 * the entry is in an undefined state.
114 static inline void list_del ( struct list_head
*entry
) {
115 __list_del ( entry
->prev
, entry
->next
);
117 #define list_del( entry ) do { \
118 assert ( (entry)->prev != NULL ); \
119 assert ( (entry)->next != NULL ); \
120 assert ( (entry)->next->prev == (entry) ); \
121 assert ( (entry)->prev->next == (entry) ); \
122 list_del ( (entry) ); \
126 * Test whether a list is empty
128 * @v head List to test.
130 static inline int list_empty ( const struct list_head
*head
) {
131 return head
->next
== head
;
135 * Get the containing struct for this entry
137 * @v ptr The struct list_head pointer
138 * @v type The type of the struct this is embedded in
139 * @v member The name of the list_struct within the struct
141 #define list_entry( ptr, type, member ) \
142 container_of ( ptr, type, member )
145 * Iterate over a list
147 * @v pos The &struct list_head to use as a loop counter
148 * @v head The head for your list
150 #define list_for_each( pos, head ) \
151 for ( pos = (head)->next; pos != (head); pos = pos->next )
154 * Iterate over entries in a list
156 * @v pos The type * to use as a loop counter
157 * @v head The head for your list
158 * @v member The name of the list_struct within the struct
160 #define list_for_each_entry( pos, head, member ) \
161 for ( pos = list_entry ( (head)->next, typeof ( *pos ), member ); \
162 &pos->member != (head); \
163 pos = list_entry ( pos->member.next, typeof ( *pos ), member ) )
166 * Iterate over entries in a list in reverse order
168 * @v pos The type * to use as a loop counter
169 * @v head The head for your list
170 * @v member The name of the list_struct within the struct
172 #define list_for_each_entry_reverse( pos, head, member ) \
173 for ( pos = list_entry ( (head)->prev, typeof ( *pos ), member ); \
174 &pos->member != (head); \
175 pos = list_entry ( pos->member.prev, typeof ( *pos ), member ) )
178 * Iterate over entries in a list, safe against deletion of entries
180 * @v pos The type * to use as a loop counter
181 * @v tmp Another type * to use for temporary storage
182 * @v head The head for your list
183 * @v member The name of the list_struct within the struct
185 #define list_for_each_entry_safe( pos, tmp, head, member ) \
186 for ( pos = list_entry ( (head)->next, typeof ( *pos ), member ), \
187 tmp = list_entry ( pos->member.next, typeof ( *tmp ), member ); \
188 &pos->member != (head); \
190 tmp = list_entry ( tmp->member.next, typeof ( *tmp ), member ) )
192 #endif /* _GPXE_LIST_H */