summaryrefslogtreecommitdiffstats
path: root/src/include/ipxe/list.h
diff options
context:
space:
mode:
authorMichael Brown2011-10-14 15:32:19 +0200
committerMichael Brown2011-10-14 15:33:32 +0200
commitc28053027b3cdacb6f454a9e56a2c3ff0bd0bdc2 (patch)
treee312a5422fb33cb8b5274f4fee50ba221f284617 /src/include/ipxe/list.h
parent[test] Add self-tests for list manipulation functions (diff)
downloadipxe-c28053027b3cdacb6f454a9e56a2c3ff0bd0bdc2.tar.gz
ipxe-c28053027b3cdacb6f454a9e56a2c3ff0bd0bdc2.tar.xz
ipxe-c28053027b3cdacb6f454a9e56a2c3ff0bd0bdc2.zip
[list] Add list functions required by ath9k driver
Originally-implemented-by: Scott K Logan <logans@cottsay.net> Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/include/ipxe/list.h')
-rw-r--r--src/include/ipxe/list.h154
1 files changed, 154 insertions, 0 deletions
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
@@ -146,6 +146,160 @@ static inline int list_empty ( const struct list_head *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
*
* @v list List entry