Переглянути джерело

[list] Add list_first_entry()

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>
tags/v1.20.1
Michael Brown 14 роки тому
джерело
коміт
ea631f6fb8

+ 2
- 2
src/core/process.c Переглянути файл

@@ -83,7 +83,8 @@ void process_del ( struct process *process ) {
83 83
 void step ( void ) {
84 84
 	struct process *process;
85 85
 
86
-	list_for_each_entry ( process, &run_queue, list ) {
86
+	if ( ( process = list_first_entry ( &run_queue, struct process,
87
+					    list ) ) ) {
87 88
 		list_del ( &process->list );
88 89
 		list_add_tail ( &process->list, &run_queue );
89 90
 		ref_get ( process->refcnt ); /* Inhibit destruction mid-step */
@@ -93,7 +94,6 @@ void step ( void ) {
93 94
 		DBGC2 ( process, "PROCESS %p (%p) finished executing\n",
94 95
 			process, process->step );
95 96
 		ref_put ( process->refcnt ); /* Allow destruction */
96
-		break;
97 97
 	}
98 98
 }
99 99
 

+ 2
- 3
src/hci/commands/fcmgmt_cmd.c Переглянути файл

@@ -144,12 +144,11 @@ static int fcels_exec ( int argc, char **argv ) {
144 144
 		}
145 145
 	} else {
146 146
 		/* Use first port */
147
-		if ( list_empty ( &fc_ports ) ) {
147
+		port = list_first_entry ( &fc_ports, struct fc_port, list );
148
+		if ( ! port ) {
148 149
 			printf ( "No ports\n" );
149 150
 			return 1;
150 151
 		}
151
-		list_for_each_entry ( port, &fc_ports, list )
152
-			break;
153 152
 	}
154 153
 	assert ( port != NULL );
155 154
 

+ 13
- 0
src/include/ipxe/list.h Переглянути файл

@@ -157,6 +157,19 @@ static inline int list_empty ( const struct list_head *list ) {
157 157
 	list_check ( (list) );				\
158 158
 	container_of ( list, type, member ); } )
159 159
 
160
+/**
161
+ * Get the container of the first entry in a list
162
+ *
163
+ * @v list		List head
164
+ * @v type		Containing type
165
+ * @v member		Name of list field within containing type
166
+ * @ret first		First list entry, or NULL
167
+ */
168
+#define list_first_entry( list, type, member )		\
169
+	( list_empty ( (list) ) ?			\
170
+	  ( type * ) NULL :				\
171
+	  list_entry ( (list)->next, type, member ) )
172
+
160 173
 /**
161 174
  * Iterate over entries in a list
162 175
  *

+ 5
- 5
src/net/80211/net80211.c Переглянути файл

@@ -668,11 +668,11 @@ struct io_buffer * net80211_mgmt_dequeue ( struct net80211_device *dev,
668 668
 			*signal = rxi->signal;
669 669
 		free ( rxi );
670 670
 
671
-		list_for_each_entry ( iobuf, &dev->mgmt_queue, list ) {
672
-			list_del ( &iobuf->list );
673
-			return iobuf;
674
-		}
675
-		assert ( 0 );
671
+		assert ( ! list_empty ( &dev->mgmt_queue ) );
672
+		iobuf = list_first_entry ( &dev->mgmt_queue, struct io_buffer,
673
+					   list );
674
+		list_del ( &iobuf->list );
675
+		return iobuf;
676 676
 	}
677 677
 
678 678
 	return NULL;

+ 5
- 5
src/net/infiniband.c Переглянути файл

@@ -982,12 +982,12 @@ struct ib_device * find_ibdev ( union ib_gid *gid ) {
982 982
 struct ib_device * last_opened_ibdev ( void ) {
983 983
 	struct ib_device *ibdev;
984 984
 
985
-	list_for_each_entry ( ibdev, &open_ib_devices, open_list ) {
986
-		assert ( ibdev->open_count != 0 );
987
-		return ibdev;
988
-	}
985
+	ibdev = list_first_entry ( &open_ib_devices, struct ib_device, list );
986
+	if ( ! ibdev )
987
+		return NULL;
989 988
 
990
-	return NULL;
989
+	assert ( ibdev->open_count != 0 );
990
+	return ibdev;
991 991
 }
992 992
 
993 993
 /* Drag in IPoIB */

+ 12
- 10
src/net/netdevice.c Переглянути файл

@@ -328,11 +328,12 @@ void netdev_poll ( struct net_device *netdev ) {
328 328
 struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
329 329
 	struct io_buffer *iobuf;
330 330
 
331
-	list_for_each_entry ( iobuf, &netdev->rx_queue, list ) {
332
-		list_del ( &iobuf->list );
333
-		return iobuf;
334
-	}
335
-	return NULL;
331
+	iobuf = list_first_entry ( &netdev->rx_queue, struct io_buffer, list );
332
+	if ( ! iobuf )
333
+		return NULL;
334
+
335
+	list_del ( &iobuf->list );
336
+	return iobuf;
336 337
 }
337 338
 
338 339
 /**
@@ -592,12 +593,13 @@ struct net_device * find_netdev_by_location ( unsigned int bus_type,
592 593
 struct net_device * last_opened_netdev ( void ) {
593 594
 	struct net_device *netdev;
594 595
 
595
-	list_for_each_entry ( netdev, &open_net_devices, open_list ) {
596
-		assert ( netdev_is_open ( netdev ) );
597
-		return netdev;
598
-	}
596
+	netdev = list_first_entry ( &open_net_devices, struct net_device,
597
+				    list );
598
+	if ( ! netdev )
599
+		return NULL;
599 600
 
600
-	return NULL;
601
+	assert ( netdev_is_open ( netdev ) );
602
+	return netdev;
601 603
 }
602 604
 
603 605
 /**

+ 2
- 3
src/net/tcp.c Переглянути файл

@@ -1014,9 +1014,8 @@ static void tcp_process_rx_queue ( struct tcp_connection *tcp ) {
1014 1014
 	 * queue, since tcp_discard() may remove packets from the RX
1015 1015
 	 * queue while we are processing.
1016 1016
 	 */
1017
-	while ( ! list_empty ( &tcp->rx_queue ) ) {
1018
-		list_for_each_entry ( iobuf, &tcp->rx_queue, list )
1019
-			break;
1017
+	while ( ( iobuf = list_first_entry ( &tcp->rx_queue, struct io_buffer,
1018
+					     list ) ) ) {
1020 1019
 
1021 1020
 		/* Stop processing when we hit the first gap */
1022 1021
 		tcpqhdr = iobuf->data;

Завантаження…
Відмінити
Зберегти