[PATCH 03/10] use continue statement to simplify "for each PCI device" loop

Jonathan Nieder jrnieder at gmail.com
Fri Feb 24 19:47:13 PST 2012


In the construct

	for each PCI device:
		if PCI ID is interesting:
			long block of code to handle
			the detected device

the "if" with long body leaves the reader in suspense about what will
happen when the PCI ID is not interesting.  Rewriting as

	for each PCI device:
		if PCI ID is uninteresting:
			continue

		handle the detected device

deals with the easy case right away and decreases nesting for the bulk
of the "while" loop body as a nice side effect.  No change in
functionality intended.

Signed-off-by: Jonathan Nieder <jrnieder at gmail.com>
---
 avivotool.c  |   62 ++++++++++++++++++++++++++++++----------------------------
 radeonreg.c  |   28 ++++++++++++++------------
 radeontool.c |   54 ++++++++++++++++++++++++++------------------------
 3 files changed, 75 insertions(+), 69 deletions(-)

diff --git a/avivotool.c b/avivotool.c
index 9a01a9fd..f08ab2d7 100644
--- a/avivotool.c
+++ b/avivotool.c
@@ -1825,40 +1825,42 @@ static int map_radeon_mem(void)
 
     while ((device = pci_device_next(iter))) {
         pci_device_probe(device);
-        if (device->vendor_id == 0x1002 &&
-            (((device->device_class & 0x00ffff00) == 0x00030000) ||
-            ((device->device_class & 0x00ffff00) == 0x00038000))) {
-	    for (i = 0; i < sizeof(RADEONCards) / sizeof(RADEONCardInfo); i++) {
-		if (RADEONCards[i].pci_device_id == device->device_id)
-		    card_info = &RADEONCards[i];
-	    }
+        if (device->vendor_id != 0x1002)
+            continue;
+        if ((device->device_class & 0x00ffff00) != 0x00030000 &&
+            (device->device_class & 0x00ffff00) != 0x00038000)
+            continue;
 
-            if (debug) {
-                printf("Found card %x:%x (%x)\n", device->vendor_id,
-                       device->device_id, device->device_class);
+        for (i = 0; i < sizeof(RADEONCards) / sizeof(RADEONCardInfo); i++) {
+            if (RADEONCards[i].pci_device_id == device->device_id)
+                card_info = &RADEONCards[i];
+        }
 
+        if (debug) {
+            printf("Found card %x:%x (%x)\n", device->vendor_id,
+                   device->device_id, device->device_class);
 
-		if (card_info)
-		  printf("Found card %x %s %s %s\n", card_info->pci_device_id,
-			 family_strings[card_info->chip_family],
-			 card_info->mobility ? "mobile" : "",
-			 card_info->igp ? "igp" : "");
-            }
 
-            for (i = 0; i < 6; i++) {
-                if (device->regions[i].size == 64 * 1024)
-                    ctrl_region = i;
-                else if (device->regions[i].size == 128 * 1024)
-                    ctrl_region = i;
-                else if (device->regions[i].size == 256 * 1024)
-                    ctrl_region = i;
-                else if (device->regions[i].size >= 128 * 1024 * 1024)
-                    fb_region = i;
-            }
-            avivo_device = device;
-            if(skip-- == 0) {
-                break;
-            }
+            if (card_info)
+                printf("Found card %x %s %s %s\n", card_info->pci_device_id,
+                       family_strings[card_info->chip_family],
+                       card_info->mobility ? "mobile" : "",
+                       card_info->igp ? "igp" : "");
+        }
+
+        for (i = 0; i < 6; i++) {
+            if (device->regions[i].size == 64 * 1024)
+                ctrl_region = i;
+            else if (device->regions[i].size == 128 * 1024)
+                ctrl_region = i;
+            else if (device->regions[i].size == 256 * 1024)
+                ctrl_region = i;
+            else if (device->regions[i].size >= 128 * 1024 * 1024)
+                fb_region = i;
+        }
+        avivo_device = device;
+        if(skip-- == 0) {
+            break;
         }
     }
 
diff --git a/radeonreg.c b/radeonreg.c
index 4d016fd6..d5b4aa23 100644
--- a/radeonreg.c
+++ b/radeonreg.c
@@ -391,20 +391,22 @@ static int map_radeon_mem(void)
 
     while ((device = pci_device_next(iter))) {
         pci_device_probe(device);
-        if (device->vendor_id == 0x1002 &&
-            (((device->device_class & 0x00ffff00) == 0x00030000) ||
-            ((device->device_class & 0x00ffff00) == 0x00038000))) {
-	    for (i = 0; i < sizeof(RADEONCards) / sizeof(RADEONCardInfo); i++) {
-		if (RADEONCards[i].pci_device_id == device->device_id)
-		    card_info = &RADEONCards[i];
-	    }
+        if (device->vendor_id != 0x1002)
+            continue;
+        if ((device->device_class & 0x00ffff00) != 0x00030000 &&
+            (device->device_class & 0x00ffff00) != 0x00038000)
+            continue;
 
-	    fb_region = 0;
-	    ctrl_region = 2;
-            avivo_device = device;
-            if(skip-- == 0) {
-                break;
-            }
+        for (i = 0; i < sizeof(RADEONCards) / sizeof(RADEONCardInfo); i++) {
+            if (RADEONCards[i].pci_device_id == device->device_id)
+                card_info = &RADEONCards[i];
+        }
+
+        fb_region = 0;
+        ctrl_region = 2;
+        avivo_device = device;
+        if(skip-- == 0) {
+            break;
         }
     }
 
diff --git a/radeontool.c b/radeontool.c
index d2592545..ecf13d10 100644
--- a/radeontool.c
+++ b/radeontool.c
@@ -932,35 +932,37 @@ static void map_radeon_cntl_mem(void)
 
     while ((device = pci_device_next(iter))) {
         pci_device_probe(device);
-        if (device->vendor_id == 0x1002 &&
-            (device->device_class & 0x00ffff00) == 0x00030000) {
-            if (debug) {
-                printf("Found card %x:%x (%x)\n", device->vendor_id,
-                       device->device_id, device->device_class);
-            }
+        if (device->vendor_id != 0x1002)
+            continue;
+        if ((device->device_class & 0x00ffff00) != 0x00030000)
+            continue;
 
-            if (skip-- != 0) {
-                continue;
-            }
-            for (i = 0; i < 6; i++) {
-                if (device->regions[i].size >= 16 * 1024 &&
-                    device->regions[i].size <= 64 * 1024) {
-                    if (ctrl_region != -1)
-                        die("cannot distinguish ctrl region");
-                    ctrl_region = i;
-                } else if (device->regions[i].size >= 64 * 1024 * 1024) {
-                    if (fb_region != -1)
-                        die("cannot distinguish fb region");
-                    fb_region = i;
-                }
+        if (debug) {
+            printf("Found card %x:%x (%x)\n", device->vendor_id,
+                   device->device_id, device->device_class);
+        }
+
+        if (skip-- != 0) {
+            continue;
+        }
+        for (i = 0; i < 6; i++) {
+            if (device->regions[i].size >= 16 * 1024 &&
+                device->regions[i].size <= 64 * 1024) {
+                if (ctrl_region != -1)
+                    die("cannot distinguish ctrl region");
+                ctrl_region = i;
+            } else if (device->regions[i].size >= 64 * 1024 * 1024) {
+                if (fb_region != -1)
+                    die("cannot distinguish fb region");
+                fb_region = i;
             }
-            if (ctrl_region == -1)
-                die("cannot find ctrl region");
-            if (fb_region == -1)
-                die("cannot find fb region");
-            avivo_device = device;
-            break;
         }
+        if (ctrl_region == -1)
+            die("cannot find ctrl region");
+        if (fb_region == -1)
+            die("cannot find fb region");
+        avivo_device = device;
+        break;
     }
 
     if (!avivo_device)
-- 
1.7.9.2



More information about the xorg-driver-ati mailing list