]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[list] Add list_first_entry()
authorMichael Brown <mcb30@ipxe.org>
Mon, 8 Nov 2010 02:51:18 +0000 (02:51 +0000)
committerMichael Brown <mcb30@ipxe.org>
Mon, 8 Nov 2010 03:15:28 +0000 (03:15 +0000)
There are several points in the iPXE codebase where
list_for_each_entry() is (ab)used to extract only the first entry from
a list.  Add a macro list_first_entry() to make this code easier to
read.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/core/process.c
src/hci/commands/fcmgmt_cmd.c
src/include/ipxe/list.h
src/net/80211/net80211.c
src/net/infiniband.c
src/net/netdevice.c
src/net/tcp.c

index bc070e06038ea4f2da441c7db18d497ce1e43f72..a3297856458fa0e6f76e5dabbba6052c8bd594c7 100644 (file)
@@ -83,7 +83,8 @@ void process_del ( struct process *process ) {
 void step ( void ) {
        struct process *process;
 
-       list_for_each_entry ( process, &run_queue, list ) {
+       if ( ( process = list_first_entry ( &run_queue, struct process,
+                                           list ) ) ) {
                list_del ( &process->list );
                list_add_tail ( &process->list, &run_queue );
                ref_get ( process->refcnt ); /* Inhibit destruction mid-step */
@@ -93,7 +94,6 @@ void step ( void ) {
                DBGC2 ( process, "PROCESS %p (%p) finished executing\n",
                        process, process->step );
                ref_put ( process->refcnt ); /* Allow destruction */
-               break;
        }
 }
 
index 3a4e2846935555625b252a00b8ba2ba4cc2cf203..98647c1349e0ca4ce3b4f321c1e5550e7ad1df54 100644 (file)
@@ -144,12 +144,11 @@ static int fcels_exec ( int argc, char **argv ) {
                }
        } else {
                /* Use first port */
-               if ( list_empty ( &fc_ports ) ) {
+               port = list_first_entry ( &fc_ports, struct fc_port, list );
+               if ( ! port ) {
                        printf ( "No ports\n" );
                        return 1;
                }
-               list_for_each_entry ( port, &fc_ports, list )
-                       break;
        }
        assert ( port != NULL );
 
index fb00bc53161b309e7c950feb183094a09d3e48d3..ab4c119dd775e93dfc003d757a324778ab62de1e 100644 (file)
@@ -157,6 +157,19 @@ static inline int list_empty ( const struct list_head *list ) {
        list_check ( (list) );                          \
        container_of ( list, type, member ); } )
 
+/**
+ * Get the container of the first entry in a list
+ *
+ * @v list             List head
+ * @v type             Containing type
+ * @v member           Name of list field within containing type
+ * @ret first          First list entry, or NULL
+ */
+#define list_first_entry( list, type, member )         \
+       ( list_empty ( (list) ) ?                       \
+         ( type * ) NULL :                             \
+         list_entry ( (list)->next, type, member ) )
+
 /**
  * Iterate over entries in a list
  *
index ffa5c9110ac0f71fb21b8f40999438af84dbbb93..d39958ba7d90eb94390c6ab3ed71062cefff1157 100644 (file)
@@ -668,11 +668,11 @@ struct io_buffer * net80211_mgmt_dequeue ( struct net80211_device *dev,
                        *signal = rxi->signal;
                free ( rxi );
 
-               list_for_each_entry ( iobuf, &dev->mgmt_queue, list ) {
-                       list_del ( &iobuf->list );
-                       return iobuf;
-               }
-               assert ( 0 );
+               assert ( ! list_empty ( &dev->mgmt_queue ) );
+               iobuf = list_first_entry ( &dev->mgmt_queue, struct io_buffer,
+                                          list );
+               list_del ( &iobuf->list );
+               return iobuf;
        }
 
        return NULL;
index bdfc45d6b5e6e56a01da1aca02d04923e9b81b88..a6e4233af21a2785897ab002f349025a20165645 100644 (file)
@@ -982,12 +982,12 @@ struct ib_device * find_ibdev ( union ib_gid *gid ) {
 struct ib_device * last_opened_ibdev ( void ) {
        struct ib_device *ibdev;
 
-       list_for_each_entry ( ibdev, &open_ib_devices, open_list ) {
-               assert ( ibdev->open_count != 0 );
-               return ibdev;
-       }
+       ibdev = list_first_entry ( &open_ib_devices, struct ib_device, list );
+       if ( ! ibdev )
+               return NULL;
 
-       return NULL;
+       assert ( ibdev->open_count != 0 );
+       return ibdev;
 }
 
 /* Drag in IPoIB */
index 894b7e7904d0ff8a43d62c623b7d9143de4a01a7..3a01a58dd76d1494f9e6ae2bc8425df0740df64c 100644 (file)
@@ -328,11 +328,12 @@ void netdev_poll ( struct net_device *netdev ) {
 struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
        struct io_buffer *iobuf;
 
-       list_for_each_entry ( iobuf, &netdev->rx_queue, list ) {
-               list_del ( &iobuf->list );
-               return iobuf;
-       }
-       return NULL;
+       iobuf = list_first_entry ( &netdev->rx_queue, struct io_buffer, list );
+       if ( ! iobuf )
+               return NULL;
+
+       list_del ( &iobuf->list );
+       return iobuf;
 }
 
 /**
@@ -592,12 +593,13 @@ struct net_device * find_netdev_by_location ( unsigned int bus_type,
 struct net_device * last_opened_netdev ( void ) {
        struct net_device *netdev;
 
-       list_for_each_entry ( netdev, &open_net_devices, open_list ) {
-               assert ( netdev_is_open ( netdev ) );
-               return netdev;
-       }
+       netdev = list_first_entry ( &open_net_devices, struct net_device,
+                                   list );
+       if ( ! netdev )
+               return NULL;
 
-       return NULL;
+       assert ( netdev_is_open ( netdev ) );
+       return netdev;
 }
 
 /**
index 1c824bdff96f8f0f6095e1f34b559fe701035fcf..420640346f6f9c23ba0659fc98bbc8815c09bb54 100644 (file)
@@ -1014,9 +1014,8 @@ static void tcp_process_rx_queue ( struct tcp_connection *tcp ) {
         * queue, since tcp_discard() may remove packets from the RX
         * queue while we are processing.
         */
-       while ( ! list_empty ( &tcp->rx_queue ) ) {
-               list_for_each_entry ( iobuf, &tcp->rx_queue, list )
-                       break;
+       while ( ( iobuf = list_first_entry ( &tcp->rx_queue, struct io_buffer,
+                                            list ) ) ) {
 
                /* Stop processing when we hit the first gap */
                tcpqhdr = iobuf->data;