[PATCH 14/15] libmultipath: Micro-optimize snprint_size()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Eliminate the stack array fmt[]. An interesting side effect of
this patch is that it makes it possible for the compiler to
verify whether the snprintf() arguments have a type that is
appropriate for the format string.

Signed-off-by: Bart Van Assche <bart.vanassche@xxxxxxxxxxx>
---
 libmultipath/print.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/libmultipath/print.c b/libmultipath/print.c
index 94d6384..9aa41ad 100644
--- a/libmultipath/print.c
+++ b/libmultipath/print.c
@@ -73,7 +73,6 @@ static int
 snprint_size (char * buff, size_t len, unsigned long long size)
 {
 	float s = (float)(size >> 1); /* start with KB */
-	char fmt[6] = {};
 	char units[] = {'K','M','G','T','P'};
 	char *u = units;
 
@@ -81,12 +80,8 @@ snprint_size (char * buff, size_t len, unsigned long long size)
 		s = s / 1024;
 		u++;
 	}
-	if (s < 10)
-		snprintf(fmt, 6, "%%.1f%c", *u);
-	else
-		snprintf(fmt, 6, "%%.0f%c", *u);
 
-	return snprintf(buff, len, fmt, s);
+	return snprintf(buff, len, "%.*f%c", s < 10, s, *u);
 }
 
 /*
-- 
2.10.0

--
dm-devel mailing list
dm-devel@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/dm-devel



[Index of Archives]     [DM Crypt]     [Fedora Desktop]     [ATA RAID]     [Fedora Marketing]     [Fedora Packaging]     [Fedora SELinux]     [Yosemite Discussion]     [KDE Users]     [Fedora Docs]

  Powered by Linux