8 * This linked list handling code is based on the Linux kernel's
12 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
17 /** A doubly-linked list entry (or list head) */
19 /** Next list entry */
20 struct list_head *next;
21 /** Previous list entry */
22 struct list_head *prev;
26 * Initialise a static list head
30 #define LIST_HEAD_INIT( list ) { &(list), &(list) }
33 * Declare a static list head
37 #define LIST_HEAD( list ) \
38 struct list_head list = LIST_HEAD_INIT ( list )
41 * Initialise a list head
45 #define INIT_LIST_HEAD( list ) do { \
46 (list)->next = (list); \
47 (list)->prev = (list); \
51 * Check a list entry or list head is valid
53 * @v list List entry or head
55 #define list_check( list ) ( { \
56 assert ( (list) != NULL ); \
57 assert ( (list)->prev != NULL ); \
58 assert ( (list)->next != NULL ); \
59 assert ( (list)->next->prev == (list) ); \
60 assert ( (list)->prev->next == (list) ); \
64 * Add a new entry to the head of a list
66 * @v new New entry to be added
67 * @v head List head, or entry after which to add the new entry
69 #define list_add( new, head ) do { \
70 list_check ( (head) ); \
71 extern_list_add ( (new), (head) ); \
72 list_check ( (head) ); \
73 list_check ( (new) ); \
75 static inline void inline_list_add ( struct list_head *new,
76 struct list_head *head ) {
77 struct list_head *prev = head;
78 struct list_head *next = head->next;
84 extern void extern_list_add ( struct list_head *new,
85 struct list_head *head );
88 * Add a new entry to the tail of a list
90 * @v new New entry to be added
91 * @v head List head, or entry before which to add the new entry
93 #define list_add_tail( new, head ) do { \
94 list_check ( (head) ); \
95 extern_list_add_tail ( (new), (head) ); \
96 list_check ( (head) ); \
97 list_check ( (new) ); \
99 static inline void inline_list_add_tail ( struct list_head *new,
100 struct list_head *head ) {
101 struct list_head *prev = head->prev;
102 struct list_head *next = head;
108 extern void extern_list_add_tail ( struct list_head *new,
109 struct list_head *head );
112 * Delete an entry from a list
116 * Note that list_empty() on entry does not return true after this;
117 * the entry is in an undefined state.
119 #define list_del( list ) do { \
120 list_check ( (list) ); \
121 inline_list_del ( (list) ); \
123 static inline void inline_list_del ( struct list_head *list ) {
124 struct list_head *next = (list)->next;
125 struct list_head *prev = (list)->prev;
129 extern void extern_list_del ( struct list_head *list );
132 * Test whether a list is empty
136 #define list_empty( list ) ( { \
137 list_check ( (list) ); \
138 inline_list_empty ( (list) ); } )
139 static inline int inline_list_empty ( const struct list_head *list ) {
140 return ( list->next == list );
142 extern int extern_list_empty ( const struct list_head *list );
145 * Test whether a list has just one entry
147 * @v list List to test
149 #define list_is_singular( list ) ( { \
150 list_check ( (list) ); \
151 inline_list_is_singular ( (list) ); } )
152 static inline int inline_list_is_singular ( const struct list_head *list ) {
153 return ( ( ! list_empty ( list ) ) && ( list->next == list->prev ) );
155 extern int extern_list_is_singular ( const struct list_head *list );
158 * Test whether an entry is the last entry in list
160 * @v list List entry to test
163 #define list_is_last( list, head ) ( { \
164 list_check ( (list) ); \
165 list_check ( (head) ); \
166 inline_list_is_last ( (list), (head) ); } )
167 static inline int inline_list_is_last ( const struct list_head *list,
168 const struct list_head *head ) {
169 return ( list->next == head );
171 extern int extern_list_is_last ( const struct list_head *list,
172 const struct list_head *head );
175 * Cut a list into two
177 * @v new A new list to contain all removed entries
178 * @v list An existing list
179 * @v entry An entry within the existing list
181 * All entries from @c list up to and including @c entry are moved to
182 * @c new, which should be an empty list. @c entry may be equal to @c
183 * list, in which case no entries are moved.
185 #define list_cut_position( new, list, entry ) do { \
186 list_check ( (new) ); \
187 assert ( list_empty ( (new) ) ); \
188 list_check ( (list) ); \
189 list_check ( (entry) ); \
190 extern_list_cut_position ( (new), (list), (entry) ); \
192 static inline void inline_list_cut_position ( struct list_head *new,
193 struct list_head *list,
194 struct list_head *entry ) {
195 struct list_head *first = entry->next;
197 if ( list != entry ) {
198 new->next = list->next;
199 new->next->prev = new;
201 new->prev->next = new;
203 list->next->prev = list;
206 extern void extern_list_cut_position ( struct list_head *new,
207 struct list_head *list,
208 struct list_head *entry );
211 * Move all entries from one list into another list
213 * @v list List of entries to add
214 * @v entry Entry after which to add the new entries
216 * All entries from @c list are inserted after @c entry. Note that @c
217 * list is left in an undefined state; use @c list_splice_init() if
218 * you want @c list to become an empty list.
220 #define list_splice( list, entry ) do { \
221 list_check ( (list) ); \
222 list_check ( (entry) ); \
223 extern_list_splice ( (list), (entry) ); \
225 static inline void inline_list_splice ( const struct list_head *list,
226 struct list_head *entry ) {
227 struct list_head *first = list->next;
228 struct list_head *last = list->prev;
230 if ( ! list_empty ( list ) ) {
231 last->next = entry->next;
232 last->next->prev = last;
234 first->prev->next = first;
237 extern void extern_list_splice ( const struct list_head *list,
238 struct list_head *entry );
241 * Move all entries from one list into another list
243 * @v list List of entries to add
244 * @v entry Entry before which to add the new entries
246 * All entries from @c list are inserted before @c entry. Note that @c
247 * list is left in an undefined state; use @c list_splice_tail_init() if
248 * you want @c list to become an empty list.
250 #define list_splice_tail( list, entry ) do { \
251 list_check ( (list) ); \
252 list_check ( (entry) ); \
253 extern_list_splice_tail ( (list), (entry) ); \
255 static inline void inline_list_splice_tail ( const struct list_head *list,
256 struct list_head *entry ) {
257 struct list_head *first = list->next;
258 struct list_head *last = list->prev;
260 if ( ! list_empty ( list ) ) {
261 first->prev = entry->prev;
262 first->prev->next = first;
264 last->next->prev = last;
267 extern void extern_list_splice_tail ( const struct list_head *list,
268 struct list_head *entry );
271 * Move all entries from one list into another list and reinitialise empty list
273 * @v list List of entries to add
274 * @v entry Entry after which to add the new entries
276 * All entries from @c list are inserted after @c entry.
278 #define list_splice_init( list, entry ) do { \
279 list_check ( (list) ); \
280 list_check ( (entry) ); \
281 extern_list_splice_init ( (list), (entry) ); \
283 static inline void inline_list_splice_init ( struct list_head *list,
284 struct list_head *entry ) {
285 list_splice ( list, entry );
286 INIT_LIST_HEAD ( list );
288 extern void extern_list_splice_init ( struct list_head *list,
289 struct list_head *entry );
292 * Move all entries from one list into another list and reinitialise empty list
294 * @v list List of entries to add
295 * @v entry Entry before which to add the new entries
297 * All entries from @c list are inserted before @c entry.
299 #define list_splice_tail_init( list, entry ) do { \
300 list_check ( (list) ); \
301 list_check ( (entry) ); \
302 extern_list_splice_tail_init ( (list), (entry) ); \
305 static inline void inline_list_splice_tail_init ( struct list_head *list,
306 struct list_head *entry ) {
307 list_splice_tail ( list, entry );
308 INIT_LIST_HEAD ( list );
310 extern void extern_list_splice_tail_init ( struct list_head *list,
311 struct list_head *entry );
314 * Get the container of a list entry
317 * @v type Containing type
318 * @v member Name of list field within containing type
319 * @ret container Containing object
321 #define list_entry( list, type, member ) ( { \
322 list_check ( (list) ); \
323 container_of ( list, type, member ); } )
326 * Get the container of the first entry in a list
329 * @v type Containing type
330 * @v member Name of list field within containing type
331 * @ret first First list entry, or NULL
333 #define list_first_entry( list, type, member ) \
334 ( list_empty ( (list) ) ? \
336 list_entry ( (list)->next, type, member ) )
339 * Get the container of the last entry in a list
342 * @v type Containing type
343 * @v member Name of list field within containing type
344 * @ret first First list entry, or NULL
346 #define list_last_entry( list, type, member ) \
347 ( list_empty ( (list) ) ? \
349 list_entry ( (list)->prev, type, member ) )
352 * Iterate over a list
357 #define list_for_each( pos, head ) \
358 for ( list_check ( (head) ), \
359 pos = (head)->next; \
364 * Iterate over entries in a list
368 * @v member Name of list field within iterator's type
370 #define list_for_each_entry( pos, head, member ) \
371 for ( list_check ( (head) ), \
372 pos = list_entry ( (head)->next, typeof ( *pos ), member ); \
373 &pos->member != (head); \
374 pos = list_entry ( pos->member.next, typeof ( *pos ), member ) )
377 * Iterate over entries in a list in reverse order
381 * @v member Name of list field within iterator's type
383 #define list_for_each_entry_reverse( pos, head, member ) \
384 for ( list_check ( (head) ), \
385 pos = list_entry ( (head)->prev, typeof ( *pos ), member ); \
386 &pos->member != (head); \
387 pos = list_entry ( pos->member.prev, typeof ( *pos ), member ) )
390 * Iterate over entries in a list, safe against deletion of the current entry
393 * @v tmp Temporary value (of same type as iterator)
395 * @v member Name of list field within iterator's type
397 #define list_for_each_entry_safe( pos, tmp, head, member ) \
398 for ( list_check ( (head) ), \
399 pos = list_entry ( (head)->next, typeof ( *pos ), member ), \
400 tmp = list_entry ( pos->member.next, typeof ( *tmp ), member ); \
401 &pos->member != (head); \
403 tmp = list_entry ( tmp->member.next, typeof ( *tmp ), member ) )
406 * Iterate over entries in a list, starting after current position
410 * @v member Name of list field within iterator's type
412 #define list_for_each_entry_continue( pos, head, member ) \
413 for ( list_check ( (head) ), \
414 pos = list_entry ( pos->member.next, typeof ( *pos ), member ); \
415 &pos->member != (head); \
416 pos = list_entry ( pos->member.next, typeof ( *pos ), member ) )
419 * Iterate over entries in a list in reverse, starting after current position
423 * @v member Name of list field within iterator's type
425 #define list_for_each_entry_continue_reverse( pos, head, member ) \
426 for ( list_check ( (head) ), \
427 pos = list_entry ( pos->member.prev, typeof ( *pos ), member ); \
428 &pos->member != (head); \
429 pos = list_entry ( pos->member.prev, typeof ( *pos ), member ) )
432 * Test if list contains a specified entry
436 * @ret present List contains specified entry
438 #define list_contains( entry, head ) ( { \
439 list_check ( (head) ); \
440 list_check ( (entry) ); \
441 extern_list_contains ( (entry), (head) ); } )
442 static inline int inline_list_contains ( struct list_head *entry,
443 struct list_head *head ) {
444 struct list_head *tmp;
446 list_for_each ( tmp, head ) {
452 extern int extern_list_contains ( struct list_head *entry,
453 struct list_head *head );
456 * Test if list contains a specified entry
460 * @ret present List contains specified entry
462 #define list_contains_entry( entry, head, member ) \
463 list_contains ( &(entry)->member, (head) )
466 * Check list contains a specified entry
470 * @v member Name of list field within iterator's type
472 #define list_check_contains_entry( entry, head, member ) do { \
473 assert ( list_contains_entry ( (entry), (head), member ) ); \
476 #endif /* _IPXE_LIST_H */