From c28053027b3cdacb6f454a9e56a2c3ff0bd0bdc2 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Fri, 14 Oct 2011 14:32:19 +0100 Subject: [list] Add list functions required by ath9k driver Originally-implemented-by: Scott K Logan Signed-off-by: Michael Brown --- src/include/ipxe/list.h | 154 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) (limited to 'src/include') diff --git a/src/include/ipxe/list.h b/src/include/ipxe/list.h index 8dc74139..231383d1 100644 --- a/src/include/ipxe/list.h +++ b/src/include/ipxe/list.h @@ -145,6 +145,160 @@ static inline int list_empty ( const struct list_head *list ) { list_check ( (list) ); \ list_empty ( (list) ); } ) +/** + * Test whether a list has just one entry + * + * @v list List to test + */ +static inline int list_is_singular ( const struct list_head *list ) { + return ( ( ! list_empty ( list ) ) && ( list->next == list->prev ) ); +} +#define list_is_singular( list ) ( { \ + list_check ( (list) ); \ + list_is_singular ( (list) ); } ) + +/** + * Test whether an entry is the last entry in list + * + * @v list List entry to test + * @v head List head + */ +static inline int list_is_last ( const struct list_head *list, + const struct list_head *head ) { + return ( list->next == head ); +} +#define list_is_last( list, head ) ( { \ + list_check ( (list) ); \ + list_check ( (head) ); \ + list_is_last ( (list), (head) ); } ) + +/** + * Cut a list into two + * + * @v new A new list to contain all removed entries + * @v list An existing list + * @v entry An entry within the existing list + * + * All entries from @c list up to and including @c entry are moved to + * @c new, which should be an empty list. @c entry may be equal to @c + * list, in which case no entries are moved. + */ +static inline void list_cut_position ( struct list_head *new, + struct list_head *list, + struct list_head *entry ) { + struct list_head *first = entry->next; + + if ( list != entry ) { + new->next = list->next; + new->next->prev = new; + new->prev = entry; + new->prev->next = new; + list->next = first; + list->next->prev = list; + } +} +#define list_cut_position( new, list, entry ) do { \ + list_check ( (new) ); \ + assert ( list_empty ( (new) ) ); \ + list_check ( (list) ); \ + list_check ( (entry) ); \ + list_cut_position ( (new), (list), (entry) ); \ + } while ( 0 ) + +/** + * Move all entries from one list into another list + * + * @v list List of entries to add + * @v entry Entry after which to add the new entries + * + * All entries from @c list are inserted after @c entry. Note that @c + * list is left in an undefined state; use @c list_splice_init() if + * you want @c list to become an empty list. + */ +static inline void list_splice ( const struct list_head *list, + struct list_head *entry ) { + struct list_head *first = list->next; + struct list_head *last = list->prev; + + if ( ! list_empty ( list ) ) { + last->next = entry->next; + last->next->prev = last; + first->prev = entry; + first->prev->next = first; + } +} +#define list_splice( list, entry ) do { \ + list_check ( (list) ); \ + list_check ( (entry) ); \ + list_splice ( (list), (entry) ); \ + } while ( 0 ) + +/** + * Move all entries from one list into another list + * + * @v list List of entries to add + * @v entry Entry before which to add the new entries + * + * All entries from @c list are inserted before @c entry. Note that @c + * list is left in an undefined state; use @c list_splice_tail_init() if + * you want @c list to become an empty list. + */ +static inline void list_splice_tail ( const struct list_head *list, + struct list_head *entry ) { + struct list_head *first = list->next; + struct list_head *last = list->prev; + + if ( ! list_empty ( list ) ) { + first->prev = entry->prev; + first->prev->next = first; + last->next = entry; + last->next->prev = last; + } +} +#define list_splice_tail( list, entry ) do { \ + list_check ( (list) ); \ + list_check ( (entry) ); \ + list_splice_tail ( (list), (entry) ); \ + } while ( 0 ) + +/** + * Move all entries from one list into another list and reinitialise empty list + * + * @v list List of entries to add + * @v entry Entry after which to add the new entries + * + * All entries from @c list are inserted after @c entry. + */ +static inline void list_splice_init ( struct list_head *list, + struct list_head *entry ) { + list_splice ( list, entry ); + INIT_LIST_HEAD ( list ); +} +#define list_splice_init( list, entry ) do { \ + list_check ( (list) ); \ + list_check ( (entry) ); \ + list_splice_init ( (list), (entry) ); \ + } while ( 0 ) + +/** + * Move all entries from one list into another list and reinitialise empty list + * + * @v list List of entries to add + * @v entry Entry before which to add the new entries + * + * All entries from @c list are inserted before @c entry. + */ +static inline void list_splice_tail_init ( struct list_head *list, + struct list_head *entry ) { + list_splice_tail ( list, entry ); + INIT_LIST_HEAD ( list ); +} +#define list_splice_tail_init( list, entry ) do { \ + list_check ( (list) ); \ + list_check ( (entry) ); \ + list_splice_tail_init ( (list), (entry) ); \ + } while ( 0 ) + /** * Get the container of a list entry * -- cgit v1.2.3-55-g7522