]> mj.ucw.cz Git - pciutils.git/commitdiff
Improve large BAR display
authorMatthew Wilcox <matthew@wil.cx>
Fri, 20 Nov 2009 21:30:50 +0000 (14:30 -0700)
committerMartin Mares <mj@ucw.cz>
Sun, 22 Nov 2009 17:16:39 +0000 (18:16 +0100)
lspci currently displays anything larger than 2GB as a raw number.
I have a device with a larger BAR and wanted to see that reported as
a multiple of a GB.

I took the opportunity to rewrite this routine to make it easier to add
higher powers in the future.  It's also slightly shorter, which is nice.
I also changed it to use the binary prefixes, much as I dislike them
personally.

Signed-off-by: Matthew Wilcox <willy@linux.intel.com>
lspci.c

diff --git a/lspci.c b/lspci.c
index e453f1bfb23d1855c559175db3319d0f4c56163b..ce00316923df3b089bd2d1cc0cc666b9998777e0 100644 (file)
--- a/lspci.c
+++ b/lspci.c
@@ -330,18 +330,16 @@ show_terse(struct device *d)
 static void
 show_size(pciaddr_t x)
 {
+  static const char suffix[][4] = { "", "KiB", "MiB", "GiB", "TiB" };
+  unsigned i;
   if (!x)
     return;
-  printf(" [size=");
-  if (x < 1024)
-    printf("%d", (int) x);
-  else if (x < 1048576)
-    printf("%dK", (int)(x / 1024));
-  else if (x < 0x80000000)
-    printf("%dM", (int)(x / 1048576));
-  else
-    printf(PCIADDR_T_FMT, x);
-  putchar(']');
+  for (i = 0; i < (sizeof(suffix) / sizeof(*suffix) - 1); i++) {
+    if (x < 1024)
+      break;
+    x /= 1024;
+  }
+  printf(" [size=%u%s]", (unsigned)x, suffix[i]);
 }
 
 static void