Skip to contentSkip to conversation

d80211: clean up some list and loop code

PATCH Johannes Berg 4 participants 8 messages Nov 16, 2006 – Nov 17, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Remove things like "for (;;)" or "for (; condition ;)". Ever heard of while loops?

Also clean up a bunch of things that list.h offers. No need to code it here.

---

This is just insane. Can't we just rewrite the whole thing? :)

I'm not really sure we need list_for_each_entry_safe in the first hunk but it doesn't hurt either.

net/d80211/ieee80211_sta.c +2 -8
@@ -1314,16 +1314,10 @@ void ieee80211_rx_bss_list_init(struct n
 void ieee80211_rx_bss_list_deinit(struct net_device *dev)
 {
 	struct ieee80211_local *local = dev->ieee80211_ptr;
-	struct ieee80211_sta_bss *bss;
-	struct list_head *ptr;
+	struct ieee80211_sta_bss *bss, *tmp;
 
-	for (;;) {
-		ptr = local->sta_bss_list.next;
-		if (!ptr || ptr == &local->sta_bss_list)
-			break;
-		bss = list_entry(ptr, struct ieee80211_sta_bss, list);
+	list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
 		ieee80211_rx_bss_put(dev, bss);
-	}
 }
 
 
net/d80211/sta_info.c +5 -11
@@ -299,7 +299,7 @@ static void sta_info_cleanup_expire_buff
 	if (skb_queue_empty(&sta->ps_tx_buf))
 		return;
 
-	for (;;) {
+	while (1) {
 		spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
 		skb = skb_peek(&sta->ps_tx_buf);
 		if (sta_info_buffer_expired(local, sta, skb)) {
@@ -324,16 +324,13 @@ static void sta_info_cleanup_expire_buff
 static void sta_info_cleanup(unsigned long data)
 {
 	struct ieee80211_local *local = (struct ieee80211_local *) data;
-	struct list_head *ptr;
+	struct sta_info *sta, *tmp;
 
 	spin_lock_bh(&local->sta_lock);
-	ptr = local->sta_list.next;
-	while (ptr && ptr != &local->sta_list) {
-		struct sta_info *sta = (struct sta_info *) ptr;
+	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
 		__sta_info_get(sta);
 		sta_info_cleanup_expire_buffered(local, sta);
 		sta_info_put(sta);
-		ptr = ptr->next;
 	}
 	spin_unlock_bh(&local->sta_lock);
 
@@ -411,14 +408,11 @@ int sta_info_start(struct ieee80211_loca
 
 void sta_info_stop(struct ieee80211_local *local)
 {
-	struct list_head *ptr;
+	struct sta_info *sta, *tmp;
 
 	del_timer(&local->sta_cleanup);
 
-	ptr = local->sta_list.next;
-	while (ptr && ptr != &local->sta_list) {
-		struct sta_info *sta = (struct sta_info *) ptr;
-		ptr = ptr->next;
+	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
 		/* sta_info_free must be called with 0 as the last
 		 * parameter to ensure all sysfs sta entries are
 		 * unregistered. We don't need locking at this
net/d80211/wme.c +1 -2
@@ -211,8 +211,7 @@ static inline int classify80211(struct s
 	skb->priority = classify_1d(skb, qd);
 
 	/* incase we are a client verify acm is not set for this ac */
-	for (; unlikely(local->wmm_acm & BIT(skb->priority)); )
-	{
+	while (unlikely(local->wmm_acm & BIT(skb->priority))) {
 		if (wme_downgrade_ac(skb)) {
 			/* No AC with lower priority has acm=0,
 			* drop packet. */
#1 lore raw
Quoted 6 lines · Johannes Berg

Remove things like "for (;;)" or "for (; condition ;)". Ever heard of while loops?

net/d80211/sta_info.c static void sta_info_cleanup_expire_buff · line 305

Quoted 8 lines
net/d80211/sta_info.c +1 -1
@@ -299,7 +299,7 @@ static void sta_info_cleanup_expire_buff
 	if (skb_queue_empty(&sta->ps_tx_buf))
 		return;
 
-	for (;;) {
+	while (1) {
 		spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
 		skb = skb_peek(&sta->ps_tx_buf);
 		if (sta_info_buffer_expired(local, sta, skb)) {

FWIW, I think I prefer the "for (;;)" version for endless loops. It looks more intentional to me. Some grep'ing showed nearly equal usage of "for (;;)" versus "while (1)". Is there any "official" preference? I don't see anything in CodingStyle about it.

I agree with the other cleanups.

John

Signature · 2 lines
John W. Linville
[email protected]
#2 lore raw
Quoted 2 lines
Quoted 2 lines

Ever heard of while loops?

was more about

Quoted 1 line

"for (; condition ;)".

:)

Quoted 4 lines

FWIW, I think I prefer the "for (;;)" version for endless loops. It looks more intentional to me. Some grep'ing showed nearly equal usage of "for (;;)" versus "while (1)". Is there any "official" preference? I don't see anything in CodingStyle about it.

I think while (1) looks more intentional, but I suppose that's because I grew up with pascal where you can't do all these weird things with for loops.

Feel free to drop this part of the patch if you wish, or, if you want, I can resend without that too :)

johannes

#3 lore raw
Quoted 11 lines · Johannes Berg
Quoted 4 lines

FWIW, I think I prefer the "for (;;)" version for endless loops. It looks more intentional to me. Some grep'ing showed nearly equal usage of "for (;;)" versus "while (1)". Is there any "official" preference? I don't see anything in CodingStyle about it.

I think while (1) looks more intentional, but I suppose that's because I grew up with pascal where you can't do all these weird things with for loops.

Feel free to drop this part of the patch if you wish, or, if you want, I can resend without that too :)

No need to resend. Any other opinions on this? I suppose it is just a beached discussion...

Signature · 2 lines
John W. Linville
[email protected]
#4 lore raw
Quoted 2 lines · John W. Linville

No need to resend. Any other opinions on this? I suppose it is just a beached discussion...

What the heck was I typing?

"a bikeshed discussion"

Signature · 2 lines
John W. Linville
[email protected]
#5 lore raw
In reply to John W. Linville
Quoted 10 lines · John W. Linville
Quoted 9 lines · Johannes Berg

Remove things like "for (;;)" or "for (; condition ;)". Ever heard of while loops?

net/d80211/sta_info.c +0 -0
@@ -299,7 +299,7 @@ static void sta_info_cleanup_expire_buff
 	if (skb_queue_empty(&sta->ps_tx_buf))
 		return;
 
Quoted 10 lines

net/d80211/sta_info.c static void sta_info_cleanup_expire_buff · line 305

Quoted 5 lines
net/d80211/sta_info.c +1 -1
-	for (;;) {
+	while (1) {
 		spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
 		skb = skb_peek(&sta->ps_tx_buf);
 		if (sta_info_buffer_expired(local, sta, skb)) {

FWIW, I think I prefer the "for (;;)" version for endless loops. It looks more intentional to me. Some grep'ing showed nearly equal usage of "for (;;)" versus "while (1)". Is there any "official" preference? I don't see anything in CodingStyle about it.

I don't know about official preference, but in ages past, using "for(;;)" was preferable because "while(1)" would often invoke a "statement has no effect" warning from the compiler. gcc 4.0.2 doesn't have this problem, but IIRC it used to.

FWIW, I prefer "for(;;)" because of painful memories with other people's code and -Werror, but I don't barf at "while(1)".

#6 lore raw
In reply to Johannes Berg
Quoted 2 lines · Johannes Berg

Remove things like "for (;;)" or "for (; condition ;)". Ever heard of while loops?

Isn't "for (;;)" the standard way of doing infinite loops? Please don't change that. Changing loops to use list_for_each*() is of course welcome.

Signature · 1 line
Jouni Malinen                                            PGP id EFC895FA
#7 lore raw
Quoted 2 lines · Jouni Malinen

Isn't "for (;;)" the standard way of doing infinite loops? Please don't change that.

In *BSD it is apparently mandated, but Linux doesn't seem to do that as John pointed out. Personally, I find while (1) much easier to read but really, there's one occurrence so ...

johannes